/
hypothesis_testing.py
572 lines (513 loc) · 24.1 KB
/
hypothesis_testing.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#exec(open('hypothesis_testing.py').read())
import subprocess as sp
import numpy as np
import pandas as pd
import importlib as il
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
import scipy.stats as stats
import plots
import confidence_intervals as ci
# ------------------------------------------------------------
# Type 2 Error for a two-sided hypothesis test
# ------------------------------------------------------------
def twoTailT2(alpha, mua, mu0, n, sigma):
delta = mua - mu0
return stats.norm.cdf(stats.norm.ppf(1 - (alpha/2)) - ((delta * math.sqrt(n)) / sigma)) - \
stats.norm.cdf(stats.norm.ppf(alpha/2) - ((delta * math.sqrt(n)) / sigma))
# ------------------------------------------------------------
# Type 2 Error for a one-sided lower-bound hypothesis test
# ------------------------------------------------------------
def oneTailT2Lo(alpha, mua, mu0, n, sigma):
delta = mua - mu0
return 1 - stats.norm.cdf(stats.norm.ppf(alpha) - ((delta * math.sqrt(n)) / sigma))
# ------------------------------------------------------------
# Type 2 Error for a one-sided upper-bound hypothesis test
# ------------------------------------------------------------
def oneTailT2Hi(alpha, mua, mu0, n, sigma):
delta = mua - mu0
return stats.norm.cdf(stats.norm.ppf(1 - alpha) - ((delta * math.sqrt(n)) / sigma))
# ------------------------------------------------------------
# Sample size to control Type 2 Error for a two-sided hypothesis test
# ------------------------------------------------------------
def twoTailT2N(alpha, mua, mu0, beta, sigma):
delta = mua - mu0
if(delta > 0):
return (sigma * (stats.norm.ppf(1 - (alpha/2)) - stats.norm.ppf(beta)) / delta)**2
else:
return (sigma * (stats.norm.ppf(alpha/2) - stats.norm.ppf(1 - beta)) / delta)**2
# ------------------------------------------------------------
# Sample size to control Type 2 Error for a one-sided lower-bound hypothesis test
# ------------------------------------------------------------
def oneTailT2NLo(alpha, mua, mu0, beta, sigma):
delta = mua - mu0
return (sigma * (stats.norm.ppf(alpha) - stats.norm.ppf(1 - beta)) / delta)**2
# ------------------------------------------------------------
# Sample size to control Type 2 Error for a one-sided upper-bound hypothesis test
# ------------------------------------------------------------
def oneTailT2NHi(alpha, mua, mu0, beta, sigma):
delta = mua - mu0
return (sigma * (stats.norm.ppf(1 - alpha) - stats.norm.ppf(beta)) / delta)**2
# ------------------------------------------------------------
# p-value for a two-tail hypothesis test
# ------------------------------------------------------------
def twoTailPvalue(n, mu0, sampmean, sigma = None, sampstd = None):
ret = None
if sigma is not None:
if sampmean > mu0:
ret = (1 - stats.norm.cdf(sampmean, loc = mu0, scale = sigma/math.sqrt(n))) * 2
else:
ret = stats.norm.cdf(sampmean, loc = mu0, scale = sigma/math.sqrt(n)) * 2
elif sampstd is not None:
if sampmean > mu0:
ret = (1 - stats.t.cdf(sampmean, df = n - 1, loc = mu0, scale = sampstd/math.sqrt(n))) * 2
else:
ret = stats.t.cdf(sampmean, df = n - 1, loc = mu0, scale = sampstd/math.sqrt(n)) * 2
return ret
# ------------------------------------------------------------
# p-value for a one-tail lower-bound hypothesis test
# ------------------------------------------------------------
def oneTailPvalueLo(n, mu0, sampmean, sigma = None, sampstd = None):
ret = None
if sigma is not None:
ret = stats.norm.cdf(sampmean, loc = mu0, scale = sigma/math.sqrt(n))
elif sampstd is not None:
ret = stats.t.cdf(sampmean, df = n - 1, loc = mu0, scale = sampstd/math.sqrt(n))
return ret
# ------------------------------------------------------------
# p-value for a one-tail upper-bound hypothesis test
# ------------------------------------------------------------
def oneTailPvalueHi(n, mu0, sampmean, sigma = None, sampstd = None):
ret = None
if sigma is not None:
ret = 1 - stats.norm.cdf(sampmean, loc = mu0, scale = sigma/math.sqrt(n))
elif sampstd is not None:
ret = 1 - stats.t.cdf(sampmean, df = n - 1, loc = mu0, scale = sampstd/math.sqrt(n))
return ret
if __name__ == '__main__':
sp.call('cls', shell = True)
il.reload(plots)
il.reload(ci)
plt.close('all')
# ----------------------------------------------------------------------
# Create a population of 1 million heights in cm with known mean and
# variance. This data set will be used for simulations.
# ----------------------------------------------------------------------
mu = 175
sigma = 5
population = np.random.randn(1000000) * sigma + mu;
mu = np.mean(population) # population mean
sigma = np.std(population, ddof = 0) # population standard deviation
print('Population Characteristics:')
print('Population mean = ' + str(round(mu, 2)))
print('Population standard deviation = ' + str(round(sigma, 2)))
print('Population range = [' +
str(round(min(population), 2)) + ',' +
str(round(max(population), 2)) + ']')
print('')
dfPop = pd.DataFrame(population, columns = ['height'])
# ----------------------------------------------------------------------
# Draw a random sample with which to perform hypothesis testing.
# ----------------------------------------------------------------------
n = 20
samp = np.random.choice(population, size = n)
sampmean = samp.mean()
sampstd = samp.std(ddof = 1)
print('Sample Characteristics:')
print('Sample mean = ' + str(round(sampmean, 2)))
print('Sample standard deviation = ' + str(round(sampstd, 2)))
print('Sample range = [' +
str(round(min(samp), 2)) + ',' +
str(round(max(samp), 2)) + ']')
print('')
print('----------------------------------------------------------------------')
print(' Two-sided Hypothesis Testing with Known Population Standard Deviation')
print('----------------------------------------------------------------------')
alpha = 0.05
mu0 = 175
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sigma = sigma/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = {0:.5} <= x <= {1:.5}, alpha = {2:.2}'.format(xlo, xhi, alpha))
if sampmean < xlo or sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 != {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 180
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sigma = sigma/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = {0:.5} <= x <= {1:.5}, alpha = {2:.2}'.format(xlo, xhi, alpha))
if sampmean < xlo or sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 != {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
print('----------------------------------------------------------------------')
print(' Two-sided Hypothesis Testing with Unknown Population Standard Deviation')
print('----------------------------------------------------------------------')
alpha = 0.05
mu0 = 175
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sampstd = sampstd/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = {0:.5} <= x <= {1:.5}, alpha = {2:.2}'.format(xlo, xhi, alpha))
if sampmean < xlo or sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 != {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 180
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sampstd = sampstd/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = {0:.5} <= x <= {1:.5}, alpha = {2:.2}'.format(xlo, xhi, alpha))
if sampmean < xlo or sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 != {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
print('----------------------------------------------------------------------')
print(' One-sided Lower-Bound Hypothesis Testing with Known Population Standard Deviation')
print('----------------------------------------------------------------------')
alpha = 0.05
mu0 = 175
xlo = ci.oneTailLo(alpha, n = n, sampmean = mu0, sigma = sigma/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x >= {0:.5} , alpha = {1:.2}'.format(xlo, alpha))
if sampmean < xlo:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 < {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 180
xlo = ci.oneTailLo(alpha, n = n, sampmean = mu0, sigma = sigma/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x >= {0:.5}, alpha = {1:.2}'.format(xlo, alpha))
if sampmean < xlo:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 < {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
print('----------------------------------------------------------------------')
print(' One-sided Lower-Bound Hypothesis Testing with Unknown Population Standard Deviation')
print('----------------------------------------------------------------------')
alpha = 0.05
mu0 = 175
xlo = ci.oneTailLo(alpha, n = n, sampmean = mu0, sampstd = sampstd/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x >= {0:.5} , alpha = {1:.2}'.format(xlo, alpha))
if sampmean < xlo:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 < {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 180
xlo = ci.oneTailLo(alpha, n = n, sampmean = mu0, sampstd = sampstd/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x >= {0:.5}, alpha = {1:.2}'.format(xlo, alpha))
if sampmean < xlo:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 < {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
print('----------------------------------------------------------------------')
print(' One-sided Upper-Bound Hypothesis Testing with Known Population Standard Deviation')
print('----------------------------------------------------------------------')
alpha = 0.05
mu0 = 175
xhi = ci.oneTailHi(alpha, n = n, sampmean = mu0, sigma = sigma/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x =< {0:.5} , alpha = {1:.2}'.format(xhi, alpha))
if sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 > {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 165
xhi = ci.oneTailHi(alpha, n = n, sampmean = mu0, sigma = sigma/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x =< {0:.5}, alpha = {1:.2}'.format(xhi, alpha))
if sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 > {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
print('----------------------------------------------------------------------')
print(' One-sided Upper-Bound Hypothesis Testing with Unknown Population Standard Deviation')
print('----------------------------------------------------------------------')
alpha = 0.05
mu0 = 175
xhi = ci.oneTailHi(alpha, n = n, sampmean = mu0, sampstd = sampstd/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x =< {0:.5} , alpha = {1:.2}'.format(xhi, alpha))
if sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 > {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 165
xhi = ci.oneTailHi(alpha, n = n, sampmean = mu0, sampstd = sampstd/math.sqrt(n))
print('Sample mean = {0:.5}'.format(sampmean))
print('Confidence Interval = x =< {0:.5}, alpha = {1:.2}'.format(xhi, alpha))
if sampmean > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 > {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
print('----------------------------------------------------------------------')
print(' P-value Hypothesis Tests')
print('----------------------------------------------------------------------')
sampmean = samp.mean()
sampstd = samp.std(ddof = 1)
mu0 = 177
pvalue = twoTailPvalue(n, mu0, sampmean, sigma = sigma)
if pvalue < alpha / 2:
msg = 'reject'
else:
msg = 'fail to reject'
print('For a two-sided test with known sigma, the pvalue = {0:.6}'.format(pvalue))
print(' {0} the null hypothesis'.format(msg))
pvalue = twoTailPvalue(n, mu0, sampmean, sampstd = sampstd)
if pvalue < alpha / 2:
msg = 'reject'
else:
msg = 'fail to reject'
print('For a two-sided test with unknown sigma, the pvalue = {0:.6}'.format(pvalue))
print(' {0} the null hypothesis'.format(msg))
mu0 = 177
pvalue = oneTailPvalueLo(n, mu0, sampmean, sigma = sigma)
if pvalue < alpha:
msg = 'reject'
else:
msg = 'fail to reject'
print('For a one-sided lower-bound test with known sigma, the pvalue = {0:.6}'.format(pvalue))
print(' {0} the null hypothesis'.format(msg))
pvalue = oneTailPvalueLo(n, mu0, sampmean, sampstd = sampstd)
if pvalue < alpha:
msg = 'reject'
else:
msg = 'fail to reject'
print('For a one-sided lower-bound test with unknown sigma, the pvalue = {0:.6}'.format(pvalue))
print(' {0} the null hypothesis'.format(msg))
mu0 = 173
pvalue = oneTailPvalueHi(n, mu0, sampmean, sigma = sigma)
if pvalue < alpha:
msg = 'reject'
else:
msg = 'fail to reject'
print('For a one-sided upper-bound test with known sigma, the pvalue = {0:.6}'.format(pvalue))
print(' {0} the null hypothesis'.format(msg))
pvalue = oneTailPvalueHi(n, mu0, sampmean, sampstd = sampstd)
if pvalue < alpha:
msg = 'reject'
else:
msg = 'fail to reject'
print('For a one-sided upper-bound test with unknown sigma, the pvalue = {0:.6}'.format(pvalue))
print(' {0} the null hypothesis'.format(msg))
print('')
# ----------------------------------------------------------------------
# Visualize Type 2 Error. NOTE: It is the area under the distribution
# centered at mua that falls in the acceptance region of the distribution
# centered at mu0.
# ----------------------------------------------------------------------
mu0 = 175
mua = 179
xmin = min(mu0, mua) - (5 * sigma / math.sqrt(n))
xmax = max(mu0, mua) + (5 * sigma / math.sqrt(n))
x = np.linspace(xmin, xmax, 500)
y0 = stats.norm.pdf(x, loc = mu0, scale = sigma/math.sqrt(n))
ya = stats.norm.pdf(x, loc = mua, scale = sigma/math.sqrt(n))
ymin = min(y0.min(), ya.min())
ymax = max(y0.max(), ya.max())
# plot both distributions
fig, ax = plots.scatter(x, y0
,ylim = (0, max(y0.max(), ya.max()))
,xlabel = 'height'
,ylabel = 'f(x)'
,markersize = 0
,linewidth = 2
,color = plots.BLUE)
plots.scatter(x, ya
,fig = fig
,ax = ax
,ylim = (0, max(y0.max(), ya.max()))
,xlabel = 'height'
,ylabel = 'f(x)'
,markersize = 0
,linewidth = 2
,color = plots.RED)
# find the acceptance region and fill it
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sigma = sigma / math.sqrt(n))
idx = np.multiply(x > xlo, x < xhi)
xaccept = x[idx]
ax.fill_between(xaccept, y0[idx], color = plots.BLUE)
# find the type 2 error region
idx = x < xhi
xt2 = x[idx]
ax.fill_between(xt2, ya[idx], color = plots.RED, zorder = 3)
# plot both means
ax.plot(np.array([mu0, mu0]), np.array([0, ymax])
,markersize = 0
,linewidth = 2
,color = plots.LIGHT_BLUE
,linestyle = 'dashed')
ax.plot(np.array([mua, mua]), np.array([0, ymax])
,markersize = 0
,linewidth = 2
,color = plots.LIGHT_RED
,linestyle = 'dashed')
legend =\
[
mpl.lines.Line2D([0], [0], color = plots.BLUE, linewidth = 2, label = 'Null Distribution')
,mpl.lines.Line2D([0], [0], color = plots.RED, linewidth = 2, label = 'Alternative Distribution')
,mpl.lines.Line2D([0], [0], color = plots.LIGHT_BLUE, linewidth = 2, linestyle = 'dashed', label = 'mu0 = {0}'.format(mu0))
,mpl.lines.Line2D([0], [0], color = plots.LIGHT_RED, linewidth = 2, linestyle = 'dashed', label = 'mua = {0}'.format(mua))
,mpl.patches.Patch(facecolor = plots.BLUE, label = '{0}% Probability Interval'.format(int((1 - alpha) * 100)))
,mpl.patches.Patch(facecolor = plots.RED, label = 'Probability of Type 2 Error')
]
ax.legend(handles = legend)
ax.set_title('Area Representing Probability of Type 2 Error')
fig.tight_layout()
print('----------------------------------------------------------------------')
print(' Type 2 Error Calculations')
print('----------------------------------------------------------------------')
mu0 = 175
mua = 179
beta = twoTailT2(alpha, mua, mu0, n, sigma)
print('For a two-sided hypothesis test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, n = {1}, and sigma = {2:.5},'.format(mua, n, sigma))
print(' the probability of Type 2 Error beta is {0:.4}'.format(beta))
mu0 = 175
mua = 171
beta = twoTailT2(alpha, mua, mu0, n, sigma)
print('For a two-sided hypothesis test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, n = {1}, and sigma = {2:.5},'.format(mua, n, sigma))
print(' the probability of Type 2 Error beta is {0:.4}'.format(beta))
mu0 = 175
mua = 171
beta = oneTailT2Lo(alpha, mua, mu0, n, sigma)
print('For a one-sided lower-bound hypothesis test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, n = {1}, and sigma = {2:.5},'.format(mua, n, sigma))
print(' the probability of Type 2 Error beta is {0:.4}'.format(beta))
mu0 = 175
mua = 179
beta = oneTailT2Hi(alpha, mua, mu0, n, sigma)
print('For a one-sided upper bound hypothesis test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, n = {1}, and sigma = {2:.5},'.format(mua, n, sigma))
print(' the probability of Type 2 Error beta is {0:.4}'.format(beta))
print('')
print('----------------------------------------------------------------------')
print(' Type 2 Error Sample Size Calculations')
print('----------------------------------------------------------------------')
mu0 = 175
mua = 179
delta = mua - mu0
beta = 0.01
n = twoTailT2N(alpha, mua, mu0, beta, sigma)
assert(abs(twoTailT2(alpha, mua, mu0, n, sigma) - beta) < 1e-6)
print('For a two-sided hypothesis test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, beta = {1}, and sigma = {2:.5},'.format(mua, beta, sigma))
print(' the sample size required is {0:.6}'.format(n))
mu0 = 175
mua = 171
delta = mua - mu0
beta = 0.01
n = twoTailT2N(alpha, mua, mu0, beta, sigma)
assert(abs(twoTailT2(alpha, mua, mu0, n, sigma) - beta) < 1e-6)
print('For a two-sided hypothesis test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, beta = {1}, and sigma = {2:.5},'.format(mua, beta, sigma))
print(' the sample size required is {0:.6}'.format(n))
mu0 = 175
mua = 171
delta = mua - mu0
beta = 0.01
n = oneTailT2NLo(alpha, mua, mu0, beta, sigma)
assert(abs(oneTailT2Lo(alpha, mua, mu0, n, sigma) - beta) < 1e-6)
print('For a one-sided hypothesis lower-bound test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, beta = {1}, and sigma = {2:.5},'.format(mua, beta, sigma))
print(' the sample size required is {0:.6}'.format(n))
mu0 = 175
mua = 179
delta = mua - mu0
beta = 0.01
n = oneTailT2NHi(alpha, mua, mu0, beta, sigma)
assert(abs(oneTailT2Hi(alpha, mua, mu0, n, sigma) - beta) < 1e-6)
print('For a one-sided hypothesis upper-bound test with alpha = {0}, mu0 = {1},'.format(alpha, mu0) +
' mua = {0}, beta = {1}, and sigma = {2:.5},'.format(mua, beta, sigma))
print(' the sample size required is {0:.6}'.format(n))
print('')
print('----------------------------------------------------------------------')
print(' Hypothesis Tests for a Population Proportion')
print('----------------------------------------------------------------------')
# find the 20th percentile
x20 = np.percentile(population, 20)
# true proportion of population belonging to class of interest
p = 0.2
# define a sample size and draw a sample
n = 40
samp = np.random.choice(population, size = n)
# find the proportion of items that belong to the class of interest in the sample
psamp = len(samp[samp <= x20]) / n
# approximate normal sampling distribution
sampmean = psamp
sampstd = math.sqrt(p * (1 - p) / n)
print('Sample proportion as mean = {0:.4}'.format(sampmean))
print('Sample standard deviation = {0:.4}'.format(sampstd))
print('')
# ----------------------------------------------------------------------
# two-sided hypothesis test
# ----------------------------------------------------------------------
mu0 = 0.2
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sigma = sampstd)
print('Two-sided confidence interval = {0:.4} <= x <= {1:.4}'.format(xlo, xhi))
if psamp < xlo or psamp > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 != {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 0.4
xlo, xhi = ci.twoTail(alpha, n = n, sampmean = mu0, sigma = sampstd)
print('Two-sided confidence interval = {0:.4} <= x <= {1:.4}'.format(xlo, xhi))
if psamp < xlo or psamp > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 != {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
# ----------------------------------------------------------------------
# one-sided lower-bound hypothesis test
# ----------------------------------------------------------------------
mu0 = 0.2
xlo = ci.oneTailLo(alpha, n = n, sampmean = mu0, sigma = sampstd)
print('One-sided lower-bound confidence interval = x >= {0:.4}'.format(xlo))
if psamp < xlo:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 < {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 0.4
xlo = ci.oneTailLo(alpha, n = n, sampmean = mu0, sigma = sampstd)
print('One-sided lower-bound confidence interval = x >= {0:.4}'.format(xlo))
if psamp < xlo:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 < {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
# ----------------------------------------------------------------------
# one-sided upper-bound hypothesis test
# ----------------------------------------------------------------------
mu0 = 0.2
xhi = ci.oneTailHi(alpha, n = n, sampmean = mu0, sigma = sampstd)
print('One-sided upper-bound confidence interval = x <= {0:.4}'.format(xhi))
if psamp > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 > {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
mu0 = 0.05
xhi = ci.oneTailHi(alpha, n = n, sampmean = mu0, sigma = sampstd)
print('One-sided upper-bound confidence interval = x <= {0:.4}'.format(xhi))
if psamp > xhi:
print('Reject H0: mu0 = {0} in favor of Ha: mu0 > {0}'.format(mu0))
else:
print('Fail to reject H0: mu0 = {0}'.format(mu0))
print('')
plt.show()