def variable_elimination(model, query, evidence): global w_X topo = bayesnet.topo_ordered(model) tbn = [m[n] for n in topo] roots = bayesnet.roots(model) w_X = {str(x):[0,0] for x in query} for q in query: print q Z = [n for n in tbn if not n.name in evidence and not n.name == q] print Z if x in roots: phi = [str(n) for n in tbn if str(n) != x] phi.insert(0,x) result = p(phi, x) return result, {x:w_X[x][1]/result for x in w_X if x in roots}
def init(X, evidence, model): t0 = time.time() # global types global N, Y, MB, O, iteration, M, tobn, nonev, mnonev, rnodes, roots, observables, E, M, enodes N = { str(x): [0, 0] for x in X } Y = { str(x): [] for x in X } enodes = [model[e] for e in evidence] M = model EC = [] #init important sets tobn = bn.topo_ordered(model) nonev = [node for node in tobn if node not in evidence] mnonev = [model[n] for n in nonev] rnodes = [model[x] for x in X] roots = [r for r in bn.roots(model)] MB = { str(x): [] for x in tobn if x not in evidence } # init net # the initial net might be improper as likelihood of evidence nodes can be 0 for e in evidence: model[e].value = evidence[e] for p in model[e].parents: if p in evidence: continue par = model[p] par.value = True if MB[p] == []: MB[p] = [model[c] for c in par.children] if p in nonev: nonev.remove(p) #if bn.likelihood(n) == 0: # print "still something wrong with initialization at node " + node for node in nonev: n = model[node] # P = [0]*2 # prod = 1 # if n.children and MB[node] == []: # MB[node] = [model[c] for c in n.children] # if n.children: # EC = [model[e] for e in n.children if e in evidence] # if EC == []: # n.random() # continue # else: # n.value = True # if bn.likelihood(n) == 0: # print n.name # n.value = False # continue # print "here" # P[0] = log(bn.likelihood(n)) # for e in EC: # if e.name == 'v': # print bn.likelihood(e) # if bn.likelihood(e) == 0: # n.value = False # break # P[0] += log(bn.likelihood(e)) # else: # n.value = False # if bn.likelihood(n) == 0: # n.value = True # continue # P[1] = log(bn.likelihood(n)) # for e in EC: # if bn.likelihood(e) == 0: # n.value = True # break # P[1] += log(bn.likelihood(e)) # else: # likelihood = exp(P[0]) / (exp(P[0]) + exp(P[1])) # if likelihood > random.random(): # n.value = True # else: # n.value = False # if random.random() > .5: # n.value = True # else: # n.value = False if n.children != [] and MB[node] == []: MB[node] = [model[c] for c in n.children] #n.random() n.value = True for node in tobn: n = model[node] if node not in evidence: flip_mb(n) else: if bn.likelihood(n) == 0: print "straaaaaange " + node print "time to init: " + str(time.time() - t0) return