/
_06_descriptive.py
189 lines (140 loc) · 4.47 KB
/
_06_descriptive.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
"""
This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
NAME: _06_descriptive.py
"""
import math
import matplotlib.pyplot as pyplot
import _01_survey
import _02_first
import _03_thinkstats
import _04_Pmf
import _05_myplot
def _process(table, name):
"""Runs various analyses on this table."""
_02_first._process(table)
table.name = name
table.var = _03_thinkstats._var(table.lengths, table.mu)
table.trim = _03_thinkstats._trimmed_mean(table.lengths)
table.hist = _04_Pmf._make_hist_from_list(table.lengths, name=name)
table.pmf = _04_Pmf._make_pmf_from_hist(table.hist)
def _pool_records(*tables):
"""
Construct a table with records from all tables.
Args:
constructor: init method used to make the new table
tables: any number of tables
Returns:
new table object
"""
pool = _01_survey.Pregnancies()
for table in tables:
pool._extend_records(table.records)
return pool
def _make_tables(data_dir='.'):
"""Reads survey data and returns a tuple of Tables"""
table, firsts, others = _02_first._make_tables(data_dir)
pool = _pool_records(firsts, others)
_process(pool, 'live births')
_process(firsts, 'first babies')
_process(others, 'others')
return pool, firsts, others
def _summarize(pool, firsts, others):
"""Print various summary statistics."""
print
print('Variance')
print('First babies', firsts.var)
print('Others', others.var)
diff_mu = firsts.mu - others.mu
print('Difference in mean', diff_mu)
sigma = math.sqrt(pool.var)
print('Pooled mean', pool.mu)
print('Pooled variance', pool.var)
print('Pooled sigma', sigma)
print(firsts.mu, others.mu)
print(firsts.trim, others.trim)
# live_lengths = pool.hist._get_dict().items()
live_lengths = list(pool.hist._get_dict().items())
# live_lengths.sort()
sorted(live_lengths)
print('Shortest lengths:')
for weeks, count in live_lengths[:10]:
print(weeks, count)
print('Longest lengths:')
for weeks, count in live_lengths[-10:]:
print(weeks, count)
def _make_figures(firsts, others):
"""Plot Hists and Pmfs for the pregnancy length."""
# bar options is a list of option dictionaries to be passed to myplot.bar
bar_options = [
dict(color='0.9'),
dict(color='blue')
]
# make the histogram
axis = [23, 46, 0, 2700]
_hists([firsts.hist, others.hist])
_05_myplot._save(root='nsfg_hist',
title='Histogram',
xlabel='weeks',
ylabel='frequency',
axis=axis)
# make the PMF
axis = [23, 46, 0, 0.6]
_hists([firsts.pmf, others.pmf])
_05_myplot._save(root='nsfg_pmf',
title='PMF',
xlabel='weeks',
ylabel='probability',
axis=axis)
def _hists(hists):
"""
Plot two histograms on the same axes.
hists: list of Hist
"""
width = 0.4
shifts = [-width, 0.0]
option_list = [
dict(color='0.9'),
dict(color='blue')
]
pyplot.clf()
for i, hist in enumerate(hists):
xs, fs = hist._render()
xs = _shift(xs, shifts[i])
pyplot.bar(xs, fs, label=hist.name, width=width, **option_list[i])
def _shift(xs, shift):
"""
Adds a constant to a sequence of values.
Args:
xs: sequence of values
shift: value to add
Returns:
sequence of numbers
"""
return [x + shift for x in xs]
def _make_diff_figure(firsts, others):
"""Plot the difference between the PMFs."""
weeks = range(35, 46)
diffs = []
for week in weeks:
p1 = firsts.pmf._prob(week)
p2 = others.pmf._prob(week)
diff = 100 * (p1 - p2)
diffs.append(diff)
pyplot.clf()
pyplot.bar(weeks, diffs, align='center')
_05_myplot._save(root='nsfg_diffs',
title='Difference in PMFs',
xlabel='weeks',
ylabel='100 (PMF$_{first}$ - PMF$_{other}$)',
legend=False)
def main(name, data_dir=''):
pool, firsts, others = _make_tables(data_dir)
_summarize(pool, firsts, others)
_make_figures(firsts, others)
_make_diff_figure(firsts, others)
if __name__ == '__main__':
import sys
main(*sys.argv)