非参数统计检验

Python 中的 A/B 测试

Moe Lotfy, PhD

Principal Data Science Manager

参数检验的假设

  1. 随机抽样
    • 数据从总体中随机抽取。
    • 检查数据收集/抽样流程。
  2. 独立性
    • 各观测/数据点相互独立。
    • 未考虑依赖会抬高错误率。
  3. 正态性
    • 数据近似正态分布。
    • 样本量足够大。
      • 两独立样本 t 检验:各组 n ≥ 30。
      • 两比例检验:各组≥10次成功且≥10次失败。
Python 中的 A/B 测试

Mann-Whitney U 检验

  • 用于统计显著性的非参数检验
  • 判断两独立样本是否来自同一分布
  • 秩和检验
  • 非配对数据
Python 中的 A/B 测试

Python 中的 Mann-Whitney U 检验

# Calculate the mean and count of time on page by variant
print(checkout.groupby('checkout_page')['time_on_page'].agg({'mean', 'count'}))
                    mean  count
checkout_page                  
A              44.668527   3000
B              42.723772   3000
C              42.223772   3000
# Set random seed for repeatability 
np.random.seed(40)
# Take a random sample of size 25 from each variant
ToP_samp_A = checkout[checkout['checkout_page'] == 'A'].sample(25)['time_on_page']
ToP_samp_B = checkout[checkout['checkout_page'] == 'B'].sample(25)['time_on_page']
Python 中的 A/B 测试

Python 中的 Mann-Whitney U 检验

# Run a Mann-Whitney U test
mwu_test = pingouin.mwu(x=ToP_samp_A,
                        y=ToP_samp_B,
                        alternative='two-sided')
# Print the test results
print(mwu_test)
     U-val alternative     p-val     RBC    CLES
MWU  441.0   two-sided  0.013007 -0.4112  0.7056
Python 中的 A/B 测试

独立性的卡方检验

  • 不受参数检验假设限制
  • 检验两个或多个分类变量是否独立
    • 原假设: 变量彼此独立。
    • 备择假设: 变量不独立。
Python 中的 A/B 测试

Python 中的卡方检验

首页注册率 A/B 测试

原假设: 设计 C 与 D 的注册率无显著差异

备择假设: 它们的注册率存在显著差异

# Calculate the number of users in groups C and D
n_C = homepage[homepage['landing_page'] == 'C']['user_id'].nunique()
n_D = homepage[homepage['landing_page'] == 'D']['user_id'].nunique()
# Compute unique signups in each group
signup_C = homepage[homepage['landing_page'] == 'C'].groupby('user_id')['signup'].max().sum()
no_signup_C = n_C - signup_C
signup_D = homepage[homepage['landing_page'] == 'D'].groupby('user_id')['signup'].max().sum()
no_signup_D = n_D - signup_D
Python 中的 A/B 测试

Python 中的卡方检验

# Create the signups table
table = [[signup_C, no_signup_C], [signup_D, no_signup_D]]
print('Group C signup rate:',round(signup_C/n_C,3))
print('Group D signup rate:',round(signup_D/n_D,3))

# Calculate p-value
print('p-value=',stats.chi2_contingency(table,correction=False)[1])
Group C signup rate: 0.064
Group D signup rate: 0.048
p-value= 0.009165
Python 中的 A/B 测试

让我们来练习!

Python 中的 A/B 测试

Preparing Video For Download...