Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96164
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
import thinkbayes
9
import thinkplot
10
11
from thinkbayes import Pmf, Percentile
12
from dice import Dice
13
14
15
class Train(Dice):
16
"""Represents hypotheses about how many trains the company has."""
17
18
19
class Train2(Dice):
20
"""Represents hypotheses about how many trains the company has."""
21
22
def __init__(self, hypos, alpha=1.0):
23
"""Initializes the hypotheses with a power law distribution.
24
25
hypos: sequence of hypotheses
26
alpha: parameter of the power law prior
27
"""
28
Pmf.__init__(self)
29
for hypo in hypos:
30
self.Set(hypo, hypo**(-alpha))
31
self.Normalize()
32
33
34
def MakePosterior(high, dataset, constructor):
35
"""Makes and updates a Suite.
36
37
high: upper bound on the range of hypotheses
38
dataset: observed data to use for the update
39
constructor: function that makes a new suite
40
41
Returns: posterior Suite
42
"""
43
hypos = xrange(1, high+1)
44
suite = constructor(hypos)
45
suite.name = str(high)
46
47
for data in dataset:
48
suite.Update(data)
49
50
return suite
51
52
53
def ComparePriors():
54
"""Runs the analysis with two different priors and compares them."""
55
dataset = [60]
56
high = 1000
57
58
thinkplot.Clf()
59
thinkplot.PrePlot(num=2)
60
61
constructors = [Train, Train2]
62
labels = ['uniform', 'power law']
63
64
for constructor, label in zip(constructors, labels):
65
suite = MakePosterior(high, dataset, constructor)
66
suite.name = label
67
thinkplot.Pmf(suite)
68
69
thinkplot.Save(root='train4',
70
xlabel='Number of trains',
71
ylabel='Probability')
72
73
def main():
74
ComparePriors()
75
76
dataset = [30, 60, 90]
77
78
thinkplot.Clf()
79
thinkplot.PrePlot(num=3)
80
81
for high in [500, 1000, 2000]:
82
suite = MakePosterior(high, dataset, Train2)
83
print high, suite.Mean()
84
85
thinkplot.Save(root='train3',
86
xlabel='Number of trains',
87
ylabel='Probability')
88
89
interval = Percentile(suite, 5), Percentile(suite, 95)
90
print interval
91
92
cdf = thinkbayes.MakeCdfFromPmf(suite)
93
interval = cdf.Percentile(5), cdf.Percentile(95)
94
print interval
95
96
97
if __name__ == '__main__':
98
main()
99
100