Probabilistic machine learning Section 16 · item 1 of 21

Bayesian
inference

Start with what you believed. Ask how well each possible truth explains what you just saw. Reweight accordingly. That loop is the whole method, and running it is the only thing any Bayesian technique — however elaborate — is ever doing.

The shift that matters is not the formula. It's that the answer is a distribution over parameters rather than a single number. Uncertainty stops being a footnote and becomes the output.

The update

posteriorlikelihood × prior
p(θ | D) = p(D | θ) p(θ)p(D)

Everything hard about Bayesian computation lives in that denominator.

Watch a belief sharpen

An unknown coin. Set what you believed before seeing it, then feed in flips. The coin is secretly biased to 0.65 — marked on the axis. Give it enough data and every starting belief converges there; give it very little and the prior is doing most of the talking.

prior likelihood (rescaled) posterior true θ = 0.65
1.0
1.0
Data so far0 flips
Posterior mean
95% credible interval
P(θ > 0.5 | data)
01The shift

The answer is a distribution

Classical estimation hands you a number and, separately, a statement about how that number would behave under hypothetical repetition. Bayesian inference hands you a distribution describing how plausible each possible value is, given the data you actually have.

What each approach treats as random

frequentist
The parameter is a fixed unknown constant. The data is random, and so is any estimate computed from it. Probability describes long-run frequencies.
bayesian
The data is what it is — you observed it. The parameter is uncertain, so it gets a distribution. Probability describes degrees of belief.

Neither is a mistake. They answer different questions, and the Bayesian one is usually closer to what a person actually wanted to ask.

What you get out

  • A full posterior, not a point. You can read off any summary you like from it — mean, mode, median, intervals, tail probabilities.
  • Direct probability statements. "There's an 89% chance the effect is positive" is a sentence a posterior licenses and a p-value does not.
  • Coherent updating. New data slots in without redoing anything from scratch.
  • Assumptions written down. The prior forces you to state what you brought to the problem, where other methods leave it implicit.
  • Uncertainty that propagates. Predictions inherit parameter uncertainty automatically. Section 09.
Everyone already does this informally. A doctor weighing a symptom against how common a disease is, an engineer discounting one anomalous reading against a hundred normal ones — that's prior times likelihood. Bayesian inference is the version where the bookkeeping is explicit and consistent, which mainly means you can be caught being wrong.
02Four parts

Prior, likelihood, posterior, evidence

p(θ | D) = p(D | θ) · p(θ)p(D)

Prior · p(θ)

What you believed about the parameter before this data. A distribution over every value it could take. Can encode real knowledge, deliberate ignorance, or a regularizing preference for simplicity.

Likelihood · p(D|θ)

How well each candidate value explains what you saw. Not a distribution over θ — it doesn't integrate to 1 in θ. It's a function scoring each hypothesis against fixed data.

Posterior · p(θ|D)

The updated belief. Always a compromise between prior and likelihood, weighted by how sharp each one is. This is the output.

Evidence · p(D)

How likely the data was overall, averaged across every value of θ. A normalizing constant that makes the posterior integrate to 1 — and the source of nearly all computational pain.

Why the proportional form is what people actually use

p(θ | D) ∝ p(D | θ) · p(θ)

The evidence doesn't depend on θ, so it changes the height of the posterior but never its shape. Drop it, work with the unnormalized product, and normalize at the end — which is exactly what MCMC does, and why MCMC never needs to compute the hard integral at all.

Point summaries, if you need one

posterior mean
The expected value. Minimizes squared error. The usual default.
posterior median
Minimizes absolute error. More robust for skewed posteriors.
MAP
The mode — the single most probable value. Cheap, and the bridge to regularized optimization.
MAP is regularized maximum likelihood. Take the log of prior × likelihood and maximize: a Gaussian prior becomes an L2 penalty, a Laplace prior becomes L1. Ridge and Lasso are MAP estimates. Anyone who has used weight decay has done Bayesian inference without the vocabulary.
03Updating

Today's posterior is tomorrow's prior

Evidence accumulates. Process a batch, get a posterior, then treat that posterior as your prior when the next batch arrives — the result is identical to having processed everything at once. Order doesn't matter either. That consistency is a genuine structural property, not a convenience.

The cleanest way to see accumulation is the odds form. Divide Bayes' rule for one hypothesis by the same rule for its complement, and the evidence term cancels completely:

posterior odds = prior odds × p(D | H)p(D | ¬H)

