Skip to content

Instantly share code, notes, and snippets.

@salilpa
Last active May 18, 2017 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salilpa/ef0f82e366b49ddf56e222ec31dc1be8 to your computer and use it in GitHub Desktop.
Save salilpa/ef0f82e366b49ddf56e222ec31dc1be8 to your computer and use it in GitHub Desktop.
Simple formula to find Bayesian A/B test confidence
import math
def calc_ab(conversion_a, interaction_a, conversion_b, interaction_b):
# more generic function which takes conversions and interactions. Calculates failures internally
# total is the confidence that b is doing better than a
alpha_a = conversion_a + 1
alpha_b = conversion_b + 1
beta_a = interaction_a - conversion_a + 1
beta_b = interaction_b - conversion_b + 1
total = 0.0
for i in range(alpha_b): # loop till alphab - 1
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment