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
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