Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

17. Correlated data and block averaging

School of Engineering, Nazarbayev University

Track: advanced  ·  Prerequisites: Chapters 4 and 12

Reading: Allen & Tildesley, Computer Simulation of Liquids · Frenkel & Smit, Understanding Molecular Simulation · Flyvbjerg & Petersen (1989) · Sokal (1997)

The standard error s/Ns/\sqrt N assumes independent readings. Data recorded as a time series are rarely independent: successive configurations of a molecular dynamics or Monte Carlo simulation, a signal sampled faster than its own relaxation time, daily values of a slowly drifting quantity. Each new value then repeats part of what the previous one already said, and s/Ns/\sqrt N underestimates the uncertainty of the mean, often by a large factor. An error bar that is five times too small is a common and serious fault in simulation papers.

This notebook

  1. derives the variance of the mean of a correlated series and introduces the integrated autocorrelation time;

  2. uses a model series with a known answer to test the estimators;

  3. presents the two standard remedies: integration of the autocorrelation function, and block averaging;

  4. checks by simulation that the corrected error bars have the right coverage;

  5. extends the block method to nonlinear quantities with the jackknife.

17.1 The variance of the mean of a correlated series

Let x1,…,xNx_1, \dots, x_N be a stationary series: its statistical properties do not change along the series. It has mean μ\mu, variance σ2\sigma^2, and a normalised autocorrelation function

ρ(t)=⟨(xi−μ)(xi+t−μ)⟩σ2,ρ(0)=1,\rho(t) = \frac{\langle (x_i - \mu)(x_{i+t} - \mu)\rangle}{\sigma^2} , \qquad \rho(0) = 1 ,

which depends only on the separation tt. The mean xˉ=1N∑ixi\bar x = \tfrac1N\sum_i x_i has variance

var⁡(xˉ)=1N2∑i=1N∑j=1N⟨(xi−μ)(xj−μ)⟩=σ2N2∑i,jρ(∣i−j∣).\operatorname{var}(\bar x) = \frac{1}{N^2}\sum_{i=1}^{N}\sum_{j=1}^{N} \langle (x_i-\mu)(x_j-\mu)\rangle = \frac{\sigma^2}{N^2}\sum_{i,j}\rho(|i-j|) .

This is the propagation law with covariance (Section 12.1) applied to the mean. In the double sum the separation t=∣i−j∣t = |i-j| occurs NN times for t=0t=0 and 2(N−t)2(N-t) times for t≥1t \ge 1, so

var⁡(xˉ)=σ2N[1+2∑t=1N−1(1−tN)ρ(t)].\operatorname{var}(\bar x) = \frac{\sigma^2}{N}\left[1 + 2\sum_{t=1}^{N-1}\left(1 - \frac{t}{N}\right)\rho(t)\right] .

If the correlations decay over a time much shorter than the length of the series, the factor (1−t/N)(1 - t/N) can be replaced by one and the sum extended to infinity. Defining the integrated autocorrelation time

τint=12+∑t=1∞ρ(t),\tau_\text{int} = \frac12 + \sum_{t=1}^{\infty}\rho(t) ,

we obtain

var⁡(xˉ)=σ2N 2τint=σ2Neff,>Neff=N2τint.\operatorname{var}(\bar x) = \frac{\sigma^2}{N}\,2\tau_\text{int} = \frac{\sigma^2}{N_\text{eff}} , > \qquad N_\text{eff} = \frac{N}{2\tau_\text{int}} .

The series is worth NeffN_\text{eff} independent values: one for every 2τint2\tau_\text{int} steps. The naive standard error is too small by the factor 2τint\sqrt{2\tau_\text{int}}. For independent data ρ(t≥1)=0\rho(t\ge1) = 0, τint=12\tau_\text{int} = \tfrac12 and the usual result is recovered. The quantity g=2τintg = 2\tau_\text{int} is also called the statistical inefficiency.

Two remarks. First, τint\tau_\text{int} is a property of the observable: in the same simulation, the pressure may decorrelate in a hundred steps and a slow structural quantity in a million. Second, saving data less often does not lose much. Once the sampling interval approaches 2τint2\tau_\text{int}, the saved values are nearly independent, and the error bar is almost what it would be with every step saved.

17.2 A test series with a known answer

To test estimators we need a series whose τint\tau_\text{int} is known exactly. The first-order autoregressive process

xi+1=ϕ xi+1−ϕ2  ξi,ξi∼N(0,1),x_{i+1} = \phi\,x_i + \sqrt{1-\phi^2}\;\xi_i , \qquad \xi_i \sim N(0,1) ,

is the discrete analogue of an overdamped particle in a harmonic well. It has zero mean, unit variance, and ρ(t)=ϕt\rho(t) = \phi^{t} (multiply the recursion by xix_i and average: ρ(1)=ϕ\rho(1) = \phi, and so on). Hence

τint=12+∑t=1∞ϕt=12+ϕ1−ϕ=1+ϕ2(1−ϕ).\tau_\text{int} = \frac12 + \sum_{t=1}^{\infty}\phi^t = \frac12 + \frac{\phi}{1-\phi} = \frac{1+\phi}{2(1-\phi)} .

For ϕ=0.9\phi = 0.9 this gives τint=9.5\tau_\text{int} = 9.5: the naive standard error is too small by 19≈4.4\sqrt{19} \approx 4.4.

N = 65536,  exact tau_int = 9.50
mean of the series        : +0.0109   (true value 0)
naive standard error      : 0.0040
exact standard error      : 0.0170
deviation of the mean from zero: 2.8 naive standard errors, 0.6 exact standard errors

Judged by the naive error bar, the mean of this series deviates from the true value by nearly three standard errors, which would be reported as a significant effect. Judged by the correct error bar, it is well within one.

17.3 Remedy 1: integrate the autocorrelation function

The direct approach is to estimate ρ(t)\rho(t) from the data and sum it. The difficulty is the tail. The estimate of ρ(t)\rho(t) has a statistical noise that does not decrease with tt, while the true ρ(t)\rho(t) decays to zero. Summing far beyond the correlation time therefore adds only noise, and the sum must be cut off at a window MM:

τ^int(M)=12+∑t=1Mρ^(t).\hat\tau_\text{int}(M) = \frac12 + \sum_{t=1}^{M}\hat\rho(t) .

Too small a window truncates the true tail (bias); too large a window accumulates noise (variance). A robust compromise is to choose the smallest MM that satisfies M≥c τ^int(M)M \ge c\,\hat\tau_\text{int}(M) with c≈5c \approx 5, known as automatic windowing. ep.integrated_autocorrelation_time implements it.

estimated tau_int = 9.64  (window M = 49),  exact 9.50
standard error from tau_int: 0.0174,  exact 0.0170

17.4 Remedy 2: block averaging

The second approach avoids ρ(t)\rho(t) altogether. Cut the series into nbn_b consecutive blocks of length bb and average within each block. If bb is much longer than the correlation time, the block averages are practically independent, and the ordinary formula applies to them:

uxˉ=sbnb,u_{\bar x} = \frac{s_b}{\sqrt{n_b}} ,

with sbs_b the standard deviation of the block averages.

How long is long enough? The answer is read from the data by a blocking analysis: repeat the calculation for b=1,2,4,8,…b = 1, 2, 4, 8, \dots and plot the estimated standard error against bb. At b=1b = 1 it is the naive value. As bb grows, more of the correlation is contained inside the blocks and the estimate rises. Once b≫τintb \gg \tau_\text{int} it stops rising: the plateau is the standard error. For a series of the AR(1) type the approach is

u(b)≃u∞1−τcb (1−e−b/τc)u(b) \simeq u_\infty\sqrt{1 - \frac{\tau_\text{c}}{b}\,\bigl(1 - e^{-b/\tau_\text{c}}\bigr)}

with τc=−1/ln⁡ϕ\tau_\text{c} = -1/\ln\phi, which is within 5 % of the plateau for b≳10 τcb \gtrsim 10\,\tau_\text{c}.

The estimate at each block size has its own statistical uncertainty. Since it is a standard deviation computed from nbn_b values (Section 4.3),

u(u(b))u(b)≈12(nb−1),\frac{u\bigl(u(b)\bigr)}{u(b)} \approx \frac{1}{\sqrt{2(n_b - 1)}} ,

so the right-hand end of the curve, where few blocks remain, is noisy. The curve must be read with these error bars. If no plateau is reached before the error bars become large, the series is too short to determine its own uncertainty, and no method will rescue it.

<Figure size 1012x792 with 4 Axes>

Panel (a) compares the correlated series with independent noise of the same variance: the correlated series wanders, and stays on one side of zero for tens of steps. Panel (b) shows the estimated autocorrelation function, the exact ϕt\phi^t, and the summation window. Panel (c) is the blocking curve: it starts at the naive value, rises, and levels off at the exact standard error once the blocks are a hundred steps or more long, in agreement with the expected approach.

Panel (d) is the test that matters. For 1000 independent series the deviation of the mean from the true value is divided by the quoted error bar. If the error bar is right, this ratio follows a standard normal distribution. With the block error bar it does; with the naive one, the distribution is more than four times too wide. The numbers:

true standard error of the mean (exact)      : 0.03405
standard deviation of the 1000 means         : 0.03490

estimator                 typical value    within 1 u    within 2 u
naive s/sqrt(N)           0.00780         0.176         0.326
blocks of 512             0.03319         0.657         0.935
tau_int, auto window      0.03374         0.674         0.946
expected for a correct error bar                0.683         0.954

With the naive error bar the true value lies within one quoted standard error in fewer than a fifth of the runs. Both corrected estimators restore the expected coverage. The block estimate uses 32 blocks here, so its coverage is a little below the normal values for the reason discussed in Section 4.4 (an error bar estimated from few values calls for Student’s tt, with ν=nb−1\nu = n_b - 1).

Which method?

