Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/dice.py
1901 views
1
"""This file contains code for use with "Think Bayes",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2012 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
from __future__ import print_function, division
9
10
from thinkbayes2 import Suite
11
12
13
class Dice(Suite):
14
"""Represents hypotheses about which die was rolled."""
15
16
def Likelihood(self, data, hypo):
17
"""Computes the likelihood of the data under the hypothesis.
18
19
hypo: integer number of sides on the die
20
data: integer die roll
21
"""
22
if hypo < data:
23
return 0
24
else:
25
return 1.0/hypo
26
27
28
def main():
29
suite = Dice([4, 6, 8, 12, 20])
30
31
suite.Update(6)
32
print('After one 6')
33
suite.Print()
34
35
for roll in [4, 8, 7, 7, 2]:
36
suite.Update(roll)
37
38
print('After more rolls')
39
suite.Print()
40
41
42
if __name__ == '__main__':
43
main()
44
45