Exemplo n.º 1
0
def rejection_sampling(X, evidence, model):
    global w_X, P, E, s, iteration

    topo = bn.topo_ordered(model)
    tobn = [model[n] for n in topo]
    w_X = {str(x):[0,0] for x in X}
    P = { str(x): [] for x in X }
    E = {'E': [] }
    s = 0
    iteration = 0
    while True:
        iteration +=1
        prior_sample(tobn)
        if evidence == {n.name:n.value for n in tobn if n.name in evidence}:
            s += 1
            for x in X:
                if model[x].value == True:
                    w_X[x][0] += 1
                else:
                    w_X[x][1] += 1

                if (w_X[x][0] + w_X[x][1]) > 0:
                    P[x].append(w_X[x][0] / s)
                else:
                    P[x].append(0)
        E['E'].append(s/iteration)  
Exemplo n.º 2
0
def plot_net(model):
    G = pgv.AGraph(strict=False,directed=True)
    tobn = bn.topo_ordered(model)
    G.add_nodes_from(tobn)
    for n in tobn:
        for c in model[n].children:
            G.add_edge(n, c)
    G.layout('dot')
    G.draw('outputs/net_plot.png')
Exemplo n.º 3
0
def variable_elimination(m, Q, E):
    global w_X
    w_X = {x:0 for x in Q}
    Q =  [m[q] for q in Q]
    topo = bayesnet.topo_ordered(m)
    Phi =  [m[n] for n in topo]
    for n in Phi:
        if n.name in E:
            n.value = E[n.name]
    for q in Q:
        Z = [p for p in Phi if not p.name in E and not p.name == q.name]
        pe = ve(Phi, q, E, Z, m)
    return pe, w_X
Exemplo n.º 4
0
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}
Exemplo n.º 5
0
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