Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign 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: 7088
License: GPL3
1
"""This file contains code for use with "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
from __future__ import print_function
9
10
import sys
11
from operator import itemgetter
12
13
import first
14
import thinkstats2
15
16
17
def Mode(hist):
18
"""Returns the value with the highest frequency.
19
20
hist: Hist object
21
22
returns: value from Hist
23
"""
24
p, x = max([(p, x) for x, p in hist.Items()])
25
return x
26
27
28
def AllModes(hist):
29
"""Returns value-freq pairs in decreasing order of frequency.
30
31
hist: Hist object
32
33
returns: iterator of value-freq pairs
34
"""
35
return sorted(hist.Items(), key=itemgetter(1), reverse=True)
36
37
38
def WeightDifference(live, firsts, others):
39
"""Explore the difference in weight between first babies and others.
40
41
live: DataFrame of all live births
42
firsts: DataFrame of first babies
43
others: DataFrame of others
44
"""
45
mean0 = live.totalwgt_lb.mean()
46
mean1 = firsts.totalwgt_lb.mean()
47
mean2 = others.totalwgt_lb.mean()
48
49
var1 = firsts.totalwgt_lb.var()
50
var2 = others.totalwgt_lb.var()
51
52
print('Mean')
53
print('First babies', mean1)
54
print('Others', mean2)
55
56
print('Variance')
57
print('First babies', var1)
58
print('Others', var2)
59
60
print('Difference in lbs', mean1 - mean2)
61
print('Difference in oz', (mean1 - mean2) * 16)
62
63
print('Difference relative to mean (%age points)',
64
(mean1 - mean2) / mean0 * 100)
65
66
d = thinkstats2.CohenEffectSize(firsts.totalwgt_lb, others.totalwgt_lb)
67
print('Cohen d', d)
68
69
70
def main(script):
71
"""Tests the functions in this module.
72
73
script: string script name
74
"""
75
live, firsts, others = first.MakeFrames()
76
hist = thinkstats2.Hist(live.prglngth)
77
78
# explore the weight difference between first babies and others
79
WeightDifference(live, firsts, others)
80
81
# test Mode
82
mode = Mode(hist)
83
print('Mode of preg length', mode)
84
assert(mode == 39)
85
86
# test AllModes
87
modes = AllModes(hist)
88
assert(modes[0][1] == 4693)
89
90
for value, freq in modes[:5]:
91
print(value, freq)
92
93
print('%s: All tests passed.' % script)
94
95
96
if __name__ == '__main__':
97
main(*sys.argv)
98
99