Exemplo n.º 1
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);
        }
        public static FiniteProbabilityModel getUmbrellaWorldModel()
        {
            // Prior belief state
            FiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV,
                                                  new double[] { 0.5, 0.5 });
            // Transition Model
            FiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[]
            {
                // R_t-1 = true, R_t = true
                0.7,
                // R_t-1 = true, R_t = false
                0.3,
                // R_t-1 = false, R_t = true
                0.3,
                // R_t-1 = false, R_t = false
                0.7
            }, rain_tm1);
            // Sensor Model
            FiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV,
                                                     new double[]
            {
                // R_t = true, U_t = true
                0.9,
                // R_t = true, U_t = false
                0.1,
                // R_t = false, U_t = true
                0.2,
                // R_t = false, U_t = false
                0.8
            }, rain_t);

            return(new FiniteBayesModel(new BayesNet(rain_tm1)));
        }
Exemplo n.º 3
0
        public static BayesianNetwork constructToothacheCavityCatchNetwork()
        {
            FiniteNode cavity = new FullCPTNode(ExampleRV.CAVITY_RV, new double[]
                                                                         {
                                                                             0.2, 0.8
                                                                         });
            FiniteNode toothache = new FullCPTNode(ExampleRV.TOOTHACHE_RV,
                                                   new double[]
                                                       {
                                                           // C=true, T=true
                                                           0.6,
                                                           // C=true, T=false
                                                           0.4,
                                                           // C=false, T=true
                                                           0.1,
                                                           // C=false, T=false
                                                           0.9

                                                       }, cavity);
            FiniteNode catchN = new FullCPTNode(ExampleRV.CATCH_RV, new double[]
                                                                        {
                                                                            // C=true, Catch=true
                                                                            0.9,
                                                                            // C=true, Catch=false
                                                                            0.1,
                                                                            // C=false, Catch=true
                                                                            0.2,
                                                                            // C=false, Catch=false
                                                                            0.8
                                                                        }, cavity);

            return new BayesNet(cavity);
        }
Exemplo n.º 4
0
        public static BayesianNetwork constructToothacheCavityCatchNetwork()
        {
            FiniteNode cavity = new FullCPTNode(ExampleRV.CAVITY_RV, new double[]
            {
                0.2, 0.8
            });
            FiniteNode toothache = new FullCPTNode(ExampleRV.TOOTHACHE_RV,
                                                   new double[]
            {
                // C=true, T=true
                0.6,
                // C=true, T=false
                0.4,
                // C=false, T=true
                0.1,
                // C=false, T=false
                0.9
            }, cavity);
            FiniteNode catchN = new FullCPTNode(ExampleRV.CATCH_RV, new double[]
            {
                // C=true, Catch=true
                0.9,
                // C=true, Catch=false
                0.1,
                // C=false, Catch=true
                0.2,
                // C=false, Catch=false
                0.8
            }, cavity);

            return(new BayesNet(cavity));
        }
Exemplo n.º 5
0
        public static BayesianNetwork constructBurglaryAlarmNetwork()
        {
            FiniteNode burglary = new FullCPTNode(ExampleRV.BURGLARY_RV,
                                                  new double[] { 0.001, 0.999 });
            FiniteNode earthquake = new FullCPTNode(ExampleRV.EARTHQUAKE_RV,
                                                    new double[] { 0.002, 0.998 });
            FiniteNode alarm = new FullCPTNode(ExampleRV.ALARM_RV, new double[]
            {
                // B=true, E=true, A=true
                0.95,
                // B=true, E=true, A=false
                0.05,
                // B=true, E=false, A=true
                0.94,
                // B=true, E=false, A=false
                0.06,
                // B=false, E=true, A=true
                0.29,
                // B=false, E=true, A=false
                0.71,
                // B=false, E=false, A=true
                0.001,
                // B=false, E=false, A=false
                0.999
            }, burglary, earthquake);

            FiniteNode johnCalls = new FullCPTNode(ExampleRV.JOHN_CALLS_RV,
                                                   new double[]
            {
                // A=true, J=true
                0.90,
                // A=true, J=false
                0.10,
                // A=false, J=true
                0.05,
                // A=false, J=false
                0.95
            }, alarm);

            FiniteNode maryCalls = new FullCPTNode(ExampleRV.MARY_CALLS_RV,
                                                   new double[]
            {
                // A=true, M=true
                0.70,
                // A=true, M=false
                0.30,
                // A=false, M=true
                0.01,
                // A=false, M=false
                0.99
            }, alarm);

            return(new BayesNet(burglary, earthquake));
        }
Exemplo n.º 6
0
        public static BayesianNetwork constructCloudySprinklerRainWetGrassNetwork()
        {
            FiniteNode cloudy = new FullCPTNode(ExampleRV.CLOUDY_RV, new double[]
            {
                0.5, 0.5
            });
            FiniteNode sprinkler = new FullCPTNode(ExampleRV.SPRINKLER_RV,
                                                   new double[]
            {
                // Cloudy=true, Sprinkler=true
                0.1,
                // Cloudy=true, Sprinkler=false
                0.9,
                // Cloudy=false, Sprinkler=true
                0.5,
                // Cloudy=false, Sprinkler=false
                0.5
            }, cloudy);
            FiniteNode rain = new FullCPTNode(ExampleRV.RAIN_RV, new double[]
            {
                // Cloudy=true, Rain=true
                0.8,
                // Cloudy=true, Rain=false
                0.2,
                // Cloudy=false, Rain=true
                0.2,
                // Cloudy=false, Rain=false
                0.8
            }, cloudy);

            FiniteNode wetGrass = new FullCPTNode(ExampleRV.WET_GRASS_RV,
                                                  new double[]
            {
                // Sprinkler=true, Rain=true, WetGrass=true
                .99,
                // Sprinkler=true, Rain=true, WetGrass=false
                .01,
                // Sprinkler=true, Rain=false, WetGrass=true
                .9,
                // Sprinkler=true, Rain=false, WetGrass=false
                .1,
                // Sprinkler=false, Rain=true, WetGrass=true
                .9,
                // Sprinkler=false, Rain=true, WetGrass=false
                .1,
                // Sprinkler=false, Rain=false, WetGrass=true
                0.0,
                // Sprinkler=false, Rain=false, WetGrass=false
                1.0
            }, sprinkler, rain);

            return(new BayesNet(cloudy));
        }
Exemplo n.º 7
0
        public static IBayesianNetwork construct2FairDiceNetwor()
        {
            IFiniteNode dice1 = new FullCPTNode(ExampleRV.DICE_1_RV, 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
            });
            IFiniteNode dice2 = new FullCPTNode(ExampleRV.DICE_2_RV, 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
            });

            return(new BayesNet(dice1, dice2));
        }
Exemplo n.º 8
0
        public static BayesianNetwork construct2FairDiceNetwor()
        {
            FiniteNode dice1 = new FullCPTNode(ExampleRV.DICE_1_RV, 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
                                                                        });
            FiniteNode dice2 = new FullCPTNode(ExampleRV.DICE_2_RV, 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
                                                                        });

            return new BayesNet(dice1, dice2);
        }
Exemplo n.º 9
0
        public static BayesianNetwork constructToothacheCavityCatchWeatherNetwork()
        {
            FiniteNode cavity = new FullCPTNode(ExampleRV.CAVITY_RV, new double[]
                                                                         {
                                                                             0.2, 0.8
                                                                         });
            FiniteNode toothache = new FullCPTNode(ExampleRV.TOOTHACHE_RV,
                                                   new double[]
                                                       {
                                                           // C=true, T=true
                                                           0.6,
                                                           // C=true, T=false
                                                           0.4,
                                                           // C=false, T=true
                                                           0.1,
                                                           // C=false, T=false
                                                           0.9

                                                       }, cavity);

            FiniteNode catchN = new FullCPTNode(ExampleRV.CATCH_RV, new double[]
                                                                        {
                                                                            // C=true, Catch=true
                                                                            0.9,
                                                                            // C=true, Catch=false
                                                                            0.1,
                                                                            // C=false, Catch=true
                                                                            0.2,
                                                                            // C=false, Catch=false
                                                                            0.8
                                                                        }, cavity);
            FiniteNode weather = new FullCPTNode(ExampleRV.WEATHER_RV,
                                                 new double[]
                                                     {
                                                         // sunny
                                                         0.6,
                                                         // rain
                                                         0.1,
                                                         // cloudy
                                                         0.29,
                                                         // snow
                                                         0.01
                                                     });

            return new BayesNet(cavity, weather);
        }
