Foundations to Frontier
Tests
K. Time Series and Sensor Data › K1 Classical Time Series

Autocorrelation and partial autocorrelation

The autocorrelation function shows how strongly a series is correlated with its own past, and the partial autocorrelation shows what each lag adds once the shorter lags are accounted for. Their cut-off patterns are the classic way to choose between autoregressive and moving-average models. This page computes both functions from scratch with the Durbin–Levinson recursion, automates the Box–Jenkins identification rule, measures how often it picks the right model, and tests residuals for remaining correlation with the Ljung–Box test.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Does today remember yesterday?

If today is warm, tomorrow is probably warm too: temperatures remember their recent past. Autocorrelation measures that memory. It compares a series with a copy of itself shifted by one day, two days, and so on. A quickly fading memory looks different from a memory that switches off after one day, and these shapes tell us which kind of model fits the data.

Time-series lab
Bars: ACF (left) and PACF (right) at lags 1–15; dashed lines ±1.96/√n = ±0.139. An AR(1) has a geometrically decaying ACF and a PACF that cuts off after lag 1. About one lag in twenty crosses the band by chance.
Why engineers care

Box and Jenkins built their 1970 forecasting method around reading these correlation plots.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Autocorrelation compares a series with…
  2. 2. Daily temperatures usually have…
  3. 3. The shape of the correlations helps us choose…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • ACF: r_k = Σ(y_t − ȳ)(y_(t+k) − ȳ) / Σ(y_t − ȳ)², the correlation between the series and itself k steps later.
  • PACF: φ_kk, the last coefficient of the best AR(k) fit, is the correlation at lag k after removing the linear effect of lags 1 to k−1.
  • AR(p): the ACF decays gradually (geometrically or as a damped wave), and the PACF cuts off after lag p.
  • MA(q): the ACF cuts off after lag q, and the PACF decays gradually.
  • White-noise band: sample autocorrelations of white noise are roughly N(0, 1/n), so ±1.96/√n marks a 95% band.
  • Ljung–Box test: Q = n(n+2) Σ r_k²/(n−k) over h lags tests whether residuals are white noise (approximately χ² with h degrees of freedom, minus the number of fitted parameters).
📝 Quick check (Level 1)
0/3 answered
  1. 1. The PACF of an AR(2) cuts off after lag…
  2. 2. The ACF of an MA(1) is zero beyond lag…
  3. 3. The 95% white-noise band for n = 400 is about…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

ACF and PACF from scratch

Exercise 1.

  • 1000 points of an AR(2) with φ = (0.6, −0.3) and of an MA(1) with θ = 0.7.
  • The sample ACF from its definition.
  • The PACF from the Durbin–Levinson recursion, which builds AR(k) coefficients from AR(k−1).
  • Comparison with theory and with the band ±1.96/√1000 = ±0.062.

Practice.

  • Look at both plots after making the series stationary, since trends make the ACF decay very slowly.
  • Expect chance spikes: about one lag in twenty crosses the 95% band for white noise.
  • Use identification as a starting point, then compare candidate models with AIC and residual diagnostics.
  • Check residuals with the ACF and the Ljung–Box test after fitting.
  • Tools: statsmodels plot_acf, plot_pacf and acorr_ljungbox; pandas autocorr.
import numpy as np

def pacf(r, nlags):
    out, phi = [1.0], np.zeros(0)
    for k in range(1, nlags + 1):
        a = (r[k] - phi @ r[1:k][::-1]) / (1 - phi @ r[1:k])
        phi = np.r_[phi - a * phi[::-1], a]
        out.append(a)
    return np.array(out)
🐍 Exercise 1: correlation signatures
Simulate an AR(2) and an MA(1), compute their ACF and PACF, and check that each cuts off where theory says.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A very slowly decaying ACF usually means…
  2. 2. statsmodels function for the Ljung–Box test:
  3. 3. Final model choice should also use…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the cut-offs happen. In an MA(q), y_t and y_(t+k) share no shocks once k exceeds q, so the autocorrelation is exactly zero beyond q. In an AR(p), y_t depends on its own past without limit, so the ACF decays. But once lags 1 to p are known, earlier lags add nothing, so the PACF is zero beyond p.

Durbin–Levinson. With φ_(k−1) the AR(k−1) coefficients, the next partial autocorrelation is a = (r_k − Σ φ_(k−1,j) r_(k−j)) / (1 − Σ φ_(k−1,j) r_j), and the coefficients update as φ_(k,j) = φ_(k−1,j) − a·φ_(k−1,k−j).

