比率指標與 Delta 方法

Python 的 A/B 測試

Moe Lotfy, PhD

Principal Data Science Manager

比率指標的 A/B 測試

  • 平均型指標

平均型指標範例:平均訂單金額與頁面平均停留時間

  • 分析單位

    • A/B 測試中被分析的實體
    • 比率指標的分母
  • 隨機化單位

    • 隨機分配到各變體的對象
Python 的 A/B 測試

比率指標的 A/B 測試

  • 每使用者比率指標

比率指標範例:點擊率與每次工作階段營收

Python 的 A/B 測試

Delta 方法動機

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 方法變異數

  • Delta 方法:比率指標的變異數估計:{

Delta 方法變異數公式

# 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 測試

Delta 方法 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 範例

# Create DataFrames for per user metrics for variants A and B
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()})

# Assign the control and treatment ratio columns
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']

# Run a z-test for ratio metrics
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 測試

一起來練習吧!

Python 的 A/B 測試

Preparing Video For Download...