Search

Search papers and lessons

Effective Sample Size

Level 3 · 13 min read

Effective sample size is how many independent draws a correlated Markov chain run is worth: the number of draws divided by the integrated autocorrelation time.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Linear algebra
  • Calculus
  • Sampling
  • Stochastic processes
  • ReadReady to readNot yet

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 NN draws has an effective size of about N/τN/\tau, where τ\tau 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 NN 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 τ\tau, an analyst can plan a run. Choose a target precision, estimate τ\tau from a short pilot run, and compute the draws needed. The same arithmetic shows whether tuning the sampler, which can lower τ\tau, 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 ff behave like a chain whose correlation halves at each step: the lag-1 correlation is 0.50.5, the lag-2 correlation is 0.250.25, the lag-3 correlation is 0.1250.125, and so on. We have N=100N = 100 draws after burn-in.

  1. Collect the draws. Keep the 100100 values of ff 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.
  2. Measure the memory. For each lag hh, find how strongly a draw is tied to the draw hh steps later. This is the autocorrelation: ρ(1)=0.5\rho(1) = 0.5, ρ(2)=0.25\rho(2) = 0.25, ρ(3)=0.125\rho(3) = 0.125. Each extra step halves the link. Check: each value is half the one before it.
  3. 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 0.8750.875. Lags 4 onward add 0.0625+0.03125+⋯=0.1250.0625 + 0.03125 + \cdots = 0.125. The total link is 0.875+0.125=10.875 + 0.125 = 1, so τ=1+2×1=3\tau = 1 + 2 \times 1 = 3. Check: the sum of all lags is 1, and 1+2×1=31 + 2 \times 1 = 3.
  4. Divide the draws by τ\tau. The effective sample size is N/τ=100/3≈33N/\tau = 100/3 \approx 33. Check: 33×3=9933 \times 3 = 99, which is close to 100.
  5. Read the answer. The average of the 100 draws is as precise as an average of 33 independent draws. Its standard error is 3≈1.73\sqrt{3} \approx 1.73 times the value you would get by treating the draws as independent. Check: 3=1.732\sqrt{3} = 1.732.
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 ff of unit variance, the standard error of the average is τ/N\sqrt{\tau/N}. The independent curve has τ=1\tau = 1, and the curve for τ=9\tau = 9 sits three times higher at every NN.

200400600800100000.20.40.60.81number of draws Nstandard error
  • independent draws (τ = 1)
  • correlated draws (τ = 9)
Standard error of the average of a quantity with unit variance. At N = 100, the correlated chain has error 0.3, three times the independent value of 0.1.

The math

Let X1,…,XNX_1, \dots, X_N be the draws after burn-in, and let ff be the function we want to average, such as f(x)=xf(x) = x for a posterior mean. Write σ2=Varπ(f)\sigma^2 = \mathrm{Var}_\pi(f) for the variance of ff under the target distribution, and fˉ=1N∑t=1Nf(Xt)\bar f = \frac{1}{N}\sum_{t=1}^{N} f(X_t) for the average of the draws. The average estimates Eπ[f]\mathbb{E}_\pi[f], as in the Law of Large Numbers.

Definition: Integrated autocorrelation time and effective sample size

Let ρ(h)\rho(h) be the autocorrelation of f(Xt)f(X_t) at lag hh. The integrated autocorrelation time is

τ=1+2∑h=1∞ρ(h),\tau = 1 + 2\sum_{h=1}^{\infty} \rho(h),

and the effective sample size of NN draws is

Neff=Nτ.N_{\text{eff}} = \frac{N}{\tau}.

NeffN_{\text{eff}} is the number of independent draws that would give the same standard error for fˉ\bar f.

Step 1 in symbols: the draws

The draws X1,…,XNX_1, \dots, X_N are the output of the chain. We compute fˉ\bar f from them. The question is how much fˉ\bar f varies from run to run, which means finding Var(fˉ)\mathrm{Var}(\bar f).

Step 2 in symbols: the autocorrelations

For a stationary chain, the Covariance of ff at lag hh is γ(h)=σ2ρ(h)\gamma(h) = \sigma^2 \rho(h). Lag 0 gives γ(0)=σ2\gamma(0) = \sigma^2.

Step 3 in symbols: the variance of the average

Expanding the square of the sum gives a double sum over all pairs of draws:

Var(fˉ)=1N2∑t=1N∑s=1Nγ(t−s).\mathrm{Var}(\bar f) = \frac{1}{N^2}\sum_{t=1}^{N}\sum_{s=1}^{N} \gamma(t-s).

Group the pairs by their lag h=t−sh = t - s. At lag hh there are N−∣h∣N - |h| pairs, because only that many pairs of draws are ∣h∣|h| apart. So

Var(fˉ)=σ2N∑∣h∣<N(1−∣h∣N)ρ(h).\mathrm{Var}(\bar f) = \frac{\sigma^2}{N}\sum_{|h| \lt N}\Big(1 - \frac{|h|}{N}\Big)\rho(h).

For a long run, the factor 1−∣h∣/N1 - |h|/N is close to 1 for the lags that matter, and the sum over ∣h∣<N|h| \lt N approaches the sum over all lags. Using the symmetry ρ(−h)=ρ(h)\rho(-h) = \rho(h), the lag 0 term appears once and every other lag appears twice:

Var(fˉ)≈σ2N(1+2∑h=1∞ρ(h))=σ2 τN.\mathrm{Var}(\bar f) \approx \frac{\sigma^2}{N}\Big(1 + 2\sum_{h=1}^{\infty}\rho(h)\Big) = \frac{\sigma^2\,\tau}{N}.

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, ρ(h)=0\rho(h) = 0 for every h≠0h \ne 0, so τ=1\tau = 1 and Var(fˉ)=σ2/N\mathrm{Var}(\bar f) = \sigma^2/N, the familiar formula. Setting the chain's variance equal to the independent formula with a size NeffN_{\text{eff}} gives

σ2 τN=σ2Neff⟹Neff=Nτ.\frac{\sigma^2\,\tau}{N} = \frac{\sigma^2}{N_{\text{eff}}} \quad\Longrightarrow\quad N_{\text{eff}} = \frac{N}{\tau}.

Estimating τ\tau in practice

We never know the ρ(h)\rho(h), so software replaces them with the sample autocorrelations r(h)r(h) 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, ρ(2m)+ρ(2m+1)\rho(2m) + \rho(2m+1), and stops at the first pair whose sum is not positive. A poor cutoff can make τ\tau too large or too small, and with it the ESS.

Worked examples

Example
Tiny: a chain whose memory halves each step

Take an AR(1) chain with φ=0.5\varphi = 0.5 (see autoregressive models and ARMA Model). From the Autocorrelation Function lesson, its autocorrelation is ρ(h)=0.5∣h∣\rho(h) = 0.5^{|h|}. Suppose we have N=12N = 12 draws.

  1. The lag correlations are ρ(1)=0.5\rho(1) = 0.5, ρ(2)=0.25\rho(2) = 0.25 and ρ(3)=0.125\rho(3) = 0.125.
  2. The sum over positive lags is a Geometric Series: ∑h≥10.5h=0.5/(1−0.5)=1\sum_{h \ge 1} 0.5^h = 0.5/(1 - 0.5) = 1.
  3. So τ=1+2×1=3\tau = 1 + 2 \times 1 = 3. This matches the AR(1) formula τ=(1+φ)/(1−φ)=1.5/0.5=3\tau = (1+\varphi)/(1-\varphi) = 1.5/0.5 = 3.
  4. The effective sample size is Neff=12/3=4N_{\text{eff}} = 12/3 = 4.
  5. Check through the variance: Var(fˉ)=σ2×3/12=σ2/4\mathrm{Var}(\bar f) = \sigma^2 \times 3/12 = \sigma^2/4. 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.

Example
Standard: 900 draws from a chain with τ = 9

