public CategoricalDistribution rejectionSampling(RandomVariable[] X,
AssignmentProposition[] e, BayesianNetwork 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)
Map<RandomVariable, 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();
}