# create new network dbnet = Network("Soliman2014DBN") # add nodes to network dbnet.add_nodes([node_m, node_k]+aarray+marray) # add link: must be prior to defining nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("Soliman2014DBN.dne") # inferences pfarray = [] for ai in aarray: beliefs = dbnet.get_node_beliefs(ai) aistate = np.searchsorted(ai.bins, acrit)-1 aitruncrv = ai.truncate_rv(aistate,lmd=trunclmd) lt = np.sum(beliefs[:aistate])+beliefs[aistate]*aitruncrv.cdf(acrit) pfarray.append(1-lt) aimeanarray = [] aistdarray = [] for ai in aarray: beliefs = dbnet.get_node_beliefs(ai) aimean,aivar = ai.node_stats(beliefs,lmd=trunclmd)
# create new network dbnet = Network("Soliman2014DBN") # add nodes to network dbnet.add_nodes([node_m, node_k] + aarray + marray) # add link: must be prior to defining nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("Soliman2014DBN.dne") # inferences pfarray = [] for ai in aarray: beliefs = dbnet.get_node_beliefs(ai) aistate = np.searchsorted(ai.bins, acrit + 1e-10) - 1 aitruncrv = ai.truncate_rv(aistate, lmd=trunclmd) lt = np.sum( beliefs[:aistate]) + beliefs[aistate] * aitruncrv.cdf(acrit) pfarray.append(1 - lt) aimeanarray = [] aistdarray = [] for ai in aarray: beliefs = dbnet.get_node_beliefs(ai)
# create new network dbnet = Network("Straub2010Ex2") # add nodes to network dbnet.add_nodes([r5, r4, m5, m4, uh, q]+harray+earray) # add link: must be prior to defining nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("Straub2010Ex2.dne") # no evidence print "Prior Belief:" beta0 = [] for e in earray[1:]: beliefs = dbnet.get_node_beliefs(e) beta0.append(-stats.norm.ppf(beliefs[1])) uhbeliefs0 = dbnet.get_node_beliefs(uh) # evidence 1: survive up to year 5, h5=80 print "Evidence 1: survive up to year 5, h5=80" for e in earray[1:6]: dbnet.enter_finding(e,0) h5ev = 80 h5 = harray[4]
netp.add_nodes([visitAsia]) netp.add_nodes([smoking]) netp.add_nodes([tuberculosis]) netp.add_nodes([cancer]) netp.add_nodes([tbOrCa, xRay]) # add link: must before define nodes netp.add_link() # define nodes netp.define_nodes() # compile the net netp.compile_net() # enable auto updating netp.set_autoupdate() # save the network netp.save_net('ChestClinicNew.dne') # prior belief beliefs = netp.get_node_beliefs(tuberculosis) belief = beliefs[0] print 'The probability of tuberculosis is {:f}\n'.format(belief) # posterior belief netp.enter_finding(xRay, 0) beliefs = netp.get_node_beliefs(tuberculosis) belief = beliefs[0] print 'Given an abnormal X-ray,' print ' the probability of tuberculosis is {:f}\n'.format(belief) netp.enter_finding(visitAsia, 0) beliefs = netp.get_node_beliefs(tuberculosis)
return utilr node_fr.assign_func(failure_risk) # create new network dbnet = Network("Soliman2014InfDiag") # add nodes to network dbnet.add_nodes([node_m, node_k] + aarray + marray + insparray + uiarray + rarray + urarray + [node_fr]) # add link: must be prior to defining nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("Soliman2014InfDiag.dne") #type 2 dbnet.retract_netfindings() dummy = dbnet.get_node_expectedutils(node_insp) dummy = dbnet.get_node_expectedutils(node_repair) decision = dbnet.get_node_funcstate(node_repair, [2, 4]) print 'If the inspection decision is {} and meausre is {}, the best repair decision is {}.\n'.format( node_insp.statenames[2], marray[0].statenames[4], node_repair.statenames[decision])
# create new network dbnet = Network("BNexample1") # add nodes to network dbnet.add_nodes([weather, forecast, umbrella, satisfy]) # add link: must before define nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("BNdecision.dne") # decision making # type 1 dbnet.enter_finding(forecast, 0) utils = dbnet.get_node_expectedutils(umbrella) print 'If the forecast is sunny, expected utility of {} is {}, of {} is {}\n'.format( umbrella.statenames[0], utils[0], umbrella.statenames[1], utils[1]) dbnet.retract_netfindings() dbnet.enter_finding(forecast, 1) utils = dbnet.get_node_expectedutils(umbrella) print 'If the forecast is cloudy, expected utility of {} is {}, of {} is {}\n'.format( umbrella.statenames[0], utils[0],
# create new network dbnet = Network("Straub2010Ex2") # add nodes to network dbnet.add_nodes([r5, r4, m5, m4, uh, q] + harray + earray) # add link: must be prior to defining nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("Straub2010Ex2.dne") # no evidence print "Prior Belief:" beta0 = [] for e in earray[1:]: beliefs = dbnet.get_node_beliefs(e) beta0.append(-stats.norm.ppf(beliefs[1])) uhbeliefs0 = dbnet.get_node_beliefs(uh) # evidence 1: survive up to year 5, h5=80 print "Evidence 1: survive up to year 5, h5=80" for e in earray[1:6]: dbnet.enter_finding(e, 0) h5ev = 80 h5 = harray[4]
return utilr node_fr.assign_func(failure_risk) # create new network dbnet = Network("Soliman2014InfDiag") # add nodes to network dbnet.add_nodes([node_m, node_k]+aarray+marray+insparray+uiarray+rarray+urarray+[node_fr]) # add link: must be prior to defining nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("Soliman2014InfDiag.dne") #type 2 dbnet.retract_netfindings() dummy = dbnet.get_node_expectedutils(node_insp) dummy = dbnet.get_node_expectedutils(node_repair) decision = dbnet.get_node_funcstate(node_repair, [2,4]) print 'If the inspection decision is {} and meausre is {}, the best repair decision is {}.\n'.format( node_insp.statenames[2], marray[0].statenames[4], node_repair.statenames[decision])
# create new network dbnet = Network("BNexample1") # add nodes to network dbnet.add_nodes([x1, x2, y]) # add link: must before define nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("BNexample1.dne") # prior belief beliefs = dbnet.get_node_beliefs(y) print "Prior Belief:" for i in xrange(y.nstates()): print 'The probability of {} is {:f}'.format(y.statenames[i], beliefs[i]) x1beliefs = dbnet.get_node_beliefs(x1) # statesvalues = x1.statenames.astype(float) x1stats = x1.node_stats(x1beliefs) print 'The mean of {} is {:f}'.format(x1.name, x1stats[0]) print 'The std of {} is {:f}'.format(x1.name, np.sqrt(x1stats[1])) x2beliefs = dbnet.get_node_beliefs(x2) # statesvalues = x2.statenames.astype(float)
# create new network dbnet = Network("BNexample1") # add nodes to network dbnet.add_nodes([weather, forecast, umbrella, satisfy]) # add link: must before define nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network dbnet.save_net("BNdecision.dne") # decision making # type 1 dbnet.enter_finding(forecast, 0) utils = dbnet.get_node_expectedutils(umbrella) print 'If the forecast is sunny, expected utility of {} is {}, of {} is {}\n'.format( umbrella.statenames[0], utils[0], umbrella.statenames[1], utils[1]) dbnet.retract_netfindings() dbnet.enter_finding(forecast, 1) utils = dbnet.get_node_expectedutils(umbrella) print 'If the forecast is cloudy, expected utility of {} is {}, of {} is {}\n'.format( umbrella.statenames[0], utils[0], umbrella.statenames[1], utils[1]) dbnet.retract_netfindings()