Bayesian A/B Testing Framework
Build a set of R functions for analyzing A/B tests the Bayesian way: instead of a p-value, you get answers people actually ask, such as "there's a 94% chance that version B converts better than A" and "if we pick B and we're wrong, we lose on average 0.1 percentage points". Conversions are modeled with Beta-Binomial distributions (where the math works out exactly), posteriors are sampled to estimate probabilities, expected loss and credible intervals, and ggplot2 charts show the two variants' distributions. You'll also check how prior choices and peeking at results early affect decisions. You'll learn Bayesian inference, conjugate priors, Monte Carlo simulation, decision rules based on expected loss, visualizing uncertainty, and writing documented, reusable R functions.
- Estimate
- ~12h
- Steps
- 7
- Completed by
- 0
- Proposed by
- codeseed.app
stats · ggplot2
Project roadmap
- 01
Compute the posteriors
~2hWrite a function posterior_beta(conversions, visitors, prior_alpha = 1, prior_beta = 1) that validates its inputs (non-negative whole numbers, and conversions not more than visitors) and returns the posterior Beta parameters (alpha + conversions and beta + failures), with a short explanation in a comment of why the Beta prior makes this exact. Add a function that returns each variant's posterior mean and 95% credible interval with qbeta. Done when: results for sample data match hand calculations.
- 02
Estimate the probability that B beats A
~1.5hDraw many samples (for example 100,000, with a set seed) from each posterior with rbeta, and estimate the probability that B's rate is higher than A's, the distribution of the difference and the relative lift, with their credible intervals. Check the estimate against the exact numerical answer (by integrating) for one example. Done when: the simulated probability matches the exact value closely.
- 03
Compute expected loss
~1.5hCompute the expected loss of choosing each variant: the average amount by which the chosen variant is worse, over the samples where it is worse. Write a decision function that recommends a variant only when its expected loss is below a threshold you set (such as 0.1 percentage points), and otherwise says to keep collecting data. Done when: the decision function gives sensible answers for clear, close and tiny-sample cases.
- 04
Visualize the results
~1.5hWrite plotting functions with ggplot2: both variants' posterior densities overlapping, with the probability that B beats A in the subtitle, and a histogram of the difference with its credible interval and zero marked. Make them readable with clear labels and colors. Done when: the charts clearly show the results for sample data.
- 05
Study priors and peeking
~2hRun simulations: compare results with a flat prior and an informative prior based on past conversion rates, and simulate many A/A tests (no real difference) checked every day, counting how often the decision rule wrongly picks a winner. Summarize what you learned in README.md. Done when: README.md shows the effect of priors and of checking early, with numbers.
- 06
Package the functions
~2hOrganize the functions as a small R package (with usethis): documented with roxygen2 (parameters, return values and examples), a vignette walking through a complete analysis, and a single summary function that prints a plain-language report. Done when: devtools::check() passes, and the vignette builds.
- 07
Test it
~1.5hWrite testthat tests: input validation, posterior parameters, probability estimates against exact values, expected loss in obvious cases (a clearly better variant has near-zero loss), and reproducibility with a seed. Done when: devtools::test() passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack