Bayesian Inference

Priors, Posteriors, Shrinkage, Model Selection, and Decisions

We Have Already Used a Prior Once

Deck 04 shrank \(\bar X\) toward zero, \(\hat\mu_2 = c\bar X\), and showed that a little bias can lower the MSE — the idea behind ridge regression. That estimator is exactly maximum a posteriori estimation under a Gaussian prior centred at zero, and the deck never said so.

\[\hat\theta_{\text{MAP}} \;=\; \arg\max_\theta \; \underbrace{p(y \mid \theta)}_{\text{likelihood}}\;\underbrace{p(\theta)}_{\text{prior}}\]

What that slide did not say

The MAP estimate is one number read off a whole distribution. That distribution — the posterior — carries the uncertainty as well, and it answers questions a point estimate cannot.

Regularization was the posterior’s mode. This deck keeps the rest of it.

Bayes’ Theorem for Parameters

Definition — Posterior Distribution

For data \(y\) and parameter \(\theta\), \[p(\theta \mid y) \;=\; \frac{p(y \mid \theta)\,p(\theta)}{p(y)}, \qquad p(y) = \int p(y\mid\theta)\,p(\theta)\,d\theta\] The denominator does not involve \(\theta\), so in practice \[p(\theta \mid y) \;\propto\; p(y \mid \theta)\,p(\theta).\]

In words

Prior belief about \(\theta\), reweighted by how well each \(\theta\) predicted what you actually saw.

The shift

\(\theta\) is now a random variable — not because it moves, but because we are uncertain about it. Probability describes belief, not frequency.

Conjugate Updating

The Beta–Bernoulli Model

Estimating a proportion — a take-up rate, a job-finding rate — from yes/no observations.

Data

\[x_i \mid \theta \;\sim\; \text{Bernoulli}(\theta), \quad i = 1,\dots,n\] with \(k = \sum_i x_i\) successes

Prior

\[\theta \;\sim\; \text{Beta}(\alpha, \beta)\] \(\alpha, \beta > 0\) set the prior’s location and strength

Multiply the two, keeping only the parts that involve \(\theta\):

\[\underbrace{\theta^{\alpha-1}(1-\theta)^{\beta-1}}_{\text{prior}} \;\times\; \underbrace{\theta^{k}(1-\theta)^{n-k}}_{\text{likelihood}} \;=\; \theta^{\,\textcolor[RGB]{237,1,125}{\alpha+k}\,-1}\,(1-\theta)^{\,\textcolor[RGB]{237,1,125}{\beta+n-k}\,-1}\]

Posterior — the same family, with updated parameters

\[\theta \mid x \;\sim\; \text{Beta}\big(\textcolor[RGB]{237,1,125}{\alpha + k},\; \textcolor[RGB]{237,1,125}{\beta + n - k}\big)\] Successes are added to \(\alpha\), failures to \(\beta\). Nothing else about the data matters.

Beta–Bernoulli, Step by Step

Same data for both rows — 7 successes in 10 — and two different priors.

\(\text{Beta}(2, 2) \;\to\; \text{Beta}(2 + \textcolor[RGB]{237,1,125}{7},\; 2 + \textcolor[RGB]{237,1,125}{3}) = \text{Beta}(\textcolor[RGB]{237,1,125}{9}, \textcolor[RGB]{237,1,125}{5})\)

A prior worth 4 observations against 10 real ones: the data move the mean from 0.50 to 0.64.

\(\text{Beta}(20, 20) \;\to\; \text{Beta}(20 + \textcolor[RGB]{237,1,125}{7},\; 20 + \textcolor[RGB]{237,1,125}{3}) = \text{Beta}(\textcolor[RGB]{237,1,125}{27}, \textcolor[RGB]{237,1,125}{23})\)

A prior worth 40 observations: the same data move the mean only to 0.54.

Conjugate Updating, Live

At \(n = 0\) the posterior is the prior. As \(n\) grows the likelihood takes over and the four posteriors converge — the data outvote the prior.

Turn the overlay on and slide \(n\) from 10 to 400 to watch it happen.

What the Update Does

  • With \(n = 0\) the posterior is exactly the prior — Bayes’ rule degrades gracefully to “we knew nothing new”
  • Each observation shifts \(\alpha\) or \(\beta\) by one. The prior is data you are pretending to have already seen: \(\text{Beta}(2,2)\) is worth four observations, \(\text{Beta}(50,50)\) a hundred
  • As \(n\) grows the posteriors become indistinguishable: the likelihood dominates any fixed prior

Bernstein–von Mises, informally

Under regularity conditions the posterior concentrates on the true \(\theta\) and becomes approximately normal with variance \(1/(nI(\theta))\) — the same Cramér–Rao quantity deck 04 derived. Asymptotically, Bayesian and frequentist answers agree.

Where it fails is where it matters

Small samples, many parameters, and priors that put zero mass on the truth. Convergence is no comfort when \(n\) is 40.

The Normal–Normal Model

Now a mean rather than a proportion, with the noise level \(\sigma\) known.

Data

\[x_i \mid \theta \;\sim\; N(\theta, \sigma^2), \quad i = 1,\dots,n\] summarised by \(\bar x\), with precision \(n/\sigma^2\)

Prior

\[\theta \;\sim\; N(\mu_0, \tau_0^2)\] with precision \(1/\tau_0^2\)

Both are exponentials of a quadratic in \(\theta\), so their product is too — a normal again:

\[\exp\!\Big(\!-\frac{(\theta-\mu_0)^2}{2\tau_0^2}\Big) \times \exp\!\Big(\!-\frac{n(\theta-\bar x)^2}{2\sigma^2}\Big) \;\propto\; \exp\!\Big(\!-\frac{(\theta-\textcolor[RGB]{237,1,125}{\mu_1})^2}{2\textcolor[RGB]{237,1,125}{\tau_1^2}}\Big)\]

Posterior — precisions add, means are weighted by precision

\[\theta \mid x \sim N(\textcolor[RGB]{237,1,125}{\mu_1}, \textcolor[RGB]{237,1,125}{\tau_1^2}), \qquad \textcolor[RGB]{237,1,125}{\tau_1^2} = \big(\tau_0^{-2} + n\,\sigma^{-2}\big)^{-1}, \qquad \textcolor[RGB]{237,1,125}{\mu_1} = \tau_1^2\,\big(\tau_0^{-2}\,\mu_0 + n\,\sigma^{-2}\,\bar x\big)\]

Normal–Normal, Step by Step

A training programme: \(n = 10\) participants, average earnings gain \(\bar x = 1.6\) (hundred €), \(\sigma = 1\). Two sceptical priors centred at zero effect.

Precision: \(\tfrac{1}{4} + 10 = \textcolor[RGB]{237,1,125}{10.25}\) \(\;\Rightarrow\; \tau_1 = \textcolor[RGB]{237,1,125}{0.31}\)

Mean: \(\dfrac{0.25 \cdot 0 + 10 \cdot 1.6}{10.25} = \textcolor[RGB]{237,1,125}{1.56}\) — almost the data

Precision: \(4 + 10 = \textcolor[RGB]{237,1,125}{14}\) \(\;\Rightarrow\; \tau_1 = \textcolor[RGB]{237,1,125}{0.27}\)

Mean: \(\dfrac{4 \cdot 0 + 10 \cdot 1.6}{14} = \textcolor[RGB]{237,1,125}{1.14}\) — pulled toward zero

Beyond Conjugacy

When the Prior Is Not Conjugate

Conjugacy is luck: prior \(\times\) likelihood lands back in the prior’s family. Most pairs do not.

Example. The same yes/no data, but the prior is set on the log-odds \(\eta = \log\frac{\theta}{1-\theta}\) — natural when \(\theta\) comes out of a logit model:

\[\eta \sim N(m, s^2)\]

\[p(\eta \mid x) \;\propto\; \frac{e^{k\eta}}{(1+e^{\eta})^{n}}\; \exp\!\Big(\!-\frac{(\eta-m)^2}{2s^2}\Big)\]

This is not a named distribution, and the normalising constant \(p(y) = \int \cdots\, d\eta\) has no closed form.

What still holds

Bayes’ rule does. The posterior exists and is perfectly well defined — we just cannot write it down.

What we do instead

  • Grid: evaluate on a mesh, normalise numerically
  • Laplace: approximate by a normal at the mode
  • MCMC: draw samples from it

This is the normal case

Logit regression, hierarchical models, and the horseshoe and spike-and-slab priors below are all non-conjugate.

Non-Conjugate Updating, Live

The pink posterior is computed on a grid of 800 points — no formula anywhere.

The green curve is the Laplace approximation: a normal centred at the posterior mode, with the curvature there. Small \(n\) with extreme shares shows its limits; large \(n\) makes it nearly exact (Bernstein–von Mises again).

The default, 6 successes in 6, has no MLE: the likelihood keeps rising, and only the prior stops the posterior — with a long right tail the normal cannot follow.

Shrinkage

The Posterior Mean Shrinks

Rewrite the normal–normal posterior mean as a weighted average:

\[\operatorname{E}[\theta \mid x] \;=\; w\,\mu_0 + (1-w)\,\bar x, \qquad w = \frac{1/\tau_0^2}{1/\tau_0^2 + n/\sigma^2} = \frac{\text{prior precision}}{\text{total precision}}\]

  • The estimate is pulled toward the prior mean by the fraction \(w\) — it is shrunk
  • In the strong-prior example \(w = 4/14 = 0.29\): the raw estimate 1.6 moves 29% of the way to zero, to 1.14
  • More data (\(n \uparrow\)) or a vaguer prior (\(\tau_0 \uparrow\)) means less shrinkage

The noisiest estimates move the most

With the same prior, a group estimated from 5 observations is pulled far toward \(\mu_0\); one estimated from 500 barely moves.

One Formula, Four Names

In estimation

  • Ridge regression — the Gaussian prior behind deck 04’s \(c\bar X\), shrinking coefficients toward zero
  • Empirical Bayes — shrinking group estimates toward the grand mean

And elsewhere

  • The Kalman filter — each update is exactly this, with the prior carried forward in time
  • Credibility theory — how insurers price a small policyholder against the portfolio

Why economists should care

Regional unemployment from 40 respondents, school effects from one cohort, firm productivity from three years — all are noisy estimates of a quantity that resembles its neighbours. Shrinkage is the answer, and it is not a fudge: it lowers mean squared error.

Shrinkage, Live

The posterior always sits between the prior mean and the sample mean, and it is always narrower than both.

Shrink \(\tau_0\) toward 0.1 for a confident prior; push \(n\) up to let the data win.

Stronger Shrinkage: The Horseshoe Prior

A normal prior shrinks every estimate by the same fraction — noise and large effects alike. With many coefficients, most near zero, we want to shrink the small ones hard and leave the large ones alone.

\[\theta_j \mid \lambda_j, \tau \;\sim\; N(0,\ \lambda_j^2\,\tau^2)\] \[\lambda_j \sim \text{Cauchy}^{+}(0, 1)\]

  • \(\tau\) — global: how much shrinkage overall
  • \(\lambda_j\) — local: one per coefficient, heavy-tailed, so a single \(\theta_j\) can escape

Why “horseshoe”

The shrinkage factor \(\kappa_j = 1/(1+\lambda_j^2\tau^2)\) piles up at 0 (keep) and 1 (kill): a horseshoe-shaped density.

Horseshoe versus Normal, Live

One noisy estimate \(y \sim N(\theta, 1)\) — think of one coefficient among many.

Left: the posterior mean as a function of \(y\). The normal prior shrinks by the same fraction everywhere: a straight line. The horseshoe crushes small \(y\) to about zero but follows the diagonal once \(y\) is clearly large.

Right: the two posteriors for the current \(y\). Move \(y\) from 1 to 6.

When to Reach for the Horseshoe

Sparse problems

  • Many candidate regressors, few that matter: growth determinants, many controls
  • Many group effects, most negligible: regional or firm effects
  • Forecasting with large VARs

In practice

  • Non-conjugate — fitted by MCMC
  • R: brms with prior = horseshoe(), or rstanarm with prior = hs()
  • Its posterior mean is never exactly zero — to select variables you need the next idea

Ridge, lasso, horseshoe

All three are priors: ridge is the normal, lasso the Laplace (at its mode), and the horseshoe gives selective shrinkage — none for large effects, almost total for small ones.

Two Kinds of Interval

Credible versus Confidence

Credible interval

A set \(C\) with \[P(\theta \in C \mid y) = 1-\alpha\]

*“Given the data and the prior, there is a 95% probability that* \(\theta\) lies in here.”

Confidence interval

A rule with \[P_\theta\big(\theta \in C(Y)\big) = 1-\alpha \ \ \forall\theta\]

*“95% of intervals built this way contain* \(\theta\).”

The credible interval is what people think a confidence interval is

Deck 05 listed \(P(H_0 \mid \text{data})\) as something a p-value is not, and deck 04 listed the probability statement as something a confidence interval is not. The Bayesian posterior is the object that actually licenses it — at the cost of requiring a prior.

Two Intervals, One Dataset

With a flat prior and a large \(n\) the two intervals nearly coincide — which is why the misinterpretation survives.

Push \(k\) toward 0 or \(n\), or shrink \(n\), and they separate. Only one of them stays inside \([0,1]\) by construction.

The Two Sentences

  • With a flat prior and plenty of data the intervals nearly coincide numerically — but they are answers to different questions
  • A credible interval is a statement about \(\theta\) given these data. A confidence interval is a statement about the procedure across hypothetical repetitions
  • The posterior respects the parameter space: a probability cannot be negative, and the credible interval never is

The honest trade

The Bayesian statement is the one you wanted. It costs you a prior, and someone can always ask where the prior came from.

Evidence

Bayes Factors

Definition — Bayes Factor

\[\text{BF}_{10} \;=\; \frac{p(y \mid H_1)}{p(y \mid H_0)} \;=\; \frac{\int p(y\mid\theta)\,p_1(\theta)\,d\theta}{p(y\mid\theta_0)}\] It converts prior odds into posterior odds: \[\frac{P(H_1\mid y)}{P(H_0\mid y)} \;=\; \text{BF}_{10}\times\frac{P(H_1)}{P(H_0)}\]

What it does that a p-value cannot

It can favour \(H_0\). A p-value of 0.6 is not evidence for the null; a Bayes factor of \(\tfrac{1}{12}\) is.

What it costs

The integral needs a prior under \(H_1\) — and unlike the posterior, the Bayes factor never stops depending on it.

Model Selection: Spike and Slab

Many Models at Once

A Bayes factor compares two models. With \(p\) candidate regressors there are \(2^p\) models: 1,024 for \(p = 10\), a billion for \(p = 30\).

  • Give every coefficient an indicator \(\gamma_j \in \{0, 1\}\): in the model or out
  • Put a prior on the indicators, and let the posterior decide which regressors belong
  • Read off each regressor’s posterior inclusion probability

\[\text{PIP}_j \;=\; P(\gamma_j = 1 \mid y)\]

What you report

The PIPs instead of significance stars, and the median probability model: every regressor with \(\text{PIP}_j > 0.5\).

Where economists use it

Growth regressions with dozens of candidate determinants, and forecasting with many predictors: Bayesian model averaging.

The Spike-and-Slab Prior

\[\beta_j \mid \gamma_j \;\sim\; (1-\gamma_j)\,\underbrace{\delta_0}_{\text{spike}} \;+\; \gamma_j\,\underbrace{N(0, c_1^2)}_{\text{slab}}\] \[\gamma_j \;\sim\; \text{Bernoulli}(\pi)\]

  • Spike \(\delta_0\): a point mass — the coefficient is exactly zero
  • Slab (\(c_1\) wide): “some plausible non-zero value”
  • \(\pi\): the prior share of regressors you expect to matter

SSVS — stochastic search variable selection

A Gibbs sampler alternates \(\beta \mid \gamma\) (a conjugate normal update for the included coefficients; the rest are exactly 0) and \(\gamma_j \mid y, \gamma_{-j}\) (a coin flip per regressor, with \(\beta_j\) integrated out). \(\text{PIP}_j\) = share of draws with \(\gamma_j = 1\).

Inclusion Probability, Live

One coefficient, estimated as \(\hat\beta\) with standard error 1. Is it spike or slab?

The PIP is prior odds times a Bayes factor — slab against spike — turned back into a probability:

\(\dfrac{\text{PIP}}{1-\text{PIP}} = \dfrac{\pi}{1-\pi} \cdot \dfrac{N(\hat\beta;\,0,\,1+c_1^2)}{N(\hat\beta;\,0,\,1)}\)

So the Bayes-factor lesson carries over: the answer depends on the slab width \(c_1\). Make the slab very wide and a \(t\) of 2 no longer earns inclusion.

Choosing Regressors, Live

Eight candidate regressors; only \(x_1, x_2, x_3\) matter (\(\beta = 0.6,\ 0.35,\ 0.2\)), and \(x_4\) is correlated with \(x_1\) (\(\rho = 0.7\)).

With a point-mass spike and a \(g\)-prior slab (\(g = n\)), all \(2^8 = 256\) models can be scored exactly in the browser. SSVS samples the same posterior when \(2^p\) is too large to enumerate.

Bars: \(\text{PIP}_j\). A ★ marks \(|t| > 1.96\) in the full OLS regression.

Try small \(n\): the weak \(x_3\) drifts in and out, and \(x_4\) borrows credit from \(x_1\).

What Spike-and-Slab Buys You

  • Model uncertainty, stated. Not “the model”, but how much each regressor is worth including, given everything else that might be included
  • Averaging instead of picking. Predictions weighted by posterior model probability — Bayesian model averaging — beat any single selected model on average
  • Correlated regressors share the credit. When \(x_1\) and \(x_4\) move together, their PIPs trade off rather than both looking significant

Priors still matter

Like every Bayes factor, the PIPs depend on the slab width and on \(\pi\). Report them, and show what happens when they change.

In R

BoomSpikeSlab::lm.spike() (SSVS-style sampler), BAS::bas.lm() and BMS::bms() (model averaging over the model space).

Decisions

Loss, Risk, and What “Best” Means

Deck 04 defined “best” with a loss, averaged over the data. The Bayes estimator minimises the same losses, averaged over the posterior instead.

Definition — Bayes Estimator

Given a loss \(L(\theta, d)\) for reporting \(d\) when the truth is \(\theta\), the Bayes estimator minimises posterior expected loss: \[\hat\theta \;=\; \arg\min_{d}\ \operatorname{E}\big[L(\theta,d) \mid y\big] \;=\; \arg\min_d \int L(\theta,d)\,p(\theta\mid y)\,d\theta\]

Loss function Bayes estimator
Squared error \((\theta-d)^2\) posterior mean
Absolute error \(\lvert\theta-d\rvert\) posterior median
0–1 loss posterior mode (the MAP)
Asymmetric linear, costs \(a\) under / \(b\) over posterior quantile at \(a/(a+b)\)

Asymmetric Loss, Live

Under-forecasting a deficit and over-forecasting it do not cost the same.

With symmetric loss the optimum is the median. Make under-estimating four times as costly and the optimal report moves into the upper tail — deliberately, not as a bias.

The mean, median and mode of the posterior have not moved at all.

Why Economists Should Recognise This

  • It is expected-utility maximisation, with the unknown parameter as the state of the world — the framework is already familiar from micro
  • The estimator is not a property of the data alone. It depends on what a mistake costs
  • Inventory, capacity, deficit forecasts, capital buffers and reserve prices are all asymmetric-loss problems, and reporting the posterior mean is simply the wrong answer for them

The unifying claim

“Best” is never a purely statistical question. Squared-error loss is a choice, and it is the one that happens to be conventional.

Computation and Practice

The Computation Ladder

  • Conjugate — closed form, as in the Beta–Bernoulli and normal–normal models. Fast, exact, and restricted to a handful of model families
  • Grid approximation — evaluate the posterior on a mesh and normalise, as in the non-conjugate slide. Any model, up to about three parameters, then the mesh explodes
  • Laplace and variational — replace the posterior by a normal (or another simple family) fitted near the mode. Fast, approximate
  • MCMC — simulate a Markov chain whose stationary distribution is the posterior; SSVS is one. Everything else, at the cost of convergence diagnostics

What changed in practice

None of this was usable for real models until cheap computation arrived. That is why the Bayesian–frequentist argument was philosophical for a century and is now largely a question of which tool fits the problem.

Where Economists Meet This

Macro and finance

  • DSGE estimation — short samples, many parameters, priors doing real work
  • Bayesian VARs — shrinkage is what makes them forecast
  • Model averaging — weight models by posterior probability instead of picking one

Micro and policy

  • Hierarchical models for regional and firm panels
  • Small-area estimation — official statistics for thin geographies
  • Sequential experiments — yesterday’s posterior is today’s prior

The recurring theme

Every one has more parameters than the data can comfortably identify. A prior is not a philosophical stance there — it is what makes the problem tractable.

Summary

Key Takeaways

  1. The posterior is the whole answer; MAP, the posterior mean and the credible interval are all readings of it
  2. Conjugate priors update by adding to their parameters: successes to \(\alpha\), failures to \(\beta\); precisions add in the normal case. Without conjugacy, compute the posterior numerically
  3. The posterior mean shrinks. A normal prior shrinks everything alike; the horseshoe shrinks noise and spares large effects
  4. Spike-and-slab priors turn variable selection into posterior inclusion probabilities
  5. A credible interval licenses the probability statement people wrongly make about confidence intervals
  6. A loss function is what turns a posterior into a decision — and defines “best”

Bayesian and frequentist

  • Agree asymptotically, under regularity conditions
  • Differ where it matters: small \(n\), many parameters, constrained spaces, and genuine prior information
  • Bayes factors can support \(H_0\); p-values never can

The cost, stated plainly

Every Bayesian answer is conditional on a prior. Report it, and show what happens when it changes.