TL;DR
Effective sample size (ESS) is the number of independent draws that a correlated run is worth. A Markov chain Monte Carlo run of draws has an effective size of about , where is the integrated autocorrelation time, a number that measures how long the chain remembers where it was. The ESS tells you how precise an average from the run really is, and that precision can be far lower than suggests.
Builds on: Autocorrelation Function, Markov Chain Monte Carlo · Leads to: Hamiltonian Monte Carlo, Gelman-Rubin Diagnostic
Where it's used
Bayesian model fitting. A posterior distribution over dozens of parameters is usually explored by an MCMC sampler, and the sampler's output is then averaged to get posterior means. Probabilistic programming systems such as Stan print an effective sample size next to each parameter's estimate. A parameter with an ESS of a few dozen is a warning: its mean is known much less precisely than the raw number of draws implies, so the run should usually be lengthened or the model reparameterised.
Physics simulations. Monte Carlo simulations of lattices of interacting spins, and of molecules, produce chains whose successive configurations look alike. Researchers estimate the autocorrelation time of each measured quantity, such as an energy or a magnetisation, so that the error bars they report on averages are honest.
Planning how long to run. Because the ESS grows in proportion to the number of draws divided by , an analyst can plan a run. Choose a target precision, estimate from a short pilot run, and compute the draws needed. The same arithmetic shows whether tuning the sampler, which can lower , is cheaper than simply running longer.
How it works
Take a tiny run whose memory is easy to see. Suppose the values of a quantity behave like a chain whose correlation halves at each step: the lag-1 correlation is , the lag-2 correlation is , the lag-3 correlation is , and so on. We have draws after burn-in.
- Collect the draws. Keep the values of taken after burn-in. Each one is a sample of the target, but each one is also tied to its neighbours. Check: the raw count is 100.
- Measure the memory. For each lag , find how strongly a draw is tied to the draw steps later. This is the autocorrelation: , , . Each extra step halves the link. Check: each value is half the one before it.
- Add up the links. The integrated autocorrelation time counts the draw itself once, and each lag twice (once looking forward and once looking back). Lags 1 to 3 sum to . Lags 4 onward add . The total link is , so . Check: the sum of all lags is 1, and .
- Divide the draws by . The effective sample size is . Check: , which is close to 100.
- Read the answer. The average of the 100 draws is as precise as an average of 33 independent draws. Its standard error is times the value you would get by treating the draws as independent. Check: .
flowchart LR A["N draws after burn-in"] --> B["Autocorrelations rho(h) at each lag"] B --> C["tau = 1 + 2 times the sum of the autocorrelations"] C --> D["ESS = N divided by tau"] D --> E["Standard error of the average = square root of (tau / N) times the spread of f"]
The plot shows what step 5 means for different run lengths. With of unit variance, the standard error of the average is . The independent curve has , and the curve for sits three times higher at every .
- independent draws (τ = 1)
- correlated draws (τ = 9)
The math
Let be the draws after burn-in, and let be the function we want to average, such as for a posterior mean. Write for the variance of under the target distribution, and for the average of the draws. The average estimates , as in the Law of Large Numbers.
Let be the autocorrelation of at lag . The integrated autocorrelation time is
and the effective sample size of draws is
is the number of independent draws that would give the same standard error for .
Step 1 in symbols: the draws
The draws are the output of the chain. We compute from them. The question is how much varies from run to run, which means finding .
Step 2 in symbols: the autocorrelations
For a stationary chain, the Covariance of at lag is . Lag 0 gives .
Step 3 in symbols: the variance of the average
Expanding the square of the sum gives a double sum over all pairs of draws:
Group the pairs by their lag . At lag there are pairs, because only that many pairs of draws are apart. So
For a long run, the factor is close to 1 for the lags that matter, and the sum over approaches the sum over all lags. Using the symmetry , the lag 0 term appears once and every other lag appears twice:
The approximation assumes the autocorrelations die away fast enough that their sum converges. Chains that violate this, for example one that barely moves, need a different treatment.
Step 4 in symbols: the effective sample size
For independent draws, for every , so and , the familiar formula. Setting the chain's variance equal to the independent formula with a size gives
Estimating in practice
We never know the , so software replaces them with the sample autocorrelations from the autocorrelation function and sums them up to a cutoff. The tail of the sum is mostly noise, so the cutoff matters. A common rule (Geyer's initial positive sequence rule) adds the autocorrelations in adjacent pairs, , and stops at the first pair whose sum is not positive. A poor cutoff can make too large or too small, and with it the ESS.
Worked examples
Take an AR(1) chain with (see autoregressive models and ARMA Model). From the Autocorrelation Function lesson, its autocorrelation is . Suppose we have draws.
- The lag correlations are , and .
- The sum over positive lags is a Geometric Series: .
- So . This matches the AR(1) formula .
- The effective sample size is .
- Check through the variance: . That is the variance of an average of 4 independent draws.
Interpretation. Twelve draws from this chain carry the information of four independent draws. The other eight repeat, in effect, what the first four already told us.
Suppose a chain has (an AR(1) chain with has this value, from the Autocorrelation Function lesson). We run it for draws and has variance .
- The effective sample size is .
- The variance of the average is , so its standard error is .
- For comparison, 900 independent draws would give a variance of , a standard error of .
- The ratio of the two standard errors is , as the formula predicts.
- Check: 100 independent draws give a variance of , the same as the chain. So the chain is worth 100 independent draws.
Interpretation. The 900 correlated draws give the precision of 100 independent ones. Lengthening the run helps, but only in proportion to .
A model is fitted with four chains of 2,000 draws each, after burn-in, so draws in total. Assume, for illustration, that two parameters have posterior variance (a standard deviation of 0.3) and integrated autocorrelation times and .
- The ESS of parameter 1 is . The ESS of parameter 2 is .
- The standard error of parameter 1's mean is .
- The standard error of parameter 2's mean is .
- Parameter 2 has eight times the autocorrelation time, so its standard error is times larger. Check: .
- Try it: parameter 2 has an ESS of 40 and you want 400 effective draws, with the same . How many total draws do you need? Answer: , ten times the run that produced 8,000 draws.
Interpretation. One chain can serve two parameters very differently. The model is well estimated for parameter 1 and poorly for parameter 2, even though both came from the same 8,000 draws.
Common mistakes and tradeoffs
"Each draw is a fresh sample." It is tempting to think 10,000 MCMC draws give the precision of 10,000 independent samples. In fact successive draws repeat much of the same information, so the precision is that of about independent draws.
"ESS is a property of the chain." It is tempting to think one number describes a whole run. In fact ESS depends on the function . A chain can mix well for one parameter and poorly for another, so compute the ESS for each quantity you report.
"Thinning creates information." Keeping every tenth draw makes the kept draws look more independent, but it throws away nine in ten. The thinned chain never has more effective draws than the original. In the AR(1) chain with , the autocorrelation at lag 10 is , so the thinned chain has and an ESS of about , below the original . Thin only when storage is the constraint.
"Low ESS means the chain is wrong." Low ESS means the average is imprecise. A biased chain, one still stuck near its starting point or trapped in one of several modes, is a separate problem. Check it with trace plots and with the potential scale reduction from several chains (Gelman-Rubin Diagnostic). A chain can have a large ESS and still be wrong.
When to use it. Report the ESS of every quantity you estimate from MCMC, next to its mean, as the basic precision check. It also lets you compare samplers fairly by ESS per second of computation.
When not to use it. The ESS depends on estimating , which is unreliable for short chains and for heavy-tailed quantities whose variance is infinite or nearly so. In those cases the ESS can be badly wrong, and the analysis should rest on more chains, longer runs or a different estimator.
History
The idea of discounting a sample for correlation is older than MCMC. In survey sampling, a design effect compares the variance of an estimate to what simple random sampling would give, and the effective size of a sample is the size of a simple random sample with the same variance. Leslie Kish's survey-sampling work in the 1960s formalised these ideas.
Metropolis and colleagues introduced the chain-based method in 1953 (Metropolis Algorithm), but for decades its statistical analysis was patchy. Charles Geyer's 1992 review Practical Markov Chain Monte Carlo set out estimators of the variance of a chain's average built from its autocorrelations, which are the basis of the ESS calculations in common use. The measure caught on as Bayesian software in the 2000s and 2010s made it routine to fit models by MCMC and to report how precisely each parameter was estimated.
Recap
- The draws from an MCMC run are correlated, so of them are not independent samples.
- The link between draws is measured by the autocorrelation , which fades with the lag.
- Summing the links gives the integrated autocorrelation time .
- The variance of the average is , so the effective sample size is .
- Compute the ESS for each quantity. A low ESS is a warning about precision; it does not, by itself, show bias.
What to read next: Hamiltonian Monte Carlo lowers by using gradients to propose better moves, and Gelman-Rubin Diagnostic checks whether the chains have converged at all.
Check yourself
QuestionIn words, what does an effective sample size of 250 for a parameter mean?Show answer
It means the average of that parameter's draws is as precise as the average of 250 independent draws from the target. The run may contain many more draws, but their correlation means only 250 of them count as independent information for that parameter.
QuestionAn AR(1) chain has , and you collect draws. What is the effective sample size?Show answer
The integrated autocorrelation time is . The effective sample size is . The answer is 500, not 2,000.
QuestionWhy does the variance of an average from a correlated chain contain , and not just the number of draws ?Show answer
Because the covariances between draws do not vanish. Each draw is correlated with the draws near it, and summing the variance over all pairs of draws gives a term for each lag, weighted by its autocorrelation. Those terms add up to instead of , so the variance of the average is . Only when , which means no correlation, does the familiar return.
QuestionOne chain of 10,000 draws gives parameter A an integrated autocorrelation time of 5 and parameter B one of 125. Assuming equal posterior variances, which mean is more precise, and by what factor in standard error?Show answer
Parameter A has an ESS of and parameter B has an ESS of . Their standard errors are proportional to , so the ratio is . Parameter A's mean is five times more precise than parameter B's, even though both came from the same run.
Sources
- Markov chain Monte Carlo (Wikipedia)
- Autocorrelation (Wikipedia)
- Geyer, C. J. (1992), Practical Markov Chain Monte Carlo, Statistical Science: https://doi.org/10.1214/ss/1177011137