import numpy as npimport scipy.stats as statsimport matplotlib.pyplot as plt# set the random seed:np.random.seed(123456)# set sample size and MC simulations:r = 10000n = 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 = np.random.normal(10, 2, 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.pvaluecv = 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 ratioplt.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 ratioplt.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')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。