Example #1
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()));
        }
Example #2
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));
            }