Stop 11 · Time Series AnalysisTopics · 2/7

Stationarity & Differencing

Part of the Data Science Roadmap

In short

Many forecasting models assume the series behaves the same way over time. Real data rarely does, so you learn to test for it and fix it with a simple trick called differencing.

A stationary series has statistical properties (mean, variance) that don't change over time — many classical forecasting models require this, and differencing (subtracting each value from the previous one) is the standard way to achieve it.

The mascot marks a pencil line on a wooden door frame above a ladder of earlier height marks.

Think of it like this

Tracking a child's growth

A child's height keeps climbing, so last year's average height is useless for guessing next month's. But growth per month, say about 0.5 cm, stays fairly steady. Look at the changes instead of the levels and the pattern becomes stable enough to predict.

Watch it happen

Differencing a rising series

100
0
104
1
109
2
113
3
118
4
122
5

1/4A series that keeps rising. Its mean depends on when you look, so it's not stationary.

The big ideas

What you need to know

What stationary means

Mean, variance and correlation with past values stay roughly the same over time.

First difference

y today minus y yesterday removes a steady trend. Doing it twice is rarely needed.

Seasonal difference

Subtract the value from one season ago, like 12 months back, to remove yearly patterns.

Test it

The ADF test's null hypothesis is 'not stationary'. A small p-value, below 0.05, suggests the series is stationary.

See it in code

Walk through the code

python
1import numpy as np2import pandas as pd3from statsmodels.tsa.stattools import adfuller4 5rng = np.random.default_rng(1)6series = pd.Series(100 + np.cumsum(rng.normal(1, 2, 200)))   # drifting upward7 8print("p-value raw: ", adfuller(series)[1])9diffed = series.diff().dropna()10print("p-value diff:", adfuller(diffed)[1])

1/3Add up random steps that average +1, giving a series that wanders upward.

A drifting random walk fails the ADF test with a high p-value. After one difference the p-value drops near zero.

Save yourself the pain

Common mistakes

Oops

Differencing again and again until the plot looks flat.

Do this instead

Over-differencing adds noise. Usually one regular difference, plus a seasonal one if needed, is enough.

Oops

Forgetting to undo differencing on the forecasts.

Do this instead

Add the predicted changes back onto the last real value, or let ARIMA's d parameter handle it.

Your mission

Make a stock price stationary

~20 min

Quick check

The ADF test gives a p-value of 0.62 on your series. What does that suggest?

Go deeper

  1. 1Run a statistical test (e.g. Augmented Dickey-Fuller) to check if a series is stationary.
  2. 2Apply differencing to a non-stationary series and re-test.
  3. 3Learn why non-stationary data can produce misleading forecasts if fed directly into certain models.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge