Lab 6.8 · 30 min

Lab: error bars by hand

Every number from a simulation needs an error bar, and the textbook formula s/√N is almost always wrong for MD, because consecutive frames are not independent. We compute error bars by hand on 10 and 16 numbers, then use block averaging and the statistical inefficiency on a process with a known answer and on the pressure of our 9-atom box. The numbers show how many independent samples a run really contains, how errors combine, and how long a run must be.

The setup

An average from a simulation is itself a random number: run again with other random velocities and you get a slightly different value. The error bar says how much. Two facts make MD special:

1. frames are saved every few steps, but the system changes slowly: consecutive frames are correlated 2. the formula error = s / √N assumes independent samples so: N frames ≠ N independent samples the true number is N_eff = N / g, g = statistical inefficiency

Notation: s is the standard deviation of the samples, "error" means the standard error of the mean (one σ, about 68 % confidence).

Step 1

The textbook error bar

Why: Start with the case where the formula is right: independent measurements. The spread of the samples tells how much one measurement varies; the error of the mean is smaller by √N because random deviations partly cancel.

10 independent pressure readings: 1.2 0.4 2.1 1.7 −0.3 1.1 0.9 2.6 0.2 1.5 mean = 11.4 / 10 = 1.140 Σ (P − mean)² = 7.0640 s = √(7.0640 / 9) = 0.8859 (divide by N − 1) error = s / √N = 0.8859 / 3.1623 = 0.2802 result: P = 1.14 ± 0.28

What the numbers say: One reading varies by ±0.89, the mean of ten by ±0.28. To halve the error you need four times more independent samples: errors fall only as 1/√N. This is the formula most analysis scripts use by default, and for MD data it gives error bars that are far too small, as the next steps show.

Step 2

Correlated data: the statistical inefficiency

Why: In MD, a frame looks much like the one before it. Such samples carry less information than independent ones. The autocorrelation ρ(k) measures how similar values k frames apart are; adding it up tells how many frames count as one independent sample.

16 consecutive readings: 1.0 1.2 1.5 1.4 1.1 0.7 0.5 0.6 0.9 1.3 1.6 1.8 1.5 1.2 0.8 0.9 mean = 1.1250, s = 0.3786 ρ(k) = ⟨δx(t) δx(t+k)⟩ / ⟨δx²⟩ lag 1: 0.1021 / 0.1344 = 0.760 lag 2: 0.0169 / 0.1344 = 0.126 lag 3: −0.556 (negative: stop adding here) g = 1 + 2 (0.760 + 0.126) = 2.77 N_eff = 16 / 2.77 = 5.8 independent samples naive error s/√16 = 0.0946 corrected error s √(g/N) = 0.1576 (1.66 × larger)

What the numbers say: Sixteen correlated numbers are worth about six independent ones, and the honest error bar is 1.66 times the naive one. For real MD, where frames may be saved every 10 fs while the system needs picoseconds to forget, g can be in the hundreds. The limitation: ρ(k) from a short series is noisy (here it swings to −0.56 at lag 3), so the sum must be cut off sensibly (at the first negative value is a common rule), and the result is only an estimate.

Step 3

Block averaging by hand

Why: Block averaging avoids computing ρ(k) at all. Cut the series into blocks, average each block, and treat the block means as samples. Once the blocks are longer than the memory of the data, the block means are independent and the naive formula becomes correct for them.

same 16 numbers block size 1: 16 means error = 0.0946 block size 2: 8 means 1.10 1.45 0.90 0.55 1.10 1.70 1.35 0.85 error = 0.1299 block size 4: 4 means 1.275 0.725 1.400 1.100 error = 0.1468 block size 8: 2 means 1.000 1.250 error = 0.1250 error = std(block means) / √(number of blocks)

What the numbers say: The error grows with block size (0.095 → 0.130 → 0.147) and stops growing once blocks are longer than the correlation: the plateau, 0.147, agrees with the g-corrected 0.158. Beyond that, too few blocks remain (two blocks of 8 give 0.125, itself very uncertain). In practice: increase the block size until the error levels off, and read the plateau while there are still at least 20–50 blocks. Squaring the ratio plateau/naive gives g directly: (0.147/0.095)² = 2.4.

Step 4

A test with a known answer

Why: To trust the method, apply it where the exact answer is known: x(t+1) = φ x(t) + noise. Each value keeps a fraction φ of the previous one, the correlation is exactly ρ(k) = φᵏ, and g = (1 + φ)/(1 − φ).

φ = 0.9, 20 000 samples, variance 1 g = (1 + 0.9)/(1 − 0.9) = 19 naive error 1/√20 000 = 0.0071 exact error √(19/20 000) = 0.0308
block size141664128256512
error0.00720.01350.02280.03160.03310.03130.0317
200 independent runs: their means scatter by 0.0302 runs whose ±1 error bar contains the true mean 0: naive bars 17 %, corrected bars 72 % (should be 68 %)

What the numbers say: The block curve rises from the naive 0.0072 to a plateau of about 0.031 at blocks of 64–512, exactly the exact error 0.0308 and the real scatter of 200 independent runs, 0.0302. With naive error bars, only 17 % of runs would "contain" the true value instead of 68 %: the naive error bars claim 4.4 times more precision than the data have. Here, 20 000 samples are worth about 1000 independent ones.

Step 5

A real simulation: the pressure of the 9-atom box

Why: Now a quantity from an actual MD run: the instantaneous pressure of our 9-atom LJ box at T = 0.72, recorded every step for 100 000 steps. We compute the naive error, the block-averaging error, and check them against 20 independent runs.

mean = 0.6030, s = 1.1149 naive error s/√100 000 = 0.0035
block (steps)blockserroruncertainty of the error
1100 0000.00350 %
1010 0000.01081 %
1001 0000.02372 %
5002000.03385 %
10001000.03537 %
2000500.038410 %
10000100.064024 %
plateau (blocks 500–2000) = 0.0358 g = (0.0358 / 0.0035)² = 103 steps (0.52 time units) independent samples in 100 000 steps: 969 check: 20 independent runs of 20 000 steps means scatter by 0.0786 -> for 100 000 steps 0.0786/√5 = 0.0351

What the numbers say: The honest result is P = 0.603 ± 0.036, ten times the naive ± 0.0035, and independent runs confirm it (0.0351). The system forgets its pressure only every 103 steps, so saving it every step gives no extra information beyond one sample per ~100 steps. The last rows show the limitation of block averaging: with 10 blocks the error estimate is itself uncertain by ±24 % and drifts up; read the plateau where there are still enough blocks. The uncertainty of an error from n blocks is about 1/√(2(n − 1)).

Step 6

Errors of derived quantities

Why: Results are often ratios or differences of measured numbers: a Haven ratio, a free-energy difference, a change of D with temperature. Independent errors add in squares: absolute errors for sums and differences, relative errors for products and ratios.

Haven ratio from Lab 6.7 (one run): D_self = 1.003 ± 0.024 (2.4 %), D_σ = 0.665 ± 0.171 (25.7 %) H = 1.003 / 0.665 = 1.508 relative error √(0.024² + 0.257²) = 0.258 H = 1.51 ± 0.39 average of 10 independent runs: ± 0.39/√10 = ± 0.12 difference of two close numbers: (5.0 ± 0.2) − (4.0 ± 0.2) = 1.0 ± √(0.2² + 0.2²) = 1.0 ± 0.28 (28 % from 4–5 % inputs)

What the numbers say: The error of a ratio is dominated by its noisiest part: the 2.4 % of D_self hardly matters next to the 26 % of the collective term, so the Haven ratio inherits ±26 %. Differences are worse: two numbers known to 4–5 % give a difference known only to 28 %, which is why small free-energy differences or small changes between two force fields need long, careful runs. When the parts are correlated (computed from the same trajectory), these formulas are only approximate; the safest error bar then comes from repeating the whole analysis on independent runs or blocks.

Step 7

Equilibration and how long to run

Why: Two practical questions remain. If the run starts away from equilibrium (a box that is too large, velocities too hot), the start biases the average. And before running, one wants to know how many steps a target precision needs.

x(t+1) = 0.99 x(t) + noise (true mean 0, g = 199), started at 30 instead of near 0. Block means of 1000 steps: 3.429 0.408 −0.123 −0.203 −0.177 0.158 −0.034 ... the start adds 30/(1 − 0.99) = 3000 to the sum: 0.300 to the mean mean of all 10 000 steps 0.306 ± 0.141 (2σ from 0) after discarding first 1000 −0.041 ± 0.149 how long to run, P of the LJ box to ± 0.01: N_eff = (s / target)² = (1.115 / 0.01)² = 12 431 steps = N_eff × g = 12 431 × 103 = 1.28 million

What the numbers say: Including the equilibration period shifts the mean by 0.30, twice the error bar, a systematic error that no amount of averaging removes. Discarding it costs a little precision (0.141 → 0.149) and removes the bias. Look at block means or running averages, and discard everything before they settle. For planning, the formula N_eff = (s/target)² × g turns a target precision into a number of steps: halving the error costs four times the run. These estimates assume the run samples all relevant states; slow events (Lab 4.6 dihedrals, Lab 7.4 barriers) need far more than the formula suggests.

Summary

methodmerit (number)limitation (number)
s/√Ncorrect for independent data (1.14 ± 0.28)MD: 4–10× too small; covers the truth 17 % of the time
statistical inefficiency gN_eff = N/g (16 -> 5.8; 100 000 -> 969)noisy ρ(k); needs a cut-off rule
block averagingno ρ(k) needed; plateau 0.0358 = runs 0.0351needs ≥ 20–50 blocks; ±24 % with 10
independent runsthe most honest checkcosts several equilibrations
error propagationratio: relative errors in squaresdifferences: 28 % from 4–5 % inputs
discard equilibrationremoves a 2σ biasslightly larger error (0.141 -> 0.149)