Exemple #1
0
    def test_from_observations__1_var__replace_vars(self):

        observations = DataFrame({
            'ace': ['a', 'c', 'c', 'e', 'e', 'e'],
        })
        discrete = Discrete.from_observations(observations, variables='ACE')
        self.assertEqual(['ACE'], discrete.variables)
        self.assertEqual({'ACE': ['a', 'c', 'e']}, discrete.states)
        self.assertEqual(1 / 6, discrete.p(ACE='a'))
        self.assertEqual(2 / 6, discrete.p(ACE='c'))
        self.assertEqual(3 / 6, discrete.p(ACE='e'))
Exemple #2
0
    def test_from_observations__1_var(self):

        observations = DataFrame({
            'ace': ['a', 'c', 'c', 'e', 'e', 'e'],
        })
        discrete = Discrete.from_observations(observations)
        self.assertEqual(['ace'], discrete.variables)
        self.assertEqual({'ace': ['a', 'c', 'e']}, discrete.states)
        self.assertEqual(1 / 6, discrete.p(ace='a'))
        self.assertEqual(2 / 6, discrete.p(ace='c'))
        self.assertEqual(3 / 6, discrete.p(ace='e'))
Exemple #3
0
    def test_from_observations__2_vars__extra_states(self):

        observations = DataFrame({
            'ace': ['a', 'c', 'c', 'e', 'e', 'e'],
            'bdf': ['b', 'd', 'd', 'f', 'f', 'f']
        })
        states = {'ace': ['a', 'c', 'e', 'g'], 'bdf': ['b', 'd', 'f', 'h']}
        discrete = Discrete.from_observations(observations, states=states)
        self.assertEqual(['ace', 'bdf'], discrete.variables)
        self.assertEqual(states, discrete.states)
        self.assertEqual(1 / 6, discrete.p(ace='a', bdf='b'))
        self.assertEqual(2 / 6, discrete.p(ace='c', bdf='d'))
        self.assertEqual(3 / 6, discrete.p(ace='e', bdf='f'))
Exemple #4
0
    def test_from_observations__2_vars__replace_vars(self):

        observations = DataFrame({
            'ace': ['a', 'c', 'c', 'e', 'e', 'e'],
            'bdf': ['b', 'd', 'd', 'f', 'f', 'f']
        })
        discrete = Discrete.from_observations(observations,
                                              variables=['ACE', 'BDF'])
        self.assertEqual(['ACE', 'BDF'], discrete.variables)
        self.assertEqual({
            'ACE': ['a', 'c', 'e'],
            'BDF': ['b', 'd', 'f']
        }, discrete.states)
        self.assertEqual(1 / 6, discrete.p(ACE='a', BDF='b'))
        self.assertEqual(2 / 6, discrete.p(ACE='c', BDF='d'))
        self.assertEqual(3 / 6, discrete.p(ACE='e', BDF='f'))
    def setUp(self) -> None:

        # cookies
        self.bowl_1_and_chocolate = 0.125
        self.bowl_1_and_vanilla = 0.375
        self.bowl_2_and_chocolate = 0.25
        self.bowl_2_and_vanilla = 0.25
        cookie_data = TestChapter01.make_cookies_observations()
        self.cookies = Discrete.from_observations(cookie_data)
        self.vanilla = self.cookies.p(flavor='vanilla')
        self.vanilla__bowl_1 = self.cookies.given(bowl='bowl 1').p(
            flavor='vanilla')
        self.vanilla__bowl_2 = self.cookies.given(bowl='bowl 2').p(
            flavor='vanilla')
        self.bowl = Discrete.from_probs({
            'bowl 1': 0.5,
            'bowl 2': 0.5
        },
                                        variables=['bowl'])
        self.bowl_1 = self.bowl.p(bowl='bowl 1')
        self.bowl_2 = self.bowl.p(bowl='bowl 2')

        # m & m's
        self.mix_1994 = Discrete.from_probs(
            {
                'brown': 0.3,
                'yellow': 0.2,
                'red': 0.2,
                'green': 0.1,
                'orange': 0.1,
                'tan': 0.1
            },
            variables='color')
        self.mix_1996 = Discrete.from_probs(
            {
                'blue': 0.24,
                'green': 0.2,
                'orange': 0.16,
                'yellow': 0.14,
                'red': 0.13,
                'brown': 0.13
            },
            variables='color')
        self.bag = Discrete.from_probs({1994: 0.5, 1996: 0.5}, variables='bag')
Exemple #6
0
    def test__1_3_1(self):

        t = Discrete.from_observations(data=DataFrame({
            't': [s_a + s_b for s_a, s_b in product(range(1, 7), range(1, 7))]
        }))
        s_a__s_b = Discrete.from_probs(data={
            (a, b): 1 / 36
            for a, b in product(range(1, 7), range(1, 7))
        },
                                       variables=['s_a', 's_b'])
        t_9__given__s_a__s_b = Conditional.from_probs(
            data={(9, a, b): int(a + b == 9)
                  for a, b in product(range(1, 7), range(1, 7))},
            joint_variables=['t'],
            conditional_variables=['s_a', 's_b'])
        t_9__s_a__s_b = t_9__given__s_a__s_b * s_a__s_b
        t_9 = t_9__s_a__s_b / t.p(t=9)
        for s_a, s_b in product(range(1, 6), range(1, 6)):
            self.assertEqual(t_9.p(s_a=s_a, s_b=s_b),
                             0.25 if s_a + s_b == 9 else 0)