def create_node_mi(name, parents, minum, milb, miub, mibins): node_mi = Node(name, parents=parents, rvname='continuous') minames = node_mi.discretize(milb, miub, minum, infinity='+-', bins=mibins) node_insp = parents[0] node_ai = parents[1] ainum = node_ai.nstates() labels = itertools.product(np.arange(node_insp.nstates()), np.arange(ainum)) labels = [label for label in labels] for i,label in enumerate(labels): if label[0] == 0: #no inspection probs = 1./minum * np.ones(minum) else: # with insepction if label[0] == 1: lmd = lmd1; beta = beta1 elif label[0] == 2: lmd = lmd2; beta = beta2 elif label[0] == 3: lmd = lmd3; beta = beta3 rvnames = ['Ai'] rvs = [node_ai.truncate_rv(label[1], lmd=trunclmd)] 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]) 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) # print 'labels: {}, progress: {}%, prob: {}'.format(label, # float(i)/len(labels)*100, np.array_str(probs,precision=3)) return node_mi
def create_node_a(name, parents, ainum, ailb, aiub, aiedges, node_repair=None, asmp0=None): if node_repair is None: node_ai = Node(name, parents=parents, rvname='continuous') else: node_ai = Node(name, parents=parents + [node_repair], rvname='continuous') # dynamic discretization of nodes a and M node_ap = parents[0] knum = parents[1].nstates() mnum = parents[2].nstates() ainames = node_ai.discretize(ailb, aiub, ainum, infinity='+', bins=aiedges) aibins = node_ai.bins if node_repair is None: labels = itertools.product(np.arange(node_ap.nstates()), np.arange(knum), np.arange(mnum)) else: labels = itertools.product(np.arange(node_ap.nstates()), np.arange(knum), np.arange(mnum), np.arange(2)) labels = [label for label in labels] for i, label in enumerate(labels): if len(label) == 4 and label[-1] == 1: binnum, dummy = np.histogram(asmp0, aibins) probs = binnum / np.sum(binnum, dtype=float) node_ai.assign_cpt(probs, label=np.asarray(label), statenames=node_ai.statenames) else: truncrvs = [] for j, pstate in enumerate(label): truncrvs.append(node_ai.parents[j].truncate_rv(pstate, lmd=trunclmd)) rvnames = ['Ap', 'K', 'M'] rvs = truncrvs[:3] probs, smpdb = mc2ai(rvnames, rvs, node_ai.bins, acrit, nsmp=nsmp) # clean Ai states given Ai-1 apstate = label[0] aplb = node_ap.bins[apstate] aiubs = node_ai.bins[1:] probs[aiubs <= aplb] = 0. probs = probs / np.sum(probs) node_ai.assign_cpt(probs, label=np.asarray(label), statenames=node_ai.statenames) # print 'labels: {}, progress: {}%, prob: {}'.format(label, # float(i)/len(labels)*100, np.array_str(probs,precision=3)) return node_ai
def create_node_mi(name, parents, minum, milb, miub, mibins): node_mi = Node(name, parents=parents, rvname='continuous') minames = node_mi.discretize(milb, miub, minum, infinity='+-', bins=mibins) node_insp = parents[0] node_ai = parents[1] ainum = node_ai.nstates() labels = itertools.product(np.arange(node_insp.nstates()), np.arange(ainum)) labels = [label for label in labels] for i, label in enumerate(labels): if label[0] == 0: #no inspection probs = 1. / minum * np.ones(minum) else: # with insepction if label[0] == 1: lmd = lmd1 beta = beta1 elif label[0] == 2: lmd = lmd2 beta = beta2 elif label[0] == 3: lmd = lmd3 beta = beta3 rvnames = ['Ai'] rvs = [node_ai.truncate_rv(label[1], lmd=trunclmd)] 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]) 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) # print 'labels: {}, progress: {}%, prob: {}'.format(label, # float(i)/len(labels)*100, np.array_str(probs,precision=3)) return node_mi
def create_node_a(name, parents, ainum, ailb, aiub, aiedges, node_repair=None, asmp0=None): if node_repair is None: node_ai = Node(name, parents=parents, rvname='continuous') else: node_ai = Node(name, parents=parents+[node_repair], rvname='continuous') # dynamic discretization of nodes a and M node_ap = parents[0] knum = parents[1].nstates() mnum = parents[2].nstates() ainames = node_ai.discretize(ailb, aiub, ainum, infinity='+', bins=aiedges) aibins = node_ai.bins if node_repair is None: labels = itertools.product(np.arange(node_ap.nstates()), np.arange(knum),np.arange(mnum)) else: labels = itertools.product(np.arange(node_ap.nstates()), np.arange(knum), np.arange(mnum), np.arange(2)) labels = [label for label in labels] for i,label in enumerate(labels): if len(label)==4 and label[-1] == 1: binnum,dummy = np.histogram(asmp0, aibins) probs = binnum/np.sum(binnum, dtype=float) node_ai.assign_cpt(probs,label=np.asarray(label),statenames=node_ai.statenames) else: truncrvs=[] for j,pstate in enumerate(label): truncrvs.append(node_ai.parents[j].truncate_rv(pstate,lmd=trunclmd)) rvnames = ['Ap', 'K', 'M'] rvs = truncrvs[:3] probs,smpdb = mc2ai(rvnames, rvs, node_ai.bins, acrit, nsmp=nsmp) # clean Ai states given Ai-1 apstate = label[0] aplb = node_ap.bins[apstate] aiubs = node_ai.bins[1:] probs[aiubs<=aplb] = 0. probs = probs/np.sum(probs) node_ai.assign_cpt(probs,label=np.asarray(label),statenames=node_ai.statenames) # print 'labels: {}, progress: {}%, prob: {}'.format(label, # float(i)/len(labels)*100, np.array_str(probs,precision=3)) return node_ai
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])
# beta = (50.*0.4)/(np.pi/np.sqrt(6)) # mu = 50.-np.euler_gamma*beta # h = stats.gumbel_r(loc=mu, scale=beta) urlogmean = np.log(35./np.sqrt(1+0.286**2)) urlogstd = np.sqrt(np.log(1+0.286**2)) uh = stats.lognorm(urlogstd, scale=np.exp(urlogmean)) theta = (60*0.2)**2/60. k = 60./theta v = stats.gamma(k, scale=theta) # network model # create nodes r5 = Node("R5", parents=None, rvname='lognormal', rv=rv5) r4 = Node("R4", parents=[r5], rvname='lognormal', rv=rv4) m4 = Node("M4", parents=[r4], rvname='continuous') m5 = Node("M5", parents=[r5], rvname='continuous') q = Node("Q", parents=[r4,r5], rvname='continuous') uh = Node("Uh", parents=None, rvname='lognormal', rv=uh) e0 = Node("E0", parents=None, rvname='discrete') earray = [e0] life = 10; harray=[] for i in range(life): h = Node("H"+str(i+1), parents=[uh], rvname='continuous') e = Node("E"+str(i+1),parents=[earray[-1],q,h], rvname='discrete') harray.append(h) earray.append(e) # discretize continuous rv
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])
theta = (60*0.2)**2/60. k = 60./theta v = stats.gamma(k, scale=theta) rolnR = 0.3 # prior reliability using pyStRe rvnames = ['ur', 'u1', 'u2', 'u3', 'u4', 'u5', 'h', 'v'] rvs = [ur, u1, u2, u3, u4, u5, h, v] syspf = sys_prior(rvnames, rvs, rolnR) print "Prior system failure probability is {}".format(syspf) print "Prior system reliability is {}".format(stats.norm.ppf(1-syspf)) # network model # create nodes r5 = Node("R5", parents=None, rvname='lognormal', rv=rv5) r4 = Node("R4", parents=[r5], rvname='lognormal', rv=rv4) m4 = Node("M4", parents=[r4], rvname='continuous') m5 = Node("M5", parents=[r5], rvname='continuous') e = Node("E",parents=[r4,r5], rvname='discrete') # discretize continuous rv r4num = 20+1 r5num = 20+1 m4num = 20+2 m5num = 20+2 m = r5.rv.stats('m'); s = np.sqrt(r5.rv.stats('v')) lb = 50.; ub = 250. r4bins = np.hstack((0, np.linspace(lb, ub, r4num-1))) r4names = r4.discretize(lb, ub, r4num, infinity='+', bins=r4bins) m4names = m4.discretize(lb, ub, m4num, infinity='+-', bins=r4bins)
theta = (60 * 0.2)**2 / 60. k = 60. / theta v = stats.gamma(k, scale=theta) rolnR = 0.3 # prior reliability using pyStRe rvnames = ['ur', 'u1', 'u2', 'u3', 'u4', 'u5', 'h', 'v'] rvs = [ur, u1, u2, u3, u4, u5, h, v] syspf = sys_prior(rvnames, rvs, rolnR) print "Prior system failure probability is {}".format(syspf) print "Prior system reliability is {}".format(stats.norm.ppf(1 - syspf)) # network model # create nodes r5 = Node("R5", parents=None, rvname='lognormal', rv=rv5) r4 = Node("R4", parents=[r5], rvname='lognormal', rv=rv4) m4 = Node("M4", parents=[r4], rvname='continuous') m5 = Node("M5", parents=[r5], rvname='continuous') e = Node("E", parents=[r4, r5], rvname='discrete') # discretize continuous rv r4num = 20 + 1 r5num = 20 + 1 m4num = 20 + 2 m5num = 20 + 2 m = r5.rv.stats('m') s = np.sqrt(r5.rv.stats('v')) lb = 50. ub = 250. r4bins = np.hstack((0, np.linspace(lb, ub, r4num - 1)))
from pyNetica import Node, Network from pyNetica.netica import DECISION_NODE, UTILITY_NODE from scipy import stats import numpy as np import itertools import sys from bnexample1_funcs import form2y # random variables rvX1 = stats.lognorm(1., scale=np.exp(0)) rvX3 = stats.lognorm(1., scale=np.exp(3*np.sqrt(2))) # create nodes weather = Node("Weather", parents=None, rvname='discrete') forecast = Node("Forecast", parents=[weather], rvname='discrete') umbrella = Node("Umbrella", parents=[forecast], rvname='discrete') satisfy = Node("Satisfaction", parents=[weather, umbrella], rvname='deterministic') umbrella.set_node_kind(DECISION_NODE) satisfy.set_node_kind(UTILITY_NODE) # assign CPT # node weather weathercpt = np.array([[0.7, 0.3]]) weather.assign_cpt(weathercpt, statenames=['sunshine', 'rain']) # node forecast forecastcpt = np.array([[0.7, 0.2, 0.1], [0.15, 0.25, 0.6]]) forecast.assign_cpt(forecastcpt, statenames=['sunny', 'cloudy', 'rainy']) # node umbrella umbrella.set_node_state_name(['take_umbrella', 'dont_take_umbrella']) # node satisfy
loc=mmean, scale=mstd) # testing error, normal with mean=0, std=15kNm # beta = (50.*0.4)/(np.pi/np.sqrt(6)) # mu = 50.-np.euler_gamma*beta # h = stats.gumbel_r(loc=mu, scale=beta) urlogmean = np.log(35. / np.sqrt(1 + 0.286**2)) urlogstd = np.sqrt(np.log(1 + 0.286**2)) uh = stats.lognorm(urlogstd, scale=np.exp(urlogmean)) theta = (60 * 0.2)**2 / 60. k = 60. / theta v = stats.gamma(k, scale=theta) # network model # create nodes r5 = Node("R5", parents=None, rvname='lognormal', rv=rv5) r4 = Node("R4", parents=[r5], rvname='lognormal', rv=rv4) m4 = Node("M4", parents=[r4], rvname='continuous') m5 = Node("M5", parents=[r5], rvname='continuous') q = Node("Q", parents=[r4, r5], rvname='continuous') uh = Node("Uh", parents=None, rvname='lognormal', rv=uh) e0 = Node("E0", parents=None, rvname='discrete') earray = [e0] life = 10 harray = [] for i in range(life): h = Node("H" + str(i + 1), parents=[uh], rvname='continuous') e = Node("E" + str(i + 1), parents=[earray[-1], q, h], rvname='discrete') harray.append(h)
if __name__ == '__main__': # random variables rv_a0 = stats.norm(0.5, 0.5*0.1) rv_m = stats.norm(3.0, 3.0*0.05) [logmean, logstd] = lognstats(2.3e-12, 0.3*2.3e-12) # [logmean, logstd] = lognstats(4.5e-13, 0.3*4.5e-13) rv_C = stats.lognorm(logstd, scale=np.exp(logmean)) [wblscale, wblc] = wblstats(22.5, 0.1*22.5) rv_Sre = stats.weibull_min(wblc, scale=wblscale) [logmean, logstd] = lognstats(2e6, 0.1*2e6) rv_Na = stats.lognorm(logstd, scale=np.exp(logmean)) # network model # create time-independent nodes node_m = Node("M", parents=None, rvname='normal', rv=rv_m) node_k = Node("K", parents=[node_m], rvname='continuous') node_a0 = Node('a0', parents=None, rvname='normal', rv=rv_a0) # discretize continuous rv # node a0 a0num = 20+2 mu,var = rv_a0.stats(); sigma = np.sqrt(var) lb = mu-3.*sigma; ub = mu+3.*sigma a0bins = np.linspace(lb, ub, a0num-1) a0names = node_a0.discretize(lb, ub, a0num, infinity='+-', bins=a0bins) # node m mnum = 20+2 mu,var = rv_m.stats(); sigma = np.sqrt(var) lb = mu-3.*sigma; ub = mu+3.*sigma mbins = np.linspace(lb, ub, mnum-1) mnames = node_m.discretize(lb, ub, mnum, infinity='+-', bins=mbins)
from pyNetica import Node, Network from scipy import stats import numpy as np import itertools import sys from bnexample1_funcs import form2y # random variables rvX1 = stats.lognorm(1., scale=np.exp(0)) rvX3 = stats.lognorm(1., scale=np.exp(3*np.sqrt(2))) # create nodes x1 = Node("X1", parents=None, rvname='lognormal', rv=rvX1) x2 = Node("X2", parents=[x1], rvname='continuous') y = Node("Y", parents=[x2], rvname='discrete') # discretize continuous rv x1num = 5 x2num = 5 m = x1.rv.stats('m'); s = np.sqrt(x1.rv.stats('v')) #lb = np.maximum(0, m-2*s); ub = m+2*s lb = 0.; ub = m+1.5*s x1names = x1.discretize(lb, ub, x1num, infinity='+') x2names = x2.discretize(lb*10., ub*10., x2num, infinity='+') # calculate and assign CPT # node X1 x1cpt = x1.rv.cdf(x1.bins[1:]) - x1.rv.cdf(x1.bins[:-1]) x1cpt = x1cpt[np.newaxis,:] x1.assign_cpt(x1cpt, statenames=x1names)
acrit = 30. life=5; lifearray = np.arange(life)+1. # random variables rv_a0 = stats.norm(0.5, 0.5*0.1) rv_m = stats.norm(3.0, 3.0*0.05) [logmean, logstd] = lognstats(2.3e-12, 0.3*2.3e-12) # [logmean, logstd] = lognstats(4.5e-13, 0.3*4.5e-13) rv_C = stats.lognorm(logstd, scale=np.exp(logmean)) [wblscale, wblc] = wblstats(22.5, 0.1*22.5) rv_Sre = stats.weibull_min(wblc, scale=wblscale) [logmean, logstd] = lognstats(2e6, 0.1*2e6) rv_Na = stats.lognorm(logstd, scale=np.exp(logmean)) # network model # create nodes node_m = Node("M", parents=None, rvname='normal', rv=rv_m) node_k = Node("K", parents=[node_m], rvname='continuous') node_a0 = Node('a0', parents=None, rvname='normal', rv=rv_a0) aarray = [node_a0] marray=[] for i in range(life): ai = Node("A"+str(i+1), parents=[aarray[-1], node_k, node_m], rvname='continuous') mi = Node("M"+str(i+1), parents=[ai], rvname='continuous') aarray.append(ai) marray.append(mi) # discretize continuous rv # node a0 a0num = 20+2 mu,var = rv_a0.stats(); sigma = np.sqrt(var) lb = mu-3.*sigma; ub = mu+3.*sigma
if __name__ == '__main__': # random variables rv_a0 = stats.norm(0.5, 0.5 * 0.1) rv_m = stats.norm(3.0, 3.0 * 0.05) [logmean, logstd] = lognstats(2.3e-12, 0.3 * 2.3e-12) # [logmean, logstd] = lognstats(4.5e-13, 0.3*4.5e-13) rv_C = stats.lognorm(logstd, scale=np.exp(logmean)) [wblscale, wblc] = wblstats(22.5, 0.1 * 22.5) rv_Sre = stats.weibull_min(wblc, scale=wblscale) [logmean, logstd] = lognstats(2e6, 0.1 * 2e6) rv_Na = stats.lognorm(logstd, scale=np.exp(logmean)) # network model # create time-independent nodes node_m = Node("M", parents=None, rvname='normal', rv=rv_m) node_k = Node("K", parents=[node_m], rvname='continuous') node_a0 = Node('a0', parents=None, rvname='normal', rv=rv_a0) # discretize continuous rv # node a0 a0num = 20 + 2 mu, var = rv_a0.stats() sigma = np.sqrt(var) lb = mu - 3. * sigma ub = mu + 3. * sigma a0bins = np.linspace(lb, ub, a0num - 1) a0names = node_a0.discretize(lb, ub, a0num, infinity='+-', bins=a0bins) # node m mnum = 20 + 2 mu, var = rv_m.stats() sigma = np.sqrt(var)
if __name__ == '__main__': # random variables rv_a0 = stats.norm(0.5, 0.5 * 0.1) rv_m = stats.norm(3.0, 3.0 * 0.05) [logmean, logstd] = lognstats(2.3e-12, 0.3 * 2.3e-12) # [logmean, logstd] = lognstats(4.5e-13, 0.3*4.5e-13) rv_C = stats.lognorm(logstd, scale=np.exp(logmean)) [wblscale, wblc] = wblstats(22.5, 0.1 * 22.5) rv_Sre = stats.weibull_min(wblc, scale=wblscale) [logmean, logstd] = lognstats(2e6, 0.1 * 2e6) rv_Na = stats.lognorm(logstd, scale=np.exp(logmean)) # network model # create nodes node_m = Node("M", parents=None, rvname='normal', rv=rv_m) node_k = Node("K", parents=[node_m], rvname='continuous') node_a0 = Node('a0', parents=None, rvname='normal', rv=rv_a0) aarray = [node_a0] marray = [] for i in range(life): ai = Node("A" + str(i + 1), parents=[aarray[-1], node_k, node_m], rvname='continuous') mi = Node("M" + str(i + 1), parents=[ai], rvname='continuous') aarray.append(ai) marray.append(mi) # discretize continuous rv # node a0 a0num = 20 + 2
from pyNetica import Node, Network from pyNetica.netica import DECISION_NODE, UTILITY_NODE from scipy import stats import numpy as np import itertools import sys from bnexample1_funcs import form2y # random variables rvX1 = stats.lognorm(1., scale=np.exp(0)) rvX3 = stats.lognorm(1., scale=np.exp(3 * np.sqrt(2))) # create nodes weather = Node("Weather", parents=None, rvname='discrete') forecast = Node("Forecast", parents=[weather], rvname='discrete') umbrella = Node("Umbrella", parents=[forecast], rvname='discrete') satisfy = Node("Satisfaction", parents=[weather, umbrella], rvname='deterministic') umbrella.set_node_kind(DECISION_NODE) satisfy.set_node_kind(UTILITY_NODE) # assign CPT # node weather weathercpt = np.array([[0.7, 0.3]]) weather.assign_cpt(weathercpt, statenames=['sunshine', 'rain']) # node forecast forecastcpt = np.array([[0.7, 0.2, 0.1], [0.15, 0.25, 0.6]]) forecast.assign_cpt(forecastcpt, statenames=['sunny', 'cloudy', 'rainy']) # node umbrella