ML Systems Lab Open interactive version →
Foundational 50 min read bayesposteriorpriorlikelihoodconjugate priors

Bayesian Inference

Likelihood, prior, posterior, conjugate priors, predictive distribution, sequential updating

You flip a coin 3 times and get H, H, H. The frequentist MLE gives P(H) = 3/3 = 1.0 — the model is certain the coin always lands heads. But you only have 3 flips. The model has fit the data perfectly and is telling you something obviously wrong. This is what happens when you collapse inference to a single point estimate without tracking uncertainty.

Bayesian inference solves this by maintaining a full distribution over the unknown parameter rather than collapsing to one value. The update rule is: posterior ∝ likelihood × prior. With a uniform prior Beta(1, 1) — encoding no prior knowledge about the coin — and likelihood P(data | θ) = θ³, the posterior is Beta(4, 1).

The posterior mean is 4/5 = 0.8, not 1.0. The prior has pulled the estimate away from the degenerate MLE, encoding the reasonable belief that most coins are somewhere near fair.

The key mechanism: the prior's influence is inversely proportional to the amount of data. With 3 flips, the posterior is 0.8 — meaningfully different from MLE. With 300 flips and 300 heads, the posterior mean is 301/302 ≈ 0.99 — nearly identical to MLE. The likelihood dominates and the prior washes out. This is the correct behavior: priors matter when data is scarce and become irrelevant when data is abundant.

The practical cost of this framework is the denominator in Bayes' theorem: p(θ | X) = p(X | θ) p(θ) / p(X), where p(X) = ∫ p(X | θ) p(θ) dθ requires integrating the likelihood over the entire parameter space. For conjugate models, this integral is analytic. For everything else — which is most real models — it has no closed form, and the entire ecosystem of approximate inference methods (MCMC, variational inference, Laplace approximation) exists to avoid or approximate this integral.

NOT this. "Bayesian methods are computationally intractable." For conjugate prior-likelihood pairs, the posterior is analytic — no integration required. Beta + Binomial, Gaussian + Gaussian, Dirichlet + Categorical all yield closed-form posteriors. For non-conjugate problems, variational inference reframes the posterior computation as an optimization problem, and MCMC constructs a Markov chain whose stationary distribution is the posterior. Bayesian deep learning with MC Dropout adds a single forward pass with dropout active at test time. The intractability is real but narrower than it appears: it applies only to the exact normalizing constant, and every major approximate inference method has a principled strategy for working around it.

Key points

Takeaway

The practical cost of collapsing to a point estimate (MAP) shows up in the predictive distribution: MAP plugged into p(x*|θ̂) underestimates uncertainty because it pretends the parameter is known exactly. The correct predictive distribution integrates over the posterior and is wider, especially in low-data regimes. The credible interval vs confidence interval distinction is the sharpest interview signal: credible intervals are direct probability statements about where the parameter is; confidence intervals are long-run coverage guarantees that say nothing about any single computed interval.

Recap

Check your understanding

Q1. You run a Bayesian A/B test. After 500 conversions each, the posterior P(p_A > p_B | data) = 0.94. Your decision threshold is 0.95. Your boss says "just call it — it's clearly A". Select the two correct responses.

Q2. What is the marginal likelihood p(X) in Bayes' theorem and why is it hard to compute?

Q3. You have Beta(2,2) prior on a coin's bias θ. You flip it 3 times and observe HHH. What is the posterior and MAP estimate? How does this differ from the MLE?

Q4. When would you NOT use Bayesian inference in production and use frequentist methods instead?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →