Autocorrelation, Explained
After reading this you will be able to compute an autocorrelation function by hand, read the white-noise bands, and use the Ljung-Box test to decide whether a series carries real serial dependence.
What autocorrelation measures
Autocorrelation is the correlation of a series with a delayed copy of itself. Shift the series forward by one step and correlate it against the original: that gives the lag-1 autocorrelation, written r_1. Shift by two steps for r_2, and so on. The full set of these values across lags is the autocorrelation function, or ACF.
The idea answers a plain question: does what happened recently tell you anything about what happens next? For a monthly sales figure that trends upward, this month resembles last month, so r_1 is large and positive, maybe 0.8. For a coin-flip sequence, each value is independent of the last, so every r_k hovers near 0.
The classic hook is seasonality. Take 24 monthly airline-style figures that peak every summer. The ACF will not just decay: it will bulge back upward around lag 12 because each month resembles the same month one year earlier. That spike at lag 12 is the fingerprint of a yearly cycle.
When to use it, and when not
Reach for the ACF whenever your data has a natural time order and you want to know if the order matters. Three common jobs:
- Checking whether residuals from a regression or forecast are white noise. If they are, you have extracted all the predictable structure.
- Detecting seasonality or repeating cycles before you fit a model.
- Measuring persistence: how long a shock echoes through the series.
Do not use the ACF on data with no meaningful order. Correlating a shuffled list of customer ages against itself produces numbers, but they mean nothing. The tool also assumes the series is roughly stationary: constant mean and variance over time. A strong trend violates that, and you will see a slowly decaying ACF that reflects the trend rather than any cycle. Difference the series first (subtract each value from the next) if you only care about the cyclical part.
An ACF full of large positive values is not evidence of a "strong signal" you can exploit. On trending data it is an artifact of the trend. Confirm stationarity before you interpret any lag.
The formula and the intuition
For a series x_1, x_2, \ldots, x_n with mean \bar{x}, the sample autocorrelation at lag k is:
The numerator sums the products of paired deviations: each value's gap from the mean times the gap of the value k steps later. When highs line up with highs, those products are positive and r_k is positive. The denominator is the total squared deviation, which normalizes the result so that r_0 = 1 always and every r_k falls between -1 and +1.
Notice the numerator has n-k terms while the denominator has n. This is deliberate. It shrinks estimates at large lags, where you have few pairs to work with, and keeps the ACF from blowing up when the data runs thin.
Under the null hypothesis that the series is white noise, each r_k is approximately Normal with standard error 1/\sqrt{n}. That gives the band drawn on the plot: \pm 1.96/\sqrt{n}. Any bar poking outside it is significant at the 5% level.
Worked example with the demo data
ACF of 24 monthly values
Load the demo series (24 values, oldest first):
112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118, 115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, 140
- The mean is \bar{x} = 131.5. Subtract it from every value to get deviations:
-19.5, -13.5, 0.5, -2.5, ... - The denominator, the sum of squared deviations, is \sum (x_t - \bar{x})^2 = 8087.
- For lag 1, pair each deviation with the next and sum the products: \sum_{t=1}^{23}(x_t-\bar{x})(x_{t+1}-\bar{x}) = 5960. So r_1 = 5960 / 8087 \approx 0.737.
- For lag 12, the products of values one year apart sum to about
2320, giving r_{12} = 2320 / 8087 \approx 0.287. - The white-noise band is \pm 1.96/\sqrt{24} \approx \pm 0.400. Only r_1 clears it decisively; r_2 \approx 0.34 sits just inside.
The picture is a series with strong month-to-month persistence and a hint of a yearly echo, exactly what you expect from data built around a rising level and a seasonal wobble.
The Ljung-Box test
Eyeballing bars against a band has a problem: with 10 lags and a 5% band, you expect about 0.5 false alarms even from pure white noise. The Ljung-Box test fixes this by pooling all lags into a single statistic:
Here n is the series length, m is the number of lags tested, and r_k is the autocorrelation at lag k. Under the null hypothesis of white noise, Q follows a chi-squared distribution with m degrees of freedom. A large Q means the squared autocorrelations, taken together, are bigger than chance allows.
For the demo data with m=10 lags, the dominant term is lag 1: 24 \times 26 \times (0.737^2 / 23) \approx 14.7. Adding the smaller lags brings Q to roughly 24. Compared against a chi-squared distribution with 10 degrees of freedom (5% critical value 18.31), that clears the bar, so you reject white noise. The series has real structure.
When you test residuals from a fitted model, subtract the number of estimated parameters from the degrees of freedom. An ARMA(1,1) fit tested at 10 lags uses 10 - 2 = 8 degrees of freedom, which raises the critical value and makes the test stricter.
Reading the results
Match the shape to a diagnosis:
- Single spike at lag 1, rest inside the band
- Short memory. The value depends on the previous one and little else, the signature of an AR(1) process.
- Slow, near-linear decay across many lags
- A trend or a random walk. Difference the series and recompute.
- Bulge at lag s (12 for monthly, 7 for daily)
- Seasonality with period s.
- Everything inside the band, high Ljung-Box p-value
- White noise. There is nothing left to model.
Explore how the band tightens as the series grows:
Common mistakes
Four traps catch people often.
Reading trend as signal. A rising series produces a wall of positive autocorrelations. That is the trend, not a cycle. Difference first if you want the cyclical structure.
Testing too many lags. With n=24, an ACF at lag 20 rests on only 4 pairs and is nearly meaningless. A common rule caps m at n/4 or 10\log_{10}(n). For 24 points that is about 6 to 13 lags.
Ignoring the parameter correction. Running Ljung-Box on model residuals with the wrong degrees of freedom inflates your confidence. Subtract the fitted parameters.
Chasing one lag. With 20 lags, one bar outside a 5% band is expected. Do not build a story around a single borderline spike; let Ljung-Box give the pooled verdict.
Related tools
Autocorrelation is one lens on a series. To describe the values without regard to order, start with the Descriptive Statistics Calculator for the mean, spread and quartiles. A single extreme point can distort an ACF, so screen it first with the Outlier Detector. Autocorrelation is built from lagged covariances, and you can compute plain two-variable covariance and Pearson correlation with the Covariance Calculator. To locate a specific value's standing in the distribution, use the Percentile Calculator.
Frequently asked questions
Why is the lag-0 autocorrelation always 1?
At lag 0 the series is correlated with an unshifted copy of itself, so the numerator equals the denominator and the ratio is exactly 1. The tool usually omits it because it carries no information.
How many data points do I need?
The white-noise band is \pm 1.96/\sqrt{n}, so short series have wide bands. At n=24 the band is ±0.40, meaning an autocorrelation must exceed 0.40 to register. Aim for at least 50 points, and more if you want to detect small effects.
What is the difference between ACF and partial autocorrelation?
The ACF measures total correlation at lag k, including whatever passes through the intervening lags. Partial autocorrelation strips that out, showing the direct link at lag k alone. This tool reports the ACF.
My Ljung-Box p-value is above 0.05. What does that mean?
You fail to reject the white-noise hypothesis. The autocorrelations, taken together, are consistent with pure noise. For residuals that is good news: the model has captured the predictable structure.
Can autocorrelation be negative?
Yes. A negative r_k means highs tend to be followed by lows k steps later. A series that alternates up-down-up-down shows r_1 near -1, the mark of mean reversion or overcorrection.