Ejemplo n.º 1
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
Ejemplo n.º 2
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
    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
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 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
Ejemplo n.º 9
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
Ejemplo n.º 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 = 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
Ejemplo n.º 11
0
# 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))
phi3 = Factor(['A','B'], [2,2], np.random.rand(4))
factor_graph.add_factors(phi1, phi2, phi3)






# Cluster Graph -- Converting Markov model into a factor graph
from pgmpy.models import MarkovModel
mm = MarkovModel()
mm.add_nodes_from(['A','B','C'])
mm.add_edges_from([('A','B'),('B','C'),('C','A')])
mm.add_factors(phi1, phi2, phi3)

factor_graph_from_mm = mm.to_factor_graph()
" Factor nodes after conversions will be automatically added to form 'phi1_node1_node2...' "
Ejemplo n.º 12
0
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()
Ejemplo n.º 13
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
Ejemplo n.º 14
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
Ejemplo n.º 15
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