// // 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 double[] mcmcAsk(String X, Dictionary <String, bool> evidence, int numberOfVariables, Randomizer r) { double[] retval = new double[2]; List <String> _nonEvidenceVariables = nonEvidenceVariables(evidence, X); Dictionary <String, bool> evt = createRandomEvent( _nonEvidenceVariables, evidence, r); for (int j = 0; j < numberOfVariables; j++) { foreach (string variable in _nonEvidenceVariables) { BayesNetNode node = getNodeOf(variable); List <BayesNetNode> blanket = markovBlanket(node); Dictionary <string, bool> mb = createMBValues(blanket, evt); // event.put(node.getVariable(), node.isTrueFor( // r.getProbability(), mb)); evt[node.getVariable()] = truthValue(rejectionSample(node .getVariable(), mb, 100, r), r); bool queryValue = evt[X]; if (queryValue) { retval[0] += 1; } else { retval[1] += 1; } } } return(Util.normalize(retval)); }
// // PRIVATE METHODS // private BayesNet createBurglaryNetwork() { BayesNetNode burglary = new BayesNetNode("Burglary"); BayesNetNode earthquake = new BayesNetNode("EarthQuake"); BayesNetNode alarm = new BayesNetNode("Alarm"); BayesNetNode johnCalls = new BayesNetNode("JohnCalls"); BayesNetNode maryCalls = new BayesNetNode("MaryCalls"); alarm.influencedBy(burglary, earthquake); johnCalls.influencedBy(alarm); maryCalls.influencedBy(alarm); burglary.setProbability(true, 0.001);// TODO behaviour changes if // root node earthquake.setProbability(true, 0.002); alarm.setProbability(true, true, 0.95); alarm.setProbability(true, false, 0.94); alarm.setProbability(false, true, 0.29); alarm.setProbability(false, false, 0.001); johnCalls.setProbability(true, 0.90); johnCalls.setProbability(false, 0.05); maryCalls.setProbability(true, 0.70); maryCalls.setProbability(false, 0.01); BayesNet net = new BayesNet(burglary, earthquake); return net; }
// // PRIVATE METHODS // private BayesNet createBurglaryNetwork() { BayesNetNode burglary = new BayesNetNode("Burglary"); BayesNetNode earthquake = new BayesNetNode("EarthQuake"); BayesNetNode alarm = new BayesNetNode("Alarm"); BayesNetNode johnCalls = new BayesNetNode("JohnCalls"); BayesNetNode maryCalls = new BayesNetNode("MaryCalls"); alarm.influencedBy(burglary, earthquake); johnCalls.influencedBy(alarm); maryCalls.influencedBy(alarm); burglary.setProbability(true, 0.001); earthquake.setProbability(true, 0.002); alarm.setProbability(true, true, 0.95); alarm.setProbability(true, false, 0.94); alarm.setProbability(false, true, 0.29); alarm.setProbability(false, false, 0.001); johnCalls.setProbability(true, 0.90); johnCalls.setProbability(false, 0.05); maryCalls.setProbability(true, 0.70); maryCalls.setProbability(false, 0.01); BayesNet net = new BayesNet(burglary, earthquake); return(net); }
public void setUp() { a = new BayesNetNode("A"); b = new BayesNetNode("B"); c = new BayesNetNode("C"); d = new BayesNetNode("D"); e = new BayesNetNode("E"); c.influencedBy(a, b); d.influencedBy(c); e.influencedBy(c); }
public double probabilityOf(String Y, bool value, Dictionary <String, bool> evidence) { BayesNetNode y = getNodeOf(Y); if (y == null) { throw new ApplicationException("Unable to find a node with variable " + Y); } else { List <BayesNetNode> parentNodes = y.getParents(); if (parentNodes.Count == 0) {// root nodes Dictionary <String, bool> YTable = new Dictionary <String, bool>(); YTable.Add(Y, value); double prob = y.probabilityOf(YTable); return(prob); } else {// non rootnodes Dictionary <String, bool> parentValues = new Dictionary <String, bool>(); foreach (BayesNetNode parent in parentNodes) { parentValues.Add(parent.getVariable(), evidence[parent .getVariable()]); } double prob = y.probabilityOf(parentValues); if (value.Equals(true)) { return(prob); } else { return(1.0 - prob); } } } }
private List <BayesNetNode> markovBlanket(BayesNetNode node, List <BayesNetNode> soFar) { // parents List <BayesNetNode> parents = node.getParents(); foreach (BayesNetNode parent in parents) { if (!soFar.Contains(parent)) { soFar.Add(parent); } } // children List <BayesNetNode> children = node.getChildren(); foreach (BayesNetNode child in children) { if (!soFar.Contains(child)) { soFar.Add(child); List <BayesNetNode> childsParents = child.getParents(); foreach (BayesNetNode childsParent in childsParents) { ; if ((!soFar.Contains(childsParent)) && (!(childsParent.Equals(node)))) { soFar.Add(childsParent); } } // childsParents } // end contains child } // end child return(soFar); }
public BayesNet(BayesNetNode root1, BayesNetNode root2, BayesNetNode root3) : this(root1, root2) { roots.Add(root3); }
// // 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 BayesNet(BayesNetNode root1, BayesNetNode root2) : this(root1) { roots.Add(root2); }
private List <BayesNetNode> markovBlanket(BayesNetNode node) { return(markovBlanket(node, new List <BayesNetNode>())); }
public BayesNet(BayesNetNode root) { roots.Add(root); }
private List<BayesNetNode> markovBlanket(BayesNetNode node) { return markovBlanket(node, new List<BayesNetNode>()); }
private List<BayesNetNode> markovBlanket(BayesNetNode node, List<BayesNetNode> soFar) { // parents List<BayesNetNode> parents = node.getParents(); foreach (BayesNetNode parent in parents) { if (!soFar.Contains(parent)) { soFar.Add(parent); } } // children List<BayesNetNode> children = node.getChildren(); foreach (BayesNetNode child in children) { if (!soFar.Contains(child)) { soFar.Add(child); List<BayesNetNode> childsParents = child.getParents(); foreach (BayesNetNode childsParent in childsParents) { ; if ((!soFar.Contains(childsParent)) && (!(childsParent.Equals(node)))) { soFar.Add(childsParent); } }// childsParents }// end contains child }// end child return soFar; }