Bootstrap 置信区间

Python 统计思维(第 2 部分)

Justin Bois

Lecturer at the California Institute of Technology

Bootstrap 样本函数

def bootstrap_replicate_1d(data, func):
    """Generate bootstrap replicate of 1D data."""
    bs_sample = np.random.choice(data, len(data))
    return func(bs_sample)

bootstrap_replicate_1d(michelson_speed_of_light, np.mean)
299859.20000000001
bootstrap_replicate_1d(michelson_speed_of_light, np.mean)
299855.70000000001
bootstrap_replicate_1d(michelson_speed_of_light, np.mean)
299850.29999999999
Python 统计思维(第 2 部分)

生成多次 bootstrap 样本

bs_replicates = np.empty(10000)

for i in range(10000): bs_replicates[i] = bootstrap_replicate_1d( michelson_speed_of_light, np.mean)
Python 统计思维(第 2 部分)

绘制 bootstrap 样本直方图

_ = plt.hist(bs_replicates, bins=30, density=True)
_ = plt.xlabel('mean speed of light (km/s)')
_ = plt.ylabel('PDF')
plt.show()
Python 统计思维(第 2 部分)

均值的 bootstrap 估计

第 2 章图 2.011

Python 统计思维(第 2 部分)

统计量的置信区间

  • 如果我们反复进行测量,p% 的观测值将落在 p% 置信区间内。
Python 统计思维(第 2 部分)

Bootstrap 置信区间

conf_int = np.percentile(bs_replicates, [2.5, 97.5])
array([ 299837.,  299868.])

第 2 章图 2.016

Python 统计思维(第 2 部分)

让我们一起练习吧!

Python 统计思维(第 2 部分)

Preparing Video For Download...