Pythonで学ぶA/Bテスト
Moe Lotfy, PhD
Principal Data Science Manager

分析単位:
ランダム化単位:

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

# 比率指標のデルタ法分散
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
# 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値# 変数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テスト