置换检验

Python 推断基础

Paul Savala

Assistant Professor of Mathematics

置换检验

两组小人,相互分开,颜色不同。

Python 推断基础

置换检验

  • 打乱样本
  • 观察结果
  • 观察到的差异像随机结果吗?

小人在彼此之间交换位置。

Python 推断基础

在 SciPy 中进行置换检验

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

# 将数据放在一起 data = (new_satisfaction, old_satisfcation)
# 定义检验统计量 def statistic(x, y): return np.mean(x) - np.mean(y)
# 计算均值差的置换检验 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 推断基础

Passons à la pratique !

Python 推断基础

Preparing Video For Download...