Exemplo n.º 10
0
        public static BayesianNetwork constructToothacheCavityCatchWeatherNetwork()
        {
            FiniteNode cavity = new FullCPTNode(ExampleRV.CAVITY_RV, new double[]
            {
                0.2, 0.8
            });
            FiniteNode toothache = new FullCPTNode(ExampleRV.TOOTHACHE_RV,
                                                   new double[]
            {
                // C=true, T=true
                0.6,
                // C=true, T=false
                0.4,
                // C=false, T=true
                0.1,
                // C=false, T=false
                0.9
            }, cavity);

            FiniteNode catchN = new FullCPTNode(ExampleRV.CATCH_RV, new double[]
            {
                // C=true, Catch=true
                0.9,
                // C=true, Catch=false
                0.1,
                // C=false, Catch=true
                0.2,
                // C=false, Catch=false
                0.8
            }, cavity);
            FiniteNode weather = new FullCPTNode(ExampleRV.WEATHER_RV,
                                                 new double[]
            {
                // sunny
                0.6,
                // rain
                0.1,
                // cloudy
                0.29,
                // snow
                0.01
            });

            return(new BayesNet(cavity, weather));
        }
Exemplo n.º 11
0
        /**
         * Return a Dynamic Bayesian Network of the Umbrella World Network.
         *
         * @return a Dynamic Bayesian Network of the Umbrella World Network.
         */

        public static DynamicBayesianNetwork getUmbrellaWorldNetwork()
        {
            FiniteNode prior_rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV,
                                                        new double[] { 0.5, 0.5 });

            BayesNet priorNetwork = new BayesNet(prior_rain_tm1);

            // Prior belief state
            FiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV,
                                                  new double[] { 0.5, 0.5 });
            // Transition Model
            FiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[]
            {
                // R_t-1 = true, R_t = true
                0.7,
                // R_t-1 = true, R_t = false
                0.3,
                // R_t-1 = false, R_t = true
                0.3,
                // R_t-1 = false, R_t = false
                0.7
            }, rain_tm1);
            // Sensor Model
            FiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV,
                                                     new double[]
            {
                // R_t = true, U_t = true
                0.9,
                // R_t = true, U_t = false
                0.1,
                // R_t = false, U_t = true
                0.2,
                // R_t = false, U_t = false
                0.8
            }, rain_t);

            Map <RandomVariable, RandomVariable> X_0_to_X_1 = new HashMap <RandomVariable, RandomVariable>();

            X_0_to_X_1.put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV);
            Set <RandomVariable> E_1 = new HashSet <RandomVariable>();

            E_1.add(ExampleRV.UMBREALLA_t_RV);

            return(new DynamicBayesNet(priorNetwork, X_0_to_X_1, E_1, rain_tm1));
        }
        /**
         * Return a Dynamic Bayesian Network of the Umbrella World Network.
         * 
         * @return a Dynamic Bayesian Network of the Umbrella World Network.
         */

        public static DynamicBayesianNetwork getUmbrellaWorldNetwork()
        {
            FiniteNode prior_rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV,
                                                        new double[] {0.5, 0.5});

            BayesNet priorNetwork = new BayesNet(prior_rain_tm1);

            // Prior belief state
            FiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV,
                                                  new double[] {0.5, 0.5});
            // Transition Model
            FiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[]
                                                                         {
                                                                             // R_t-1 = true, R_t = true
                                                                             0.7,
                                                                             // R_t-1 = true, R_t = false
                                                                             0.3,
                                                                             // R_t-1 = false, R_t = true
                                                                             0.3,
                                                                             // R_t-1 = false, R_t = false
                                                                             0.7
                                                                         }, rain_tm1);
            // Sensor Model
            FiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV,
                                                     new double[]
                                                         {
                                                             // R_t = true, U_t = true
                                                             0.9,
                                                             // R_t = true, U_t = false
                                                             0.1,
                                                             // R_t = false, U_t = true
                                                             0.2,
                                                             // R_t = false, U_t = false
                                                             0.8
                                                         }, rain_t);

            Map<RandomVariable, RandomVariable> X_0_to_X_1 = new HashMap<RandomVariable, RandomVariable>();
            X_0_to_X_1.put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV);
            Set<RandomVariable> E_1 = new HashSet<RandomVariable>();
            E_1.add(ExampleRV.UMBREALLA_t_RV);

            return new DynamicBayesNet(priorNetwork, X_0_to_X_1, E_1, rain_tm1);
        }
