比較抽樣與自助法分配

Python 中的抽樣

James Chapman

Curriculum Manager, DataCamp

聚焦咖啡的子集合

coffee_sample = coffee_ratings[["variety", "country_of_origin", "flavor"]]\
    .reset_index().sample(n=500)
     index         variety       country_of_origin  flavor
132    132           Other              Costa Rica    7.58
51      51            None  United States (Hawaii)    8.17
42      42  Yellow Bourbon                  Brazil    7.92
569    569         Bourbon               Guatemala    7.67
..     ...             ...                     ...     ...
643    643          Catuai              Costa Rica    7.42
356    356         Caturra                Colombia    7.58
494    494            None               Indonesia    7.58
169    169            None                  Brazil    7.81

[500 rows x 4 columns]
Python 中的抽樣

咖啡風味平均值的自助法

import numpy as np
mean_flavors_5000 = []
for i in range(5000):
    mean_flavors_5000.append(
        np.mean(coffee_sample.sample(frac=1, replace=True)['flavor'])
    )
bootstrap_distn = mean_flavors_5000
Python 中的抽樣

風味平均值的自助法分配

import matplotlib.pyplot as plt
plt.hist(bootstrap_distn, bins=15)
plt.show()

自助法分配的長條圖。

Python 中的抽樣

樣本、自助法分配、母體平均

樣本平均:

coffee_sample['flavor'].mean()
7.5132200000000005

母體平均的估計:

np.mean(bootstrap_distn)
7.513357731999999

母體真實平均:

coffee_ratings['flavor'].mean()
7.526046337817639
Python 中的抽樣

如何解讀平均值

自助法分配的平均:

  • 通常接近樣本平均
  • 可能不是母體平均的良好估計

  自助法無法修正抽樣帶來的偏誤

Python 中的抽樣

樣本 sd 與自助法分配 sd 比較

樣本標準差:

coffee_sample['flavor'].std()
0.3540883911928703

母體標準差的估計?

np.std(bootstrap_distn, ddof=1)
0.015768474367958217
Python 中的抽樣

樣本、自助法分配、母體標準差

樣本標準差:

coffee_sample['flavor'].std()
0.3540883911928703

母體標準差的估計:

standard_error = np.std(bootstrap_distn, ddof=1)

標準誤 是所關心統計量的標準差

真實標準差:

coffee_ratings['flavor'].std(ddof=0)
0.34125481224622645
standard_error * np.sqrt(500)
0.3525938058821761

標準誤乘上樣本大小的平方根可估計母體標準差

Python 中的抽樣

如何解讀標準誤

  • 標準誤的估計 → 樣本統計量之自助法分配的標準差
  • $\text{Population std. dev} \approx \text{Std. Error} \times \sqrt{\text{Sample size}}$
Python 中的抽樣

一起來練習吧!

Python 中的抽樣

Preparing Video For Download...