Stationarity
The property of a time series whose statistical properties (like mean and variance) do not change over time. A strict requirement for almost all classical forecasting algorithms.
Why Does This Exist?
You fit arima on yearly demand that climbs every year. The model learns one fixed mean and one fixed spread, then predicts the future will look like the past average. On a rising trend that average sits below where you are now, so forecasts run low from day one.
Classical models need stable statistics to learn a stable formula. They assume the mean does not drift and the spread does not widen. When the raw series breaks that promise, you reshape it first and model the reshaped version.
One idea underneath it. Stationarity means the rules do not move with the calendar. The average of January looks like the average of June, and the typical swing looks the same too. Differencing and transforms are ways to coax a drifting series into that shape before fitting.
This page covers the property, the two fixes, and the traps. The model that assumes it lives in arima. The correlation diagnostic lives in autocorrelation. The smoothing alternative lives in exponential-smoothing.
Think of It Like This
Weighing yourself on a moving elevator
You step on a bathroom scale inside an elevator that keeps rising. The reading climbs even though your weight did not change. No diet plan fitted to those readings will predict tomorrow, because the floor itself moves.
Now record the change between readings instead of the reading. Up one floor, the change is steady. That change series is flat and easy to model, and you can always add it back up to get the level. Differencing is weighing the change, not the floor.
Where it stops: the trick fixes a moving floor, not a widening wobble. If the elevator also starts swinging harder each floor, differences still swing wider. You need a second fix for spread, not more differencing.
How It Actually Works
Call the series at time t. It is stationary when its mean and variance stay put as t moves and the link between and depends only on the gap k, not on t. A straight climb breaks the first part. Widening swings break the second. You will often hear weak stationarity for this mean plus variance plus gap only version, which is what forecasting assumes.
Removing trend with differencing
First differencing replaces each value with the step from the last one, . A steady climb becomes a flat line. A straight line trend becomes a constant. That constant has a stable mean by construction, which is why arima carries its d term for how many times to difference.
Worked example with six readings
Take , , , , and . Raw mean is . Raw variance around that mean is . The mean visibly drifts with time, so no single level describes it.
Difference it. Steps are , , , and . Mean of the steps is . Variance of the steps is . You turned a climbing level with spread into a flat line with zero spread. That is the diagram headline: six climbing dots on the left, five flat 5s on the right.
Taming spread and checking the result
When swings widen with level, differencing alone leaves wider steps later. A log or similar squeeze compresses large values more than small ones, which steadies spread before you difference. In practice you stabilise spread first, then difference for level.
You do not judge by eye alone. Plot the series and its autocorrelation, and run a unit root test where the null says non-stationary and a small p value rejects it. One difference that flattens the mean is enough. A second difference on already flat data injects fake zigzag correlation.
Code
Difference the six readings with plain Python and confirm the flat steps.
raw = [10.0, 15.0, 20.0, 25.0, 30.0, 35.0]diff = [raw[i] - raw[i-1] for i in range(1, len(raw))]raw_mean = sum(raw) / len(raw)raw_var = sum((v - raw_mean) ** 2 for v in raw) / len(raw)diff_mean = sum(diff) / len(diff)diff_var = sum((v - diff_mean) ** 2 for v in diff) / len(diff)print(f"raw mean={raw_mean:.1f} var={raw_var:.2f}")# -> raw mean=22.5 var=72.92print(f"diff={diff} mean={diff_mean:.1f} var={diff_var:.1f}")# -> diff=[5.0, 5.0, 5.0, 5.0, 5.0] mean=5.0 var=0.0The printout matches the hand arithmetic. Replace the last raw value with and rerun: the steps stop being flat and variance jumps above zero, which is the signal that one difference no longer suffices or spread needs treatment.
Watch Out For
Differencing twice when once already worked
First differences look flat, so you difference again for safety. The twice differenced series now zigzags with negative correlation that was never in the data, and forecasts wobble.
Symptom: flat once differenced series plus worse validation after a second difference, with lag one autocorrelation near minus 0.5. Fix: stop at the first order that flattens the mean and steadies spread. Difference to achieve stationarity, not as a habit.
Assuming deep models need the same prep
arima fails mathematically without stationarity. A tree or recurrent net can often learn a trend from time features without explicit differencing, provided you give it those features.
Symptom: differenced inputs help ARIMA yet hurt a gradient boosted tree that loses level information it could have used. Fix: match prep to the model. Difference for classical linear models. For flexible models, try time features first and only difference if validation says it helps.
The Quick Version
- Stationarity means mean and variance stay put over time, so one learned formula keeps applying.
- Our six readings average 22.5 with variance 72.92, while their five steps are all 5.0 with mean 5 and zero variance.
- First differencing removes a drifting level and turns a climb into a flat line.
- Widening spread needs a squeeze like a log first, since differencing alone cannot fix it.
- Stop at the first order that works, since extra differencing injects fake zigzag correlation.
- Classical models require this prep, while flexible models can sometimes learn trends from time features.