Пример #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 testGibbsAsk_compare()
        {
            // create two nodes: parent and child with an arc from parent to child
            IRandomVariable rvParent   = new RandVar("Parent", new BooleanDomain());
            IRandomVariable rvChild    = new RandVar("Child", new BooleanDomain());
            FullCPTNode     nodeParent = new FullCPTNode(rvParent, new double[] { 0.7, 0.3 });

            new FullCPTNode(rvChild, new double[] { 0.8, 0.2, 0.2, 0.8 }, nodeParent);

            // create net
            BayesNet net = new BayesNet(nodeParent);

            // query parent probability
            IRandomVariable[] rvX = new IRandomVariable[] { rvParent };

            // ...given child evidence (true)
            AssignmentProposition[] propE = new AssignmentProposition[] { new AssignmentProposition(rvChild, true) };

            // sample with LikelihoodWeighting
            ICategoricalDistribution samplesLW = new LikelihoodWeighting().Ask(rvX, propE, net, 1000);

            Assert.AreEqual(0.9, samplesLW.getValue(true), DELTA_THRESHOLD);

            // sample with RejectionSampling
            ICategoricalDistribution samplesRS = new RejectionSampling().Ask(rvX, propE, net, 1000);

            Assert.AreEqual(0.9, samplesRS.getValue(true), DELTA_THRESHOLD);

            // sample with GibbsAsk
            ICategoricalDistribution samplesGibbs = new GibbsAsk().Ask(rvX, propE, net, 1000);

            Assert.AreEqual(0.9, samplesGibbs.getValue(true), DELTA_THRESHOLD);
        }
Пример #3
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);
        }