Tutorials › Stochastic Processes › Trace, Running-Mean, and Autocorrelation Diagnostics

Markov Chain Monte Carlo · Tutorial 930 of 994

Trace, Running-Mean, and Autocorrelation Diagnostics

Interpret three complementary visual diagnostics for MCMC output, calculate their summaries, and recognize what they cannot establish.

Advanced 12 min read

What You'll Learn

  • Define trace plots and running means for a scalar summary of an MCMC state.
  • Compute empirical autocorrelations from a finite output sequence.
  • Identify patterns consistent with drift, persistence, or strong serial dependence.
  • Use the running-mean update identity to interpret changes over time.
  • Explain why flat or reassuring-looking plots do not prove convergence.
  • Recognize when an empirical autocorrelation is undefined.

Three Views of One MCMC Output

An MCMC run is a time-ordered sequence, not a collection of independent observations. A trace plot preserves that order; a running mean shows how an estimate accumulates; and an autocorrelation plot summarizes linear dependence at different lags. Each view can reveal features that are difficult to notice from a histogram or a single final estimate. None, by itself, certifies that the chain has converged to its target distribution or explored all relevant parts of the state space.

Let \((\Omega,\mathcal{F},\mathbb{P})\) be a probability space, and let \((X_n)_{n\ge 0}\) be a discrete-time Markov chain on a state space \(S\), with its natural filtration \(\mathcal{F}_n=\sigma(X_0,\ldots,X_n)\). For a scalar quantity of interest, choose a measurable function \(f:S\to\mathbb{R}\) and write \(Y_n=f(X_n)\). Diagnostics are usually made from a finite retained sequence \(y_1,\ldots,y_N\) of these scalar values. If the state itself is scalar, one can take \(f(x)=x\). For a vector-valued state, a trace of one coordinate or one scientifically meaningful function is only a view of the full state.

Definition: A trace plot places the observed values \(y_n\) against their iteration indices \(n\). The running mean at iteration \(n\) is \(\bar y_n=\frac{1}{n}\sum_{i=1}^n y_i\). If the process \(Y_n\) is weakly stationary with finite positive variance, its lag-\(k\) autocorrelation is \(\rho_k=\operatorname{Cov}(Y_0,Y_k)/\operatorname{Var}(Y_0)\). For observed data, a common empirical autocorrelation at lag \(k\), \(0\le k<N\), is \(r_k=\frac{\sum_{i=1}^{N-k}(y_i-\bar y_N)(y_{i+k}-\bar y_N)}{\sum_{i=1}^N(y_i-\bar y_N)^2}\), provided the denominator is positive.

The empirical formula uses the overall sample mean \(\bar y_N\), even when only \(N-k\) pairs contribute to its numerator. This convention makes \(r_0=1\) whenever the observations are not all equal. Other software conventions exist, so comparisons should use the convention actually reported. A population autocorrelation describes a stationary process; a finite-sample \(r_k\) is a descriptive estimate and does not make a nonstationary sequence stationary.

How to Read the Diagnostics

A trace plot is primarily a record of order and movement. Look for sustained trends, long stretches at similar values, abrupt shifts, repeated patterns, and unusually large excursions. Such features may be evidence of slow exploration, dependence, or a changing distribution over the displayed interval. Their absence is not proof that the chain is sampling its target correctly: a trace can look stable while remaining in a region it has not left.

The running mean addresses a different question: how much is the estimated average changing as observations are added? It is cumulative, so early observations continue to affect it, with decreasing weight. A stable-looking tail of running means is reassuring about the observed average over that run, but does not establish that the target expectation has been estimated accurately. In particular, if the chain has not visited an important region, the running mean may settle to a misleading value.

An autocorrelation plot displays \(r_k\) against lag \(k\). A slowly decaying positive pattern indicates that nearby observations tend to remain similar across multiple iterations; this is a warning that the chain moves through the observed quantity slowly. Negative autocorrelation indicates a tendency for deviations from the mean to alternate in sign. The pattern across lags matters: one value, such as \(r_1\), is not a complete description of dependence. Autocorrelation is also specific to the selected function \(f\). A coordinate can appear to move well while another function of the state moves poorly.

Result (running-mean update): For \(n\ge2\), the running means satisfy \(\bar y_n-\bar y_{n-1}=(y_n-\bar y_{n-1})/n\). Thus the change at iteration \(n\) is the new observation’s deviation from the previous running mean, divided by \(n\).

Proof. By definition, \(n\bar y_n=\sum_{i=1}^n y_i\) and \((n-1)\bar y_{n-1}=\sum_{i=1}^{n-1}y_i\). Subtracting the second equality from the first gives \(n\bar y_n-(n-1)\bar y_{n-1}=y_n\). Rearranging yields \(n(\bar y_n-\bar y_{n-1})=y_n-\bar y_{n-1}\). Division by the positive integer \(n\) proves the identity. \(\square\)

This identity explains why a running mean often changes less near the end of a run: the same-sized deviation is divided by a larger \(n\). Small late changes are therefore not, by themselves, strong evidence of stability. The new observations may simply be receiving less weight.

Result (bound on the empirical autocorrelation): When the empirical autocorrelation is defined as above, \(|r_k|\le1\) for every \(0\le k<N\).

Proof. Put \(z_i=y_i-\bar y_N\) and \(D=\sum_{i=1}^N z_i^2\), so \(D>0\). For \(1\le k<N\), the numerator is \(\sum_{i=1}^{N-k}z_i z_{i+k}\). By the Cauchy–Schwarz inequality, its absolute value is at most \(\sqrt{(\sum_{i=1}^{N-k}z_i^2)(\sum_{i=1}^{N-k}z_{i+k}^2)}\). Each sum inside the square root is at most \(D\), since it includes only some of the nonnegative terms in \(D\). The numerator’s absolute value is therefore at most \(D\), giving \(|r_k|\le1\) after division by \(D\). At \(k=0\), the numerator equals \(D\), so \(r_0=1\). \(\square\)

Worked Examples

Worked Example: A Trace and Running Mean That Keep Rising

Consider the observed scalar sequence \(y_1,\ldots,y_8=(1,2,3,4,5,6,7,8)\). Read its trace and compute the running means. This is an illustrative finite record, not a claim that these values form a typical MCMC run.

The trace rises by one at every iteration, so it displays a sustained upward pattern over the full record. The sums through iterations \(1\) to \(8\) are, respectively, \(1,3,6,10,15,21,28,36\). Dividing each sum by its iteration count gives

$$ \bar y_1=1,\quad \bar y_2=\tfrac32,\quad \bar y_3=2,\quad \bar y_4=\tfrac52,\quad \bar y_5=3,\quad \bar y_6=\tfrac72,\quad \bar y_7=4,\quad \bar y_8=\tfrac92. $$

Both views show that the sequence has not settled to a constant level over the displayed interval. The update identity checks, for example, at \(n=8\): \((y_8-\bar y_7)/8=(8-4)/8=\tfrac12\), and \(\bar y_8-\bar y_7=\tfrac92-4=\tfrac12\). A rising trace from a real run warrants investigation, but a finite plot alone cannot determine whether the pattern is transient or reflects a problem with the chain.

Worked Example: A Long Block and Positive Lag-One Dependence

Take \(y_1,\ldots,y_8=(0,0,0,0,1,1,1,1)\). The trace has two long blocks. Compute its overall mean and empirical lag-one autocorrelation using the stated convention.

The sum is \(4\), so \(\bar y_8=4/8=1/2\). The centered values are \(-1/2,-1/2,-1/2,-1/2,1/2,1/2,1/2,1/2\). The denominator in \(r_1\) is \(8(1/2)^2=2\). In the lag-one numerator, three adjacent pairs within the first block each contribute \(1/4\), the pair at the block boundary contributes \(-1/4\), and three pairs within the second block each contribute \(1/4\). Therefore the numerator is \(3/4-1/4+3/4=5/4\), and

$$ r_1=\frac{5/4}{2}=\frac58. $$

The running means are \(0,0,0,0,1/5,1/3,3/7,1/2\): after the four zeros, the cumulative sums are \(1,2,3,4\), divided by \(5,6,7,8\), respectively. The late mean approaches \(1/2\), while the trace makes a single transition between its blocks. This example shows how a positive lag-one value and a blocky trace can both flag persistence. It does not show that every persistent MCMC output has the same pattern or that \(r_1\) alone measures all its dependence.

Check: The numerator \(5/4\) divided by the denominator \(2\) gives \(5/8\), which lies between \(-1\) and \(1\), as the bound requires.

Worked Example: A Flat Trace and an Undefined Autocorrelation

Suppose the six recorded values are \(y_1,\ldots,y_6=(2,2,2,2,2,2)\). Find the running means and determine whether the empirical autocorrelation is defined.

The total after \(n\) observations is \(2n\), so \(\bar y_n=2n/n=2\) for every \(n=1,\ldots,6\). The overall mean is also \(2\). Thus every centered value \(y_i-\bar y_6\) equals \(0\), and the denominator of the empirical autocorrelation is \(\sum_{i=1}^6 0^2=0\). The empirical autocorrelation is undefined: its formula would require division by zero.

A flat record is compatible with different explanations. The target quantity might genuinely be constant, or the recorded chain might not have moved during this interval. The plots cannot distinguish those possibilities from the six values alone. In particular, an undefined autocorrelation is not evidence of independence, and a constant running mean is not a certificate of adequate sampling.

Limitations and a Practical Reading Order

These plots are exploratory diagnostics, not convergence tests. They describe the selected output and selected function; they do not prove that the chain reached the intended distribution, that its long-run average equals the target expectation, or that other coordinates and functions behave similarly. Even a weakly stationary process can produce a finite segment with an apparent trend by chance, while an apparently stable segment can conceal slow movement on a longer time scale. The population autocorrelation definition requires stationarity and finite positive variance; the empirical formula can be calculated without assuming stationarity, but then it is only a summary of that finite sequence.

A useful reading order is to inspect the trace first for visible shifts, plateaus, or unusual movement; inspect the running mean next for changes in the cumulative estimate; then inspect autocorrelation over several lags for persistence or alternation. Return to the trace when an autocorrelation pattern seems important: the time-ordered record can show whether that summary corresponds to blocks, oscillation, or another structure. Repeat the inspection for relevant functions of the state, not only the most convenient coordinate.

Common mistake. It is tempting to say, “The running mean is nearly flat, so the chain has converged.” That conclusion does not follow. The update identity gives \(\bar y_n-\bar y_{n-1}=(y_n-\bar y_{n-1})/n\), so division by a large \(n\) can make changes small even when new values still differ substantially from the current mean. A flat trace has a separate ambiguity, as the constant-output example shows. The correction is to treat visual stability as a reason to examine the output further, not as proof; assess trace, cumulative behavior, and dependence together, and keep their limitations explicit.

Check Your Understanding

Use the definitions and conventions above. When computing an empirical autocorrelation, first check that its denominator is positive.

  1. For observations \(2,4,6\), calculate the three running means and use the update identity to check the change at \(n=3\).
  2. What does a sustained upward pattern in a trace suggest, and why does it not by itself prove that the target distribution has not been reached?
  3. For a weakly stationary process, what additional condition is needed for the stated population autocorrelation to be defined?
  4. Why is the empirical autocorrelation undefined for a record in which all observed values are equal?
  5. Give one reason a nearly flat tail of running means may be misleading.