Skip to content

Commit

Permalink
Add statistical experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
samirelanduk committed Mar 13, 2018
1 parent e335e45 commit da9253a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion inferi/probability.py
Expand Up @@ -56,7 +56,7 @@ def chances_of(self, outcome):
statistical experiment.
:param outcome: The outcome to test for."""

for e in self._simple_events:
if e.outcome() == outcome:
return e.probability()
Expand All @@ -78,3 +78,9 @@ def outcomes(self):
:rtype: ``set``"""

return set([e.outcome() for e in self._simple_events])


def experiment(self):
"""Generate an outcome."""

return random.sample(self.outcomes(), 1)[0]
8 changes: 8 additions & 0 deletions tests/integration/test_probability.py
Expand Up @@ -46,3 +46,11 @@ def test_events(self):
self.assertEqual(sample_space.chances_of(1), 1 / 6)
self.assertEqual(sample_space.chances_of(6), 1 / 6)
self.assertEqual(sample_space.chances_of(7), 0)
for i in range(1000):
self.assertIn(sample_space.experiment(), range(1, 7))

# Unfair die

# Rolling six die

# Picking cards
23 changes: 23 additions & 0 deletions tests/unit/test_sample_space.py
Expand Up @@ -66,3 +66,26 @@ def test_can_get_chances_of(self):
self.assertEqual(space.chances_of("H"), 0.33)
self.simple_events[0].outcome.assert_called_with()
self.simple_events[0].probability.assert_called_with()



class SampleSpaceExperimentTests(SampleSpaceTest):

def setUp(self):
SampleSpaceTest.setUp(self)
self.patch1 = patch("inferi.probability.SampleSpace.outcomes")
self.mock_outcomes = self.patch1.start()
self.mock_outcomes.return_value = set(["H", "T"])


def tearDown(self):
self.patch1.stop()


def test_can_run_statistical_experiment(self):
space = SampleSpace("H", "T")
results = [space.experiment() for _ in range(100)]
self.assertEqual(set(results), set(["H", "T"]))
self.assertGreaterEqual(results.count("H"), 35)
self.assertGreaterEqual(results.count("T"), 35)
self.mock_outcomes.assert_called_with()

0 comments on commit da9253a

Please sign in to comment.