始める無料で始める

標本統計量のばらつき

母集団から size=1000 の点を抽出して 1 つのサンプルを作り、その標本平均のような標本統計量(サンプルを要約する単一の値)を計算します。

このサンプリングを num_samples=100 回繰り返すと、100 個のサンプルが得られます。各サンプルについて標本平均のような統計量を計算すると、平均値の分布が得られます。ここでの目標は、その「平均の平均」と「平均の標準偏差」を計算することです。

ここでは、あらかじめ読み込まれている populationnum_samplesnum_pts を使います。meansdeviations 配列は 0 で初期化済みで、for ループ内で使用するための入れ物になっています。

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

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

コースを見る

演習の手順

  • num_samples=100 の各回について、サンプルを生成し、標本統計量を計算して保存します。
  • 各イテレーションで、np.random.choice() を使って母集団からランダムに 1000 点を抽出し、sample を作成します。
  • 各イテレーションで、sample.mean()sample.std() を計算して保存し、サンプルの平均と標準偏差を求めます。
  • means 配列と deviations 配列について、それぞれの平均と標準偏差を計算し、結果を出力します。

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

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

# Initialize two arrays of zeros to be used as containers
means = np.zeros(num_samples)
stdevs = np.zeros(num_samples)

# For each iteration, compute and store the sample mean and sample stdev
for ns in range(num_samples):
    sample = np.____.choice(population, num_pts)
    means[ns] = sample.____()
    stdevs[ns] = sample.____()

# Compute and print the mean() and std() for the sample statistic distributions
print("Means:  center={:>6.2f}, spread={:>6.2f}".format(means.mean(), means.std()))
print("Stdevs: center={:>6.2f}, spread={:>6.2f}".format(stdevs.____(), stdevs.____()))
コードを編集して実行