-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbayes.py
More file actions
49 lines (39 loc) · 1.41 KB
/
bayes.py
File metadata and controls
49 lines (39 loc) · 1.41 KB
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
class Bayes(object):
'''
INPUT:
prior (dict): key is the value (e.g. 4-sided die),
value is the probability
likelihood_func (function): takes a new piece of data and the value and
outputs the likelihood of getting that data
'''
def __init__(self, prior, likelihood_func):
self.prior = prior
self.likelihood_func = likelihood_func
def normalize(self):
'''
INPUT: None
OUTPUT: None
Makes the sum of the probabilities equal 1.
'''
total = sum(self.prior.values())
for k, v in self.prior.items():
self.prior[k] = self.prior[k] / total
def update(self, data):
'''
INPUT:
data (int or str): A single observation (data point)
OUTPUT: None
Conduct a bayesian update. Multiply the prior by the likelihood and
make this the new prior.
'''
for die, prior in self.prior.items():
self.prior[die] = self.prior[die] * self.likelihood_func(data, die)
# fair_posterior = (p_fair_prior * p_head_fair)
self.normalize()
def print_distribution(self):
'''
Print the current posterior probability.
'''
for key in sorted(self.prior.keys()):
print('For die {} the posterior prob is {}'.format(
key, self.prior[key]))