예제 #1
0
    def to_bn(self, use_mst=False):
        """ Given the undirected graph, return the directed model corresponding to the minimal I-map. The directed model is
        placed in the directed attribute.
        Remarks: This method supports models which are not connected.
        :param use_mst (boolean): To direct a prelearned model in the directed attribute, direct_mst=False. To obtain
            the directed model from the maximum spanning tree learned by self.mst, use direct_mst=True.
        """

        # Generate connected components
        if use_mst:
            edges = list(self.maxtree.edges())
            vertices = list(self.maxtree.nodes)
            mm = MarkovModel()
            mm.add_nodes_from(vertices)
            mm.add_edges_from(edges)
            bm = mm.to_bayesian_model()

            self.directed = bm

        else:
            connected = list(nx.algorithms.components.connected_components(self.undirected))
            # Hold the edges of each connected component as a list(list())
            connected_comps = list()
            # If the graph is not completely connected
            if len(connected) > 1:
                self.directed = BayesianModel()
                for comp in connected:
                    # If the connected component is not a single vertex
                    if len(comp) > 1:
                        edges_to_add = list()
                        temp = MarkovModel()
                        for vert1 in comp:
                            neighbours = list(self.undirected.neighbors(vert1))
                            for vert2 in neighbours:
                                if not ((vert1, vert2) in edges_to_add) and not ((vert2, vert1) in edges_to_add):
                                    edges_to_add.append((vert1, vert2))

                        temp.add_nodes_from(comp)
                        temp.add_edges_from(edges_to_add)
                        temp_bn = temp.to_bayesian_model()
                        connected_comps.append(temp_bn)
                    else:
                        self.directed.add_nodes_from(comp)

                for bn in connected_comps:
                    self.directed.add_nodes_from(list(bn.nodes))
                    self.directed.add_edges_from(list(bn.edges))

            else:
                # If the graph is completely connected, just add all edges to markov model
                edges = list(self.undirected.edges())
                vertices = list(self.undirected.nodes)
                mm = MarkovModel()
                mm.add_nodes_from(vertices)
                mm.add_edges_from(edges)
                bm = mm.to_bayesian_model()

                self.directed = bm
예제 #2
0
    def setUp(self):
        self.bayesian = BayesianModel([('a', 'b'), ('b', 'c'), ('c', 'd'),
                                       ('d', 'e')])
        a_cpd = TabularCPD('a', 2, [[0.4, 0.6]])
        b_cpd = TabularCPD('b',
                           2, [[0.2, 0.4], [0.3, 0.4]],
                           evidence='a',
                           evidence_card=[2])
        c_cpd = TabularCPD('c',
                           2, [[0.1, 0.2], [0.3, 0.4]],
                           evidence='b',
                           evidence_card=[2])
        d_cpd = TabularCPD('d',
                           2, [[0.4, 0.3], [0.2, 0.1]],
                           evidence='c',
                           evidence_card=[2])
        e_cpd = TabularCPD('e',
                           2, [[0.3, 0.2], [0.4, 0.1]],
                           evidence='d',
                           evidence_card=[2])
        self.bayesian.add_cpds(a_cpd, b_cpd, c_cpd, d_cpd, e_cpd)

        self.markov = MarkovModel([('a', 'b'), ('b', 'd'), ('a', 'c'),
                                   ('c', 'd')])
        factor_1 = Factor(['a', 'b'], [2, 2], np.array([100, 1, 1, 100]))
        factor_2 = Factor(['a', 'c'], [2, 2], np.array([40, 30, 100, 20]))
        factor_3 = Factor(['b', 'd'], [2, 2], np.array([1, 100, 100, 1]))
        factor_4 = Factor(['c', 'd'], [2, 2], np.array([60, 60, 40, 40]))
        self.markov.add_factors(factor_1, factor_2, factor_3, factor_4)