That fraction is the likelihood ratio — the only thing evidence contributes. Take logs and multiplication becomes addition, so each independent observation adds a fixed number of bits to your belief. Toggle the items below.

0.50%
posterior probability of fraud

Evidence lives on a log scale, and belief doesn't. Each item shifts you by a fixed amount in log-odds. Near a 50% probability that shift looks enormous; out at 99% the same evidence barely moves the needle. Turing's team at Bletchley measured evidence in exactly these units and called them bans.
The addition rule assumes independence given the hypothesis. Three correlated pieces of evidence — three witnesses who spoke to each other, three features that measure the same underlying thing — are not three independent multiplications. Treating them as such is how confident, badly wrong conclusions get built. This is also exactly the naive Bayes assumption, and exactly why naive Bayes is badly calibrated.
04Conjugacy

When the maths closes cleanly

For some prior-likelihood pairings the posterior stays in the same family as the prior, and updating reduces to arithmetic on the parameters. No integrals, no sampling. The hero above runs on exactly this.

LikelihoodConjugate priorPosteriorModels
Bernoulli / BinomialBeta(α, β)Beta(α + heads, β + tails)Conversion rates, click-through, defect rates
PoissonGamma(α, β)Gamma(α + Σx, β + n)Event counts, arrivals, failures per hour
ExponentialGamma(α, β)Gamma(α + n, β + Σx)Waiting times, time between failures
Normal (known σ)Normal(μ₀, σ₀²)Normal, precision-weighted meanMeasurement with known noise
Normal (both unknown)Normal-Inverse-GammaNormal-Inverse-GammaThe general one-dimensional case
Categorical / MultinomialDirichlet(α)Dirichlet(α + counts)Topic models, word distributions, multi-class rates

Pseudo-counts: the intuition that makes it stick

A Beta(α, β) prior behaves exactly like having already seen α−1 heads and β−1 tails. Beta(1,1) is flat — no imaginary data at all. Beta(30,30) is 58 imaginary fair flips, which is why it takes real effort to shift.

This makes prior strength something you can state in units a colleague understands: "my prior is worth about ten observations." Try the stubbornly-fair preset in the hero and then click flip 100 — you can watch the data outvote it.

Conjugacy is a convenience, not a principle. Real models are hierarchical, have many parameters, and use likelihoods with no conjugate partner. Modern practice reaches for MCMC or variational inference and chooses priors on their merits — conjugate families survive mainly for teaching, for fast online updating, and as components inside larger samplers.
Where it still earns its place: anything that must update in real time. Multi-armed bandits and Thompson sampling keep a Beta per arm and update with two additions per observation. That's fast enough to sit in a serving path.
05The hard part

Why the denominator ruins everything

p(D) = ∫ p(D | θ) p(θ) dθ

An integral over the entire parameter space. In one dimension you could evaluate it on a grid. In two or three, still fine. At fifty parameters a grid with ten points per axis needs 10⁵⁰ evaluations, which is more operations than there are atoms available to perform them.

This single integral is why Bayesian methods were largely theoretical until the 1990s. The framework was settled in the eighteenth century. The ability to actually compute with it, for models anyone cares about, arrived with cheap computers and the MCMC literature — which is why the field's practical history is so much shorter than its intellectual one.
The way out: stop trying to compute it. Because the evidence doesn't depend on θ, any method that only needs ratios of posterior densities never encounters it — the constant cancels. That observation is the foundation of every sampler in the next section.
ApproachIdeaCost / accuracy
ConjugacyPick a prior that makes the integral analyticExact and instant, but only for special pairings
Grid approximationEvaluate on a lattice and normalize numericallyExact enough in 1–3 dimensions; hopeless beyond
Laplace approximationFit a Gaussian at the mode using the HessianVery fast; wrong whenever the posterior is skewed or multimodal
MCMCDraw correlated samples from the posteriorAsymptotically exact, slow, needs diagnostics
Variational inferenceOptimize the closest member of a simple familyFast and scalable; systematically underestimates uncertainty
ABCSimulate data, keep parameters producing close matchesFor models you can simulate but cannot write a likelihood for
06MCMC

Sampling instead of solving

Build a random walk whose long-run visiting frequency is the posterior. Run it, keep the positions, and treat them as draws. Any quantity you wanted from the posterior becomes an average over samples.

0.80
Acceptance rate
Distinct positions
Verdict
A real Metropolis–Hastings chain, 4,000 iterations, run in your browser