Suppose a chain has τ=9\tau = 9 (an AR(1) chain with φ=0.8\varphi = 0.8 has this value, from the Autocorrelation Function lesson). We run it for N=900N = 900 draws and ff has variance σ2=4\sigma^2 = 4.

  1. The effective sample size is Neff=900/9=100N_{\text{eff}} = 900/9 = 100.
  2. The variance of the average is σ2τ/N=4×9/900=36/900=0.04\sigma^2\tau/N = 4 \times 9/900 = 36/900 = 0.04, so its standard error is 0.04=0.2\sqrt{0.04} = 0.2.
  3. For comparison, 900 independent draws would give a variance of 4/900≈0.004444/900 \approx 0.00444, a standard error of 0.00444≈0.0667\sqrt{0.00444} \approx 0.0667.
  4. The ratio of the two standard errors is 0.2/0.0667=3=90.2/0.0667 = 3 = \sqrt{9}, as the formula predicts.
  5. Check: 100 independent draws give a variance of 4/100=0.044/100 = 0.04, 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 1/τ1/\tau.

Example
Realistic: four chains for a posterior (illustrative)

A model is fitted with four chains of 2,000 draws each, after burn-in, so N=8,000N = 8{,}000 draws in total. Assume, for illustration, that two parameters have posterior variance σ2=0.09\sigma^2 = 0.09 (a standard deviation of 0.3) and integrated autocorrelation times τ1=25\tau_1 = 25 and τ2=200\tau_2 = 200.

  1. The ESS of parameter 1 is 8000/25=3208000/25 = 320. The ESS of parameter 2 is 8000/200=408000/200 = 40.
  2. The standard error of parameter 1's mean is 0.09×25/8000=0.00028125≈0.0168\sqrt{0.09 \times 25/8000} = \sqrt{0.00028125} \approx 0.0168.
  3. The standard error of parameter 2's mean is 0.09×200/8000=0.00225≈0.0474\sqrt{0.09 \times 200/8000} = \sqrt{0.00225} \approx 0.0474.
  4. Parameter 2 has eight times the autocorrelation time, so its standard error is 8≈2.8\sqrt{8} \approx 2.8 times larger. Check: 0.0474/0.0168≈2.80.0474/0.0168 \approx 2.8.
  5. Try it: parameter 2 has an ESS of 40 and you want 400 effective draws, with the same τ=200\tau = 200. How many total draws do you need? Answer: 400×200=80,000400 \times 200 = 80{,}000, 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 N/τN/\tau 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 ff. 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 φ=0.8\varphi = 0.8, the autocorrelation at lag 10 is 0.810≈0.1070.8^{10} \approx 0.107, so the thinned chain has τ≈1.24\tau \approx 1.24 and an ESS of about N/12.4N/12.4, below the original N/9N/9. 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 R^\hat{R} 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 τ\tau, 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 NN of them are not NN independent samples.
  • The link between draws is measured by the autocorrelation ρ(h)\rho(h), which fades with the lag.
  • Summing the links gives the integrated autocorrelation time τ=1+2∑h≥1ρ(h)\tau = 1 + 2\sum_{h \ge 1}\rho(h).
  • The variance of the average is σ2τ/N\sigma^2\tau/N, so the effective sample size is N/τN/\tau.
  • 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 τ\tau 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 φ=0.6\varphi = 0.6, and you collect N=2,000N = 2{,}000 draws. What is the effective sample size?Show answer

The integrated autocorrelation time is τ=(1+0.6)/(1−0.6)=1.6/0.4=4\tau = (1+0.6)/(1-0.6) = 1.6/0.4 = 4. The effective sample size is N/τ=2000/4=500N/\tau = 2000/4 = 500. The answer is 500, not 2,000.

QuestionWhy does the variance of an average from a correlated chain contain τ\tau, and not just the number of draws NN?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 σ2τ\sigma^2\tau instead of σ2\sigma^2, so the variance of the average is σ2τ/N\sigma^2\tau/N. Only when τ=1\tau = 1, which means no correlation, does the familiar σ2/N\sigma^2/N 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 10000/5=200010000/5 = 2000 and parameter B has an ESS of 10000/125=8010000/125 = 80. Their standard errors are proportional to τ\sqrt{\tau}, so the ratio is 125/5=25=5\sqrt{125/5} = \sqrt{25} = 5. Parameter A's mean is five times more precise than parameter B's, even though both came from the same run.

Sources