Autocorrelation integralBlock averaging
Gives τint\tau_\text{int}yesindirectly, 2τint=[u∞/u(1)]22\tau_\text{int} = [u_\infty/u(1)]^2
Precision for given datasomewhat betterlimited by the number of blocks
Diagnosticplot ρ^(t)\hat\rho(t) and the windowplateau of the blocking curve
Extends to derived quantitiesawkwardeasily, see below

In practice it is sensible to use one and check with the other.

17.5 Derived quantities: the jackknife on blocks

Many observables are not simple averages but nonlinear functions of averages: a heat capacity from the variance of the energy, a compressibility from volume fluctuations, a ratio of two averages, a free energy from −kBTln⁡⟨e−βΔU⟩-k_\mathrm{B}T\ln\langle e^{-\beta\Delta U}\rangle. Computing the quantity separately in each block and taking the spread is tempting, but a nonlinear function of a noisy block average is biased (Section 12.3), and short blocks make the bias worse.

The jackknife avoids this. Instead of evaluating the quantity on each block, evaluate it on all the data except one block:

θ(k)=θ(all blocks but the kth),k=1,…,nb.\theta_{(k)} = \theta(\text{all blocks but the } k\text{th}) , \qquad k = 1, \dots, n_b .

Each θ(k)\theta_{(k)} uses almost the whole series, so the nonlinearity does little harm. The leave-one-out values are strongly correlated with one another, which is accounted for by the factor (nb−1)(n_b - 1) in

uθ2=nb−1nb∑k=1nb(θ(k)−θˉ(⋅))2.u_\theta^2 = \frac{n_b - 1}{n_b}\sum_{k=1}^{n_b}\bigl(\theta_{(k)} - \bar\theta_{(\cdot)}\bigr)^2 .

For the mean itself this reduces exactly to sb/nbs_b/\sqrt{n_b}.

As an example we estimate the variance ⟨x2⟩−⟨x⟩2\langle x^2\rangle - \langle x\rangle^2 of the AR(1) series (true value 1), the prototype of all fluctuation formulas.

variance of x: 1.035 ± 0.018   (true value 1)
spread of the estimate over 300 independent series: 0.0169
naive error bar assuming independent data         : 0.0057

The jackknife error bar agrees with the actual spread over independent series, within the precision to be expected from 32 blocks, and is about three times the value obtained by treating the data as independent. (The estimate itself lies about two standard errors above the true value. That happens by chance about one time in twenty, and this is one of them.)

17.6 Two warnings

Equilibration. Everything above assumes a stationary series. The initial part of a simulation, during which the system relaxes from its starting configuration, is not a sample of the equilibrium distribution. Including it biases the mean, and the bias decreases only as 1/N1/N. Discard it. A drifting block average is the usual warning sign.

Slow modes that were never sampled. A blocking curve with a clean plateau proves only that the correlations visible in the data have been accounted for. If the system has a relaxation time longer than the whole run, such as a barrier never crossed, the series looks stationary and the error bar is confidently wrong. Statistical analysis cannot detect what the simulation did not visit. Independent runs from different starting points are the only defence.

Summary

  • For a correlated series, var⁡(xˉ)=2τint σ2/N\operatorname{var}(\bar x) = 2\tau_\text{int}\,\sigma^2/N. The naive standard error is too small by 2τint\sqrt{2\tau_\text{int}}.

  • τint\tau_\text{int} can be obtained by summing the autocorrelation function up to a self-consistent window.

  • Block averaging gives the standard error from the plateau of the blocking curve. No plateau means the run is too short.

  • For nonlinear functions of averages, use the jackknife over blocks.

  • None of this corrects for missing equilibration or unsampled slow modes.

References

  • M. P. Allen and D. J. Tildesley, Computer Simulation of Liquids, 2nd ed. (Oxford University Press, 2017).

  • H. Flyvbjerg and H. G. Petersen, “Error estimates on averages of correlated data”, J. Chem. Phys. 91, 461 (1989).

  • D. Frenkel and B. Smit, Understanding Molecular Simulation, 2nd ed. (Academic Press, 2002).

  • A. D. Sokal, “Monte Carlo methods in statistical mechanics: foundations and new algorithms”, in Functional Integration: Basics and Applications, edited by C. DeWitt-Morette, P. Cartier and A. Folacci (Plenum, New York, 1997).

Exercises

  1. Generate AR(1) series with ϕ=0.5\phi = 0.5, 0.99 and 0.999, all of length 216. Produce the blocking curve for each. For which is the series too short, and how does the curve tell you?

  2. Keep only every 20th value of the ϕ=0.9\phi = 0.9 series. How do the naive and the exact standard errors of the mean change? Explain.

  3. Show analytically that the jackknife formula reduces to s/ns/\sqrt n when θ\theta is the mean.

  4. Add a decaying transient 3 e−i/20003\,e^{-i/2000} to the series and repeat the blocking analysis. What happens to the mean and to the curve? Then discard the first 10 000 steps and repeat.