-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFB_Dice_script.py
67 lines (52 loc) · 1.48 KB
/
FB_Dice_script.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
### In this script, I calculate the probability that a dice has 4,6,8,12 or 20 faces
### based on a succession of observed rolls
# Full list version #
# def update(prior, data, hypothesis):
# lklhd = []
# posterior = prior
# norm = 0
# for d in hypothesis:
# if d < data:
# lklhd.append(0)
# else:
# lklhd.append(1.0 / d)
# for i in range(len(prior)):
# norm += prior[i] * lklhd[i]
# for j in range(len(prior)):
# posterior[j] = prior[j] * lklhd[j] / sum(norm)
# return posterior
#
# # Definition of the hypotheses
# hypothesis = [4, 6, 8, 12, 20]
# prior = [1.0/5]*5
# rolls = [6, 6, 8, 7, 7, 5, 4]
#
# # Initializing the posterior probability at the prior values
# posterior = prior
#for roll in rolls:
# posterior = update(posterior, roll, hypothesis)
# Full dictionary version #
def update(prior, roll, hypothesis):
posterior = {}
lklhd = {}
norm = 0
for h in hypothesis:
if h < roll:
lklhd[h] = 0
else:
lklhd[h] = 1.0 / h
posterior[h] = prior[h] * lklhd[h]
norm = sum(posterior.values())
for h in hypothesis:
posterior[h] = posterior[h] / norm
return posterior
hypothesis = input('What dice do you have? ')
n = len(hypothesis)
prior = {}
for h in hypothesis:
prior[h] = 1.0 / n
posterior = prior
rolls = input('What rolls did you observe? ')
for r in rolls:
posterior = update(posterior, r, hypothesis)
print(posterior)