/** * 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(Node Xi, Map <RandomVariable, Object> evt) { FiniteDomain fd = (FiniteDomain)Xi.getRandomVariable().getDomain(); double[] X = new double[fd.size()]; 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>)) double cprob = 1.0; foreach (Node Yj in Xi.getChildren()) { cprob *= Yj.getCPD().getValue( getEventValuesForXiGivenParents(Yj, evt)); } X[i] = Xi.getCPD() .getValue( getEventValuesForXiGivenParents(Xi, fd.getValueAt(i), evt)) * cprob; } return(Util.normalize(X)); }
/** * 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)); }