/** * Calculate the probability distribution for <b>P</b>(X<sub>i</sub> | * mb(X<sub>i</sub>)), where mb(X<sub>i</sub>) is the Markov Blanket of * X<sub>i</sub>. The probability of a variable given its Markov blanket is * proportional to the probability of the variable given its parents times * the probability of each child given its respective parents (see equation * 14.12 pg. 538 AIMA3e):<br> * <br> * P(x'<sub>i</sub>|mb(Xi)) = * αP(x'<sub>i</sub>|parents(X<sub>i</sub>)) * * ∏<sub>Y<sub>j</sub> ∈ Children(X<sub>i</sub>)</sub> * P(y<sub>j</sub>|parents(Y<sub>j</sub>)) * * @param Xi * a Node from a Bayesian network for the Random Variable * X<sub>i</sub>. * @param event * comprising assignments for the Markov Blanket X<sub>i</sub>. * @return a random sample from <b>P</b>(X<sub>i</sub> | mb(X<sub>i</sub>)) */ public static double[] mbDistribution(INode Xi, IMap <IRandomVariable, object> even) { IFiniteDomain fd = (IFiniteDomain)Xi.GetRandomVariable().getDomain(); double[] X = new double[fd.Size()]; /** * As we iterate over the domain of a ramdom variable corresponding to Xi * it is necessary to make the modified values of the variable visible * to the child nodes of Xi in the computation of the markov blanket * probabilities. */ //Copy contents of event to generatedEvent so as to leave event untouched IMap <IRandomVariable, object> generatedEvent = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>(); foreach (var entry in even) { generatedEvent.Put(entry.GetKey(), entry.GetValue()); } for (int i = 0; i < fd.Size(); ++i) { /** P(x'<sub>i</sub>|mb(Xi)) = * αP(x'<sub>i</sub>|parents(X<sub>i</sub>)) * * ∏<sub>Y<sub>j</sub> ∈ Children(X<sub>i</sub>)</sub> * P(y<sub>j</sub>|parents(Y<sub>j</sub>)) */ generatedEvent.Put(Xi.GetRandomVariable(), fd.GetValueAt(i)); double cprob = 1.0; foreach (INode Yj in Xi.GetChildren()) { cprob *= Yj.GetCPD().GetValue( getEventValuesForXiGivenParents(Yj, generatedEvent)); } X[i] = Xi.GetCPD() .GetValue( getEventValuesForXiGivenParents(Xi, fd.GetValueAt(i), even)) * cprob; } return(Util.normalize(X)); }
/** * * @param probabilityChoice * a probability choice for the sample * @param Xi * a Random Variable with a finite domain from which a random * sample is to be chosen based on the probability choice. * @param distribution * Xi's distribution. * @return a Random Sample from Xi's domain. */ public static object sample(double probabilityChoice, IRandomVariable Xi, double[] distribution) { IFiniteDomain fd = (IFiniteDomain)Xi.getDomain(); if (fd.Size() != distribution.Length) { throw new IllegalArgumentException("Size of domain Xi " + fd.Size() + " is not equal to the size of the distribution " + distribution.Length); } int i = 0; double total = distribution[0]; while (probabilityChoice > total) { ++i; total += distribution[i]; } return(fd.GetValueAt(i)); }
public object getDomainValueAt(int idx) { return(varDomain.GetValueAt(idx)); }