Skip to content

Instantly share code, notes, and snippets.

@nickelser
Created June 4, 2014 23:46
Show Gist options
  • Save nickelser/6dfa0f2737c502dae267 to your computer and use it in GitHub Desktop.
Save nickelser/6dfa0f2737c502dae267 to your computer and use it in GitHub Desktop.
# http://www.evanmiller.org/bayesian-ab-testing.html implemented in ruby
# requires the distribution gem from https://github.com/clbustos/distribution (gem 'distribution', require: false)
def probability_b_beats_a(completed_a, total_a, completed_b, total_b)
require 'distribution/math_extension'
total = 0.0
alpha_a = completed_a + 1
beta_a = total_a - completed_a + 1
alpha_b = completed_b + 1
beta_b = total_b - completed_b + 1
0.upto(alpha_b - 1) do |i|
total += Math.exp(Math::Beta.log_beta(alpha_a+i, beta_b+beta_a).first - Math.log(beta_b+i) - Math::Beta.log_beta(1+i, beta_b).first - Math::Beta.log_beta(alpha_a, beta_a).first)
end
total
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment