def test_is_true_for_root_node(self): root_node = BayesNetNode("Cloudy") root_node.set_probablity(0.9, [True]) self.assertTrue(root_node.is_true_for(0.4, {})) self.assertFalse(root_node.is_true_for(0.99, {}))
def test_parents(self): root1 = BayesNetNode("root1") root2 = BayesNetNode("root2") root3 = BayesNetNode("root3") child = BayesNetNode("child") child.influenced_by(root1) child.influenced_by(root2) child.influenced_by(root3) self.assertSameElements([root1, root2, root3], child.parents)
def test_probability_of_root_variable(self): bnn = BayesNetNode("var1") bnn.set_probablity(0.3, [True]) false_result = bnn.probability_of({"var1": False}) expected_false_result = 0.7 self.assertAlmostEqual(expected_false_result, false_result, places=5) true_result = bnn.probability_of({"var1": True}) expected_true_result = 0.3 self.assertAlmostEqual(expected_true_result, true_result, places=5)
def test_childs(self): root_node = BayesNetNode("root") child1 = BayesNetNode("child1") child2 = BayesNetNode("child2") child3 = BayesNetNode("child3") child1.influenced_by(root_node) child2.influenced_by(root_node) child3.influenced_by(root_node) self.assertSameElements([child1, child2, child3], root_node.children)
def test_get_variables_with_several_childs(self): root_node = BayesNetNode("root") child1 = BayesNetNode("child1") child2 = BayesNetNode("child2") child3 = BayesNetNode("child3") child1.influenced_by(root_node) child2.influenced_by(root_node) child3.influenced_by(root_node) bn = BayesNet([root_node]) vars = bn.get_variables() self.assertSameElements(["root", "child1", "child2", "child3"], vars)
def create_burglary_network(): burglary = BayesNetNode("Burglary") earthQuake = BayesNetNode("EarthQuake") alarm = BayesNetNode("Alarm") johnCalls = BayesNetNode("JohnCalls") maryCalls = BayesNetNode("MaryCalls") alarm.influenced_by(burglary, earthQuake) johnCalls.influenced_by(alarm) maryCalls.influenced_by(alarm) burglary.set_probablity(0.001, [True]) earthQuake.set_probablity(0.002, [True]) alarm.set_probablity(0.95, [True, True]) alarm.set_probablity(0.94, [True, False]) alarm.set_probablity(0.29, [False, True]) alarm.set_probablity(0.001, [False, False]) johnCalls.set_probablity(0.9, [True]) johnCalls.set_probablity(0.05, [False]) maryCalls.set_probablity(0.7, [True]) maryCalls.set_probablity(0.01, [False]) return BayesNet((burglary, earthQuake))
def _create_wet_grass_network(self): cloudy_node = BayesNetNode("Cloudy") sprinkler_node = BayesNetNode("Sprinkler") rain_node = BayesNetNode("Rain") wet_grass_node = BayesNetNode("WetGrass") sprinkler_node.influenced_by(cloudy_node) rain_node.influenced_by(cloudy_node) wet_grass_node.influenced_by(rain_node, sprinkler_node) cloudy_node.set_probablity(0.5, [True]) sprinkler_node.set_probablity(0.1, [True]) sprinkler_node.set_probablity(0.5, [False]) rain_node.set_probablity(0.8, [True]) rain_node.set_probablity(0.2, [False]) wet_grass_node.set_probablity(0.99, [True, True]) wet_grass_node.set_probablity(0.9, [True, False]) wet_grass_node.set_probablity(0.9, [False, True]) wet_grass_node.set_probablity(0, [False, False]) return BayesNet([cloudy_node])
def test_is_true_for_not_root_node(self): sprinkler_node = BayesNetNode("Sprinkler") rain_node = BayesNetNode("Rain") wet_grass_node = BayesNetNode("WetGrass") wet_grass_node.influenced_by(sprinkler_node, rain_node) wet_grass_node.set_probablity(0.99, [True, True]) wet_grass_node.set_probablity(0.90, [True, False]) wet_grass_node.set_probablity(0.90, [False, True]) wet_grass_node.set_probablity(0, [False, False]) self.assertTrue(wet_grass_node.is_true_for(0.5, {"Sprinkler" : True, "Rain" : True})) self.assertFalse(wet_grass_node.is_true_for(0.99876, {"Sprinkler" : True, "Rain" : True}))
def test_get_root_variable(self): root_node = BayesNetNode("root") bn = BayesNet([root_node]) vars = bn.get_variables() self.assertSameElements(["root"], vars)
def test_is_true_for_not_root_node(self): sprinkler_node = BayesNetNode("Sprinkler") rain_node = BayesNetNode("Rain") wet_grass_node = BayesNetNode("WetGrass") wet_grass_node.influenced_by(sprinkler_node, rain_node) wet_grass_node.set_probablity(0.99, [True, True]) wet_grass_node.set_probablity(0.90, [True, False]) wet_grass_node.set_probablity(0.90, [False, True]) wet_grass_node.set_probablity(0, [False, False]) self.assertTrue( wet_grass_node.is_true_for(0.5, { "Sprinkler": True, "Rain": True })) self.assertFalse( wet_grass_node.is_true_for(0.99876, { "Sprinkler": True, "Rain": True }))
def test_get_variables_with_several_roots(self): root_node1 = BayesNetNode("root1") root_node2 = BayesNetNode("root2") child1 = BayesNetNode("child1") child2 = BayesNetNode("child2") child3 = BayesNetNode("child3") child1.influenced_by(root_node1) child2.influenced_by(root_node1, root_node2) child3.influenced_by(root_node2) bn = BayesNet([root_node1, root_node2]) vars = bn.get_variables() self.assertSameElements( ["root1", "root2", "child1", "child2", "child3"], vars)
def test_probability_of_non_root_var(self): root_bnn1 = BayesNetNode("root1") root_bnn2 = BayesNetNode("root2") root_bnn3 = BayesNetNode("root3") dependent_bnn = BayesNetNode("dependentNode") dependent_bnn.influenced_by(root_bnn1, root_bnn2, root_bnn3) dependent_bnn.influenced_by(root_bnn1, root_bnn2, root_bnn3) dependent_bnn.set_probablity(0.1, (True, True, True)) dependent_bnn.set_probablity(0.15, (True, False, True)) dependent_bnn.set_probablity(0.2, (True, True, False)) dependent_bnn.set_probablity(0.25, (False, True, True)) dependent_bnn.set_probablity(0.3, (False, False, True)) result = dependent_bnn.probability_of({"root1" : True}) expected_result = 0.1 + 0.15 + 0.2 self.assertAlmostEqual(expected_result, result, places=5)
def test_probability_of_non_root_var(self): root_bnn1 = BayesNetNode("root1") root_bnn2 = BayesNetNode("root2") root_bnn3 = BayesNetNode("root3") dependent_bnn = BayesNetNode("dependentNode") dependent_bnn.influenced_by(root_bnn1, root_bnn2, root_bnn3) dependent_bnn.influenced_by(root_bnn1, root_bnn2, root_bnn3) dependent_bnn.set_probablity(0.1, (True, True, True)) dependent_bnn.set_probablity(0.15, (True, False, True)) dependent_bnn.set_probablity(0.2, (True, True, False)) dependent_bnn.set_probablity(0.25, (False, True, True)) dependent_bnn.set_probablity(0.3, (False, False, True)) result = dependent_bnn.probability_of({"root1": True}) expected_result = 0.1 + 0.15 + 0.2 self.assertAlmostEqual(expected_result, result, places=5)