Beispiel #1
0
    def to_factor_graph(self):
        """
        Converts the markov model into factor graph.

        A factor graph contains two types of nodes. One type corresponds to
        random variables whereas the second type corresponds to factors over
        these variables. The graph only contains edges between variables and
        factor nodes. Each factor node is associated with one factor whose
        scope is the set of variables that are its neighbors.

        Examples
        --------
        >>> from pgmpy.models import MarkovModel
        >>> from pgmpy.factors import Factor
        >>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles')])
        >>> factor1 = Factor(['Alice', 'Bob'], [3, 2], np.random.rand(6))
        >>> factor2 = Factor(['Bob', 'Charles'], [2, 2], np.random.rand(4))
        >>> student.add_factors(factor1, factor2)
        >>> factor_graph = student.to_factor_graph()
        """
        from pgmpy.models import FactorGraph
        factor_graph = FactorGraph()

        if not self.factors:
            raise ValueError('Factors not associated with the random variables.')

        factor_graph.add_nodes_from(self.nodes())
        for factor in self.factors:
            scope = factor.scope()
            factor_node = 'phi_' + '_'.join(scope)
            factor_graph.add_edges_from(itertools.product(scope, [factor_node]))
            factor_graph.add_factors(factor)

        return factor_graph
Beispiel #2
0
 def add_new_model(self):
     if self.max_inference_size > 0 or isinstance(
             self.inference, ApproximateSearchInference):
         model = FactorGraph()
         self.ordered_models.append(model)
         return model
     else:
         return None
    def to_factor_graph(self):
        """
        Converts the markov model into factor graph.

        A factor graph contains two types of nodes. One type corresponds to
        random variables whereas the second type corresponds to factors over
        these variables. The graph only contains edges between variables and
        factor nodes. Each factor node is associated with one factor whose
        scope is the set of variables that are its neighbors.

        Examples
        --------
        >>> from pgmpy.models import MarkovModel
        >>> from pgmpy.factors.discrete import DiscreteFactor
        >>> student = MarkovModel([('Alice', 'Bob'), ('Bob', 'Charles')])
        >>> factor1 = DiscreteFactor(['Alice', 'Bob'], [3, 2], np.random.rand(6))
        >>> factor2 = DiscreteFactor(['Bob', 'Charles'], [2, 2], np.random.rand(4))
        >>> student.add_factors(factor1, factor2)
        >>> factor_graph = student.to_factor_graph()
        """
        from pgmpy.models import FactorGraph
        factor_graph = FactorGraph()

        if not self.factors:
            raise ValueError('Factors not associated with the random variables.')

        factor_graph.add_nodes_from(self.nodes())
        for factor in self.factors:
            scope = factor.scope()
            factor_node = 'phi_' + '_'.join(scope)
            factor_graph.add_edges_from(itertools.product(scope, [factor_node]))
            factor_graph.add_factors(factor)

        return factor_graph
Beispiel #4
0
    def reset(self):
        """ Resets variables which need to be updated for each new scenario
        """
        self.model = FactorGraph()

        self.ordered_models = []

        if self.inference_type == InferenceType.SearchInference:
            self.inference = ApproximateSearchInference(
                self.max_beam_size, self.ordered_models)
            # print("Using approximate search inference")
        else:
            self.inference = PGMPYInference(self.model,
                                            inference_type=self.inference_type,
                                            sampling_type=self.sampling_type)

        self.observed = {}

        self.models = []
        self.model_nodes = set()
Beispiel #5
0
def reduce_model(model, evidence):
    model = copy.deepcopy(model)
    # continuous_factors = [factor for factor in model.factors if isinstance(factor, ContinuousFactor)]

    for var, val in evidence.items():
        for factor in model.factors:
            if var in factor.scope(
            ):  # and "F(" in var:  # make sure that we only reduce at this stage for continuous values, let the inference algorithm deal with reducing for binary variables
                try:
                    factor.reduce([(var, val)])
                except ValueError as e:
                    print(factor)
                    raise e

    new_model = FactorGraph()

    additional_evidence = {}

    for node in model.factors:
        if isinstance(node, ContinuousFactor):
            if len(node.scope()) == 1:
                node = TabularCPD(str(node.scope()[0]), 2,
                                  [[node.assignment(0),
                                    node.assignment(1)]]).to_factor()
        if len(node.scope()) == 0:
            continue

        #         try:
        #             var = node.variable
        #         except:
        #             print(node.scope())
        #         for v in node.scope():
        #             if var != v:
        #                 new_model.add_edge(str(v), str(var))

        #         if "same_reason" in var:
        #             additional_evidence[var] = 1

        new_model.add_nodes_from([str(n) for n in node.scope()])
        new_model.add_factors(node)
    return new_model, additional_evidence
Beispiel #6
0
class TestFactorGraphMethods(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_get_cardinality(self):

        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'), ('c', 'phi2'),
                                   ('d', 'phi2'), ('a', 'phi3'),
                                   ('d', 'phi3')])

        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2})
        self.graph.remove_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['c', 'd'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1, phi2)
        self.assertDictEqual(self.graph.get_cardinality(), {
            'd': 2,
            'a': 2,
            'b': 2,
            'c': 1
        })

        phi3 = DiscreteFactor(['d', 'a'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {
            'd': 1,
            'c': 1,
            'b': 2,
            'a': 2
        })

        self.graph.remove_factors(phi1, phi2, phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {})

    def test_get_cardinality_check_cardinality(self):
        self.graph.add_nodes_from(['a', 'b', 'c', 'd'])

        phi1 = DiscreteFactor(['a', 'b'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1)
        self.graph.add_edges_from([('a', phi1), ('b', phi1)])
        self.assertRaises(ValueError,
                          self.graph.get_cardinality,
                          check_cardinality=True)

        phi2 = DiscreteFactor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi2)
        self.graph.add_edges_from([('a', phi2), ('c', phi2)])
        self.assertRaises(ValueError,
                          self.graph.get_cardinality,
                          check_cardinality=True)

        phi3 = DiscreteFactor(['d', 'a'], [1, 1], np.random.rand(1))
        self.graph.add_factors(phi3)
        self.graph.add_edges_from([('d', phi3), ('a', phi3)])
        self.assertDictEqual(
            self.graph.get_cardinality(check_cardinality=True), {
                'd': 1,
                'c': 2,
                'b': 2,
                'a': 1
            })

    def test_get_factor_nodes(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))

        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.get_factor_nodes(), [phi1, phi2])

    def test_get_variable_nodes(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.get_variable_nodes(),
                             ['a', 'b', 'c'])

    def test_get_variable_nodes_raises_error(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'), ('b', 'phi2'),
                                   ('c', 'phi2')])
        self.assertRaises(ValueError, self.graph.get_variable_nodes)

    def test_to_markov_model(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        mm = self.graph.to_markov_model()
        self.assertIsInstance(mm, MarkovModel)
        self.assertListEqual(sorted(mm.nodes()), ['a', 'b', 'c'])
        self.assertListEqual(hf.recursive_sorted(mm.edges()),
                             [['a', 'b'], ['b', 'c']])
        self.assertListEqual(sorted(mm.get_factors(), key=lambda x: x.scope()),
                             [phi1, phi2])

    def test_to_junction_tree(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])

        self.graph.add_factors(phi1, phi2)
        jt = self.graph.to_junction_tree()
        self.assertIsInstance(jt, JunctionTree)
        self.assertListEqual(hf.recursive_sorted(jt.nodes()),
                             [['a', 'b'], ['b', 'c']])
        self.assertEqual(len(jt.edges()), 1)

    def test_check_model(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertTrue(self.graph.check_model())

        self.graph.remove_factors(phi1)
        self.graph.remove_node(phi1)
        phi1 = DiscreteFactor(['a', 'b'], [4, 2], np.random.rand(8))
        self.graph.add_factors(phi1)
        self.graph.add_edges_from([('a', phi1)])
        self.assertTrue(self.graph.check_model())

    def test_check_model1(self):
        self.graph.add_nodes_from(['a', 'b', 'c', 'd'])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_node('d')
        self.assertTrue(self.graph.check_model())

    def test_check_model2(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)

        self.graph.add_edges_from([('a', 'b')])
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.add_edges_from([(phi1, phi2)])
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_edges_from([('a', 'b'), (phi1, phi2)])
        self.assertTrue(self.graph.check_model())

    def test_check_model3(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        phi3 = DiscreteFactor(['a', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2, phi3)
        self.assertRaises(ValueError, self.graph.check_model)
        self.graph.remove_factors(phi3)
        self.assertTrue(self.graph.check_model())

    def test_check_model4(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [3, 2], np.random.rand(6))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_factors(phi2)
        self.graph.remove_node(phi2)
        phi3 = DiscreteFactor(['c', 'a'], [4, 4], np.random.rand(16))
        self.graph.add_factors(phi3)
        self.graph.add_edges_from([('a', phi3), ('c', phi3)])
        self.assertRaises(ValueError, self.graph.check_model)

    def tearDown(self):
        del self.graph
Beispiel #7
0
class TestFactorGraphMethods(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_get_factor_nodes(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        self.assertListEqual(sorted(self.graph.get_factor_nodes()),
                             ['phi1', 'phi2'])

    def test_get_variable_nodes(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        self.assertListEqual(sorted(self.graph.get_variable_nodes()),
                             ['a', 'b', 'c'])

    def test_get_variable_nodes_raises_error(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'),
                                   ('b', 'phi2'), ('c', 'phi2')])
        self.assertRaises(ValueError, self.graph.get_variable_nodes)

    def test_to_markov_model(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        mm = self.graph.to_markov_model()
        self.assertIsInstance(mm, MarkovModel)
        self.assertListEqual(sorted(mm.nodes()), ['a', 'b', 'c'])
        self.assertListEqual(hf.recursive_sorted(mm.edges()),
                             [['a', 'b'], ['b', 'c']])
        self.assertListEqual(sorted(mm.get_factors(),
                             key=lambda x: x.scope()), [phi1, phi2])

    def test_to_junction_tree(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        jt = self.graph.to_junction_tree()
        self.assertIsInstance(jt, JunctionTree)
        self.assertListEqual(hf.recursive_sorted(jt.nodes()),
                             [['a', 'b'], ['b', 'c']])
        self.assertEqual(len(jt.edges()), 1)

    def tearDown(self):
        del self.graph
Beispiel #8
0
class TestFactorGraphFactorOperations(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_add_single_factor(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        self.assertListEqual(self.graph.get_factors(), [phi1])

    def test_add_multiple_factors(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_factors(node='phi1'), phi1)
        self.assertEqual(self.graph.get_factors(node='phi2'), phi2)

    def test_remove_factors(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        self.graph.remove_factors(phi1)
        self.assertListEqual(self.graph.get_factors(), [phi2])

    def test_get_partition_function(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'),
                                   ('b', 'phi2'), ('c', 'phi2')])
        phi1 = Factor(['a', 'b'], [2, 2], range(4))
        phi2 = Factor(['b', 'c'], [2, 2], range(4))
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_partition_function(), 22.0)

    def tearDown(self):
        del self.graph
Beispiel #9
0
 def test_class_init_data_string(self):
     self.graph = FactorGraph([('a', 'phi1'), ('b', 'phi1')])
     self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
     self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                          [['a', 'phi1'], ['b', 'phi1']])
Beispiel #10
0
class TestFactorGraphMethods(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_get_cardinality(self):

        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'),
                                   ('c', 'phi2'), ('d', 'phi2'),
                                   ('a', 'phi3'), ('d', 'phi3')])

        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2})
        self.graph.remove_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['c', 'd'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1, phi2)
        self.assertDictEqual(self.graph.get_cardinality(), {'d': 2, 'a': 2, 'b': 2, 'c': 1})

        phi3 = Factor(['d', 'a'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {'d': 1, 'c': 1, 'b': 2, 'a': 2})

        self.graph.remove_factors(phi1, phi2, phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {})

    
    def test_get_cardinality_check_cardinality(self):

        self.graph.add_nodes_from(['a', 'b', 'c', 'd'])
        
        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1)
        self.graph.add_edges_from([('a', phi1), ('b', phi1)])
        self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True)

        phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi2)
        self.graph.add_edges_from([('a', phi2), ('c', phi2)])
        self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True)

        phi3 = Factor(['d', 'a'], [1, 1], np.random.rand(1))
        self.graph.add_factors(phi3)
        self.graph.add_edges_from([('d', phi3), ('a', phi3)])
        self.assertDictEqual(self.graph.get_cardinality(check_cardinality=True), {'d': 1, 'c': 2, 'b': 2, 'a': 1})

    def test_get_factor_nodes(self):
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))

        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.get_factor_nodes(), [phi1, phi2])

    def test_get_variable_nodes(self):
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.get_variable_nodes(), ['a', 'b', 'c'])

    def test_get_variable_nodes_raises_error(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'),
                                   ('b', 'phi2'), ('c', 'phi2')])
        self.assertRaises(ValueError, self.graph.get_variable_nodes)

    def test_to_markov_model(self):
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        mm = self.graph.to_markov_model()
        self.assertIsInstance(mm, MarkovModel)
        self.assertListEqual(sorted(mm.nodes()), ['a', 'b', 'c'])
        self.assertListEqual(hf.recursive_sorted(mm.edges()),
                             [['a', 'b'], ['b', 'c']])
        self.assertListEqual(sorted(mm.get_factors(),
                             key=lambda x: x.scope()), [phi1, phi2])

    def test_to_junction_tree(self):
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])
        
        self.graph.add_factors(phi1, phi2)
        jt = self.graph.to_junction_tree()
        self.assertIsInstance(jt, JunctionTree)
        self.assertListEqual(hf.recursive_sorted(jt.nodes()),
                             [['a', 'b'], ['b', 'c']])
        self.assertEqual(len(jt.edges()), 1)

    def test_check_model(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), 
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertTrue(self.graph.check_model())

        self.graph.remove_factors(phi1)
        self.graph.remove_node(phi1)
        phi1 = Factor(['a', 'b'], [4, 2], np.random.rand(8))
        self.graph.add_factors(phi1)
        self.graph.add_edges_from([('a', phi1)])
        self.assertTrue(self.graph.check_model())

    def test_check_model1(self):
        self.graph.add_nodes_from(['a', 'b', 'c', 'd'])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), 
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_node('d')
        self.assertTrue(self.graph.check_model())

    def test_check_model2(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), 
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)

        self.graph.add_edges_from([('a', 'b')])
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.add_edges_from([(phi1, phi2)])
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_edges_from([('a', 'b'), (phi1, phi2)])
        self.assertTrue(self.graph.check_model())

    def test_check_model3(self):
        
        
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        phi3 = Factor(['a', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), 
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2, phi3)
        self.assertRaises(ValueError, self.graph.check_model)
        self.graph.remove_factors(phi3)
        self.assertTrue(self.graph.check_model())

    def test_check_model4(self):
        self.graph.add_nodes_from(['a', 'b', 'c'])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [3, 2], np.random.rand(6))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([('a', phi1), ('b', phi1), 
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_factors(phi2)
        self.graph.remove_node(phi2)
        phi3 = Factor(['c', 'a'], [4, 4], np.random.rand(16))
        self.graph.add_factors(phi3)
        self.graph.add_edges_from([('a', phi3), ('c', phi3)])
        self.assertRaises(ValueError, self.graph.check_model)

    def tearDown(self):
        del self.graph
Beispiel #11
0
class TestFactorGraphFactorOperations(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_add_single_factor(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        six.assertCountEqual(self, self.graph.factors, [phi1])

    def test_add_multiple_factors(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.factors, [phi1, phi2])

    def test_get_factors(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])

        six.assertCountEqual(self, self.graph.get_factors(), [])

        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_factors(node=phi1), phi1)
        self.assertEqual(self.graph.get_factors(node=phi2), phi2)
        six.assertCountEqual(self, self.graph.get_factors(), [phi1, phi2])

        self.graph.remove_factors(phi1)
        self.assertRaises(ValueError, self.graph.get_factors, node=phi1)

    def test_remove_factors(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1'), ('b', 'phi2'),
                                   ('c', 'phi2')])
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1, phi2)
        self.graph.remove_factors(phi1)
        six.assertCountEqual(self, self.graph.factors, [phi2])

    def test_get_partition_function(self):
        phi1 = DiscreteFactor(['a', 'b'], [2, 2], range(4))
        phi2 = DiscreteFactor(['b', 'c'], [2, 2], range(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1), ('b', phi2),
                                   ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_partition_function(), 22.0)

    def tearDown(self):
        del self.graph
# First import FactorGraph class from pgmpy.models
from pgmpy.models import FactorGraph
factor_graph = FactorGraph()
# Add nodes (both variable nodes and factor nodes) to the model
# as we did in previous other models
factor_graph.add_nodes_from(['A', 'B', 'C', 'D', 'phi1', 'phi2', 'phi3'])
# Add edges between all variable nodes and factor nodes
factor_graph.add_edges_from([('A', 'phi1'), ('B', 'phi1'),
                             ('B', 'phi2'), ('C', 'phi2'),
                             ('C', 'phi3'), ('A', 'phi3')])
Beispiel #13
0
# First import FactorGraph class from pgmpy.models
from pgmpy.models import FactorGraph
factor_graph = FactorGraph()
# Add nodes (both variable nodes and factor nodes) to the model
# as we did in previous other models
factor_graph.add_nodes_from(['A', 'B', 'C', 'D', 'phi1', 'phi2', 'phi3'])
# Add edges between all variable nodes and factor nodes
factor_graph.add_edges_from([('A', 'phi1'), ('B', 'phi1'), ('B', 'phi2'),
                             ('C', 'phi2'), ('C', 'phi3'), ('A', 'phi3')])
Beispiel #14
0
class TestFactorGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_class_init_without_data(self):
        self.assertIsInstance(self.graph, FactorGraph)

    def test_class_init_data_string(self):
        self.graph = FactorGraph([("a", "phi1"), ("b", "phi1")])
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])
        self.assertListEqual(
            hf.recursive_sorted(self.graph.edges()), [["a", "phi1"], ["b", "phi1"]]
        )

    def test_add_single_node(self):
        self.graph.add_node("phi1")
        self.assertEqual(list(self.graph.nodes()), ["phi1"])

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from(["a", "b", "phi1"])
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])

    def test_add_single_edge(self):
        self.graph.add_edge("a", "phi1")
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "phi1"])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()), [["a", "phi1"]])

    def test_add_multiple_edges(self):
        self.graph.add_edges_from([("a", "phi1"), ("b", "phi1")])
        self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])
        self.assertListEqual(
            hf.recursive_sorted(self.graph.edges()), [["a", "phi1"], ["b", "phi1"]]
        )

    def test_add_self_loop_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, "a", "a")

    def tearDown(self):
        del self.graph
Beispiel #15
0
class PGMModel(object):
    def __init__(self,
                 inference_type=InferenceType.SearchInference,
                 sampling_type=SamplingType.LikelihoodWeighted,
                 max_inference_size=-1,
                 max_beam_size=0):
        self.inference_type = inference_type
        self.sampling_type = sampling_type
        self.max_inference_size = max_inference_size
        self.max_beam_size = max_beam_size
        self.reset()

    def reset(self):
        """ Resets variables which need to be updated for each new scenario
        """
        self.model = FactorGraph()

        self.ordered_models = []

        if self.inference_type == InferenceType.SearchInference:
            self.inference = ApproximateSearchInference(
                self.max_beam_size, self.ordered_models)
            # print("Using approximate search inference")
        else:
            self.inference = PGMPYInference(self.model,
                                            inference_type=self.inference_type,
                                            sampling_type=self.sampling_type)

        self.observed = {}

        self.models = []
        self.model_nodes = set()

    def add_new_model(self):
        if self.max_inference_size > 0 or isinstance(
                self.inference, ApproximateSearchInference):
            model = FactorGraph()
            self.ordered_models.append(model)
            return model
        else:
            return None

    def get_model_scopes(self):
        scope = set()
        for model in self.models:
            scope = scope.union(get_scope(model))
        return scope

    def get_nodes(self):
        nodes = set()
        for model in self.models:
            nodes = nodes.union(model.nodes())
        return nodes

    def test_models(self):
        for model in self.models:
            variable_nodes = set(
                [x for factor in model.factors for x in factor.scope()])
            factor_nodes = set(model.nodes()) - variable_nodes

            assert (len(factor_nodes) == len(model.factors))

    def reduce_models(self):

        for i, model1 in enumerate(self.models):
            for j, model2 in enumerate(self.models):
                if i != j:
                    if is_scope_overlap(model1, model2):
                        new_combined = combine_models(model1, model2)
                        self.models.remove(model1)
                        self.models.remove(model2)
                        new_combined.factors = list(set(new_combined.factors))
                        self.models.append(new_combined)

                        return self.reduce_models()

    def add_factor(self, nodes, factor, model=None):

        if model is not None:
            self.model_nodes.add(factor)
            new_model = create_new_model(nodes, factor)
            combine_models(model, new_model)

        if self.inference_type != InferenceType.SearchInference:

            if any([
                    factors_are_equal(factor, factor2)
                    for factor2 in self.model_nodes
            ]):
                return

            self.model_nodes.add(factor)

            new_model = create_new_model(nodes, factor)
            combined = False

            for model in self.models:
                if is_scope_overlap(model, new_model):
                    updated_model = combine_models(model, new_model)
                    model.factors = list(set(model.factors))
                    self.reduce_models()
                    combined = True
            if not combined:
                self.models.append(new_model)
        else:
            node_filter = lambda x: filter(
                lambda y: y not in self.model.nodes(), x)
            self.model.add_nodes_from(
                node_filter(set([str(n) for n in nodes] + [str(factor)])))
            self.model.add_factors(factor)

    def observe(self, observable=None):

        if self.inference_type == InferenceType.SearchInference:
            start = time.time()
            self.inference.infer(self.observed, observable)
            self.observed.update(observable)
            end = time.time()
            return end - start
        else:
            self.observed.update(observable)

    def query(self, variables, values=None):

        if self.inference_type == InferenceType.SearchInference:
            try:
                return self.inference.query(variables, values=values)
            except IndexError:
                raise InferenceFailedError(
                    "Impossible to find valid inference for given evidence")

        elif self.max_inference_size > 0:

            reverse_models = defaultdict(list)
            for variable in variables:
                latest_relevant_model = [
                    model for model in self.ordered_models
                    if variable in get_scope(model)
                ][-1]

                reverse_models[latest_relevant_model].append(variable)

            output = {}

            for model, variables in reverse_models.items():
                relevant_models = [model]
                for i in range(self.max_inference_size - 1):
                    try:
                        next_relevant_model = [
                            m for m in self.ordered_models
                            if is_relevant(m, relevant_models)
                        ][-1]
                        relevant_models.append(next_relevant_model)
                    except IndexError:
                        break

                inference_model = build_combined_model(relevant_models)
                inference = PGMPYInference(inference_model,
                                           inference_type=self.inference_type,
                                           sampling_type=self.sampling_type)
                inference.infer({}, self.observed)

                output.update(inference.query(variables, values=values))
            return output

        else:
            output = {}
            for model in self.models:
                if any(
                    [variable in get_scope(model) for variable in variables]):
                    inference = PGMPYInference(
                        model,
                        inference_type=self.inference_type,
                        sampling_type=self.sampling_type)
                    inference.infer({}, self.observed)

                    output.update(inference.query(variables, values=values))

            return output
Beispiel #16
0
def build_combined_model(relevant_models):
    new_model = FactorGraph()
    for model in relevant_models:
        combine_models(new_model, model)
    return new_model
Beispiel #17
0
def create_new_model(nodes, factor):
    new_model = FactorGraph()
    new_model.add_nodes_from([str(n) for n in nodes] + [str(factor)])
    new_model.add_factors(factor)
    return new_model
Beispiel #18
0
class TestFactorGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_class_init_without_data(self):
        self.assertIsInstance(self.graph, FactorGraph)

    def test_class_init_data_string(self):
        self.graph = FactorGraph([('a', 'phi1'), ('b', 'phi1')])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1'], ['b', 'phi1']])

    def test_add_single_node(self):
        self.graph.add_node('phi1')
        self.assertEqual(self.graph.nodes(), ['phi1'])

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from(['a', 'b', 'phi1'])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])

    def test_add_single_edge(self):
        self.graph.add_edge('a', 'phi1')
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1']])

    def test_add_multiple_edges(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1'], ['b', 'phi1']])

    def test_add_self_loop_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, 'a', 'a')

    def tearDown(self):
        del self.graph
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()
Beispiel #20
0
 def test_class_init_data_string(self):
     self.graph = FactorGraph([('a', 'phi1'), ('b', 'phi1')])
     self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
     self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                          [['a', 'phi1'], ['b', 'phi1']])
