def test_query_multiple_variable(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(['Q', 'J']) np_test.assert_array_almost_equal(query_result['J'].values, np.array([0.416, 0.584])) np_test.assert_array_almost_equal(query_result['Q'].values, np.array([0.4912, 0.5088]))
def test_query_multiple_variable_with_evidence(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(variables=['J', 'Q'], evidence={'A': 0, 'R': 0, 'G': 0, 'L': 1}) np_test.assert_array_almost_equal(query_result['J'].values, np.array([0.818182, 0.181818])) np_test.assert_array_almost_equal(query_result['Q'].values, np.array([0.772727, 0.227273]))
def test_query_multiple_variable_with_evidence(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(variables=['J', 'Q'], evidence={ 'A': 0, 'R': 0, 'G': 0, 'L': 1 }) np_test.assert_array_almost_equal(query_result['J'].values, np.array([0.818182, 0.181818])) np_test.assert_array_almost_equal(query_result['Q'].values, np.array([0.772727, 0.227273]))
def test_query_single_variable_with_evidence(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(variables=["J"], evidence={ "A": 0, "R": 1 }) self.assertEqual( query_result, DiscreteFactor(variables=["J"], cardinality=[2], values=np.array([0.072, 0.048])), )
def main(): """ Main functiont to lunch the belief propagation algorithm """ M = model() M.check_model() #Belief propagation bp = BeliefPropagation(M) bp.calibrate() print("maximal cliques are:") print(bp.get_cliques()) # first query print("computing probability of B=") query1 = bp.query(variables=list('b'), show_progress=True) print(query1) #second query print("computing probability of B|C") query2 = bp.query(variables=['b', 'c']) query2.marginalize(['c']) print(query2) #Third query print("computing joint") query3 = bp.query(['a', 'b', 'c', 'd', 'e']) query3.normalize() print(query3)
class IsingModel: def __init__(self, theta, seed=None): if seed is not None: np.random.seed(seed) # graph self.G = MarkovModel() for _, row in theta.iterrows(): # unary if row["j"]==row["k"]: self.G.add_node(str(int(row["j"]))) theta_jj = row["value"] self.G.add_factors(DiscreteFactor([str(int(row["j"]))], [2], np.exp([-theta_jj,theta_jj]))) # pairwise elif row["value"]!=0: self.G.add_edge(str(int(row["j"])), str(int(row["k"]))) theta_jk = row["value"] self.G.add_factors(DiscreteFactor([str(int(row["j"])), str(int(row["k"]))], [2, 2], np.exp([theta_jk, -theta_jk, -theta_jk, theta_jk]))) self.G.check_model() self.infer = BeliefPropagation(self.G) self.infer.calibrate() def get_moments(self): p = len(list(self.G.nodes)) factor_dict = self.infer.get_clique_beliefs() mom_matrix = np.zeros([p,p]) for clique in factor_dict: for pair in it.combinations(clique,2): moment = factor_dict[clique].marginalize(set(clique).difference(set(pair)),inplace=False) moment = moment.normalize(inplace=False) pair_int = [int(x) for x in pair] moment = moment.values[0,0]+moment.values[1,1]-moment.values[0,1]-moment.values[1,0] mom_matrix[pair_int[0],pair_int[1]] = moment mom_matrix[pair_int[1],pair_int[0]] = moment for unary in it.combinations(clique,1): unary_int = [int(x) for x in unary][0] moment = factor_dict[clique].marginalize(set(clique).difference(set(unary)),inplace=False).normalize(inplace=False).values moment = moment[1]-moment[0] mom_matrix[unary_int,unary_int] = moment return mom_matrix
def infer(self, evidence, new_evidence): evidence.update(new_evidence) new_model, additional_evidence = self.reduce_model(evidence) try: if self.inference_type == InferenceType.BeliefPropagation: inference = BeliefPropagation(new_model) elif self.inference_type == InferenceType.GibbsSampling: inference = GibbsSampling(new_model) elif self.inference_type == InferenceType.BayesianModelSampler: inference = BayesianModelSampling(new_model) except Exception as e: # for factor in new_model.factors: # print(factor) raise e self.evidence = { var: val for (var, val) in evidence.items() if "F(" not in var } self.evidence.update(additional_evidence) self.inference = inference self.scope = get_scope(new_model) return new_model
def test_max_calibrate_clique_belief(self): belief_propagation = BeliefPropagation(self.junction_tree) belief_propagation.max_calibrate() clique_belief = belief_propagation.get_clique_beliefs() phi1 = Factor(['A', 'B'], [2, 3], range(6)) phi2 = Factor(['B', 'C'], [3, 2], range(6)) phi3 = Factor(['C', 'D'], [2, 2], range(4)) b_A_B = phi1 * (phi3.maximize(['D'], inplace=False) * phi2).maximize(['C'], inplace=False) b_B_C = phi2 * (phi1.maximize(['A'], inplace=False) * phi3.maximize(['D'], inplace=False)) b_C_D = phi3 * (phi1.maximize(['A'], inplace=False) * phi2).maximize(['B'], inplace=False) np_test.assert_array_almost_equal(clique_belief[('A', 'B')].values, b_A_B.values) np_test.assert_array_almost_equal(clique_belief[('B', 'C')].values, b_B_C.values) np_test.assert_array_almost_equal(clique_belief[('C', 'D')].values, b_C_D.values)
def test_query_multiple_variable_with_evidence(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(variables=["J", "Q"], evidence={ "A": 0, "R": 0, "G": 0, "L": 1 }) self.assertEqual( query_result, DiscreteFactor( variables=["J", "Q"], cardinality=[2, 2], values=np.array([[0.003888, 0.000432], [0.000192, 0.000768]]), ), )
def test_max_calibrate_sepset_belief(self): belief_propagation = BeliefPropagation(self.junction_tree) belief_propagation.max_calibrate() sepset_belief = belief_propagation.get_sepset_beliefs() phi1 = Factor(['A', 'B'], [2, 3], range(6)) phi2 = Factor(['B', 'C'], [3, 2], range(6)) phi3 = Factor(['C', 'D'], [2, 2], range(4)) b_B = (phi1 * (phi3.maximize(['D'], inplace=False) * phi2).maximize(['C'], inplace=False)).maximize(['A'], inplace=False) b_C = (phi2 * (phi1.maximize(['A'], inplace=False) * phi3.maximize(['D'], inplace=False))).maximize(['B'], inplace=False) np_test.assert_array_almost_equal(sepset_belief[frozenset((('A', 'B'), ('B', 'C')))].values, b_B.values) np_test.assert_array_almost_equal(sepset_belief[frozenset((('B', 'C'), ('C', 'D')))].values, b_C.values)
def step(self, adjustment, episode): print('######## Ajustes ########') print(adjustment) print('######## Episódio atual ########') print(episode) bp = BeliefPropagation(self.model) replaced_episode = {k: replacer[k][v] for k, v in episode.iteritems()} upper_bound = self.state[0] + adjustment lower_bound = self.state[1] - adjustment if not (upper_bound > 1 or upper_bound < 0): state_aware = [upper_bound, lower_bound] cpds = self._tabular_cpds_to_dict(self.model) adjustments = self.fit_probabilities(cpds, adjustment) for node in self.model.get_cpds(): if node.variable != 'Consciente': node.values = self._get_cpd_values( adjustments[node.variable]) node.normalize() else: node.values = np.array(state_aware) for node in self.model.get_cpds(): print(node) else: state_aware = [self.state] print('######## Consciente ########') bp = BeliefPropagation(self.model) print( bp.query(['Consciente'], evidence=replaced_episode)['Consciente']) reward = float(input('Recompensa entre -1 e 1: ')) next_state = [] next_state.append(np.round(state_aware, 2)) next_state.extend(list(replaced_episode.values())) return next_state, reward
def test_find_MAP(): print '-' * 80 G = MarkovModel() G.add_nodes_from(['x1', 'x2', 'x3']) G.add_edges_from([('x1', 'x2'), ('x1', 'x3')]) phi = [ DiscreteFactor(['x2', 'x1'], cardinality=[2, 2], values=np.array([[1.0 / 1, 1.0 / 2], [1.0 / 3, 1.0 / 4]])), DiscreteFactor(['x3', 'x1'], cardinality=[2, 2], values=np.array([[1.0 / 1, 1.0 / 2], [1.0 / 3, 1.0 / 4]])) ] # DiscreteFactor(['x1'], cardinality=[2], # values=np.array([2,2]))] G.add_factors(*phi) print "nodes:", G.nodes() bp = BeliefPropagation(G) bp.max_calibrate() # bp.calibrate() clique_beliefs = bp.get_clique_beliefs() print clique_beliefs print clique_beliefs[('x1', 'x2')] print clique_beliefs[('x1', 'x3')] # print 'partition function should be', np.sum(clique_beliefs[('x1', 'x3')].values) phi_query = bp._query(['x1', 'x2', 'x3'], operation='maximize') # phi_query = bp._query(['x1', 'x2', 'x3'], operation='marginalize') print phi_query sleep(52)
def test_max_calibrate_clique_belief(self): belief_propagation = BeliefPropagation(self.junction_tree) belief_propagation.max_calibrate() clique_belief = belief_propagation.get_clique_beliefs() phi1 = DiscreteFactor(["A", "B"], [2, 3], range(6)) phi2 = DiscreteFactor(["B", "C"], [3, 2], range(6)) phi3 = DiscreteFactor(["C", "D"], [2, 2], range(4)) b_A_B = phi1 * (phi3.maximize(["D"], inplace=False) * phi2).maximize( ["C"], inplace=False) b_B_C = phi2 * (phi1.maximize(["A"], inplace=False) * phi3.maximize(["D"], inplace=False)) b_C_D = phi3 * (phi1.maximize(["A"], inplace=False) * phi2).maximize( ["B"], inplace=False) np_test.assert_array_almost_equal(clique_belief[("A", "B")].values, b_A_B.values) np_test.assert_array_almost_equal(clique_belief[("B", "C")].values, b_B_C.values) np_test.assert_array_almost_equal(clique_belief[("C", "D")].values, b_C_D.values)
def test_max_calibrate_sepset_belief(self): belief_propagation = BeliefPropagation(self.junction_tree) belief_propagation.max_calibrate() sepset_belief = belief_propagation.get_sepset_beliefs() phi1 = DiscreteFactor(["A", "B"], [2, 3], range(6)) phi2 = DiscreteFactor(["B", "C"], [3, 2], range(6)) phi3 = DiscreteFactor(["C", "D"], [2, 2], range(4)) b_B = (phi1 * (phi3.maximize(["D"], inplace=False) * phi2).maximize( ["C"], inplace=False)).maximize(["A"], inplace=False) b_C = (phi2 * (phi1.maximize(["A"], inplace=False) * phi3.maximize(["D"], inplace=False))).maximize( ["B"], inplace=False) np_test.assert_array_almost_equal( sepset_belief[frozenset((("A", "B"), ("B", "C")))].values, b_B.values) np_test.assert_array_almost_equal( sepset_belief[frozenset((("B", "C"), ("C", "D")))].values, b_C.values)
def map_query(self, targets, evidences, algorithm): if algorithm == "Variable Elimination": from pgmpy.inference import VariableElimination model_infer = VariableElimination(self.model_pgmpy) if algorithm == "Belief Propagation": from pgmpy.inference import BeliefPropagation model_infer = BeliefPropagation(self.model_pgmpy) if algorithm == "MPLP": from pgmpy.inference import Mplp model_infer = Mplp(self.model_pgmpy.to_markov_model()) return model_infer.map_query(variables=list(targets), evidence=evidences)
def test_max_calibrate_clique_belief(self): belief_propagation = BeliefPropagation(self.junction_tree) belief_propagation.max_calibrate() clique_belief = belief_propagation.get_clique_beliefs() phi1 = Factor(['A', 'B'], [2, 3], range(6)) phi2 = Factor(['B', 'C'], [3, 2], range(6)) phi3 = Factor(['C', 'D'], [2, 2], range(4)) b_A_B = phi1 * (phi3.maximize(['D'], inplace=False) * phi2).maximize( ['C'], inplace=False) b_B_C = phi2 * (phi1.maximize(['A'], inplace=False) * phi3.maximize(['D'], inplace=False)) b_C_D = phi3 * (phi1.maximize(['A'], inplace=False) * phi2).maximize( ['B'], inplace=False) np_test.assert_array_almost_equal(clique_belief[('A', 'B')].values, b_A_B.values) np_test.assert_array_almost_equal(clique_belief[('B', 'C')].values, b_B_C.values) np_test.assert_array_almost_equal(clique_belief[('C', 'D')].values, b_C_D.values)
def test_max_calibrate_sepset_belief(self): belief_propagation = BeliefPropagation(self.junction_tree) belief_propagation.max_calibrate() sepset_belief = belief_propagation.get_sepset_beliefs() phi1 = Factor(['A', 'B'], [2, 3], range(6)) phi2 = Factor(['B', 'C'], [3, 2], range(6)) phi3 = Factor(['C', 'D'], [2, 2], range(4)) b_B = (phi1 * (phi3.maximize(['D'], inplace=False) * phi2).maximize( ['C'], inplace=False)).maximize(['A'], inplace=False) b_C = (phi2 * (phi1.maximize(['A'], inplace=False) * phi3.maximize(['D'], inplace=False))).maximize( ['B'], inplace=False) np_test.assert_array_almost_equal( sepset_belief[frozenset((('A', 'B'), ('B', 'C')))].values, b_B.values) np_test.assert_array_almost_equal( sepset_belief[frozenset((('B', 'C'), ('C', 'D')))].values, b_C.values)
def _pure_spe_finder(self) -> List[defaultdict]: """this finds all pure strategy subgame perfect NE when the strategic relevance graph is acyclic - first initialises the maid with uniform random conditional probability distributions at every decision. - then fills up a queue with trees containing each solution - the queue will contain only one entry (tree) if there's only one pure strategy subgame perfect NE""" for dec in self.all_decision_nodes: self.impute_random_decision( dec) # impute random fully mixed policy to all decision nodes. bp = BeliefPropagation(self) print(type(bp)) queue = self._instantiate_initial_tree() while not self._stopping_condition(queue): queue = self._reduce_tree_once(queue, bp) return queue
def find_MAP_state(G): ''' Inputs: - G: MarkovModel ''' bp = BeliefPropagation(G) bp.max_calibrate() clique_beliefs = bp.get_clique_beliefs() phi_query = bp._query(G.nodes(), operation='maximize') print phi_query return phi_query
def _get_ev(self, dec_instantiation: Tuple[int], row: int, bp: BeliefPropagation) -> float: """Return the expected value of a certain decision node instantiation for the agent making the decision""" macid = self.copy_without_cpds() dec_list = macid.get_valid_acyclic_dec_node_ordering() dec = dec_list[row] agent = self.whose_node[dec] # gets the agent making that decision utils = self.utility_nodes_agent[ agent] # gets the utility nodes for that agent factor = bp.query(variables=utils, evidence=dict(zip(dec_list, dec_instantiation))) ev = 0 for idx, prob in np.ndenumerate(factor.values): for i in range( len(utils) ): # account for each agent having multiple utilty nodes if prob != 0: ev += prob * idx[i] return ev
def gen_env_model(): """Specify BBN.""" cpd_tws = TabularCPD('TWS', 2, values=[[0.8, 0.2]]) cpd_twa = TabularCPD('TWA', 2, values=[[0.8, 0.2]]) cpd_wind = TabularCPD( 'Wind', 2, # values=[[1, 0.1, 0.1, 0.0], # [0.0, 0.9, 0.9, 1.0]], values=[[1, 0.999, 0.999, 0.998], [0.0, 0.001, 0.001, 0.002]], # min evidence=['TWA', 'TWS'], evidence_card=[2, 2]) cpd_wh = TabularCPD('WH', 2, values=[[0.8, 0.2]]) cpd_wd = TabularCPD('WD', 2, values=[[0.8, 0.2]]) cpd_waves = TabularCPD( 'Waves', 2, values=[ [1, 0.1, 0.1, 0.0], # normal vals [0.0, 0.9, 0.9, 1.0] ], # values = [[1, 0.999, 0.999, 0.998], # [0.0, 0.001, 0.001, 0.002]], # min failure evidence=['WH', 'WD'], evidence_card=[2, 2]) cpd_fail = TabularCPD('Craft failure', 2, values=[[1.0, 0.1, 0.1, 0.0], [0.0, 0.9, 0.9, 1.0]], evidence=['Waves', 'Wind'], evidence_card=[2, 2]) model = BayesianModel([('TWS', 'Wind'), ('TWA', 'Wind'), ('WH', 'Waves'), ('WD', 'Waves'), ('Waves', 'Craft failure'), ('Wind', 'Craft failure')]) model.add_cpds(cpd_tws, cpd_twa, cpd_wind, cpd_wh, cpd_wd, cpd_waves, cpd_fail) belief_propagation = BeliefPropagation(model) return belief_propagation
def compute_pk(self, type_list, fid): assert len(type_list) == 5, print("ComputePk Error: number of type_list should be 5") constraint_name = ['m', 'r', 's', 'd', 'v'] ''' m, r, s, d, v = type_list p_m, p_r, p_s, p_d, p_v = self.p_observation p_ktox, p_xtok = self.p_implication p_ktom, p_ktor, p_ktos, p_ktod, p_ktov = p_ktox p_mtok, p_rtok, p_stok, p_dtok, p_vtok = p_xtok ''' fg = FactorGraph() fg.add_node('k') for i in range(len(type_list)): if type_list[i] == 0: fg = self.add_constraints_k2x_x2k(fg, self.p_observation[fid][i], self.p_implication[fid][0][i], self.p_implication[fid][1][i], constraint_name[i]) elif type_list[i] == 1: fg = self.add_constraints_k2x(fg, self.p_observation[fid][i], self.p_implication[fid][0][i], constraint_name[i]) elif type_list[i] == 2: fg = self.add_constraints_x2k(fg, self.p_observation[fid][i], self.p_implication[fid][1][i], constraint_name[i]) ''' if m == 0: fg = add_constraints_kv_vk(fg, p_m, p_ktom, p_mtok, 'm') elif m == 1: fg = add_constraints_kv(fg, p_m, p_mtok, 'm') elif m == 2: fg = add_constraints_vk(fg, p_m, p_mtok, 'm') if r == 0: fg = add_constraints_kv_vk(fg, p_r, p_ktor, p_rtok, 'r') elif r == 1: fg = add_constraints_kv(fg, p_r, p_ktor, 'r') elif r == 2: fg = add_constraints_vk(fg, p_r, p_rtok, 'r') if s == 0: fg = add_constraints_kv_vk(fg, p_s, p_ktos, p_stok, 's') elif s == 1: fg = add_constraints_kv(fg, p_s, p_ktos, 's') elif s == 2: fg = add_constraints_vk(fg, p_s, p_stok, 's') if d == 0: fg = add_constraints_kv_vk(fg, p_d, p_ktod, p_dtok, 'd') elif d == 1: fg = add_constraints_kv(fg, p_d, p_ktod, 'd') elif d == 2: fg = add_constraints_vk(fg, p_d, p_dtok, 'd') if v == 0: fg = add_constraints_kv_vk(fg, p_v, p_ktov, p_vtok, 'v') elif v == 1: fg = add_constraints_kv(fg, p_v, p_ktov, 'v') elif v == 2: fg = add_constraints_vk(fg, p_v, p_vtok, 'v') ''' bp = BeliefPropagation(fg) #result = bp.query(variables=['k'])['k'] #result = bp.query(variables=['k'], joint=False)['k'] result = bp.query(variables=['k']) result.normalize() #print(result) return result.values[1]
def backward_inference(self, variables, evidence=None): """ Backward inference method using belief propagation. Parameters: ---------- variables: list list of variables for which you want to compute the probability evidence: dict a dict key, value pair as {var: state_of_var_observed} None if no evidence Examples: -------- >>> from pgmpy.factors import TabularCPD >>> from pgmpy.models import DynamicBayesianNetwork as DBN >>> from pgmpy.inference import DBNInference >>> dbnet = DBN() >>> dbnet.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)), ... (('Z', 0), ('Z', 1))]) >>> z_start_cpd = TabularCPD(('Z', 0), 2, [[0.5, 0.5]]) >>> x_i_cpd = TabularCPD(('X', 0), 2, [[0.6, 0.9], ... [0.4, 0.1]], ... evidence=[('Z', 0)], ... evidence_card=2) >>> y_i_cpd = TabularCPD(('Y', 0), 2, [[0.2, 0.3], ... [0.8, 0.7]], ... evidence=[('X', 0)], ... evidence_card=2) >>> z_trans_cpd = TabularCPD(('Z', 1), 2, [[0.4, 0.7], ... [0.6, 0.3]], ... evidence=[('Z', 0)], ... evidence_card=2) >>> dbnet.add_cpds(z_start_cpd, z_trans_cpd, x_i_cpd, y_i_cpd) >>> dbnet.initialize_initial_state() >>> dbn_inf = DBNInference(dbnet) >>> dbn_inf.backward_inference([('X', 0)], {('Y', 0):0, ('Y', 1):1, ('Y', 2):1})[('X', 0)].values array([ 0.66594382, 0.33405618]) """ variable_dict = defaultdict(list) for var in variables: variable_dict[var[1]].append(var) time_range = max(variable_dict) interface_nodes_dict = {} if evidence: evid_time_range = max([time_slice for var, time_slice in evidence.keys()]) time_range = max(time_range, evid_time_range) end_bp = BeliefPropagation(self.start_junction_tree) potential_dict = self.forward_inference(variables, evidence, 'potential') update_factor = self._shift_factor(potential_dict[time_range], 1) factor_values = {} for time_slice in range(time_range, 0, -1): evidence_time = self._get_evidence(evidence, time_slice, 1) evidence_prev_time = self._get_evidence(evidence, time_slice - 1, 0) if evidence_prev_time: interface_nodes_dict = {k: v for k, v in evidence_prev_time.items() if k in self.interface_nodes_0} if evidence_time: evidence_time.update(interface_nodes_dict) mid_bp = BeliefPropagation(self.one_and_half_junction_tree) self._update_belief(mid_bp, self.in_clique, potential_dict[time_slice - 1]) forward_factor = self._shift_factor(potential_dict[time_slice], 1) self._update_belief(mid_bp, self.out_clique, forward_factor, update_factor) if variable_dict[time_slice]: variable_time = self._shift_nodes(variable_dict[time_slice], 1) new_values = mid_bp.query(variable_time, evidence=evidence_time) changed_values = {} for key in new_values.keys(): new_key = (key[0], time_slice) new_factor = Factor([new_key], new_values[key].cardinality, new_values[key].values) changed_values[new_key] = new_factor factor_values.update(changed_values) clique_phi = self._get_factor(mid_bp, evidence_time) in_clique_phi = self._marginalize_factor(self.interface_nodes_0, clique_phi) update_factor = self._shift_factor(in_clique_phi, 1) out_clique_phi = self._shift_factor(update_factor, 0) self._update_belief(end_bp, self.start_interface_clique, potential_dict[0], out_clique_phi) evidence_0 = self._get_evidence(evidence, 0, 0) if variable_dict[0]: factor_values.update(end_bp.query(variable_dict[0], evidence_0)) return factor_values
def test_query_single_variable(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(['J']) np_test.assert_array_almost_equal(query_result['J'].values, np.array([0.416, 0.584]))
def test_query_single_variable_with_evidence(self): belief_propagation = BeliefPropagation(self.bayesian_model) query_result = belief_propagation.query(variables=['J'], evidence={'A': 0, 'R': 1}) np_test.assert_array_almost_equal(query_result['J'].values, np.array([0.60, 0.40]))
PGM = MarkovModel() PGM.add_nodes_from(['w1', 'w2', 'w3']) PGM.add_edges_from([('w1', 'w2'), ('w2', 'w3')]) tr_matrix = np.array([1, 10, 3, 2, 1, 5, 3, 3, 2]) tr_matrix = np.array([1, 2, 3, 10, 1, 3, 3, 5, 2]).reshape(3, 3).T.reshape(-1) phi = [DiscreteFactor(edge, [3, 3], tr_matrix) for edge in PGM.edges()] print(phi[0]) print(phi[1]) PGM.add_factors(*phi) # Calculate partition funtion Z = PGM.get_partition_function() print('The partition function is:', Z) # Calibrate the click belief_propagation = BeliefPropagation(PGM) belief_propagation.calibrate() # Output calibration result, which you should get query = belief_propagation.query(variables=['w2']) print('After calibration you should get the following mu(S):\n', query * Z) # Get marginal distribution over third word query = belief_propagation.query(variables=['w3']) print('Marginal distribution over the third word is:\n', query) #Get conditional distribution over third word query = belief_propagation.query(variables=['w3'], evidence={'w1': 0}) # 0 stays for "noun" print( 'Conditional distribution over the third word, given that the first word is noun is:\n',
('traffic_jam', 'long_queues'), ('traffic_jam', 'late_for_school'), ('getting_up_late', 'late_for_school')]) cpd_rain = TabularCPD('rain', 2, [[0.4], [0.6]]) cpd_accident = TabularCPD('accident', 2, [[0.2], [0.8]]) cpd_traffic_jam = TabularCPD('traffic_jam', 2, [[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]], evidence=['rain', 'accident'], evidence_card=[2, 2]) cpd_getting_up_late = TabularCPD('getting_up_late', 2, [[0.6], [0.4]]) cpd_late_for_school = TabularCPD('late_for_school', 2, [[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]], evidence=['getting_up_late','traffic_jam'], evidence_card=[2, 2]) cpd_long_queues = TabularCPD('long_queues', 2, [[0.9, 0.2], [0.1, 0.8]], evidence=['traffic_jam'], evidence_card=[2]) model.add_cpds(cpd_rain, cpd_accident, cpd_traffic_jam, cpd_getting_up_late, cpd_late_for_school, cpd_long_queues) belief_propagation = BeliefPropagation(model) # To calibrate the clique tree, use calibrate() method belief_propagation.calibrate() # To get cluster (or clique) beliefs use the corresponding getters belief_propagation.get_clique_beliefs()
def test_map_query(self): belief_propagation = BeliefPropagation(self.bayesian_model) map_query = belief_propagation.map_query() self.assertDictEqual(map_query, {'A': 1, 'R': 1, 'J': 1, 'Q': 1, 'G': 0, 'L': 0})
def test_map_query_with_evidence(self): belief_propagation = BeliefPropagation(self.bayesian_model) map_query = belief_propagation.map_query(['A', 'R', 'L'], {'J': 0, 'Q': 1, 'G': 0}) self.assertDictEqual(map_query, {'A': 1, 'R': 0, 'L': 0})
######################################################################## # Construct Naive Bayes network (without help from structure learning) # ######################################################################## model = BayesianModel([('class', 'doors'), ('class', 'safety'), ('class', 'maint'), ('class', 'buying'), ('class', 'persons'), ('class', 'lug_boot')]) model.fit(df_train, estimator=MaximumLikelihoodEstimator) # print(model.get_cpds('class').values) # print(model.active_trail_nodes('doors', observed='class')) # can also use Belief propagation # inference = BeliefPropagation(model) print("Class variable prior:\n{}\n".format( inference.query(variables=['class'])['class'])) print("Class variable posterior after certain observation:\n{}\n".format( inference.query(variables=['class'], evidence={'doors': 0})['class'])) ##################### # Predict test data # ##################### predict_data = df_test.copy() predict_data.drop(['class'], axis=1, inplace=True) # y_pred = model.predict(predict_data) pred_values = []
def markov_inference(dict_of_esp_jointprob): """Calculate the markov model """ factor_mmb_cmma = Factor(variables=['money_market_bonus', 'collateral_mma'], cardinality=[2, 2], values=[dict_of_esp_jointprob['mmb0_cmma0'], dict_of_esp_jointprob['mmb0_cmma1'], dict_of_esp_jointprob['mmb1_cmma0'], dict_of_esp_jointprob['mmb1_cmma1']]) factor_mmb_cm = Factor(variables=['money_market_bonus', 'cash_management'], cardinality=[2, 2], values=[dict_of_esp_jointprob['mmb0_cm0'], dict_of_esp_jointprob['mmb0_cm1'], dict_of_esp_jointprob['mmb1_cm0'], dict_of_esp_jointprob['mmb1_cm1']]) factor_mmb_fx = Factor(variables=['money_market_bonus', 'fx_products'], cardinality=[2, 2], values=[dict_of_esp_jointprob['mmb0_fx0'], dict_of_esp_jointprob['mmb0_fx1'], dict_of_esp_jointprob['mmb1_fx0'], dict_of_esp_jointprob['mmb1_fx1']]) factor_mmb_loc = Factor(variables=['money_market_bonus', 'letters_of_credit'], cardinality=[2, 2], values=[dict_of_esp_jointprob['mmb0_loc0'], dict_of_esp_jointprob['mmb0_loc1'], dict_of_esp_jointprob['mmb1_loc0'], dict_of_esp_jointprob['mmb1_loc1']]) factor_mmb_es = Factor(variables=['money_market_bonus', 'enterprise_sweep'], cardinality=[2, 2], values=[dict_of_esp_jointprob['mmb0_es0'], dict_of_esp_jointprob['mmb0_es1'], dict_of_esp_jointprob['mmb1_es0'], dict_of_esp_jointprob['mmb1_es1']]) factor_mmb_checking = Factor(variables=['money_market_bonus', 'checking_usd'], cardinality=[2, 2], values=[dict_of_esp_jointprob['mmb0_checking0'], dict_of_esp_jointprob['mmb0_checking1'], dict_of_esp_jointprob['mmb1_checking0'], dict_of_esp_jointprob['mmb1_checking1']]) # collateral mma factor_cmma_cm = Factor(variables=['collateral_mma','cash_management'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cmma0_cm0'], dict_of_esp_jointprob['cmma0_cm1'], dict_of_esp_jointprob['cmma1_cm0'], dict_of_esp_jointprob['cmma1_cm1']]) factor_cmma_fx = Factor(variables=['collateral_mma', 'fx_products'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cmma0_fx0'], dict_of_esp_jointprob['cmma0_fx1'], dict_of_esp_jointprob['cmma1_fx0'], dict_of_esp_jointprob['cmma1_fx1']]) factor_cmma_loc = Factor(variables=['collateral_mma', 'letters_of_credit'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cmma0_loc0'], dict_of_esp_jointprob['cmma0_loc1'], dict_of_esp_jointprob['cmma1_loc0'], dict_of_esp_jointprob['cmma1_loc1']]) factor_cmma_es= Factor(variables=['collateral_mma', 'enterprise_sweep'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cmma0_es0'], dict_of_esp_jointprob['cmma0_es1'], dict_of_esp_jointprob['cmma1_es0'], dict_of_esp_jointprob['cmma1_es1']]) factor_cmma_checking = Factor(variables=['collateral_mma', 'checking_usd'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cmma0_checking0'], dict_of_esp_jointprob['cmma0_checking1'], dict_of_esp_jointprob['cmma1_checking0'],dict_of_esp_jointprob['cmma1_checking1']]) # cash management factor_cm_fx = Factor(variables=['cash_management', 'fx_products'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cm0_fx0'], dict_of_esp_jointprob['cm0_fx1'], dict_of_esp_jointprob['cm1_fx0'], dict_of_esp_jointprob['cm1_fx1']]) factor_cm_loc = Factor(variables=['cash_management', 'letters_of_credit'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cm0_loc0'], dict_of_esp_jointprob['cm0_loc1'], dict_of_esp_jointprob['cm1_loc0'], dict_of_esp_jointprob['cm1_loc1']]) factor_cm_es= Factor(variables=['cash_management', 'enterprise_sweep'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cm0_es0'], dict_of_esp_jointprob['cm0_es1'], dict_of_esp_jointprob['cm1_es0'], dict_of_esp_jointprob['cm1_es1']]) factor_cm_checking = Factor(variables=['cash_management', 'checking_usd'], cardinality=[2, 2], values=[dict_of_esp_jointprob['cm0_checking0'], dict_of_esp_jointprob['cm0_checking1'], dict_of_esp_jointprob['cm1_checking0'], dict_of_esp_jointprob['cm1_checking1']]) # FX products factor_fx_loc = Factor(variables=['fx_products', 'letters_of_credit'], cardinality=[2, 2], values=[dict_of_esp_jointprob['fx0_loc0'], dict_of_esp_jointprob['fx0_loc1'], dict_of_esp_jointprob['fx1_loc0'], dict_of_esp_jointprob['fx1_loc1']]) factor_fx_es= Factor(variables=['fx_products', 'enterprise_sweep'], cardinality=[2, 2], values=[dict_of_esp_jointprob['fx0_es0'], dict_of_esp_jointprob['fx0_es1'], dict_of_esp_jointprob['fx1_es0'], dict_of_esp_jointprob['fx1_es1']]) factor_fx_checking = Factor(variables=['fx_products', 'checking_usd'], cardinality=[2, 2], values=[dict_of_esp_jointprob['fx0_checking0'], dict_of_esp_jointprob['fx0_checking1'], dict_of_esp_jointprob['fx1_checking0'], dict_of_esp_jointprob['fx1_checking1']]) # letters of credit factor_loc_es= Factor(variables=['letters_of_credit', 'enterprise_sweep'], cardinality=[2, 2], values=[dict_of_esp_jointprob['loc0_es0'], dict_of_esp_jointprob['loc0_es1'], dict_of_esp_jointprob['loc1_es0'], dict_of_esp_jointprob['loc1_es1']]) factor_loc_checking = Factor(variables=['letters_of_credit', 'checking_usd'], cardinality=[2, 2], values=[dict_of_esp_jointprob['loc0_checking0'], dict_of_esp_jointprob['loc0_checking1'], dict_of_esp_jointprob['loc1_checking0'], dict_of_esp_jointprob['loc1_checking1']]) #enterprise sweep factor_es_checking = Factor(variables=['enterprise_sweep', 'checking_usd'], cardinality=[2, 2], values=[dict_of_esp_jointprob['es0_checking0'], dict_of_esp_jointprob['es0_checking1'], dict_of_esp_jointprob['es1_checking0'], dict_of_esp_jointprob['es1_checking1']]) # built the markov model model.add_factors(factor_mmb_cmma , factor_mmb_cm, factor_mmb_fx, factor_mmb_loc,factor_mmb_es, factor_mmb_checking, factor_cmma_cm , factor_cmma_fx, factor_cmma_loc, factor_cmma_es,factor_cmma_checking, factor_cm_fx, factor_cm_loc, factor_cm_es, factor_cm_checking , factor_fx_loc, factor_fx_es , factor_fx_checking, factor_loc_es, factor_loc_checking , factor_es_checking ) belief_propagation = BeliefPropagation(model) all_products = ['money_market_bonus','collateral_mma', 'cash_management','enterprise_sweep', 'fx_products','letters_of_credit','checking_usd'] # perform inference for all product except the one in the for loop for prod in all_products: if evidence_==None: new_evidence=evidence_ else: new_evidence = {key: value for key, value in evidence_.items() if key != prod} # perform belief inference on only one product at a time belief_inference_products = str(prod) # belief propogation on one product at a time given evidence from all other products belief = belief_propagation.query(variables=[belief_inference_products], evidence=new_evidence) try: #mmb = belief_mmb['money_market_bonus'].values[1] mmb = belief['money_market_bonus'].values[1] if mmb <0 : mmb = .0000001 elif mmb >1: mmb =1 prob_mmb.append(mmb)# one is having the product except: # can't perform inference on this product pass try: cmma = belief['collateral_mma'].values[1] if cmma <0: cmma = .0000001 elif cmma >1: cmma =1 prob_cmma.append(cmma) except:## don't have this product pass try: cm = belief['cash_management'].values[1] if cm <0: cm = .0000001 elif cm >1: cm =1 prob_cm.append(cm) except: pass try: checking = belief['checking_usd'].values[1] if checking <0: checking = .0000001 elif checking >1: checking =1 prob_checking.append(checking) except: pass try: fx = belief['fx_products'].values[1] if fx <0: fx = .0000001 elif fx >1: fx =1 prob_fx.append(fx) except: pass try: loc = belief['letters_of_credit'].values[1] if loc <0: loc = .0000001 elif loc > 1: loc = 1 prob_loc.append(loc) except: pass try: es = belief['enterprise_sweep'].values[1] if es<0: es = .0000001 elif es >1: es = 1 prob_es.append(es) except: pass
input_value = int(sys.argv[1]) test_value = np.array([0 for x in range(18)]) for i in reversed(range(18)): test_value[i] = int(input_value / (3**i)) input_value = input_value % (3**i) for i in reversed(range(18)): if (max_value[i] == 1): test_value[i] = 0 elif (min_value[i] == 1): if (test_value[i] < 1): test_value[i] = 0 else: test_value[i] = test_value[i] - 1 #print(model.check_model()) #bp.calibrate() bp = BeliefPropagation(new_model) result = bp.map_query(variables=['AN'], evidence={ 'CsM': test_value[0], 'CsH': test_value[1], 'CsI': test_value[2], 'InI': test_value[3], 'InD': test_value[4], 'InN': test_value[5], 'OutI': test_value[6], 'OutD': test_value[7], 'OutN': test_value[8], 'PitC': test_value[9], 'PitUp': test_value[10], 'PitD': test_value[11], 'PitT': test_value[12],
import numpy as np from pgmpy.models import FactorGraph from pgmpy.factors.discrete import DiscreteFactor from pgmpy.inference import BeliefPropagation G = FactorGraph() G.add_node(0) G.add_node(1) G.add_node(2) f01 = DiscreteFactor([0, 1], [2, 2], np.random.rand(4)) f02 = DiscreteFactor([0, 2], [2, 2], np.random.rand(4)) f12 = DiscreteFactor([1, 2], [2, 2], np.random.rand(4)) G.add_factors(f01) G.add_factors(f02) G.add_factors(f12) G.add_edges_from([(0, f01), (1, f01), (0, f02), (2, f02), (1, f12), (2, f12)]) bp = BeliefPropagation(G) bp.calibrate()
ND_Sample_NS = ND_Sample_NS[ND_Sample_NS[i_switch[jj]] == state[ii] [jj]] if len(ND_Sample_NS) != 0: P2.append(sum(ND_Sample_NS[i]) / len(ND_Sample_NS)) P1.append(1 - sum(ND_Sample_NS[i]) / len(ND_Sample_NS)) else: P2.append(0) P1.append(1) CPD = TabularCPD(i, 2, [P1, P2], evidence=i_switch, evidence_card=[2] * len(i_switch)) PPN_N.add_cpds(CPD) error = 0 bp_N = BeliefPropagation(PPN_N) #确定观测变量和隐变量,计算BP算法推理结果的误差 for i in range(0, 500): ITEM = random.sample(ND_Sample_N.index.tolist(), 1) # vbs = ['SL32','SC22','SF1','SC33','SF2'] vbs = ['SC33', 'SF2', 'SL34', 'SF3', 'SF4', 'SF5', 'SC44'] # VBS = {'SL32':int(ND_Sample_N['SL32'][ITEM]),'SC22':int(ND_Sample_N['SC22'][ITEM]),'SF1':int(ND_Sample_N['SF1'][ITEM]), # 'SC33':int(ND_Sample_N['SC33'][ITEM]),'SF2':int(ND_Sample_N['SF2'][ITEM])} # EDS = {'CP2B21':int(ND_Sample_N['CP2B21'][ITEM]),'CB22B21':int(ND_Sample_N['CB22B21'][ITEM]), # 'CB32B22':int(ND_Sample_N['CB32B22'][ITEM]),'CB32B31':int(ND_Sample_N['CB32B31'][ITEM]), # 'CP3B31':int(ND_Sample_N['CP3B31'][ITEM])} VBS = { 'SC33': int(ND_Sample_N['SC33'][ITEM]), 'SF2': int(ND_Sample_N['SF2'][ITEM]), 'SL34': int(ND_Sample_N['SL34'][ITEM]),
from pgmpy.factors.discrete import TabularCPD from pgmpy.models import BayesianModel from pgmpy.inference import BeliefPropagation bayesian_model = BayesianModel([('A', 'J'), ('R', 'J'), ('J', 'Q'), ('J', 'L'), ('G', 'L')]) cpd_a = TabularCPD('A', 2, [[0.2], [0.8]]) cpd_r = TabularCPD('R', 2, [[0.4], [0.6]]) cpd_j = TabularCPD('J', 2, [[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]], ['R', 'A'], [2, 2]) cpd_q = TabularCPD('Q', 2, [[0.9, 0.2], [0.1, 0.8]], ['J'], [2]) cpd_l = TabularCPD('L', 2, [[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]], ['G', 'J'], [2, 2]) cpd_g = TabularCPD('G', 2, [[0.6], [0.4]]) bayesian_model.add_cpds(cpd_a, cpd_r, cpd_j, cpd_q, cpd_l, cpd_g) belief_propagation = BeliefPropagation(bayesian_model) print( belief_propagation.map_query(variables=['J', 'Q'], evidence={ 'A': 0, 'R': 0, 'G': 0, 'L': 1 }))
from pgmpy.factors import TabularCPD, Factor from pgmpy.inference import BeliefPropagation # create a bayesian model as we did before model = BayesianModel(....) cpd_var1 = TabularCPD(....) cpd_var2 = TabularCPD(....) cpd_var3 = TabularCPD(....) cpd_var4 = TabularCPD(....) cpd_var5 = TabularCPD(....) model.add_cpds(..........) # Apply propagation belief_propagation = BeliefPropagation(model) # To calibrate the clique tree, use calibrate() method belief_propagation.calibrate() # To get cluster (or clique) beliefs use the corresponding getters belief_propagation.get_clique_beliefs() # To get the sepset beliefs use the corresponding getters belief_propagation.get_sepset_beliefs() >> # Query variables not in the same cluster belief_propagation.query(variables=['no_of_people'], evidence={'location':1, 'quality':1}) >> # Can apply MAP_Query - next belief_propagation.map_query(variables=['no_of_people'], evidence={'location':1, 'quality':1})
def backward_inference(self, variables, evidence=None): """ Backward inference method using belief propagation. Parameters: ---------- variables: list list of variables for which you want to compute the probability evidence: dict a dict key, value pair as {var: state_of_var_observed} None if no evidence Examples: -------- >>> from pgmpy.factors.discrete import TabularCPD >>> from pgmpy.models import DynamicBayesianNetwork as DBN >>> from pgmpy.inference import DBNInference >>> dbnet = DBN() >>> dbnet.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)), ... (('Z', 0), ('Z', 1))]) >>> z_start_cpd = TabularCPD(('Z', 0), 2, [[0.5, 0.5]]) >>> x_i_cpd = TabularCPD(('X', 0), 2, [[0.6, 0.9], ... [0.4, 0.1]], ... evidence=[('Z', 0)], ... evidence_card=[2]) >>> y_i_cpd = TabularCPD(('Y', 0), 2, [[0.2, 0.3], ... [0.8, 0.7]], ... evidence=[('X', 0)], ... evidence_card=[2]) >>> z_trans_cpd = TabularCPD(('Z', 1), 2, [[0.4, 0.7], ... [0.6, 0.3]], ... evidence=[('Z', 0)], ... evidence_card=[2]) >>> dbnet.add_cpds(z_start_cpd, z_trans_cpd, x_i_cpd, y_i_cpd) >>> dbnet.initialize_initial_state() >>> dbn_inf = DBNInference(dbnet) >>> dbn_inf.backward_inference([('X', 0)], {('Y', 0):0, ('Y', 1):1, ('Y', 2):1})[('X', 0)].values array([ 0.66594382, 0.33405618]) """ variable_dict = defaultdict(list) for var in variables: variable_dict[var[1]].append(var) time_range = max(variable_dict) interface_nodes_dict = {} if evidence: evid_time_range = max( [time_slice for var, time_slice in evidence.keys()]) time_range = max(time_range, evid_time_range) end_bp = BeliefPropagation(self.start_junction_tree) potential_dict = self.forward_inference(variables, evidence, 'potential') update_factor = self._shift_factor(potential_dict[time_range], 1) factor_values = {} for time_slice in range(time_range, 0, -1): evidence_time = self._get_evidence(evidence, time_slice, 1) evidence_prev_time = self._get_evidence(evidence, time_slice - 1, 0) if evidence_prev_time: interface_nodes_dict = { k: v for k, v in evidence_prev_time.items() if k in self.interface_nodes_0 } if evidence_time: evidence_time.update(interface_nodes_dict) mid_bp = BeliefPropagation(self.one_and_half_junction_tree) self._update_belief(mid_bp, self.in_clique, potential_dict[time_slice - 1]) forward_factor = self._shift_factor(potential_dict[time_slice], 1) self._update_belief(mid_bp, self.out_clique, forward_factor, update_factor) if variable_dict[time_slice]: variable_time = self._shift_nodes(variable_dict[time_slice], 1) new_values = mid_bp.query(variable_time, evidence=evidence_time) changed_values = {} for key in new_values.keys(): new_key = (key[0], time_slice) new_factor = DiscreteFactor([new_key], new_values[key].cardinality, new_values[key].values) changed_values[new_key] = new_factor factor_values.update(changed_values) clique_phi = self._get_factor(mid_bp, evidence_time) in_clique_phi = self._marginalize_factor(self.interface_nodes_0, clique_phi) update_factor = self._shift_factor(in_clique_phi, 1) out_clique_phi = self._shift_factor(update_factor, 0) self._update_belief(end_bp, self.start_interface_clique, potential_dict[0], out_clique_phi) evidence_0 = self._get_evidence(evidence, 0, 0) if variable_dict[0]: factor_values.update(end_bp.query(variable_dict[0], evidence_0)) return factor_values
('PVS', 'VTUB'), ('PVS', 'ACO2'), ('SAO2', 'VMCH'), ('SAO2', 'VLNG'), ('SAO2', 'VALV'), ('SAO2', 'ACO2'), ('SHNT', 'INT'), ('INT', 'VALV'), ('PRSS', 'VTUB'), ('DISC', 'VTUB'), ('MVS', 'VMCH'), ('VMCH', 'VTUB'), ('VMCH', 'VALV'), ('VTUB', 'VLNG'), ('VTUB', 'VALV'), ('VLNG', 'VALV'), ('VLNG', 'ACO2'), ('VALV', 'ACO2'), ('CCHL', 'HR'), ('HR', 'CO'), ('CO', 'BP'), ('HRBP', 'INT')]) # pe = ParameterEstimator(model, df) # print("\n", pe.state_counts('SAO2')) mle = MaximumLikelihoodEstimator(model, df) model.fit(df, MaximumLikelihoodEstimator) model.fit(df, estimator=BayesianEstimator) # infer = VariableElimination(model) # print (" **************** Inference using variable elimination ...***********"); # print ("VALV : 'VTUB':0,'PRSS':1"); # print (infer.query(['VALV'],evidence={'VTUB':0,'PRSS':1}) ['VALV']) print(" ****************Inference using belief propagation ...***********") print("VALV : 'VTUB':0,'PRSS':1") belief_propagation = BeliefPropagation(model) belief_propagation.map_query(variables=['VALV'], evidence={ 'VTUB': 0, 'PRSS': 0 })