コード例 #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);
        }
コード例 #2
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);
        }
コード例 #3
0
ファイル: GibbsAskTest.cs プロジェクト: tvn-cosine/aima.net
        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);
        }
コード例 #4
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);
        }
コード例 #5
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);
        }
コード例 #6
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));
        }