protected static void demoBurglaryAlarmModel(IFiniteProbabilityModel model) { System.Console.WriteLine("--------------------"); System.Console.WriteLine("Burglary Alarm Model"); System.Console.WriteLine("--------------------"); AssignmentProposition aburglary = new AssignmentProposition( ExampleRV.BURGLARY_RV, true); AssignmentProposition anotburglary = new AssignmentProposition( ExampleRV.BURGLARY_RV, false); AssignmentProposition anotearthquake = new AssignmentProposition( ExampleRV.EARTHQUAKE_RV, false); AssignmentProposition aalarm = new AssignmentProposition( ExampleRV.ALARM_RV, true); AssignmentProposition anotalarm = new AssignmentProposition( ExampleRV.ALARM_RV, false); AssignmentProposition ajohnCalls = new AssignmentProposition( ExampleRV.JOHN_CALLS_RV, true); AssignmentProposition amaryCalls = new AssignmentProposition( ExampleRV.MARY_CALLS_RV, true); // AIMA3e pg. 514 System.Console.WriteLine("P(j,m,a,~b,~e) = " + model.prior(ajohnCalls, amaryCalls, aalarm, anotburglary, anotearthquake)); System.Console.WriteLine("P(j,m,~a,~b,~e) = " + model.prior(ajohnCalls, amaryCalls, anotalarm, anotburglary, anotearthquake)); // AIMA3e. pg. 514 // P<>(Alarm | JohnCalls = true, MaryCalls = true, Burglary = false, // Earthquake = false) // = <0.558, 0.442> System.Console .WriteLine("P<>(Alarm | JohnCalls = true, MaryCalls = true, Burglary = false, Earthquake = false) = " + model.posteriorDistribution(ExampleRV.ALARM_RV, ajohnCalls, amaryCalls, anotburglary, anotearthquake)); // AIMA3e pg. 523 // P<>(Burglary | JohnCalls = true, MaryCalls = true) = <0.284, 0.716> System.Console .WriteLine("P<>(Burglary | JohnCalls = true, MaryCalls = true) = " + model.posteriorDistribution(ExampleRV.BURGLARY_RV, ajohnCalls, amaryCalls)); // AIMA3e pg. 528 // P<>(JohnCalls | Burglary = true) System.Console.WriteLine("P<>(JohnCalls | Burglary = true) = " + model.posteriorDistribution(ExampleRV.JOHN_CALLS_RV, aburglary)); }
protected static void demoToothacheCavityCatchModel(IFiniteProbabilityModel model) { System.Console.WriteLine("Toothache, Cavity, and Catch Model"); System.Console.WriteLine("----------------------------------"); AssignmentProposition atoothache = new AssignmentProposition( ExampleRV.TOOTHACHE_RV, true); AssignmentProposition acavity = new AssignmentProposition( ExampleRV.CAVITY_RV, true); AssignmentProposition anotcavity = new AssignmentProposition( ExampleRV.CAVITY_RV, false); AssignmentProposition acatch = new AssignmentProposition( ExampleRV.CATCH_RV, true); // AIMA3e pg. 485 System.Console.WriteLine("P(cavity) = " + model.prior(acavity)); System.Console.WriteLine("P(cavity | toothache) = " + model.posterior(acavity, atoothache)); // AIMA3e pg. 492 DisjunctiveProposition cavityOrToothache = new DisjunctiveProposition( acavity, atoothache); System.Console.WriteLine("P(cavity OR toothache) = " + model.prior(cavityOrToothache)); // AIMA3e pg. 493 System.Console.WriteLine("P(~cavity | toothache) = " + model.posterior(anotcavity, atoothache)); // AIMA3e pg. 493 // P<>(Cavity | toothache) = <0.6, 0.4> System.Console.WriteLine("P<>(Cavity | toothache) = " + model.posteriorDistribution(ExampleRV.CAVITY_RV, atoothache)); // AIMA3e pg. 497 // P<>(Cavity | toothache AND catch) = <0.871, 0.129> System.Console.WriteLine("P<>(Cavity | toothache AND catch) = " + model.posteriorDistribution(ExampleRV.CAVITY_RV, atoothache, acatch)); }
protected void test_BurglaryAlarmModel_Distributions( IFiniteProbabilityModel model) { AssignmentProposition aburglary = new AssignmentProposition( ExampleRV.BURGLARY_RV, true); AssignmentProposition anotburglary = new AssignmentProposition( ExampleRV.BURGLARY_RV, false); AssignmentProposition anotearthquake = new AssignmentProposition( ExampleRV.EARTHQUAKE_RV, false); AssignmentProposition ajohnCalls = new AssignmentProposition( ExampleRV.JOHN_CALLS_RV, true); AssignmentProposition amaryCalls = new AssignmentProposition( ExampleRV.MARY_CALLS_RV, true); // AIMA3e. pg. 514 // P<>(Alarm | JohnCalls = true, MaryCalls = true, Burglary = false, // Earthquake = false) // = <0.558, 0.442> assertArrayEquals( new double[] { 0.5577689243027888, 0.44223107569721115 }, model.posteriorDistribution(ExampleRV.ALARM_RV, ajohnCalls, amaryCalls, anotburglary, anotearthquake).getValues(), DELTA_THRESHOLD); // AIMA3e pg. 523 // P<>(Burglary | JohnCalls = true, MaryCalls = true) = <0.284, 0.716> assertArrayEquals( new double[] { 0.2841718353643929, 0.7158281646356071 }, model.posteriorDistribution(ExampleRV.BURGLARY_RV, ajohnCalls, amaryCalls).getValues(), DELTA_THRESHOLD); // AIMA3e pg. 528 // P<>(JohnCalls | Burglary = true) assertArrayEquals(new double[] { 0.8490169999999999, 0.15098299999999998 }, model.posteriorDistribution(ExampleRV.JOHN_CALLS_RV, aburglary) .getValues(), DELTA_THRESHOLD); }
public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability) { // <b>P</b>(X<sub>t+1</sub> | x<sub>t</sub>)* // P(x<sub>t</sub> | e<sub>1:t</sub>) foreach (var av in possibleWorld) { xtVarAssignMap.Get(av.GetKey()).setValue(av.GetValue()); } int i = 0; foreach (double tp in transitionModel.posteriorDistribution(xtp1, xt).getValues()) { s1.setValue(i, s1.getValues()[i] + (tp * probability)); ++i; } }
// AIMA3e pg. 496 protected void test_MeningitisStiffNeckModel_Distributions( IFiniteProbabilityModel model) { AssignmentProposition astiffNeck = new AssignmentProposition( ExampleRV.STIFF_NECK_RV, true); // AIMA3e pg. 497 // P<>(Mengingitis | stiffneck) = α<P(s | m)P(m), P(s | ~m)P(~m)> ICategoricalDistribution dMeningitisGivenStiffNeck = model .posteriorDistribution(ExampleRV.MENINGITIS_RV, astiffNeck); Assert.AreEqual(2, dMeningitisGivenStiffNeck.getValues().Length); Assert.AreEqual(0.0014, dMeningitisGivenStiffNeck.getValues()[0], DELTA_THRESHOLD); Assert.AreEqual(0.9986, dMeningitisGivenStiffNeck.getValues()[1], DELTA_THRESHOLD); }
public ICategoricalDistribution forward(ICategoricalDistribution f1_t, ICollection <AssignmentProposition> e_tp1) { ICategoricalDistribution s1 = new ProbabilityTable(f1_t.getFor()); // Set up required working variables IProposition[] props = new IProposition[s1.getFor().Size()]; int i = 0; foreach (IRandomVariable rv in s1.getFor()) { props[i] = new RandVar(rv.getName(), rv.getDomain()); ++i; } IProposition Xtp1 = ProbUtil.constructConjunction(props); AssignmentProposition[] xt = new AssignmentProposition[tToTm1StateVarMap.Size()]; IMap <IRandomVariable, AssignmentProposition> xtVarAssignMap = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, AssignmentProposition>(); i = 0; foreach (IRandomVariable rv in tToTm1StateVarMap.GetKeys()) { xt[i] = new AssignmentProposition(tToTm1StateVarMap.Get(rv), "<Dummy Value>"); xtVarAssignMap.Put(rv, xt[i]); ++i; } // Step 1: Calculate the 1 time step prediction // ∑<sub>x<sub>t</sub></sub> CategoricalDistributionIterator if1_t = new CategoricalDistributionIteratorImpl(transitionModel, xtVarAssignMap, s1, Xtp1, xt); f1_t.iterateOver(if1_t); // Step 2: multiply by the probability of the evidence // and normalize // <b>P</b>(e<sub>t+1</sub> | X<sub>t+1</sub>) ICategoricalDistribution s2 = sensorModel.posteriorDistribution(ProbUtil .constructConjunction(e_tp1.ToArray()), Xtp1); return(s2.multiplyBy(s1).normalize()); }
public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability) { // Assign current values for x<sub>k+1</sub> foreach (var av in possibleWorld) { x_kp1VarAssignMap.Get(av.GetKey()).setValue(av.GetValue()); } // P(e<sub>k+1</sub> | x<sub>k+1</sub>) // P(e<sub>k+2:t</sub> | x<sub>k+1</sub>) double p = sensorModel.posterior(pe_kp1, x_kp1) * probability; // <b>P</b>(x<sub>k+1</sub> | X<sub>k</sub>) int i = 0; foreach (double tp in transitionModel.posteriorDistribution(x_kp1, xk).getValues()) { b_kp1t.setValue(i, b_kp1t.getValues()[i] + (tp * p)); ++i; } }
// // 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); } }
protected void test_ToothacheCavityCatchModel_Distributions(IFiniteProbabilityModel model) { AssignmentProposition atoothache = new AssignmentProposition(ExampleRV.TOOTHACHE_RV, true); AssignmentProposition anottoothache = new AssignmentProposition(ExampleRV.TOOTHACHE_RV, false); AssignmentProposition acatch = new AssignmentProposition(ExampleRV.CATCH_RV, true); AssignmentProposition anotcatch = new AssignmentProposition(ExampleRV.CATCH_RV, false); // AIMA3e pg. 493 // P<>(Cavity | toothache) = <0.6, 0.4> assertArrayEquals(new double[] { 0.6, 0.4 }, model .posteriorDistribution(ExampleRV.CAVITY_RV, atoothache) .getValues(), DELTA_THRESHOLD); // AIMA3e pg. 497 // P<>(Cavity | toothache AND catch) = <0.871, 0.129> assertArrayEquals(new double[] { 0.8709677419354839, 0.12903225806451615 }, model.posteriorDistribution(ExampleRV.CAVITY_RV, atoothache, acatch).getValues(), DELTA_THRESHOLD); // AIMA3e pg. 498 // (13.17) // P<>(toothache AND catch | Cavity) // = P<>(toothache | Cavity)P<>(catch | Cavity) ConjunctiveProposition toothacheAndCatch = new ConjunctiveProposition(atoothache, acatch); assertArrayEquals(model.posteriorDistribution(toothacheAndCatch, ExampleRV.CAVITY_RV).getValues(), model.posteriorDistribution(atoothache, ExampleRV.CAVITY_RV) .multiplyBy( model.posteriorDistribution(acatch, ExampleRV.CAVITY_RV)).getValues(), DELTA_THRESHOLD); // (13.18) // P<>(Cavity | toothache AND catch) // = αP<>(toothache | Cavity)P<>(catch | Cavity)P(Cavity) assertArrayEquals(model.posteriorDistribution(ExampleRV.CAVITY_RV, toothacheAndCatch).getValues(), model.posteriorDistribution(atoothache, ExampleRV.CAVITY_RV) .multiplyBy( model.posteriorDistribution(acatch, ExampleRV.CAVITY_RV)) .multiplyBy( model.priorDistribution(ExampleRV.CAVITY_RV)) .normalize().getValues(), DELTA_THRESHOLD); // (13.19) // P<>(Toothache, Catch | Cavity) // = P<>(Toothache | Cavity)P<>(Catch | Cavity) ConjunctiveProposition toothacheAndCatchRV = new ConjunctiveProposition(ExampleRV.TOOTHACHE_RV, ExampleRV.CATCH_RV); assertArrayEquals(model.posteriorDistribution(toothacheAndCatchRV, ExampleRV.CAVITY_RV).getValues(), model.posteriorDistribution(ExampleRV.TOOTHACHE_RV, ExampleRV.CAVITY_RV) .multiplyByPOS( model.posteriorDistribution(ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV), ExampleRV.TOOTHACHE_RV, ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV).getValues(), DELTA_THRESHOLD); // (product rule) // P<>(Toothache, Catch, Cavity) // = P<>(Toothache, Catch | Cavity)P<>(Cavity) assertArrayEquals(model.priorDistribution(ExampleRV.TOOTHACHE_RV, ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV).getValues(), model.posteriorDistribution(toothacheAndCatchRV, ExampleRV.CAVITY_RV) .multiplyBy( model.priorDistribution(ExampleRV.CAVITY_RV)) .getValues(), DELTA_THRESHOLD); // (using 13.19) // P<>(Toothache, Catch | Cavity)P<>(Cavity) // = P<>(Toothache | Cavity)P<>(Catch | Cavity)P<>(Cavity) assertArrayEquals(model.posteriorDistribution(toothacheAndCatchRV, ExampleRV.CAVITY_RV) .multiplyBy( model.priorDistribution(ExampleRV.CAVITY_RV)) .getValues(), model.posteriorDistribution(ExampleRV.TOOTHACHE_RV, ExampleRV.CAVITY_RV) .multiplyByPOS( model.posteriorDistribution(ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV) .multiplyBy( model.priorDistribution(ExampleRV.CAVITY_RV)), ExampleRV.TOOTHACHE_RV, ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV).getValues(), DELTA_THRESHOLD); // // P<>(Toothache, Catch, Cavity) // = P<>(Toothache | Cavity)P<>(Catch | Cavity)P<>(Cavity) assertArrayEquals(model.priorDistribution(ExampleRV.TOOTHACHE_RV, ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV).getValues(), model.posteriorDistribution(ExampleRV.TOOTHACHE_RV, ExampleRV.CAVITY_RV) .multiplyByPOS( model.posteriorDistribution(ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV), ExampleRV.TOOTHACHE_RV, ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV) .multiplyBy( model.priorDistribution(ExampleRV.CAVITY_RV)) .getValues(), DELTA_THRESHOLD); // AIMA3e pg. 496 // General case of Bayes' Rule // P<>(Y | X) = P<>(X | Y)P<>(Y)/P<>(X) // Note: Performing in this order - // P<>(Y | X) = (P<>(Y)P<>(X | Y))/P<>(X) // as default multiplication of distributions are not commutative (could // also use pointwiseProductPOS() to specify the order). assertArrayEquals(model.posteriorDistribution(ExampleRV.CAVITY_RV, ExampleRV.TOOTHACHE_RV).getValues(), model.priorDistribution(ExampleRV.CAVITY_RV) .multiplyBy( model.posteriorDistribution( ExampleRV.TOOTHACHE_RV, ExampleRV.CAVITY_RV)) .divideBy( model.priorDistribution(ExampleRV.TOOTHACHE_RV)) .getValues(), DELTA_THRESHOLD); assertArrayEquals( model.posteriorDistribution(ExampleRV.CAVITY_RV, ExampleRV.CATCH_RV).getValues(), model.priorDistribution(ExampleRV.CAVITY_RV) .multiplyBy( model.posteriorDistribution(ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV)) .divideBy(model.priorDistribution(ExampleRV.CATCH_RV)) .getValues(), DELTA_THRESHOLD); // General Bayes' Rule conditionalized on background evidence e (13.3) // P<>(Y | X, e) = P<>(X | Y, e)P<>(Y|e)/P<>(X | e) // Note: Performing in this order - // P<>(Y | X, e) = (P<>(Y|e)P<>(X | Y, e)))/P<>(X | e) // as default multiplication of distributions are not commutative (could // also use pointwiseProductPOS() to specify the order). assertArrayEquals( model.posteriorDistribution(ExampleRV.CAVITY_RV, ExampleRV.TOOTHACHE_RV, acatch).getValues(), model.posteriorDistribution(ExampleRV.CAVITY_RV, acatch) .multiplyBy( model.posteriorDistribution( ExampleRV.TOOTHACHE_RV, ExampleRV.CAVITY_RV, acatch)) .divideBy( model.posteriorDistribution( ExampleRV.TOOTHACHE_RV, acatch)) .getValues(), DELTA_THRESHOLD); // assertArrayEquals( model.posteriorDistribution(ExampleRV.CAVITY_RV, ExampleRV.TOOTHACHE_RV, anotcatch).getValues(), model.posteriorDistribution(ExampleRV.CAVITY_RV, anotcatch) .multiplyBy( model.posteriorDistribution( ExampleRV.TOOTHACHE_RV, ExampleRV.CAVITY_RV, anotcatch)) .divideBy( model.posteriorDistribution( ExampleRV.TOOTHACHE_RV, anotcatch)) .getValues(), DELTA_THRESHOLD); // assertArrayEquals( model.posteriorDistribution(ExampleRV.CAVITY_RV, ExampleRV.CATCH_RV, atoothache).getValues(), model.posteriorDistribution(ExampleRV.CAVITY_RV, atoothache) .multiplyBy( model.posteriorDistribution(ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV, atoothache)) .divideBy( model.posteriorDistribution(ExampleRV.CATCH_RV, atoothache)).getValues(), DELTA_THRESHOLD); assertArrayEquals( model.posteriorDistribution(ExampleRV.CAVITY_RV, ExampleRV.CATCH_RV, anottoothache).getValues(), model.posteriorDistribution(ExampleRV.CAVITY_RV, anottoothache) .multiplyBy( model.posteriorDistribution(ExampleRV.CATCH_RV, ExampleRV.CAVITY_RV, anottoothache)) .divideBy( model.posteriorDistribution(ExampleRV.CATCH_RV, anottoothache)).getValues(), DELTA_THRESHOLD); }