-
Notifications
You must be signed in to change notification settings - Fork 0
/
motivating.py
50 lines (40 loc) · 1.48 KB
/
motivating.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
import math
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from scipy.stats import beta
THETA = 0.5
TRIAL_COUNT = 20
class BetaDistribution:
def __init__(self, a, b):
self.a = a
self.b = b
def compute_c1(a, b):
return math.factorial((a + b - 1)) / (math.factorial((a - 1)) * math.factorial((b - 1)))
def compute_c2(outcome, N):
return math.factorial(N) / (math.factorial(outcome) * math.factorial(N - outcome))
def compute_c3(a, b, outcome, N):
return math.factorial(a + b + N - 1) / (math.factorial(a + outcome - 1) * math.factorial(b + N - outcome - 1))
def update_distribution(prior, outcome):
if outcome: # heads
return BetaDistribution(prior.a + 1, prior.b)
else:
return BetaDistribution(prior.a, prior.b + 1)
def get_coin_flip():
return np.random.uniform(0, 1) < THETA
x = np.arange (0, 1000) / 1000
values = [((0, 0, 1), BetaDistribution(15, 5)), ((1, 0, 0), BetaDistribution(1, 1))]
for color_components, prior in values:
for trial_number in range(TRIAL_COUNT):
y = beta.pdf(x, prior.a, prior.b)
opacity = trial_number / TRIAL_COUNT * 0.9 + 0.1
color = (*color_components, opacity)
plt.plot(x, y, color=color)
outcome = get_coin_flip()
prior = update_distribution(prior, outcome)
print(f'Largest y at: {np.argmax(y) / 1000}')
plt.xlabel('Theta')
plt.ylabel('Probability')
plt.title('Beta Prior Convergence')
plt.savefig('test.png')