In Exercise 1:

  • the AR(2) PACF was 0.46, −0.27, 0.03 (theory 0.46, −0.3, 0);
  • the MA(1) ACF was 0.44, −0.04, −0.03 (theory 0.47, 0, 0);
  • the MA(1) PACF decayed with alternating sign: 0.44, −0.3, 0.18.

Multiple looks. Each of K lags has about a 5% chance of a spurious spike, so white noise shows at least one spike in 1 − 0.95^K of cases: 23% for K = 5. A rule that reads “the last significant lag” therefore makes many mistakes even with unlimited data. The Ljung–Box test combines lags into a single statistic with a controlled error rate.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Beyond lag q the ACF of an MA(q) is zero because…
  2. 2. Chance that white noise shows a spike among 5 lags at the 5% level:
  3. 3. Ljung–Box combines lags to…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How often does the textbook ACF/PACF cut-off rule identify the right model, how does that change with series length, and how well does the Ljung–Box test detect weak correlation?

History.

  • G. Yule (1927) and G. Walker (1931) introduced autoregressive models and the equations linking them to autocorrelations.
  • N. Levinson (1947) and J. Durbin (1960) gave the fast recursion.
  • G. Box and G. Jenkins (1970) made ACF and PACF identification the first step of ARIMA modelling.
  • G. Ljung and G. Box (1978) proposed the portmanteau test used today.

Exercise 2.

  • 100 series from each of white noise, AR(1) with φ = 0.7, AR(2) with φ = (0.6, −0.3) and MA(1) with θ = 0.7, at lengths 100, 400 and 1600.
  • The rule: p = last PACF lag (of 5) outside ±1.96/√n, q = last ACF lag outside it; pick the simpler of AR(p) and MA(q); white noise if neither has a spike.
  • The Ljung–Box test (h = 10, 5%) on 400 white-noise series and 400 AR(1) series with φ = 0.2.

Seed 0, share identified correctly:

n white noise AR(1) AR(2) MA(1)
100 0.75 0.91 0.4 0.73
400 0.75 0.85 0.71 0.68
1600 0.72 0.76 0.83 0.69
Ljung–Box rejection white noise AR(1), φ = 0.2
n = 100 0.075 0.2
n = 400 0.052 0.78
  • More data helped only where a real effect was hard to see: AR(2) rose from 0.4 to 0.83, because its weak lag-2 coefficient needs many points.
  • White noise stayed near 75% at every length, as the 23% chance of a spurious spike among 5 lags predicts. AR(1) and MA(1) even fell slightly, since spurious spikes at higher lags overrode the true cut-off.
  • Ljung–Box kept its size near 5% and found φ = 0.2 in 20% of series of length 100 but 78% of length 400.
  • Seeds 1 and 2: AR(2) at n = 100 was 0.4 and 0.31, and at 1600 0.92 and 0.88; white noise ranged from 0.75 to 0.88; Ljung–Box power at n = 400 was 0.78 and 0.79.
🐍 Exercise 2 (research): how often does the textbook ACF/PACF cut-off rule identify the right model, how does that change with series length, and how well does the Ljung–Box test detect weak correlation?
Measure how often an automated ACF/PACF cut-off rule identifies white noise, AR(1), AR(2) and MA(1) at three series lengths, and the size and power of the Ljung–Box test.

Identification in practice

  • Automatic order selection by information criteria (auto.arima, pmdarima) has largely replaced reading plots, but the plots remain the best diagnostic.
  • Seasonal patterns show up as spikes at multiples of the season length in the ACF and PACF.
  • Residual checks after fitting any model (statistical or neural) should include the ACF of the residuals and a portmanteau test.
  • Long memory: ACFs that decay like a power law rather than geometrically suggest fractional differencing (ARFIMA).

References

  1. G. E. P. Box and G. M. Jenkins (1970). Time Series Analysis: Forecasting and Control. Holden-Day.
  2. J. Durbin (1960). “The fitting of time-series models.” Revue de l’Institut International de Statistique.
  3. G. M. Ljung and G. E. P. Box (1978). “On a measure of lack of fit in time series models.” Biometrika.
  4. P. J. Brockwell and R. A. Davis (2016). Introduction to Time Series and Forecasting, 3rd ed. Springer.
  5. R. H. Shumway and D. S. Stoffer (2017). Time Series Analysis and Its Applications, 4th ed. Springer.