Skip to content
Snippets Groups Projects
Commit 5304ef9a authored by Denis John PC's avatar Denis John PC
Browse files

Easy Factorization done

parent d98b3430
No related branches found
No related tags found
No related merge requests found
......@@ -16,20 +16,20 @@ baum_calls = DiscreteNode("Baum calls", ["Calling", "Not Calling"])
bn.add_node(burglary)
bn.add_node(alarm)
bn.add_node(earthquake)
bn.add_node(john_calls)
bn.add_node(baum_calls)
#bn.add_node(john_calls)
#bn.add_node(baum_calls)
bn.add_edge(burglary,alarm)
bn.add_edge(earthquake, alarm)
bn.add_edge(alarm, john_calls)
bn.add_edge(alarm, baum_calls)
#bn.add_edge(alarm, john_calls)
#bn.add_edge(alarm, baum_calls)
cpt_burglary = numpy.array([0.002,0.998])
cpt_burglary = numpy.array([0.001,0.999])
burglary.set_probability_table(cpt_burglary,[burglary])
cpt_earthquake = numpy.array([0.001,0.999])
cpt_earthquake = numpy.array([0.002,0.998])
earthquake.set_probability_table(cpt_earthquake,[earthquake])
alarm.set_probability(0.95,[(alarm,"Ringing"),(burglary,"Intruder"),(earthquake,"Shaking")])
......@@ -52,7 +52,6 @@ john_calls.set_probability(0.01,[(alarm,"Silent"),(john_calls,"Calling")])
john_calls.set_probability(0.99,[(alarm,"Silent"),(john_calls,"Not Calling")])
#first Elimination:
fe = EasiestFactorElimination()
fe.set_BayesNet(bn)
......@@ -65,3 +64,7 @@ fe.set_BayesNet(bn)
#print "PoE Earthquake: " + str(fe.calculate_PoE([(earthquake, "Calm")]))
#print "PoE BaumCalls is Calling: " + str(fe.calculate_PoE([(baum_calls, "Calling")]))
print "Posterior of earthquake : " + str(fe.calculate_PosteriorMarginal([burglary],[(alarm, "Ringing"),(earthquake, "Calm")]))
......@@ -82,7 +82,7 @@ class ProbabilityTable(Density):
def normalize_as_jpt(self):
'''This method normalizes this ProbabilityTable so it represents a valid joint probability table'''
self.table=self.table*1.0/numpy.sum(self.table)
return self.table*1.0/numpy.sum(self.table)
def multiplication(self, inputFactor):
'''This method returns a unified ProbabilityTable which contains the variables of both; the inputFactor
......
......@@ -37,26 +37,41 @@ class EasiestFactorElimination(object):
# Dann PoE berechnen und damit normalisieren
nodes = self.bn.get_all_nodes()
unzipped_list = zip(*evidence)
ev_list = zip(*evidence)
# Special Case: First Node
node1 = nodes.pop()
if node1 in unzipped_list[0]:
ind = unzipped_list[0].index(node1)
if node1 in ev_list[0]:
ind = ev_list[0].index(node1)
finCpd = node1.get_cpd().set_evidence(evidence[ind])
else:
finCpd = node1.get_cpd()
# For all other nodes
for n in nodes:
if n in unzipped_list[0]:
ind = unzipped_list[0].index(n)
if n in ev_list[0]:
ind = ev_list[0].index(n)
nCPD = n.get_cpd().set_evidence(evidence[ind])
finCpd = finCpd.multiplication(nCPD)
finCpd = finCpd.multiplication(nCPD)
else:
finCpd = finCpd.multiplication(n.get_cpd())
for v in finCpd.get_variables()[:]:
finCpd = finCpd.marginalization(v)
if v not in variables:
finCpd = finCpd.marginalization(v)
finCpd = finCpd.normalize_as_jpt()
#unityMarg = finCpd
#for v in finCpd.get_variables()[:]:
# finCpd = finCpd.marginalization(v)
#unityMarg /= finCpd
return finCpd
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment