public void testGibbsAsk_compare() { // create two nodes: parent and child with an arc from parent to child IRandomVariable rvParent = new RandVar("Parent", new BooleanDomain()); IRandomVariable rvChild = new RandVar("Child", new BooleanDomain()); FullCPTNode nodeParent = new FullCPTNode(rvParent, new double[] { 0.7, 0.3 }); new FullCPTNode(rvChild, new double[] { 0.8, 0.2, 0.2, 0.8 }, nodeParent); // create net BayesNet net = new BayesNet(nodeParent); // query parent probability IRandomVariable[] rvX = new IRandomVariable[] { rvParent }; // ...given child evidence (true) AssignmentProposition[] propE = new AssignmentProposition[] { new AssignmentProposition(rvChild, true) }; // sample with LikelihoodWeighting ICategoricalDistribution samplesLW = new LikelihoodWeighting().Ask(rvX, propE, net, 1000); Assert.AreEqual(0.9, samplesLW.getValue(true), DELTA_THRESHOLD); // sample with RejectionSampling ICategoricalDistribution samplesRS = new RejectionSampling().Ask(rvX, propE, net, 1000); Assert.AreEqual(0.9, samplesRS.getValue(true), DELTA_THRESHOLD); // sample with GibbsAsk ICategoricalDistribution samplesGibbs = new GibbsAsk().Ask(rvX, propE, net, 1000); Assert.AreEqual(0.9, samplesGibbs.getValue(true), DELTA_THRESHOLD); }
public void testGibbsAsk_basic() { IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork(); AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) }; GibbsAsk ga = new GibbsAsk(); double[] estimate = ga.gibbsAsk(new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 1000).getValues(); assertArrayEquals(new double[] { 0.3, 0.7 }, estimate, DELTA_THRESHOLD); }