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

Stationarity, trends and seasonality

Most methods for time series assume that the process does not change its statistical behaviour over time. This page separates a series into trend, seasonal pattern and remainder, shows how differencing makes a series stationary, and demonstrates why it matters: two unrelated random walks look significantly related three times out of four. It then implements the Dickey–Fuller unit-root test and measures how often it gets the answer right.

Level 0: Foundation

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

Patterns that repeat and patterns that drift

Ice-cream sales rise every summer and fall every winter: that is seasonality. Over the years a shop may also sell more and more: that is a trend. What is left after removing both is the day-to-day randomness. A series is called stationary when its average level and its wiggles stay the same over time. Many forecasting tools only work on stationary series, so the first step is often to remove the trend and the season.

Time-series lab
12 years of monthly data (light) and the centred 2×12 moving-average trend (dark). Seasonal means recover an amplitude of 5.88 (true 6.0). The moving average is undefined for the first and last six months.
Why engineers care

Monthly airline passengers, electricity demand and CO₂ at Mauna Loa all combine a trend with a yearly season.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A pattern that repeats every year is called…
  2. 2. A steady rise over many years is…
  3. 3. A stationary series keeps the same…

Level 1: Beginner

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

By hand

  • Weak stationarity: constant mean, constant variance, and an autocovariance that depends only on the lag, not on time.
  • Additive decomposition: y = trend + seasonal + remainder. Use a multiplicative decomposition, or logarithms, when the seasonal swings grow with the level.
  • Trend estimate: a centred moving average over one full season (a 2×12 average for monthly data) cancels the season.
  • Differencing: the first difference y_t − y_(t−1) removes a linear trend or a random-walk level. The seasonal difference y_t − y_(t−s) removes a fixed seasonal pattern.
  • Unit root: a random walk y_t = y_(t−1) + e_t has a variance that grows like t. Shocks never die out, and it is not stationary.
  • Trend-stationary versus difference-stationary: a deterministic trend plus stationary noise should be detrended; a unit-root series should be differenced.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Weak stationarity requires a constant…
  2. 2. A seasonal difference with period 12 is…
  3. 3. The variance of a random walk after t steps grows like…

Level 2: Intermediate

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

Decomposing a monthly series

Exercise 1.

  • 12 years of monthly data: level 100, trend 0.5 per month, a seasonal sine wave of amplitude 10, and white noise with sd 2.
  • Trend: a centred 2×12 moving average.
  • Seasonal: the mean detrended value at each month, centred to sum to zero.
  • Then first and seasonal differences.

Practice.

  • Plot first: the trend, the season and changes in variance are usually visible.
  • Log-transform series whose seasonal swings grow with the level.
  • Prefer robust decompositions (STL) for real data with outliers and slowly changing seasons.
  • Test for unit roots with the right deterministic terms, and combine ADF with KPSS, whose null hypothesis is stationarity.
  • Tools: statsmodels (seasonal_decompose, STL, adfuller, kpss), pandas, and the R forecast package.
import numpy as np

def centered_ma(y, period=12):
    w = np.r_[0.5, np.ones(period - 1), 0.5] / period
    h = period // 2
    out = np.full(len(y), np.nan)
    out[h:len(y) - h] = np.convolve(y, w, mode="valid")
    return out
🐍 Exercise 1: trend, season and remainder
Simulate a monthly series with a trend and a yearly season, decompose it with a centred moving average and seasonal means, and compare first and seasonal differences.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Seasonal swings that grow with the level suggest…
  2. 2. A robust seasonal decomposition:
  3. 3. KPSS has the null hypothesis of…

Level 3: Advanced

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

Theory

What the decomposition recovers. In Exercise 1 the centred moving average gave a trend slope of 0.501 per month (true 0.5). The seasonal means recovered the amplitude of 10 with a largest error of 1.01, and the remainder had sd 1.65 (the noise sd was 2; the averaging absorbs part of it). First differences still had sd 4.5 because the season remained. Seasonal differences removed the season and turned the trend into a constant 12 × 0.5 = 6 (measured 6.1).

Why non-stationarity is dangerous. If x and y are independent random walks, the usual t-test of a regression of y on x assumes independent errors. The residuals are themselves a random walk, so the standard error is far too small and “significant” slopes appear most of the time (Granger and Newbold, 1974). Differencing both series restores the usual behaviour.

