Example #1
0
    def test_init_multi_variable_nodes(self):
        tree = TreeCPD([(('B', 'C'), Factor(['A'], [2], [0.8, 0.2]), (0, 0)),
                        (('B', 'C'), 'D', (0, 1)),
                        (('B', 'C'), Factor(['A'], [2], [0.1, 0.9]), (1, 0)),
                        (('B', 'C'), 'E', (1, 1)),
                        ('D', Factor(['A'], [2], [0.9, 0.1]), 0),
                        ('D', Factor(['A'], [2], [0.4, 0.6]), 1),
                        ('E', Factor(['A'], [2], [0.3, 0.7]), 0),
                        ('E', Factor(['A'], [2], [0.8, 0.2]), 1)
                        ])

        self.assertTrue(('B', 'C') in tree.nodes())
        self.assertTrue('D' in tree.nodes())
        self.assertTrue('E' in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.8, 0.2]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.9, 0.1]) in tree.nodes())

        self.assertTrue((('B', 'C'), Factor(['A'], [2], [0.8, 0.2]) in tree.edges()))
        self.assertTrue((('B', 'C'), 'E') in tree.edges())
        self.assertTrue(('D', Factor(['A'], [2], [0.4, 0.6])) in tree.edges())
        self.assertTrue(('E', Factor(['A'], [2], [0.8, 0.2])) in tree.edges())

        self.assertEqual(tree[('B', 'C')][Factor(['A'], [2], [0.8, 0.2])]['label'], (0, 0))
        self.assertEqual(tree[('B', 'C')]['D']['label'], (0, 1))
        self.assertEqual(tree['D'][Factor(['A'], [2], [0.9, 0.1])]['label'], 0)
        self.assertEqual(tree['E'][Factor(['A'], [2], [0.3, 0.7])]['label'], 0)
Example #2
0
    def test_init_single_variable_nodes(self):
        tree = TreeCPD([('B', Factor(['A'], [2], [0.8, 0.2]), 0),
                        ('B', 'C', 1),
                        ('C', Factor(['A'], [2], [0.1, 0.9]), 0),
                        ('C', 'D', 1),
                        ('D', Factor(['A'], [2], [0.9, 0.1]), 0),
                        ('D', Factor(['A'], [2], [0.4, 0.6]), 1)])

        self.assertTrue('B' in tree.nodes())
        self.assertTrue('C' in tree.nodes())
        self.assertTrue('D' in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.8, 0.2]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.1, 0.9]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.9, 0.1]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.4, 0.6]) in tree.nodes())

        self.assertTrue(('B', Factor(['A'], [2], [0.8, 0.2]) in tree.edges()))
        self.assertTrue(('B', Factor(['A'], [2], [0.1, 0.9]) in tree.edges()))
        self.assertTrue(('B', Factor(['A'], [2], [0.9, 0.1]) in tree.edges()))
        self.assertTrue(('B', Factor(['A'], [2], [0.4, 0.6]) in tree.edges()))
        self.assertTrue(('C', 'D') in tree.edges())
        self.assertTrue(('B', 'C') in tree.edges())

        self.assertEqual(tree['B'][Factor(['A'], [2], [0.8, 0.2])]['label'], 0)
        self.assertEqual(tree['B']['C']['label'], 1)
        self.assertEqual(tree['C'][Factor(['A'], [2], [0.1, 0.9])]['label'], 0)
        self.assertEqual(tree['C']['D']['label'], 1)
        self.assertEqual(tree['D'][Factor(['A'], [2], [0.9, 0.1])]['label'], 0)
        self.assertEqual(tree['D'][Factor(['A'], [2], [0.4, 0.6])]['label'], 1)

        self.assertRaises(ValueError, tree.add_edges_from, [('F', 'G')])
