// // PRIVATE METHODS // private BayesNet createWetGrassNetwork() { BayesNetNode cloudy = new BayesNetNode("Cloudy"); BayesNetNode sprinkler = new BayesNetNode("Sprinkler"); BayesNetNode rain = new BayesNetNode("Rain"); BayesNetNode wetGrass = new BayesNetNode("WetGrass"); sprinkler.influencedBy(cloudy); rain.influencedBy(cloudy); wetGrass.influencedBy(rain, sprinkler); cloudy.setProbability(true, 0.5); sprinkler.setProbability(true, 0.10); sprinkler.setProbability(false, 0.50); rain.setProbability(true, 0.8); rain.setProbability(false, 0.2); wetGrass.setProbability(true, true, 0.99); wetGrass.setProbability(true, false, 0.90); wetGrass.setProbability(false, true, 0.90); wetGrass.setProbability(false, false, 0.00); BayesNet net = new BayesNet(cloudy); return(net); }
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 testRejectionSample() { BayesNet net = createWetGrassNetwork(); MockRandomizer r = new MockRandomizer(new double[] { 0.1 }); Dictionary <String, bool> evidence = new Dictionary <String, bool>(); evidence.Add("Sprinkler", true); double[] results = net.rejectionSample("Rain", evidence, 100, r); Assert.AreEqual(1.0, results[0], 0.001); Assert.AreEqual(0.0, results[1], 0.001); }
public void testLikelihoodWeighting() { MockRandomizer r = new MockRandomizer( new double[] { 0.5, 0.5, 0.5, 0.5 }); BayesNet net = createWetGrassNetwork(); Dictionary <String, bool> evidence = new Dictionary <String, bool>(); evidence.Add("Sprinkler", true); double[] results = net.likelihoodWeighting("Rain", evidence, 1000, r); Assert.AreEqual(1.0, results[0], 0.001); Assert.AreEqual(0.0, results[1], 0.001); }
public void testPriorSample() { BayesNet net = createWetGrassNetwork(); MockRandomizer r = new MockRandomizer( new double[] { 0.5, 0.5, 0.5, 0.5 }); Dictionary <String, bool> table = net.getPriorSample(r); Assert.AreEqual(4, table.Count); Assert.AreEqual(true, table["Cloudy"]); Assert.AreEqual(false, table["Sprinkler"]); Assert.AreEqual(true, table["Rain"]); Assert.AreEqual(true, table["WetGrass"]); }
private static double enumerateAll(BayesNet net, List<string> unprocessedVariables, Dictionary<String, bool> evidenceVariables) { if (unprocessedVariables.Count == 0) { return 1.0; } else { String Y = (String)unprocessedVariables[0]; if (evidenceVariables.ContainsKey(Y)) { double probYGivenParents = net.probabilityOf(Y, evidenceVariables[Y], evidenceVariables); double secondTerm = enumerateAll(net, Util .rest(unprocessedVariables), evidenceVariables); return probYGivenParents * secondTerm; } else { double sigma = 0.0; Dictionary<String, bool> clone1 = cloneEvidenceVariables(evidenceVariables); clone1.Add(Y, true); double probYTrueGivenParents = net.probabilityOf(Y, true, clone1); double secondTerm = enumerateAll(net, Util .rest(unprocessedVariables), clone1); double trueProbabilityY = probYTrueGivenParents * secondTerm; Dictionary<String, bool> clone2 = cloneEvidenceVariables(evidenceVariables); clone2.Add(Y, false); double probYFalseGivenParents = net.probabilityOf(Y, false, clone2); secondTerm = enumerateAll(net, Util.rest(unprocessedVariables), clone2); double falseProbabilityY = probYFalseGivenParents * secondTerm; // System.Console.Write(secondTerm + " ) )"); sigma = trueProbabilityY + falseProbabilityY; return sigma; } } }
public void testMCMCask2() { BayesNet net = createWetGrassNetwork(); MockRandomizer r = new MockRandomizer( new double[] { 0.5, 0.5, 0.5, 0.5 }); Dictionary <String, bool> evidence = new Dictionary <String, bool>(); evidence.Add("Sprinkler", true); double[] results = net.mcmcAsk("Rain", evidence, 1, r); Assert.AreEqual(0.333, results[0], 0.001); Assert.AreEqual(0.666, results[1], 0.001); }
public static double[] ask(Query q, BayesNet net) { Dictionary<String, bool> evidenceVariables = q.getEvidenceVariables(); double[] probDist = new double[2]; // true probability evidenceVariables[q.getQueryVariable()] = true; probDist[0] = enumerateAll(net, net.getVariables(), evidenceVariables); // false probability evidenceVariables[q.getQueryVariable()] = false; probDist[1] = enumerateAll(net, net.getVariables(), evidenceVariables); // System.Console.WriteLine( probDist[0] + " " + probDist[1]); // return probDist; double[] normalized = Util.normalize(probDist); // System.Console.WriteLine( normalized[0] + " " + normalized[1]); return normalized; }
/** * Return a Dynamic Bayesian Network of the Umbrella World Network. * * @return a Dynamic Bayesian Network of the Umbrella World Network. */ public static DynamicBayesianNetwork getUmbrellaWorldNetwork() { FiniteNode prior_rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 }); BayesNet priorNetwork = new BayesNet(prior_rain_tm1); // Prior belief state FiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 }); // Transition Model FiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[] { // R_t-1 = true, R_t = true 0.7, // R_t-1 = true, R_t = false 0.3, // R_t-1 = false, R_t = true 0.3, // R_t-1 = false, R_t = false 0.7 }, rain_tm1); // Sensor Model FiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV, new double[] { // R_t = true, U_t = true 0.9, // R_t = true, U_t = false 0.1, // R_t = false, U_t = true 0.2, // R_t = false, U_t = false 0.8 }, rain_t); Map <RandomVariable, RandomVariable> X_0_to_X_1 = new HashMap <RandomVariable, RandomVariable>(); X_0_to_X_1.put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV); Set <RandomVariable> E_1 = new HashSet <RandomVariable>(); E_1.add(ExampleRV.UMBREALLA_t_RV); return(new DynamicBayesNet(priorNetwork, X_0_to_X_1, E_1, rain_tm1)); }
/** * Return a Dynamic Bayesian Network of the Umbrella World Network. * * @return a Dynamic Bayesian Network of the Umbrella World Network. */ public static DynamicBayesianNetwork getUmbrellaWorldNetwork() { FiniteNode prior_rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] {0.5, 0.5}); BayesNet priorNetwork = new BayesNet(prior_rain_tm1); // Prior belief state FiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] {0.5, 0.5}); // Transition Model FiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[] { // R_t-1 = true, R_t = true 0.7, // R_t-1 = true, R_t = false 0.3, // R_t-1 = false, R_t = true 0.3, // R_t-1 = false, R_t = false 0.7 }, rain_tm1); // Sensor Model FiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV, new double[] { // R_t = true, U_t = true 0.9, // R_t = true, U_t = false 0.1, // R_t = false, U_t = true 0.2, // R_t = false, U_t = false 0.8 }, rain_t); Map<RandomVariable, RandomVariable> X_0_to_X_1 = new HashMap<RandomVariable, RandomVariable>(); X_0_to_X_1.put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV); Set<RandomVariable> E_1 = new HashSet<RandomVariable>(); E_1.add(ExampleRV.UMBREALLA_t_RV); return new DynamicBayesNet(priorNetwork, X_0_to_X_1, E_1, rain_tm1); }
/** * Return a Dynamic Bayesian Network of the Umbrella World Network. * * @return a Dynamic Bayesian Network of the Umbrella World Network. */ public static IDynamicBayesianNetwork getUmbrellaWorldNetwork() { IFiniteNode prior_rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 }); BayesNet priorNetwork = new BayesNet(prior_rain_tm1); // Prior belief state IFiniteNode rain_tm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 }); // Transition Model IFiniteNode rain_t = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[] { // R_t-1 = true, R_t = true 0.7, // R_t-1 = true, R_t = false 0.3, // R_t-1 = false, R_t = true 0.3, // R_t-1 = false, R_t = false 0.7 }, rain_tm1); // Sensor Model IFiniteNode umbrealla_t = new FullCPTNode(ExampleRV.UMBREALLA_t_RV, new double[] { // R_t = true, U_t = true 0.9, // R_t = true, U_t = false 0.1, // R_t = false, U_t = true 0.2, // R_t = false, U_t = false 0.8 }, rain_t); IMap <IRandomVariable, IRandomVariable> X_0_to_X_1 = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, IRandomVariable>(); X_0_to_X_1.Put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV); ISet <IRandomVariable> E_1 = CollectionFactory.CreateSet <IRandomVariable>(); E_1.Add(ExampleRV.UMBREALLA_t_RV); return(new DynamicBayesNet(priorNetwork, X_0_to_X_1, E_1, rain_tm1)); }