Beispiel #1
0
    def test_compute_posterior(self):
        """Test the function Bag.compute_posterior()."""
        factors = examples.get_student_CPTs()
        bag = Bag('Student', list(factors.values()))

        I = bag.compute_posterior(['I'], {}, [], {})
        self.assertAlmostEqual(I['i0'], 0.70, places=2)
        self.assertAlmostEqual(I['i1'], 0.30, places=2)

        G = bag.compute_posterior(['G'], {}, [], {})
        self.assertAlmostEqual(G['g1'], 0.3620, places=4)
        self.assertAlmostEqual(G['g2'], 0.2884, places=4)
        self.assertAlmostEqual(G['g3'], 0.3496, places=4)

        S_i1 = bag.compute_posterior(['S'], {}, [], {'I': 'i1'})
        self.assertAlmostEqual(S_i1['s0'], 0.20, places=2)
        self.assertAlmostEqual(S_i1['s1'], 0.80, places=2)

        S_i0 = bag.compute_posterior(['S'], {}, [], {'I': 'i0'})
        self.assertAlmostEqual(S_i0['s0'], 0.95, places=2)
        self.assertAlmostEqual(S_i0['s1'], 0.05, places=2)

        G_I = bag.compute_posterior(['G'], {}, ['I'], {})
        self.assertAlmostEqual(G_I['i0', 'g1'], 0.20, places=2)
        self.assertAlmostEqual(G_I['i0', 'g2'], 0.34, places=2)
        self.assertAlmostEqual(G_I['i0', 'g3'], 0.46, places=2)

        s0_i0 = bag.compute_posterior([], {'S': 's0'}, [], {'I': 'i0'})
        s1_i0 = bag.compute_posterior([], {'S': 's1'}, [], {'I': 'i0'})
        self.assertAlmostEqual(s0_i0, 0.95, places=2)
        self.assertAlmostEqual(s1_i0, 0.05, places=2)

        s0G_i0 = bag.compute_posterior(['G'], {'S': 's0'}, [], {'I': 'i0'})
        self.assertEqual(len(s0G_i0), 3)
Beispiel #2
0
    def test_JPT(self):
        """Test Joint Probability Table."""
        factors = examples.get_student_CPTs()
        bag = Bag('Student', list(factors.values()))

        jpt = bag.variable_elimination(list(bag.scope)).normalize()
        self.assertAlmostEqual(jpt.sum(), 1, places=5)
Beispiel #3
0
    def test_div_cpt(self):
        """Test CPT division."""
        CPTs = examples.get_student_CPTs()
        I = CPTs['I']
        S_I = CPTs['S']

        # Dividing a CPT should yield a Factor
        SI = S_I.div(I)
        self.assertIsInstance(SI, Factor)
Beispiel #4
0
    def test_mul_scalar(self):
        """Test CPT multiplication with a scalar."""
        CPTs = examples.get_student_CPTs()
        I = CPTs['I']
        S_I = CPTs['S']

        # Multiplying a CPT should yield a Factor
        Sx3 = S_I.mul(3)
        self.assertIsInstance(Sx3, Factor)
Beispiel #5
0
    def test_mul_cpt(self):
        """Test CPT multiplication with another Factor/CPT."""
        CPTs = examples.get_student_CPTs()
        I = CPTs['I']
        S_I = CPTs['S']

        # Multiplying a CPT should yield a Factor
        SI = S_I.mul(I)
        self.assertIsInstance(SI, Factor)
Beispiel #6
0
    def test_mul_div(self):
        """Test division after multiplication for equality."""
        CPTs = examples.get_student_CPTs()
        I = CPTs['I']
        S_I = CPTs['S']

        # Multiplying a CPT should yield a Factor
        result = S_I.mul(I).div(I)
        self.assertIsInstance(result, Factor)
        self.assertTrue(np.allclose(result.values, S_I.values))
Beispiel #7
0
    def test_node_cpt(self):
        """Test getting/setting node's CPT."""
        node = self.Gs['I']
        cpts = examples.get_student_CPTs()

        with self.assertRaises(Exception):
            node.cpt = 1

        with self.assertRaises(Exception):
            node.cpt = CPT(cpts['G'].as_factor(), conditioned=['I', 'D'])

        with self.assertRaises(Exception):
            node.cpt = CPT(cpts['G'])
Beispiel #8
0
    def test_MAP(self):
        """Test the Bag.MAP() function."""
        factors = examples.get_student_CPTs()
        bag = Bag('Student', list(factors.values()))

        argmax_I = bag.MAP(['I'], {}, False)
        self.assertEqual(argmax_I, 'i0')

        argmax_G = bag.MAP(['G'], {}, False)
        self.assertEqual(argmax_G, 'g1')

        argmax_G = bag.MAP(['G'], {}, True)
        self.assertEqual(argmax_G[0], 'g1')
        self.assertAlmostEqual(argmax_G[1], 0.362)
Beispiel #9
0
    def test_repr(self):
        """Test repr(jpt)."""
        CPTs = examples.get_student_CPTs()
        I = CPTs['I']
        S_I = CPTs['S']

        self.assertEqual(I.display_name, 'P(I)')
        self.assertEqual(S_I.display_name, 'P(S|I)')

        # Unfortunately, pandas.DataFrame._repr_html_() returns syntactically
        # invalid html, so we cannot validate the html itself :-(.
        try:
            html = S_I._repr_html_()
        except Exception as e:
            self.fail('Creating an HTML representation raised an exception?')
Beispiel #10
0
    def test_P(self):
        """Test the function Bag.P()."""
        factors = examples.get_student_CPTs()
        bag = Bag('Student', list(factors.values()))

        I = bag.P('I')
        self.assertAlmostEqual(I['i0'], 0.7, places=3)
        self.assertAlmostEqual(I['i1'], 0.3, places=3)

        G_I = bag.P('G|I')
        self.assertEqual(G_I.scope, ['I', 'G'])
        self.assertEqual(G_I.conditioned, ['G'])
        self.assertEqual(G_I.conditioning, ['I'])

        I_g1 = bag.P('I|G=g1')
        self.assertAlmostEqual(I_g1['i0'], 0.387, places=3)
        self.assertAlmostEqual(I_g1['i1'], 0.613, places=3)
Beispiel #11
0
 def test_as_factor(self):
     """Test CPT.as_factor()."""
     CPTs = examples.get_student_CPTs()
     G_ID = CPTs['G']
     self.assertIsInstance(G_ID.as_factor(), Factor)