public ProbabilityTable sumOut(params IRandomVariable[] vars) { ISet <IRandomVariable> soutVars = CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys()); foreach (IRandomVariable rv in vars) { soutVars.Remove(rv); } ProbabilityTable summedOut = new ProbabilityTable(soutVars); if (1 == summedOut.getValues().Length) { summedOut.getValues()[0] = getSum(); } else { // Otherwise need to iterate through this distribution // to calculate the summed out distribution. object[] termValues = new object[summedOut.randomVarInfo.Size()]; ProbabilityTableIterator di = new ProbabilityTableIteratorImpl(summedOut, termValues); iterateOverTable(di); } return(summedOut); }
// function ENUMERATION-ASK(X, e, bn) returns a distribution over X /** * The ENUMERATION-ASK algorithm in Figure 14.9 evaluates expression trees * (Figure 14.8) using depth-first recursion. * * @param X * the query variables. * @param observedEvidence * observed values for variables E. * @param bn * a Bayes net with variables {X} ∪ E ∪ Y /* Y = hidden * variables // * @return a distribution over the query variables. */ public ICategoricalDistribution enumerationAsk(IRandomVariable[] X, AssignmentProposition[] observedEvidence, IBayesianNetwork bn) { // Q(X) <- a distribution over X, initially empty ProbabilityTable Q = new ProbabilityTable(X); ObservedEvidence e = new ObservedEvidence(X, observedEvidence, bn); // for each value x<sub>i</sub> of X do ProbabilityTable.ProbabilityTableIterator di = new ProbabilityTableIteratorImpl(bn, Q, e, X, this); Q.iterateOverTable(di); // return NORMALIZE(Q(X)) return(Q.normalize()); }