def getArgs(argv): try: opts, args = getopt.getopt(sys.argv[1:], 'm:j:p:g:') except getopt.GetoptError as err: print str(err) sys.exit(2) output = None operation = None p_output = None val = None for o,a in opts: if o in ("-j"): operation = o output = a elif o == ("-m"): operation = o output = a elif o == ("-g"): operation = o output = a elif o == ("-p"): operation = o p_output = a[0] val = float(a[1:]) output = a[0] else: assert False, "unhandled option" pollution_val = .9 smoker_val = .3 if(p_output == "S"): bn = BayesNode.create_bayesNet(pollution_val, val) elif(p_output == "P"): bn = BayesNode.create_bayesNet(val, smoker_val) else: bn = BayesNode.create_bayesNet(pollution_val, smoker_val) #call functions for math if operation == "-m": if output == "p": print(bn.nodes["Pollution"].marginal) elif output == "~p": print(1.0 - bn.nodes["Pollution"].marginal) elif output == "P": print("True:/n") print(bn.nodes["Pollution"].marginal) print("/nFalse: ") print(1.0 - bn.nodes["Pollution"].marginal) elif output == "s": print(bn.nodes["Smoker"].marginal) elif output == "~s": print(1.0 - bn.nodes["Smoker"].marginal) elif output == "S": print("True:") print(bn.nodes["Smoker"].marginal) print("False: ") print(1.0 - bn.nodes["Smoker"].marginal) elif output == "c": print(bn.nodes["Cancer"].marginal) elif output == "~c": print(1.0 - bn.nodes["Cancer"].marginal) elif output == "C": print("True:") print(bn.nodes["Cancer"].marginal) print("False: ") print(1.0 - bn.nodes["Cancer"].marginal) elif output == "d": print(bn.nodes["Dyspnoea"].marginal) elif output == "~d": print(1.0 - bn.nodes["Dyspnoea"].marginal) elif output == "D": print("True:") print(bn.nodes["Dyspnoea"].marginal) print("False: ") print(1.0 - bn.nodes["Dyspnoea"].marginal) elif output == "x": print(bn.nodes["XRay"].marginal) elif output == "~x": print(1.0 - bn.nodes["XRay"].marginal) elif output == "X": print("True:") print(bn.nodes["XRay"].marginal) print("False: ") print(1.0 - bn.nodes["XRay"].marginal) else: print("Not a valid option") sys.exit(2) elif operation == "-j": if output == "PSC": print ProbabilityCalcs.joint_PSC(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"]) else: wanted = output print ProbabilityCalcs.joint_psc(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"], wanted) elif operation == "-g": #P=H row if output == "p|d": x = ProbabilityCalcs.probPollutionGivenDys(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x) elif output == "p|c": x = ProbabilityCalcs.probPollutionGivenCancer(bn.nodes["Cancer"], bn.nodes["XRay"], bn.nodes["Pollution"], bn.nodes["Smoker"]) print(x) elif output == "p|s": print(1.0 - bn.nodes["Pollution"].marginal) elif output == "p|cs" : x = ProbabilityCalcs.probPollutionGivenCancerAndSmoker(bn.nodes["Cancer"], bn.nodes["XRay"], bn.nodes["Pollution"], bn.nodes["Smoker"]) print(x) elif output == "p|ds" : x = ProbabilityCalcs.probPollutionGivenDysAndSmoker(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x) #S=T row elif output == "s|d": x = ProbabilityCalcs.probSmokerGivenDyspnoea(bn.nodes["Cancer"],bn.nodes["XRay"],bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x) elif output == "s|s" or output == "s|cs" or output == "s|sc": print("1.0") elif output == "s|c": x = ProbabilityCalcs.probSmokerGivenCancer(bn.nodes["Cancer"],bn.nodes["Pollution"],bn.nodes["Smoker"]) print(x) #C=T row elif output == "c|d": x = ProbabilityCalcs.probCancerGivenDyspnoea(bn.nodes["Cancer"], bn.nodes["Dyspnoea"]) print(x) elif output == "c|s": x = ProbabilityCalcs.probCancerGivenSmoker(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"]) print(x) elif output == "c|c": print("1.0") elif output == "c|ds": x = ProbabilityCalcs.probCancerGivenDysAndSmoker(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x) #not required elif output == "c|p": x = ProbabilityCalcs.probCancerGivenPollutionHigh(bn.nodes["Cancer"], bn.nodes["XRay"], bn.nodes["Pollution"], bn.nodes["Smoker"]) print(x) #X=T row elif output == "x|d": x = ProbabilityCalcs.probXrayGivenDys(bn.nodes["Cancer"], bn.nodes["XRay"], bn.nodes["Dyspnoea"]) print(x) elif output == "x|s": x = ProbabilityCalcs.probXrayGivenSmoker(bn.nodes["Cancer"], bn.nodes["XRay"], bn.nodes["Pollution"], bn.nodes["Smoker"]) print(x) elif output == "x|c" or output == "x|sc" or output == "x|cs": print(bn.nodes["XRay"].probabilities["C"]) elif output == "x|ds": x = ProbabilityCalcs.probXrayGivenDysandSmoker(bn.nodes["Smoker"], bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Dyspnoea"], bn.nodes["XRay"]) print(x) #D=T row elif output == "d|d": print("1.0") elif output == "d|s": x = ProbabilityCalcs.probDyspnoeaGivenSmoker(bn.nodes["Cancer"], bn.nodes["XRay"], bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x) elif output == "d|c" or "d|cs": x = ProbabilityCalcs.probDyspnoeaGivenCancer(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x) #not required elif output == "d|p": x = ProbabilityCalcs.probDyspnoeaGivenPollution(bn.nodes["Cancer"], bn.nodes["Pollution"], bn.nodes["Smoker"],bn.nodes["Dyspnoea"]) print(x)
def build_bnet(): """ Builds CBnet in accompanying gif : bnet_HuangDarwiche.gif From "Inference Belief Networks: A Procedural Guide", by C.Huang and A. Darwiche """ a_node = BayesNode(0, name="A") b_node = BayesNode(1, name="B") c_node = BayesNode(2, name="C") d_node = BayesNode(3, name="D") e_node = BayesNode(4, name="E") f_node = BayesNode(5, name="F") g_node = BayesNode(6, name="G") h_node = BayesNode(7, name="H") b_node.add_parent(a_node) c_node.add_parent(a_node) d_node.add_parent(b_node) e_node.add_parent(c_node) f_node.add_parent(d_node) f_node.add_parent(e_node) g_node.add_parent(c_node) h_node.add_parent(e_node) h_node.add_parent(g_node) nodes = {a_node, b_node, c_node, d_node, e_node, f_node, g_node, h_node} a_node.potential = DiscreteUniPot(False, a_node) # P(a) b_node.potential = DiscreteCondPot(False, [a_node, b_node]) # P(b| a) c_node.potential = DiscreteCondPot(False, [a_node, c_node]) d_node.potential = DiscreteCondPot(False, [b_node, d_node]) e_node.potential = DiscreteCondPot(False, [c_node, e_node]) # P(f|d, e) f_node.potential = DiscreteCondPot(False, [d_node, e_node, f_node]) g_node.potential = DiscreteCondPot(False, [c_node, g_node]) h_node.potential = DiscreteCondPot(False, [e_node, g_node, h_node]) # in general # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3) # off = 0 # on = 1 a_node.potential.pot_arr[:] = [.5, .5] b_node.potential.pot_arr[1, :] = [.5, .5] b_node.potential.pot_arr[0, :] = [.4, .6] c_node.potential.pot_arr[1, :] = [.7, .3] c_node.potential.pot_arr[0, :] = [.2, .8] d_node.potential.pot_arr[1, :] = [.9, .1] d_node.potential.pot_arr[0, :] = [.5, .5] e_node.potential.pot_arr[1, :] = [.3, .7] e_node.potential.pot_arr[0, :] = [.6, .4] f_node.potential.pot_arr[1, 1, :] = [.01, .99] f_node.potential.pot_arr[1, 0, :] = [.01, .99] f_node.potential.pot_arr[0, 1, :] = [.01, .99] f_node.potential.pot_arr[0, 0, :] = [.99, .01] g_node.potential.pot_arr[1, :] = [.8, .2] g_node.potential.pot_arr[0, :] = [.1, .9] h_node.potential.pot_arr[1, 1, :] = [.05, .95] h_node.potential.pot_arr[1, 0, :] = [.95, .05] h_node.potential.pot_arr[0, 1, :] = [.95, .05] h_node.potential.pot_arr[0, 0, :] = [.95, .05] return BayesNet(nodes)
def build_bnet(): """ Builds CBnet called WetGrass with diamond shape Cloudy / \ Rain Sprinkler \ / WetGrass All arrows pointing down """ cl = BayesNode(0, name="Cloudy") sp = BayesNode(1, name="Sprinkler") ra = BayesNode(2, name="Rain") we = BayesNode(3, name="WetGrass") we.add_parent(sp) we.add_parent(ra) sp.add_parent(cl) ra.add_parent(cl) nodes = {cl, ra, sp, we} cl.potential = DiscreteUniPot(False, cl) # P(a) sp.potential = DiscreteCondPot(False, [cl, sp]) # P(b| a) ra.potential = DiscreteCondPot(False, [cl, ra]) we.potential = DiscreteCondPot(False, [sp, ra, we]) # in general # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3) # off = 0 # on = 1 cl.potential.pot_arr[:] = [.5, .5] ra.potential.pot_arr[1, :] = [.5, .5] ra.potential.pot_arr[0, :] = [.4, .6] sp.potential.pot_arr[1, :] = [.7, .3] sp.potential.pot_arr[0, :] = [.2, .8] we.potential.pot_arr[1, 1, :] = [.01, .99] we.potential.pot_arr[1, 0, :] = [.01, .99] we.potential.pot_arr[0, 1, :] = [.01, .99] we.potential.pot_arr[0, 0, :] = [.99, .01] return BayesNet(nodes)
By an edge we mean a set of two nodes. Parameters ---------- nd : BayesNode Returns ------- list[set[BayesNode]] """ edges = list() neig_list = list(nd.neighbors) for k in range(len(neig_list)): n1 = neig_list[k] for n2 in neig_list[k+1:]: if not n2.has_neighbor(n1): edges.append({n1, n2}) return edges from Node import * if __name__ == "__main__": a = BayesNode(0, name="a") b = BayesNode(1, name="b") c = BayesNode(2, name="c") a.add_neighbor(b) a.add_neighbor(c) assert(Star.get_missing_edges_of_node(a) == [{b, c}])
def build_bnet(): """ Builds QBnet called QuWetGrass with diamond shape Cloudy / \ Rain Sprinkler \ / WetGrass All arrows pointing down """ cl = BayesNode(0, name="Cloudy") sp = BayesNode(1, name="Sprinkler") ra = BayesNode(2, name="Rain") we = BayesNode(3, name="WetGrass") we.add_parent(sp) we.add_parent(ra) sp.add_parent(cl) ra.add_parent(cl) nodes = {cl, ra, sp, we} cl.potential = DiscreteUniPot(True, cl) # P(a) sp.potential = DiscreteCondPot(True, [cl, sp]) # P(b| a) ra.potential = DiscreteCondPot(True, [cl, ra]) we.potential = DiscreteCondPot(True, [sp, ra, we]) # in general # DiscreteCondPot(True, [y1, y2, y3, x]) refers to A(x| y1, y2, y3) # off = 0 # on = 1 cl.potential.pot_arr[:] = [.5 + .1j, .5] ra.potential.pot_arr[1, :] = [.5 - .1j, .5 + .3j] ra.potential.pot_arr[0, :] = [.4, .6 - .7j] sp.potential.pot_arr[1, :] = [.7 + 3.j, .3 - 1.j] sp.potential.pot_arr[0, :] = [.2 + .5j, .8] we.potential.pot_arr[1, 1, :] = [.01 + 1j, .99] we.potential.pot_arr[1, 0, :] = [.01 - 5.j, .99] we.potential.pot_arr[0, 1, :] = [.01, .99 + 2.3j] we.potential.pot_arr[0, 0, :] = [.99, .01 - .01j] cl.potential.normalize_self() ra.potential.normalize_self() sp.potential.normalize_self() we.potential.normalize_self() return BayesNet(nodes)