健全性チェック:内的妥当性

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 テスト
    • 2 群に同一体験を提示
    • 実験設定の不具合を発見
    • 指標間に統計的有意差はないはず
    • 指定した $\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テスト

Passons à la pratique !

Pythonで学ぶA/Bテスト

Preparing Video For Download...