def __init__(self): Pollution = DiscreteDistribution({'F': 0.9, 'T': 0.1}) Smoker = DiscreteDistribution({'T': 0.3, 'F': 0.7}) print(Smoker) Cancer = ConditionalProbabilityTable([ ['T', 'T', 'T', 0.05], ['T', 'T', 'F', 0.95], ['T', 'F', 'T', 0.02], ['T', 'F', 'F', 0.98], ['F', 'T', 'T', 0.03], ['F', 'T', 'F', 0.97], ['F', 'F', 'T', 0.001], ['F', 'F', 'F', 0.999], ], [Pollution, Smoker]) print(Cancer) XRay = ConditionalProbabilityTable([ ['T', 'T', 0.9], ['T', 'F', 0.1], ['F', 'T', 0.2], ['F', 'F', 0.8], ], [Cancer]) Dyspnoea = ConditionalProbabilityTable([ ['T', 'T', 0.65], ['T', 'F', 0.35], ['F', 'T', 0.3], ['F', 'F', 0.7], ], [Cancer]) s1 = Node(Pollution, name="Pollution") s2 = Node(Smoker, name="Smoker") s3 = Node(Cancer, name="Cancer") s4 = Node(XRay, name="XRay") s5 = Node(Dyspnoea, name="Dyspnoea") model = BayesianNetwork("Lung Cancer") model.add_states(s1, s2, s3, s4, s5) model.add_edge(s1, s3) model.add_edge(s2, s3) model.add_edge(s3, s4) model.add_edge(s3, s5) model.bake() self.model = model meta = [] name_mapper = ["Pollution", "Smoker", "Cancer", "XRay", "Dyspnoea"] for i in range(self.model.node_count()): meta.append({ "name": name_mapper[i], "type": "categorical", "size": 2, "i2s": ['T', 'F'] }) self.meta = meta
def __init__(self): A = DiscreteDistribution({'1': 1. / 3, '2': 1. / 3, '3': 1. / 3}) B = ConditionalProbabilityTable([ ['1', '1', 0.5], ['1', '2', 0.5], ['1', '3', 0], ['2', '1', 0], ['2', '2', 0.5], ['2', '3', 0.5], ['3', '1', 0.5], ['3', '2', 0], ['3', '3', 0.5], ], [A]) C = ConditionalProbabilityTable([ ['1', '4', 0.5], ['1', '5', 0.5], ['1', '6', 0], ['2', '4', 0], ['2', '5', 0.5], ['2', '6', 0.5], ['3', '4', 0.5], ['3', '5', 0], ['3', '6', 0.5], ], [A]) s1 = Node(A, name="A") s2 = Node(B, name="B") s3 = Node(C, name="C") model = BayesianNetwork("tree") model.add_states(s1, s2, s3) model.add_edge(s1, s2) model.add_edge(s1, s3) model.bake() self.model = model meta = [] for i in range(self.model.node_count() - 1): meta.append({ "name": chr(ord('A') + i), "type": "categorical", "size": 3, "i2s": ['1', '2', '3'] }) meta.append({ "name": "C", "type": "categorical", "size": 3, "i2s": ['4', '5', '6'] }) self.meta = meta
def build_net(cpts): states = dict() for name, cpt in cpts.items(): states[name] = State(cpt, name=name) model = BayesianNetwork('Poker Game') model.add_states(*list(states.values())) for name, parents, _ in sheets: for parent in parents: print(states[parent]) model.add_transition(states[parent], states[name]) model.bake() return model
def __init__(self): Rain = DiscreteDistribution({'T': 0.2, 'F': 0.8}) Sprinkler = ConditionalProbabilityTable([ ['F', 'T', 0.4], ['F', 'F', 0.6], ['T', 'T', 0.1], ['T', 'F', 0.9], ], [Rain]) Wet = ConditionalProbabilityTable([ ['F', 'F', 'T', 0.01], ['F', 'F', 'F', 0.99], ['F', 'T', 'T', 0.8], ['F', 'T', 'F', 0.2], ['T', 'F', 'T', 0.9], ['T', 'F', 'F', 0.1], ['T', 'T', 'T', 0.99], ['T', 'T', 'F', 0.01], ], [Sprinkler, Rain]) s1 = Node(Rain, name="Rain") s2 = Node(Sprinkler, name="Sprinkler") s3 = Node(Wet, name="Wet") model = BayesianNetwork("Simple fully connected") model.add_states(s1, s2, s3) model.add_edge(s1, s2) model.add_edge(s1, s3) model.add_edge(s2, s3) model.bake() self.model = model meta = [] for i in range(self.model.node_count()): meta.append({ "name": None, "type": "categorical", "size": 2, "i2s": ['T', 'F'] }) meta[0]['name'] = 'Rain' meta[1]['name'] = 'Sprinkler' meta[2]['name'] = 'Wet' self.meta = meta
from pomegranate import DiscreteDistribution from pomegranate import ConditionalProbabilityTable from pomegranate import BayesianNetwork from pomegranate import Node guest = DiscreteDistribution({'A': 1. / 3, 'B': 1. / 3, 'C': 1. / 3}) prize = DiscreteDistribution({'A': 1. / 3, 'B': 1. / 3, 'C': 1. / 3}) 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]) s1 = Node(guest, name="guest") s2 = Node(prize, name="prize") s3 = Node(monty, name="monty") model = BayesianNetwork("Monty Hall Problem") model.add_states(s1, s2, s3) model.add_edge(s1, s3) model.add_edge(s2, s3) model.bake()
def __init__(self, filename): with open(filename) as f: bif = f.read() vars = re.findall(r"variable[^\{]+{[^\}]+}", bif) probs = re.findall(r"probability[^\{]+{[^\}]+}", bif) var_nodes = {} var_index_to_name = [] edges = [] self.meta = [] todo = set() for v, p in zip(vars, probs): m = re.search(r"variable\s+([^\{\s]+)\s+", v) v_name = m.group(1) m = re.search(r"type\s+discrete\s+\[\s*(\d+)\s*\]\s*\{([^\}]+)\}", v) v_opts_n = int(m.group(1)) v_opts = m.group(2).replace(',', ' ').split() assert v_opts_n == len(v_opts) # print(v_name, v_opts_n, v_opts) m = re.search(r"probability\s*\(([^)]+)\)", p) cond = m.group(1).replace('|', ' ').replace(',', ' ').split() assert cond[0] == v_name # print(cond) self.meta.append({ "name": v_name, "type": "categorical", "size": v_opts_n, "i2s": v_opts }) if len(cond) == 1: m = re.search(r"table([e\-\d\.\s,]*);", p) margin_p = m.group(1).replace(',', ' ').split() margin_p = [float(x) for x in margin_p] assert abs(sum(margin_p) - 1) < 1e-6 assert len(margin_p) == v_opts_n margin_p = dict(zip(v_opts, margin_p)) var_index_to_name.append(v_name) tmp = DiscreteDistribution(margin_p) # print(tmp) var_nodes[v_name] = tmp else: m_iter = re.finditer(r"\(([^)]*)\)([\s\d\.,\-e]+);", p) cond_p_table = [] for m in m_iter: cond_values = m.group(1).replace(',', ' ').split() cond_p = m.group(2).replace(',', ' ').split() cond_p = [float(x) for x in cond_p] assert len(cond_values) == len(cond) - 1 assert len(cond_p) == v_opts_n assert abs(sum(cond_p) - 1) < 1e-6 for opt, opt_p in zip(v_opts, cond_p): cond_p_table.append(cond_values + [opt, opt_p]) var_index_to_name.append(v_name) tmp = (cond_p_table, cond) # print(tmp) var_nodes[v_name] = tmp for x in cond[1:]: edges.append((x, v_name)) todo.add(v_name) while len(todo) > 0: # print(todo) for v_name in todo: # print(v_name, type(var_nodes[v_name])) cond_p_table, cond = var_nodes[v_name] flag = True for y in cond[1:]: if y in todo: flag = False break if flag: cond_t = [var_nodes[x] for x in cond[1:]] var_nodes[v_name] = ConditionalProbabilityTable( cond_p_table, cond_t) todo.remove(v_name) break for x in var_index_to_name: var_nodes[x] = Node(var_nodes[x], name=x) var_nodes_list = [var_nodes[x] for x in var_index_to_name] # print(var_nodes_list) model = BayesianNetwork("tmp") model.add_states(*var_nodes_list) for edge in edges: model.add_edge(var_nodes[edge[0]], var_nodes[edge[1]]) model.bake() # print(model.to_json()) self.model = model
["none", "no", "delayed", 0.1], ["light", "yes", "on time", 0.6], ["light", "yes", "delayed", 0.4], ["light", "no", "on time", 0.7], ["light", "no", "delayed", 0.3], ["heavy", "yes", "on time", 0.4], ["heavy", "yes", "delayed", 0.6], ["heavy", "no", "on time", 0.5], ["heavy", "no", "delayed", 0.5], ], [rain.distribution, maintenance.distribution]), name="train") # Appointment node is conditional on train appointment = Node(ConditionalProbabilityTable( [["on time", "attend", 0.9], ["on time", "miss", 0.1], ["delayed", "attend", 0.6], ["delayed", "miss", 0.4]], [train.distribution]), name="appointment") # Create a bayesian network and add the states model = BayesianNetwork() model.add_states(rain, maintenance, train, appointment) # Add edges connecting nodes model.add_edge(rain, maintenance) model.add_edge(rain, train) model.add_edge(maintenance, train) model.add_edge(train, appointment) # Finalize model model.bake()
states['Anxiety'] = State(Anxiety, name="Anxiety") states['Peer_Pressure'] = State(Peer_Pressure, name="Peer_Pressure") states['Smoking'] = State(Smoking, name="Smoking") states['Yellow_Fingers'] = State(Yellow_Fingers, name="Yellow_Fingers") states['Genetics'] = State(Genetics, name="Genetics") states['Lung_cancer'] = State(Lung_cancer, name="Lung_cancer") states['Attention_Disorder'] = State(Attention_Disorder, name="Attention_Disorder") states['Allergy'] = State(Allergy, name="Allergy") states['Coughing'] = State(Coughing, name="Coughing") states['Born_an_Even_Day'] = State(Born_an_Even_Day, name="Born_an_Even_Day") states['Fatigue'] = State(Fatigue, name="Fatigue") states['Car_Accident' ] = State(Car_Accident, name="Car_Accident") network = BayesianNetwork("Monty hall problem") network.add_states(*states.values()) network.add_edge(states["Peer_Pressure"],states["Smoking"]) network.add_edge(states["Anxiety"],states["Smoking"]) network.add_edge(states["Smoking"],states["Yellow_Fingers"]) network.add_edge(states["Genetics"],states["Lung_cancer"]) network.add_edge(states["Smoking"],states["Lung_cancer"]) network.add_edge(states["Genetics"],states["Attention_Disorder"]) network.add_edge(states['Lung_cancer'], states["Coughing"]) network.add_edge(states['Allergy'], states["Coughing"]) network.add_edge(states['Coughing'], states["Fatigue"]) network.add_edge(states['Lung_cancer'], states["Fatigue"]) network.add_edge(states["Fatigue"], states["Car_Accident"]) network.add_edge(states["Attention_Disorder"], states["Car_Accident"]) import ast network.bake() beliefs = network.predict_proba({"Genetics":"T"},max_iterations=100000)
ashwin = ConditionalProbabilityTable( returnConditionalProbability(df, 'Location', 'Ashwin'), [location]) batting = ConditionalProbabilityTable( returnConditionalProbability(df, 'Toss', 'Bat'), [toss]) result = ConditionalProbabilityTable( returnConditionalProbability(df, 'Bat', 'Result'), [batting]) sLocation = State(location, name="Location") sToss = State(toss, name="Toss") sBatting = State(batting, name="Batting") sAshwin = State(ashwin, name="Ashwin") sResult = State(result, name="Result") # Create the Bayesian network object with a useful name model = BayesianNetwork("Ashwin Playing Problem") # Add the three states to the network model.add_states(sLocation, sToss, sBatting, sAshwin, sResult) model.add_edge(sLocation, sAshwin) model.add_edge(sToss, sBatting) model.add_edge(sBatting, sResult) model.bake() model.predict_proba([None, None, '2nd', 'Y', 'won'])[1] model.predict_proba([None, None, '2nd', 'N', 'won'])[0] model.predict_proba([None, None, '2nd', 'Y', 'lost'])[1] model.predict_proba([None, None, '2nd', 'N', 'lost'])[0]