Bayesian A/B Test (Beta-Binomial)
Background
Bayesian A/B testing replaces "is the p-value below 0.05?" with the directly interpretable "what's the probability that B is better than A?" The Beta-Binomial model is the canonical choice for conversion-rate metrics: with conjugate Beta priors and Binomial likelihoods, the posterior for each arm's true rate is Beta-shaped, and the probability one arm exceeds the other is estimated by sampling from the two posteriors and counting.
Problem statement
Implement bayesian_ab_prob(a_succ, a_n, b_succ, b_n, prior_alpha=1.0, prior_beta=1.0, n_samples=20000, seed=0) returning .
Posteriors (conjugate Beta-Binomial):
Estimate the desired probability by Monte Carlo: draw n_samples from each posterior independently and report the fraction of draws where the B-sample exceeds the A-sample.
Input
a_succ-int >= 0, successes in arm A.a_n-int >= a_succ, total trials in arm A.b_succ,b_n- same for arm B.prior_alpha,prior_beta-float > 0, hyperparameters of the Beta prior (default , the uniform).n_samples-int, Monte Carlo samples per arm (default ).seed-int, RNG seed (default ). Required for reproducibility in tests.
Output
floatin - the estimated .
Examples
Example 1 - B clearly better
Input: a_succ=10, a_n=100, b_succ=20, b_n=100
Output: >0.99
Example 2 - Equal evidence, same priors
Input: a_succ=50, a_n=100, b_succ=50, b_n=100
Output: ~0.5
Constraints
- Use a single RNG seeded by
seedso the test is reproducible. - Raise
ValueErrorifa_succ > a_norb_succ > b_n(impossible counts). - Return a Python
float.
Notes
- Why Bayesian over frequentist. is the question stakeholders ask; the -value is a question about the data under a null. Bayesian framing also lets you state "we have 92% probability that B is at least 1% better" with no - trade-off.
- Prior choice. is uniform (no opinion). Stronger priors ( with larger ) shrink the posterior toward - useful for high-variance / low-traffic settings.
- Closed form. There is a closed-form expression for as a sum of hypergeometric-flavoured terms, but Monte Carlo is the production default because it generalises to non-conjugate priors with no re-derivation.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example: B clearly better
- •Worked sample: equal evidence, same priors
- •Sample: A clearly better