健全性檢查:內部效度

Python 的 A/B 測試

Moe Lotfy, PhD

Principal Data Science Manager

樣本比例不符(SRM)

  • 樣本比例不符(SRM)
    • 各版本分配偏離實驗設計
  • 卡方適合度檢定

卡方公式

樣本比例不符的分配範例

Python 的 A/B 測試

SRM 的 Python 範例

# Calculate the unique IDs per variant
AdSmart.groupby('experiment')['auction_id'].nunique()
experiment
control    4071
exposed    4006
# Assign the unqiue counts to each variant
control_users=AdSmart[AdSmart['experiment']=='control']['auction_id'].nunique()
exposed_users=AdSmart[AdSmart['experiment']=='exposed']['auction_id'].nunique()
total_users=control_users+exposed_users
# Calculate allocation ratios per variant
control_perc = control_users / total_users
exposed_perc = exposed_users / total_users
print("Percentage of users in the Control group:",100*round(control_perc,5),"%")
print("Percentage of users in the Exposed group:",100*round(exposed_perc,5),"%")
Percentage of users in the Control group: 50.402 %
Percentage of users in the Exposed group: 49.598 %
1 Adsmart Kaggle dataset: https://www.kaggle.com/datasets/osuolaleemmanuel/ad-ab-testing
Python 的 A/B 測試

SRM 的 Python 範例

# Creat lists of observed and expected counts per variant
observed = [ control_users, exposed_users ]
expected = [ total_users/2, total_users/2 ]
# Import chisquare from scipy library
from scipy.stats import chisquare
# Run chisquare test on observed and expected lists
chi = chisquare(observed, f_exp=expected)
# Print test results and interpretation
print(chi)
if chi[1] < 0.01:
    print("SRM may be present")
else:
    print("SRM likely not present")
Power_divergenceResult(statistic=0.5230902562832735, pvalue=0.4695264353014863)
SRM likely not present
1 Adsmart Kaggle dataset: https://www.kaggle.com/datasets/osuolaleemmanuel/ad-ab-testing
Python 的 A/B 測試

追查 SRM 根因

SRM 常見成因:$^1$

  • 指派:分桶錯誤或隨機化函式有誤
  • 執行:各版本啟動時間或漸進流量不同
  • 紀錄:資料記錄延遲或機器人過濾
  • 介入:實驗者暫停某版本
1 Diagnosing Sample Ratio Mismatch in Online Controlled Experiments: A Taxonomy and Rules of Thumb for Practitioners
Python 的 A/B 測試

A/A 測試

  • A/A 測試
    • 兩組使用者體驗完全相同
    • 揭露實驗設定中的錯誤
    • 各指標之間不應有統計上顯著差異
    • 在指定的 $\alpha$ 下仍可能出現偽陽性(約 5% 機率)
    • 可發現各組在分布上的不均(如瀏覽器、裝置等)
Python 的 A/B 測試

分布平衡的 Python 範例

  • 瀏覽器分布平衡
  • 測試有效
checkout.groupby('checkout_page')['browser'].value_counts(normalize=True)
checkout_page  browser
A              chrome     0.341333
               safari     0.332000
               firefox    0.326667
B              safari     0.352000
               firefox    0.325000
               chrome     0.323000
C              safari     0.346000
               chrome     0.330000
               firefox    0.324000
  • 瀏覽器分布不平衡
  • 測試無效
 AdSmart.groupby('experiment')['browser'].value_counts(normalize=True)
experiment  browser                   
control     Chrome Mobile                 0.591992
            Facebook                      0.137804
            Samsung Internet              0.120855
            Chrome Mobile WebView         0.071727
            Mobile Safari                 0.060427
            Chrome Mobile iOS             0.008352
            Mobile Safari UI/WKWebView    0.007369
exposed     Chrome Mobile                 0.535197
            Chrome Mobile WebView         0.298802
            Samsung Internet              0.082876
            Facebook                      0.050674
            Mobile Safari                 0.022716
            Chrome Mobile iOS             0.004244
1 Adsmart Kaggle dataset: https://www.kaggle.com/datasets/osuolaleemmanuel/ad-ab-testing
Python 的 A/B 測試

一起來練習吧!

Python 的 A/B 測試

Preparing Video For Download...