# 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) aimeanarray.append(aimean) aistdarray.append(np.sqrt(aivar))
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) aimean, aivar = ai.node_stats(beliefs, lmd=trunclmd) aimeanarray.append(aimean) aistdarray.append(np.sqrt(aivar))
# 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) x2stats = x2.node_stats(x2beliefs, lmd=lmd) print 'The mean of {} is {:f}'.format(x2.name, x2stats[0]) print 'The std of {} is {:f}'.format(x2.name, np.sqrt(x2stats[1])) optx, dummy = x2.fit_stats('lognormal', x2beliefs, lmd=lmd) print 'The mean of {} by fitting cdf is {:f}'.format(x2.name, optx[0]) print 'The std of {} by fitting cdf is {:f}'.format(x2.name, np.sqrt(optx[1])) # posterior belief dbnet.enter_finding(x1, 2) beliefs = dbnet.get_node_beliefs(y)