Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Think Stats by Allen B. Downey Think Stats is an introduction to Probability and Statistics for Python programmers.

This is the accompanying code for this book.

Website: http://greenteapress.com/wp/think-stats-2e/

Views: 7115
License: GPL3
1
"""This file contains code used in "Think Stats",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2014 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
import thinkstats2
9
import thinkplot
10
11
import relay
12
13
14
def ObservedPmf(pmf, speed, label=None):
15
"""Returns a new Pmf representing speeds observed at a given speed.
16
17
The chance of observing a runner is proportional to the difference
18
in speed.
19
20
Args:
21
pmf: distribution of actual speeds
22
speed: speed of the observing runner
23
label: string label for the new dist
24
25
Returns:
26
Pmf object
27
"""
28
new = pmf.Copy(label=label)
29
for val in new.Values():
30
diff = abs(val - speed)
31
new.Mult(val, diff)
32
new.Normalize()
33
return new
34
35
36
def main():
37
results = relay.ReadResults()
38
speeds = relay.GetSpeeds(results)
39
speeds = relay.BinData(speeds, 3, 12, 100)
40
41
# plot the distribution of actual speeds
42
pmf = thinkstats2.Pmf(speeds, 'actual speeds')
43
44
# plot the biased distribution seen by the observer
45
biased = ObservedPmf(pmf, 7.5, label='observed speeds')
46
47
thinkplot.Pmf(biased)
48
thinkplot.Save(root='observed_speeds',
49
title='PMF of running speed',
50
xlabel='speed (mph)',
51
ylabel='PMF')
52
53
cdf = thinkstats2.Cdf(pmf)
54
cdf_biased = thinkstats2.Cdf(biased)
55
56
thinkplot.PrePlot(2)
57
thinkplot.Cdfs([cdf, cdf_biased])
58
thinkplot.Save(root='observed_speeds_cdf',
59
title='CDF of running speed',
60
xlabel='speed (mph)',
61
ylabel='CDF')
62
63
64
if __name__ == '__main__':
65
main()
66
67