Commit afc13a69 authored by Manuel Baum's avatar Manuel Baum
Browse files

Added MAP to examples/MCMC_Continuous.py

parent 4a0c97d8
......@@ -43,7 +43,7 @@ diameter_parameters=LinearBetaParameters(-10.0,{age:4.0},10.0,{age:-4.0})
diameter.set_density_parameters(diameter_parameters)
mcmc_ask=MCMC(bn,10000,convergence_test=ConvergenceTestSimpleCounting(5000))
mcmc_ask=MCMC(bn,1000,convergence_test=ConvergenceTestSimpleCounting(500))
print "------PriorMarginal:------"
......@@ -52,7 +52,6 @@ print "------PriorMarginal:------"
pm=mcmc_ask.calculate_PriorMarginal([age],Gauss)
print pm
print "Ground truth: mu=0.5 C=[0.25]"
#pm=mcmc_ask.calculate_PriorMarginal([height,diameter],Gauss)
pm=mcmc_ask.calculate_PriorMarginal([height],Gauss)
print pm
print ""
......@@ -60,14 +59,12 @@ print ""
print "------PosteriorMarginal:------"
pm=mcmc_ask.calculate_PosteriorMarginal([age,height],{age:EvEqual(2)},Gauss)
#pm=mcmc_ask.calculate_PosteriorMarginal([height],evidence,Gauss)
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)},Gauss)
#pm=mcmc_ask.calculate_PosteriorMarginal([height],evidence,Gauss)
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"
......@@ -78,6 +75,11 @@ 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)
print "Ground truth=0.5"
print ""
print "------MAP------"
map_hypothesis=mcmc_ask.calculate_MAP([height,diameter],{},Gauss)
print map_hypothesis
......@@ -7,7 +7,7 @@ from primo.reasoning.density import Gauss
from primo.reasoning import MCMC
from primo.reasoning import EvidenceEqual as EvEqual
from primo.reasoning import EvidenceIntervall as EvInterval
from primo.reasoning import EvidenceInterval as EvInterval
import numpy
......@@ -47,21 +47,17 @@ bn.add_edge(height,children)
bn.add_edge(ground,children)
#parameterization
age_parameters=LinearExponentialParameters(0.1,{})
age.set_density_parameters(age_parameters)
age.set_density_parameters(LinearExponentialParameters(0.1,{}))
sun.set_density_parameters(LinearBetaParameters(2,{},2,{}))
ground.set_density_parameters(LinearGaussParameters(2.0,{},1.5))
#age,ground,sun
growth.set_density_parameters(LinearGaussParameters(0.1,{age:5.0,ground:1.0,sun:4.0},2.5))
height_parameters=LinearBetaParameters(0.1,{growth:1},0.5,{growth:0.5})
height.set_density_parameters(height_parameters)
height.set_density_parameters(LinearBetaParameters(0.1,{growth:1},0.5,{growth:0.5}))
diameter_parameters=LinearExponentialParameters(0.01,{growth:0.2})
diameter.set_density_parameters(diameter_parameters)
diameter.set_density_parameters(LinearExponentialParameters(0.01,{growth:0.2}))
children.set_density_parameters(LinearExponentialParameters(0.1,{ground:1.0,height:1.0}))
......@@ -71,10 +67,6 @@ mcmc_ask=MCMC(bn,1000)
evidence={age:EvEqual(2)}
#print "ProbabilityOfEvidence: "
#poe=mcmc_ask.calculate_PoE(evidence)
#print poe
print "PosteriorMarginal:"
pm=mcmc_ask.calculate_PosteriorMarginal([age,height],evidence,Gauss)
#pm=mcmc_ask.calculate_PosteriorMarginal([height],evidence,Gauss)
......
......@@ -60,6 +60,9 @@ class Gauss(Density):
self.C=numpy.cov(X.transpose())
return self
def get_most_probable_instantiation(self):
return self.mu
def __str__(self):
ret= "Gauss(\nmu="+str(self.mu)+"\nC="+str(self.C)+")"
return ret
......@@ -40,22 +40,10 @@ class LinearBeta(Density):
def get_probability(self,value, node_value_pairs):
#print "Probability to compute for value:"+str(value)
#Compute the offset for the density and displace the value accordingly
#p = self.p0
#q = self.q0
#for node,node_value in node_value_pairs:
# p = p + self.p[node]*node_value
# q = q + self.q[node]*node_value
#p=1.0/(1.0+math.exp(-p))
#q=1.0/(1.0+math.exp(-q))
p=self._compute_p_given_parents(dict(node_value_pairs))
q=self._compute_q_given_parents(dict(node_value_pairs))
#print node_value_pairs
#print "beta "+str(p)+" "+str(q)
probability = beta(p, q).pdf(value)
#print "/beta"
return probability
def _compute_p_given_parents(self, state):
......@@ -76,16 +64,7 @@ class LinearBeta(Density):
def sample_global(self, state, lower_limit, upper_limit):
p=self._compute_p_given_parents(state)
q=self._compute_q_given_parents(state)
#value=random.betavariate(p,q)
#print "Sampled:"+str(value)
#return value
#_lambda=self._compute_lambda_given_parents(state)
distribution=beta(p,q)
lower_cdf=distribution.cdf(lower_limit)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment