AIMA.Test.Core.Unit.Probability.CommonProbabilityModelTests.test_ToothacheCavityCatchWeatherModel C# (CSharp) Method

test_ToothacheCavityCatchWeatherModel() protected method

protected test_ToothacheCavityCatchWeatherModel ( ProbabilityModel model ) : void
model AIMA.Probability.ProbabilityModel
return void
        protected void test_ToothacheCavityCatchWeatherModel(ProbabilityModel model)
        {

            // Should be able to run all the same queries for this independent
            // sub model.
            test_ToothacheCavityCatchModel(model);

            // AIMA3e pg. 486
            AssignmentProposition asunny = new AssignmentProposition(
                ExampleRV.WEATHER_RV, "sunny");
            AssignmentProposition arain = new AssignmentProposition(
                ExampleRV.WEATHER_RV, "rain");
            AssignmentProposition acloudy = new AssignmentProposition(
                ExampleRV.WEATHER_RV, "cloudy");
            AssignmentProposition asnow = new AssignmentProposition(
                ExampleRV.WEATHER_RV, "snow");

            Assert.AreEqual(0.6, model.prior(asunny), DELTA_THRESHOLD);
            Assert(0.1, model.prior(arain), DELTA_THRESHOLD);
            Assert.AreEqual(0.29, model.prior(acloudy), DELTA_THRESHOLD);
            Assert.AreEqual(0.01, model.prior(asnow), DELTA_THRESHOLD);

            // AIMA3e pg. 488
            // P(sunny, cavity)
            // P(sunny AND cavity)
            AssignmentProposition atoothache = new AssignmentProposition(
                ExampleRV.TOOTHACHE_RV, Boolean.TRUE);
            AssignmentProposition acatch = new AssignmentProposition(
                ExampleRV.CATCH_RV, Boolean.TRUE);
            AssignmentProposition acavity = new AssignmentProposition(
                ExampleRV.CAVITY_RV, Boolean.TRUE);
            ConjunctiveProposition sunnyAndCavity = new ConjunctiveProposition(
                asunny, acavity);

            // 0.6 (sunny) * 0.2 (cavity) = 0.12
            Assert.AreEqual(0.12, model.prior(asunny, acavity), DELTA_THRESHOLD);
            Assert.AreEqual(0.12, model.prior(sunnyAndCavity), DELTA_THRESHOLD);

            // AIMA3e pg. 494
            // P(toothache, catch, cavity, cloudy) =
            // P(cloudy | toothache, catch, cavity)P(toothache, catch, cavity)
            Assert.AreEqual(
                model.prior(atoothache, acatch, acavity, acloudy),
                model.posterior(acloudy, atoothache, acatch, acavity)
                * model.prior(atoothache, acatch, acavity),
                DELTA_THRESHOLD);
            ConjunctiveProposition toothacheAndCatchAndCavityAndCloudy = new ConjunctiveProposition(
                new ConjunctiveProposition(atoothache, acatch),
                new ConjunctiveProposition(acavity, acloudy));
            ConjunctiveProposition toothacheAndCatchAndCavity = new ConjunctiveProposition(
                new ConjunctiveProposition(atoothache, acatch), acavity);
            Assert.AreEqual(
                model.prior(toothacheAndCatchAndCavityAndCloudy),
                model.posterior(acloudy, atoothache, acatch, acavity)
                * model.prior(toothacheAndCatchAndCavity),
                DELTA_THRESHOLD);

            // P(cloudy | toothache, catch, cavity) = P(cloudy)
            // (13.10)
            Assert.AreEqual(
                model.posterior(acloudy, atoothache, acatch, acavity),
                model.prior(acloudy), DELTA_THRESHOLD);

            // P(toothache, catch, cavity, cloudy) =
            // P(cloudy)P(tootache, catch, cavity)
            Assert.AreEqual(
                model.prior(atoothache, acatch, acavity, acloudy),
                model.prior(acloudy) * model.prior(atoothache, acatch, acavity),
                DELTA_THRESHOLD);

            // P(a | b) = P(a)
            Assert.AreEqual(model.posterior(acavity, acloudy),
                            model.prior(acavity), DELTA_THRESHOLD);
            // P(b | a) = P(b)
            Assert.AreEqual(model.posterior(acloudy, acavity),
                            model.prior(acloudy), DELTA_THRESHOLD);
            // P(a AND b) = P(a)P(b)
            Assert.AreEqual(model.prior(acavity, acloudy), model.prior(acavity)
                                                           * model.prior(acloudy), DELTA_THRESHOLD);
            ConjunctiveProposition acavityAndacloudy = new ConjunctiveProposition(
                acavity, acloudy);
            Assert.AreEqual(model.prior(acavityAndacloudy),
                            model.prior(acavity) * model.prior(acloudy), DELTA_THRESHOLD);
        }