Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96160
License: OTHER
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 dice import Dice
9
import thinkplot
10
11
class Train(Dice):
12
"""The likelihood function for the train problem is the same as
13
for the Dice problem."""
14
15
16
def Mean(suite):
17
total = 0
18
for hypo, prob in suite.Items():
19
total += hypo * prob
20
return total
21
22
23
def MakePosterior(high, dataset):
24
hypos = xrange(1, high+1)
25
suite = Train(hypos)
26
suite.name = str(high)
27
28
for data in dataset:
29
suite.Update(data)
30
31
thinkplot.Pmf(suite)
32
return suite
33
34
35
def main():
36
dataset = [30, 60, 90]
37
38
for high in [500, 1000, 2000]:
39
suite = MakePosterior(high, dataset)
40
print high, suite.Mean()
41
42
thinkplot.Save(root='train2',
43
xlabel='Number of trains',
44
ylabel='Probability')
45
46
47
if __name__ == '__main__':
48
main()
49
50