https://blog.csdn.net/weixin_39679367/article/details/114631706
https://zhuanlan.zhihu.com/p/35410345
直接意义:
进行100次抽样,选定置信水平95%,每次抽样都会产生一个置信区间,一共100个置信区间。那么包含总体均值的置信区间大概占95%(95个置信区间),另外5个置信区间没有包含总体均值。
import seaborn as sns import matplotlib.pyplot as plt import matplotlib import numpy as np import scipy.stats as st matplotlib.rcParams.update({'font.size': 12}) # generate dataset data_points = 50 sample_points = 10000 Mu = (np.linspace(-5, 5, num=data_points)) ** 2 Sigma = np.ones(data_points) * 5 data = np.random.normal(loc=Mu, scale=Sigma, size=(2, data_points)) print(data) # predicted expect and calculate confidence interval predicted_expect = np.mean(data, 0) low_CI_bound, high_CI_bound = st.t.interval(0.95, data_points - 1, loc=np.mean(data, 0), scale=st.sem(data)) # plot confidence interval x = np.linspace(0, data_points - 1, num=data_points) plt.plot(predicted_expect, linewidth=3., label='estimated value') plt.plot(Mu, color='r', label='grand truth') plt.fill_between(x, low_CI_bound, high_CI_bound, alpha=0.25, label='confidence interval') plt.legend() plt.title('Confidence interval') plt.show()