Beispiel #21
0
 def setUp(self):
     self.graph = FactorGraph()
Beispiel #22
0
 def setUp(self):
     self.graph = FactorGraph()
Beispiel #23
0
class TestFactorGraphMethods(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_get_cardinality(self):
        self.graph.add_edges_from(
            [
                ("a", "phi1"),
                ("b", "phi1"),
                ("c", "phi2"),
                ("d", "phi2"),
                ("a", "phi3"),
                ("d", "phi3"),
            ]
        )

        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = DiscreteFactor(["a", "b"], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {"a": 1, "b": 2})
        self.graph.remove_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["c", "d"], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1, phi2)
        self.assertDictEqual(
            self.graph.get_cardinality(), {"d": 2, "a": 2, "b": 2, "c": 1}
        )

        phi3 = DiscreteFactor(["d", "a"], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi3)
        self.assertDictEqual(
            self.graph.get_cardinality(), {"d": 1, "c": 1, "b": 2, "a": 2}
        )

        self.graph.remove_factors(phi1, phi2, phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {})

    def test_get_cardinality_with_node(self):
        self.graph.add_nodes_from(["a", "b", "c"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_cardinality("a"), 2)
        self.assertEqual(self.graph.get_cardinality("b"), 2)
        self.assertEqual(self.graph.get_cardinality("c"), 2)

    def test_get_factor_nodes(self):
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))

        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.get_factor_nodes(), [phi1, phi2])

    def test_get_variable_nodes(self):
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.get_variable_nodes(), ["a", "b", "c"])

    def test_get_variable_nodes_raises_error(self):
        self.graph.add_edges_from(
            [("a", "phi1"), ("b", "phi1"), ("b", "phi2"), ("c", "phi2")]
        )
        self.assertRaises(ValueError, self.graph.get_variable_nodes)

    def test_to_markov_model(self):
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        mm = self.graph.to_markov_model()
        self.assertIsInstance(mm, MarkovModel)
        self.assertListEqual(sorted(mm.nodes()), ["a", "b", "c"])
        self.assertListEqual(hf.recursive_sorted(mm.edges()), [["a", "b"], ["b", "c"]])
        self.assertListEqual(
            sorted(mm.get_factors(), key=lambda x: x.scope()), [phi1, phi2]
        )

    def test_to_junction_tree(self):
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])

        self.graph.add_factors(phi1, phi2)
        jt = self.graph.to_junction_tree()
        self.assertIsInstance(jt, JunctionTree)
        self.assertListEqual(hf.recursive_sorted(jt.nodes()), [["a", "b"], ["b", "c"]])
        self.assertEqual(len(jt.edges()), 1)

    def test_check_model(self):
        self.graph.add_nodes_from(["a", "b", "c"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertTrue(self.graph.check_model())

        self.graph.remove_factors(phi1)
        self.graph.remove_node(phi1)
        phi1 = DiscreteFactor(["a", "b"], [4, 2], np.random.rand(8))
        self.graph.add_factors(phi1)
        self.graph.add_edges_from([("a", phi1)])
        self.assertTrue(self.graph.check_model())

    def test_check_model1(self):
        self.graph.add_nodes_from(["a", "b", "c", "d"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_node("d")
        self.assertTrue(self.graph.check_model())

    def test_check_model2(self):
        self.graph.add_nodes_from(["a", "b", "c"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)

        self.graph.add_edges_from([("a", "b")])
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.add_edges_from([(phi1, phi2)])
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_edges_from([("a", "b"), (phi1, phi2)])
        self.assertTrue(self.graph.check_model())

    def test_check_model3(self):
        self.graph.add_nodes_from(["a", "b", "c"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        phi3 = DiscreteFactor(["a", "c"], [2, 2], np.random.rand(4))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2, phi3)
        self.assertRaises(ValueError, self.graph.check_model)
        self.graph.remove_factors(phi3)
        self.assertTrue(self.graph.check_model())

    def test_check_model4(self):
        self.graph.add_nodes_from(["a", "b", "c"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [3, 2], np.random.rand(6))
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertRaises(ValueError, self.graph.check_model)

        self.graph.remove_factors(phi2)
        self.graph.remove_node(phi2)
        phi3 = DiscreteFactor(["c", "a"], [4, 4], np.random.rand(16))
        self.graph.add_factors(phi3)
        self.graph.add_edges_from([("a", phi3), ("c", phi3)])
        self.assertRaises(ValueError, self.graph.check_model)

    def test_copy(self):
        self.graph.add_nodes_from(["a", "b", "c"])
        phi1 = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        phi2 = DiscreteFactor(["b", "c"], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1, phi2)
        self.graph.add_nodes_from([phi1, phi2])
        self.graph.add_edges_from([("a", phi1), ("b", phi1), ("b", phi2), ("c", phi2)])
        graph_copy = self.graph.copy()
        self.assertIsInstance(graph_copy, FactorGraph)
        self.assertTrue(graph_copy.check_model())
        self.assertEqual(self.graph.get_factors(), graph_copy.get_factors())
        self.graph.remove_factors(phi1, phi2)
        self.assertTrue(
            phi1 not in self.graph.factors and phi2 not in self.graph.factors
        )
        self.assertTrue(phi1 in graph_copy.factors and phi2 in graph_copy.factors)
        self.graph.add_factors(phi1, phi2)
        self.graph.factors[0] = DiscreteFactor(["a", "b"], [2, 2], np.random.rand(4))
        self.assertNotEqual(self.graph.get_factors()[0], graph_copy.get_factors()[0])
        self.assertNotEqual(self.graph.factors, graph_copy.factors)

    def tearDown(self):
        del self.graph
Beispiel #24
0
class TestFactorGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_class_init_without_data(self):
        self.assertIsInstance(self.graph, FactorGraph)

    def test_class_init_data_string(self):
        self.graph = FactorGraph([('a', 'phi1'), ('b', 'phi1')])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1'], ['b', 'phi1']])

    def test_add_single_node(self):
        self.graph.add_node('phi1')
        self.assertEqual(self.graph.nodes(), ['phi1'])

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from(['a', 'b', 'phi1'])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])

    def test_add_single_edge(self):
        self.graph.add_edge('a', 'phi1')
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1']])

    def test_add_multiple_edges(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'phi1'])
        self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
                             [['a', 'phi1'], ['b', 'phi1']])

    def test_add_self_loop_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, 'a', 'a')

    def tearDown(self):
        del self.graph
Beispiel #25
0
# Cluster Graph -- "Cluster Graph"

########################################################
#
#              ________   _______   ________
#              |__f1__|---|__A__|---|__f3__|
#                  |                    |     
#                  |                    |
#               ___|___   ________   ___|___
#               |__B__|---|__f2__|---|__C__|
#             
#               
########################################################
# First import factor graph class from pgmpy.models
from pgmpy.models import FactorGraph
factor_graph = FactorGraph()

# Add variable nodes and factor nodes to model
factor_graph.add_nodes_from(['A','B','C','D','phi1','phi2','phi3'])

# Add edges between all nodes
factor_graph.add_edges_from([('A','phi1'), ('B','phi1'),
	                         ('B','phi2'), ('C','phi2'),
	                         ('C','phi3'), ('A','phi3')])


# Add factors into phi1, phi2, phi3
from pgmpy.factors import Factor
import numpy as np
phi1 = Factor(['A','B'], [2,2], np.random.rand(4))
phi2 = Factor(['A','B'], [2,2], np.random.rand(4))
Beispiel #26
0
class TestFactorGraphFactorOperations(unittest.TestCase):
    def setUp(self):
        self.graph = FactorGraph()

    def test_add_single_factor(self):
        self.graph.add_edges_from([('a', 'phi1'), ('b', 'phi1')])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        six.assertCountEqual(self, self.graph.factors, [phi1])

    def test_add_multiple_factors(self):
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        six.assertCountEqual(self, self.graph.factors, [phi1, phi2])
    
    def test_get_factors(self):
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])

        six.assertCountEqual(self, self.graph.get_factors(), [])

        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_factors(node=phi1), phi1)
        self.assertEqual(self.graph.get_factors(node=phi2), phi2)
        six.assertCountEqual(self, self.graph.get_factors(), [phi1, phi2])

        self.graph.remove_factors(phi1)
        self.assertRaises(ValueError, self.graph.get_factors, node=phi1)


    def test_remove_factors(self):
        self.graph.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))
        self.graph.add_factors(phi1, phi2)
        self.graph.remove_factors(phi1)
        six.assertCountEqual(self, self.graph.factors, [phi2])

    def test_get_partition_function(self):
        phi1 = Factor(['a', 'b'], [2, 2], range(4))
        phi2 = Factor(['b', 'c'], [2, 2], range(4))
        self.graph.add_edges_from([('a', phi1), ('b', phi1),
                                   ('b', phi2), ('c', phi2)])
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_partition_function(), 22.0)

    def tearDown(self):
        del self.graph
Beispiel #27
0
 def test_class_init_data_string(self):
     self.graph = FactorGraph([("a", "phi1"), ("b", "phi1")])
     self.assertListEqual(sorted(self.graph.nodes()), ["a", "b", "phi1"])
     self.assertListEqual(
         hf.recursive_sorted(self.graph.edges()), [["a", "phi1"], ["b", "phi1"]]
     )
Beispiel #28
0
    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]
Beispiel #29
0
def create_model():
    # Init graph.
    G = FactorGraph()

    # Add variable nodes to model.
    G.add_nodes_from(v)

    # Add factor nodes to model.
    G.add_nodes_from(f)

    # Add edges to the model.
    edges = [('X1', f1), ('X2', f2),    \
             ('X3', f31), ('X1', f31),  \
             ('X5', f52), ('X2', f52),  \
             ('X4', f42), ('X2', f42),  \
             ('X6', f68), ('X8', f68),
             ('X8', f843), ('X4', f843), ('X3', f843),
             ('X7', f758), ('X5', f758), ('X8', f758)]
    G.add_edges_from(edges)

    # Finally add all factors.
    G.add_factors(f1, f2, f31, f52, f42, f68, f843, f758)

    assert (G.check_model())
    return G