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