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 testPriorSample_basic() { IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork(); AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) }; MockRandomizer r = new MockRandomizer(new double[] { 0.1 }); RejectionSampling rs = new RejectionSampling(new PriorSample(r)); double[] estimate = rs.rejectionSampling(new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 100).getValues(); assertArrayEquals(new double[] { 1.0, 0.0 }, estimate, DELTA_THRESHOLD); }
public void testRejectionSampling_AIMA3e_pg532() { // AIMA3e pg. 532 IBayesianNetwork bn = BayesNetExampleFactory.constructCloudySprinklerRainWetGrassNetwork(); AssignmentProposition[] e = new AssignmentProposition[] { new AssignmentProposition(ExampleRV.SPRINKLER_RV, true) }; // 400 required as 4 variables and 100 samples planned double[] ma = new double[400]; for (int i = 0; i < ma.Length; i += 4) { // Of the 100 that we generate, suppose // that 73 have Sprinkler = false and are rejected, if (i < (73 * 4)) { ma[i] = 0.5; // i.e Cloudy=true ma[i + 1] = 0.2; // i.e. Sprinkler=false ma[i + 2] = 0.5; // i.e. Rain=true ma[i + 3] = 0.1; // i.e. WetGrass=true } else { ma[i] = 0.5; // i.e Cloudy=true ma[i + 1] = 0.09; // i.e. Sprinkler=true // while 27 have Sprinkler = true; of the 27, // 8 have Rain = true if (i < ((73 + 8) * 4)) { ma[i + 2] = 0.5; // i.e. Rain=true } else { // and 19 have Rain = false. ma[i + 2] = 0.9; // i.e. Rain=false } ma[i + 3] = 0.1; // i.e. WetGrass=true } } MockRandomizer r = new MockRandomizer(ma); RejectionSampling rs = new RejectionSampling(new PriorSample(r)); double[] estimate = rs.rejectionSampling( new IRandomVariable[] { ExampleRV.RAIN_RV }, e, bn, 100) .getValues(); assertArrayEquals(new double[] { 0.2962962962962963, 0.7037037037037037 }, estimate, DELTA_THRESHOLD); }