예제 #3
0
    def setUp(self):
        # A test Bayesian model
        diff_cpd = TabularCPD('diff', 2, [[0.6], [0.4]])
        intel_cpd = TabularCPD('intel', 2, [[0.7], [0.3]])
        grade_cpd = TabularCPD('grade',
                               3,
                               [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3],
                                [0.3, 0.7, 0.02, 0.2]],
                               evidence=['diff', 'intel'],
                               evidence_card=[2, 2])
        self.bayesian_model = BayesianModel()
        self.bayesian_model.add_nodes_from(['diff', 'intel', 'grade'])
        self.bayesian_model.add_edges_from([('diff', 'grade'),
                                            ('intel', 'grade')])
        self.bayesian_model.add_cpds(diff_cpd, intel_cpd, grade_cpd)

        # A test Markov model
        self.markov_model = MarkovModel([('A', 'B'), ('C', 'B'), ('B', 'D')])
        factor_ab = DiscreteFactor(['A', 'B'], [2, 3], [1, 2, 3, 4, 5, 6])
        factor_cb = DiscreteFactor(['C', 'B'], [4, 3],
                                   [3, 1, 4, 5, 7, 8, 1, 3, 10, 4, 5, 6])
        factor_bd = DiscreteFactor(['B', 'D'], [3, 2], [5, 7, 2, 1, 9, 3])
        self.markov_model.add_factors(factor_ab, factor_cb, factor_bd)

        self.gibbs = GibbsSampling(self.bayesian_model)
예제 #4
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)
예제 #5
0
    def setUp(self):
        # It is just a moralised version of the above Bayesian network so all the results are same. Only factors
        # are under consideration for inference so this should be fine.
        self.markov_model = MarkovModel([('A', 'J'), ('R', 'J'), ('J', 'Q'),
                                         ('J', 'L'), ('G', 'L'), ('A', 'R'),
                                         ('J', 'G')])

        factor_a = TabularCPD('A', 2, values=[[0.2], [0.8]]).to_factor()
        factor_r = TabularCPD('R', 2, values=[[0.4], [0.6]]).to_factor()
        factor_j = TabularCPD('J',
                              2,
                              values=[[0.9, 0.6, 0.7, 0.1],
                                      [0.1, 0.4, 0.3, 0.9]],
                              evidence=['A', 'R'],
                              evidence_card=[2, 2]).to_factor()
        factor_q = TabularCPD('Q',
                              2,
                              values=[[0.9, 0.2], [0.1, 0.8]],
                              evidence=['J'],
                              evidence_card=[2]).to_factor()
        factor_l = TabularCPD('L',
                              2,
                              values=[[0.9, 0.45, 0.8, 0.1],
                                      [0.1, 0.55, 0.2, 0.9]],
                              evidence=['J', 'G'],
                              evidence_card=[2, 2]).to_factor()
        factor_g = TabularCPD('G', 2, [[0.6], [0.4]]).to_factor()

        self.markov_model.add_factors(factor_a, factor_r, factor_j, factor_q,
                                      factor_l, factor_g)
        self.markov_inference = VariableElimination(self.markov_model)
예제 #6
0
    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()
예제 #7
0
    def setUp(self):
        self.bayesian = BayesianModel([("a", "b"), ("b", "c"), ("c", "d"),
                                       ("d", "e")])
        a_cpd = TabularCPD("a", 2, [[0.4, 0.6]])
        b_cpd = TabularCPD("b",
                           2, [[0.2, 0.4], [0.8, 0.6]],
                           evidence=["a"],
                           evidence_card=[2])
        c_cpd = TabularCPD("c",
                           2, [[0.1, 0.2], [0.9, 0.8]],
                           evidence=["b"],
                           evidence_card=[2])
        d_cpd = TabularCPD("d",
                           2, [[0.4, 0.3], [0.6, 0.7]],
                           evidence=["c"],
                           evidence_card=[2])
        e_cpd = TabularCPD("e",
                           2, [[0.3, 0.2], [0.7, 0.8]],
                           evidence=["d"],
                           evidence_card=[2])
        self.bayesian.add_cpds(a_cpd, b_cpd, c_cpd, d_cpd, e_cpd)

        self.markov = MarkovModel([("a", "b"), ("b", "d"), ("a", "c"),
                                   ("c", "d")])
        factor_1 = DiscreteFactor(["a", "b"], [2, 2],
                                  np.array([100, 1, 1, 100]))
        factor_2 = DiscreteFactor(["a", "c"], [2, 2],
                                  np.array([40, 30, 100, 20]))
        factor_3 = DiscreteFactor(["b", "d"], [2, 2],
                                  np.array([1, 100, 100, 1]))
        factor_4 = DiscreteFactor(["c", "d"], [2, 2],
                                  np.array([60, 60, 40, 40]))
        self.markov.add_factors(factor_1, factor_2, factor_3, factor_4)
    def setUp(self):
        # A test Bayesian model
        diff_cpd = TabularCPD("diff", 2, [[0.6], [0.4]])
        intel_cpd = TabularCPD("intel", 2, [[0.7], [0.3]])
        grade_cpd = TabularCPD(
            "grade",
            3,
            [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3],
             [0.3, 0.7, 0.02, 0.2]],
            evidence=["diff", "intel"],
            evidence_card=[2, 2],
        )
        self.bayesian_model = BayesianModel()
        self.bayesian_model.add_nodes_from(["diff", "intel", "grade"])
        self.bayesian_model.add_edges_from([("diff", "grade"),
                                            ("intel", "grade")])
        self.bayesian_model.add_cpds(diff_cpd, intel_cpd, grade_cpd)

        # A test Markov model
        self.markov_model = MarkovModel([("A", "B"), ("C", "B"), ("B", "D")])
        factor_ab = DiscreteFactor(["A", "B"], [2, 3], [1, 2, 3, 4, 5, 6])
        factor_cb = DiscreteFactor(["C", "B"], [4, 3],
                                   [3, 1, 4, 5, 7, 8, 1, 3, 10, 4, 5, 6])
        factor_bd = DiscreteFactor(["B", "D"], [3, 2], [5, 7, 2, 1, 9, 3])
        self.markov_model.add_factors(factor_ab, factor_cb, factor_bd)

        self.gibbs = GibbsSampling(self.bayesian_model)
