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