Skip to content

Instantly share code, notes, and snippets.

@arnov
Last active May 13, 2019 14:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arnov/60de0b1ad62d329bc222 to your computer and use it in GitHub Desktop.
Save arnov/60de0b1ad62d329bc222 to your computer and use it in GitHub Desktop.
Bayesian AB Test
#!/usr/bin/python
# -*- coding: utf-8 -*-
import math
def calc_ab(alpha_a, beta_a, alpha_b, beta_b):
'''
See http://www.evanmiller.org/bayesian-ab-testing.html
αA is one plus the number of successes for A
βA is one plus the number of failures for A
αB is one plus the number of successes for B
βB is one plus the number of failures for B
'''
total = 0.0
for i in range(alpha_b):
num = math.lgamma(alpha_a+i) + math.lgamma(beta_a+beta_b) + math.lgamma(1+i+beta_b) + math.lgamma(alpha_a+beta_a)
den = math.log(beta_b+i) + math.lgamma(alpha_a+i+beta_a+beta_b) + math.lgamma(1+i) + math.lgamma(beta_b) + math.lgamma(alpha_a) + math.lgamma(beta_a)
total += math.exp(num - den)
return total
print(calc_ab(1600+1,1500+1,3200+1,3300+1))
@ThierryGrb
Copy link

Thanks for sharing! I think it should be for i in range(alpha_b) to include alpha_b-1 in the counting.

@arnov
Copy link
Author

arnov commented Jun 15, 2017

Ah I think you're right, the for loop in Evan Millar's example is in Julia where it is inclusive! I've updated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment