Cross correlation function: find the lag between two series
The cross correlation function (CCF) measures the correlation between two time series at different time shifts (lags). It answers questions such as "does a change in booth humidity show up in coating weight, and how many samples later?". The lag with the strongest correlation estimates the delay between the two series, and its sign tells you whether they move together or in opposite directions. An ordinary correlation looks only at lag 0 and can miss a strong relationship that takes time to appear.
When to use it
- You suspect an input (temperature, humidity, feed rate) affects an output, but with a transport or process delay.
- You want to know which of two series leads the other.
- You are building a model with a lagged input, such as a transfer function or a regression on a lagged predictor.
- You want to align two logs recorded at different points in a line before comparing them.
How it works
For series x and y of length n, with means x̄ and ȳ and standard deviations sₓ and s_y (computed with n in the denominator), the cross correlation at lag k is:
rₓᵧ(k) = c(k) / (sₓ · s_y)
c(k) = (1/n) · Σₜ (xₜ − x̄)(yₜ₊ₖ − ȳ)In this article a positive k pairs x at time t with y at time t + k, so a large value at k = 2 means x leads y by two periods. Negative k means y leads x. Conventions differ between textbooks and programs, so check which series leads at positive lags before interpreting the result.
For two unrelated random series, each rₓᵧ(k) falls within about ±2/√n. That bound assumes that at least one of the series is not autocorrelated. If both are, spurious cross correlations appear, and the peak spreads to the neighboring lags. The fix is prewhitening: fit a time series model to x (often an AR model), apply the same filter to y, and compute the CCF of the two filtered series.
Example
A paint line logs booth relative humidity (%) and the coating weight (g/m²) on test panels every 10 minutes. The panels are weighed after flash-off and cure, roughly 20 minutes after they pass through the booth. Forty readings of each:
| Readings | Humidity (%) | Coating weight (g/m²) |
|---|---|---|
| 1–10 | 56, 57, 54, 54, 53, 56, 56, 54, 52, 53 | 41.3, 41.5, 41.1, 40.6, 42.5, 42.1, 42.8, 42.1, 40.7, 42.5 |
| 11–20 | 54, 54, 58, 59, 49, 47, 51, 52, 55, 55 | 43.8, 42.5, 41.9, 42.8, 41.1, 41.1, 43.9, 43.9, 43.3, 42.8 |
| 21–30 | 62, 54, 54, 61, 59, 59, 55, 50, 54, 55 | 42.5, 42.1, 38.6, 41.6, 42.9, 40.3, 40.2, 40.6, 42.3, 44.0 |
| 31–40 | 51, 51, 53, 52, 53, 55, 55, 53, 56, 58 | 42.9, 42.2, 43.3, 43.2, 43.3, 41.7, 42.6, 42.0, 41.1, 42.9 |
The ordinary correlation at lag 0 is 0.015: no visible relationship. The CCF tells a different story:
The bound is ±2/√40 = ±0.316. The peak is at lag +2, r = −0.875: humidity leads coating weight by two readings (20 minutes), matching the cure time, and higher humidity goes with lower coating weight. Lag 3 (−0.394) is also beyond the bound and lag 1 (−0.287) is close to it.
Those side lags come partly from the autocorrelation of the humidity series, whose lag-1 autocorrelation is 0.39. Prewhitening checks this: an AR(1) model for humidity, xₜ = 32.51 + 0.403·xₜ₋₁, filters both series, and the CCF of the filtered series is −0.841 at lag 2 but only 0.027 at lag 1 and −0.148 at lag 3 (bound ±0.320 with 39 points). The relationship is a single delay of two readings.
Regressing coating weight on humidity two readings earlier gives a slope of −0.345 g/m² per percentage point (r = −0.901). The team adds the humidity reading to the booth display with a note that the coating weight responds 20 minutes later, and plans a test of a humidity setpoint to confirm the effect.
How to read the results
- The lag of the peak estimates the delay. Check that it makes physical sense, such as a transport or cure time.
- The sign of the peak says whether the series move together (positive) or opposite (negative).
- Values at negative lags would mean the output leads the input, which usually points to feedback, such as an operator adjusting the input after seeing the output.
- A broad peak spread over several lags often means one or both series are autocorrelated; prewhiten before concluding that the effect is spread out.
Common mistakes
- Using only the lag-0 correlation. As the example shows, a strong delayed effect can have a lag-0 correlation near zero.
- Correlating two trending series. Two series that both rise over time show high cross correlation at every lag; detrend or difference them first.
- Skipping prewhitening with autocorrelated series. Spurious peaks appear, and the delay can look longer than it is.
- Reading cause and effect from a correlation. A CCF peak shows timing, not proof. Confirm with an experiment that changes the input.
Frequently asked questions
Which series should be x and which y?
Put the suspected input as x and the output as y. Then a peak at a positive lag means the input leads, as expected.
Why is the CCF not symmetric like the ACF?
The ACF of a series at lag k equals its value at lag −k. For two different series, x leading y is not the same as y leading x, so the two sides differ.
How many lags should I look at?
Enough to cover the longest plausible delay, and usually no more than about n/4. With 40 readings here, lags −6 to +6 cover an hour each way.
What do I do with the delay once I know it?
Use the lagged input in a regression or a transfer function model, align the data before a scatterplot, or move the control point upstream so corrections act earlier.