統計検定の
思い出した
記事はR
でしたので、
実装した
参考
実装時に、
カイ二乗
二項検定
t検定
使用する データに ついて
A/Bテストに
施策名 / コンバージョンしたか? | YES | NO |
---|---|---|
施策A(改善案サイト) | 454 | 20,933 |
施策B(現状サイト) | 189 | 10,845 |
実装する 検定に ついて
こちらも、
- χ2検定
- 二項検定
- t検定
また、
CVR の 計算
#施策AのCVR yes = 454 no = 20933 result = yes / (yes + no ) print(result)
0.02122784869313134
#施策BのCVR yes = 189 no = 10845 result = yes / (yes + no ) print(result)
0.017128874388254486
A の
χ2検定 (カイ二乗検定)
R だと、chisq.test()
でscipy.stats.chi2_contingency
で
import scipy.stats import numpy as np data = np.matrix([ [ 454, 20933 ], [ 189, 10845 ] ]) chi2, p, ddof, expected = scipy.stats.chi2_contingency( data , correction=False) msg = "Test Statistic: {}\np-value: {}\nDegrees of Freedom: {}\n" print( msg.format( chi2, p, ddof ) ) print( expected )
Test Statistic: 6.291035173658824 p-value: 0.012135015711322547 Degrees of Freedom: 1 [[ 424.16461553 20962.83538447] [ 218.83538447 10815.16461553]]
R と
scipy.stats.chi2_contingency
のcorrection
には、
補正とは、
の
二項検定
scipy.stats.binom_test
を
import scipy.stats import numpy as np data = np.matrix([ [ 454, 20933 ], [ 189, 10845 ] ]) p_a = np.sum(data, axis=1).item((0, 0)) / data.sum() print("p_a=", p_a) conversion_total = np.sum(data, axis=0).item(0, 0) print("conversion_total=", conversion_total) conversion_a = data.item(0,0) print("conversion_a=", conversion_a) # 確率 p_a の基で、n回試行して、x回 (Aは)yesになった p_value = scipy.stats.binom_test(x=conversion_a, n=conversion_total, p=p_a, alternative="two-sided") print("p_value=", p_value)
p_a= 0.659665031924 conversion_total= 643 conversion_a= 454 p_value= 0.0125320280817
t検定
対応しないt検定
のttest_ind
をequal_var = False
に
import scipy.stats import numpy as np data = np.matrix([ [ 454, 20933 ], [ 189, 10845 ] ]) # YES を 1、NO を 0 とする、配列を作成する 要素数合計 は 454 + 20933 a = np.hstack((np.repeat(1, data.item(0,0)), np.repeat(0, data.item(0, 1)))) print("a=", a) # YES を 1、NO を 0 とする、配列を作成する 要素数合計 は 189 + 10845 b = np.hstack((np.repeat(1, data.item(1,0)), np.repeat(0, data.item(1, 1)))) print("b=", b) # t検定実施 対応するt検定の場合は、equal_var = True res = scipy.stats.ttest_ind(a, b, equal_var = False) print("t_value", res[0]) print("p_value", res[1])
a= [1 1 1 ..., 0 0 0] b= [1 1 1 ..., 0 0 0] t_value 2.59373998665 p_value 0.00949948651733
R の
p_value が
実際に
以上です。
コメント