// function WEIGHTED-SAMPLE(bn, e) returns an event and a weight

        /**
         * The WEIGHTED-SAMPLE function in Figure 14.15.
         *
         * @param e
         *            observed values for variables E
         * @param bn
         *            a Bayesian network specifying joint distribution
         *            <b>P</b>(X<sub>1</sub>,...,X<sub>n</sub>)
         * @return return <b>x</b>, w - an event with its associated weight.
         */
        public Pair <IMap <IRandomVariable, object>, double> weightedSample(IBayesianNetwork bn, AssignmentProposition[] e)
        {
            // w <- 1;
            double w = 1.0;
            // <b>x</b> <- an event with n elements initialized from e
            IMap <IRandomVariable, object> x = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();

            foreach (AssignmentProposition ap in e)
            {
                x.Put(ap.getTermVariable(), ap.getValue());
            }

            // foreach variable X<sub>i</sub> in X<sub>1</sub>,...,X<sub>n</sub> do
            foreach (IRandomVariable Xi in bn.GetVariablesInTopologicalOrder())
            {
                // if X<sub>i</sub> is an evidence variable with value x<sub>i</sub>
                // in e
                if (x.ContainsKey(Xi))
                {
                    // then w <- w * P(X<sub>i</sub> = x<sub>i</sub> |
                    // parents(X<sub>i</sub>))
                    w *= bn.GetNode(Xi)
                         .GetCPD()
                         .GetValue(ProbUtil.getEventValuesForXiGivenParents(bn.GetNode(Xi), x));
                }
                else
                {
                    // else <b>x</b>[i] <- a random sample from
                    // <b>P</b>(X<sub>i</sub> | parents(X<sub>i</sub>))
                    x.Put(Xi, ProbUtil.randomSample(bn.GetNode(Xi), x, randomizer));
                }
            }
            // return <b>x</b>, w
            return(new Pair <IMap <IRandomVariable, object>, double>(x, w));
        }
Beispiel #2
0
 /**
  * <pre>
  * Q(x<sub>i</sub>) <- ENUMERATE-ALL(bn.VARS, e<sub>x<sub>i</sub></sub>)
  *   where e<sub>x<sub>i</sub></sub> is e extended with X = x<sub>i</sub>
  * </pre>
  */
 public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability)
 {
     for (int i = 0; i < x.Length; ++i)
     {
         e.setExtendedValue(x[i], possibleWorld.Get(x[i]));
     }
     q.setValue(cnt, enumerationAsk.enumerateAll(bn.GetVariablesInTopologicalOrder(), e));
     cnt++;
 }
Beispiel #3
0
 public FiniteBayesModel(IBayesianNetwork bn, IBayesInference bi)
 {
     if (null == bn)
     {
         throw new IllegalArgumentException("Bayesian Network describing the model must be specified.");
     }
     this.bayesNet = bn;
     this.representation.AddAll(bn.GetVariablesInTopologicalOrder());
     setBayesInference(bi);
 }
Beispiel #4
0
        // function GIBBS-ASK(X, e, bn, N) returns an estimate of <b>P</b>(X|e)

        /**
         * The GIBBS-ASK algorithm in Figure 14.16. For answering queries given
         * evidence in a Bayesian Network.
         *
         * @param X
         *            the query variables
         * @param e
         *            observed values for variables E
         * @param bn
         *            a Bayesian network specifying joint distribution
         *            <b>P</b>(X<sub>1</sub>,...,X<sub>n</sub>)
         * @param Nsamples
         *            the total number of samples to be generated
         * @return an estimate of <b>P</b>(X|e)
         */
        public ICategoricalDistribution gibbsAsk(IRandomVariable[] X,
                                                 AssignmentProposition[] e, IBayesianNetwork bn, int Nsamples)
        {
            // local variables: <b>N</b>, a vector of counts for each value of X,
            // initially zero
            double[] N = new double[ProbUtil.expectedSizeOfCategoricalDistribution(X)];
            // Z, the nonevidence variables in bn
            ISet <IRandomVariable> Z = CollectionFactory.CreateSet <IRandomVariable>(bn.GetVariablesInTopologicalOrder());

            foreach (AssignmentProposition ap in e)
            {
                Z.Remove(ap.getTermVariable());
            }
            // <b>x</b>, the current state of the network, initially copied from e
            IMap <IRandomVariable, object> x = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();

            foreach (AssignmentProposition ap in e)
            {
                x.Put(ap.getTermVariable(), ap.getValue());
            }

            // initialize <b>x</b> with random values for the variables in Z
            foreach (IRandomVariable Zi in Z)
            {
                x.Put(Zi, ProbUtil.randomSample(bn.GetNode(Zi), x, randomizer));
            }

            // for j = 1 to N do
            for (int j = 0; j < Nsamples; j++)
            {
                // for each Z<sub>i</sub> in Z do
                foreach (IRandomVariable Zi in Z)
                {
                    // set the value of Z<sub>i</sub> in <b>x</b> by sampling from
                    // <b>P</b>(Z<sub>i</sub>|mb(Z<sub>i</sub>))
                    x.Put(Zi, ProbUtil.mbRandomSample(bn.GetNode(Zi), x, randomizer));
                }
                // Note: moving this outside the previous for loop,
                // as described in fig 14.6, as will only work
                // correctly in the case of a single query variable X.
                // However, when multiple query variables, rare events
                // will get weighted incorrectly if done above. In case
                // of single variable this does not happen as each possible
                // value gets * |Z| above, ending up with the same ratios
                // when normalized (i.e. its still more efficient to place
                // outside the loop).
                //
                // <b>N</b>[x] <- <b>N</b>[x] + 1
                // where x is the value of X in <b>x</b>
                N[ProbUtil.indexOf(X, x)] += 1.0;
            }
            // return NORMALIZE(<b>N</b>)
            return(new ProbabilityTable(N, X).normalize());
        }
