Course contentsShow
← Machine Learning and Deep Learning
Lesson 2388 of 3,53852. Time Series and ForecastingFree lesson

Differencing for Stationarity

Removing trend and seasonality through first-order and seasonal differencing to achieve stationarity.

Differencing for Stationarity

What you'll learn: How to transform non-stationary time series into stationary ones by removing trends and seasonality through differencing operations.

The Core Idea

Remember that stationarity means your time series has constant statistical properties over time—no wandering trends or repeating seasonal patterns. When your data isn't stationary, differencing is your go-to transformation: instead of looking at raw values, you look at the changes between consecutive observations.

Think of it like tracking your bank balance versus tracking your daily spending. Your balance might grow steadily (a trend), but your daily spending differences might be much more stable and predictable.

First-Order Differencing

First-order differencing removes linear trends by computing:

y'(t) = y(t) - y(t-1)

If your original series climbs steadily upward, the differenced series captures just the step-by-step changes, eliminating that trend. You can apply differencing multiple times if needed (second-order differencing: difference the differences).

Seasonal Differencing

When you have repeating patterns—like monthly sales peaks every December—use seasonal differencing:

y'(t) = y(t) - y(t-s)

where s is the seasonal period (e.g., 12 for monthly data with yearly patterns, 7 for daily data with weekly patterns). This subtracts the value from one full cycle ago, removing the seasonal component.

Combining Both

For data with both trend and seasonality, you often need both types of differencing. Apply seasonal differencing first, then first-order differencing (or vice versa, depending on the data).

Key Takeaway: Differencing transforms non-stationary time series by computing changes between observations—use first-order differencing to remove trends and seasonal differencing (lag=seasonal period) to remove repeating patterns, making your data suitable for forecasting models that assume stationarity.