/** * Reset this instances persistent variables to be used between called to * particleFiltering(). * * @param N * the number of samples to be maintained * @param dbn * a DBN with prior <b>P</b>(<b>X</b><sub>0</sub>), transition * model <b>P</b>(<b>X</b><sub>1</sub> | <b>X</b><sub>0</sub>), * sensor model <b>P</b>(<b>E</b><sub>1</sub> | * <b>X</b><sub>1</sub>) */ public void initPersistent(int N, IDynamicBayesianNetwork dbn) { this.N = N; this.dbn = dbn; // persistent: S, a vector of samples of size N, initially generated // from <b>P</b>(<b>X</b><sub>0</sub>) S = new AssignmentProposition[N][]; S_tp1 = new AssignmentProposition[N][]; int[] indexes = new int[N]; for (int i = 0; i < N; ++i) { S[i] = new AssignmentProposition[this.dbn.GetX_0().Size()]; S_tp1[i] = new AssignmentProposition[this.dbn.GetX_0().Size()]; indexes[i] = i; IMap <IRandomVariable, object> sample = priorSampler.priorSample(this.dbn.GetPriorNetwork()); int idx = 0; foreach (var sa in sample) { S[i][idx] = new AssignmentProposition(this.dbn.GetX_0_to_X_1().Get(sa.GetKey()), sa.GetValue()); S_tp1[i][idx] = new AssignmentProposition(this.dbn.GetX_0_to_X_1().Get(sa.GetKey()), sa.GetValue()); idx++; } } sensorModel = new FiniteBayesModel(dbn, new EliminationAsk()); sampleIndexes = new RandVar("SAMPLE_INDEXES", new FiniteIntegerDomain(indexes)); }
// function REJECTION-SAMPLING(X, e, bn, N) returns an estimate of // <b>P</b>(X|e) /** * The REJECTION-SAMPLING algorithm in Figure 14.14. For answering queries * given evidence in a Bayesian Network. * * @param X * the query variables * @param e * observed values for variables E * @param bn * a Bayesian network * @param Nsamples * the total number of samples to be generated * @return an estimate of <b>P</b>(X|e) */ public ICategoricalDistribution rejectionSampling(IRandomVariable[] X, AssignmentProposition[] e, IBayesianNetwork bn, int Nsamples) { // local variables: <b>N</b>, a vector of counts for each value of X, // initially zero double[] N = new double[ProbUtil.expectedSizeOfCategoricalDistribution(X)]; // for j = 1 to N do for (int j = 0; j < Nsamples; j++) { // <b>x</b> <- PRIOR-SAMPLE(bn) IMap <IRandomVariable, object> x = ps.priorSample(bn); // if <b>x</b> is consistent with e then if (isConsistent(x, e)) { // <b>N</b>[x] <- <b>N</b>[x] + 1 // where x is the value of X in <b>x</b> N[ProbUtil.indexOf(X, x)] += 1.0; } } // return NORMALIZE(<b>N</b>) return(new ProbabilityTable(N, X).normalize()); }