始める無料で始める

ブートストラップを可視化する

このレッスンの前半で行った内容の続きとして、ブートストラップ再標本化で推定した速度の分布を可視化してみましょう。各サンプルごとに傾きに対して最小二乗フィットを行い、傾き推定のばらつきや不確実性を検証しました。

最初の一歩として、速度のサンプル分布を生成する計算を行う関数 compute_resample_speeds(distances, times) をあらかじめ読み込んであります。

この演習はコースの一部です

Pythonで学ぶ線形モデリング入門

コースを見る

演習の手順

  • 事前定義された compute_resample_speeds(distances, times) を使って resample_speeds を計算します。
  • np.mean() を使って、resample_speeds から speed_estimate を計算します。
  • np.percentile()[5, 95] を指定して、resample_speedspercentiles(信頼区間の下限と上限)を計算します。
  • 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()
コードを編集して実行