LoginSignup
2
0

More than 5 years have passed since last update.

BigQueryでA/Bテストの信頼区間を計算してみよう

Last updated at Posted at 2017-12-28

以前説明していたのは、ボタンをクリックするかどうかの二択を続けているという意味で二項分布のbinom.testを利用してました
jstatを利用して、信頼区間を計算してみましょう
Wantedlyでは、BigQueryを使って分析するのは基盤もあるため非常に簡単なので、今回簡単な統計計算をBigQuery上でやってみようと思いました
BigQueryではRを実行できないので、基礎的な統計処理の実装が入っている、jstatを利用して、信頼区間の計算をしました

信頼区間の計算

A/Bテストはコインのようにある確率のものを繰り返す時に何枚表かという問題に似ているため、binom.testを利用して評価しています。
Rでは以下のように実行しています。

> x=20
> n=80
> conf=0.95
> binom.test(x,n,conf.level=conf)

    Exact binomial test

data:  x and n
number of successes = 20, number of trials = 80, p-value = 8.581e-06
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.1598796 0.3593635
sample estimates:
probability of success
                  0.25

実際に区間の計算で用いているのは以下の二つのメソッドになります
詳しいRのコードはこちらになります。
https://github.com/wch/r-source/blob/af7f52f70101960861e5d995d3a4bec010bc89e6/src/library/stats/R/binom.test.R

> qbeta((1 - conf)/2, x, n - x + 1)
[1] 0.1598796
> qbeta(1 - (1-conf)/2, x + 1, n - x)
[1] 0.3593635

qbetaは、不完全ベータ関数の逆関数で、
jstatにあるibetainvがこのqbetaに当たるのでこちらを用います
https://github.com/jstat/jstat/blob/bbb79875c0708c1687e9f8082c63350455130e0e/src/special.js#L325
(jStatsはRの実装を参考にして実装しているようでした)

BigQueryで実行するには以下のように書きます。

CREATE TEMPORARY FUNCTION binom_range(conf FLOAT64, x FLOAT64, n FLOAT64)
RETURNS ARRAY<FLOAT64>
LANGUAGE js AS """
  return [jStat.ibetainv((1 - conf)/2, x, n - x + 1), jStat.ibetainv(1 - (1-conf)/2, x + 1, n - x)]
"""
OPTIONS (
  library="gs://#{project_name}/jstat.js"
);

SELECT
  binom_range(0.95, 20, 80)

関連

BigQueryで統計量の計算をしてみよう
https://qiita.com/reikubonaga/items/ed125e6e3b7aae9d1ee8
乱数(モンテカルロ法)を使って確率を計算しよう
https://qiita.com/reikubonaga/items/28ee2c2c8074b98b0b04

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0