-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation-Inference-Figure.py
More file actions
64 lines (56 loc) · 1.94 KB
/
Copy pathSimulation-Inference-Figure.py
File metadata and controls
64 lines (56 loc) · 1.94 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
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
# set the random seed:
np.random.seed(123456)
# set sample size and MC simulations:
r = 10000
n = 100
# initialize arrays to later store results:
CIlower = np.empty(r)
CIupper = np.empty(r)
pvalue1 = np.empty(r)
pvalue2 = np.empty(r)
# repeat r times:
for j in range(r):
# draw a sample:
sample = stats.norm.rvs(10, 2, size=n)
sample_mean = np.mean(sample)
sample_sd = np.std(sample, ddof=1)
# test the (correct) null hypothesis mu=10:
testres1 = stats.ttest_1samp(sample, popmean=10)
pvalue1[j] = testres1.pvalue
cv = stats.t.ppf(0.975, df=n - 1)
CIlower[j] = sample_mean - cv * sample_sd / np.sqrt(n)
CIupper[j] = sample_mean + cv * sample_sd / np.sqrt(n)
# test the (incorrect) null hypothesis mu=9.5 & store the p value:
testres2 = stats.ttest_1samp(sample, popmean=9.5)
pvalue2[j] = testres2.pvalue
##################
## correct H0 ##
##################
plt.figure(figsize=(3, 5)) # set figure ratio
plt.ylim(0, 101)
plt.xlim(9, 11)
for j in range(1, 101):
if 10 > CIlower[j] and 10 < CIupper[j]:
plt.plot([CIlower[j], CIupper[j]], [j, j], linestyle='-', color='grey')
else:
plt.plot([CIlower[j], CIupper[j]], [j, j], linestyle='-', color='black')
plt.axvline(10, linestyle='--', color='black', linewidth=0.5)
plt.ylabel('Sample No.')
plt.savefig('PyGraphs/Simulation-Inference-Figure1.pdf')
##################
## incorrect H0 ##
##################
plt.figure(figsize=(3, 5)) # set figure ratio
plt.ylim(0, 101)
plt.xlim(9, 11)
for j in range(1, 101):
if 9.5 > CIlower[j] and 9.5 < CIupper[j]:
plt.plot([CIlower[j], CIupper[j]], [j, j], linestyle='-', color='grey')
else:
plt.plot([CIlower[j], CIupper[j]], [j, j], linestyle='-', color='black')
plt.axvline(9.5, linestyle='--', color='black', linewidth=0.5)
plt.ylabel('Sample No.')
plt.savefig('PyGraphs/Simulation-Inference-Figure2.pdf')