始める無料で始める

ブートストラップと標準誤差

国立公園では、レンジャーが毎日トレイル整備の一環としてハイキングをします。毎回同じルートではありませんが、最終的な移動距離と所要時間は記録しています。ここでは、ある1人のレンジャーから得られた限られた標本データを使って、日々の移動距離のばらつきを統計モデル化したいと考えます。

あなたの目標は、ブートストラップ再標本化を用いて、各再標本ごとに平均を1つ計算し、その平均の分布を作ることです。次に、その分布から標準誤差を計算し、母集団統計量の推定量としての標本統計量に含まれる「不確実性」を定量化します。

あらかじめ読み込まれている sample_data 配列には、移動距離の独立な測定値が500件含まれています。今回はレッスンを簡潔にするためにシミュレートしたデータを使います。後ほど、より現実的なデータも見ていきます。

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

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

コースを見る

演習の手順

  • sample_data を母集団のモデルとして population_model に代入します。

  • num_resamples 回ループして、次を行います:

    • 毎回 np.random.choice() を使い、population_model から size=resample_sizereplace=True を指定して bootstrap_sample を生成します。
    • 毎回、標本平均を計算して保存します。
  • bootstrap_means に対して np.mean()np.std() を計算して表示します。

  • 事前定義の plot_data_hist() を使って、bootstrap_means の分布を可視化します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Use the sample_data as a model for the population
population_model = ____

# Resample the population_model 100 times, computing the mean each sample
for nr in range(num_resamples):
    bootstrap_sample = np.random.____(population_model, size=____, replace=____)
    bootstrap_means[nr] = np.____(bootstrap_sample)

# Compute and print the mean, stdev of the resample distribution of means
distribution_mean = np.mean(____)
standard_error = np.std(____)
print('Bootstrap Distribution: center={:0.1f}, spread={:0.1f}'.format(____, ____))

# Plot the bootstrap resample distribution of means
fig = plot_data_hist(____)
コードを編集して実行