예제 #1
0
    def test_get(self):
        """Test factor.get()."""
        fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()

        self.assertIsInstance(fA.get(A='a0'), np.ndarray)
        self.assertIsInstance(fB_A.get(A='a0', B='b1'), np.ndarray)
        self.assertIsInstance(fB_A.get(A='a0'), np.ndarray)
예제 #2
0
    def test_scope(self):
        """Test a Bag's scope."""
        # Get the Factors for the Sprinkler network
        factors = examples.get_sprinkler_factors()
        bag = Bag('Sprinkler', factors)

        self.assertEqual(bag.scope, {'A', 'B', 'C', 'D', 'E'})
예제 #3
0
    def test_variable_elimination_with_evidence(self):
        """Test the variable elimination algorithm."""
        factors = examples.get_sprinkler_factors()
        bag = Bag('Sprinkler', factors)

        # Compute the (unnormalized) factor over C and A=a1
        fC_a1 = bag.variable_elimination(['C'], {'A': 'a1'})

        self.assertAlmostEqual(fC_a1['c0'], 0.12, places=2)
        self.assertAlmostEqual(fC_a1['c1'], 0.48, places=2)
예제 #4
0
    def test_as_dict(self):
        """Test serialization."""
        factors = examples.get_sprinkler_factors()
        bag = Bag('Sprinkler', factors)

        bd = bag.as_dict()

        self.assertEqual(bd['type'], 'Bag')
        self.assertEqual(bd['name'], bag.name)
        self.assertEqual(len(bd['factors']), len(bag))
예제 #5
0
    def test_variable_elimination_single(self):
        """Test the variable elimination algorithm."""
        # Get the Factors for the Sprinkler network
        factors = examples.get_sprinkler_factors()
        bag = Bag('Sprinkler', factors)

        # Compute the prior over C
        fC = bag.variable_elimination(['C'])

        self.assertAlmostEqual(fC['c0'], 0.48, places=2)
        self.assertAlmostEqual(fC['c1'], 0.52, places=2)
        self.assertAlmostEqual(fC.sum(), 1, places=8)
예제 #6
0
    def test_variable_elimination_multiple(self):
        """Test the variable elimination algorithm."""
        # Get the Factors for the Sprinkler network
        factors = examples.get_sprinkler_factors()
        bag = Bag('Sprinkler', factors)

        # Compute the joint over A and C
        fAC = bag.variable_elimination(['A', 'C'])

        self.assertAlmostEqual(fAC['a0', 'c0'], 0.36, places=2)
        self.assertAlmostEqual(fAC['a0', 'c1'], 0.04, places=2)
        self.assertAlmostEqual(fAC['a1', 'c0'], 0.12, places=2)
        self.assertAlmostEqual(fAC['a1', 'c1'], 0.48, places=2)
        self.assertAlmostEqual(fAC.sum(), 1, places=8)
예제 #7
0
    def test_summing_out_all(self):
        """Test summing out variables."""
        # Get the Factors for the Sprinkler network
        fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()

        # Multiplying the factor with a *prior* with a *conditional* distribution, yields
        # a *joint* distribution.
        fAB = fA * fB_A

        total = fAB.sum_out(['A', 'B'])

        # Make sure we did this right :-)
        self.assertAlmostEquals(total.values, 1.00, places=2)
        self.assertTrue(fA.sum_out([]).equals(fA))
예제 #8
0
    def test_serialization_complex(self):
        """Test the JSON serialization."""
        [fA, fB_A, fC_A, fD_BC, fE_C] = examples.get_sprinkler_factors()

        dict_repr = fB_A.as_dict()

        # Make sure the expected keys exist
        for key in ['type', 'scope', 'states', 'data']:
            self.assertTrue(key in dict_repr)

        self.assertTrue(dict_repr['type'] == 'Factor')

        fB_A2 = Factor.from_dict(dict_repr)
        self.assertEquals(fB_A.scope, fB_A2.scope)
        self.assertEquals(fB_A.states, fB_A2.states)