Metropolis–Hastings, in five lines

  1. Start anywhere.
  2. Propose a nearby point.
  3. Compute the ratio of unnormalized posterior densities. The evidence cancels here.
  4. If the ratio exceeds 1, move. Otherwise move with probability equal to the ratio.
  5. Record the position and repeat.

Downhill moves are sometimes accepted, which is what stops it collapsing onto the mode and lets it map the whole distribution.

Step size is the whole game, and the slider shows why. Too small: nearly every proposal is accepted, but the chain shuffles and takes forever to cross the space. Too large: almost everything is rejected, the chain sticks in place for long stretches, and the samples are enormously correlated. Somewhere near 25–50% acceptance is the sweet spot for random-walk methods.
Nobody tunes this by hand any more. Hamiltonian Monte Carlo uses posterior gradients to propose distant points that are still accepted, and NUTS chooses its own trajectory length. That's what Stan, PyMC and NumPyro run by default, and it's the reason models with thousands of parameters are now routine.

R-hat

Run several chains from different starts and compare between-chain to within-chain variance. Values above about 1.01 mean they haven't converged on the same answer yet.

Effective sample size

Samples are correlated, so 10,000 draws may carry the information of 200 independent ones. ESS tells you how many you really have. Aim for several hundred per quantity of interest.

Divergences

HMC-specific, and the most useful warning in applied Bayes. They signal geometry the sampler cannot navigate — usually a model that needs reparameterizing. Never ignore them.

07Variational

Optimization instead of sampling

Give up on the exact posterior. Choose a family of tractable distributions, and find the member closest to the truth. Inference becomes an optimization problem, which means gradient descent, mini-batches and GPUs all apply.

The approximation is only as good as the family allows
ELBO = 𝔼q[log p(D, θ)] − 𝔼q[log q(θ)]

Maximizing the evidence lower bound is equivalent to minimizing KL(q ‖ p), and the gap between the ELBO and the true log evidence is exactly that divergence. The first term rewards explaining the data; the second rewards staying spread out.

The direction of the KL matters enormously. Minimizing KL(q‖p) punishes q for putting mass where p has none, but not for missing mass that p does have. The result is mode-seeking: variational posteriors are reliably too narrow, and on a multimodal target they will often find one mode and ignore the rest entirely.

Use it when the dataset is large, you need speed, and approximate uncertainty is acceptable. Avoid it when the uncertainty estimate is the deliverable — for a safety threshold or a clinical decision, run MCMC.

08Predicting

The posterior predictive

To predict a new observation, don't pick the best parameter and use it. Predict under every parameter value and average, weighted by how plausible each one is. Parameter uncertainty flows straight into the prediction.

p( | D) = ∫ p( | θ) p(θ | D) dθ
Lines drawn from the posterior · the fan is the uncertainty

In practice it's a sum, not an integral: you already have posterior samples, so draw a prediction from each one and collect the results. Twenty lines of code, and the spread you get back is honest rather than assumed.

Notice where the fan widens. A single fitted line is equally confident everywhere; the posterior predictive automatically becomes less certain away from the data, which is exactly the behaviour you want from anything making decisions.

Prior predictive checks come first. Before touching data, simulate from the prior alone and look at what it implies. If your priors generate human heights of 400cm or conversion rates of exactly zero, you've learned something important for free — and it costs one function call.
Posterior predictive checks come after. Simulate replicate datasets from the fitted model and compare their summary statistics against the real data. If the model cannot generate data that looks like what you observed, its parameter estimates aren't worth interpreting — no matter how tight the intervals are.
09Intervals

Credible is not confidence

Credible interval

"Given this model and this data, there is a 95% probability the parameter lies in [a, b]."

A statement about the parameter, conditional on what you observed. It is the sentence people want to say, and the Bayesian framework is what licenses saying it.

Confidence interval

"If I repeated this experiment many times and built an interval each time by this procedure, 95% of those intervals would contain the true value."

A statement about the procedure, not about the particular interval in front of you. Your specific interval either contains the value or doesn't; the 95% belongs to the method.

Almost everyone reads a confidence interval as though it were a credible interval. Usually the numbers land close enough that nothing goes wrong, especially with plenty of data and a flat prior. But they are different claims, and the difference becomes real with small samples, strong priors, or constrained parameters — where a credible interval respects the constraint and a confidence interval may not.

Equal-tailed interval

Chop 2.5% off each end. Simple, and invariant under monotone transformations. Can exclude the mode on a strongly skewed posterior, which looks odd.

Highest density interval (HDI)

