平均值與 SEM 的 Bootstrap 複本
在這個練習中,你會用 bootstrap 來估計雪菲爾氣象站年度平均降雨量的機率密度函式。請記住,我們要估計的是:如果雪菲爾氣象站能把 1883 到 2015 年的所有量測無限次重複,所得到的年度平均降雨量會是什麼樣子。這是對「平均值」的機率式估計。你會把 PDF 畫成長條圖,並看到它呈現常態分佈。
事實上,在相當寬鬆的條件下,可以從理論上證明「平均值」會呈現常態分佈。(這不是對所有統計量都成立,只對平均值與少數統計量成立。)這個分佈的標準差稱為「平均數的標準誤」(standard error of the mean,SEM),其公式為資料的標準差除以資料點數的平方根。也就是對資料集而言,sem = np.std(data) / np.sqrt(len(data))。用駭客統計(hacker statistics)的方法,你可以不必推導就得到相同結果,而你也會透過自己的 bootstrap 複本來驗證這個結果。
資料集已預先載入為名為 rainfall 的陣列。
本練習屬於課程
Statistical Thinking in Python(第 2 部分)
練習說明
- 使用你的
draw_bs_reps()函式與rainfall陣列,抽取10000個年度降雨量「平均值」的 bootstrap 複本。提示:將np.mean傳入func以計算平均值。- 提醒:
draw_bs_reps()接受 3 個引數:data、func、size。
- 提醒:
- 計算並印出
rainfall的平均數標準誤(SEM)。- 計算公式為
np.std(data) / np.sqrt(len(data))。
- 計算公式為
- 計算並印出你的 bootstrap 複本
bs_replicates的標準差。 - 使用
density=True與50個 bins 繪製複本的長條圖。 - 按下 Submit 查看圖表!
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Take 10,000 bootstrap replicates of the mean: bs_replicates
bs_replicates = ____
# Compute and print SEM
sem = ____ / np.sqrt(____)
print(sem)
# Compute and print standard deviation of bootstrap replicates
bs_std = ____
print(bs_std)
# Make a histogram of the results
_ = plt.hist(____, ____=50, ____=True)
_ = plt.xlabel('mean annual rainfall (mm)')
_ = plt.ylabel('PDF')
# Show the plot
plt.show()