forked from AllenDowney/ThinkStats2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormal.py
More file actions
419 lines (302 loc) · 10.3 KB
/
normal.py
File metadata and controls
419 lines (302 loc) · 10.3 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""This file contains code for use with "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
"""
from __future__ import print_function, division
import math
import numpy as np
import random
import scipy.stats
import first
import hypothesis
import thinkstats2
import thinkplot
class Normal(object):
"""Represents a Normal distribution"""
def __init__(self, mu, sigma2, label=''):
"""Initializes.
mu: mean
sigma2: variance
"""
self.mu = mu
self.sigma2 = sigma2
self.label = label
def __repr__(self):
"""Returns a string representation."""
if self.label:
return 'Normal(%g, %g, %s)' % (self.mu, self.sigma2, self.label)
else:
return 'Normal(%g, %g)' % (self.mu, self.sigma2)
__str__ = __repr__
@property
def sigma(self):
"""Returns the standard deviation."""
return math.sqrt(self.sigma2)
def __add__(self, other):
"""Adds a number or other Normal.
other: number or Normal
returns: new Normal
"""
if isinstance(other, Normal):
return Normal(self.mu + other.mu, self.sigma2 + other.sigma2)
else:
return Normal(self.mu + other, self.sigma2)
__radd__ = __add__
def __sub__(self, other):
"""Subtracts a number or other Normal.
other: number or Normal
returns: new Normal
"""
if isinstance(other, Normal):
return Normal(self.mu - other.mu, self.sigma2 + other.sigma2)
else:
return Normal(self.mu - other, self.sigma2)
__rsub__ = __sub__
def __mul__(self, factor):
"""Multiplies by a scalar.
factor: number
returns: new Normal
"""
return Normal(factor * self.mu, factor**2 * self.sigma2)
__rmul__ = __mul__
def __div__(self, divisor):
"""Divides by a scalar.
divisor: number
returns: new Normal
"""
return 1.0 / divisor * self
__truediv__ = __div__
def Sum(self, n):
"""Returns the distribution of the sum of n values.
n: int
returns: new Normal
"""
return Normal(n * self.mu, n * self.sigma2)
def Render(self):
"""Returns pair of xs, ys suitable for plotting.
"""
mean, std = self.mu, self.sigma
low, high = mean - 3 * std, mean + 3 * std
xs, ys = thinkstats2.RenderNormalCdf(mean, std, low, high)
return xs, ys
def Prob(self, x):
"""Cumulative probability of x.
x: numeric
"""
return thinkstats2.EvalNormalCdf(x, self.mu, self.sigma)
def Percentile(self, p):
"""Inverse CDF of p.
p: percentile rank 0-100
"""
return thinkstats2.EvalNormalCdfInverse(p/100, self.mu, self.sigma)
def NormalPlotSamples(samples, plot=1, ylabel=''):
"""Makes normal probability plots for samples.
samples: list of samples
label: string
"""
for n, sample in samples:
thinkplot.SubPlot(plot)
thinkstats2.NormalProbabilityPlot(sample)
thinkplot.Config(title='n=%d' % n,
legend=False,
xticks=[],
yticks=[],
ylabel=ylabel)
plot += 1
def MakeExpoSamples(beta=2.0, iters=1000):
"""Generates samples from an exponential distribution.
beta: parameter
iters: number of samples to generate for each size
returns: list of samples
"""
samples = []
for n in [1, 10, 100]:
sample = [np.sum(np.random.exponential(beta, n))
for _ in range(iters)]
samples.append((n, sample))
return samples
def MakeLognormalSamples(mu=1.0, sigma=1.0, iters=1000):
"""Generates samples from a lognormal distribution.
mu: parmeter
sigma: parameter
iters: number of samples to generate for each size
returns: list of samples
"""
samples = []
for n in [1, 10, 100]:
sample = [np.sum(np.random.lognormal(mu, sigma, n))
for _ in range(iters)]
samples.append((n, sample))
return samples
def MakeParetoSamples(alpha=1.0, iters=1000):
"""Generates samples from a Pareto distribution.
alpha: parameter
iters: number of samples to generate for each size
returns: list of samples
"""
samples = []
for n in [1, 10, 100]:
sample = [np.sum(np.random.pareto(alpha, n))
for _ in range(iters)]
samples.append((n, sample))
return samples
def GenerateCorrelated(rho, n):
"""Generates a sequence of correlated values from a standard normal dist.
rho: coefficient of correlation
n: length of sequence
returns: iterator
"""
x = random.gauss(0, 1)
yield x
sigma = math.sqrt(1 - rho**2)
for _ in range(n-1):
x = random.gauss(x * rho, sigma)
yield x
def GenerateExpoCorrelated(rho, n):
"""Generates a sequence of correlated values from an exponential dist.
rho: coefficient of correlation
n: length of sequence
returns: NumPy array
"""
normal = list(GenerateCorrelated(rho, n))
uniform = scipy.stats.norm.cdf(normal)
expo = scipy.stats.expon.ppf(uniform)
return expo
def MakeCorrelatedSamples(rho=0.9, iters=1000):
"""Generates samples from a correlated exponential distribution.
rho: correlation
iters: number of samples to generate for each size
returns: list of samples
"""
samples = []
for n in [1, 10, 100]:
sample = [np.sum(GenerateExpoCorrelated(rho, n))
for _ in range(iters)]
samples.append((n, sample))
return samples
def SamplingDistMean(data, n):
"""Computes the sampling distribution of the mean.
data: sequence of values representing the population
n: sample size
returns: Normal object
"""
mean, var = data.mean(), data.var()
dist = Normal(mean, var)
return dist.Sum(n) / n
def PlotPregLengths(live, firsts, others):
"""Plots sampling distribution of difference in means.
live, firsts, others: DataFrames
"""
print('prglngth example')
delta = firsts.prglngth.mean() - others.prglngth.mean()
print(delta)
dist1 = SamplingDistMean(live.prglngth, len(firsts))
dist2 = SamplingDistMean(live.prglngth, len(others))
dist = dist1 - dist2
print('null hypothesis', dist)
print(dist.Prob(-delta), 1 - dist.Prob(delta))
thinkplot.Plot(dist, label='null hypothesis')
thinkplot.Save(root='normal3',
xlabel='difference in means (weeks)',
ylabel='CDF')
class CorrelationPermute(hypothesis.CorrelationPermute):
"""Tests correlations by permutation."""
def TestStatistic(self, data):
"""Computes the test statistic.
data: tuple of xs and ys
"""
xs, ys = data
return np.corrcoef(xs, ys)[0][1]
def ResampleCorrelations(live):
"""Tests the correlation between birth weight and mother's age.
live: DataFrame for live births
returns: sample size, observed correlation, CDF of resampled correlations
"""
live2 = live.dropna(subset=['agepreg', 'totalwgt_lb'])
data = live2.agepreg.values, live2.totalwgt_lb.values
ht = CorrelationPermute(data)
p_value = ht.PValue()
return len(live2), ht.actual, ht.test_cdf
def StudentCdf(n):
"""Computes the CDF correlations from uncorrelated variables.
n: sample size
returns: Cdf
"""
ts = np.linspace(-3, 3, 101)
ps = scipy.stats.t.cdf(ts, df=n-2)
rs = ts / np.sqrt(n - 2 + ts**2)
return thinkstats2.Cdf(rs, ps)
def TestCorrelation(live):
"""Tests correlation analytically.
live: DataFrame for live births
"""
n, r, cdf = ResampleCorrelations(live)
model = StudentCdf(n)
thinkplot.Plot(model.xs, model.ps, color='gray',
alpha=0.3, label='Student t')
thinkplot.Cdf(cdf, label='sample')
thinkplot.Save(root='normal4',
xlabel='correlation',
ylabel='CDF')
t = r * math.sqrt((n-2) / (1-r**2))
p_value = 1 - scipy.stats.t.cdf(t, df=n-2)
print(r, p_value)
def ChiSquaredCdf(n):
"""Discrete approximation of the chi-squared CDF with df=n-1.
n: sample size
returns: Cdf
"""
xs = np.linspace(0, 25, 101)
ps = scipy.stats.chi2.cdf(xs, df=n-1)
return thinkstats2.Cdf(xs, ps)
def TestChiSquared():
"""Performs a chi-squared test analytically.
"""
data = [8, 9, 19, 5, 8, 11]
dt = hypothesis.DiceChiTest(data)
p_value = dt.PValue(iters=1000)
n, chi2, cdf = len(data), dt.actual, dt.test_cdf
model = ChiSquaredCdf(n)
thinkplot.Plot(model.xs, model.ps, color='gray',
alpha=0.3, label='chi squared')
thinkplot.Cdf(cdf, label='sample')
thinkplot.Save(root='normal5',
xlabel='chi-squared statistic',
ylabel='CDF',
loc='lower right')
# compute the p-value
p_value = 1 - scipy.stats.chi2.cdf(chi2, df=n-1)
print(chi2, p_value)
def MakeCltPlots():
"""Makes plot showing distributions of sums converging to normal.
"""
thinkplot.PrePlot(num=3, rows=2, cols=3)
samples = MakeExpoSamples()
NormalPlotSamples(samples, plot=1, ylabel='sum of expo values')
thinkplot.PrePlot(num=3)
samples = MakeLognormalSamples()
NormalPlotSamples(samples, plot=4, ylabel='sum of lognormal values')
thinkplot.Save(root='normal1', legend=False)
thinkplot.PrePlot(num=3, rows=2, cols=3)
samples = MakeParetoSamples()
NormalPlotSamples(samples, plot=1, ylabel='sum of Pareto values')
thinkplot.PrePlot(num=3)
samples = MakeCorrelatedSamples()
NormalPlotSamples(samples, plot=4, ylabel='sum of correlated expo values')
thinkplot.Save(root='normal2', legend=False)
def main():
thinkstats2.RandomSeed(17)
MakeCltPlots()
print('Gorilla example')
dist = Normal(90, 7.5**2)
print(dist)
dist_xbar = dist.Sum(9) / 9
print(dist_xbar.sigma)
print(dist_xbar.Percentile(5), dist_xbar.Percentile(95))
live, firsts, others = first.MakeFrames()
TestCorrelation(live)
PlotPregLengths(live, firsts, others)
TestChiSquared()
if __name__ == '__main__':
main()