forked from AllenDowney/DataScience
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelay_soln.py
63 lines (46 loc) · 1.57 KB
/
relay_soln.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
"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2014 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import thinkstats2
import thinkplot
import relay
def BiasPmf(pmf, speed, name=None):
"""Returns a new Pmf representing speeds observed at a given speed.
The chance of observing a runner is proportional to the difference
in speed.
Args:
pmf: distribution of actual speeds
speed: speed of the observing runner
name: string name for the new dist
Returns:
Pmf object
"""
new = pmf.Copy(name=name)
for val, prob in new.Items():
diff = abs(val - speed)
new.Mult(val, diff)
new.Normalize()
return new
def main():
results = relay.ReadResults()
speeds = relay.GetSpeeds(results)
# plot the distribution of actual speeds
pmf = thinkstats2.MakePmfFromList(speeds, 'actual speeds')
# plot the biased distribution seen by the observer
biased = BiasPmf(pmf, 7.5, name='observed speeds')
thinkplot.Hist(biased)
thinkplot.Save(root='observed_speeds',
title='PMF of running speed',
xlabel='speed (mph)',
ylabel='probability')
cdf = thinkstats2.MakeCdfFromPmf(biased)
thinkplot.Clf()
thinkplot.Cdf(cdf)
thinkplot.Save(root='observed_speeds_cdf',
title='CDF of running speed',
xlabel='speed (mph)',
ylabel='cumulative probability')
if __name__ == '__main__':
main()