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?
Machine learning models are fundamentally pattern-matching engines. They assume that the rules governing the past will continue to govern the future.
If a time series is trending upwards, the mean (average) value of the data in 2025 is mathematically different from the mean in 2020. If the statistical properties are constantly changing, a classical model (like ARIMA) cannot learn a stable mathematical formula to describe it. It will assume the future looks like the overall historical average, causing it to radically under-predict an upward-trending asset.
Stationarity is the mathematical property that ensures the mean and variance of a dataset are flat and stable over time, giving models a reliable anchor.
Think of It Like This
Think of It Like This
Imagine trying to predict a person's height based on their age.
If you study a group of children from age 2 to age 15, the "average height" is constantly trending upwards. If you build a model on this non-stationary data, it will predict that a 40-year-old is 12 feet tall.
But what if you don't predict absolute height? What if you predict the change in height per year? The "change in height" stabilizes as they hit adulthood and remains at zero. This new metric is stationary. It is mathematically stable and vastly easier to forecast into the future.
How It Actually Works
To achieve stationarity, we must remove two things: Trend and Changing Variance.
1. Removing Trend via Differencing
If a series is trending upwards, we apply Differencing. Instead of trying to forecast today's absolute value (), we forecast the difference between today and yesterday (). If a stock goes , the raw data is trending (non-stationary). The differenced data is . The mean is now a perfectly flat line.
2. Removing Changing Variance via Log Transforms
If the swings in the data get wider over time (heteroskedasticity), differencing won't fix it. You must apply a mathematical transformation, typically a Log Transform or Box-Cox transform, to physically compress the widening variance back into a stable tunnel.
How do we prove it is stationary?
You don't just eyeball the graph. You run a Unit Root Test, most commonly the Augmented Dickey-Fuller (ADF) test.
- Null Hypothesis: The series is non-stationary.
- P-Value < 0.05: You reject the null hypothesis. The series is definitively stationary.
Show Me the Code
Using Python and statsmodels to run the ADF test and apply differencing.
import pandas as pdfrom statsmodels.tsa.stattools import adfuller
# A clearly non-stationary trending seriesdata = [10, 15, 20, 25, 30, 35, 40, 45, 50]ts = pd.Series(data)
# Run the ADF testresult = adfuller(ts)print(f"P-Value (Raw): {result[1]:.4f}")# -> P-Value (Raw): 0.99 (>> 0.05, highly NON-stationary)
# Apply First-Order Differencing (subtract previous day)diff_ts = ts.diff().dropna()print(diff_ts.values)# -> [5. 5. 5. 5. 5. 5. 5. 5.]
# Re-run the ADF testresult_diff = adfuller(diff_ts)print(f"P-Value (Differenced): {result_diff[1]:.4f}")# -> P-Value (Differenced): 0.00 (<< 0.05, perfectly stationary!)Watch Out For
Watch Out For
Over-differencing. If first-order differencing (subtracting the previous day) makes the series stationary, stop there. If you difference a stationary series again, you artificially inject negative correlation (noise) into the data, which will cripple your forecasting model.
Watch Out For
Deep Learning doesn't strictly care. ARIMA requires strict stationarity or it will fail mathematically. However, deep forecasting models (like LSTMs or Transformers) and Gradient Boosting (like XGBoost) can often learn to approximate trends internally without explicit differencing, provided you give them the right time-based features.
The Quick Version
- Stationarity means a time series has a constant mean and variance over time.
- It is a strict prerequisite for classical forecasting models (like ARIMA).
- We remove trends using Differencing (subtracting from ).
- We remove expanding variance using Log Transforms.
- The Augmented Dickey-Fuller (ADF) test mathematically proves whether a series is stationary.