Example #3
0
    def test_init_multi_variable_nodes(self):
        tree = TreeCPD([(('B', 'C'), Factor(['A'], [2], [0.8, 0.2]), (0, 0)),
                        (('B', 'C'), 'D', (0, 1)),
                        (('B', 'C'), Factor(['A'], [2], [0.1, 0.9]), (1, 0)),
                        (('B', 'C'), 'E', (1, 1)),
                        ('D', Factor(['A'], [2], [0.9, 0.1]), 0),
                        ('D', Factor(['A'], [2], [0.4, 0.6]), 1),
                        ('E', Factor(['A'], [2], [0.3, 0.7]), 0),
                        ('E', Factor(['A'], [2], [0.8, 0.2]), 1)])

        self.assertTrue(('B', 'C') in tree.nodes())
        self.assertTrue('D' in tree.nodes())
        self.assertTrue('E' in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.8, 0.2]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.9, 0.1]) in tree.nodes())

        self.assertTrue((('B', 'C'), Factor(['A'], [2], [0.8, 0.2])
                         in tree.edges()))
        self.assertTrue((('B', 'C'), 'E') in tree.edges())
        self.assertTrue(('D', Factor(['A'], [2], [0.4, 0.6])) in tree.edges())
        self.assertTrue(('E', Factor(['A'], [2], [0.8, 0.2])) in tree.edges())

        self.assertEqual(
            tree[('B', 'C')][Factor(['A'], [2], [0.8, 0.2])]['label'], (0, 0))
        self.assertEqual(tree[('B', 'C')]['D']['label'], (0, 1))
        self.assertEqual(tree['D'][Factor(['A'], [2], [0.9, 0.1])]['label'], 0)
        self.assertEqual(tree['E'][Factor(['A'], [2], [0.3, 0.7])]['label'], 0)
Example #4
0
    def test_init_single_variable_nodes(self):
        tree = TreeCPD([('B', Factor(['A'], [2],
                                     [0.8, 0.2]), 0), ('B', 'C', 1),
                        ('C', Factor(['A'], [2],
                                     [0.1, 0.9]), 0), ('C', 'D', 1),
                        ('D', Factor(['A'], [2], [0.9, 0.1]), 0),
                        ('D', Factor(['A'], [2], [0.4, 0.6]), 1)])

        self.assertTrue('B' in tree.nodes())
        self.assertTrue('C' in tree.nodes())
        self.assertTrue('D' in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.8, 0.2]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.1, 0.9]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.9, 0.1]) in tree.nodes())
        self.assertTrue(Factor(['A'], [2], [0.4, 0.6]) in tree.nodes())

        self.assertTrue(('B', Factor(['A'], [2], [0.8, 0.2]) in tree.edges()))
        self.assertTrue(('B', Factor(['A'], [2], [0.1, 0.9]) in tree.edges()))
        self.assertTrue(('B', Factor(['A'], [2], [0.9, 0.1]) in tree.edges()))
        self.assertTrue(('B', Factor(['A'], [2], [0.4, 0.6]) in tree.edges()))
        self.assertTrue(('C', 'D') in tree.edges())
        self.assertTrue(('B', 'C') in tree.edges())

        self.assertEqual(tree['B'][Factor(['A'], [2], [0.8, 0.2])]['label'], 0)
        self.assertEqual(tree['B']['C']['label'], 1)
        self.assertEqual(tree['C'][Factor(['A'], [2], [0.1, 0.9])]['label'], 0)
        self.assertEqual(tree['C']['D']['label'], 1)
        self.assertEqual(tree['D'][Factor(['A'], [2], [0.9, 0.1])]['label'], 0)
        self.assertEqual(tree['D'][Factor(['A'], [2], [0.4, 0.6])]['label'], 1)

        self.assertRaises(ValueError, tree.add_edges_from, [('F', 'G')])
Example #5
0
class TestTreeCPD(unittest.TestCase):
    def setUp(self):
        self.tree1 = TreeCPD([('B', Factor(['A'], [2], [0.8, 0.2]), 0),
                              ('B', 'C', 1),
                              ('C', Factor(['A'], [2], [0.1, 0.9]), 0),
                              ('C', 'D', 1),
                              ('D', Factor(['A'], [2], [0.9, 0.1]), 0),
                              ('D', Factor(['A'], [2], [0.4, 0.6]), 1)])

        self.tree2 = TreeCPD([(('B', 'C'), Factor(['A'], [2], [0.8, 0.2]), (0, 0)),
                              (('B', 'C'), 'D', (0, 1)),
                              (('B', 'C'), Factor(['A'], [2], [0.1, 0.9]), (1, 0)),
                              (('B', 'C'), 'E', (1, 1)),
                              ('D', Factor(['A'], [2], [0.9, 0.1]), 0),
                              ('D', Factor(['A'], [2], [0.4, 0.6]), 1),
                              ('E', Factor(['A'], [2], [0.3, 0.7]), 0),
                              ('E', Factor(['A'], [2], [0.8, 0.2]), 1)
        ])

    def test_add_edge(self):
        self.tree1.add_edge('yolo', 'yo', 0)
        self.assertTrue('yolo' in self.tree1.nodes() and 'yo' in self.tree1.nodes())
        self.assertTrue(('yolo', 'yo') in self.tree1.edges())
        self.assertEqual(self.tree1['yolo']['yo']['label'], 0)

    def test_add_edges_from(self):
        self.tree1.add_edges_from([('yolo', 'yo', 0), ('hello', 'world', 1)])
        self.assertTrue('yolo' in self.tree1.nodes() and 'yo' in self.tree1.nodes() and
                        'hello' in self.tree1.nodes() and 'world' in self.tree1.nodes())
        self.assertTrue(('yolo', 'yo') in self.tree1.edges())
        self.assertTrue(('hello', 'world') in self.tree1.edges())
        self.assertEqual(self.tree1['yolo']['yo']['label'], 0)
        self.assertEqual(self.tree1['hello']['world']['label'], 1)

    @unittest.skip('Not implemented yet')
    def test_to_tabular_cpd(self):
        tabular_cpd = self.tree1.to_tabular_cpd('A')
        self.assertEqual(tabular_cpd.evidence, ['B', 'C', 'D'])
        self.assertEqual(tabular_cpd.evidence_card, [2, 2, 2])
        self.assertEqual(list(tabular_cpd.variables), ['A', 'B', 'C', 'D'])
        np_test.assert_array_equal(tabular_cpd.values,
                                   np.array([0.8, 0.8, 0.8, 0.8, 0.1, 0.1, 0.9, 0.4,
                                             0.2, 0.2, 0.2, 0.2, 0.9, 0.9, 0.1, 0.6]))

        tabular_cpd = self.tree2.to_tabular_cpd('A')
        self.assertEqual(tabular_cpd.evidence, ['B', 'C', 'D', 'E'])
        self.assertEqual(tabular_cpd.evidence_card, [2, 2, 2, 2])
        self.assertEqual(list(tabular_cpd.variables), ['A', 'B', 'C', 'D', 'E'])
        np_test.assert_array_equal(tabular_cpd.values,
                                   np.array([0.8, 0.8, 0.8, 0.8, 0.9, 0.9, 0.4, 0.4,
                                             0.1, 0.1, 0.1, 0.1, 0.3, 0.8, 0.3, 0.8,
                                             0.2, 0.2, 0.2, 0.2, 0.1, 0.1, 0.6, 0.6,
                                             0.9, 0.9, 0.9, 0.9, 0.7, 0.2, 0.7, 0.2]))

    @unittest.skip('Not implemented yet')
    def test_to_tabular_cpd_parent_order(self):
        tabular_cpd = self.tree1.to_tabular_cpd('A', parents_order=['D', 'C', 'B'])
        self.assertEqual(tabular_cpd.evidence, ['D', 'C', 'B'])
        self.assertEqual(tabular_cpd.evidence_card, [2, 2, 2])
        self.assertEqual(list(tabular_cpd.variables), ['A', 'D', 'C', 'B'])
        np_test.assert_array_equal(tabular_cpd.values,
                                   np.array([0.8, 0.1, 0.8, 0.9, 0.8, 0.1, 0.8, 0.4,
                                             0.2, 0.9, 0.2, 0.1, 0.2, 0.9, 0.2, 0.6]))

        tabular_cpd = self.tree2.to_tabular_cpd('A', parents_order=['E', 'D', 'C', 'B'])

    @unittest.skip('Not implemented yet')
    def test_to_rule_cpd(self):
        rule_cpd = self.tree1.to_rule_cpd()
        self.assertEqual(rule_cpd.cardinality(), {'A': 2, 'B': 2, 'C': 2, 'D': 2})
        self.assertEqual(rule_cpd.scope(), {'A', 'B', 'C', 'D'})
        self.assertEqual(rule_cpd.variable, 'A')
        self.assertEqual(rule_cpd.rules, {('A_0', 'B_0'): 0.8,
                                          ('A_1', 'B_0'): 0.2,
                                          ('A_0', 'B_1', 'C_0'): 0.1,
                                          ('A_0', 'B_1', 'C_1', 'D_0'): 0.9,
                                          ('A_1', 'B_1', 'C_1', 'D_0'): 0.1,
                                          ('A_0', 'B_1', 'C_1', 'D_1'): 0.4,
                                          ('A_1', 'B_!', 'C_1', 'D_1'): 0.6})

        rule_cpd = self.tree2.to_rule_cpd()
        self.assertEqual(rule_cpd.cardinality(), {'A': 2, 'B': 2, 'C': 2, 'D': 2, 'E': 2})
        self.assertEqual(rule_cpd.scope(), {'A', 'B', 'C', 'D', 'E'})
        self.assertEqual(rule_cpd.variable, 'A')
        self.assertEqual(rule_cpd.rules, {('A_0', 'B_0', 'C_0'): 0.8,
                                          ('A_1', 'B_0', 'C_0'): 0.2,
                                          ('A_0', 'B_0', 'C_1', 'D_0'): 0.9,
                                          ('A_1', 'B_0', 'C_1', 'D_0'): 0.1,
                                          ('A_0', 'B_0', 'C_1', 'D_1'): 0.4,
                                          ('A_1', 'B_0', 'C_1', 'D_1'): 0.6,
                                          ('A_0', 'B_1', 'C_0'): 0.1,
                                          ('A_1', 'B_1', 'C_0'): 0.9,
                                          ('A_0', 'B_1', 'C_1', 'E_0'): 0.3,
                                          ('A_1', 'B_1', 'C_1', 'E_0'): 0.7,
                                          ('A_0', 'B_1', 'C_1', 'E_1'): 0.8,
                                          ('A_1', 'B_1', 'C_1', 'E_1'): 0.2})