예제 #9
0
    def test_multiplication(self):
        """Test factor multiplication."""
        # Get the Factors for the Sprinkler network
        fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()

        # Multiplying the factor with a *prior* with a *conditional* distribution, yields
        # a *joint* distribution.
        fAB = fA * fB_A

        # Make sure we did this right :-)
        self.assertAlmostEquals(fAB['a1', 'b1'], 0.12, places=2)
        self.assertAlmostEquals(fAB['a1', 'b0'], 0.48, places=2)
        self.assertAlmostEquals(fAB['a0', 'b1'], 0.30, places=2)
        self.assertAlmostEquals(fAB['a0', 'b0'], 0.10, places=2)

        self.assertAlmostEquals(fAB.sum(), 1, places=8)
예제 #10
0
    def test_summing_out(self):
        """Test summing out variables."""
        # Get the Factors for the Sprinkler network
        fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()

        # Multiplying the factor with a *prior* with a *conditional* distribution, yields
        # a *joint* distribution.
        fAB = fA * fB_A

        # By summing out A, we'll get the prior over B
        fB = fAB.sum_out('A')

        # Make sure we did this right :-)
        self.assertAlmostEquals(fB['b1'], 0.42, places=2)
        self.assertAlmostEquals(fB['b0'], 0.58, places=2)

        self.assertAlmostEquals(fB.sum(), 1, places=8)
예제 #11
0
    def test_mul(self):
        """Test factor.mul()."""
        fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()

        # int * int
        self.assertEqual(mul(3, 3), 9)

        # int
        fA2 = mul(fA, 2)
        self.assertTrue(isinstance(fA2, Factor))
        self.assertEqual(fA2.scope, ['A'])
        self.assertEqual(fA2.sum(), 2)

        # float
        fA2 = mul(fA, 2.0)
        self.assertTrue(isinstance(fA2, Factor))
        self.assertEqual(fA2.scope, ['A'])
        self.assertEqual(fA2.sum(), 2)

        # Two Factors
        fAB = mul(fA, fB_A)
        self.assertTrue(isinstance(fAB, Factor))
        self.assertEqual(fAB.scope, ['A', 'B'])
예제 #12
0
    def test_add(self):
        fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()

        fAB = fA * fB_A
        f2 = fA.add(fAB)

        self.assertTrue(isinstance(f2, Factor))

        # Adding int
        self.assertEqual((fA + 1)['a1'], 1.6)
        self.assertEqual((fA + 1)['a0'], 1.4)

        # Adding float
        self.assertEqual((fA + 1.0)['a1'], 1.6)
        self.assertEqual((fA + 1.0)['a0'], 1.4)

        # Adding Factor
        self.assertEqual((fA + fB_A)['a1', 'b1'], 0.8)
        self.assertEqual((fA + fB_A)['a1', 'b0'], 1.4)
        self.assertEqual((fA + fB_A)['a0', 'b1'], 1.15)
        self.assertEqual((fA + fB_A)['a0', 'b0'], 0.65)

        with self.assertRaises(Exception):
            fA.add('noooooo')
예제 #13
0
 def test_repr(self):
     """Test repr(Bag)."""
     factors = examples.get_sprinkler_factors()
     bag = Bag('Sprinkler', factors)
     self.assertEqual(repr(bag), f"<Bag: '{bag.name}'>")
예제 #14
0
    def test_error(self):
        factors = examples.get_sprinkler_factors()
        fB_A = factors[1]

        with self.assertRaises(error.NotInScopeError) as context:
            fB_A.sum_out('C')
예제 #15
0
 def test_overlaps_with(self):
     """Test factor.overlaps_with()."""
     fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()
     overlap = fD_BC.overlaps_with(['B', 'C'])
     self.assertTrue(overlap)
예제 #16
0
 def test_getitem(self):
     """Test casting to Factor when accessing Factor by index."""
     fA, fB_A, fC_A, fD_BC, fE_C = examples.get_sprinkler_factors()
     fAB = fA * fB_A
     self.assertTrue(isinstance(fAB['a0'], np.ndarray))