Search

Search papers and lessons

Practical Bayesian Model Evaluation with Leave-One-Out CV and WAIC

Aki Vehtari, Andrew Gelman, Jonah Gabry · 2016

Estimates a Bayesian model's out-of-sample predictive accuracy from one set of posterior draws, using Pareto-smoothed leave-one-out cross-validation with a built-in reliability check, plus WAIC.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Inference
  • Calculus
  • Linear algebra
  • Sampling
  • Sign in to see your progress

TL;DR

Refitting a Bayesian model once for every held-out data point is too slow for most real models. This paper shows that leave-one-out predictive accuracy can be estimated from a single set of posterior draws by reweighting them, with the weights smoothed by a Pareto tail fit. The method also reports when its own estimate is unreliable. WAIC, a cheaper penalised-fit criterion, is presented alongside it.

Where you'll see it: in the loo package used to compare Bayesian models fitted with Stan, and in routine model checking of regression and multilevel models.

The problem

A Bayesian model is fitted once, and its posterior draws describe the parameters given the data. A natural question follows: how well would this model predict new data it has not seen? The usual answer is Cross-Validation. Hold out some data, refit the model on the rest, and score the predictions. For a Bayesian model with expensive posterior sampling, refitting once for every held-out point is often impractical. If a model takes an hour to fit and the dataset has a thousand points, leave-one-out cross-validation means a thousand hours of computation.

Information criteria offer a shortcut. The Akaike information criterion (AIC) and the deviance information criterion (DIC) estimate out-of-sample fit from a single fit. DIC is awkward for Bayesian work: it uses a point estimate of the parameters, can give negative estimates of the effective number of parameters, and is not defined for some singular models. Practitioners needed a method that was fully Bayesian, cheap to compute from existing posterior draws, and honest about when its own approximation failed.

Key idea

The paper's central object is the expected log pointwise predictive density, written elpd. For each data point, it is the log of the probability the model assigns to that point, averaged over new data drawn from the truth. Summing over the nn data points gives a single number for the model, and higher is better.

Leave-one-out cross-validation estimates this by asking, for each point ii, how well the model fitted to all the other points predicts point ii. Doing that exactly needs nn refits. The paper shows that the answer can be approximated from one set of posterior draws by reweighting them. It uses importance sampling, which reuses draws from one distribution to estimate expectations under another, and it stabilises the weights with a Pareto tail fit. The name for this is Pareto-smoothed importance sampling leave-one-out (PSIS-LOO).

WAIC takes a different route. It adds a penalty to the average fit on the observed data, and the penalty is computed from how much each point's log-likelihood varies across posterior draws. For a model that is well behaved, WAIC and LOO agree closely.

The picture to keep in mind is a student who has one practice exam and wants to predict the score on a different exam. Rather than sitting it again each time with one question removed, the student reweights the answers already written to estimate what would happen if each question had been left out, and checks whether that reweighting can be trusted.

The method

Leave-one-out from posterior draws

Let θ\theta denote the parameters, y1,…,yny_1, \dots, y_n the data, and p(θ∣y)p(\theta \mid y) the posterior. The exact leave-one-out quantity for point ii is p(yi∣y−i)p(y_i \mid y_{-i}), the probability of yiy_i under a posterior fitted to every other point. The summed quantity is

elpdloo=∑i=1nlog⁡p(yi∣y−i).\mathrm{elpd}_{\mathrm{loo}} = \sum_{i=1}^{n} \log p(y_i \mid y_{-i}).

The posterior with point ii removed is related to the full posterior by a factor 1/p(yi∣θ)1 / p(y_i \mid \theta). Averaging over the full-posterior draws θs\theta^s for s=1,…,Ss = 1, \dots, S gives the raw importance sampling estimate:

p(yi∣y−i)≈(1S∑s=1S1p(yi∣θs))−1p(y_i \mid y_{-i}) \approx \left( \frac{1}{S} \sum_{s=1}^{S} \frac{1}{p(y_i \mid \theta^s)} \right)^{-1}

This is the harmonic mean of the likelihoods of point ii across the draws. It is easy to compute, but it is unstable. When one draw gives a point a very small likelihood, its reciprocal is very large and dominates the sum. The weights 1/p(yi∣θs)1/p(y_i \mid \theta^s) can have infinite variance.

Smoothing the weights with a Pareto tail

PSIS replaces the largest weights for each point with values from a fitted generalised Pareto distribution. The tail of the weight distribution is modelled with a shape parameter k^\hat{k}, and the largest weights are replaced by quantiles of that fitted distribution. The fit is then used in place of the raw extreme values.

The shape parameter k^\hat{k} is also a diagnostic. The paper gives these working guides for interpreting it:

  • k^<0.5\hat{k} < 0.5: the estimate has finite variance and is reliable.
  • 0.5≤k^≤0.70.5 \le \hat{k} \le 0.7: the estimate is still useful in practice, but there is a warning.
  • k^>0.7\hat{k} > 0.7: the estimate is unreliable for that point. The paper recommends refitting or sampling directly from the leave-one-out posterior for those points, and using KK-fold cross-validation when there are many such points.

This is the key practical contribution. The method does not only give an answer. It says when that answer should not be trusted.

WAIC

WAIC estimates the same elpd as the sum of two pieces: a fit term and a penalty term.

elpd^waic=lpd^−p^waic\widehat{\mathrm{elpd}}_{\mathrm{waic}} = \widehat{\mathrm{lpd}} - \widehat{p}_{\mathrm{waic}}

The fit term is the log pointwise predictive density on the observed data, averaged over the draws:

lpd^=∑i=1nlog⁡(1S∑s=1Sp(yi∣θs))\widehat{\mathrm{lpd}} = \sum_{i=1}^{n} \log \left( \frac{1}{S} \sum_{s=1}^{S} p(y_i \mid \theta^s) \right)

The penalty is an estimate of the effective number of parameters. It is the sum over points of the sample variance, across draws, of the log-likelihood of each point:

p^waic=∑i=1nVars=1S(log⁡p(yi∣θs))\widehat{p}_{\mathrm{waic}} = \sum_{i=1}^{n} \mathrm{Var}_{s=1}^{S} \left( \log p(y_i \mid \theta^s) \right)

Each point's variance is computed separately and then summed. The paper notes that this is more stable than DIC's single-variance version. WAIC is asymptotically equal to Bayesian leave-one-out cross-validation, but it can fail in finite samples. The paper reports that it starts to fail when the posterior variance of the log predictive density for some point exceeds about 0.4, which is a sign of a point the model finds very influential.

Comparing two models

Comparing two models uses the difference of their elpd values. Because both models were evaluated on the same data, the standard error of the difference can be computed from the pointwise differences:

se(elpd^A−elpd^B)=n Vari=1n(elpd^iA−elpd^iB)\mathrm{se}\left( \widehat{\mathrm{elpd}}^{A} - \widehat{\mathrm{elpd}}^{B} \right) = \sqrt{ n \, \mathrm{Var}_{i=1}^{n} \left( \widehat{\mathrm{elpd}}_{i}^{A} - \widehat{\mathrm{elpd}}_{i}^{B} \right) }

A difference that is several standard errors wide is a real difference in predictive accuracy. A difference smaller than one standard error means the data do not clearly separate the models.

Results

The paper tests the methods on several examples fitted with Stan. They include a hierarchical model of eight schools, a regression of stack loss, a logistic model of leukaemia survival and a multilevel model of radon measurements, among others. For each example the authors computed exact leave-one-out values by refitting, and then compared each approximation against that reference over repeated simulations of the whole fitting process.

