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 sys
9
10
import thinkstats2
11
import thinkplot
12
13
14
def ReadFile(filename):
15
"""Reads a list of numbers from a file.
16
17
filename: string
18
19
returns: list of float
20
"""
21
fp = open(filename)
22
data = []
23
for line in fp:
24
x = float(line.strip())
25
data.append(x)
26
return data
27
28
29
def main(script, filename='mystery0.dat'):
30
data = ReadFile(filename)
31
cdf = thinkstats2.Cdf(data)
32
33
thinkplot.PrePlot(num=6, rows=2, cols=3)
34
thinkplot.SubPlot(1)
35
thinkplot.Cdf(cdf, color='C0', label=filename)
36
thinkplot.Config(title='CDF on linear scale', ylabel='CDF')
37
38
thinkplot.SubPlot(2)
39
scale = thinkplot.Cdf(cdf, xscale='log', color='C0')
40
thinkplot.Config(title='CDF on log-x scale', ylabel='CDF', **scale)
41
42
thinkplot.SubPlot(3)
43
scale = thinkplot.Cdf(cdf, transform='exponential', color='C0')
44
thinkplot.Config(title='CCDF on log-y scale', ylabel='log CCDF', **scale)
45
46
thinkplot.SubPlot(4)
47
xs, ys = thinkstats2.NormalProbability(data)
48
thinkplot.Plot(xs, ys, color='C0')
49
thinkplot.Config(title='Normal probability plot',
50
xlabel='random normal', ylabel='data')
51
52
thinkplot.SubPlot(5)
53
scale = thinkplot.Cdf(cdf, transform='pareto', color='C0')
54
thinkplot.Config(title='CCDF on log-log scale', ylabel='log CCDF', **scale)
55
56
thinkplot.SubPlot(6)
57
scale = thinkplot.Cdf(cdf, transform='weibull', color='C0')
58
thinkplot.Config(title='CCDF on loglog-y log-x scale',
59
ylabel='log log CCDF', **scale)
60
61
thinkplot.Show(legend=False)
62
63
64
if __name__ == '__main__':
65
main(*sys.argv)
66
67