Example #6
0
class TestTreeCPD(unittest.TestCase):
    def setUp(self):
        self.tree1 = TreeCPD([('B', Factor(['A'], [2], [0.8, 0.2]), '0'),
                              ('B', 'C', '1'),
                              ('C', Factor(['A'], [2], [0.1, 0.9]), '0'),
                              ('C', 'D', '1'),
                              ('D', Factor(['A'], [2], [0.9, 0.1]), '0'),
                              ('D', Factor(['A'], [2], [0.4, 0.6]), '1')])

        self.tree2 = TreeCPD([('C', 'A', '0'), ('C', 'B', '1'),
                              ('A', Factor(['J'], [2], [0.9, 0.1]), '0'),
                              ('A', Factor(['J'], [2], [0.3, 0.7]), '1'),
                              ('B', Factor(['J'], [2], [0.8, 0.2]), '0'),
                              ('B', Factor(['J'], [2], [0.4, 0.6]), '1')])

    def test_add_edge(self):
        self.tree1.add_edge('yolo', 'yo', 0)
        self.assertTrue('yolo' in self.tree1.nodes()
                        and 'yo' in self.tree1.nodes())
        self.assertTrue(('yolo', 'yo') in self.tree1.edges())
        self.assertEqual(self.tree1['yolo']['yo']['label'], 0)

    def test_add_edges_from(self):
        self.tree1.add_edges_from([('yolo', 'yo', 0), ('hello', 'world', 1)])
        self.assertTrue('yolo' in self.tree1.nodes()
                        and 'yo' in self.tree1.nodes()
                        and 'hello' in self.tree1.nodes()
                        and 'world' in self.tree1.nodes())
        self.assertTrue(('yolo', 'yo') in self.tree1.edges())
        self.assertTrue(('hello', 'world') in self.tree1.edges())
        self.assertEqual(self.tree1['yolo']['yo']['label'], 0)
        self.assertEqual(self.tree1['hello']['world']['label'], 1)

    def test_to_tabular_cpd(self):
        tabular_cpd = self.tree1.to_tabular_cpd()
        self.assertEqual(tabular_cpd.evidence, ['D', 'C', 'B'])
        self.assertEqual(tabular_cpd.evidence_card, [2, 2, 2])
        self.assertEqual(list(tabular_cpd.variables), ['A', 'B', 'C', 'D'])
        np_test.assert_array_equal(
            tabular_cpd.values,
            np.array([
                0.8, 0.8, 0.8, 0.8, 0.1, 0.1, 0.9, 0.4, 0.2, 0.2, 0.2, 0.2,
                0.9, 0.9, 0.1, 0.6
            ]))

        tabular_cpd = self.tree2.to_tabular_cpd()
        self.assertEqual(tabular_cpd.evidence, ['A', 'B', 'C'])
        self.assertEqual(tabular_cpd.evidence_card, [2, 2, 2])
        self.assertEqual(list(tabular_cpd.variables), ['J', 'C', 'B', 'A'])
        np_test.assert_array_equal(
            tabular_cpd.values,
            np.array([
                0.9, 0.3, 0.9, 0.3, 0.8, 0.8, 0.4, 0.4, 0.1, 0.7, 0.1, 0.7,
                0.2, 0.2, 0.6, 0.6
            ]))

    @unittest.skip('Not implemented yet')
    def test_to_tabular_cpd_parent_order(self):
        tabular_cpd = self.tree1.to_tabular_cpd('A',
                                                parents_order=['D', 'C', 'B'])
        self.assertEqual(tabular_cpd.evidence, ['D', 'C', 'B'])
        self.assertEqual(tabular_cpd.evidence_card, [2, 2, 2])
        self.assertEqual(list(tabular_cpd.variables), ['A', 'D', 'C', 'B'])
        np_test.assert_array_equal(
            tabular_cpd.values,
            np.array([
                0.8, 0.1, 0.8, 0.9, 0.8, 0.1, 0.8, 0.4, 0.2, 0.9, 0.2, 0.1,
                0.2, 0.9, 0.2, 0.6
            ]))

        tabular_cpd = self.tree2.to_tabular_cpd(
            'A', parents_order=['E', 'D', 'C', 'B'])

    @unittest.skip('Not implemented yet')
    def test_to_rule_cpd(self):
        rule_cpd = self.tree1.to_rule_cpd()
        self.assertEqual(rule_cpd.cardinality(), {
            'A': 2,
            'B': 2,
            'C': 2,
            'D': 2
        })
        self.assertEqual(rule_cpd.scope(), {'A', 'B', 'C', 'D'})
        self.assertEqual(rule_cpd.variable, 'A')
        self.assertEqual(
            rule_cpd.rules, {
                ('A_0', 'B_0'): 0.8,
                ('A_1', 'B_0'): 0.2,
                ('A_0', 'B_1', 'C_0'): 0.1,
                ('A_0', 'B_1', 'C_1', 'D_0'): 0.9,
                ('A_1', 'B_1', 'C_1', 'D_0'): 0.1,
                ('A_0', 'B_1', 'C_1', 'D_1'): 0.4,
                ('A_1', 'B_!', 'C_1', 'D_1'): 0.6
            })

        rule_cpd = self.tree2.to_rule_cpd()
        self.assertEqual(rule_cpd.cardinality(), {
            'A': 2,
            'B': 2,
            'C': 2,
            'D': 2,
            'E': 2
        })
        self.assertEqual(rule_cpd.scope(), {'A', 'B', 'C', 'D', 'E'})
        self.assertEqual(rule_cpd.variable, 'A')
        self.assertEqual(
            rule_cpd.rules, {
                ('A_0', 'B_0', 'C_0'): 0.8,
                ('A_1', 'B_0', 'C_0'): 0.2,
                ('A_0', 'B_0', 'C_1', 'D_0'): 0.9,
                ('A_1', 'B_0', 'C_1', 'D_0'): 0.1,
                ('A_0', 'B_0', 'C_1', 'D_1'): 0.4,
                ('A_1', 'B_0', 'C_1', 'D_1'): 0.6,
                ('A_0', 'B_1', 'C_0'): 0.1,
                ('A_1', 'B_1', 'C_0'): 0.9,
                ('A_0', 'B_1', 'C_1', 'E_0'): 0.3,
                ('A_1', 'B_1', 'C_1', 'E_0'): 0.7,
                ('A_0', 'B_1', 'C_1', 'E_1'): 0.8,
                ('A_1', 'B_1', 'C_1', 'E_1'): 0.2
            })