def belief(self, vName): self.cleanMessages() incoming_messages = [] for fName in self.nodeVariables[vName].getNodeFactorNames(): incoming_messages.append(self.msgFactorVariable(fName, vName)) joined = Factor.joint(incoming_messages) #normalized =joined.normalize() return joined
def buildMaxFactorVariable(self, fName, vName): incoming_messages = [self.nodeFactors[fName].getFactor()] marginalization_variables = [] for vNameMeighbor in self.nodeFactors[fName].getVariableNames(): if vNameMeighbor != vName: msg = self.msgMaxVariableFactor(vNameMeighbor, fName) incoming_messages.append(msg) marginalization_variables.append(vNameMeighbor) joined = Factor.joint(incoming_messages) maximized = joined.maximize(marginalization_variables) #normalized =maximized.normalize() return maximized
def buildVariableFactor(self, vName, fName): incoming_messages = [] for fNameNeighbor in self.nodeVariables[vName].getNodeFactorNames(): if fNameNeighbor != fName: msg = self.msgFactorVariable(fNameNeighbor, vName) incoming_messages.append(msg) if not incoming_messages: incoming_messages.append( Factor([vName], np.array([1.] * self.nodeVariables[vName].getRank()))) joined = Factor.joint(incoming_messages) #normalized =joined.normalize() return joined
def buildFactorVariable(self, fName, vName): incoming_messages = [self.nodeFactors[fName].getFactor()] marginalization_variables = [] for vNameMeighbor in self.nodeFactors[fName].getVariableNames(): if vNameMeighbor != vName: msg = self.msgVariableFactor(vNameMeighbor, fName) incoming_messages.append(msg) marginalization_variables.append(vNameMeighbor) joined = Factor.joint(incoming_messages) #This doesn't seem to work for evicences :-( reduced = self.reduce(joined, marginalization_variables + [vName]) marginalized = reduced.marginalize(marginalization_variables) #normalized =marginalized.normalize() return marginalized
variables = list(fgDiag.nodeVariables.keys()) variables.remove("cold") variables.remove("smokes") factors = [ psmokes, plungSmokes, pcold, pbreathLung, pchestLung, pcoughLungCold, pfeverCold ] while variables: #len(factors)>1: print("variables=", variables) #variable=chooseVariable(factors) variable = chooseRandom(variables) print("variable=", variable) fv = factorsVariable(factors, variable) print("fv=", len(fv)) fj = Factor.joint(fv) print("fj =", fj.getVariables()) fj = fj.marginalize(variable) print("fjm=", fj.getVariables()) variables.remove(variable) for f in fv: factors.remove(f) factors.append(fj) print("factors=", len(factors)) for f in factors: print(f.getVariables()) jointAll = Factor.joint(factors) p = jointAll.getDistribution() print("p=", p) print(jointAll.getVariables())