public void test_RollingPairFairDiceModel(ProbabilityModel model)
        {
            Assert.IsTrue(model.isValid());

            // Ensure each dice has 1/6 probability
            for (int d = 1; d <= 6; d++)
            {
                AssignmentProposition ad1 = new AssignmentProposition(
                    ExampleRV.DICE_1_RV, d);
                AssignmentProposition ad2 = new AssignmentProposition(
                    ExampleRV.DICE_2_RV, d);

                Assert.AreEqual(1.0 / 6.0, model.prior(ad1), DELTA_THRESHOLD);
                Assert.AreEqual(1.0 / 6.0, model.prior(ad2), DELTA_THRESHOLD);
            }

            // Ensure each combination is 1/36
            for (int d1 = 1; d1 <= 6; d1++)
            {
                for (int d2 = 1; d2 <= 6; d2++)
                {
                    AssignmentProposition ad1 = new AssignmentProposition(
                        ExampleRV.DICE_1_RV, d1);
                    AssignmentProposition ad2 = new AssignmentProposition(
                        ExampleRV.DICE_2_RV, d2);
                    ConjunctiveProposition d1AndD2 = new ConjunctiveProposition(
                        ad1, ad2);

                    Assert.AreEqual(1.0 / 6.0, model.prior(ad1),
                                    DELTA_THRESHOLD);
                    Assert.AreEqual(1.0 / 6.0, model.prior(ad2),
                                    DELTA_THRESHOLD);

                    // pg. 485 AIMA3e
                    Assert.AreEqual(1.0 / 36.0, model.prior(ad1, ad2),
                                    DELTA_THRESHOLD);
                    Assert.AreEqual(1.0 / 36.0, model.prior(d1AndD2),
                                    DELTA_THRESHOLD);

                    Assert.AreEqual(1.0 / 6.0, model.posterior(ad1, ad2),
                                    DELTA_THRESHOLD);
                    Assert.AreEqual(1.0 / 6.0, model.posterior(ad2, ad1),
                                    DELTA_THRESHOLD);
                }
            }

            // Test Sets of events defined via constraint propositions
            IntegerSumProposition total11 = new IntegerSumProposition("Total11",
                                                                      new FiniteIntegerDomain(11), ExampleRV.DICE_1_RV,
                                                                      ExampleRV.DICE_2_RV);
            Assert.AreEqual(2.0 / 36.0, model.prior(total11), DELTA_THRESHOLD);
            EquivalentProposition doubles = new EquivalentProposition("Doubles",
                                                                      ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);
            Assert.AreEqual(1.0 / 6.0, model.prior(doubles), DELTA_THRESHOLD);
            SubsetProposition evenDice1 = new SubsetProposition("EvenDice1",
                                                                new FiniteIntegerDomain(2, 4, 6), ExampleRV.DICE_1_RV);
            Assert.AreEqual(0.5, model.prior(evenDice1), DELTA_THRESHOLD);
            SubsetProposition oddDice2 = new SubsetProposition("OddDice2",
                                                               new FiniteIntegerDomain(1, 3, 5), ExampleRV.DICE_2_RV);
            Assert.AreEqual(0.5, model.prior(oddDice2), DELTA_THRESHOLD);

            // pg. 485 AIMA3e
            AssignmentProposition dice1Is5 = new AssignmentProposition(
                ExampleRV.DICE_1_RV, 5);
            Assert.AreEqual(1.0 / 6.0, model.posterior(doubles, dice1Is5),
                            DELTA_THRESHOLD);

            Assert.AreEqual(1.0, model.prior(ExampleRV.DICE_1_RV),
                            DELTA_THRESHOLD);
            Assert.AreEqual(1.0, model.prior(ExampleRV.DICE_2_RV),
                            DELTA_THRESHOLD);
            Assert.AreEqual(1.0,
                            model.posterior(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV),
                            DELTA_THRESHOLD);
            Assert.AreEqual(1.0,
                            model.posterior(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV),
                            DELTA_THRESHOLD);

            // Test a disjunctive proposition pg.489
            // P(a OR b) = P(a) + P(b) - P(a AND b)
            // = 1/6 + 1/6 - 1/36
            AssignmentProposition dice2Is5 = new AssignmentProposition(
                ExampleRV.DICE_2_RV, 5);
            DisjunctiveProposition dice1Is5OrDice2Is5 = new DisjunctiveProposition(
                dice1Is5, dice2Is5);
            Assert.AreEqual(1.0 / 6.0 + 1.0 / 6.0 - 1.0 / 36.0,
                            model.prior(dice1Is5OrDice2Is5), DELTA_THRESHOLD);
        }
        //
        // PROTECTED METHODS
        //
        protected void test_RollingPairFairDiceModel(IProbabilityModel model)
        {
            Assert.IsTrue(model.isValid());

            // Ensure each dice has 1/6 probability
            for (int d = 1; d <= 6; d++)
            {
                AssignmentProposition ad1 = new AssignmentProposition(
                    ExampleRV.DICE_1_RV, d);
                AssignmentProposition ad2 = new AssignmentProposition(
                    ExampleRV.DICE_2_RV, d);

                Assert.AreEqual(1.0 / 6.0, model.prior(ad1), DELTA_THRESHOLD);
                Assert.AreEqual(1.0 / 6.0, model.prior(ad2), DELTA_THRESHOLD);
            }

            // Ensure each combination is 1/36
            for (int d1 = 1; d1 <= 6; d1++)
            {
                for (int d2 = 1; d2 <= 6; d2++)
                {
                    AssignmentProposition ad1 = new AssignmentProposition(
                        ExampleRV.DICE_1_RV, d1);
                    AssignmentProposition ad2 = new AssignmentProposition(
                        ExampleRV.DICE_2_RV, d2);
                    ConjunctiveProposition d1AndD2 = new ConjunctiveProposition(
                        ad1, ad2);

                    Assert.AreEqual(1.0 / 6.0, model.prior(ad1),
                                    DELTA_THRESHOLD);
                    Assert.AreEqual(1.0 / 6.0, model.prior(ad2),
                                    DELTA_THRESHOLD);

                    // pg. 485 AIMA3e
                    Assert.AreEqual(1.0 / 36.0, model.prior(ad1, ad2),
                                    DELTA_THRESHOLD);
                    Assert.AreEqual(1.0 / 36.0, model.prior(d1AndD2),
                                    DELTA_THRESHOLD);

                    Assert.AreEqual(1.0 / 6.0, model.posterior(ad1, ad2),
                                    DELTA_THRESHOLD);
                    Assert.AreEqual(1.0 / 6.0, model.posterior(ad2, ad1),
                                    DELTA_THRESHOLD);
                }
            }

            // Test Sets of events defined via constraint propositions
            IntegerSumProposition total11 = new IntegerSumProposition("Total11",
                                                                      new FiniteIntegerDomain(11), ExampleRV.DICE_1_RV,
                                                                      ExampleRV.DICE_2_RV);

            Assert.AreEqual(2.0 / 36.0, model.prior(total11), DELTA_THRESHOLD);
            EquivalentProposition doubles = new EquivalentProposition("Doubles",
                                                                      ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            Assert.AreEqual(1.0 / 6.0, model.prior(doubles), DELTA_THRESHOLD);
            SubsetProposition evenDice1 = new SubsetProposition("EvenDice1",
                                                                new FiniteIntegerDomain(2, 4, 6), ExampleRV.DICE_1_RV);

            Assert.AreEqual(0.5, model.prior(evenDice1), DELTA_THRESHOLD);
            SubsetProposition oddDice2 = new SubsetProposition("OddDice2",
                                                               new FiniteIntegerDomain(1, 3, 5), ExampleRV.DICE_2_RV);

            Assert.AreEqual(0.5, model.prior(oddDice2), DELTA_THRESHOLD);

            // pg. 485 AIMA3e
            AssignmentProposition dice1Is5 = new AssignmentProposition(
                ExampleRV.DICE_1_RV, 5);

            Assert.AreEqual(1.0 / 6.0, model.posterior(doubles, dice1Is5),
                            DELTA_THRESHOLD);

            Assert.AreEqual(1.0, model.prior(ExampleRV.DICE_1_RV),
                            DELTA_THRESHOLD);
            Assert.AreEqual(1.0, model.prior(ExampleRV.DICE_2_RV),
                            DELTA_THRESHOLD);
            Assert.AreEqual(1.0,
                            model.posterior(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV),
                            DELTA_THRESHOLD);
            Assert.AreEqual(1.0,
                            model.posterior(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV),
                            DELTA_THRESHOLD);

            // Test a disjunctive proposition pg.489
            // P(a OR b) = P(a) + P(b) - P(a AND b)
            // = 1/6 + 1/6 - 1/36
            AssignmentProposition dice2Is5 = new AssignmentProposition(
                ExampleRV.DICE_2_RV, 5);
            DisjunctiveProposition dice1Is5OrDice2Is5 = new DisjunctiveProposition(
                dice1Is5, dice2Is5);

            Assert.AreEqual(1.0 / 6.0 + 1.0 / 6.0 - 1.0 / 36.0,
                            model.prior(dice1Is5OrDice2Is5), DELTA_THRESHOLD);
        }
Ejemplo n.º 3
0
        //
        // PROTECTED
        //
        protected void test_RollingPairFairDiceModel_Distributions(IFiniteProbabilityModel model)
        {
            AssignmentProposition    ad1_1 = new AssignmentProposition(ExampleRV.DICE_1_RV, 1);
            ICategoricalDistribution dD1_1 = model.priorDistribution(ad1_1);

            assertArrayEquals(new double[] { 1.0 / 6.0 }, dD1_1.getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dPriorDice1 = model.priorDistribution(ExampleRV.DICE_1_RV);

            assertArrayEquals(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 },
                              dPriorDice1.getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dPriorDice2 = model.priorDistribution(ExampleRV.DICE_2_RV);

            assertArrayEquals(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 },
                              dPriorDice2.getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dJointDice1Dice2 = model.jointDistribution(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            Assert.AreEqual(36, dJointDice1Dice2.getValues().Length);
            for (int i = 0; i < dJointDice1Dice2.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 36.0, dJointDice1Dice2.getValues()[i], DELTA_THRESHOLD);
            }

            ICategoricalDistribution dJointDice2Dice1 = model.jointDistribution(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV);

            Assert.AreEqual(36, dJointDice2Dice1.getValues().Length);
            for (int i = 0; i < dJointDice2Dice1.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 36.0, dJointDice2Dice1.getValues()[i], DELTA_THRESHOLD);
            }

            //
            // Test Sets of events
            IntegerSumProposition total11 = new IntegerSumProposition("Total",
                                                                      new FiniteIntegerDomain(11), ExampleRV.DICE_1_RV,
                                                                      ExampleRV.DICE_2_RV);

            // P<>(Total = 11) = <2.0/36.0>
            assertArrayEquals(new double[] { 2.0 / 36.0 }, model.priorDistribution(total11).getValues(), DELTA_THRESHOLD);

            // P<>(Dice1, Total = 11)
            // = <0.0, 0.0, 0.0, 0.0, 1.0/36.0, 1.0/36.0>
            assertArrayEquals(new double[] { 0, 0, 0, 0, 1.0 / 36.0, 1.0 / 36.0 },
                              model.priorDistribution(ExampleRV.DICE_1_RV, total11)
                              .getValues(), DELTA_THRESHOLD);

            EquivalentProposition doubles = new EquivalentProposition("Doubles",
                                                                      ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            // P(Doubles) = <1.0/6.0>
            assertArrayEquals(new double[] { 1.0 / 6.0 }, model
                              .priorDistribution(doubles).getValues(), DELTA_THRESHOLD);

            //
            // Test posterior
            //
            // P<>(Dice1, Total = 11)
            // = <0.0, 0.0, 0.0, 0.0, 0.5, 0.5>
            assertArrayEquals(new double[] { 0, 0, 0, 0, 0.5, 0.5 }, model
                              .posteriorDistribution(ExampleRV.DICE_1_RV, total11)
                              .getValues(), DELTA_THRESHOLD);

            // P<>(Dice1 | Doubles) = <1/6, 1/6, 1/6, 1/6, 1/6, 1/6>
            assertArrayEquals(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 }, model
                              .posteriorDistribution(ExampleRV.DICE_1_RV, doubles)
                              .getValues(), DELTA_THRESHOLD);

            ICategoricalDistribution dPosteriorDice1GivenDice2 = model
                                                                 .posteriorDistribution(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);

            Assert.AreEqual(36, dPosteriorDice1GivenDice2.getValues().Length);
            for (int i = 0; i < dPosteriorDice1GivenDice2.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 6.0, dPosteriorDice1GivenDice2.getValues()[i], DELTA_THRESHOLD);
            }

            ICategoricalDistribution dPosteriorDice2GivenDice1 = model
                                                                 .posteriorDistribution(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV);

            Assert.AreEqual(36, dPosteriorDice2GivenDice1.getValues().Length);
            for (int i = 0; i < dPosteriorDice2GivenDice1.getValues().Length; i++)
            {
                Assert.AreEqual(1.0 / 6.0, dPosteriorDice2GivenDice1.getValues()[i], DELTA_THRESHOLD);
            }
        }