ブートストラップを可視化する
このレッスンの前半で行った内容の続きとして、ブートストラップ再標本化で推定した速度の分布を可視化してみましょう。各サンプルごとに傾きに対して最小二乗フィットを行い、傾き推定のばらつきや不確実性を検証しました。
最初の一歩として、速度のサンプル分布を生成する計算を行う関数 compute_resample_speeds(distances, times) をあらかじめ読み込んであります。

この演習はコースの一部です
Pythonで学ぶ線形モデリング入門
演習の手順
- 事前定義された
compute_resample_speeds(distances, times)を使ってresample_speedsを計算します。 np.mean()を使って、resample_speedsからspeed_estimateを計算します。np.percentile()に[5, 95]を指定して、resample_speedsのpercentiles(信頼区間の下限と上限)を計算します。axis.hist()を使って、hist_bin_edgesをビンとして指定しながらresample_speedsをプロットします。axis.axvlineを使い、信頼区間の境界を示すためにpercentilesの正しい「2つの」インデックスを指定してチャートに縦線を描きます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create the bootstrap distribution of speeds
resample_speeds = compute_resample_speeds(____, ____)
speed_estimate = np.mean(____)
percentiles = np.percentile(____, [5, 95])
# Plot the histogram with the estimate and confidence interval
fig, axis = plt.subplots()
hist_bin_edges = np.linspace(0.0, 4.0, 21)
axis.hist(____, ____, color='green', alpha=0.35, rwidth=0.8)
axis.axvline(speed_estimate, label='Estimate', color='black')
axis.axvline(percentiles[____], label=' 5th', color='blue')
axis.axvline(percentiles[____], label='95th', color='blue')
axis.legend()
plt.show()