Beispiel #5
0
            public ObservedEvidence(IRandomVariable[] queryVariables,
                                    AssignmentProposition[] e, IBayesianNetwork bn)
            {
                this.bn = bn;

                int maxSize = bn.GetVariablesInTopologicalOrder().Size();

                extendedValues = new object[maxSize];
                var            = new IRandomVariable[maxSize];
                // query variables go first
                int idx = 0;

                for (int i = 0; i < queryVariables.Length; ++i)
                {
                    var[idx] = queryVariables[i];
                    varIdxs.Put(var[idx], idx);
                    idx++;
                }
                // initial evidence variables go next
                for (int i = 0; i < e.Length; ++i)
                {
                    var[idx] = e[i].getTermVariable();
                    varIdxs.Put(var[idx], idx);
                    extendedValues[idx] = e[i].getValue();
                    idx++;
                }
                extendedIdx = idx - 1;
                hiddenStart = idx;
                // the remaining slots are left open for the hidden variables
                foreach (IRandomVariable rv in bn.GetVariablesInTopologicalOrder())
                {
                    if (!varIdxs.ContainsKey(rv))
                    {
                        var[idx] = rv;
                        varIdxs.Put(var[idx], idx);
                        idx++;
                    }
                }
            }
Beispiel #6
0
        // function PRIOR-SAMPLE(bn) returns an event sampled from the prior
        // specified by bn

        /**
         * The PRIOR-SAMPLE algorithm in Figure 14.13. A sampling algorithm that
         * generates events from a Bayesian network. Each variable is sampled
         * according to the conditional distribution given the values already
         * sampled for the variable's parents.
         *
         * @param bn
         *            a Bayesian network specifying joint distribution
         *            <b>P</b>(X<sub>1</sub>,...,X<sub>n</sub>)
         * @return an event sampled from the prior specified by bn
         */
        public IMap <IRandomVariable, object> priorSample(IBayesianNetwork bn)
        {
            // x <- an event with n elements
            IMap <IRandomVariable, object> x = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>();

            // foreach variable X<sub>i</sub> in X<sub>1</sub>,...,X<sub>n</sub> do
            foreach (IRandomVariable Xi in bn.GetVariablesInTopologicalOrder())
            {
                // x[i] <- a random sample from
                // <b>P</b>(X<sub>i</sub> | parents(X<sub>i</sub>))
                x.Put(Xi, ProbUtil.randomSample(bn.GetNode(Xi), x, randomizer));
            }
            // return x
            return(x);
        }
Beispiel #7
0
        // END-BayesInference
        //

        //
        // PROTECTED METHODS
        //

        /**
         * <b>Note:</b>Override this method for a more efficient implementation as
         * outlined in AIMA3e pgs. 527-28. Calculate the hidden variables from the
         * Bayesian Network. The default implementation does not perform any of
         * these.<br>
         * <br>
         * Two calcuations to be performed here in order to optimize iteration over
         * the Bayesian Network:<br>
         * 1. Calculate the hidden variables to be enumerated over. An optimization
         * (AIMA3e pg. 528) is to remove 'every variable that is not an ancestor of
         * a query variable or evidence variable as it is irrelevant to the query'
         * (i.e. sums to 1). 2. The subset of variables from the Bayesian Network to
         * be retained after irrelevant hidden variables have been removed.
         *
         * @param X
         *            the query variables.
         * @param e
         *            observed values for variables E.
         * @param bn
         *            a Bayes net with variables {X} &cup; E &cup; Y /* Y = hidden
         *            variables //
         * @param hidden
         *            to be populated with the relevant hidden variables Y.
         * @param bnVARS
         *            to be populated with the subset of the random variables
         *            comprising the Bayesian Network with any irrelevant hidden
         *            variables removed.
         */
        protected void calculateVariables(IRandomVariable[] X,
                                          AssignmentProposition[] e, IBayesianNetwork bn,
                                          ISet <IRandomVariable> hidden, ICollection <IRandomVariable> bnVARS)
        {
            bnVARS.AddAll(bn.GetVariablesInTopologicalOrder());
            hidden.AddAll(bnVARS);

            foreach (IRandomVariable x in X)
            {
                hidden.Remove(x);
            }
            foreach (AssignmentProposition ap in e)
            {
                hidden.RemoveAll(ap.getScope());
            }

            return;
        }