Exemplo n.º 13
0
        public static IBayesianNetwork constructMeningitisStiffNeckNetwork()
        {
            IFiniteNode meningitis = new FullCPTNode(ExampleRV.MENINGITIS_RV,
                                                     new double[] { 1.0 / 50000.0, 1.0 - (1.0 / 50000.0) });

            IFiniteNode stiffneck = new FullCPTNode(ExampleRV.STIFF_NECK_RV,
                                                    new double[] {
                // M=true, S=true
                0.7,
                // M=true, S=false
                0.3,
                // M=false, S=true
                0.009986199723994478,
                // M=false, S=false
                0.9900138002760055
            }, meningitis);

            return(new BayesNet(meningitis));
        }
        /**
         * Return a Dynamic Bayesian Network of the Umbrella World Network.
         *
         * @return a Dynamic Bayesian Network of the Umbrella World Network.
         */
        public static IDynamicBayesianNetwork getUmbrellaWorldNetwork()
        {
            IFiniteNode prior_rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 });

            BayesNet priorNetwork = new BayesNet(prior_rain_tm1);

            // Prior belief state
            IFiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 });
            // Transition Model
            IFiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[] {
                // R_t-1 = true, R_t = true
                0.7,
                // R_t-1 = true, R_t = false
                0.3,
                // R_t-1 = false, R_t = true
                0.3,
                // R_t-1 = false, R_t = false
                0.7
            }, rain_tm1);
            // Sensor Model

            IFiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV,
                                                      new double[] {
                // R_t = true, U_t = true
                0.9,
                // R_t = true, U_t = false
                0.1,
                // R_t = false, U_t = true
                0.2,
                // R_t = false, U_t = false
                0.8
            }, rain_t);

            IMap <IRandomVariable, IRandomVariable> X_0_to_X_1 = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, IRandomVariable>();

            X_0_to_X_1.Put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV);
            ISet <IRandomVariable> E_1 = CollectionFactory.CreateSet <IRandomVariable>();

            E_1.Add(ExampleRV.UMBREALLA_t_RV);

            return(new DynamicBayesNet(priorNetwork, X_0_to_X_1, E_1, rain_tm1));
        }
