Exemple #1
0
    def test_factor_product2(self):
        from pgmpy import factors
        phi = factors.Factor(['x1', 'x2'], [2, 2], range(4))
        phi1 = factors.Factor(['x3', 'x4'], [2, 2], range(4))
        prod = phi.product(phi1)
        np_test.assert_array_equal(
            prod.values,
            np.array([0, 0, 0, 0, 0, 1, 2, 3, 0, 2, 4, 6, 0, 3, 6, 9]))
        self.assertEqual(
            prod.variables,
            OrderedDict([('x1', [State('x1', 0),
                                 State('x1', 1)]),
                         ('x2', [State('x2', 0),
                                 State('x2', 1)]),
                         ('x3', [State('x3', 0),
                                 State('x3', 1)]),
                         ('x4', [State('x4', 0),
                                 State('x4', 1)])]))

        phi = Factor(['x1', 'x2'], [3, 2], range(6))
        phi1 = Factor(['x2', 'x3'], [2, 2], range(4))
        prod = phi.product(phi1)
        np_test.assert_array_equal(
            prod.values, np.array([0, 0, 2, 3, 0, 2, 6, 9, 0, 4, 10, 15]))
        self.assertEqual(
            prod.variables,
            OrderedDict([('x1',
                          [State('x1', 0),
                           State('x1', 1),
                           State('x1', 2)]),
                         ('x2', [State('x2', 0),
                                 State('x2', 1)]),
                         ('x3', [State('x3', 0),
                                 State('x3', 1)])]))
Exemple #2
0
    def test_factor_product2(self):
        from pgmpy import factors
        phi = factors.Factor(['x1', 'x2'], [2, 2], range(4))
        phi1 = factors.Factor(['x3', 'x4'], [2, 2], range(4))
        prod = phi.product(phi1)
        np_test.assert_array_equal(prod.values,
                                   np.array([0, 0, 0, 0, 0, 1,
                                             2, 3, 0, 2, 4, 6,
                                             0, 3, 6, 9]))
        self.assertEqual(prod.variables, OrderedDict([
            ('x1', [State('x1', 0), State('x1', 1)]),
            ('x2', [State('x2', 0), State('x2', 1)]),
            ('x3', [State('x3', 0), State('x3', 1)]),
            ('x4', [State('x4', 0), State('x4', 1)])]
        ))

        phi = Factor(['x1', 'x2'], [3, 2], range(6))
        phi1 = Factor(['x2', 'x3'], [2, 2], range(4))
        prod = phi.product(phi1)
        np_test.assert_array_equal(prod.values,
                                   np.array([0, 0, 2, 3, 0, 2, 6, 9, 0, 4, 10, 15]))
        self.assertEqual(prod.variables, OrderedDict(
            [('x1', [State('x1', 0), State('x1', 1), State('x1', 2)]),
             ('x2', [State('x2', 0), State('x2', 1)]),
             ('x3', [State('x3', 0), State('x3', 1)])]))
Exemple #3
0
    def test_factor_product2(self):
        from pgmpy import factors
        phi = factors.Factor(['x1', 'x2'], [2, 2], range(4))
        phi1 = factors.Factor(['x3', 'x4'], [2, 2], range(4))
        prod = phi.product(phi1, inplace=False)
        expected_factor = Factor(['x1', 'x2', 'x3', 'x4'], [2, 2, 2, 2],
                                 [0, 0, 0, 0, 0, 1, 2, 3, 0, 2, 4, 6, 0, 3, 6, 9])
        self.assertEqual(prod, expected_factor)
        self.assertEqual(sorted(prod.variables), ['x1', 'x2', 'x3', 'x4'])

        phi = Factor(['x1', 'x2'], [3, 2], range(6))
        phi1 = Factor(['x2', 'x3'], [2, 2], range(4))
        prod = phi.product(phi1, inplace=False)
        expected_factor = Factor(['x1', 'x2', 'x3'], [3, 2, 2],
                                 [0, 0, 2, 3, 0, 2, 6, 9, 0, 4, 10, 15])
        self.assertEqual(prod, expected_factor)
        self.assertEqual(sorted(prod.variables), ['x1', 'x2', 'x3'])
Exemple #4
0
factor2_reduced = factor2.reduce([('B', 0), ('C', 1)], inplace=False)
print(factor2_reduced)
"  a      factor2(a)    "
"  A_0      ###         "
"  A_1      ###         "
factor2_reduced.scope()
" ['A'] "

-3-
# --- Factor production
factor1 = Factor(['A', 'B'], [2, 2], [1000, 1, 5, 100])
factor2 = Factor(['B', 'C'], [2, 3], [10, 30, 5, 120, 1500, 90])

factor_product = factor1 * factor2
" or "
factor_product = factor1.product(factor2)
factor_product.scope()
" ['A', 'B', 'C'] "
print(factor_product)
"  a    b    c      factor_product(a,b,c)  "
"  A_0  B_0  C_0    1000                   "
" .......................................  "

-4-
# --- factor Division
" f(a,b) / f(b) "
factor1 = Factor(['a','b'], [2,3], range(6))
factor2 = Factor(['b'], [3], range(3))

factor_divid = factor1 / factor2