def generate_random_fully_connected_MRF(N):
    '''
    Create an MRF with N binary nodes that are all part of the same clique, which
    has 2^N random values

    '''

    G = MarkovModel()

    #create an N nodes
    node_names = ['x%d' % i for i in range(N)]

    G.add_nodes_from(node_names)

    #add an edge between each node and its 4 neighbors, except when the
    #node is on the grid border and has fewer than 4 neighbors
    edges = []

    for i in range(N):
        for j in range(i + 1, N):
            edges.append(('x%d' % i, 'x%d' % j))

    assert (len(edges) == .5 * N**2)

    G.add_edges_from(edges)

    single_factor = DiscreteFactor(
        edges,
        cardinality=[2 for i in range(N)],
        values=np.random.rand(*[2 for i in range(N)]))
    G.add_factors(single_factor)

    return G
예제 #10
0
 def test_is_iequivalent(self):
     G = BayesianModel([('x', 'y'), ('z', 'y'), ('x', 'z'), ('w', 'y')])
     self.assertRaises(TypeError, G.is_iequivalent, MarkovModel())
     G1 = BayesianModel([('V', 'W'), ('W', 'X'), ('X', 'Y'), ('Z', 'Y')])
     G2 = BayesianModel([('W', 'V'), ('X', 'W'), ('X', 'Y'), ('Z', 'Y')])
     self.assertTrue(G1.is_iequivalent(G2))
     G3 = BayesianModel([('W', 'V'), ('W', 'X'), ('Y', 'X'), ('Z', 'Y')])
     self.assertFalse(G3.is_iequivalent(G2))
 def test_is_iequivalent(self):
     G = BayesianModel([("x", "y"), ("z", "y"), ("x", "z"), ("w", "y")])
     self.assertRaises(TypeError, G.is_iequivalent, MarkovModel())
     G1 = BayesianModel([("V", "W"), ("W", "X"), ("X", "Y"), ("Z", "Y")])
     G2 = BayesianModel([("W", "V"), ("X", "W"), ("X", "Y"), ("Z", "Y")])
     self.assertTrue(G1.is_iequivalent(G2))
     G3 = BayesianModel([("W", "V"), ("W", "X"), ("Y", "X"), ("Z", "Y")])
     self.assertFalse(G3.is_iequivalent(G2))
예제 #12
0
파일: Model.py 프로젝트: MCM-Fudan/MCM-2018
 def build_graph(self):
     mm = MarkovModel()
     mm.add_nodes_from(self.dataset.dataframe.columns)
     graph_structure_name = list(
         map(lambda tuple: (self.columns[tuple[0]], self.columns[tuple[1]]),
             self.graph_structure_index))
     mm.add_edges_from(graph_structure_name)
     self.model = mm.to_bayesian_model()
예제 #13
0
    def setUp(self):
        self.maxDiff = None
        edges = [['family-out', 'dog-out'],
                 ['bowel-problem', 'dog-out'],
                 ['family-out', 'light-on'],
                 ['dog-out', 'hear-bark']]
        cpds = {'bowel-problem': np.array([[0.01],
                                           [0.99]]),
                'dog-out': np.array([[0.99, 0.01, 0.97, 0.03],
                                     [0.9, 0.1, 0.3, 0.7]]),
                'family-out': np.array([[0.15],
                                        [0.85]]),
                'hear-bark': np.array([[0.7, 0.3],
                                       [0.01, 0.99]]),
                'light-on': np.array([[0.6, 0.4],
                                      [0.05, 0.95]])}
        states = {'bowel-problem': ['true', 'false'],
                  'dog-out': ['true', 'false'],
                  'family-out': ['true', 'false'],
                  'hear-bark': ['true', 'false'],
                  'light-on': ['true', 'false']}
        parents = {'bowel-problem': [],
                   'dog-out': ['bowel-problem', 'family-out'],
                   'family-out': [],
                   'hear-bark': ['dog-out'],
                   'light-on': ['family-out']}

        self.bayesmodel = BayesianModel(edges)

        tabular_cpds = []
        for var, values in cpds.items():
            cpd = TabularCPD(var, len(states[var]), values,
                             evidence=parents[var],
                             evidence_card=[len(states[evidence_var])
                                            for evidence_var in parents[var]])
            tabular_cpds.append(cpd)
        self.bayesmodel.add_cpds(*tabular_cpds)
        self.bayeswriter = UAIWriter(self.bayesmodel)

        edges = {('var_0', 'var_1'), ('var_0', 'var_2'), ('var_1', 'var_2')}
        self.markovmodel = MarkovModel(edges)
        tables = [(['var_0', 'var_1'],
                   ['4.000', '2.400', '1.000', '0.000']),
                  (['var_0', 'var_1', 'var_2'],
                   ['2.2500', '3.2500', '3.7500', '0.0000', '0.0000', '10.0000',
                    '1.8750', '4.0000', '3.3330', '2.0000', '2.0000', '3.4000'])]
        domain = {'var_1': '2', 'var_2': '3', 'var_0': '2'}
        factors = []
        for table in tables:
            variables = table[0]
            cardinality = [int(domain[var]) for var in variables]
            values = list(map(float, table[1]))
            factor = DiscreteFactor(variables, cardinality, values)
            factors.append(factor)
        self.markovmodel.add_factors(*factors)
        self.markovwriter = UAIWriter(self.markovmodel)
예제 #14
0
def markovmodel_test():
    """
        >>> from ibeis.algo.hots.pgm_ext import *  # NOQA
    """
    from pgmpy.models import MarkovModel
    from pgmpy.factors import Factor
    markovmodel = MarkovModel([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')])
    factor_a_b = Factor(variables=['A', 'B'], cardinality=[2, 2], values=[100, 5, 5, 100])
    factor_b_c = Factor(variables=['B', 'C'], cardinality=[2, 2], values=[100, 3, 2, 4])
    factor_c_d = Factor(variables=['C', 'D'], cardinality=[2, 2], values=[3, 5, 1, 6])
    factor_d_a = Factor(variables=['D', 'A'], cardinality=[2, 2], values=[6, 2, 56, 2])
    markovmodel.add_factors(factor_a_b, factor_b_c, factor_c_d, factor_d_a)

    pgm_viz.show_markov_model(markovmodel)
    pgm_viz.show_junction_tree(markovmodel)
예제 #15
0
 def __init__(self, pnh, gh):
     '''
     Constructor
     '''
     extractor = pnh.get_data_extractor()
     self.best_model = BayesianModel()
     self.training_instances = ""
     self.device_considered = pnh.get_device()
     self.priority_considered = pnh.get_priority()
     self.markov = MarkovModel()
     self.general_handler = gh
     self.variables_names = extractor.get_variable_names()
     self.rankedDevices = extractor.get_ranked_devices()
     self.data = pnh.get_dataframe()
     self.file_writer = pnh.get_file_writer()
     self.file_suffix = pnh.get_file_suffix()
예제 #16
0
    def get_model(self):
        """
        Returns an instance of Bayesian Model or Markov Model.
        Varibles are in the pattern var_0, var_1, var_2 where var_0 is
        0th index variable, var_1 is 1st index variable.

        Return
        ------
        model: an instance of Bayesian or Markov Model.

        Examples
        --------
        >>> reader = UAIReader('TestUAI.uai')
        >>> reader.get_model()
        """
        if self.network_type == 'BAYES':
            model = BayesianModel()
            model.add_nodes_from(self.variables)
            model.add_edges_from(self.edges)

            tabular_cpds = []
            for cpd in self.tables:
                child_var = cpd[0]
                states = int(self.domain[child_var])
                arr = list(map(float, cpd[1]))
                values = np.array(arr)
                values = values.reshape(states, values.size // states)
                tabular_cpds.append(TabularCPD(child_var, states, values))

            model.add_cpds(*tabular_cpds)
            return model

        elif self.network_type == 'MARKOV':
            model = MarkovModel(self.edges)

            factors = []
            for table in self.tables:
                variables = table[0]
                cardinality = [int(self.domain[var]) for var in variables]
                value = list(map(float, table[1]))
                factor = DiscreteFactor(variables=variables,
                                        cardinality=cardinality,
                                        values=value)
                factors.append(factor)

            model.add_factors(*factors)
            return model
예제 #17
0
 def setUp(self):
     self.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]])
     self.bayesian_model.add_cpds(cpd_a, cpd_g, cpd_j, cpd_l, cpd_q, cpd_r)
     self.sampling_inference = BayesianModelSampling(self.bayesian_model)
     self.markov_model = MarkovModel()
 def setUp(self):
     self.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]])
     self.bayesian_model.add_cpds(cpd_a, cpd_g, cpd_j, cpd_l, cpd_q, cpd_r)
     self.sampling_inference = BayesianModelSampling(self.bayesian_model)
     self.markov_model = MarkovModel()
