示例#1
0
 def create_timeslice(self, state):
     for node_x in self.__initial_nodes:
         for node_y in state:
             if node_x.name == node_y.name:
                 cpd = ProbabilityTable()
                 cpd.add_variable(node_x)
                 node_x.set_cpd(cpd)
                 node_x.set_probability(1., [(node_x, state[node_y])])
     return self
示例#2
0
class DiscreteNode(RandomNode):
    '''#TODO: write doc'''

    def __init__(self, name, value_range):
        super(DiscreteNode, self).__init__(name)

        self.value_range = value_range
        self.cpd = ProbabilityTable()
        self.cpd.add_variable(self)

    def announce_parent(self, 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 get_cpd_reduced(self, evidence):
        return self.cpd.reduction(evidence)

    def get_value_range(self):
        return self.value_range
        
    def set_cpd(self, cpd):
        self.cpd = cpd
        
    def get_cpd(self):
        return self.cpd

    def is_valid(self):
        return self.cpd.is_normalized_as_cpt(self)
示例#3
0
 def calculate_marginal(self,variables):
     ''' If evidence is set, then this methods calculates the posterior marginal.
     With an empty evidence this is automatically the prior marginal.'''
     if not self.graph.graph['messagesValid']:
         self.calculate_messages()
         
         
     resPT = ProbabilityTable.get_neutral_multiplication_PT()
     
         
     for f in self.graph.nodes():
         if f.get_node() in variables:
             resPT = resPT.multiplication(self.calculate_marginal_forOne(f))
             
     resPT = resPT.normalize_as_jpt()
             
     return resPT
示例#4
0
 def calculate_messages(self):
     ''' Calculates the messages and stores the intermediate results.'''
     self.pull_phase(self.rootNode,self.graph)
     self.push_phase(self.rootNode,self.graph,ProbabilityTable.get_neutral_multiplication_PT())
     self.graph.graph['messagesValid'] = True
示例#5
0
burglary.set_probability_table(burglary_cpt, [burglary])

alarm_cpt=numpy.array([[0.8,0.15,0.05],[0.05,0.9,0.05]])
alarm.set_probability_table(alarm_cpt, [burglary,alarm])

#Construct a Markov Chain by sampling states from this Network

transition_model = GibbsTransitionModel()

mcs = MarkovChainSampler()
initial_state={burglary:"Safe",alarm:"Silent"}
chain = mcs.generateMarkovChain(bn, 5000, transition_model, initial_state)

#for c in chain:
#    print c


pt = ProbabilityTable()
pt.add_variable(burglary)
pt.add_variable(alarm)
pt.to_jpt_by_states(chain)
print "----joint-probability----"
print pt
print "----burglary----"
print pt.marginalization(alarm)
print "----alarm----"
print pt.division(burglary.get_cpd())

bn.draw()

示例#6
0
    def __init__(self, name, value_range):
        super(DiscreteNode, self).__init__(name)

        self.value_range = value_range
        self.cpd = ProbabilityTable()
        self.cpd.add_variable(self)