loading...
running...
How to use
Pick a preset or a scenario, set the prior with alpha and beta, then draw observations from the hidden urn with the buttons (or switch on auto-draw). The orange curve is the posterior; the dashed grey curve is the prior; the green curve is the likelihood scaled to unit area. The shaded band is the credible interval, the bracket under the axis is the HDI, the white line is the hidden truth.
What to try. (1) Fair coin, flat prior: add 10 flips and then 1000; the interval narrows like 1/sqrt(n). (2) Stubborn wrong prior: the posterior stays near the prior for a long time, then swings to the data. (3) Ten successes in a row: the posterior mean is 11/12, never 1. (4) Raise the credible mass to 0.99 and compare the equal-tailed interval with the HDI for a skewed posterior. (5) Set the proposal step to 0.005 and then 0.6 and watch the chain trace, acceptance rate and ESS. (6) Run the calibration experiment and compare the green points with the diagonal. (7) Move theta0 and watch the Bayes factor and the ROPE probability. Space pauses auto-draw, R resets.
The science
Bayes' theorem for one probability. A success probability theta (a coin's chance of heads, a test's positive rate, a click-through rate) is unknown. The prior p(theta) states what is believed before data; the likelihood is how probable the observed counts are for each theta; the posterior is their normalised product:
p(theta | k of n) = p(k | n, theta) p(theta) / p(k | n), p(k | n, theta) = C(n,k) theta^k (1-theta)^(n-k)
Conjugacy. A Beta(alpha, beta) prior has density proportional to theta^(alpha-1) (1-theta)^(beta-1), so prior times likelihood is again a Beta density and no integral is needed:
posterior = Beta(alpha + k, beta + n - k), mean = (alpha+k)/(alpha+beta+n), mode (MAP) = (alpha+k-1)/(alpha+beta+n-2), var = ab/((a+b)^2 (a+b+1))
The posterior mean is a weighted average of the prior mean and the data frequency k/n with weights (alpha+beta) and n, so alpha+beta acts as the number of pseudo-observations carried by the prior. Updating one observation at a time gives exactly the same posterior as updating with all of them at once.
Credible intervals. The equal-tailed interval cuts (1-mass)/2 of probability off each end, using the inverse of the regularised incomplete beta function. The highest-density interval (HDI) is the shortest interval holding the chosen mass and is found here by golden-section search on the lower-tail probability. Both are statements about the parameter given the data, unlike a frequentist confidence interval. The Wilson score interval and the MLE k/n are shown for comparison.
Prediction. The chance of j successes in m future trials averages the binomial over the posterior, a Beta-Binomial law: P(j) = C(m,j) B(alpha'+j, beta'+m-j) / B(alpha', beta').
Bayes factor and ROPE. For the point value theta0 the Savage-Dickey identity gives BF01 = p(theta0 | data) / p(theta0), the odds in favour of theta = theta0 against the Beta alternative; the page cross-checks it against the ratio of marginal likelihoods. The ROPE reading is the posterior probability inside theta0 +/- the half-width, a practical-equivalence region.
MCMC. The purple chain is a random-walk Metropolis sampler for the same posterior: propose theta' = theta + N(0, s^2), accept with probability min(1, p(theta'|data)/p(theta|data)); it never uses the normalising constant. Compare its histogram, mean and interval with the exact curve. The effective sample size is N / (1 + 2 sum of autocorrelations); a step that is too small or too large lowers the acceptance rate or the ESS.
Calibration. If the true theta is drawn from the prior, a 95% credible interval contains it in exactly 95% of experiments. The calibration experiment simulates this directly.
Numerical checks run in the page. Closed forms of the incomplete beta; Simpson integration of the density; a brute-force grid posterior (prior times likelihood, normalised numerically) against the conjugate mean and 97.5% point; sequential against batch updates; Savage-Dickey against marginal-likelihood ratio; a 160,000-step Metropolis chain against the exact Beta CDF (Kolmogorov distance); and credible-interval coverage by simulation.
Honest simplifications. One parameter, independent identical Bernoulli trials and a Beta prior family only. Real analyses often have several parameters, dependence, hierarchical priors and model misspecification, where no closed form exists. The scenarios change labels and defaults only; the maths is the same. The hidden truth only generates the data and plays no role in the posterior.