-
Notifications
You must be signed in to change notification settings - Fork 0
/
probability.py
86 lines (53 loc) · 2.24 KB
/
probability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import random
class SimpleEvent:
"""A simple event - a single outcome of a statistical experiment.
:param outcome: The result of this event occuring.
:param float probability: The probability of this event occuring.
:raises TypeError: if probability isn't numeric.
:raises ValueError: if probability is not between 0 and 1."""
def __init__(self, outcome, probability):
self._outcome = outcome
if not isinstance(probability, (int, float)):
raise TypeError("probability {} is not numeric".format(probability))
if not 0 <= probability <= 1:
raise ValueError("probability {} is invalid".format(probability))
self._probability = probability
def __repr__(self):
return "<SimpleEvent: {}>".format(self._outcome)
def outcome(self):
"""The result of this event occuring."""
return self._outcome
def probability(self):
"""The probability of this event occuring."""
return self._probability
class SampleSpace:
"""The set of all possible things that can result from a statistical
experiment.
:param \*simple_events: All the possible outcomes."""
def __init__(self, *simple_events):
p = 1 / len(simple_events)
self._simple_events = set([SimpleEvent(e, p) for e in simple_events])
def __repr__(self):
return "<SampleSpace ({} simple events)>".format(
len(self._simple_events)
)
def chances_of(self, outcome):
"""Returns the probability of the given outcome occuring in a single
statistical experiment.
:param outcome: The outcome to test for."""
for e in self._simple_events:
if e.outcome() == outcome:
return e.probability()
return 0
def simple_events(self):
"""The set of simple events in this sample space.
:rtype: ``set``"""
return set(self._simple_events)
def outcomes(self):
"""The set of outcomes that the sample space's simple events can
produce.
: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]