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)); }
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; } }
/** * The particle filtering algorithm implemented as a recursive update * operation with state (the set of samples). * * @param e * <b>e</b>, the new incoming evidence * @return a vector of samples of size N, where each sample is a vector of * assignment propositions for the X_1 state variables, which is * intended to represent the generated sample for time t. */ public AssignmentProposition[][] particleFiltering(AssignmentProposition[] e) { // local variables: W, a vector of weights of size N double[] W = new double[N]; // for i = 1 to N do for (int i = 0; i < N; ++i) { /* step 1 */ // S[i] <- sample from <b>P</b>(<b>X</b><sub>1</sub> | // <b>X</b><sub>0</sub> = S[i]) sampleFromTransitionModel(i); /* step 2 */ // W[i] <- <b>P</b>(<b>e</b> | <b>X</b><sub>1</sub> = S[i]) W[i] = sensorModel.posterior(ProbUtil.constructConjunction(e), S_tp1[i]); } /* step 3 */ // S <- WEIGHTED-SAMPLE-WITH-REPLACEMENT(N, S, W) S = weightedSampleWithReplacement(N, S, W); // return S return(S); }