Skip to content
Snippets Groups Projects
Commit 68fdfdd3 authored by mbaumBielefeld's avatar mbaumBielefeld
Browse files

Added functionality for ProbabilityDensity and did some refactoring

parent eaa7563f
No related branches found
No related tags found
No related merge requests found
from core.BayesNet import *
from reasoning.DiscreteNode import DiscreteNode
import numpy
bn = BayesNet()
burglary = DiscreteNode("Burglary", ["Intruder","Safe"])
......@@ -26,5 +27,10 @@ burglary.set_probability(0.2,[(burglary,"Intruder")])
alarm.set_probability(0.1,[(alarm,"Ringing"),(burglary,"Safe"),(earthquake,"Calm")])
cpt = numpy.array([[0.1,0.9],[0.5,0.5],[0.4,0.6]])
john_calls.set_probability_table(cpt, [alarm, john_calls])
print john_calls.is_valid()
print alarm.is_valid()
bn.draw()
from CPD import CPD
from CPT import CPT
from Gauss import Gauss
# -*- coding: utf-8 -*-
from reasoning.RandomNode import RandomNode
from reasoning.CPD.CPT import CPT
from reasoning.density.ProbabilityTable import ProbabilityTable
class DiscreteNode(RandomNode):
'''TODO: write doc'''
......@@ -10,13 +10,20 @@ class DiscreteNode(RandomNode):
super(DiscreteNode, self).__init__(name)
self.value_range = value_range
self.cpd = CPT(self)
self.cpd = ProbabilityTable()
self.cpd.add_variable(self)
def announce_parent(self, node):
self.cpd.add_parent(node)
self.cpd.add_variable(node)
def __str__(self):
return self.name + "\n" + str(self.cpd)
def set_probability(self, value, node_value_pairs):
self.cpd.set_probability(value, node_value_pairs)
def set_probability_table(self, table, nodes):
self.cpd.set_probability_table(table, nodes)
def is_valid(self):
return self.cpd.is_normalized_as_cpt(self)
# -*- coding: utf-8 -*-
from core.Node import Node
from reasoning.CPD import CPD
from reasoning.density import Density
class RandomNode(Node):
'''TODO: write doc'''
cpd = CPD()
cpd = Density()
def __init__(self, name):
super(RandomNode, self).__init__(name)
def is_valid(self):
raise Exception("Called an unimplemented function")
# -*- coding: utf-8 -*-
class CPD(object):
class Density(object):
'''TODO: write doc'''
def __init__(self):
super(CPD, self).__init__()
super(Density, self).__init__()
# -*- coding: utf-8 -*-
from reasoning.CPD import CPD
from reasoning.density import Density
class Gauss(CPD):
class Gauss(Density):
'''TODO: write doc'''
def __init__(self):
......
# -*- coding: utf-8 -*-
import numpy
from reasoning.CPD import CPD
from reasoning.density import Density
class CPT(CPD):
class ProbabilityTable(Density):
'''TODO: write doc'''
def __init__(self, owner):
super(CPT, self).__init__()
def __init__(self):
super(ProbabilityTable, self).__init__()
self.owner = owner
self.variables = [owner]
#self.owner = owner
#self.variables = [owner]
size_of_range = len(owner.value_range)
self.values = numpy.ones(size_of_range) / size_of_range
#size_of_range = len(owner.value_range)
#self.table = numpy.ones(size_of_range) / size_of_range
def add_parent(self, parent):
self.variables.append(parent)
self.variables = []
self.table = numpy.array(0)
ax = self.values.ndim
self.values=numpy.expand_dims(self.values,ax)
self.values=numpy.repeat(self.values,len(parent.value_range),axis = ax)
def add_variable(self, variable):
self.variables.append(variable)
ax = self.table.ndim
self.table=numpy.expand_dims(self.table,ax)
self.table=numpy.repeat(self.table,len(variable.value_range),axis = ax)
def set_probability_table(self, table, nodes):
if not set(nodes) == set(self.variables):
raise Exception("The list which should define the ordering of the variables does not match"
" the variables that this cpt depends on (plus the node itself)")
if not self.table.ndim == table.ndim:
raise Exception("The provided probability table does not have the right number of dimensions")
for d,node in enumerate(nodes):
if len(node.value_range) != table.shape[d]:
raise Exception("The size of the provided probability table does not match the number of possible values of the node "+node.name+" in dimension "+str(d))
self.table = table
self.variables = nodes
def set_probability(self, value, node_value_pairs):
index = self.get_cpt_index(node_value_pairs)
self.values[tuple(index)]=value
self.table[tuple(index)]=value
def get_cpt_index(self, node_value_pairs):
nodes, values = zip(*node_value_pairs)
......@@ -35,15 +51,24 @@ class CPT(CPD):
value = values[index_in_values_list]
index.append(node.value_range.index(value))
return index
def is_normalized_as_cpt(self,owner):
dim_of_owner = self.variables.index(owner)
sum_of_owner_probs = numpy.sum(self.table, dim_of_owner)
return set(sum_of_owner_probs.flatten()) == set([1])
def is_normalized_as_jpt(self):
return numpy.sum(table) == 1.0
def multiplication(self, factor):
raise Exception("Called unimplemented function")
def marginalization(self, value_name):
raise Exception("Called unimplemented function")
def marginalization(self, variable):
raise Exception("Called unimplemented function")
def reduction(self):
raise Exception("Called unimplemented function")
......@@ -51,4 +76,4 @@ class CPT(CPD):
raise Exception("Called unimplemented function")
def __str__(self):
return str(self.values)
return str(self.table)
from Density import Density
from ProbabilityTable import ProbabilityTable
from Gauss import Gauss
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment