forked from AllenDowney/ThinkBayes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paintball.py
213 lines (151 loc) · 5.3 KB
/
paintball.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""This file contains code used in "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 matplotlib.pyplot as pyplot
import thinkplot
import math
import sys
FORMATS = ['pdf', 'eps', 'png']
def StrafingSpeed(alpha, beta, x):
"""Computes strafing speed, given location of shooter and impact.
alpha: x location of shooter
beta: y location of shooter
x: location of impact
Returns: derivative of x with respect to theta
"""
theta = math.atan2(x - alpha, beta)
speed = beta / math.cos(theta)**2
return speed
def MakeLocationPmf(alpha, beta, locations):
"""Computes the Pmf of the locations, given alpha and beta.
Given that the shooter is at coordinates (alpha, beta),
the probability of hitting any spot is inversely proportionate
to the strafe speed.
alpha: x position
beta: y position
locations: x locations where the pmf is evaluated
Returns: Pmf object
"""
pmf = thinkbayes.Pmf()
for x in locations:
prob = 1.0 / StrafingSpeed(alpha, beta, x)
pmf.Set(x, prob)
pmf.Normalize()
return pmf
class Paintball(thinkbayes.Suite, thinkbayes.Joint):
"""Represents hypotheses about the location of an opponent."""
def __init__(self, alphas, betas, locations):
"""Makes a joint suite of parameters alpha and beta.
Enumerates all pairs of alpha and beta.
Stores locations for use in Likelihood.
alphas: possible values for alpha
betas: possible values for beta
locations: possible locations along the wall
"""
self.locations = locations
pairs = [(alpha, beta)
for alpha in alphas
for beta in betas]
thinkbayes.Suite.__init__(self, pairs)
def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: pair of alpha, beta
data: location of a hit
Returns: float likelihood
"""
alpha, beta = hypo
x = data
pmf = MakeLocationPmf(alpha, beta, self.locations)
like = pmf.Prob(x)
return like
def MakePmfPlot(alpha = 10):
"""Plots Pmf of location for a range of betas."""
locations = range(0, 31)
betas = [10, 20, 40]
thinkplot.PrePlot(num=len(betas))
for beta in betas:
pmf = MakeLocationPmf(alpha, beta, locations)
pmf.name = 'beta = %d' % beta
thinkplot.Pmf(pmf)
thinkplot.Save('paintball1',
xlabel='Distance',
ylabel='Prob',
formats=FORMATS)
def MakePosteriorPlot(suite):
"""Plots the posterior marginal distributions for alpha and beta.
suite: posterior joint distribution of location
"""
marginal_alpha = suite.Marginal(0)
marginal_alpha.name = 'alpha'
marginal_beta = suite.Marginal(1)
marginal_beta.name = 'beta'
print 'alpha CI', marginal_alpha.CredibleInterval(50)
print 'beta CI', marginal_beta.CredibleInterval(50)
thinkplot.PrePlot(num=2)
#thinkplot.Pmf(marginal_alpha)
#thinkplot.Pmf(marginal_beta)
thinkplot.Cdf(thinkbayes.MakeCdfFromPmf(marginal_alpha))
thinkplot.Cdf(thinkbayes.MakeCdfFromPmf(marginal_beta))
thinkplot.Save('paintball2',
xlabel='Distance',
ylabel='Prob',
loc=4,
formats=FORMATS)
def MakeConditionalPlot(suite):
"""Plots marginal CDFs for alpha conditioned on beta.
suite: posterior joint distribution of location
"""
betas = [10, 20, 40]
thinkplot.PrePlot(num=len(betas))
for beta in betas:
cond = suite.Conditional(0, 1, beta)
cond.name = 'beta = %d' % beta
thinkplot.Pmf(cond)
thinkplot.Save('paintball3',
xlabel='Distance',
ylabel='Prob',
formats=FORMATS)
def MakeContourPlot(suite):
"""Plots the posterior joint distribution as a contour plot.
suite: posterior joint distribution of location
"""
thinkplot.Contour(suite.GetDict(), contour=False, pcolor=True)
thinkplot.Save('paintball4',
xlabel='alpha',
ylabel='beta',
axis=[0, 30, 0, 20],
formats=FORMATS)
def MakeCrediblePlot(suite):
"""Makes a plot showing several two-dimensional credible intervals.
suite: Suite
"""
d = dict((pair, 0) for pair in suite.Values())
percentages = [75, 50, 25]
for p in percentages:
interval = suite.MaxLikeInterval(p)
for pair in interval:
d[pair] += 1
thinkplot.Contour(d, contour=False, pcolor=True)
pyplot.text(17, 4, '25', color='white')
pyplot.text(17, 15, '50', color='white')
pyplot.text(17, 30, '75')
thinkplot.Save('paintball5',
xlabel='alpha',
ylabel='beta',
formats=FORMATS)
def main(script):
alphas = range(0, 31)
betas = range(1, 51)
locations = range(0, 31)
suite = Paintball(alphas, betas, locations)
suite.UpdateSet([15, 16, 18, 21])
MakeCrediblePlot(suite)
MakeContourPlot(suite)
MakePosteriorPlot(suite)
MakeConditionalPlot(suite)
MakePmfPlot()
if __name__ == '__main__':
main(*sys.argv)