def setup_titanic(): # Build a model of the titanic disaster global titanic_network, passenger, gender, tclass # Passengers on the Titanic either survive or perish passenger = DiscreteDistribution({'survive': 0.6, 'perish': 0.4}) # Gender, given survival data gender = ConditionalProbabilityTable( [['survive', 'male', 0.0], ['survive', 'female', 1.0], ['perish', 'male', 1.0], ['perish', 'female', 0.0]], [passenger]) # Class of travel, given survival data tclass = ConditionalProbabilityTable( [['survive', 'first', 0.0], ['survive', 'second', 1.0], ['survive', 'third', 0.0], ['perish', 'first', 1.0], ['perish', 'second', 0.0], ['perish', 'third', 0.0]], [passenger]) # State objects hold both the distribution, and a high level name. s1 = State(passenger, name="passenger") s2 = State(gender, name="gender") s3 = State(tclass, name="class") # Create the Bayesian network object with a useful name titanic_network = BayesianNetwork("Titanic Disaster") # Add the three nodes to the network titanic_network.add_nodes(s1, s2, s3) # Add transitions which represent conditional dependencies, where the # second node is conditionally dependent on the first node (Monty is # dependent on both guest and prize) titanic_network.add_edge(s1, s2) titanic_network.add_edge(s1, s3) titanic_network.bake()
def get_bayesnet(self): door_lock = DiscreteDistribution({'d1': 0.7, 'd2': 0.3}) clock_alarm = DiscreteDistribution( { 'a1' : 0.8, 'a2' : 0.2} ) light = ConditionalProbabilityTable( [[ 'd1', 'a1', 'l1', 0.96 ], ['d1', 'a1', 'l2', 0.04 ], [ 'd1', 'a2', 'l1', 0.89 ], [ 'd1', 'a2', 'l2', 0.11 ], [ 'd2', 'a1', 'l1', 0.96 ], [ 'd2', 'a1', 'l2', 0.04 ], [ 'd2', 'a2', 'l1', 0.89 ], [ 'd2', 'a2', 'l2', 0.11 ]], [door_lock, clock_alarm]) coffee_maker = ConditionalProbabilityTable( [[ 'a1', 'c1', 0.92 ], [ 'a1', 'c2', 0.08 ], [ 'a2', 'c1', 0.03 ], [ 'a2', 'c2', 0.97 ]], [clock_alarm] ) s_door_lock = State(door_lock, name="door_lock") s_clock_alarm = State(clock_alarm, name="clock_alarm") s_light = State(light, name="light") s_coffee_maker = State(coffee_maker, name="coffee_maker") network = BayesianNetwork("User_pref") network.add_nodes(s_door_lock, s_clock_alarm, s_light, s_coffee_maker) network.add_edge(s_door_lock,s_light) network.add_edge(s_clock_alarm,s_coffee_maker) network.add_edge(s_clock_alarm,s_light) network.bake() return network
def setup_monty(): # Build a model of the Monty Hall Problem global monty_network, monty_index, prize_index, guest_index random.seed(0) # Friends emissions are completely random guest = DiscreteDistribution({'A': 1. / 3, 'B': 1. / 3, 'C': 1. / 3}) # The actual prize is independent of the other distributions prize = DiscreteDistribution({'A': 1. / 3, 'B': 1. / 3, 'C': 1. / 3}) # Monty is dependent on both the guest and the prize. monty = ConditionalProbabilityTable( [['A', 'A', 'A', 0.0], ['A', 'A', 'B', 0.5], ['A', 'A', 'C', 0.5], ['A', 'B', 'A', 0.0], ['A', 'B', 'B', 0.0], ['A', 'B', 'C', 1.0], ['A', 'C', 'A', 0.0], ['A', 'C', 'B', 1.0], ['A', 'C', 'C', 0.0], ['B', 'A', 'A', 0.0], ['B', 'A', 'B', 0.0], ['B', 'A', 'C', 1.0], ['B', 'B', 'A', 0.5], ['B', 'B', 'B', 0.0], ['B', 'B', 'C', 0.5], ['B', 'C', 'A', 1.0], ['B', 'C', 'B', 0.0], ['B', 'C', 'C', 0.0], ['C', 'A', 'A', 0.0], ['C', 'A', 'B', 1.0], ['C', 'A', 'C', 0.0], ['C', 'B', 'A', 1.0], ['C', 'B', 'B', 0.0], ['C', 'B', 'C', 0.0], ['C', 'C', 'A', 0.5], ['C', 'C', 'B', 0.5], ['C', 'C', 'C', 0.0]], [guest, prize]) # Make the states s1 = State(guest, name="guest") s2 = State(prize, name="prize") s3 = State(monty, name="monty") # Make the bayes net, add the states, and the conditional dependencies. monty_network = BayesianNetwork("test") monty_network.add_nodes(s1, s2, s3) monty_network.add_edge(s1, s3) monty_network.add_edge(s2, s3) monty_network.bake() monty_index = monty_network.states.index(s3) prize_index = monty_network.states.index(s2) guest_index = monty_network.states.index(s1)
def test_io_fit(): d1 = DiscreteDistribution({True: 0.6, False: 0.4}) d2 = ConditionalProbabilityTable([ [True, 'A', 0.2], [True, 'B', 0.8], [False, 'A', 0.3], [False, 'B', 0.7]], [d1]) d3 = ConditionalProbabilityTable([ ['A', 0, 0.3], ['A', 1, 0.7], ['B', 0, 0.8], ['B', 1, 0.2]], [d2]) n1 = Node(d1) n2 = Node(d2) n3 = Node(d3) model1 = BayesianNetwork() model1.add_nodes(n1, n2, n3) model1.add_edge(n1, n2) model1.add_edge(n2, n3) model1.bake() model1.fit(X, weights=weights) d1 = DiscreteDistribution({True: 0.2, False: 0.8}) d2 = ConditionalProbabilityTable([ [True, 'A', 0.7], [True, 'B', 0.2], [False, 'A', 0.4], [False, 'B', 0.6]], [d1]) d3 = ConditionalProbabilityTable([ ['A', 0, 0.9], ['A', 1, 0.1], ['B', 0, 0.0], ['B', 1, 1.0]], [d2]) n1 = Node(d1) n2 = Node(d2) n3 = Node(d3) model2 = BayesianNetwork() model2.add_nodes(n1, n2, n3) model2.add_edge(n1, n2) model2.add_edge(n2, n3) model2.bake() model2.fit(data_generator) logp1 = model1.log_probability(X) logp2 = model2.log_probability(X) assert_array_almost_equal(logp1, logp2)
def pomegranate_User_pref(): door_lock = DiscreteDistribution({'True': 0.7, 'False': 0.3}) thermostate = ConditionalProbabilityTable( [['True', 'True', 0.2], ['True', 'False', 0.8], ['False', 'True', 0.01], ['False', 'False', 0.99]], [door_lock]) clock_alarm = DiscreteDistribution({'True': 0.8, 'False': 0.2}) light = ConditionalProbabilityTable( [['True', 'True', 'True', 0.96], ['True', 'True', 'False', 0.04], ['True', 'False', 'True', 0.89], ['True', 'False', 'False', 0.11], ['False', 'True', 'True', 0.96], ['False', 'True', 'False', 0.04], ['False', 'False', 'True', 0.89], ['False', 'False', 'False', 0.11]], [door_lock, clock_alarm]) coffee_maker = ConditionalProbabilityTable( [['True', 'True', 0.92], ['True', 'False', 0.08], ['False', 'True', 0.03], ['False', 'False', 0.97]], [clock_alarm]) window = ConditionalProbabilityTable( [['True', 'True', 0.885], ['True', 'False', 0.115], ['False', 'True', 0.04], ['False', 'False', 0.96]], [thermostate]) s0_door_lock = State(door_lock, name="door_lock") s1_clock_alarm = State(clock_alarm, name="clock_alarm") s2_light = State(light, name="light") s3_coffee_maker = State(coffee_maker, name="coffee_maker") s4_thermostate = State(thermostate, name="thermostate") s5_window = State(window, name="Window") network = BayesianNetwork("User_pref") network.add_nodes(s0_door_lock, s1_clock_alarm, s2_light, s3_coffee_maker, s4_thermostate, s5_window) network.add_edge(s0_door_lock, s2_light) network.add_edge(s0_door_lock, s4_thermostate) network.add_edge(s1_clock_alarm, s3_coffee_maker) network.add_edge(s1_clock_alarm, s2_light) network.add_edge(s4_thermostate, s5_window) network.bake() return network
def setup_huge_monty(): # Build the huge monty hall huge_monty_network. This is an example I made # up with which may not exactly flow logically, but tests a varied type of # tables ensures heterogeneous types of data work together. global huge_monty_network, huge_monty_friend, huge_monty_guest, huge_monty global huge_monty_remaining, huge_monty_randomize, huge_monty_prize # Huge_Monty_Friend huge_monty_friend = DiscreteDistribution({True: 0.5, False: 0.5}) # Huge_Monty_Guest emisisons are completely random huge_monty_guest = ConditionalProbabilityTable( [[True, 'A', 0.50], [True, 'B', 0.25], [True, 'C', 0.25], [False, 'A', 0.0], [False, 'B', 0.7], [False, 'C', 0.3]], [huge_monty_friend]) # Number of huge_monty_remaining cars huge_monty_remaining = DiscreteDistribution({0: 0.1, 1: 0.7, 2: 0.2, }) # Whether they huge_monty_randomize is dependent on the numnber of # huge_monty_remaining cars huge_monty_randomize = ConditionalProbabilityTable( [[0, True, 0.05], [0, False, 0.95], [1, True, 0.8], [1, False, 0.2], [2, True, 0.5], [2, False, 0.5]], [huge_monty_remaining]) # Where the huge_monty_prize is depends on if they huge_monty_randomize or # not and also the huge_monty_guests huge_monty_friend huge_monty_prize = ConditionalProbabilityTable( [[True, True, 'A', 0.3], [True, True, 'B', 0.4], [True, True, 'C', 0.3], [True, False, 'A', 0.2], [True, False, 'B', 0.4], [True, False, 'C', 0.4], [False, True, 'A', 0.1], [False, True, 'B', 0.9], [False, True, 'C', 0.0], [False, False, 'A', 0.0], [False, False, 'B', 0.4], [False, False, 'C', 0.6]], [huge_monty_randomize, huge_monty_friend]) # Monty is dependent on both the huge_monty_guest and the huge_monty_prize. huge_monty = ConditionalProbabilityTable( [['A', 'A', 'A', 0.0], ['A', 'A', 'B', 0.5], ['A', 'A', 'C', 0.5], ['A', 'B', 'A', 0.0], ['A', 'B', 'B', 0.0], ['A', 'B', 'C', 1.0], ['A', 'C', 'A', 0.0], ['A', 'C', 'B', 1.0], ['A', 'C', 'C', 0.0], ['B', 'A', 'A', 0.0], ['B', 'A', 'B', 0.0], ['B', 'A', 'C', 1.0], ['B', 'B', 'A', 0.5], ['B', 'B', 'B', 0.0], ['B', 'B', 'C', 0.5], ['B', 'C', 'A', 1.0], ['B', 'C', 'B', 0.0], ['B', 'C', 'C', 0.0], ['C', 'A', 'A', 0.0], ['C', 'A', 'B', 1.0], ['C', 'A', 'C', 0.0], ['C', 'B', 'A', 1.0], ['C', 'B', 'B', 0.0], ['C', 'B', 'C', 0.0], ['C', 'C', 'A', 0.5], ['C', 'C', 'B', 0.5], ['C', 'C', 'C', 0.0]], [huge_monty_guest, huge_monty_prize]) # Make the states s0 = State(huge_monty_friend, name="huge_monty_friend") s1 = State(huge_monty_guest, name="huge_monty_guest") s2 = State(huge_monty_prize, name="huge_monty_prize") s3 = State(huge_monty, name="huge_monty") s4 = State(huge_monty_remaining, name="huge_monty_remaining") s5 = State(huge_monty_randomize, name="huge_monty_randomize") # Make the bayes net, add the states, and the conditional dependencies. huge_monty_network = BayesianNetwork("test") huge_monty_network.add_nodes(s0, s1, s2, s3, s4, s5) huge_monty_network.add_transition(s0, s1) huge_monty_network.add_transition(s1, s3) huge_monty_network.add_transition(s2, s3) huge_monty_network.add_transition(s4, s5) huge_monty_network.add_transition(s5, s2) huge_monty_network.add_transition(s0, s2) huge_monty_network.bake()
sGenetics = Node(genetics, name="Genetics") sLungCancer = Node(lung_cancer, name="Lung_cancer") sYellowFingers = Node(yellow_fingers, name="Yellow_Fingers") sAttentionDisorder = Node(attention_disorder, name="Attention_Disorder") sCoughing = Node(coughing, name="Coughing") sFatigue = Node(fatigue, name="Fatigue") sCarAccident = Node(car_accident, name="Car_Accident") sAllergy = Node(allergy, name="Allergy") model = BayesianNetwork("Smoking Risk") model.add_nodes( sAnxiety, sPeerPressure, sSmoking, sGenetics, sLungCancer, sAllergy, sCoughing, sFatigue, sAttentionDisorder, sCarAccident, ) model.add_edge(sAnxiety, sSmoking) model.add_edge(sPeerPressure, sSmoking) model.add_edge(sSmoking, sLungCancer) model.add_edge(sGenetics, sLungCancer) model.add_edge(sGenetics, sAttentionDisorder) model.add_edge(sAllergy, sCoughing) model.add_edge(sLungCancer, sCoughing) model.add_edge(sCoughing, sFatigue) model.add_edge(sLungCancer, sFatigue) model.add_edge(sFatigue, sCarAccident)