AIMA.Core.Probability.BayesNet.probabilityOf C# (CSharp) Method

probabilityOf() public method

public probabilityOf ( String Y, bool value, bool>.Dictionary evidence ) : double
Y String
value bool
evidence bool>.Dictionary
return double
        public double probabilityOf(String Y, bool value,
                Dictionary<String, bool> evidence)
        {
            BayesNetNode y = getNodeOf(Y);
            if (y == null)
            {
                throw new ApplicationException("Unable to find a node with variable "
                        + Y);
            }
            else
            {
                List<BayesNetNode> parentNodes = y.getParents();
                if (parentNodes.Count == 0)
                {// root nodes
                    Dictionary<String, bool> YTable = new Dictionary<String, bool>();
                    YTable.Add(Y, value);

                    double prob = y.probabilityOf(YTable);
                    return prob;

                }
                else
                {// non rootnodes
                    Dictionary<String, bool> parentValues = new Dictionary<String, bool>();
                    foreach (BayesNetNode parent in parentNodes)
                    {
                        parentValues.Add(parent.getVariable(), evidence[parent
                                .getVariable()]);
                    }
                    double prob = y.probabilityOf(parentValues);
                    if (value.Equals(true))
                    {
                        return prob;
                    }
                    else
                    {
                        return (1.0 - prob);
                    }

                }

            }
        }

Usage Example

Beispiel #1
0
        private static double enumerateAll(BayesNet net, List<string> unprocessedVariables,
                Dictionary<String, bool> evidenceVariables)
        {
            if (unprocessedVariables.Count == 0)
            {

                return 1.0;
            }
            else
            {
                String Y = (String)unprocessedVariables[0];

                if (evidenceVariables.ContainsKey(Y))
                {

                    double probYGivenParents = net.probabilityOf(Y,
                            evidenceVariables[Y], evidenceVariables);

                    double secondTerm = enumerateAll(net, Util
                            .rest(unprocessedVariables), evidenceVariables);

                    return probYGivenParents * secondTerm;
                }
                else
                {
                    double sigma = 0.0;
                    Dictionary<String, bool> clone1 = cloneEvidenceVariables(evidenceVariables);
                    clone1.Add(Y, true);
                    double probYTrueGivenParents = net.probabilityOf(Y,
                            true, clone1);

                    double secondTerm = enumerateAll(net, Util
                            .rest(unprocessedVariables), clone1);

                    double trueProbabilityY = probYTrueGivenParents * secondTerm;

                    Dictionary<String, bool> clone2 = cloneEvidenceVariables(evidenceVariables);
                    clone2.Add(Y, false);
                    double probYFalseGivenParents = net.probabilityOf(Y,
                            false, clone2);

                    secondTerm = enumerateAll(net, Util.rest(unprocessedVariables),
                            clone2);
                    double falseProbabilityY = probYFalseGivenParents * secondTerm;
                    // System.Console.Write(secondTerm + " ) )");
                    sigma = trueProbabilityY + falseProbabilityY;
                    return sigma;

                }
            }
        }
All Usage Examples Of AIMA.Core.Probability.BayesNet::probabilityOf