Stationarity and differencing

Why many forecasting models assume a stable series, and how differencing and transforms get you there.

Lesson 53 of 6610 minAdvanced

You will learn to

  • Define stationarity and tell a stationary series from a trending one
  • Use differencing and log transforms to stabilise a series
  • Read a unit-root test result

Before this lesson

Intuition

Imagine you are asked to describe "typical" weather in a city. That only makes sense if the climate is stable: the average and the spread this decade look like the ones last decade. If the average keeps climbing, "typical" depends on which year you ask about.

A stationary time series is one whose statistical behaviour does not depend on when you look. Its average does not drift, its spread does not grow, and the way a value relates to earlier values stays the same. Many classical forecasting models, ARIMA above all, are built for such series: they learn the stable relations and project them forward. A series with a trend or a growing swing breaks their assumptions, so we first transform it into something stable, forecast that, and undo the transform at the end.

Two tools do most of the work. Differencing replaces each value with its change from the previous one, which removes a trend. A log transform turns percentage growth into steady additive growth, which stabilises a growing swing.

Picture it

On the left, the level drifts upward, so the average depends on the time. On the right, the changes from one step to the next hover around a constant value.

Original: the level drifts upwardTime→After differencing: a steady levelConstant meanTime→
A trending series and its first differences (illustrative data). The differenced series has a steady level.

The math

A series yty_t is weakly stationary when three things hold for every tt and every lag hh:

E[yt]=μ,Var⁡(yt)=σ2,Cov⁡(yt,yt+h)=γ(h)\mathbb{E}[y_t] = \mu, \qquad \operatorname{Var}(y_t) = \sigma^2, \qquad \operatorname{Cov}(y_t, y_{t+h}) = \gamma(h)

The mean μ\mu and variance σ2\sigma^2 are constants, and the covariance depends only on the lag hh, not on the time tt.

First difference and seasonal difference with period mm:

Δyt=yt−yt−1,Δmyt=yt−yt−m\Delta y_t = y_t - y_{t-1}, \qquad \Delta_m y_t = y_t - y_{t-m}

The classic non-stationary series is the random walk, yt=yt−1+εty_t = y_{t-1} + \varepsilon_t with εt\varepsilon_t a random shock. Its first difference is just εt\varepsilon_t, which is stationary. We say the walk is integrated of order 1; the "I" in ARIMA stands for this.

A unit-root test, such as the Augmented Dickey-Fuller (ADF) test, asks whether a series behaves like a random walk. Its null hypothesis is "the series has a unit root" (non-stationary). A small p-value, conventionally below 0.05, rejects that null and supports stationarity. A large p-value does not prove non-stationarity; it only says the data gives no evidence against it. A second test, KPSS, reverses the null (the null is stationarity), and the two are often read together.

For growth by a constant factor, the log difference is the useful quantity:

log⁡yt−log⁡yt−1=log⁡ytyt−1\log y_t - \log y_{t-1} = \log\frac{y_t}{y_{t-1}}

Worked example

Differencing a linear trend. Take six monthly values: 10, 12, 15, 17, 20, 22.

  1. The first differences are 2, 3, 2, 3, 2. They vary a little around a mean of 12 / 5 = 2.4.
  2. A model on the differences only has to forecast something near 2.4 per month.
  3. To forecast the next level, add the forecast difference to the last value: 22 + 2.4 = 24.4. This "undoing" step is called integration.

Log for percentage growth. A series grows by 10% per month: 100, 110, 121, 133.1.

  1. The raw differences are 10, 11, 12.1. They keep growing, so the series is still not stable after a plain difference.
  2. The log differences are ln⁡(110/100)=ln⁡(121/110)=ln⁡(133.1/121)=ln⁡1.1≈0.0953\ln(110/100) = \ln(121/110) = \ln(133.1/121) = \ln 1.1 \approx 0.0953 every month. They are constant.
  3. In practice you take the log first, then difference, forecast, and finally exponentiate the forecast back to the original scale.

Reading a test. Suppose an ADF test on the raw series returns p = 0.62, and on its first difference p = 0.01. The first result gives no reason to reject a unit root; the second rejects it. Together they suggest that one difference (d = 1) is enough.

How mlflow does it

  • Target transforms. In a forecasting pipeline you can put a step on the target series. Only value-only, invertible steps are allowed there: Standardise, Scale from 0 to 1, Scale ignoring extremes, Scale by the maximum, Make it bell-shaped and Logarithm. You pick them with Choose... on the series to forecast, and mlflow undoes them on the forecasts for you. The log step is log1p, so it needs values above -1.
  • Differencing. There is no differencing step. The ARIMA-type models (arima and auto_arima) difference inside the model, and auto_arima chooses the number of differences itself, including seasonal ones.
  • Unit-root tests. mlflow does not show an ADF or KPSS result in the app. Use the ACF plot for a visual check: for a trending series the bars fade very slowly instead of dropping quickly. If you want the test itself, run it on the data in a notebook.
  • Other columns. Steps on columns other than the target prepare the exogenous inputs and are separate from the target series.

Common mistakes

  • Differencing a stationary series. Over-differencing adds noise and creates artificial negative correlation. Difference only when the series or a test says it is needed, and usually no more than once or twice.
  • Treating "not rejected" as "proved". A large ADF p-value does not prove a unit root, and a small one does not prove perfect stability. Combine the test with a plot.
  • Forgetting to undo the transform. A forecast of differences or logs is not a forecast of the original values. Use a pipeline that inverts the transform, as the target transforms in mlflow do.
  • Taking a log of zero or negative values. The log is undefined there. Shift the series or use a power transform that handles negatives.
  • Removing seasonality by hand and then adding a seasonal model. The model then looks for a pattern that is no longer there. Do one or the other.

Try it in mlflow

Add a target transform

  1. Open Pipelines and create a pipeline with the task Time series forecasting.
  2. In the Columns part, press Choose... on a numeric column and look at the Scale numbers group.
  3. Press Choose... on the series to forecast and pick Logarithm or Make it bell-shaped.
  4. Train, then compare the run with one that has no transform.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. Which series is stationary in the weak sense?
  2. 2. An ADF (unit-root) test returns a p-value of 0.62. What is a reasonable reading?
  3. 3. Why take the log of a series whose seasonal swing grows with the level?