比率指标与德尔塔法

Python 中的 A/B 测试

Moe Lotfy, PhD

Principal Data Science Manager

比率指标的 A/B 测试

  • 均值型指标

均值型指标示例:平均订单金额与平均页面停留时间

  • 分析单位:

    • A/B 测试中被分析的实体
    • 比率指标的分母
  • 随机化单位:

    • 随机分配到各变体的对象
Python 中的 A/B 测试

比率指标的 A/B 测试

  • 用户级比率指标

比率指标示例:点击率与每会话收入

Python 中的 A/B 测试

德尔塔法动机

print(checkout.groupby('checkout_page')[['order_value','purchased']].agg({'sum','count','mean'}))
              order_value                     purchased              
                     mean           sum count      mean     sum count
checkout_page                                                        
A               24.956437  61417.791564  2461  0.820333  2461.0  3000
B               29.876202  75915.430125  2541  0.847000  2541.0  3000
C               34.917589  90890.484142  2603  0.867667  2603.0  3000
checkout.groupby('checkout_page')['order_value'].sum()/
checkout.groupby('checkout_page')['purchased'].count()
checkout_page
A    20.472597
B    25.305143
C    30.296828
dtype: float64
Python 中的 A/B 测试

德尔塔法方差

  • 德尔塔法:比率指标的方差估计:{

德尔塔方法差方差公式

# Delta method variance of ratio metric
def var_delta(x,y): 
    x_bar = np.mean(x)
    y_bar = np.mean(y)
    x_var = np.var(x,ddof=1)
    y_var = np.var(y,ddof=1)
    cov_xy = np.cov(x,y,ddof=1)[0][1]
    # Note that we divide by len(x) here because the denominator of the test statistic is standard error (=sqrt(var/n))
    var_ratio = (x_var/y_bar**2 + y_var*(x_bar**2/y_bar**4) - 2*cov_xy*(x_bar/y_bar**3))/len(x)
    return var_ratio
1 Budylin, Roman & Drutsa, Alexey & Katsev, Ilya & Tsoy, Valeriya. (2018). Consistent Transformation of Ratio Metrics for Efficient Online Controlled Experiments. 55-63. 10.1145/3159652.3159699.
Python 中的 A/B 测试

德尔塔法 z 检验

# Delta method ztest calculation 
ztest_delta(x_control,y_control,x_treatment,y_treatment, alpha = 0.05)

输入参数:

  • x_control:对照组用户级比率的分子列
  • y_control:对照组用户级比率的分母列
  • x_treatment:实验组用户级比率的分子列
  • y_treatment:实验组用户级比率的分母列
  • alpha:显著性水平。

输出:

  • mean_control:对照组比率指标均值
  • mean_treatment:实验组比率指标均值
  • difference:实验组与对照组均值之差
  • diff_CI:均值差的置信区间
  • p-value:双侧 z 检验的 p 值
1 https://medium.com/@ahmadnuraziz3/applying-delta-method-for-a-b-tests-analysis-8b1d13411c22
Python 中的 A/B 测试

Python 示例

# 为变体 A 与 B 构建用户级指标的 DataFrame
A_per_user = pd.DataFrame({'order_value':checkout[checkout['checkout_page']=='A'].groupby('user_id')['order_value'].sum()
                            ,'page_view':checkout[checkout['checkout_page']=='A'].groupby('user_id')['user_id'].count()})
B_per_user = pd.DataFrame({'order_value':checkout[checkout['checkout_page']=='B'].groupby('user_id')['order_value'].sum()
                           ,'page_view':checkout[checkout['checkout_page']=='B'].groupby('user_id')['user_id'].count()})

# 指定对照与实验的比率列
x_control = A_per_user['order_value']
y_control = A_per_user['page_view']
x_treatment = B_per_user['order_value']
y_treatment = B_per_user['page_view']

# 对比率指标运行 z 检验
ztest_delta(x_control,y_control,x_treatment,y_treatment)
{'mean_control': 20.472597188012,
 'mean_treatment': 25.30514337484097,
 'difference': 4.833,
 'diff_CI': '[4.257, 5.408]',
 'p-value': 5.954978880467735e-61}
Python 中的 A/B 测试

Passons à la pratique !

Python 中的 A/B 测试

Preparing Video For Download...