The Dickey–Fuller test. Write Δy_t = c + γ y_(t−1) + e_t. A unit root means γ = 0. Under that null the t-statistic of γ does not follow a t-distribution. Its 5% critical value is −2.86 with a constant and −3.41 when a trend term is included. The augmented version (ADF) adds lagged differences to absorb autocorrelation. Close to φ = 1, a stationary AR(1) looks almost like a random walk, so the test has little power in short samples.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Regressing one random walk on another gives…
  2. 2. The 5% Dickey–Fuller critical value with a constant is about…
  3. 3. The test has low power when φ is…

Level 4: Expert

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

How often does a regression of independent random walks look significant, and how reliably does the Dickey–Fuller test separate unit roots from stationary series?

History.

  • W. Persons (1919) and F. Macaulay (1931) decomposed economic series into trend, cycle and season; Census X-11 (1965) automated it.
  • G. Yule (1926) warned of “nonsense correlations” between time series; C. Granger and P. Newbold (1974) showed spurious regression by simulation.
  • D. Dickey and W. Fuller (1979) derived the unit-root test, and D. Kwiatkowski et al. (1992) the KPSS test.
  • R. Cleveland et al. (1990) introduced STL.

Exercise 2.

  • 400 regressions of one independent random walk on another (n = 100), in levels and in first differences.
  • The Dickey–Fuller test with a constant, at the 5% level, on 400 random walks and 400 AR(1) series with φ = 0.95, 0.9 and 0.8, at n = 100 and n = 400.
  • 400 trend-stationary series (trend 0.05 per step plus AR(1) noise with φ = 0.7, n = 200), tested without and with the trend term.

Seed 0:

regression of independent series share with |t| above 1.96 median R²
random walks in levels 0.75 0.18
after differencing 0.052 0.0049
Dickey–Fuller rejection rate random walk φ = 0.95 φ = 0.9 φ = 0.8
n = 100 0.048 0.17 0.39 0.9
n = 400 0.043 0.84 1.0 1.0
  • Three regressions in four found a “significant” link between unrelated random walks. After differencing, the rate fell to the nominal 5%.
  • The test held its size: it rejected true unit roots about 5% of the time.
  • With 100 points it detected φ = 0.95 only 17% of the time; with 400 points, 84%.
  • The trend-stationary series was declared stationary only 7.7% of the time without the trend term, and every time (1.0) with it.
  • Seeds 1 and 2: levels 0.75 in both; after differencing 0.045 and 0.048; size at n = 100 0.068 and 0.055; power at φ = 0.95, n = 400, 0.89 and 0.86.
🐍 Exercise 2 (research): how often does a regression of independent random walks look significant, and how reliably does the Dickey–Fuller test separate unit roots from stationary series?
Regress independent random walks on each other in levels and differences, then measure the size and power of the Dickey–Fuller test on random walks, AR(1) series and trend-stationary series.

Stationarity in practice

  • Workflow: plot; transform (log or Box–Cox); decompose (STL); test (ADF and KPSS); difference or detrend; then model the remainder.
  • Cointegration: two non-stationary series can share a common random walk, so a combination of them is stationary (Engle and Granger, 1987). Then regressing in levels is meaningful.
  • Structural breaks can make a stationary series look like a unit root; tests such as Zivot–Andrews allow for one break.
  • Machine learning: most ML forecasters also need normalisation, such as differencing or per-window scaling (RevIN), to cope with level shifts.

References

  1. C. W. J. Granger and P. Newbold (1974). “Spurious regressions in econometrics.” Journal of Econometrics.
  2. D. A. Dickey and W. A. Fuller (1979). “Distribution of the estimators for autoregressive time series with a unit root.” Journal of the American Statistical Association.
  3. D. Kwiatkowski, P. C. B. Phillips, P. Schmidt and Y. Shin (1992). “Testing the null hypothesis of stationarity against the alternative of a unit root.” Journal of Econometrics.
  4. R. B. Cleveland, W. S. Cleveland, J. E. McRae and I. Terpenning (1990). “STL: a seasonal-trend decomposition procedure based on loess.” Journal of Official Statistics.
  5. R. J. Hyndman and G. Athanasopoulos (2021). Forecasting: Principles and Practice, 3rd ed. OTexts.