Example #1
0
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)
Example #2
0
def get_partition_function_BP(G):
    '''
	Calculate partition function of G using belief propogation

	'''
    bp = BeliefPropagation(G)
    bp.calibrate()
    clique_beliefs = bp.get_clique_beliefs()
    partition_function = np.sum(clique_beliefs.values()[0].values)
    return partition_function
Example #3
0
def find_MAP_val(G):
    '''
	Inputs:
	- G: MarkovModel
	'''

    bp = BeliefPropagation(G)
    bp.max_calibrate()
    clique_beliefs = bp.get_clique_beliefs()
    map_val = np.max(clique_beliefs.values()[0].values)
    return map_val
Example #4
0
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
Example #5
0
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
Example #6
0
    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)
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
                       ('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()