置換檢定

Python 推論基礎

Paul Savala

Assistant Professor of Mathematics

置換檢定

兩組顏色不同、彼此分開的小人圖示。

Python 推論基礎

置換檢定

  • 隨機洗牌樣本
  • 觀察結果
  • 觀察到的差異像隨機結果嗎?

小人彼此交換位置。

Python 推論基礎

在 SciPy 進行置換檢定

new_satisfaction = [94, 85, 79, 91, 82]
old_satisfcation = [90, 87, 77, 85, 82]

# Group together our data data = (new_satisfaction, old_satisfcation)
# Define our test statistic def statistic(x, y): return np.mean(x) - np.mean(y)
# Compute a permutation test for the difference in means stats.permutation_test(data, statistic, n_resamples=1000, vectorized=False, alternative='greater')
Python 推論基礎

相關性的置換檢定

紅藍兩條略為相關的折線圖。

stats.pearsonr(red_data, blue_data)[0]
0.08
Python 推論基礎

相關性的置換檢定

data = (red_data, blue_data)

def statistic(x, y): return stats.pearsonr(x, y)[0]
res = stats.permutation_test(data, statistic, n_resamples=1000, vectorized=False, alternative='two-sided')
print(res.pvalue < 0.05)
False
Python 推論基礎

置換檢定

 

  • 透過隨機「洗牌」資料建立虛無分佈
  • 檢定結果的「顯著性」

自助法(Bootstrapping)

 

  • 透過隨機「重抽樣」資料建立機率分佈
  • 建立信賴區間,呈現「最可能」的結果
Python 推論基礎

一起來練習吧!

Python 推論基礎

Preparing Video For Download...