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

Enhanced examples/MCMC_Gauss.py

parent 3d44e9fc
......@@ -13,22 +13,28 @@ import numpy
bn = BayesNet()
age = LinearGaussNode("Plant_age")
height = LinearGaussNode("Plant_height")
diameter = LinearGaussNode("Plant_diameter")
bn.add_node(age)
bn.add_node(height)
bn.add_node(diameter)
bn.add_edge(age,height)
bn.add_edge(age,diameter)
#parameterization
age_b0=4.0
age_var=2.0
age_b={}
age.set_density_parameters(age_b0,age_b,age_var)
#age.set_density_parameters(age_b0,age_variance)
height_b0=2.0
height_var=1.5
height_b={age:0.5}
height.set_density_parameters(height_b0, height_b, height_var)
#height.set_density_parameters(height_b0, height_variance)
diameter_b0=0.1
diameter_var=0.1
diameter_b={age:-0.2}
diameter.set_density_parameters(diameter_b0, diameter_b, diameter_var)
mcmc_ask=MCMC(bn)
......@@ -41,7 +47,11 @@ evidence={age:2}
#print poe
print "PosteriorMarginal:"
pm=mcmc_ask.calculate_PosteriorMarginal([height],evidence,Gauss)
pm=mcmc_ask.calculate_PosteriorMarginal([height,diameter],evidence,Gauss)
print pm
print "PriorMarginal:"
pm=mcmc_ask.calculate_PriorMarginal([height,diameter],Gauss)
print pm
#print "PriorMarginal:"
......
......@@ -12,8 +12,7 @@ class Gauss(Density):
self.mu=numpy.array([0.0])
self.C=numpy.array([[1.0]])
self.variables=[]
print self.mu
print self.C
def add_variable(self, variable):
if( not variable.get_value_range() == (-float("Inf"),float("Inf"))):
......@@ -47,11 +46,14 @@ class Gauss(Density):
for j,variable in enumerate(self.variables):
X[i,j]=state[variable]
self.mu=numpy.mean(X)
self.mu=numpy.mean(X,axis=0)
self.C=numpy.cov(X.transpose())
print X
print "--mu--"
print self.mu
print "--C--"
print self.C
return self
def __str__(self):
ret= "Gauss:\n"
ret=ret+ "--mu--\n"
ret=ret+ str(self.mu)+"\n"
ret=ret+ "--C--\n"
ret=ret+ str(self.C)+"\n"
return ret
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