Exemplo n.º 15
0
        public static BayesianNetwork constructCloudySprinklerRainWetGrassNetwork()
        {
            FiniteNode cloudy = new FullCPTNode(ExampleRV.CLOUDY_RV, new double[]
                                                                         {
                                                                             0.5, 0.5
                                                                         });
            FiniteNode sprinkler = new FullCPTNode(ExampleRV.SPRINKLER_RV,
                                                   new double[]
                                                       {
                                                           // Cloudy=true, Sprinkler=true
                                                           0.1,
                                                           // Cloudy=true, Sprinkler=false
                                                           0.9,
                                                           // Cloudy=false, Sprinkler=true
                                                           0.5,
                                                           // Cloudy=false, Sprinkler=false
                                                           0.5
                                                       }, cloudy);
            FiniteNode rain = new FullCPTNode(ExampleRV.RAIN_RV, new double[]
                                                                     {
                                                                         // Cloudy=true, Rain=true
                                                                         0.8,
                                                                         // Cloudy=true, Rain=false
                                                                         0.2,
                                                                         // Cloudy=false, Rain=true
                                                                         0.2,
                                                                         // Cloudy=false, Rain=false
                                                                         0.8
                                                                     }, cloudy);

            FiniteNode wetGrass = new FullCPTNode(ExampleRV.WET_GRASS_RV,
                                                  new double[]
                                                      {
                                                          // Sprinkler=true, Rain=true, WetGrass=true
                                                          .99,
                                                          // Sprinkler=true, Rain=true, WetGrass=false
                                                          .01,
                                                          // Sprinkler=true, Rain=false, WetGrass=true
                                                          .9,
                                                          // Sprinkler=true, Rain=false, WetGrass=false
                                                          .1,
                                                          // Sprinkler=false, Rain=true, WetGrass=true
                                                          .9,
                                                          // Sprinkler=false, Rain=true, WetGrass=false
                                                          .1,
                                                          // Sprinkler=false, Rain=false, WetGrass=true
                                                          0.0,
                                                          // Sprinkler=false, Rain=false, WetGrass=false
                                                          1.0
                                                      }, sprinkler, rain);

            return new BayesNet(cloudy);
        }
Exemplo n.º 16
0
        public static BayesianNetwork constructBurglaryAlarmNetwork()
        {
            FiniteNode burglary = new FullCPTNode(ExampleRV.BURGLARY_RV,
                                                  new double[] {0.001, 0.999});
            FiniteNode earthquake = new FullCPTNode(ExampleRV.EARTHQUAKE_RV,
                                                    new double[] {0.002, 0.998});
            FiniteNode alarm = new FullCPTNode(ExampleRV.ALARM_RV, new double[]
                                                                       {
                                                                           // B=true, E=true, A=true
                                                                           0.95,
                                                                           // B=true, E=true, A=false
                                                                           0.05,
                                                                           // B=true, E=false, A=true
                                                                           0.94,
                                                                           // B=true, E=false, A=false
                                                                           0.06,
                                                                           // B=false, E=true, A=true
                                                                           0.29,
                                                                           // B=false, E=true, A=false
                                                                           0.71,
                                                                           // B=false, E=false, A=true
                                                                           0.001,
                                                                           // B=false, E=false, A=false
                                                                           0.999
                                                                       }, burglary, earthquake);

            FiniteNode johnCalls = new FullCPTNode(ExampleRV.JOHN_CALLS_RV,
                                                   new double[]
                                                       {
                                                           // A=true, J=true
                                                           0.90,
                                                           // A=true, J=false
                                                           0.10,
                                                           // A=false, J=true
                                                           0.05,
                                                           // A=false, J=false
                                                           0.95
                                                       }, alarm);

            FiniteNode maryCalls = new FullCPTNode(ExampleRV.MARY_CALLS_RV,
                                                   new double[]
                                                       {
                                                           // A=true, M=true
                                                           0.70,
                                                           // A=true, M=false
                                                           0.30,
                                                           // A=false, M=true
                                                           0.01,
                                                           // A=false, M=false
                                                           0.99
                                                       }, alarm);

            return new BayesNet(burglary, earthquake);
        }
Exemplo n.º 17
0
        public static BayesianNetwork constructMeningitisStiffNeckNetwork()
        {
            FiniteNode meningitis = new FullCPTNode(ExampleRV.MENINGITIS_RV,
                                                    new double[] {1.0/50000.0, 1.0 - (1.0/50000.0)});

            FiniteNode stiffneck = new FullCPTNode(ExampleRV.STIFF_NECK_RV,
                                                   new double[]
                                                       {
                                                           // M=true, S=true
                                                           0.7,
                                                           // M=true, S=false
                                                           0.3,
                                                           // M=false, S=true
                                                           0.009986199723994478,
                                                           // M=false, S=false
                                                           0.9900138002760055

                                                       }, meningitis);
            return new BayesNet(meningitis);
        }