예제 #19
0
    def setUp(self):
        # It is just a moralised version of the above Bayesian network so all the results are same. Only factors
        # are under consideration for inference so this should be fine.
        self.markov_model = MarkovModel([
            ("A", "J"),
            ("R", "J"),
            ("J", "Q"),
            ("J", "L"),
            ("G", "L"),
            ("A", "R"),
            ("J", "G"),
        ])

        factor_a = TabularCPD("A", 2, values=[[0.2], [0.8]]).to_factor()
        factor_r = TabularCPD("R", 2, values=[[0.4], [0.6]]).to_factor()
        factor_j = TabularCPD(
            "J",
            2,
            values=[[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]],
            evidence=["A", "R"],
            evidence_card=[2, 2],
        ).to_factor()
        factor_q = TabularCPD("Q",
                              2,
                              values=[[0.9, 0.2], [0.1, 0.8]],
                              evidence=["J"],
                              evidence_card=[2]).to_factor()
        factor_l = TabularCPD(
            "L",
            2,
            values=[[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]],
            evidence=["J", "G"],
            evidence_card=[2, 2],
        ).to_factor()
        factor_g = TabularCPD("G", 2, [[0.6], [0.4]]).to_factor()

        self.markov_model.add_factors(factor_a, factor_r, factor_j, factor_q,
                                      factor_l, factor_g)
        self.markov_inference = VariableElimination(self.markov_model)
예제 #20
0
    def to_markov_model(self):
        """
        Converts bayesian model to markov model. The markov model created would
        be the moral graph of the bayesian model.

        Examples
        --------
        >>> from pgmpy.models import BayesianModel
        >>> G = BayesianModel([('diff', 'grade'), ('intel', 'grade'),
        ...                    ('intel', 'SAT'), ('grade', 'letter')])
        >>> mm = G.to_markov_model()
        >>> mm.nodes()
        ['diff', 'grade', 'intel', 'SAT', 'letter']
        >>> mm.edges()
        [('diff', 'intel'), ('diff', 'grade'), ('intel', 'grade'),
        ('intel', 'SAT'), ('grade', 'letter')]
        """
        from pgmpy.models import MarkovModel
        moral_graph = self.moralize()
        mm = MarkovModel(moral_graph.edges())
        mm.add_factors(*[cpd.to_factor() for cpd in self.cpds])

        return mm
예제 #21
0
파일: MLE.py 프로젝트: EJHortala/books-2
    def get_model(self, threshold=0.95):
        if isinstance(self.model, BayesianModel):
            nodes = self.data.columns
            self.model.add_nodes_from(nodes)
            edges = []
            for u, v in combinations(nodes, 2):
                f_exp = self.data.groupby([u, v]).size().values
                u_f_obs = self.data.ix[:, u].value_counts().values
                v_f_obs = self.data.ix[:, v].value_counts().values
                if stats.chisquare(
                        f_obs=[i * j for i in u_f_obs for j in v_f_obs],
                        f_exp=f_exp).pvalue < threshold:
                    edges.append((u, v))

            self.model.add_edges_from(edges)
            return nodes, edges

        elif isinstance(self.model, MarkovModel):
            nodes = self.data.columns
            for node in nodes:
                self.node_card[node] = self.data.ix[:, node].unique().size
            self.model.add_nodes_from(nodes)
            all_possible_edges = list(combinations(nodes, 2))
            optimum = 1000000
            optimal_edges = None
            for edge_comb in chain(*[
                    combinations(all_possible_edges, r)
                    for r in range(1,
                                   len(all_possible_edges) + 1)
            ]):
                self.model = MarkovModel(edge_comb)
                factors, score = self.get_parameters(score=True)
                if score < optimum:
                    optimum = score
                    optimal_edges = edge_comb

            return nodes, edge_comb
예제 #22
0
    def to_markov_model(self):
        """
        Converts the factor graph into markov model.

        A markov model contains nodes as random variables and edge between
        two nodes imply interaction between them.

        Examples
        --------
        >>> from pgmpy.models import FactorGraph
        >>> from pgmpy.factors import Factor
        >>> G = FactorGraph()
        >>> G.add_nodes_from(['a', 'b', 'c'])
        >>> G.add_nodes_from(['phi1', 'phi2'])
        >>> G.add_edges_from([('a', 'phi1'), ('b', 'phi1'),
        ...                   ('b', 'phi2'), ('c', 'phi2')])
        >>> phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        >>> phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        >>> G.add_factors(phi1, phi2)
        >>> mm = G.to_markov_model()
        """
        from pgmpy.models import MarkovModel
        mm = MarkovModel()

        variable_nodes = self.get_variable_nodes()

        if len(set(self.nodes()) - set(variable_nodes)) != len(self.factors):
            raise ValueError(
                'Factors not associated with all the factor nodes.')

        mm.add_nodes_from(variable_nodes)
        for factor in self.factors:
            scope = factor.scope()
            mm.add_edges_from(itertools.combinations(scope, 2))
            mm.add_factors(factor)

        return mm
예제 #23
0
import numpy as np
import pandas as pd
from pgmpy.models import MarkovModel
from pgmpy.estimators import PseudoMomentMatchingEstimator
# Generating some random data
raw_data = np.random.randint(low=0, high=2, size=(100, 4))
raw_data
data = pd.DataFrame(raw_data, columns=['A', 'B', 'C', 'D'])
data

# Diamond shaped Markov Model as stated in Fig. 6.1
markov_model = MarkovModel([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')])
markov_model.fit(data, estimator=PseudoMomentMatchingEstimator)
factors = coin_model.get_factors()
factors
예제 #24
0
 def test_class_init_with_data_nonstring(self):
     self.g = MarkovModel([(1, 2), (2, 3)])
예제 #25
0
import numpy as np
import pandas as pd
from pgmpy.models import MarkovModel
from pgmpy.estimators import MaximumLikelihoodEstimator
# Generating some random data
raw_data = np.random.randint(low=0, high=2, size=(100, 2))
raw_data
data = pd.DataFrame(raw_data, columns=['A', 'B'])
data

# Markov Model as stated in Fig. 6.5
markov_model = MarkovModel([('A', 'B')])
markov_model.fit(data, estimator=MaximumLikelihoodEstimator)
factors = coin_model.get_factors()
print(factors[0])
예제 #26
0
import numpy as np
import pandas as pd
from pgmpy.models import MarkovModel
from pgmpy.estimators import MaximumLikelihoodEstimator
# Generating random data
raw_data = np.random.randint(low=0, high=2, size=(1000, 2))
data = pd.DataFrame(raw_data, columns=['X', 'Y'])
model = MarkovModel()
model.fit(data, estimator=MaximumLikelihoodEstimator)
model.get_factors()
model.nodes()
model.edges()
예제 #27
0
    # Computing the cluster belief according the formula stated
    # above
    cluster_belief = psi * messages_prod_factor
    # As cluster belief represents a probability distribution in
    # this case, thus it should be normalized
    cluster_belief.normalize()
    return cluster_belief


phi_a_b = Factor(['a', 'b'], [2, 2], [10, 0.1, 0.1, 10])
phi_a_c = Factor(['a', 'c'], [2, 2], [5, 0.2, 0.2, 5])
phi_c_d = Factor(['c', 'd'], [2, 2], [0.5, 1, 20, 2.5])
phi_d_b = Factor(['d', 'b'], [2, 2], [5, 0.2, 0.2, 5])

# Cluster 1 is a MarkovModel A--B
cluster_1 = MarkovModel([('a', 'b')])

# Adding factors
cluster_1.add_factors(phi_a_b)

# Cluster 2 is a MarkovModel A--C--D--B
cluster_2 = MarkovModel([('a', 'c'), ('c', 'd'), ('d', 'b')])

# Adding factors
cluster_2.add_factors(phi_a_c, phi_c_d, phi_d_b)

# Message passed from cluster 1 -> 2 should the M-Projection of psi1
# as the sepset of cluster 1 and 2 is A, B thus there is no need to
# marginalize psi1
delta_1_2 = compute_message(cluster_1, cluster_2)
예제 #28
0
def main():
    # maximum size of segments grid cell
    map_size = 50
    # minimum number of pixels to consider segment
    pixels_thresh = 500
    # number of image in the database
    num_images = 70

    # counter for incorrectly classified segments
    num_incorrect = 0
    for idx in range(num_images):
        print('\n\nImage ', idx)

        # read image from disk
        image = cv2.imread('export/image_%04d.jpg' % idx)
        # read ground truth classes
        labels_gt_im = cv2.imread('export/labels_%04d.png' % idx,
                                  cv2.IMREAD_ANYDEPTH).astype(np.int)
        labels_gt_im[labels_gt_im == 65535] = -1
        # read a priori probabilities from classifier
        prob_im = cv2.imread('export/prob_%04d.png' % idx,
                             cv2.IMREAD_ANYDEPTH) / 65535.0
        # read image division into segments
        segments_im = cv2.imread('export/segments_%04d.png' % idx,
                                 cv2.IMREAD_ANYDEPTH)

        # display image
        cv2.imshow('original image', image)
        cv2.waitKey(1)

        # mean R, G, B values for each segment
        mean_rgb = np.zeros([map_size, map_size, 3], dtype=np.float)
        # number of pixels for each segment
        num_pixels = np.zeros([map_size, map_size], dtype=np.uint)
        # a priori probabilities for each segment
        prob = np.zeros([map_size, map_size, 2], dtype=np.float)
        # ground truth classes for each segment
        labels_gt = -1 * np.ones([map_size, map_size], dtype=np.int)
        for y in range(map_size):
            for x in range(map_size):
                # segment identifier
                cur_seg = y * map_size + x
                # indices of pixels belonging to the segment
                cur_pixels = np.nonzero(segments_im == cur_seg)

                if len(cur_pixels[0]) > 0:
                    num_pixels[y, x] = len(cur_pixels[0])
                    # flip because opencv stores images as BGR by default
                    mean_rgb[y,
                             x, :] = np.flip(np.mean(image[cur_pixels],
                                                     axis=0))
                    # average results from the classifier - should not be necessary
                    prob[y, x, 0] = np.mean(prob_im[cur_pixels])
                    prob[y, x, 1] = 1.0 - prob[y, x, 0]
                    # labeling boundaries don't have to be aligned with segments boundaties
                    # count which label is the most common in this segment
                    labels_unique, count = np.unique(labels_gt_im[cur_pixels],
                                                     return_counts=True)
                    labels_gt[y, x] = labels_unique[np.argmax(count)]
                    pass

        # build model
        # PUT YOUR CODE HERE

        nodes = []
        # if segment size is bigger than pixels_thresh, add current segment to node list
        for y in range(map_size):
            for x in range(map_size):
                if num_pixels[y, x] > pixels_thresh:
                    nodes.append("s_" + str(y) + "_" + str(x))

        factors_unary = []
        # if segment size is bigger than pixels_thresh, add unary factor of segment to list
        for y in range(map_size):
            for x in range(map_size):
                if num_pixels[y, x] > pixels_thresh:
                    # print(prob[y, x] * 0.9/1 + 0.05)
                    cur_f = create_factor(["s_" + str(y) + "_" + str(x)],
                                          [[0, 1]], [0.945212], [unary_feat],
                                          [prob[y, x]])
                    factors_unary.append(cur_f)

        factors_pairway = []
        edges_pairway = []
        # if segment size is bigger than pixels_thresh and his neighbour too, add pairway factor to list
        for y in range(map_size - 1):
            for x in range(map_size - 1):
                if num_pixels[y, x] > pixels_thresh:

                    # check neighnour to the right
                    if num_pixels[y, x + 1] > pixels_thresh:
                        cur_f = create_factor([
                            "s_" + str(y) + "_" + str(x),
                            "s_" + str(y) + "_" + str(x + 1)
                        ], [[0, 1], [0, 1]], [1.86891, 1.07741, 1.89271], [
                            pairway_feat_red, pairway_feat_green,
                            pairway_feat_blue
                        ], [mean_rgb[y, x], mean_rgb[y, x + 1]])
                        factors_pairway.append(cur_f)
                        edges_pairway.append(
                            ("s_" + str(y) + "_" + str(x),
                             "s_" + str(y) + "_" + str(x + 1)))
                    # check neighbour under
                    if num_pixels[y + 1, x] > pixels_thresh:
                        cur_f = create_factor([
                            "s_" + str(y) + "_" + str(x),
                            "s_" + str(y + 1) + "_" + str(x)
                        ], [[0, 1], [0, 1]], [1.86891, 1.07741, 1.89271], [
                            pairway_feat_red, pairway_feat_green,
                            pairway_feat_blue
                        ], [mean_rgb[y, x], mean_rgb[y + 1, x]])
                        factors_pairway.append(cur_f)
                        edges_pairway.append(
                            ("s_" + str(y) + "_" + str(x),
                             "s_" + str(y + 1) + "_" + str(x)))

        # use Markov model, because MPLP doesn't support factor graphs
        G = MarkovModel()
        G.add_nodes_from(nodes)  # add nodes
        G.add_factors(*factors_unary)  # add unary factors
        G.add_factors(*factors_pairway)  # add pairway factors
        G.add_edges_from(edges_pairway)  # add edges

        # check if everything is ok
        print("Check model: ", G.check_model())

        # ------------------

        # inferred image pixels
        labels_infer = -1 * np.ones([map_size, map_size], dtype=np.int)

        # read results of the inference
        # PUT YOUR CODE HERE
        mplp_infer = Mplp(G)
        q = mplp_infer.map_query()

        segment_cnt = 0
        for y in range(map_size):
            for x in range(map_size):

                if num_pixels[y, x] > pixels_thresh:
                    val = q["s_" + str(y) + "_" + str(x)]
                    rv = None
                    if val == factors_unary[segment_cnt].values[0]:
                        rv = 0
                    else:
                        rv = 1
                    labels_infer[y, x] = rv

                    segment_cnt += 1

        # ------------------

        labels_class = -1 * np.ones([map_size, map_size], dtype=np.int)

        for y in range(map_size - 1):
            for x in range(map_size - 1):
                if num_pixels[y, x] > pixels_thresh:
                    # label as the class with the highest probability
                    if prob[y, x, 0] >= 0.5:
                        labels_class[y, x] = 0
                    else:
                        labels_class[y, x] = 1

        # count correct pixels
        cnt_corr = np.sum(
            np.logical_and(labels_gt == labels_infer, labels_gt != -1))

        print('Accuracy = ', cnt_corr / np.sum(labels_gt != -1))
        num_incorrect += np.sum(labels_gt != -1) - cnt_corr

        # transfer results for segments onto image
        labels_infer_im = -1 * np.ones_like(labels_gt_im)
        labels_class_im = -1 * np.ones_like(labels_gt_im)
        for y in range(map_size - 1):
            for x in range(map_size - 1):
                if labels_infer[y, x] >= 0:
                    cur_seg = y * map_size + x
                    cur_pixels = np.nonzero(segments_im == cur_seg)

                    labels_infer_im[cur_pixels] = labels_infer[y, x]
                    labels_class_im[cur_pixels] = labels_class[y, x]

        # class 0 - green, class 1 - red in BGR
        colors = np.array([[0, 255, 0], [0, 0, 255]], dtype=np.uint8)

        image_infer = image.copy()
        image_class = image.copy()
        image_gt = image.copy()

        # color pixels according to label
        for l in range(2):
            image_infer[labels_infer_im == l] = colors[l]
            image_class[labels_class_im == l] = colors[l]
            image_gt[labels_gt_im == l] = colors[l]

        # show inferred, classified, and gt image by blending the original image with the colored one
        image_infer_vis = (0.75 * image + 0.25 * image_infer).astype(np.uint8)
        cv2.imshow('inferred', image_infer_vis)

        image_class_vis = (0.75 * image + 0.25 * image_class).astype(np.uint8)
        cv2.imshow('classified', image_class_vis)

        image_gt_vis = (0.75 * image + 0.25 * image_gt).astype(np.uint8)
        cv2.imshow('ground truth', image_gt_vis)

        # uncomment to stop after each image
        # cv2.waitKey()
        cv2.waitKey(10)

    print('Incorrectly inferred ', num_incorrect, ' segments')
예제 #29
0
 def setUp(self):
     self.graph = MarkovModel()
예제 #30
0
# THIS CODE HAS TO BE RUN ON PYTHON 2
# Otherwise, you will get wrong results

from pgmpy.models import MarkovModel
from pgmpy.factors.discrete import DiscreteFactor
from pgmpy.inference import BeliefPropagation
import numpy as np

# Construct a graph
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)