// 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 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());
}