Ejemplo n.º 1
0
sun.set_density_parameters(BetaParameters(2, {}, 2, {}))

ground.set_density_parameters(GaussParameters(2.0, {}, 1.5))

growth.set_density_parameters(GaussParameters(0.1, {age: 5.0, ground: 1.0, sun: 4.0}, 2.5))

height.set_density_parameters(BetaParameters(0.1, {growth: 1}, 0.5, {growth: 0.5}))

diameter.set_density_parameters(ExponentialParameters(0.01, {growth: 0.2}))

children.set_density_parameters(ExponentialParameters(0.1, {ground: 1.0, height: 1.0}))


mcmc_ask = MCMC(bn, 1000)

evidence = {age: EvEqual(2)}


print "PosteriorMarginal:"
pm = mcmc_ask.calculate_PosteriorMarginal([age, height], evidence, NDGauss)
# pm=mcmc_ask.calculate_PosteriorMarginal([height],evidence,Gauss)
print pm

print "PriorMarginal:"
pm = mcmc_ask.calculate_PriorMarginal([age], NDGauss)
print pm
# pm=mcmc_ask.calculate_PriorMarginal([height,diameter],Gauss)
pm = mcmc_ask.calculate_PriorMarginal([height], NDGauss)
print pm
Ejemplo n.º 2
0
mcmc_ask=MCMC(bn,1000,convergence_test=ConvergenceTestSimpleCounting(500))


print "------PriorMarginal:------"


pm=mcmc_ask.calculate_PriorMarginal([age],NDGauss)
print pm
print "Ground truth: mu=0.5 C=[0.25]"
pm=mcmc_ask.calculate_PriorMarginal([height],NDGauss)
print pm
print ""


print "------PosteriorMarginal:------"
pm=mcmc_ask.calculate_PosteriorMarginal([age,height],{age:EvEqual(2)},NDGauss)
print "P(age,height|age=2):"
print pm
print "Ground truth: age=2, height=mu:1.9,C=0.3"
print ""

pm=mcmc_ask.calculate_PosteriorMarginal([age,height],{age:EvLower(0.1)},NDGauss)
print "P(age,height|age<0.1):"
print pm
print "Ground truth: age=0:0.1, height=mu:-0.1:0.0,C=0.3"
print ""

print "------PropabilityOfEvidence------"
poe=mcmc_ask.calculate_PoE({age:EvLower(0.347)})
print "Probabilty that age is lower than it's median:"
print "p(age<0.347)="+str(poe)
Ejemplo n.º 3
0
        ground: 1.0,
        sun: 4.0
    }, 2.5))

height.set_density_parameters(
    BetaParameters(0.1, {growth: 1}, 0.5, {growth: 0.5}))

diameter.set_density_parameters(ExponentialParameters(0.01, {growth: 0.2}))

children.set_density_parameters(
    ExponentialParameters(0.1, {
        ground: 1.0,
        height: 1.0
    }))

mcmc_ask = MCMC(bn, 1000)

evidence = {age: EvEqual(2)}

print "PosteriorMarginal:"
pm = mcmc_ask.calculate_PosteriorMarginal([age, height], evidence, NDGauss)
#pm=mcmc_ask.calculate_PosteriorMarginal([height],evidence,Gauss)
print pm

print "PriorMarginal:"
pm = mcmc_ask.calculate_PriorMarginal([age], NDGauss)
print pm
#pm=mcmc_ask.calculate_PriorMarginal([height,diameter],Gauss)
pm = mcmc_ask.calculate_PriorMarginal([height], NDGauss)
print pm
Ejemplo n.º 4
0
alarm_cpt = numpy.array([[0.8, 0.15, 0.05], [0.05, 0.9, 0.05]])
alarm.set_probability_table(alarm_cpt, [burglary, alarm])

#Get some inference object
mcmc_ask = MCMC(bn, 5000, transition_model=GibbsTransitionModel())

#Do some Inferences
evidence = {burglary: EvEq("Intruder")}

print "-------ProbabilityOfEvidence:-------"
poe = mcmc_ask.calculate_PoE(evidence)
print "p(evidence=Intruder)=" + str(poe)
print "Ground truth=0.2\n"

print "-------PosteriorMarginal:-------"
pm = mcmc_ask.calculate_PosteriorMarginal([alarm], evidence, ProbabilityTable)
print "P(alarm|burglary=Intruder)=" + str(pm)
print "Ground truth=[0.8, 0.15, 0.05]\n"

print "-------PriorMarginal:-------"
pm = mcmc_ask.calculate_PriorMarginal([alarm], ProbabilityTable)
print "P(Alarm)= " + str(pm)
print "Ground truth=[0.2, 0.75, 0.05]\n"

pm = mcmc_ask.calculate_PriorMarginal([burglary], ProbabilityTable)
print "P(Burglary)= " + str(pm)
print "Ground truth=[0.2, 0.8]\n"

print "-------MAP:-------"
hyp = mcmc_ask.calculate_MAP([alarm], evidence, ProbabilityTable)
print "MAP(alarm|burglary=intruder)=" + str(hyp)
Ejemplo n.º 5
0
print "Prior Marginal:"

