public Pair<Map<RandomVariable, Object>, Double> weightedSample(
BayesianNetwork bn, AssignmentProposition[] e)
{
// w <- 1;
double w = 1.0;
// <b>x</b> <- an event with n elements initialized from e
Map<RandomVariable, Object> x = new LinkedHashMap<RandomVariable, Object>();
foreach (AssignmentProposition ap in
e)
{
x.Add(ap.getTermVariable(), ap.getValue());
}
// foreach variable X<sub>i</sub> in X<sub>1</sub>,...,X<sub>n</sub> do
foreach (RandomVariable Xi in
bn.getVariablesInTopologicalOrder())
{
// if X<sub>i</sub> is an evidence variable with value x<sub>i</sub>
// in e
if (x.ContainsKey(Xi))
{
// then w <- w * P(X<sub>i</sub> = x<sub>i</sub> |
// parents(X<sub>i</sub>))
w *= bn.getNode(Xi)
.getCPD()
.getValue(
ProbUtil.getEventValuesForXiGivenParents(
bn.getNode(Xi), x));
}
else
{
// else <b>x</b>[i] <- a random sample from
// <b>P</b>(X<sub>i</sub> | parents(X<sub>i</sub>))
x.Add(Xi, ProbUtil.randomSample(bn.getNode(Xi), x, randomizer));
}
}
// return <b>x</b>, w
return new Pair<Map<RandomVariable, Object>, Double>(x, w);
}