The narrowest interval containing 95% of the mass. Always includes the mode, and can be disjoint on a multimodal posterior — which is informative rather than a bug.

10Priors

The part everyone argues about

The standard objection is that priors are subjective. The standard answer is that every method embeds assumptions, and the Bayesian ones are at least written down where a reviewer can attack them.

KindWhat it doesWhen
InformativeEncodes real domain knowledge or previous studiesYou genuinely know something. Say where it came from
Weakly informativeRules out the absurd, stays agnostic within the plausibleThe sensible default for most applied work
Flat / uniformEqual density everywhereFeels neutral; often isn't. Not flat after a reparameterization
ImproperDoesn't integrate to 1 at allSometimes yields a proper posterior. Breaks Bayes factors entirely
JeffreysInvariant under reparameterizationWhen "objectivity" matters and the model is simple
HierarchicalPriors whose parameters are themselves estimatedGrouped data. Produces partial pooling, which is often the whole reason to go Bayesian
RegularizingDeliberately shrinks toward zeroMany parameters, limited data. This is what ridge and lasso are
Report a sensitivity analysis. Refit under two or three defensible priors and show the posteriors side by side. If the conclusion holds across all of them, the prior objection evaporates. If it doesn't, you've discovered that your data is too weak to settle the question — which is itself the finding, and better learned by you than by a referee.
"Flat" is not the same as "uninformative". A uniform prior on a standard deviation is not uniform on the variance, so the choice of parameterization silently smuggles in a belief. And on unbounded parameters a flat prior places almost all its mass on absurdly large values. Weakly informative priors are nearly always the more honest option.
The prior washes out — until it doesn't. With enough data the likelihood dominates and the starting point stops mattering; the Bernstein–von Mises theorem makes this precise. But "enough" scales with the number of parameters. In high-dimensional or hierarchical models, or with rare events, the prior keeps its influence indefinitely. Sample size alone is not a defence.
11Comparison

Choosing between models

Bayes factors

BF = p(D | M1)p(D | M2)

The ratio of evidences — how much better one model predicted the data than the other. It penalizes complexity automatically, because a model that can explain anything spreads its predictive mass thin and therefore assigns less to what actually happened.

Brutally sensitive to the prior. Widen a prior and the Bayes factor moves, even though the posterior barely does — Lindley's paradox. With improper priors it isn't defined at all. Use them carefully or not at all.

Predictive criteria

WAIC
Estimates out-of-sample predictive accuracy from the posterior, with a complexity correction.
PSIS-LOO
Approximate leave-one-out cross-validation from the samples you already have. Generally preferred, and it flags its own unreliability.
K-fold CV
Refit the model on each fold. Expensive and hard to argue with.

These ask which model predicts better rather than which is more probable — usually the more useful question, and far less prior-sensitive.

Consider not choosing. Bayesian model averaging weights each model by its posterior probability and averages the predictions, which propagates model uncertainty instead of pretending you resolved it. Stacking does something similar with predictive weights and tends to perform better in practice.
12In machine learning

Where it actually shows up

MethodWhat's Bayesian about itWhy it's used
Ridge / LassoMAP with a Gaussian / Laplace priorNearly everyone doing this has no idea. The penalty is a prior
Bayesian linear regressionPosterior over coefficients, not point estimatesHonest coefficient uncertainty and predictive intervals
Gaussian processesA prior over functions, updated by dataNon-parametric regression with calibrated uncertainty built in
Bayesian optimizationGP posterior plus an acquisition functionHyperparameter tuning when each evaluation is expensive
Thompson samplingSample a parameter from the posterior, act greedily on itBandits and A/B testing. Elegant exploration with no tuning knob
Variational autoencodersVariational inference over a latent codeThe V in VAE is exactly the ELBO from section 07
Bayesian neural networksDistributions over weightsPrincipled, and still expensive enough to be rare in production
MC dropoutDropout at inference, read as approximate VICheap uncertainty from a network you already trained
Deep ensemblesNot formally Bayesian, but a similar posterior-averaging effectThe strong practical baseline for uncertainty in deep learning
Naive BayesBayes' rule plus a conditional independence assumptionFast, and surprisingly hard to beat on small text problems
Kalman filtersSequential Bayesian updating for linear Gaussian statesRobotics, tracking, sensor fusion — running this loop at 100 Hz
Hierarchical modelsPartial pooling across groupsThe killer application. Borrows strength across small groups
# the modern workflow, in PyMC — the model reads like the maths
import pymc as pm

