Esempio n. 1
0
        public void testFixedLagSmoothing_lag_1_UmbrellaWorld()
        {
            FixedLagSmoothing uw = new FixedLagSmoothing(HMMExampleFactory.getUmbrellaWorldModel(), 1);

            // Day 1 - Lag 1
            ICollection <AssignmentProposition> e1 = CollectionFactory.CreateQueue <AssignmentProposition>();

            e1.Add(new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, true));

            ICategoricalDistribution smoothed = uw.fixedLagSmoothing(e1);

            Assert.IsNull(smoothed);

            // Day 2 - Lag 1
            ICollection <AssignmentProposition> e2 = CollectionFactory.CreateQueue <AssignmentProposition>();

            e2.Add(new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, true));

            smoothed = uw.fixedLagSmoothing(e2);

            // Day 1 smoothed probabilities based on 2 days of evidence
            Assert.IsNotNull(smoothed);
            assertArrayEquals(new double[] { 0.883, 0.117 }, smoothed.getValues(), DELTA_THRESHOLD);

            // Day 3 - Lag 1
            ICollection <AssignmentProposition> e3 = CollectionFactory.CreateQueue <AssignmentProposition>();

            e3.Add(new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, false));

            smoothed = uw.fixedLagSmoothing(e3);

            // Day 2 smoothed probabilities based on 3 days of evidence
            Assert.IsNotNull(smoothed);
            assertArrayEquals(new double[] { 0.799, 0.201 }, smoothed.getValues(), DELTA_THRESHOLD);
        }
