示例#1
0
        public void testLikelihoodWeighting_AIMA3e_pg533()
        {
            // AIMA3e pg. 533
            // <b>P</b>(Rain | Cloudy = true, WetGrass = true)
            IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork();

            AssignmentProposition[] e = new AssignmentProposition[] {
                new AssignmentProposition(ExampleRV.CLOUDY_RV, true),
                new AssignmentProposition(ExampleRV.WET_GRASS_RV, true)
            };
            // sample P(Sprinkler | Cloudy = true) = <0.1, 0.9>; suppose
            // Sprinkler=false
            // sample P(Rain | Cloudy = true) = <0.8, 0.2>; suppose Rain=true
            MockRandomizer r = new MockRandomizer(new double[] { 0.5, 0.5 });

            LikelihoodWeighting lw = new LikelihoodWeighting(r);

            double[] estimate = lw.likelihoodWeighting(
                new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 1)
                                .getValues();

            // Here the event [true,false,true,true] should have weight 0.45,
            // and this is tallied under Rain = true, which when normalized
            // should be <1.0, 0.0>;
            assertArrayEquals(new double[] { 1.0, 0.0 }, estimate, DELTA_THRESHOLD);
        }
 public void test_RollingPairFairDiceModel_Distributions()
 {
     foreach (IBayesInference bi in getBayesInferenceImplementations())
     {
         test_RollingPairFairDiceModel_Distributions(new FiniteBayesModel(
                                                         BayesNetExampleFactory.construct2FairDiceNetwor(), bi));
     }
 }
 public void test_BurglaryAlarmModel_Distributions()
 {
     foreach (IBayesInference bi in getBayesInferenceImplementations())
     {
         test_BurglaryAlarmModel_Distributions(new FiniteBayesModel(
                                                   BayesNetExampleFactory.constructBurglaryAlarmNetwork(), bi));
     }
 }
示例#4
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);
        }
 public void test_MeningitisStiffNeckModel()
 {
     foreach (IBayesInference bi in getBayesInferenceImplementations())
     {
         test_MeningitisStiffNeckModel(new FiniteBayesModel(
                                           BayesNetExampleFactory.constructMeningitisStiffNeckNetwork(),
                                           bi));
     }
 }
 public void test_ToothacheCavityCatchWeatherModel_Distributions()
 {
     foreach (IBayesInference bi in getBayesInferenceImplementations())
     {
         test_ToothacheCavityCatchWeatherModel_Distributions(new FiniteBayesModel(
                                                                 BayesNetExampleFactory
                                                                 .constructToothacheCavityCatchWeatherNetwork(),
                                                                 bi));
     }
 }
 static void bayesEnumerationAskDemo()
 {
     System.Console.WriteLine("DEMO: Bayes Enumeration Ask");
     System.Console.WriteLine("===========================");
     demoToothacheCavityCatchModel(new FiniteBayesModel(
                                       BayesNetExampleFactory.constructToothacheCavityCatchNetwork(),
                                       new EnumerationAsk()));
     demoBurglaryAlarmModel(new FiniteBayesModel(
                                BayesNetExampleFactory.constructBurglaryAlarmNetwork(),
                                new EnumerationAsk()));
     System.Console.WriteLine("===========================");
 }
示例#8
0
        public void testPriorSample_basic()
        {
            IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork();

            AssignmentProposition[] e  = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) };
            MockRandomizer          r  = new MockRandomizer(new double[] { 0.1 });
            RejectionSampling       rs = new RejectionSampling(new PriorSample(r));

            double[] estimate = rs.rejectionSampling(new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 100).getValues();

            assertArrayEquals(new double[] { 1.0, 0.0 }, estimate, DELTA_THRESHOLD);
        }
