Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 40 ยท Stationarity and Transformations

Differencing and Detrending

Make a series stationary with first and seasonal differencing, learn when to difference or detrend, avoid over-differencing, and invert differences to return forecasts to the original scale.

The most common way to remove a trend is differencing: instead of the values themselves, you model the changes between them. It costs one line of code, it fixes random walks, and it is built into ARIMA (the "I" stands for integrated, the opposite of differenced). This lesson covers first differences, seasonal differences, how many to take, and how to undo them when producing forecasts.

First differences

The first difference is d(t) = y(t) - y(t-1). A series with a steady trend becomes a series with a constant mean (the slope), and a random walk becomes white noise.

Python First difference removes a linear trend

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 original series has very different means in the two halves; the differenced series has the same mean (close to the slope 0.3) in both. One observation is lost at the start, because the first value has no predecessor.

Second differences

If the trend itself is curved, take the difference of the difference: d2(t) = d1(t) - d1(t-1). This removes a quadratic trend. In practice you almost never need more than two differences; if you do, something else is wrong, such as an outlier or a structural break.

Python Second difference removes a quadratic trend

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 second difference is constant (0.04, twice the quadratic coefficient), so it has no trend left.

Seasonal differencing

A seasonal difference subtracts the value from one season earlier: y(t) - y(t-m), where m is the period (12 for monthly, 7 for daily-with-weekly). It removes a stable seasonal pattern and any trend that is roughly linear in that window, because year-over-year change is roughly constant.

Python Seasonal difference of a monthly 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.

After the seasonal difference the monthly means are nearly equal (the season is gone) and the mean equals the yearly growth of 12. Adding a first difference on top removes that remaining level.

How many differences do I need?

Work in this order and stop as soon as the series looks stationary:

  1. If there is clear, strong seasonality, take a seasonal difference first.
  2. If a trend remains, take a first difference.
  3. Check again. Rarely, take a second.

Signs of over-differencing:

  • The variance of the series goes up after differencing.
  • The autocorrelation at lag 1 becomes strongly negative (close to -0.5 or lower).
  • The model needs a moving-average term to cancel what was introduced.
Python Over-differencing inflates variance

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.

One difference turns the walk into white noise (variance near 1, autocorrelation near 0). A second difference doubles the variance and pushes the lag-1 autocorrelation toward -0.5. That is the fingerprint of over-differencing.

Detrending instead

Differencing assumes the series wanders (difference-stationary). If instead it fluctuates around a deterministic trend (trend-stationary), subtract a fitted trend line, as in regression-based decomposition:

Python
import numpy as np
t = np.arange(len(y))
slope, intercept = np.polyfit(t, y, 1)
detrended = y - (intercept + slope * t)

Choosing between the two is the job of the unit root tests later in this chapter. When in doubt for forecasting, differencing is more forgiving because it adapts to a changing level, while a fixed line keeps extrapolating its old slope.

Undoing differences

A model fitted on differences predicts differences. To get forecasts of the original series, integrate them back: add the predicted change to the last known value, step by step. Libraries like statsmodels' ARIMA do this for you, but you should know how it works.

Python Invert differences with cumsum

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 cumulative sum rebuilds the original exactly, and a forecast of constant change continues the line from the last observation.

Summary of transformations so far

Problem Remedy
Stochastic trend (random walk) first difference
Deterministic trend subtract fitted trend, or difference
Fixed seasonality seasonal difference or seasonal terms
Variance grows with level log or Box-Cox first, then difference

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the first difference of a random walk look like?

    y(t) - y(t-1) equals the new shock e(t), which is white noise.
  2. 2. Monthly data has a strong yearly pattern and a trend. What is a sensible first step?

    Differencing at the seasonal lag removes most of the seasonality; then check if a first difference is still needed.
  3. 3. Which suggests over-differencing?

    Differencing an already stationary series introduces negative lag-1 correlation and inflates variance.
  4. 4. How do you return a forecast of differences to the original scale?

    Integration is the inverse of differencing: cumulate the changes starting from the last known level.

Summary

  • A first difference removes a stochastic or linear trend; a second removes a curved one.
  • A seasonal difference at lag m removes a stable season.
  • Take as few differences as needed; extra ones inflate variance and create negative autocorrelation.
  • Detrending suits deterministic trends; differencing adapts to wandering levels.
  • Forecasts of differences are integrated back with a cumulative sum.

Next: Transformations: Log and Box-Cox