1 Star 0 Fork 0

yanbridge/Python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Simulation-Inference-Figure.py 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
yanbridge 提交于 2020-10-07 19:42 +08:00 . Add files via upload
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')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yanbridge/Python.git
git@gitee.com:yanbridge/Python.git
yanbridge
Python
Python
main

搜索帮助