示例#9
0
 static void bayesGibbsAskDemo()
 {
     System.Console.WriteLine("DEMO: Bayes Gibbs Ask N = " + NUM_SAMPLES);
     System.Console.WriteLine("=====================");
     demoToothacheCavityCatchModel(new FiniteBayesModel(
             BayesNetExampleFactory.constructToothacheCavityCatchNetwork(),
             new BayesInferenceApproxAdapter(new GibbsAsk(), NUM_SAMPLES)));
     demoBurglaryAlarmModel(new FiniteBayesModel(
             BayesNetExampleFactory.constructBurglaryAlarmNetwork(),
             new BayesInferenceApproxAdapter(new GibbsAsk(), NUM_SAMPLES)));
     System.Console.WriteLine("=====================");
 } 
示例#10
0
        public void testGibbsAsk_basic()
        {
            IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork();

            AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) };

            GibbsAsk ga = new GibbsAsk();

            double[] estimate = ga.gibbsAsk(new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 1000).getValues();

            assertArrayEquals(new double[] { 0.3, 0.7 }, estimate, DELTA_THRESHOLD);
        }
示例#11
0
        public void testRejectionSampling_AIMA3e_pg532()
        {
            // AIMA3e pg. 532

            IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork();

            AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) };

            // 400 required as 4 variables and 100 samples planned
            double[] ma = new double[400];
            for (int i = 0; i < ma.Length; i += 4)
            {
                // Of the 100 that we generate, suppose
                // that 73 have Sprinkler = false and are rejected,
                if (i < (73 * 4))
                {
                    ma[i]     = 0.5; // i.e Cloudy=true
                    ma[i + 1] = 0.2; // i.e. Sprinkler=false
                    ma[i + 2] = 0.5; // i.e. Rain=true
                    ma[i + 3] = 0.1; // i.e. WetGrass=true
                }
                else
                {
                    ma[i]     = 0.5;  // i.e Cloudy=true
                    ma[i + 1] = 0.09; // i.e. Sprinkler=true
                                      // while 27 have Sprinkler = true; of the 27,
                                      // 8 have Rain = true
                    if (i < ((73 + 8) * 4))
                    {
                        ma[i + 2] = 0.5; // i.e. Rain=true
                    }
                    else
                    {
                        // and 19 have Rain = false.
                        ma[i + 2] = 0.9; // i.e. Rain=false
                    }

                    ma[i + 3] = 0.1; // i.e. WetGrass=true
                }
            }
            MockRandomizer    r  = new MockRandomizer(ma);
            RejectionSampling rs = new RejectionSampling(new PriorSample(r));

            double[] estimate = rs.rejectionSampling(
                new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 100)
                                .getValues();

            assertArrayEquals(new double[] { 0.2962962962962963, 0.7037037037037037 }, estimate, DELTA_THRESHOLD);
        }
示例#12
0
        public void testLikelihoodWeighting_basic()
        {
            IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork();

            AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) };
            MockRandomizer          r = new MockRandomizer(new double[] { 0.5, 0.5, 0.5, 0.5 });

            LikelihoodWeighting lw = new LikelihoodWeighting(r);

            double[] estimate = lw.likelihoodWeighting(
                new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 1000)
                                .getValues();

            assertArrayEquals(new double[] { 1.0, 0.0 }, estimate, DELTA_THRESHOLD);
        }
示例#13
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);
        }
示例#14
0
        public void testPriorSample_basic()
        {
            // AIMA3e pg. 530
            IBayesianNetwork bn = BayesNetExampleFactory
                                  .constructCloudySprinklerRainWetGrassNetwork();
            IRandom r = new MockRandomizer(
                new double[] { 0.5, 0.5, 0.5, 0.5 });

            PriorSample ps = new PriorSample(r);
            IMap <IRandomVariable, object> even = ps.priorSample(bn);

            Assert.AreEqual(4, even.GetKeys().Size());
            Assert.AreEqual(true, even.Get(ExampleRV.CLOUDY_RV));
            Assert.AreEqual(false, even.Get(ExampleRV.SPRINKLER_RV));
            Assert.AreEqual(true, even.Get(ExampleRV.RAIN_RV));
            Assert.AreEqual(true, even.Get(ExampleRV.WET_GRASS_RV));
        }