-
Notifications
You must be signed in to change notification settings - Fork 209
/
train2.py
64 lines (48 loc) · 1.7 KB
/
train2.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
64
"""This file contains code for use with "Think Bayes",
by Allen B. Downey, available from greenteapress.com
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from __future__ import print_function, division
import thinkbayes
import thinkplot
class Train(thinkbayes.Suite):
"""Represents hypotheses about how many trains the company has.
The likelihood function for the train problem is the same as
for the Dice problem.
"""
def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: number of trains the carrier operates
data: the number of the observed train
"""
if hypo < data:
return 0
else:
return 1.0/hypo
def main():
hypos = range(1, 101)
suite = Train(hypos)
suite.Update(25)
print('Posterior mean', suite.Mean())
print('Posterior MLE', suite.MaximumLikelihood())
print('Posterior CI 90', suite.CredibleInterval(90))
thinkplot.PrePlot(1)
thinkplot.Pmf(suite, linewidth=5)
thinkplot.Save(root='train2',
xlabel='Number of trains',
ylabel='Probability',
formats=['png'])
thinkplot.Pmf(suite, linewidth=5, color='0.8')
suite.Update(42)
print('Posterior mean', suite.Mean())
print('Posterior MLE', suite.MaximumLikelihood())
print('Posterior CI 90', suite.CredibleInterval(90))
thinkplot.PrePlot(1)
thinkplot.Pmf(suite, linewidth=5)
thinkplot.Save(root='train3',
xlabel='Number of trains',
ylabel='Probability',
formats=['png'])
if __name__ == '__main__':
main()