print "AlarmFT: " + str(mcmc_ask.calculate_PriorMarginal([alarm],ProbabilityTable))
print "John_CallsFT: " + str(mcmc_ask.calculate_PriorMarginal([john_calls],ProbabilityTable))
print "Baum_CallsFT: " + str(mcmc_ask.calculate_PriorMarginal([baum_calls],ProbabilityTable))
print "BurglaryFT: " + str(mcmc_ask.calculate_PriorMarginal([burglary],ProbabilityTable))
print "EarthquakeFT: " + str(mcmc_ask.calculate_PriorMarginal([earthquake],ProbabilityTable))

evidences = {alarm: EvEq("Ringing"),earthquake: EvEq("Calm")}

print "PoE: " + str(mcmc_ask.calculate_PoE(evidences))

print "Posterior Marginal (alarm->ringing , earthquake->calm):"

print "AlarmFT: " + str(mcmc_ask.calculate_PosteriorMarginal([alarm],evidences,ProbabilityTable))
print "John_CallsFT: " + str(mcmc_ask.calculate_PosteriorMarginal([john_calls],evidences,ProbabilityTable))
print "Baum_CallsFT: " + str(mcmc_ask.calculate_PosteriorMarginal([baum_calls],evidences,ProbabilityTable))
print "BurglaryFT: " + str(mcmc_ask.calculate_PosteriorMarginal([burglary],evidences,ProbabilityTable))
print "EarthquakeFT: " + str(mcmc_ask.calculate_PosteriorMarginal([earthquake],evidences,ProbabilityTable))




#evidence={burglary:"Intruder"}


#print "ProbabilityOfEvidence: " 
#poe=mcmc_ask.calculate_PoE(evidence)
#print poe
Ejemplo n.º 6
0
print "Prior Marginal:"

print "AlarmFT: " + str(mcmc_ask.calculate_PriorMarginal([alarm],ProbabilityTable))
print "John_CallsFT: " + str(mcmc_ask.calculate_PriorMarginal([john_calls],ProbabilityTable))
print "Baum_CallsFT: " + str(mcmc_ask.calculate_PriorMarginal([baum_calls],ProbabilityTable))
print "BurglaryFT: " + str(mcmc_ask.calculate_PriorMarginal([burglary],ProbabilityTable))
print "EarthquakeFT: " + str(mcmc_ask.calculate_PriorMarginal([earthquake],ProbabilityTable))

evidences = {alarm: EvEq("Ringing"),earthquake: EvEq("Calm")}

print "PoE: " + str(mcmc_ask.calculate_PoE(evidences))

print "Posterior Marginal (alarm->ringing , earthquake->calm):"

print "AlarmFT: " + str(mcmc_ask.calculate_PosteriorMarginal([alarm],evidences,ProbabilityTable))
print "John_CallsFT: " + str(mcmc_ask.calculate_PosteriorMarginal([john_calls],evidences,ProbabilityTable))
print "Baum_CallsFT: " + str(mcmc_ask.calculate_PosteriorMarginal([baum_calls],evidences,ProbabilityTable))
print "BurglaryFT: " + str(mcmc_ask.calculate_PosteriorMarginal([burglary],evidences,ProbabilityTable))
print "EarthquakeFT: " + str(mcmc_ask.calculate_PosteriorMarginal([earthquake],evidences,ProbabilityTable))




#evidence={burglary:"Intruder"}


#print "ProbabilityOfEvidence: " 
#poe=mcmc_ask.calculate_PoE(evidence)
#print poe
Ejemplo n.º 7
0
alarm.set_probability_table(alarm_cpt, [burglary,alarm])


#Get some inference object
mcmc_ask=MCMC(bn,5000,transition_model=GibbsTransitionModel())

#Do some Inferences
evidence={burglary:EvEq("Intruder")}

print "-------ProbabilityOfEvidence:-------" 
poe=mcmc_ask.calculate_PoE(evidence)
print "p(evidence=Intruder)="+str(poe)
print "Ground truth=0.2\n"

print "-------PosteriorMarginal:-------"
pm=mcmc_ask.calculate_PosteriorMarginal([alarm],evidence,ProbabilityTable)
print "P(alarm|burglary=Intruder)="+str(pm)
print "Ground truth=[0.8, 0.15, 0.05]\n"

print "-------PriorMarginal:-------"
pm=mcmc_ask.calculate_PriorMarginal([alarm],ProbabilityTable)
print "P(Alarm)= " + str(pm)
print "Ground truth=[0.2, 0.75, 0.05]\n"

pm=mcmc_ask.calculate_PriorMarginal([burglary],ProbabilityTable)
print "P(Burglary)= " + str(pm)
print "Ground truth=[0.2, 0.8]\n"

print "-------MAP:-------"
hyp=mcmc_ask.calculate_MAP([alarm],evidence,ProbabilityTable)
print "MAP(alarm|burglary=intruder)=" + str(hyp)