Interactive Explainer
The Posterior, Built Up
Drag a prior, click data points onto an axis, and watch the posterior crystallize. Then go conjugate, separate posterior predictive from a MAP plug-in, race grid evaluation against Metropolis MCMC against variational inference on a non-conjugate posterior, and finish with Bayesian linear regression whose uncertainty band bows out exactly where data is missing — every number computed live in pure JS.
From "the right answer" to a distribution over answers
Maximum likelihood gives you one number for an unknown parameter $\theta$ — the value most consistent with the data. The Bayesian alternative says: the data don't pick a single $\theta$, they update your belief over all possible values. That belief is a probability distribution; the recipe for updating it is one line of math:
Three ingredients: the prior $p(\theta)$ (what you believed before), the likelihood $p(\mathcal{D}\mid\theta)$ (how the data depend on $\theta$), and the posterior $p(\theta \mid \mathcal{D})$ (your updated belief). The denominator is just a normalising constant that makes the posterior integrate to 1; we'll mostly ignore it.
The whole rest of this article is one slogan rendered in nine increasingly hard ways:
Prior × likelihood = posterior, by hand
Easiest setting: a single unknown mean $\theta$ of a Gaussian with known variance $\sigma^2$. Conjugate prior is also Gaussian. The posterior is closed-form:
Drag the prior with the slider below; click on the data axis to add observations (or use the buttons). The three curves recompute live: the prior in cool blue, the likelihood as a function of $\theta$ in warm orange, and the posterior in deep teal. The posterior is the prior squeezed by the likelihood — literally the pointwise product, normalised.
How fast does data sharpen the posterior?
The posterior precision (inverse variance) is the sum of the prior precision and the data precision:
Two clean consequences. First, posterior variance falls like $1/n$ — classical "data sharpens beliefs" but with a principled rate. Second, the prior contributes a fixed amount of pseudo-data: a tight prior with $\sigma_0 = 0.1$ is "worth" $\sigma^2 / \sigma_0^2 = 100$ data points for $\sigma=1$.
The horizontal asymptote at $1/\sigma_0$ goes away as $n$ grows; eventually the data precision dominates whatever prior you brought.
The conjugate coin (Beta-Binomial)
Bernoulli observations, parameter $\theta = $ probability of heads, prior $\theta \sim \mathrm{Beta}(\alpha, \beta)$. After $k$ heads in $n$ flips:
Conjugacy means "the posterior stays in the same family" — here Beta. Update is just adding counts to the parameters. Click the buttons below to flip a real coin and watch the Beta posterior reshape live.
Posterior predictive ≠ plug-in MAP
The posterior is the right object for the parameter. But downstream we usually care about predicting new data. There are two ways to do that:
- Plug-in MAP / MLE. Pick one $\theta$ (the maximum) and predict $p(x_{\text{new}}\mid\hat\theta)$.
- Posterior predictive. Average over every $\theta$ weighted by the posterior: $\;p(x_{\text{new}}\mid\mathcal{D}) = \int p(x_{\text{new}}\mid\theta)\,p(\theta\mid\mathcal{D})\,d\theta$.
Plug-in is faster but lies about uncertainty. The posterior predictive is wider whenever you don't yet know $\theta$ precisely — precisely the regime where calibration matters most. Watch the gap shrink as $n$ grows.
What if the posterior isn't a nice family?
Conjugate priors give us closed-form posteriors. Real posteriors — from a neural net, a hierarchical model, a non-Gaussian likelihood — usually aren't conjugate. Three families of algorithms cover all of practice:
- Grid evaluation. Compute the unnormalised posterior on a grid; normalise. Exact, dies above ~3 dimensions.
- MCMC (we use Metropolis-Hastings). Random walk on $\theta$ that converges in distribution to the posterior. Exact in the limit, finite-sample-noisy.
- Variational inference (VI). Pick a tractable family (we use a Gaussian) and fit it by minimising the KL divergence to the true posterior. Fast but biased — especially for multimodal targets.
Below: a deliberately cooked, bimodal, non-conjugate posterior. Grid runs once. The MCMC chain walks live; you can watch samples slowly fill out both modes (or get stuck in one). The VI fit is a single Gaussian dropped on top — almost always centered on one mode and miscalling the other.
Bayesian linear regression — uncertainty that bows out
One last conjugate model with a famous picture. Linear regression with Gaussian prior on the weights and Gaussian observation noise has a Gaussian posterior over weights, and a Gaussian posterior predictive at every input $x$:
Click anywhere to add a data point. The fit line and its uncertainty band recompute live. Notice the band: thin where you have data, fat where you don't. That is what honest uncertainty looks like, and it is exactly what Gaussian processes generalise.
Hierarchical models — partial pooling, the Bayesian superpower
Suppose you have several related groups: many sites, sensors, schools, patients, batches. Each has a small pile of observations. You can fit one model per group ("no pooling") or one model for everything ("complete pooling"). Both are wrong in known ways. The Bayesian fix is partial pooling: a hierarchical prior that lets each group have its own mean while pulling outliers toward the global mean.
$\mu$ is the global mean; $\tau$ is how strongly groups are pulled toward it; $\theta_g$ is the group-specific mean. The posterior of $\theta_g$ is shrunk toward $\mu$ by an amount that depends on $\tau$ and the group's data count $n_g$.
Why this matters: data-poor groups borrow strength from data-rich ones without being forced to agree with them. James & Stein, 1961, proved that this estimator dominates the per-group MLE in mean squared error — in three or more dimensions, no-pool is provably suboptimal.
Five things people get wrong
"Bayesian = subjective, frequentist = objective."
Both pick assumptions: frequentists pick a sampling
model and a procedure (significance level, estimator);
Bayesians pick a sampling model and a prior. Bayes makes
the assumption explicit, which is the actual difference.
"With enough data the prior doesn't matter."
True for low-dimensional, well-identified parameters
(Step 2 made this concrete). False in high-dimensional
models, hierarchical models, or any setting where the
likelihood is flat in some directions — which is
most of modern deep learning.
"MCMC always converges to the truth."
In the infinite-sample limit, yes. With finite chains and
poor proposals (Step 5), MCMC spends a long time in a
single mode, returns a confidently wrong histogram, and
tells you it converged. Always run multiple chains.
"VI is just a faster MCMC."
VI is fundamentally biased: it returns the closest member
of your variational family, even if no member of the
family looks like the truth. On multimodal posteriors a
Gaussian VI fit can put zero mass on the second mode
while still claiming to have "converged".
"A Bayesian neural network is just one trained with a prior."
The prior is the easy part. The hard part is getting a
calibrated posterior over a 100M-dimensional
parameter space, which neither MCMC nor VI does honestly
today. SGLD, Laplace, deep ensembles, and SWAG are
practical compromises — none of them is "the
Bayesian answer".
"Bayesian inference is overkill if you only want a point estimate."
The posterior mean minimises mean-squared error
for any loss; the MAP minimises 0–1 loss. They're often
different, and pretending the MAP is the right summary
has cost more papers than overfitting has.
Where this lives in your research
Three places the Bayesian frame buys you something concrete rather than aesthetic:
- Calibrated uncertainty in deployed models. A sensor model that says "I don't know" off-distribution is safer than one that says "0.97" everywhere. Reliability diagrams + temperature scaling + deep ensembles get you most of the way; full Bayesian deep learning is the principled limit.
- Hierarchical models for sparse, structured data. Patients, sites, sensors, languages, individuals — anywhere you have many groups with few observations each, partial pooling beats both "one model per group" and "one model for all" by a lot. This is the Bayesian superpower for data with regional structure.
- Inverse problems and physics-informed ML. Energy disaggregation, source apportionment for air quality, satellite pixel unmixing, sleep-stage estimation — the unknown is a high-dimensional latent and the forward model is partially known. Bayes is the language of inverse problems; everything else is an approximation to it.