private void sampleFromTransitionModel(int i) { // x <- an event initialized with S[i] IMap <IRandomVariable, object> x = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>(); for (int n = 0; n < S[i].Length; n++) { AssignmentProposition x1 = S[i][n]; x.Put(this.dbn.GetX_1_to_X_0().Get(x1.getTermVariable()), x1.getValue()); } // foreach variable X<sub>1<sub>i</sub></sub> in // X<sub>1<sub>1</sub></sub>,...,X<sub>1<sub>n<</sub>/sub> do foreach (IRandomVariable X1_i in dbn.GetX_1_VariablesInTopologicalOrder()) { // x1[i] <- a random sample from // <b>P</b>(X<sub>1<sub>i</sub></sub> | // parents(X<sub>1<sub>i</sub></sub>)) x.Put(X1_i, ProbUtil.randomSample(dbn.GetNode(X1_i), x, randomizer)); } // S[i] <- sample from <b>P</b>(<b>X</b><sub>1</sub> | // <b>X</b><sub>0</sub> = S[i]) for (int n = 0; n < S_tp1[i].Length; n++) { AssignmentProposition x1 = S_tp1[i][n]; x1.setValue(x.Get(x1.getTermVariable())); } }
/** * The population is re-sampled to generate a new population of N samples. * Each new sample is selected from the current population; the probability * that a particular sample is selected is proportional to its weight. The * new samples are un-weighted. * * @param N * the number of samples * @param S * 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 sample for time t * @param W * a vector of weights of size N * * @return a new vector of samples of size N sampled from S based on W */ private AssignmentProposition[][] weightedSampleWithReplacement(int N, AssignmentProposition[][] S, double[] W) { AssignmentProposition[][] newS = new AssignmentProposition[N][]; double[] normalizedW = Util.normalize(W); for (int i = 0; i < N; ++i) { newS[i] = new AssignmentProposition[this.dbn.GetX_0().Size()]; int sample = (int)ProbUtil.sample(randomizer.NextDouble(), sampleIndexes, normalizedW); for (int idx = 0; idx < S_tp1[i].Length; idx++) { AssignmentProposition ap = S_tp1[sample][idx]; newS[i][idx] = new AssignmentProposition(ap.getTermVariable(), ap.getValue()); } } return(newS); }
public void test_AIMA3e_Fig15_18() { IRandom mr = new MockRandomizer(new double[] { // Prior Sample: // 8 with Rain_t-1=true from prior distribution 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, // 2 with Rain_t-1=false from prior distribution 0.6, 0.6, // (a) Propagate 6 samples Rain_t=true 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, // 4 samples Rain_t=false 0.71, 0.71, 0.31, 0.31, // (b) Weight should be for first 6 samples: // Rain_t-1=true, Rain_t=true, Umbrella_t=false = 0.1 // Next 2 samples: // Rain_t-1=true, Rain_t=false, Umbrealla_t=false= 0.8 // Final 2 samples: // Rain_t-1=false, Rain_t=false, Umbrella_t=false = 0.8 // gives W[] = // [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.8, 0.8, 0.8, 0.8] // normalized = // [0.026, ...., 0.211, ....] is approx. 0.156 = true // the remainder is false // (c) Resample 2 Rain_t=true, 8 Rain_t=false 0.15, 0.15, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, // // Next Sample: // (a) Propagate 1 samples Rain_t=true 0.7, // 9 samples Rain_t=false 0.71, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, // (c) resample 1 Rain_t=true, 9 Rain_t=false 0.0001, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2 }); int N = 10; ParticleFiltering pf = new ParticleFiltering(N, DynamicBayesNetExampleFactory.getUmbrellaWorldNetwork(), mr); AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, false) }; AssignmentProposition[][] S = pf.particleFiltering(e); Assert.AreEqual(N, S.Length); for (int i = 0; i < N; ++i) { Assert.AreEqual(1, S[i].Length); AssignmentProposition ap = S[i][0]; Assert.AreEqual(ExampleRV.RAIN_t_RV, ap.getTermVariable()); if (i < 2) { Assert.AreEqual(true, ap.getValue()); } else { Assert.AreEqual(false, ap.getValue()); } } // Generate next sample to ensure everything roles forward ok // in this case with prefixed probabilities only expect 1 Rain_t=true S = pf.particleFiltering(e); Assert.AreEqual(N, S.Length); for (int i = 0; i < N; ++i) { Assert.AreEqual(1, S[i].Length); AssignmentProposition ap = S[i][0]; Assert.AreEqual(ExampleRV.RAIN_t_RV, ap.getTermVariable()); if (i < 1) { Assert.AreEqual(true, ap.getValue()); } else { Assert.AreEqual(false, ap.getValue()); } } }