Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 40 ยท Stationarity and Transformations

Autocorrelation: ACF and PACF

Understand autocorrelation, the ACF and the PACF: compute them by hand and with statsmodels, read confidence bands, recognise AR, MA, trend and seasonal patterns, and test with Ljung-Box.

Time series are special because neighbouring values are usually related: warm days follow warm days, high sales follow high sales. Autocorrelation measures this relationship between a series and a delayed copy of itself. The autocorrelation function (ACF) and partial autocorrelation function (PACF) are the two main diagnostic tools of time series analysis. They tell you whether a series is stationary, whether it has seasonality, and which ARIMA model is a sensible starting point.

Autocorrelation at lag k

The autocorrelation at lag k is the correlation between y(t) and y(t-k):

r(k) = sum of (y(t) - mean) * (y(t-k) - mean) / sum of (y(t) - mean)^2

It lies between -1 and 1. r(0) is always 1. A value near 1 at lag 1 means "today looks like yesterday".

Python Autocorrelation by hand and with pandas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For an AR(1) process with coefficient 0.8 the theoretical autocorrelations are 0.8, 0.64, 0.51 and so on, geometrically decaying, and the estimates follow that closely. The two computations differ in tiny ways (pandas computes a plain correlation, the ACF formula uses one overall mean) but tell the same story.

The ACF plot and confidence bands

The ACF plot (or correlogram) draws r(k) for lags 1, 2, 3, ... as bars. Dashed lines show approximate 95 percent bounds, +-1.96 / sqrt(n). For white noise about 5 percent of bars poke outside by chance, so one marginal spike among 30 lags is not evidence of structure.

Python statsmodels ACF with confidence limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 10 lags, one bar just outside the band is normal chance (about 5 percent of bars), not structure. To draw the plot yourself use plot_acf and plot_pacf from statsmodels.graphics.tsaplots with matplotlib; the numbers above are exactly what those bars show.

The partial autocorrelation (PACF)

r(2) includes the effect that flows through lag 1: if y(t) relates to y(t-1) and y(t-1) relates to y(t-2), then y(t) and y(t-2) correlate indirectly. The partial autocorrelation at lag k is the correlation between y(t) and y(t-k) after removing the effect of the lags in between. It answers: "does lag k add anything new?"

Python ACF versus PACF for AR(2) and MA(1)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The patterns differ in a way you can read at a glance:

Process ACF PACF
AR(p) decays gradually (geometric or wavy) cuts off after lag p
MA(q) cuts off after lag q decays gradually
ARMA(p, q) decays decays
White noise no significant bars no significant bars

The AR(2) shows a PACF with two clear spikes and then nothing; the MA(1) shows an ACF with a single spike. This is the heart of the Box-Jenkins method covered in choosing ARIMA orders, with the models themselves in AR models and MA models.

Reading the ACF of real-looking series

  • Trend: autocorrelations are large and decay very slowly (still 0.5 after 20 lags). Difference the series (differencing).
  • Seasonality: spikes at multiples of the period (12, 24, 36 for monthly data).
  • Trend plus seasonality: both signs, slow decay with a scalloped shape.
  • Stationary and no structure: bars inside the bands.
Python ACF patterns of trend, season and noise

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trend has big values at every lag, the seasonal series flips sign at lag 6 and peaks again at 12 and 24, and white noise is close to zero everywhere.

Lag plots

A lag plot scatters y(t) against y(t-k). A tight diagonal cloud means strong autocorrelation at that lag, and a round blob means none. pandas.plotting.lag_plot draws it. It is the same information as one ACF bar but shows nonlinear relationships.

Testing for white noise: Ljung-Box

Rather than eyeball many bars, the Ljung-Box test checks whether a group of autocorrelations is jointly zero. A small p-value (below 0.05) means there is leftover structure. It is the standard check on residuals of a fitted model.

Python Ljung-Box on noise and on an AR(1) series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

White noise gives a large p-value (no evidence of autocorrelation), the AR(1) series a p-value essentially zero.

Common mistakes

  • Reading an ACF of a trending series as meaningful structure; always make it stationary first.
  • Treating a single bar slightly outside the band as real.
  • Using too few observations: with fewer than about 50 points the estimates are unreliable.
  • Forgetting that outliers and level shifts distort the ACF.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A PACF with significant spikes at lags 1 and 2 and nothing afterwards suggests:

    The PACF of an AR(p) process cuts off after lag p.
  2. 2. The ACF of a series stays high (above 0.7) for 30 lags and decays very slowly. What does this suggest?

    Very slow decay is the signature of a non-stationary series.
  3. 3. What is the approximate 95% bound for white-noise autocorrelations with n = 400?

    1.96 / sqrt(400) is about 0.098.
  4. 4. A small Ljung-Box p-value on model residuals means:

    The null hypothesis is no autocorrelation; rejecting it signals unmodelled structure.

Summary

  • Autocorrelation is the correlation of a series with its own lagged copy; the ACF plots it for many lags, the PACF removes indirect effects.
  • Bars within about +-1.96/sqrt(n) are consistent with no correlation.
  • AR(p): PACF cuts off at p; MA(q): ACF cuts off at q; slow ACF decay means non-stationarity; spikes at multiples of m mean seasonality.
  • The Ljung-Box test checks many lags at once and is the standard residual check.

Next: Unit Root Tests: ADF and KPSS