Esempio n. 2
0
        public void testInferenceOnBurglaryAlarmNetwork()
        {
            IBayesianNetwork bn = BayesNetExampleFactory
                                  .constructBurglaryAlarmNetwork();

            // AIMA3e. pg. 514
            ICategoricalDistribution d = bayesInference
                                         .Ask(new IRandomVariable[] { ExampleRV.ALARM_RV },
                                              new AssignmentProposition[] {
                new AssignmentProposition(
                    ExampleRV.BURGLARY_RV, false),
                new AssignmentProposition(
                    ExampleRV.EARTHQUAKE_RV, false),
                new AssignmentProposition(
                    ExampleRV.JOHN_CALLS_RV, true),
                new AssignmentProposition(
                    ExampleRV.MARY_CALLS_RV, true)
            }, bn);

            // System.Console.WriteLine("P(Alarm | ~b, ~e, j, m)=" + d);
            Assert.AreEqual(2, d.getValues().Length);
            Assert.AreEqual(0.5577689243027888, d.getValues()[0],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
            Assert.AreEqual(0.44223107569721115, d.getValues()[1],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);

            // AIMA3e pg. 523
            // P(Burglary | JohnCalls = true, MaryCalls = true) = <0.284, 0.716>
            d = bayesInference
                .Ask(new IRandomVariable[] { ExampleRV.BURGLARY_RV },
                     new AssignmentProposition[] {
                new AssignmentProposition(
                    ExampleRV.JOHN_CALLS_RV, true),
                new AssignmentProposition(
                    ExampleRV.MARY_CALLS_RV, true)
            }, bn);

            // System.Console.WriteLine("P(Burglary | j, m)=" + d);
            Assert.AreEqual(2, d.getValues().Length);
            Assert.AreEqual(0.2841718353643929, d.getValues()[0],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
            Assert.AreEqual(0.7158281646356071, d.getValues()[1],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);

            // AIMA3e pg. 528
            // P(JohnCalls | Burglary = true)
            d = bayesInference.Ask(
                new IRandomVariable[] { ExampleRV.JOHN_CALLS_RV },
                new AssignmentProposition[] { new AssignmentProposition(
                                                  ExampleRV.BURGLARY_RV, true) }, bn);
            // System.Console.WriteLine("P(JohnCalls | b)=" + d);
            Assert.AreEqual(2, d.getValues().Length);
            Assert.AreEqual(0.8490169999999999, d.getValues()[0],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
            Assert.AreEqual(0.15098299999999998, d.getValues()[1],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
        }
Esempio n. 3
0
        protected void testForwardStep_UmbrellaWorld(IForwardStepInference uw)
        {
            // AIMA3e pg. 572
            // Day 0, no observations only the security guards prior beliefs
            // P(R<sub>0</sub>) = <0.5, 0.5>
            ICategoricalDistribution prior = new ProbabilityTable(new double[] { 0.5, 0.5 }, ExampleRV.RAIN_t_RV);

            // Day 1, the umbrella appears, so U<sub>1</sub> = true.
            // &asymp; <0.818, 0.182>
            ICollection <AssignmentProposition> e1 = CollectionFactory.CreateQueue <AssignmentProposition>();

            e1.Add(new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, true));
            ICategoricalDistribution f1 = uw.forward(prior, e1);

            assertArrayEquals(new double[] { 0.818, 0.182 }, f1.getValues(), DELTA_THRESHOLD);

            // Day 2, the umbrella appears, so U<sub>2</sub> = true.
            // &asymp; <0.883, 0.117>
            ICollection <AssignmentProposition> e2 = CollectionFactory.CreateQueue <AssignmentProposition>();

            e2.Add(new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, true));
            ICategoricalDistribution f2 = uw.forward(f1, e2);

            assertArrayEquals(new double[] { 0.883, 0.117 }, f2.getValues(),
                              DELTA_THRESHOLD);
        }
Esempio n. 4
0
        // AIMA3e pg. 496
        protected void test_MeningitisStiffNeckModel_Distributions(
            IFiniteProbabilityModel model)
        {
            AssignmentProposition astiffNeck = new AssignmentProposition(
                ExampleRV.STIFF_NECK_RV, true);

            // AIMA3e pg. 497
            // P<>(Mengingitis | stiffneck) = &alpha;<P(s | m)P(m), P(s | ~m)P(~m)>
            ICategoricalDistribution dMeningitisGivenStiffNeck = model
                                                                 .posteriorDistribution(ExampleRV.MENINGITIS_RV, astiffNeck);

            Assert.AreEqual(2, dMeningitisGivenStiffNeck.getValues().Length);
            Assert.AreEqual(0.0014, dMeningitisGivenStiffNeck.getValues()[0],
                            DELTA_THRESHOLD);
            Assert.AreEqual(0.9986, dMeningitisGivenStiffNeck.getValues()[1],
                            DELTA_THRESHOLD);
        }
Esempio n. 5
0
        public void testInferenceOnToothacheCavityCatchNetwork()
        {
            IBayesianNetwork bn = BayesNetExampleFactory
                                  .constructToothacheCavityCatchNetwork();

            ICategoricalDistribution d = bayesInference.Ask(
                new IRandomVariable[] { ExampleRV.CAVITY_RV },
                new AssignmentProposition[] { }, bn);

            // System.Console.WriteLine("P(Cavity)=" + d);
            Assert.AreEqual(2, d.getValues().Length);
            Assert.AreEqual(0.2, d.getValues()[0],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
            Assert.AreEqual(0.8, d.getValues()[1],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);

            // AIMA3e pg. 493
            // P(Cavity | toothache) = <0.6, 0.4>
            d = bayesInference.Ask(new IRandomVariable[] { ExampleRV.CAVITY_RV },
                                   new AssignmentProposition[] { new AssignmentProposition(
                                                                     ExampleRV.TOOTHACHE_RV, true) }, bn);

            // System.Console.WriteLine("P(Cavity | toothache)=" + d);
            Assert.AreEqual(2, d.getValues().Length);
            Assert.AreEqual(0.6, d.getValues()[0],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
            Assert.AreEqual(0.4, d.getValues()[1],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);

            // AIMA3e pg. 497
            // P(Cavity | toothache AND catch) = <0.871, 0.129>
            d = bayesInference
                .Ask(new IRandomVariable[] { ExampleRV.CAVITY_RV },
                     new AssignmentProposition[] {
                new AssignmentProposition(
                    ExampleRV.TOOTHACHE_RV, true),
                new AssignmentProposition(ExampleRV.CATCH_RV,
                                          true)
            }, bn);

            // System.Console.WriteLine("P(Cavity | toothache, catch)=" + d);
            Assert.AreEqual(2, d.getValues().Length);
            Assert.AreEqual(0.8709677419354839, d.getValues()[0],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
            Assert.AreEqual(0.12903225806451615, d.getValues()[1],
                            ProbabilityModelImpl.DEFAULT_ROUNDING_THRESHOLD);
        }
Esempio n. 6
0
        protected void testBackwardStep_UmbrellaWorld(IBackwardStepInference uw)
        {
            // AIMA3e pg. 575
            ICategoricalDistribution            b_kp2t = new ProbabilityTable(new double[] { 1.0, 1.0 }, ExampleRV.RAIN_t_RV);
            ICollection <AssignmentProposition> e2     = CollectionFactory.CreateQueue <AssignmentProposition>();

            e2.Add(new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, true));
            ICategoricalDistribution b1 = uw.backward(b_kp2t, e2);

            assertArrayEquals(new double[] { 0.69, 0.41 }, b1.getValues(), DELTA_THRESHOLD);
        }
Esempio n. 7
0
            public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability)
            {
                // <b>P</b>(X<sub>t+1</sub> | x<sub>t</sub>)*
                // P(x<sub>t</sub> | e<sub>1:t</sub>)
                foreach (var av in possibleWorld)
                {
                    xtVarAssignMap.Get(av.GetKey()).setValue(av.GetValue());
                }
                int i = 0;

                foreach (double tp in transitionModel.posteriorDistribution(xtp1, xt).getValues())
                {
                    s1.setValue(i, s1.getValues()[i] + (tp * probability));
                    ++i;
                }
            }
Esempio n. 8
0
            public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability)
            {
                // Assign current values for x<sub>k+1</sub>
                foreach (var av in possibleWorld)
                {
                    x_kp1VarAssignMap.Get(av.GetKey()).setValue(av.GetValue());
                }

                // P(e<sub>k+1</sub> | x<sub>k+1</sub>)
                // P(e<sub>k+2:t</sub> | x<sub>k+1</sub>)
                double p = sensorModel.posterior(pe_kp1, x_kp1) * probability;

                // <b>P</b>(x<sub>k+1</sub> | X<sub>k</sub>)
                int i = 0;

                foreach (double tp in transitionModel.posteriorDistribution(x_kp1, xk).getValues())
                {
                    b_kp1t.setValue(i, b_kp1t.getValues()[i] + (tp * p));
                    ++i;
                }
            }
Esempio n. 9
0
 public virtual Matrix convert(ICategoricalDistribution fromCD)
 {
     double[] values = fromCD.getValues();
     return(new Matrix(values, values.Length));
 }
Esempio n. 10
0
        //
        // PROTECTED
        //
        protected void test_RollingPairFairDiceModel_Distributions(IFiniteProbabilityModel model)
        {
            AssignmentProposition    ad1_1 = new AssignmentProposition(ExampleRV.DICE_1_RV, 1);
            ICategoricalDistribution dD1_1 = model.priorDistribution(ad1_1);

            assertArrayEquals(new double[] { 1.0 / 6.0 }, dD1_1.getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dPriorDice1 = model.priorDistribution(ExampleRV.DICE_1_RV);

            assertArrayEquals(new double[] { 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0 },
                              dPriorDice1.getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dPriorDice2 = model.priorDistribution(ExampleRV.DICE_2_RV);

            assertArrayEquals(new double[] { 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0 },
                              dPriorDice2.getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dJointDice1Dice2 = model.jointDistribution(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            Assert.AreEqual(36, dJointDice1Dice2.getValues().Length);
            for (int i = 0; i < dJointDice1Dice2.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 36.0, dJointDice1Dice2.getValues()[i], DELTA_THRESHOLD);
            }

            ICategoricalDistribution dJointDice2Dice1 = model.jointDistribution(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV);

            Assert.AreEqual(36, dJointDice2Dice1.getValues().Length);
            for (int i = 0; i < dJointDice2Dice1.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 36.0, dJointDice2Dice1.getValues()[i], DELTA_THRESHOLD);
            }

            //
            // Test Sets of events
            IntegerSumProposition total11 = new IntegerSumProposition("Total",
                                                                      new FiniteIntegerDomain(11), ExampleRV.DICE_1_RV,
                                                                      ExampleRV.DICE_2_RV);

            // P<>(Total = 11) = <2.0/36.0>
            assertArrayEquals(new double[] { 2.0 / 36.0 }, model.priorDistribution(total11).getValues(), DELTA_THRESHOLD);

            // P<>(Dice1, Total = 11)
            // = <0.0, 0.0, 0.0, 0.0, 1.0/36.0, 1.0/36.0>
            assertArrayEquals(new double[] { 0, 0, 0, 0, 1.0 / 36.0, 1.0 / 36.0 },
                              model.priorDistribution(ExampleRV.DICE_1_RV, total11)
                              .getValues(), DELTA_THRESHOLD);

            EquivalentProposition doubles = new EquivalentProposition("Doubles",
                                                                      ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            // P(Doubles) = <1.0/6.0>
            assertArrayEquals(new double[] { 1.0 / 6.0 }, model
                              .priorDistribution(doubles).getValues(), DELTA_THRESHOLD);

            //
            // Test posterior
            //
            // P<>(Dice1, Total = 11)
            // = <0.0, 0.0, 0.0, 0.0, 0.5, 0.5>
            assertArrayEquals(new double[] { 0, 0, 0, 0, 0.5, 0.5 }, model
                              .posteriorDistribution(ExampleRV.DICE_1_RV, total11)
                              .getValues(), DELTA_THRESHOLD);

            // P<>(Dice1 | Doubles) = <1/6, 1/6, 1/6, 1/6, 1/6, 1/6>
            assertArrayEquals(new double[] { 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0 }, model
                              .posteriorDistribution(ExampleRV.DICE_1_RV, doubles)
                              .getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dPosteriorDice1GivenDice2 = model
                                                                 .posteriorDistribution(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            Assert.AreEqual(36, dPosteriorDice1GivenDice2.getValues().Length);
            for (int i = 0; i < dPosteriorDice1GivenDice2.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 6.0, dPosteriorDice1GivenDice2.getValues()[i], DELTA_THRESHOLD);
            }

            ICategoricalDistribution dPosteriorDice2GivenDice1 = model
                                                                 .posteriorDistribution(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV);

            Assert.AreEqual(36, dPosteriorDice2GivenDice1.getValues().Length);
            for (int i = 0; i < dPosteriorDice2GivenDice1.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 6.0, dPosteriorDice2GivenDice1.getValues()[i], DELTA_THRESHOLD);
            }
        }