// END-BayesInference // // // PROTECTED METHODS // // function ENUMERATE-ALL(vars, e) returns a real number protected double enumerateAll(ICollection <IRandomVariable> vars, ObservedEvidence e) { // if EMPTY?(vars) then return 1.0 if (0 == vars.Size()) { return(1); } // Y <- FIRST(vars) IRandomVariable 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 ((IFiniteDomain)Y.getDomain()).GetPossibleValues()) { e.setExtendedValue(Y, y); sum += e.posteriorForParents(Y) * enumerateAll(Util.rest(vars), e); } return(sum); }
public CPT(IRandomVariable on, double[] values, params IRandomVariable[] conditionedOn) { this.on = on; if (null == conditionedOn) { conditionedOn = new IRandomVariable[0]; } IRandomVariable[] tableVars = new IRandomVariable[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.AddAll(((IFiniteDomain)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, 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 ICategoricalDistribution backward(ICategoricalDistribution b_kp2t, ICollection <AssignmentProposition> e_kp1) { ICategoricalDistribution b_kp1t = new ProbabilityTable(b_kp2t.getFor()); // Set up required working variables IProposition[] props = new IProposition[b_kp1t.getFor().Size()]; int i = 0; foreach (IRandomVariable rv in b_kp1t.getFor()) { IRandomVariable prv = tToTm1StateVarMap.Get(rv); props[i] = new RandVar(prv.getName(), prv.getDomain()); ++i; } IProposition Xk = ProbUtil.constructConjunction(props); AssignmentProposition[] ax_kp1 = new AssignmentProposition[tToTm1StateVarMap.Size()]; IMap <IRandomVariable, AssignmentProposition> x_kp1VarAssignMap = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, AssignmentProposition>(); i = 0; foreach (IRandomVariable rv in b_kp1t.getFor()) { ax_kp1[i] = new AssignmentProposition(rv, "<Dummy Value>"); x_kp1VarAssignMap.Put(rv, ax_kp1[i]); ++i; } IProposition x_kp1 = ProbUtil.constructConjunction(ax_kp1); props = e_kp1.ToArray(); IProposition pe_kp1 = ProbUtil.constructConjunction(props); // ∑<sub>x<sub>k+1</sub></sub> CategoricalDistributionIterator ib_kp2t = new CategoricalDistributionIteratorImpl2(x_kp1VarAssignMap, sensorModel, transitionModel, b_kp1t, pe_kp1, Xk, x_kp1); b_kp2t.iterateOver(ib_kp2t); return(b_kp1t); }
/// <summary> /// Instantiate a Hidden Markov Model. /// </summary> /// <param name="stateVariable"> /// the single discrete random variable used to describe the process states 1,...,S. /// </param> /// <param name="transitionModel"> /// the transition model:<para /> /// P(Xt | Xt-1)<para /> /// is represented by an S * S matrix T where /// Tij= P(Xt = j | Xt-1 = i). /// </param> /// <param name="sensorModel"> /// the sensor model in matrix form:<para /> /// P(et | Xt = i) for each state i. For /// mathematical convenience we place each of these values into an /// S * S diagonal matrix. /// </param> /// <param name="prior">the prior distribution represented as a column vector in Matrix form.</param> public HiddenMarkovModel(IRandomVariable stateVariable, Matrix transitionModel, IMap <object, Matrix> sensorModel, Matrix prior) { if (!stateVariable.getDomain().IsFinite()) { throw new IllegalArgumentException("State Variable for HHM must be finite."); } this.stateVariable = stateVariable; stateVariableDomain = (IFiniteDomain)stateVariable.getDomain(); if (transitionModel.GetRowDimension() != transitionModel .GetColumnDimension()) { throw new IllegalArgumentException("Transition Model row and column dimensions must match."); } if (stateVariableDomain.Size() != transitionModel.GetRowDimension()) { throw new IllegalArgumentException("Transition Model Matrix does not map correctly to the HMM's State Variable."); } this.transitionModel = transitionModel; foreach (Matrix smVal in sensorModel.GetValues()) { if (smVal.GetRowDimension() != smVal.GetColumnDimension()) { throw new IllegalArgumentException("Sensor Model row and column dimensions must match."); } if (stateVariableDomain.Size() != smVal.GetRowDimension()) { throw new IllegalArgumentException("Sensor Model Matrix does not map correctly to the HMM's State Variable."); } } this.sensorModel = sensorModel; if (transitionModel.GetRowDimension() != prior.GetRowDimension() && prior.GetColumnDimension() != 1) { throw new IllegalArgumentException("Prior is not of the correct dimensions."); } this.prior = prior; }
public RVInfo(IRandomVariable rv) { variable = rv; varDomain = (IFiniteDomain)variable.getDomain(); }