rvs = truncrvs aimean = rvs[0].stats('m')[()] rv_am = stats.norm(aimean, sigmae) pod = 1.-stats.norm.cdf((np.log(aimean)-lmd)/beta) probs = rv_am.cdf(node_mi.bins[1:])-rv_am.cdf(node_mi.bins[:-1]) print 'labels: {}, progress: {}%, prob: {}'.format(label, float(i)/len(labels)*100, np.array_str(probs,precision=3)) probs = probs/np.sum(probs)*pod probs[0] = probs[0]+(1.-pod) node_mi.assign_cpt(probs,label=np.asarray(label),statenames=node_mi.statenames) # 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:
pod = 1. - stats.norm.cdf((np.log(aimean) - lmd2) / beta2) probs = rv_am.cdf(node_mi.bins[1:]) - rv_am.cdf(node_mi.bins[:-1]) print 'labels: {}, progress: {}%, prob: {}'.format( label, float(i) / len(labels) * 100, np.array_str(probs, precision=3)) probs = probs / np.sum(probs) * pod probs[0] = probs[0] + (1. - pod) node_mi.assign_cpt(probs, label=np.asarray(label), statenames=node_mi.statenames) # 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:
for j,pstate in enumerate(label[1:]): truncrvs.append(e.parents[j+1].truncate_rv(pstate)) if label[0] == 1: # failure in previous time slot prob = 1. else: prob = intg2e(truncrvs[0], truncrvs[1]) print 'labels: {}, progress: {}%, prob: {}'.format(label, float(i)/len(labels)*100, prob) probs = np.array([1.-prob, prob]) e.assign_cpt(probs,label=np.asarray(label),statenames=['safe','fail'], labels=labels) # 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 = []
# assign CPT visitAsiaCpt = np.array([0.01, 0.99])[np.newaxis, :] visitAsia.assign_cpt(visitAsiaCpt, statenames=['visit', 'no_visit']) smokingCpt = np.array([0.50, 0.50])[np.newaxis, :] smoking.assign_cpt(smokingCpt, statenames=['smoker', 'nonsmoker']) tuberCpt = np.array([[0.05, 0.95], [0.01, 0.99]]) tuberculosis.assign_cpt(tuberCpt, statenames=['present', 'absent']) cancerCpt = np.array([[0.10, 0.90], [0.01, 0.99]]) cancer.assign_cpt(cancerCpt, statenames=['present', 'absent']) tbOrCaCpt = np.array([[1.0, 0.0], [1.0, 0.0], [1.0, 0.0], [0.0, 1.0]]) tbOrCa.assign_cpt(tbOrCaCpt, statenames=['true', 'false']) xRayCpt = np.array([[0.98, 0.02], [0.05, 0.95]]) xRay.assign_cpt(xRayCpt, statenames=['abnormal', 'normal']) # add node 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')
for j,pstate in enumerate(label): truncrvs.append(e.parents[j].truncate_rv(pstate)) rvnames = ['ur', 'u1', 'u2', 'u3', 'u4', 'u5', 'h', 'v'] rvs = [ur, u1, u2, u3, u4, u5, h, v] trunclb = [rv.lb for rv in truncrvs] truncub = [rv.ub for rv in truncrvs] syspf = msr2e(rvnames, rvs, logmean, logstd, rolnR, trunclb, truncub) probs = np.array([1.-syspf, syspf]) e.assign_cpt(probs,label=np.asarray(label),statenames=['safe', 'fail']) print 'labels: {}, progress: {}%, pf: {}'.format(label, float(i)/len(labels)*100, syspf) # create new network dbnet = Network("Straub2010Ex1") # add nodes to network dbnet.add_nodes([r5, r4, m5, m4, e]) # 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("Straub2010Ex1.dne") # prior belief beliefs = dbnet.get_node_beliefs(e) print "Prior Belief:"
# assign CPT visitAsiaCpt = np.array([0.01, 0.99])[np.newaxis, :] visitAsia.assign_cpt(visitAsiaCpt, statenames=['visit','no_visit']) smokingCpt = np.array([0.50, 0.50])[np.newaxis, :] smoking.assign_cpt(smokingCpt, statenames=['smoker','nonsmoker']) tuberCpt = np.array([[0.05, 0.95], [0.01, 0.99]]) tuberculosis.assign_cpt(tuberCpt, statenames=['present','absent']) cancerCpt = np.array([[0.10, 0.90], [0.01, 0.99]]) cancer.assign_cpt(cancerCpt, statenames=['present','absent']) tbOrCaCpt = np.array([[1.0, 0.0], [1.0, 0.0], [1.0, 0.0], [0.0, 1.0]]) tbOrCa.assign_cpt(tbOrCaCpt, statenames=['true','false']) xRayCpt = np.array([[0.98, 0.02], [0.05, 0.95]]) xRay.assign_cpt(xRayCpt, statenames=['abnormal','normal']) # add node 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')
def calculate_util(pstate): forecaststate,umbrellastate = pstate if forecaststate==0 and umbrellastate==0: return 20. elif forecaststate==0 and umbrellastate==1: return 100. elif forecaststate==1 and umbrellastate==0: return 70. elif forecaststate==1 and umbrellastate==1: return 0. satisfy.assign_func(calculate_util) # 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
if aistate < acrstate: utilr = 0. elif aistate > acrstate: utilr = -Cfail elif aistate == acrstate: pf = 1. - truncrv_ai.cdf(acrit) utilr = -pf * Cfail 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)
else: prob = intg2e(truncrvs[0], truncrvs[1]) print 'labels: {}, progress: {}%, prob: {}'.format( label, float(i) / len(labels) * 100, prob) probs = np.array([1. - prob, prob]) e.assign_cpt(probs, label=np.asarray(label), statenames=['safe', 'fail'], labels=labels) # 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 = []
utilr = 0. elif aistate>acrstate: utilr = -Cfail elif aistate == acrstate: pf = 1.-truncrv_ai.cdf(acrit) utilr = -pf*Cfail 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()
corr = np.eye(nstate) for i, label in enumerate(labels): rvs = [] probs=[] for j, pstate in enumerate(label): rvs.append(y.parents[j].truncate_rv(pstate, lmd=lmd)) rvs.append(rvX3) pf = form2y(rvnames, rvs, corr) probs = np.array([1.-pf, pf]) y.assign_cpt(probs,label=np.asarray(label),statenames=['neg', 'init']) # 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:"
trunclb = [rv.lb for rv in truncrvs] truncub = [rv.ub for rv in truncrvs] syspf = msr2e(rvnames, rvs, logmean, logstd, rolnR, trunclb, truncub) probs = np.array([1. - syspf, syspf]) e.assign_cpt(probs, label=np.asarray(label), statenames=['safe', 'fail']) print 'labels: {}, progress: {}%, pf: {}'.format( label, float(i) / len(labels) * 100, syspf) # create new network dbnet = Network("Straub2010Ex1") # add nodes to network dbnet.add_nodes([r5, r4, m5, m4, e]) # 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("Straub2010Ex1.dne") # prior belief beliefs = dbnet.get_node_beliefs(e) print "Prior Belief:"
if forecaststate == 0 and umbrellastate == 0: return 20. elif forecaststate == 0 and umbrellastate == 1: return 100. elif forecaststate == 1 and umbrellastate == 0: return 70. elif forecaststate == 1 and umbrellastate == 1: return 0. satisfy.assign_func(calculate_util) # 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)