with pm.Model() as model:
    # priors — weakly informative, stated explicitly
    intercept = pm.Normal("intercept", mu=0, sigma=10)
    slope     = pm.Normal("slope",     mu=0, sigma=5)
    noise     = pm.HalfNormal("noise", sigma=1)

    # likelihood
    mu = intercept + slope * x
    pm.Normal("y", mu=mu, sigma=noise, observed=y)

    # check the priors BEFORE looking at the data
    prior = pm.sample_prior_predictive()

    # NUTS, four chains, automatic tuning
    idata = pm.sample(2000, tune=1000, chains=4, target_accept=0.9)
    idata.extend(pm.sample_posterior_predictive(idata))

import arviz as az
az.summary(idata)              # means, HDIs, r_hat, ess — read r_hat first
az.plot_trace(idata)           # fuzzy caterpillars = healthy
az.loo(idata)                  # predictive comparison against another model
13When

Worth the cost, and when it isn't

Reach for it when

  • The uncertainty is the deliverable. Risk estimates, safety margins, anything feeding a decision with asymmetric costs.
  • Data is scarce or expensive. Priors do real work here, and this is where Bayesian methods most outperform.
  • You have genuine prior knowledge. Physical constraints, previous studies, expert calibration — throwing that away is a choice.
  • The structure is hierarchical. Many small groups, partial pooling. Frequentist alternatives exist and are clumsier.
  • Evidence arrives over time. Sequential updating is native, with no multiple-comparisons penalty for looking.
  • You need to explain the model. A generative model plus explicit priors is far easier to defend to a regulator than a tuned pipeline.

Skip it when

  • You have enormous data and only want a point prediction. The posterior collapses to the MLE; you paid for nothing.
  • Prediction accuracy is the sole metric. Gradient boosting will usually win on tabular data and take minutes.
  • The model is very high-dimensional. Sampling a deep network's weights is still largely impractical — deep ensembles are the pragmatic answer.
  • You need results this afternoon. MCMC takes real wall-clock time, and models need diagnosis.
  • Nobody on the team can maintain it. A model no one can debug at 2am is a liability regardless of its elegance.
A pragmatic middle. Fit the fast thing first and get a baseline. Then, if the decision depends on knowing how wrong you might be, build the Bayesian version for that specific quantity. Uncertainty quantification does not have to be all-or-nothing across a whole system.
14Practice

Drills that build the intuition

#Do thisYou'll know it when
1Do the Beta-Binomial update by hand on paper for ten flips, then check it against the hero abovePseudo-counts stop being an analogy
2Fit the same data with a flat prior and a strong wrong prior; plot both posteriors as n growsYou can say how much data is needed to overturn a given prior
3Implement grid approximation in 1D, then try it in 5D and watch it dieThe curse of dimensionality is felt, not recited
4Write Metropolis–Hastings from scratch in thirty linesYou understand why the normalizing constant never appears
5Deliberately mis-tune the step size and inspect the trace plotsYou can diagnose a bad chain from its picture alone
6Fit the same model in PyMC or NumPyro; read r_hat, ESS and divergencesYou check diagnostics before you look at results
7Run a prior predictive check and find a prior that generates absurd dataYou do this before every fit, automatically
8Build a hierarchical model over groups; compare against no pooling and complete poolingPartial pooling clicks, and you see why it's the main event
9Derive ridge regression as MAP with a Gaussian priorRegularization and priors are one idea in your head
10Take a decision you actually face and write down your prior odds and the likelihood ratiosThe framework leaves the notebook

The one-paragraph summary

Bayesian inference treats unknown parameters as uncertain and therefore as having distributions, and updates those distributions with data through Bayes' rule: the posterior is proportional to the likelihood times the prior. The output is a full distribution rather than a point, so uncertainty is a first-class result and probability statements about parameters become licensed. Updating is sequential and order-independent, and in odds form each independent piece of evidence contributes a fixed additive shift in log-odds. The normalizing constant is an integral over the whole parameter space and is intractable beyond a few dimensions, which is why practical Bayes is a story about computation: conjugate families when the maths closes, MCMC when you want asymptotic exactness, and variational inference when you want speed and can accept systematically narrow uncertainty. Predictions come from the posterior predictive, which averages over parameter uncertainty instead of conditioning on one estimate. Priors are the standard objection and the standard answer is a sensitivity analysis. It is worth the cost when uncertainty drives a decision, when data is scarce, or when the structure is hierarchical — and rarely worth it when you have abundant data and only want a point prediction.