Exemplo n.º 1
0
 public MapVariable(
     IRandomVariable <TIn> input,
     Func <TIn, TOut> func)
 {
     _input = input;
     _func  = func;
 }
Exemplo n.º 2
0
        // 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 &sum;<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);
        }
Exemplo n.º 3
0
 public void resetBiases(IRandomVariable rv)
 {
     for (int j = 0; j < outputs; j++)
     {
         biases[j] = rv.next();
     }
 }
Exemplo n.º 4
0
        public virtual ICategoricalDistribution jointDistribution(params IProposition[] propositions)
        {
            ProbabilityTable       d        = null;
            IProposition           conjProp = ProbUtil.constructConjunction(propositions);
            ISet <IRandomVariable> vars     = CollectionFactory.CreateSet <IRandomVariable>(conjProp.getUnboundScope());

            if (vars.Size() > 0)
            {
                IRandomVariable[] distVars = new IRandomVariable[vars.Size()];
                int i = 0;
                foreach (IRandomVariable rv in vars)
                {
                    distVars[i] = rv;
                    ++i;
                }

                ProbabilityTable ud     = new ProbabilityTable(distVars);
                object[]         values = new object[vars.Size()];

                CategoricalDistributionIterator di = new CategoricalDistributionIteratorJointDistribution(conjProp, vars, ud, values);

                IRandomVariable[] X = conjProp.getScope().ToArray();
                bayesInference.Ask(X, new AssignmentProposition[0], bayesNet).iterateOver(di);

                d = ud;
            }
            else
            {
                // No Unbound Variables, therefore just return
                // the singular probability related to the proposition.
                d = new ProbabilityTable();
                d.setValue(0, prior(propositions));
            }
            return(d);
        }
Exemplo n.º 5
0
        public DynamicBayesNet(IBayesianNetwork priorNetwork,
                               IMap <IRandomVariable, IRandomVariable> X_0_to_X_1,
                               ISet <IRandomVariable> E_1, params INode[] rootNodes)
            : base(rootNodes)
        {
            foreach (var x0_x1 in X_0_to_X_1)
            {
                IRandomVariable x0 = x0_x1.GetKey();
                IRandomVariable x1 = x0_x1.GetValue();
                this.X_0.Add(x0);
                this.X_1.Add(x1);
                this.X_0_to_X_1.Put(x0, x1);
                this.X_1_to_X_0.Put(x1, x0);
            }
            this.E_1.AddAll(E_1);

            // Assert the X_0, X_1, and E_1 sets are of expected sizes
            ISet <IRandomVariable> combined = CollectionFactory.CreateSet <IRandomVariable>();

            combined.AddAll(X_0);
            combined.AddAll(X_1);
            combined.AddAll(E_1);
            if (SetOps.difference(CollectionFactory.CreateSet <IRandomVariable>(varToNodeMap.GetKeys()), combined).Size() != 0)
            {
                throw new IllegalArgumentException("X_0, X_1, and E_1 do not map correctly to the Nodes describing this Dynamic Bayesian Network.");
            }
            this.priorNetwork = priorNetwork;

            X_1_VariablesInTopologicalOrder.AddAll(GetVariablesInTopologicalOrder());
            X_1_VariablesInTopologicalOrder.RemoveAll(X_0);
            X_1_VariablesInTopologicalOrder.RemoveAll(E_1);
        }
Exemplo n.º 6
0
        /**
         * Reset this instances persistent variables to be used between called to
         * particleFiltering().
         *
         * @param N
         *            the number of samples to be maintained
         * @param dbn
         *            a DBN with prior <b>P</b>(<b>X</b><sub>0</sub>), transition
         *            model <b>P</b>(<b>X</b><sub>1</sub> | <b>X</b><sub>0</sub>),
         *            sensor model <b>P</b>(<b>E</b><sub>1</sub> |
         *            <b>X</b><sub>1</sub>)
         */
        public void initPersistent(int N, IDynamicBayesianNetwork dbn)
        {
            this.N   = N;
            this.dbn = dbn;
            // persistent: S, a vector of samples of size N, initially generated
            // from <b>P</b>(<b>X</b><sub>0</sub>)
            S     = new AssignmentProposition[N][];
            S_tp1 = new AssignmentProposition[N][];
            int[] indexes = new int[N];
            for (int i = 0; i < N; ++i)
            {
                S[i]       = new AssignmentProposition[this.dbn.GetX_0().Size()];
                S_tp1[i]   = new AssignmentProposition[this.dbn.GetX_0().Size()];
                indexes[i] = i;
                IMap <IRandomVariable, object> sample = priorSampler.priorSample(this.dbn.GetPriorNetwork());
                int idx = 0;
                foreach (var sa in sample)
                {
                    S[i][idx]     = new AssignmentProposition(this.dbn.GetX_0_to_X_1().Get(sa.GetKey()), sa.GetValue());
                    S_tp1[i][idx] = new AssignmentProposition(this.dbn.GetX_0_to_X_1().Get(sa.GetKey()), sa.GetValue());
                    idx++;
                }
            }

            sensorModel   = new FiniteBayesModel(dbn, new EliminationAsk());
            sampleIndexes = new RandVar("SAMPLE_INDEXES", new FiniteIntegerDomain(indexes));
        }
Exemplo n.º 7
0
        public void testGibbsAsk_compare()
        {
            // create two nodes: parent and child with an arc from parent to child
            IRandomVariable rvParent   = new RandVar("Parent", new BooleanDomain());
            IRandomVariable rvChild    = new RandVar("Child", new BooleanDomain());
            FullCPTNode     nodeParent = new FullCPTNode(rvParent, new double[] { 0.7, 0.3 });

            new FullCPTNode(rvChild, new double[] { 0.8, 0.2, 0.2, 0.8 }, nodeParent);

            // create net
            BayesNet net = new BayesNet(nodeParent);

            // query parent probability
            IRandomVariable[] rvX = new IRandomVariable[] { rvParent };

            // ...given child evidence (true)
            AssignmentProposition[] propE = new AssignmentProposition[] { new AssignmentProposition(rvChild, true) };

            // sample with LikelihoodWeighting
            ICategoricalDistribution samplesLW = new LikelihoodWeighting().Ask(rvX, propE, net, 1000);

            Assert.AreEqual(0.9, samplesLW.getValue(true), DELTA_THRESHOLD);

            // sample with RejectionSampling
            ICategoricalDistribution samplesRS = new RejectionSampling().Ask(rvX, propE, net, 1000);

            Assert.AreEqual(0.9, samplesRS.getValue(true), DELTA_THRESHOLD);

            // sample with GibbsAsk
            ICategoricalDistribution samplesGibbs = new GibbsAsk().Ask(rvX, propE, net, 1000);

            Assert.AreEqual(0.9, samplesGibbs.getValue(true), DELTA_THRESHOLD);
        }
Exemplo n.º 8
0
        public override bool holds(IMap <IRandomVariable, object> possibleWorld)
        {
            bool holds = true;
            bool first = true;

            IRandomVariable rvL = null;

            foreach (IRandomVariable rvC in getScope())
            {
                if (first)
                {
                    rvL   = rvC;
                    first = false;
                    continue;
                }
                if (!possibleWorld.Get(rvL).Equals(possibleWorld.Get(rvC)))
                {
                    holds = false;
                    break;
                }
                rvL = rvC;
            }

            return(holds);
        }
 public AbstractTermProposition(IRandomVariable var)
 {
     if (null == var)
     {
         throw new IllegalArgumentException("The Random Variable for the Term must be specified.");
     }
     this.termVariable = var;
     addScope(this.termVariable);
 }
Exemplo n.º 10
0
 public SymmetricConfidenceInterval(IRandomVariable <T> rv, double confidence)
 {
     if (confidence < 0 || confidence > 1)
     {
         throw new ArgumentException("Confidence must be between 0 and 1");
     }
     alpha   = 1 - confidence;
     this.rv = rv;
 }
Exemplo n.º 11
0
 public void resetWeights(IRandomVariable rv)
 {
     for (int i = 0; i < inputs; i++)
     {
         for (int j = 0; j < outputs; j++)
         {
             weights[j, i] = rv.next();
         }
     }
 }
Exemplo n.º 12
0
 public SubsetProposition(string name, IFiniteDomain subsetDomain, IRandomVariable ofVar)
     : base(name)
 {
     if (null == subsetDomain)
     {
         throw new IllegalArgumentException("Sum Domain must be specified.");
     }
     this.subsetDomain = subsetDomain;
     this.varSubsetOf  = ofVar;
     addScope(this.varSubsetOf);
 }
Exemplo n.º 13
0
        public void test_indexesOfValue()
        {
            RandVar X = new RandVar("X", new BooleanDomain());
            RandVar Y = new RandVar("Y", new ArbitraryTokenDomain("A", "B", "C"));
            RandVar Z = new RandVar("Z", new BooleanDomain());

            // An ordered X,Y,Z enumeration of values should look like:
            // 00: true, A, true
            // 01: true, A, false
            // 02: true, B, true
            // 03: true, B, false
            // 04: true, C, true
            // 05: true, C, false
            // 06: false, A, true
            // 07: false, A, false
            // 08: false, B, true
            // 09: false, B, false
            // 10: false, C, true
            // 11: false, C, false
            IRandomVariable[] vars = new IRandomVariable[] { X, Y, Z };
            IMap <IRandomVariable, object> even = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();


            even.Put(X, true);
            CollectionAssert.AreEqual(new int[] { 0, 1, 2, 3, 4, 5 },
                                      ProbUtil.indexesOfValue(vars, 0, even));

            even.Put(X, false);
            CollectionAssert.AreEqual(new int[] { 6, 7, 8, 9, 10, 11 },
                                      ProbUtil.indexesOfValue(vars, 0, even));


            even.Put(Y, "A");
            CollectionAssert.AreEqual(new int[] { 0, 1, 6, 7 },
                                      ProbUtil.indexesOfValue(vars, 1, even));

            even.Put(Y, "B");
            CollectionAssert.AreEqual(new int[] { 2, 3, 8, 9 },
                                      ProbUtil.indexesOfValue(vars, 1, even));

            even.Put(Y, "C");
            CollectionAssert.AreEqual(new int[] { 4, 5, 10, 11 },
                                      ProbUtil.indexesOfValue(vars, 1, even));


            even.Put(Z, true);
            CollectionAssert.AreEqual(new int[] { 0, 2, 4, 6, 8, 10 },
                                      ProbUtil.indexesOfValue(vars, 2, even));

            even.Put(Z, false);
            CollectionAssert.AreEqual(new int[] { 1, 3, 5, 7, 9, 11 },
                                      ProbUtil.indexesOfValue(vars, 2, even));
        }
Exemplo n.º 14
0
        public FullCPTNode(IRandomVariable var, double[] values, params INode[] parents)
            : base(var, parents)
        {
            IRandomVariable[] conditionedOn = new IRandomVariable[GetParents().Size()];
            int i = 0;

            foreach (INode p in GetParents())
            {
                conditionedOn[i++] = p.GetRandomVariable();
            }

            cpt = new CPT(var, values, conditionedOn);
        }
Exemplo n.º 15
0
            public void setExtendedValue(IRandomVariable rv, object value)
            {
                int idx = varIdxs.Get(rv);

                extendedValues[idx] = value;
                if (idx >= hiddenStart)
                {
                    extendedIdx = idx;
                }
                else
                {
                    extendedIdx = hiddenStart - 1;
                }
            }
Exemplo n.º 16
0
        public override bool Equals(object o)
        {
            if (this == o)
            {
                return(true);
            }
            if (!(o is IRandomVariable))
            {
                return(false);
            }

            // The name (not the name:domain combination) uniquely identifies a Random Variable
            IRandomVariable other = (IRandomVariable)o;

            return(this.name.Equals(other.getName()));
        }
Exemplo n.º 17
0
 public AbstractNode(IRandomVariable var, params INode[] parents)
 {
     if (null == var)
     {
         throw new IllegalArgumentException("Random Variable for Node must be specified.");
     }
     this.variable = var;
     this.parents  = CollectionFactory.CreateSet <INode>();
     if (null != parents)
     {
         foreach (INode p in parents)
         {
             ((AbstractNode)p).addChild(this);
             this.parents.Add(p);
         }
     }
     this.parents  = CollectionFactory.CreateReadOnlySet <INode>(this.parents);
     this.children = CollectionFactory.CreateReadOnlySet <INode>(CollectionFactory.CreateSet <INode>());
 }
Exemplo n.º 18
0
        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();
        }
Exemplo n.º 19
0
        /**
         *
         * @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));
        }
Exemplo n.º 20
0
        //
        // PRIVATE METHODS
        //
        private IFactor makeFactor(IRandomVariable var, AssignmentProposition[] e, IBayesianNetwork bn)
        {
            INode n = bn.GetNode(var);

            if (!(n is IFiniteNode))
            {
                throw new IllegalArgumentException("Elimination-Ask only works with finite Nodes.");
            }
            IFiniteNode fn = (IFiniteNode)n;
            ICollection <AssignmentProposition> evidence = CollectionFactory.CreateQueue <AssignmentProposition>();

            foreach (AssignmentProposition ap in e)
            {
                if (fn.GetCPT().Contains(ap.getTermVariable()))
                {
                    evidence.Add(ap);
                }
            }

            return(fn.GetCPT().GetFactorFor(evidence.ToArray()));
        }
Exemplo n.º 21
0
        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);

            // &sum;<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);
        }
Exemplo n.º 22
0
            public double posteriorForParents(IRandomVariable rv)
            {
                INode n = bn.GetNode(rv);

                if (!(n is IFiniteNode))
                {
                    throw new IllegalArgumentException("Enumeration-Ask only works with finite Nodes.");
                }
                IFiniteNode fn = (IFiniteNode)n;

                object[] vals = new object[1 + fn.GetParents().Size()];
                int      idx  = 0;

                foreach (INode pn in n.GetParents())
                {
                    vals[idx] = extendedValues[varIdxs.Get(pn.GetRandomVariable())];
                    idx++;
                }
                vals[idx] = extendedValues[varIdxs.Get(rv)];

                return(fn.GetCPT().GetValue(vals));
            }
Exemplo n.º 23
0
        private ICollection <IFactor> sumOut(IRandomVariable var, ICollection <IFactor> factors, IBayesianNetwork bn)
        {
            ICollection <IFactor> summedOutFactors = CollectionFactory.CreateQueue <IFactor>();
            ICollection <IFactor> toMultiply       = CollectionFactory.CreateQueue <IFactor>();

            foreach (IFactor f in factors)
            {
                if (f.contains(var))
                {
                    toMultiply.Add(f);
                }
                else
                {
                    // This factor does not contain the variable
                    // so no need to sum out - see AIMA3e pg. 527.
                    summedOutFactors.Add(f);
                }
            }

            summedOutFactors.Add(pointwiseProduct(toMultiply).sumOut(var));

            return(summedOutFactors);
        }
Exemplo n.º 24
0
 /// <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;
 }
Exemplo n.º 25
0
 public RVInfo(IRandomVariable rv)
 {
     variable  = rv;
     varDomain = (IFiniteDomain)variable.getDomain();
 }
Exemplo n.º 26
0
 public bool contains(IRandomVariable rv)
 {
     return(randomVarInfo.GetKeys().Contains(rv));
 }
Exemplo n.º 27
0
 public bool containsValue(IRandomVariable rv)
 {
     return(varIdxs.Get(rv) <= extendedIdx);
 }
Exemplo n.º 28
0
 public virtual bool Contains(IRandomVariable rv)
 {
     return(table.contains(rv));
 }
Exemplo n.º 29
0
 public AssignmentProposition(IRandomVariable forVariable, object value)
     : base(forVariable)
 {
     setValue(value);
 }
Exemplo n.º 30
0
        public void test_indexOf()
        {
            RandVar X = new RandVar("X", new BooleanDomain());
            RandVar Y = new RandVar("Y", new ArbitraryTokenDomain("A", "B", "C"));
            RandVar Z = new RandVar("Z", new BooleanDomain());

            // An ordered X,Y,Z enumeration of values should look like:
            // 00: true, A, true
            // 01: true, A, false
            // 02: true, B, true
            // 03: true, B, false
            // 04: true, C, true
            // 05: true, C, false
            // 06: false, A, true
            // 07: false, A, false
            // 08: false, B, true
            // 09: false, B, false
            // 10: false, C, true
            // 11: false, C, false
            IRandomVariable[] vars = new IRandomVariable[] { X, Y, Z };
            IMap <IRandomVariable, object> even = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();


            even.Put(X, true);

            even.Put(Y, "A");

            even.Put(Z, true);
            Assert.AreEqual(0, ProbUtil.indexOf(vars, even));

            even.Put(Z, false);
            Assert.AreEqual(1, ProbUtil.indexOf(vars, even));

            even.Put(Y, "B");

            even.Put(Z, true);
            Assert.AreEqual(2, ProbUtil.indexOf(vars, even));

            even.Put(Z, false);
            Assert.AreEqual(3, ProbUtil.indexOf(vars, even));

            even.Put(Y, "C");

            even.Put(Z, true);
            Assert.AreEqual(4, ProbUtil.indexOf(vars, even));

            even.Put(Z, false);
            Assert.AreEqual(5, ProbUtil.indexOf(vars, even));
            //
            even.Put(X, false);

            even.Put(Y, "A");

            even.Put(Z, true);
            Assert.AreEqual(6, ProbUtil.indexOf(vars, even));

            even.Put(Z, false);
            Assert.AreEqual(7, ProbUtil.indexOf(vars, even));

            even.Put(Y, "B");

            even.Put(Z, true);
            Assert.AreEqual(8, ProbUtil.indexOf(vars, even));

            even.Put(Z, false);
            Assert.AreEqual(9, ProbUtil.indexOf(vars, even));

            even.Put(Y, "C");

            even.Put(Z, true);
            Assert.AreEqual(10, ProbUtil.indexOf(vars, even));

            even.Put(Z, false);
            Assert.AreEqual(11, ProbUtil.indexOf(vars, even));
        }