Exemplo n.º 1
0
        /**
         * Calculate the probability distribution for <b>P</b>(X<sub>i</sub> |
         * mb(X<sub>i</sub>)), where mb(X<sub>i</sub>) is the Markov Blanket of
         * X<sub>i</sub>. The probability of a variable given its Markov blanket is
         * proportional to the probability of the variable given its parents times
         * the probability of each child given its respective parents (see equation
         * 14.12 pg. 538 AIMA3e):<br>
         * <br>
         * P(x'<sub>i</sub>|mb(Xi)) =
         * &alpha;P(x'<sub>i</sub>|parents(X<sub>i</sub>)) *
         * &prod;<sub>Y<sub>j</sub> &isin; Children(X<sub>i</sub>)</sub>
         * P(y<sub>j</sub>|parents(Y<sub>j</sub>))
         *
         * @param Xi
         *            a Node from a Bayesian network for the Random Variable
         *            X<sub>i</sub>.
         * @param event
         *            comprising assignments for the Markov Blanket X<sub>i</sub>.
         * @return a random sample from <b>P</b>(X<sub>i</sub> | mb(X<sub>i</sub>))
         */

        public static double[] mbDistribution(Node Xi,
                                              Map <RandomVariable, Object> evt)
        {
            FiniteDomain fd = (FiniteDomain)Xi.getRandomVariable().getDomain();

            double[] X = new double[fd.size()];

            for (int i = 0; i < fd.size(); i++)
            {
                // P(x'<sub>i</sub>|mb(Xi)) =
                // &alpha;P(x'<sub>i</sub>|parents(X<sub>i</sub>)) *
                // &prod;<sub>Y<sub>j</sub> &isin; Children(X<sub>i</sub>)</sub>
                // P(y<sub>j</sub>|parents(Y<sub>j</sub>))
                double cprob = 1.0;
                foreach (Node Yj in Xi.getChildren())
                {
                    cprob *= Yj.getCPD().getValue(
                        getEventValuesForXiGivenParents(Yj, evt));
                }
                X[i] = Xi.getCPD()
                       .getValue(
                    getEventValuesForXiGivenParents(Xi,
                                                    fd.getValueAt(i), evt))
                       * cprob;
            }

            return(Util.normalize(X));
        }
Exemplo n.º 2
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, 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));
        }
Exemplo n.º 3
0
 public Object getDomainValueAt(int idx)
 {
     return(varDomain.getValueAt(idx));
 }