// END-BayesInference // // // PROTECTED METHODS // // function ENUMERATE-ALL(vars, e) returns a real number protected double enumerateAll(List <RandomVariable> vars, ObservedEvidence e) { // if EMPTY?(vars) then return 1.0 if (0 == vars.Count) { return(1); } // Y <- FIRST(vars) RandomVariable Y = Util.first(vars); // if Y has value y in e if (e.containsValue(Y)) { // then return P(y | parents(Y)) * ENUMERATE-ALL(REST(vars), e) return(e.posteriorForParents(Y) * enumerateAll(Util.rest(vars), e)); } /** * <pre> * else return ∑<sub>y</sub> P(y | parents(Y)) * ENUMERATE-ALL(REST(vars), e<sub>y</sub>) * where e<sub>y</sub> is e extended with Y = y * </pre> */ double sum = 0; foreach (Object y in ((FiniteDomain)Y.getDomain()).getPossibleValues()) { e.setExtendedValue(Y, y); sum += e.posteriorForParents(Y) * enumerateAll(Util.rest(vars), e); } return(sum); }
public CPT(RandomVariable on, double[] values, params RandomVariable [] conditionedOn) { this.on = on; if (null == conditionedOn) { conditionedOn = new RandomVariable[0]; } RandomVariable[] tableVars = new RandomVariable[conditionedOn.Length + 1]; for (int i = 0; i < conditionedOn.Length; i++) { tableVars[i] = conditionedOn[i]; parents.add(conditionedOn[i]); } tableVars[conditionedOn.Length] = on; table = new ProbabilityTable(values, tableVars); onDomain.AddRange(((FiniteDomain) on.getDomain()).getPossibleValues()); checkEachRowTotalsOne(); }
public CPT(RandomVariable on, double[] values, params RandomVariable [] conditionedOn) { this.on = on; if (null == conditionedOn) { conditionedOn = new RandomVariable[0]; } RandomVariable[] tableVars = new RandomVariable[conditionedOn.Length + 1]; for (int i = 0; i < conditionedOn.Length; i++) { tableVars[i] = conditionedOn[i]; parents.add(conditionedOn[i]); } tableVars[conditionedOn.Length] = on; table = new ProbabilityTable(values, tableVars); onDomain.AddRange(((FiniteDomain)on.getDomain()).getPossibleValues()); checkEachRowTotalsOne(); }
/** * * @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, RandomVariable Xi, double[] distribution) { FiniteDomain fd = (FiniteDomain)Xi.getDomain(); if (fd.size() != distribution.Length) { throw new ArgumentException("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)); }
/** * * @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, RandomVariable Xi, double[] distribution) { FiniteDomain fd = (FiniteDomain) Xi.getDomain(); if (fd.size() != distribution.Length) { throw new ArgumentException("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 RVInfo(RandomVariable rv) { variable = rv; varDomain = (FiniteDomain)variable.getDomain(); }
public RVInfo(RandomVariable rv) { variable = rv; varDomain = (FiniteDomain) variable.getDomain(); }