forked from AllenDowney/ThinkBayes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train3.py
98 lines (68 loc) · 2.32 KB
/
train3.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""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
"""
import thinkbayes
import thinkplot
from thinkbayes import Pmf, Percentile
from dice import Dice
class Train(Dice):
"""Represents hypotheses about how many trains the company has."""
class Train2(Dice):
"""Represents hypotheses about how many trains the company has."""
def __init__(self, hypos, alpha=1.0):
"""Initializes the hypotheses with a power law distribution.
hypos: sequence of hypotheses
alpha: parameter of the power law prior
"""
Pmf.__init__(self)
for hypo in hypos:
self.Set(hypo, hypo**(-alpha))
self.Normalize()
def MakePosterior(high, dataset, constructor):
"""Makes and updates a Suite.
high: upper bound on the range of hypotheses
dataset: observed data to use for the update
constructor: function that makes a new suite
Returns: posterior Suite
"""
hypos = xrange(1, high+1)
suite = constructor(hypos)
suite.name = str(high)
for data in dataset:
suite.Update(data)
return suite
def ComparePriors():
"""Runs the analysis with two different priors and compares them."""
dataset = [60]
high = 1000
thinkplot.Clf()
thinkplot.PrePlot(num=2)
constructors = [Train, Train2]
labels = ['uniform', 'power law']
for constructor, label in zip(constructors, labels):
suite = MakePosterior(high, dataset, constructor)
suite.name = label
thinkplot.Pmf(suite)
thinkplot.Save(root='train4',
xlabel='Number of trains',
ylabel='Probability')
def main():
ComparePriors()
dataset = [30, 60, 90]
thinkplot.Clf()
thinkplot.PrePlot(num=3)
for high in [500, 1000, 2000]:
suite = MakePosterior(high, dataset, Train2)
print high, suite.Mean()
thinkplot.Save(root='train3',
xlabel='Number of trains',
ylabel='Probability')
interval = Percentile(suite, 5), Percentile(suite, 95)
print interval
cdf = thinkbayes.MakeCdfFromPmf(suite)
interval = cdf.Percentile(5), cdf.Percentile(95)
print interval
if __name__ == '__main__':
main()