The main finding is that PSIS-LOO has the lowest root mean squared error across the examples. Raw importance sampling is unstable where the weights have high variance. Truncated importance sampling and WAIC are biased in some examples. KK-fold cross-validation needs additional refits, which PSIS-LOO avoids. When the diagnostic flags a few problem points, sampling directly for those points, as in the PSIS-LOO+ variant, improves the result at a modest extra cost.

The standard error results matter for applied work. They let a researcher report not just which model predicts better, but whether the difference is distinguishable from noise.

Why it mattered

The paper turned leave-one-out cross-validation from a method that was too costly for most Bayesian work into a routine step. The loo package in R, and ports in other languages, computes these quantities directly from the log-likelihood matrix that a Stan or similar fit already stores. The diagnostic, which flags points where the approximation cannot be trusted, made the method safe to use without expert tuning.

It also put WAIC and PSIS-LOO side by side with clear guidance. Practitioners now have a default, PSIS-LOO, with a check built in, and a faster alternative, WAIC, for cases where the diagnostics are clean. Model comparison in Bayesian workflows, including model averaging, typically starts from these pointwise quantities.

Reading guide

Read the prerequisites in this order, matched to the paper sections:

  1. Posterior Distribution and Likelihood Function: needed for the method sections, where the log-likelihood of each point is evaluated at each posterior draw.
  2. Posterior Predictive Distribution: needed for the definition of elpd, which is an expected log predictive density.
  3. Train, Validation and Test Sets and Cross-Validation: needed for the motivation and for the KK-fold comparison.
  4. Markov Chain Monte Carlo: needed to understand where the posterior draws come from, and why the method treats them as the input.
  5. Importance Sampling: needed for the core computation of leave-one-out from draws and the Pareto smoothing that follows.
  6. Kullback–Leibler Divergence and Bias-Variance Tradeoff: useful for the discussion of how predictive accuracy and model complexity trade off.
  7. Bayesian Model Averaging: useful for the model comparison ideas.

For the Results section you need items 1, 3 and 5. Item 4 helps with the reference computations.

Check yourself

QuestionRecall: what does the diagnostic value k^\hat{k} measure, and what does a value above 0.7 mean for a particular data point?Show answer
k^\hat{k} is the shape parameter of the generalised Pareto distribution fitted to the tail of that point's importance weights. A value above 0.7 means the tail is too heavy for the smoothed estimate to be reliable for that point, so the leave-one-out value for that point should be recomputed, for example by sampling directly from the leave-one-out posterior.
QuestionComputation: a point has likelihoods 0.2, 0.5, 0.25 and 0.4 across four posterior draws. What is the raw importance sampling estimate of its leave-one-out predictive density?Show answer
The reciprocals are 5, 2, 4 and 2.5. Their mean is 13.5 / 4 = 3.375. The estimate is the reciprocal of that mean: 1 / 3.375, which is about 0.296. This is lower than the plain average likelihood of 0.3375, which shows the harmonic-mean character of the raw estimate. A single small likelihood in the draws would push the reciprocal mean up sharply, which is why the raw estimate is unstable.
QuestionWhy: why does WAIC's penalty use the variance of each point's log-likelihood across draws, summed over points, instead of one variance of the total log-likelihood?Show answer
Summing per-point variances is more stable. Each point's variance is estimated from the same draws, and the sum of the per-point terms behaves well as the number of points grows. A single variance of the total log-likelihood is noisier and, as the paper notes for DIC, can produce unreliable or even negative estimates of the effective number of parameters.
QuestionApplied: model A has elpd_loo of -512.3 and model B has -515.9, with a standard error for the difference of 4.1. Is model A clearly better?Show answer
The difference is 3.6 in favour of model A, which is about 0.9 standard errors (3.6 / 4.1). That is not a clear separation. The data do not strongly distinguish the two models on predictive accuracy. A careful analyst would report both models, note the uncertainty, and check the Pareto k^\hat{k} diagnostics before trusting either number.

Sources

Read the full paper

Aki Vehtari, Andrew Gelman, Jonah Gabry, 2016. arXiv:1507.04544