Esempio n. 1
0
def cancer_example():
    '''
    Cancer example
    '''
    C = Factor(name='Cancer', values=["no", "yes"],
                cpd=[0.999, 0.001])
    T = Factor(name='Test', values=["pos", "neg"],
                cond=[C], cpd=[0.2, 0.8, 0.9, 0.1])
##    print C
##    print T
##    print T.joint()
##    print T.uncond()
##    R = T.query(query=[C], evidence={T:'pos'})
    print "First algorithm"
    print T._query1(query=[C], evidence=[T])
    print "Second algorithm"
    print T._query2(query=[C], evidence=[T])
Esempio n. 2
0
def local_inference():
    A = Factor(name='A', values=[0, 1], cpd=[0.2, 0.8])
    B = Factor(name='B', values=[0, 1], cond=[A], cpd=[0.6, 0.4, 0.3, 0.7])
    C = Factor(name='C', values=[0, 1], cond=[B], cpd=[0.6, 0.4, 0.3, 0.7])
    D = Factor(name='D', values=[0, 1], cond=[C], cpd=[0.6, 0.4, 0.3, 0.7])
    E = Factor(name='E', values=[0, 1], cond=[D], cpd=[0.6, 0.4, 0.3, 0.7])
    F = Factor(name='F', values=[0, 1], cond=[E], cpd=[0.6, 0.4, 0.3, 0.7])
    G = Factor(name='G', values=[0, 1], cond=[F], cpd=[0.6, 0.4, 0.3, 0.7])

##    print A.uncond()
##    print B.uncond()
##    print C.uncond()
##    print D.uncond()
##    print E.uncond()
##    print F.uncond()

##    print B.uncond()
##    print B.query(query=[B], evidence=[])
##    print (B*A).marginal(var=A.cons)

##    print C.uncond()
##    print C.query(query=[C], evidence=[])

##    print C.query(query=[C], evidence=[B])
##    print C

    print C._query2(query=[C], evidence=[A, B])
##    print C.uncond()
##    print (C*B*A).marginal(A.cons).marginal(B.cons)
##    print (C*D).marginal(var=D.cons)
##    print C.query(query=[C], evidence=[A, B])


    A = Factor(name='A', values=[0, 1], cpd=[0.01, 0.99])
    B = Factor(name='B', values=[0, 1], cond=[A], cpd=[0.96, 0.04, 0.93, 0.07])
    C = Factor(name='C', values=[0, 1], cond=[B], cpd=[0.96, 0.04, 0.93, 0.07])
    D = Factor(name='D', values=[0, 1], cond=[C], cpd=[0.96, 0.04, 0.93, 0.07])
    E = Factor(name='E', values=[0, 1], cond=[D], cpd=[0.96, 0.04, 0.93, 0.07])
    F = Factor(name='F', values=[0, 1], cond=[E], cpd=[0.96, 0.04, 0.93, 0.07])
    G = Factor(name='G', values=[0, 1], cond=[F], cpd=[0.96, 0.04, 0.93, 0.07])