Beispiel #1
0
    def test_copy(self):
        model = MC(['a', 'b'], [2, 2], [State('a', 0), State('b', 1)])
        model.add_transition_model('a', {0: {0: 0.1, 1: 0.9}, 1: {0: 0.2, 1: 0.8}})
        model.add_transition_model('b', {0: {0: 0.3, 1: 0.7}, 1: {0: 0.4, 1: 0.6}})
        copy = model.copy()

        self.assertIsInstance(copy, MC)
        self.assertEqual(sorted(model.variables), sorted(copy.variables))
        self.assertEqual(model.cardinalities, copy.cardinalities)
        self.assertEqual(model.transition_models, copy.transition_models)
        self.assertEqual(model.state, copy.state)

        model.add_variable('p', 1)
        model.set_start_state([State('a', 0), State('b', 1), State('p', 0)])
        model.add_transition_model('p', {0: {0: 1}})

        self.assertNotEqual(sorted(model.variables), sorted(copy.variables))
        self.assertEqual(sorted(['a', 'b']), sorted(copy.variables))
        self.assertNotEqual(model.cardinalities, copy.cardinalities)
        self.assertEqual({'a': 2, 'b': 2}, copy.cardinalities)
        self.assertNotEqual(model.state, copy.state)
        self.assertEqual([State('a', 0), State('b', 1)], copy.state)
        self.assertNotEqual(model.transition_models, copy.transition_models)
        self.assertEqual(len(copy.transition_models), 2)
        self.assertEqual(copy.transition_models['a'], {0: {0: 0.1, 1: 0.9}, 1: {0: 0.2, 1: 0.8}})
        self.assertEqual(copy.transition_models['b'], {0: {0: 0.3, 1: 0.7}, 1: {0: 0.4, 1: 0.6}})
Beispiel #2
0
 def test_set_start_state_list(self, check_state):
     model = MC(['b', 'a'], [1, 2])
     check_state.return_value = True
     model.set_start_state([State('a', 0), State('b', 1)])
     model_state = [State('b', 1), State('a', 0)]
     check_state.assert_called_once_with(model, model_state)
     self.assertEqual(model.state, model_state)
Beispiel #3
0
 def test_generate_sample_less_arg(self, random_state, sample_discrete):
     model = MC(['a', 'b'], [2, 2])
     model.transition_models['a'] = {
         0: {
             0: 0.1,
             1: 0.9
         },
         1: {
             0: 0.2,
             1: 0.8
         }
     }
     model.transition_models['b'] = {
         0: {
             0: 0.3,
             1: 0.7
         },
         1: {
             0: 0.4,
             1: 0.6
         }
     }
     random_state.return_value = [State('a', 0), State('b', 1)]
     sample_discrete.side_effect = [[1], [0]] * 2
     gen = model.generate_sample(size=2)
     samples = [sample for sample in gen]
     expected_samples = [[State('a', 1), State('b', 0)]] * 2
     self.assertEqual(samples, expected_samples)
Beispiel #4
0
 def test_sample(self):
     model = MC(['a', 'b'], [2, 2])
     model.transition_models['a'] = {
         0: {
             0: 0.1,
             1: 0.9
         },
         1: {
             0: 0.2,
             1: 0.8
         }
     }
     model.transition_models['b'] = {
         0: {
             0: 0.3,
             1: 0.7
         },
         1: {
             0: 0.4,
             1: 0.6
         }
     }
     sample = model.sample(start_state=[State('a', 0),
                                        State('b', 1)],
                           size=2)
     self.assertEqual(len(sample), 2)
     self.assertEqual(list(sample.columns), ['a', 'b'])
     self.assertTrue(
         list(sample.loc[0]) in [[0, 0], [0, 1], [1, 0], [1, 1]])
     self.assertTrue(
         list(sample.loc[1]) in [[0, 0], [0, 1], [1, 0], [1, 1]])
Beispiel #5
0
 def test_random_state(self):
     model = MC(['a', 'b'], [2, 3])
     state = model.random_state()
     vars = [v for v, s in state]
     self.assertEqual(vars, ['a', 'b'])
     self.assertGreaterEqual(state[0].state, 0)
     self.assertGreaterEqual(state[1].state, 0)
     self.assertLessEqual(state[0].state, 1)
     self.assertLessEqual(state[1].state, 2)
Beispiel #6
0
 def test_sample_less_arg(self, random_state):
     model = MC(['a', 'b'], [2, 2])
     random_state.return_value = [State('a', 0), State('b', 1)]
     sample = model.sample(size=1)
     random_state.assert_called_once_with(model)
     self.assertEqual(model.state, random_state.return_value)
     self.assertEqual(len(sample), 1)
     self.assertEqual(list(sample.columns), ['a', 'b'])
     self.assertEqual(list(sample.loc[0]), [0, 1])
Beispiel #7
0
 def test_check_state_success(self):
     model = MC(['a'], [2])
     self.assertTrue(model._check_state([State('a', 1)]))
Beispiel #8
0
 def test_set_start_state_none(self):
     model = MC()
     model.state = 'state'
     model.set_start_state(None)
     self.assertIsNone(model.state)
Beispiel #9
0
 def test_prob_from_sample(self, sample):
     model = MC(['a', 'b'], [2, 2])
     sample.return_value = self.sample
     probabilites = model.prob_from_sample([State('a', 1), State('b', 0)])
     self.assertEqual(list(probabilites), [1] * 50 + [0] * 50)
Beispiel #10
0
 def test_add_transition_model_success(self):
     model = MC(['var'], [2])
     transition_model = {0: {0: 0.3, 1: 0.7}, 1: {0: 0.5, 1: 0.5}}
     model.add_transition_model('var', transition_model)
     self.assertDictEqual(model.transition_models['var'], transition_model)
Beispiel #11
0
 def test_add_variables_from(self, add_var):
     model = MC()
     model.add_variables_from(self.variables, self.card)
     calls = [call(model, *p) for p in zip(self.variables, self.card)]
     add_var.assert_has_calls(calls)
Beispiel #12
0
 def test_add_variable_existing(self, warn):
     model = MC(['p'], [2])
     model.add_variable('p', 3)
     self.assertEqual(warn.call_count, 1)
Beispiel #13
0
 def test_add_variable_new(self):
     model = MC(['a'], [2])
     model.add_variable('p', 3)
     self.assertIn('p', model.variables)
     self.assertEqual(model.cardinalities['p'], 3)
     self.assertDictEqual(model.transition_models['p'], {})