Skip to content
AI360XpertBeta

Autocorrelation

Today usually looks like yesterday, and some days repeat on a weekly rhythm. Autocorrelation turns both hunches into one number per lag.

Yesterday predicts today with correlation 0.88, while the same weekday repeats with correlation 0.97 at lag 7
Yesterday predicts today with correlation 0.88, while the same weekday repeats with correlation 0.97 at lag 7

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 kk, written ρk\rho_k and read "rho-k", is the Pearson correlation between the series and itself shifted by kk steps. Here xtx_t means the value at time tt, μ\mu means the mean, and kk means how many steps you shift. Values run from −1-1 to 11. Lag 00 is always exactly 11, since anything correlates perfectly with itself unshifted.

How one lag number is built

Take lag 11. 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 ρ1\rho_1 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, ρ1,ρ2,ρ3\rho_1, \rho_2, \rho_3, and so on. Two shapes tell you almost everything. Bars that start near 11 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 pp and the ACF to choose the moving-average order qq when you configure ARIMA.

Worked example

Seven days of cups sold: 5,7,9,11,10,12,145, 7, 9, 11, 10, 12, 14. You will correlate today against yesterday, step by step.

Split into pairs. Today holds [7,9,11,10,12,14][7, 9, 11, 10, 12, 14] and yesterday holds [5,7,9,11,10,12][5, 7, 9, 11, 10, 12]. Today's mean is 63/6=10.563 / 6 = 10.5 and yesterday's mean is 54/6=9.054 / 6 = 9.0.

Take deviations from each mean. Today deviates by [−3.5,−1.5,0.5,−0.5,1.5,3.5][-3.5, -1.5, 0.5, -0.5, 1.5, 3.5] and yesterday by [−4,−2,0,2,1,3][-4, -2, 0, 2, 1, 3]. Multiply each pair: [14.0,3.0,0.0,−1.0,1.5,10.5][14.0, 3.0, 0.0, -1.0, 1.5, 10.5], which sums to 28.028.0.

Square and sum each side. Today's squared deviations sum to 29.529.5 and yesterday's to 34.034.0. The denominator is 29.5×34=1003≈31.67\sqrt{29.5 \times 34} = \sqrt{1003} \approx 31.67.

Divide:

r1=28.031.67≈0.88r_1 = \frac{28.0}{31.67} \approx 0.88

That 0.880.88 is the height of the lag-1 bar in the picture. Yesterday predicts today strongly in this week. The same recipe at lag 22 gives 0.730.73. And when the cafe's full 28-day record in Code lines up each day with the same weekday, lag 77 reads 0.970.97: 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.97

The printout matches the hand arithmetic above: 0.880.88 at lag 11, 0.730.73 at lag 22, and 0.970.97 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 11 and sags slowly instead of dropping. Our 0.880.88 sits on top of an upward climb from 55 to 1414, 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-kk correlation on nn days uses only n−kn - k pairs. Ask for lag 77 on a 77-day snippet and zero pairs remain, yet software will still print a number, often near ±1.00\pm 1.00 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 ±1.96/T\pm 1.96 / \sqrt{T}, where TT means the day count. Our 0.970.97 earns trust because 2121 pairs back it, never the 77-day snippet.

The Quick Version

  • Autocorrelation is the correlation of a series with itself shifted by kk steps, written ρk\rho_k, running from −1-1 to 11 with lag 00 always exactly 11.
  • Seven cafe days give lag-1 correlation 0.880.88, 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 0.970.97 at lag 77 across 2828 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.