rvs = truncrvs aimean = rvs[0].stats('m')[()] rv_am = stats.norm(aimean, sigmae) 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")
from pyNetica import Network, Node import numpy as np # create new net netp = Network("ChestClinic") # define nodes visitAsia = Node("VisitAsia", parents=None, rvname='discrete') smoking = Node("Smoking", parents=None, rvname='discrete') tuberculosis = Node("Tuberculosis", parents=[visitAsia], rvname='discrete') cancer = Node("Cancer", parents=[smoking], rvname='discrete') tbOrCa = Node("TbOrCa", parents=[tuberculosis, cancer], rvname='discrete') xRay = Node("XRay", parents=[tbOrCa], rvname='discrete') # 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])
for j,pstate in enumerate(label): truncrvs.append(node_mi.parents[j].truncate_rv(pstate,lmd=trunclmd)) rvnames = ['Ai'] 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")
from pyNetica import Network, Node import numpy as np # create new net netp = Network("ChestClinic") # define nodes visitAsia = Node("VisitAsia", parents=None, rvname='discrete') smoking = Node("Smoking", parents=None, rvname='discrete') tuberculosis = Node("Tuberculosis", parents=[visitAsia], rvname='discrete') cancer = Node("Cancer", parents=[smoking], rvname='discrete') tbOrCa = Node("TbOrCa", parents=[tuberculosis, cancer], rvname='discrete') xRay = Node("XRay", parents=[tbOrCa], rvname='discrete') # 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])
i = 0 for i,label in enumerate(labels): truncrvs=[] 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")
aistate, = pstate acrstate = np.searchsorted(node_al.bins, acrit + 1e-10) - 1 truncrv_ai = node_ai.truncate_rv(aistate, lmd=trunclmd) 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")
# labels = [(10,10),(4,4),(4,4)] for i,label in enumerate(labels): truncrvs=[] 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")
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")
# node umbrella umbrella.set_node_state_name(['take_umbrella', 'dont_take_umbrella']) # node satisfy 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")
acrstate = np.searchsorted(node_al.bins, acrit)-1 truncrv_ai = node_ai.truncate_rv(aistate, lmd=trunclmd) 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")
nstate = 2 labels = itertools.product(np.arange(x2.nstates())) rvnames = [x2.name, 'X3'] 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")
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")
from pyNetica import Network def statename2edges(statenames): edges = [] for statename in statenames: numstr = statename[1:].replace('_', '.') if numstr != 'inf': edges.append(numstr) else: continue return np.array(edges, dtype=float) if __name__ == '__main__': # load net from dbn file dbnet = Network('Soliman2014DBN', file='Soliman2014DBN.dne') # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() m3 = dbnet.find_nodenamed('M3') if m3 == 'error': print "ERROR: cannot find node M3" sys.exit(1) m3meaure = 0.6 m3edges = statename2edges(m3.statenames) m3bins = np.hstack((-np.inf, m3edges, np.inf)) m3state = np.searchsorted(m3bins, m3meaure)-1 dbnet.enter_finding(m3, m3state) a5 = dbnet.find_nodenamed('A5')
# node satisfy 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")