Bayesian Monte Carlo and MCMCLesson 74 of 76 · 11 min

Account for serial dependence

Estimate a mean-specific effective sample size rather than using the independent-draw SE.

Autocorrelation compares a sequence with itself shifted by a lag: lag 1 pairs neighbours, lag 2 pairs values two steps apart. Stationarity means their joint distribution does not change with the position in the sequence. For a stationary sequence with finite variance and sufficiently decaying correlations, the mean's variance includes these lag contributions.

The correlation-time factor τ is 1+2×(lag-1 correlation+lag-2 correlation+…). Count only positive lags; their correlation values can have either sign. If the correlations are .5, .25, .125,…, they sum to 1, so τ=3: N rows provide about N/3 independent-draw equivalents for this mean. Mean ESS≈N/τ and mean SE≈outcome SD/sqrt(ESS). A tail indicator can have a different ESS.

Use the supplied AR(1), a first-order autoregression: next=ρ×previous+fresh normal noise with SD sqrt(1−ρ²). The initial standard normal draw makes this toy sequence stationary. At ρ=.9, the exact correlation sum gives τ=(1+ρ)/(1−ρ)=19 and large-sample ESS≈N/19. The code calculates sample lag correlations directly, adding successive pairs until a nonpositive pair or the stated lag cap; the cutoff is an approximation, not a convergence test.

τ\tau is the mean's correlation-time factor, N is retained row count, s is sample outcome SD and ESS is the independent-draw equivalent for this specific mean.

Rows are not an independent count
  1. Serially dependent draws
    X1,…,XnX_1,\ldots,X_n
  2. Mean-specific effective size
    ESS≈n/τ\mathrm{ESS}\approx n/\tau
  3. Adjusted mean SE
    SE(Xˉ)≈s/ESS\mathrm{SE}(\bar X)\approx s/\sqrt{\mathrm{ESS}}
Reference illustration. Here τ summarises serial correlation for the mean. For the authored AR(1) model, the long-run approximation is ESS ≈ n(1−ρ)/(1+ρ). Estimating ESS does not establish that an arbitrary MCMC chain reached its target.

For positively correlated draws, will the iid formula usually overstate or understate mean precision?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Draws: 16000 estimated mean ESS: 923.0
iid SE: 0.00787
Dependence-adjusted mean SE: 0.03278
Theoretical long-run ESS approximation: 842.1

Read the first result

The known AR(1) approximation gives a reference for this experiment. ESS is not a guarantee that an arbitrary MCMC chain reached its target.

Try an edit

Replace rho, n = .9, 16000 with rho, n = .3, 16000 and run. Then set rho to .95. Select Run Python again. Compare estimated mean ESS and Dependence-adjusted mean SE across runs. At rho 0.95 change default_rng(2073) to default_rng(74) and rerun. Compare the estimates against Theoretical long-run ESS approximation.

Back to the Python editor

Scope. This simplified calculation is for positively correlated, stationary AR(1) draws with finite variance. The lag cap can miss long dependence and τ is floored at 1. Negative correlations can legitimately give ESS greater than N, so this is not a general ESS routine. It does not diagnose warm-up, missed modes or tail accuracy.

Try a fresh case

Can a large ESS for the parameter mean guarantee an accurate rare-tail probability?

Choose an answer
Need a hint?
Hint 1

Replace the parameter by a threshold indicator.

Hint 2

Ask whether that new series has the same autocorrelation.

Your notes