Autocorrelation
Today usually looks like yesterday, and some days repeat on a weekly rhythm. Autocorrelation turns both hunches into one number per lag.
Why Does This Exist?
You run a small cafe and need to know how many cups you will pour tomorrow. Yesterday's sales are a strong hint, and last Saturday's rush is a strong hint too. But which hint do you trust, and by how much? Guess wrong and you either dump fresh milk or run dry at noon.
Treating each day as independent misses both patterns. Shuffling the days would hide the momentum from one day to the next and the spike every seventh day, so any forecast built on shuffled days plans for an average day that never comes.
This page builds the measuring tool: shift the series, correlate it with itself, and read the ACF and PACF plots. The model that consumes these numbers lives in ARIMA. Splitting a series into trend, season, and rest is the job of time-series decomposition. Checking that the series is stable enough for any of this belongs to stationarity. One prerequisite, briefly: ordinary correlation compares two different columns, while here both columns are the same series shifted in time. If correlation itself feels shaky, skim correlation vs causation first.
Think of It Like This
Shouts in a canyon
You shout once in a canyon and listen. An echo arriving one second later comes back loud, so a wall stands close. Another loud echo arrives seven seconds later, so a second, farther wall stands exactly there. You never see the walls. The delays with loud echoes map them.
Lag is the delay. Correlation is the loudness. The ACF is you testing every delay and writing down each loudness. A loud early echo means momentum, since neighbours resemble each other. A loud late echo at exactly 7 means a weekly wall, since the same weekday repeats.
Where it stops: echoes never say why a wall stands there. Correlation names no cause, and if the walls shift while you listen, because the series drifts, yesterday's echo map misleads you. That drift is why stationarity comes first in practice.
How It Actually Works
Autocorrelation at lag , written and read "rho-k", is the Pearson correlation between the series and itself shifted by steps. Here means the value at time , means the mean, and means how many steps you shift. Values run from to . Lag is always exactly , since anything correlates perfectly with itself unshifted.
How one lag number is built
Take lag . Pair each day with the day before it: a today list and a yesterday list. Correlate the two lists with the ordinary Pearson recipe: deviations from each mean, products averaged against the spreads. The two means differ slightly because the lists start and end on different days, and that is expected, not an error.
High positive means momentum: up days follow up days. Near zero means no memory: today ignores yesterday. Negative means zigzag: up days follow down days.
What the ACF and PACF add
The ACF draws one bar per lag, , and so on. Two shapes tell you almost everything. Bars that start near and decay slowly signal drift, since a trending series resembles its past at every lag. A spike at one fixed lag signals a repeating season, like Saturday resembling Saturday. Exponential smoothing is the forecasting cousin that leans on this same momentum without naming it.
The PACF answers a stricter question. If today tracks yesterday and yesterday tracks the day before, today will seem to track the day before through the middle day alone. The PACF strips out those middle echoes and keeps only the direct link. So you read the PACF to choose the autoregressive order and the ACF to choose the moving-average order when you configure ARIMA.
Worked example
Seven days of cups sold: . You will correlate today against yesterday, step by step.
Split into pairs. Today holds and yesterday holds . Today's mean is and yesterday's mean is .
Take deviations from each mean. Today deviates by and yesterday by . Multiply each pair: , which sums to .
Square and sum each side. Today's squared deviations sum to and yesterday's to . The denominator is .
Divide:
That is the height of the lag-1 bar in the picture. Yesterday predicts today strongly in this week. The same recipe at lag gives . And when the cafe's full 28-day record in Code lines up each day with the same weekday, lag reads : the seasonal spike beside the momentum bars.
Code
Plain Python, no extra libraries. It repeats the hand calculation, then runs the 28-day weekday check:
from __future__ import annotations
def pearson(xs: list[float], ys: list[float]) -> float: mx: float = sum(xs) / len(xs) my: float = sum(ys) / len(ys) num: float = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) den: float = (sum((x - mx) ** 2 for x in xs) * sum((y - my) ** 2 for y in ys)) ** 0.5 return num / den
week: list[float] = [5.0, 7.0, 9.0, 11.0, 10.0, 12.0, 14.0]lag1: float = pearson(week[1:], week[:-1])lag2: float = pearson(week[2:], week[:-2])
month: list[float] = [ cups for days in ( [12.0, 15.0, 17.0, 18.0, 17.0, 15.0, 12.0], [13.0, 16.0, 17.0, 19.0, 18.0, 16.0, 13.0], [14.0, 16.0, 19.0, 20.0, 18.0, 17.0, 14.0], [15.0, 18.0, 20.0, 21.0, 20.0, 18.0, 15.0], ) for cups in days]lag7: float = pearson(month[7:], month[:-7])
print(f"lag-1 {lag1:.2f}") # -> lag-1 0.88print(f"lag-2 {lag2:.2f}") # -> lag-2 0.73print(f"lag-7 {lag7:.2f}") # -> lag-7 0.97The printout matches the hand arithmetic above: at lag , at lag , and when each of the 28 days meets the same weekday a week earlier.
Watch Out For
A trend makes every lag look meaningful
A drifting series resembles its past at all lags, so the ACF starts near and sags slowly instead of dropping. Our sits on top of an upward climb from to , which is momentum plus drift blended together.
Symptom: every lag bar looks significant and no single lag stands out. Fix: difference the series or confirm it is stable on stationarity before you let any lag pick model orders.
Long lags on short series lie
A lag- correlation on days uses only pairs. Ask for lag on a -day snippet and zero pairs remain, yet software will still print a number, often near from two or three surviving pairs.
Symptom: a huge spike at a long lag that vanishes the moment a second week of data arrives. Fix: keep the longest lag under a quarter of the length, and distrust bars inside the chance band around , where means the day count. Our earns trust because pairs back it, never the -day snippet.
The Quick Version
- Autocorrelation is the correlation of a series with itself shifted by steps, written , running from to with lag always exactly .
- Seven cafe days give lag-1 correlation , which means yesterday predicts today strongly in that week.
- An ACF plot draws one bar per lag, where slow decay signals drift and a spike at a fixed lag signals a repeating season, like at lag across days.
- PACF strips out the middle echoes, so the ACF guides the moving-average order and the PACF guides the autoregressive order when you configure ARIMA.
- Trends inflate every lag and short series corrupt long lags, so check stationarity and keep the longest lag under a quarter of the length.