public virtual double prior(params IProposition[] phi) { // Calculating the prior, therefore no relevant evidence // just query over the scope of proposition phi in order // to get a joint distribution for these IProposition conjunct = ProbUtil.constructConjunction(phi); IRandomVariable[] X = conjunct.getScope().ToArray(); ICategoricalDistribution d = bayesInference.Ask(X, new AssignmentProposition[0], bayesNet); // Then calculate the probability of the propositions phi // be seeing where they hold. double[] probSum = new double[1]; CategoricalDistributionIterator di = new CategoricalDistributionIteraorPrior(conjunct, probSum); d.iterateOver(di); return(probSum[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); }