Introduction
In our last lesson, we established a formal understanding of stationarity, defining both its strict and weak forms and exploring why it is a fundamental assumption for many classical forecasting models like ARIMA. You learned that non-stationary series, with their changing means or variances, break the core assumption that patterns from the past will hold in the future.
While visual inspection can give us clues about non-stationarity, it's often subjective. Today, we will move from intuition to statistical rigor. This lesson focuses on the practical task of formally testing for stationarity using two of the most common statistical procedures: the Augmented Dickey-Fuller (ADF) test and the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test. By the end of this lesson, you will be able to apply these tests, interpret their results, and, crucially, know what to do when they seem to contradict each other. This is a critical skill for any data scientist working with time series.
From Visuals to Hypothesis Testing: The Unit Root
In the previous lesson, we saw that a random walk is a classic example of a non-stationary process. Its mean and variance are time-dependent. This behavior is caused by what is known as a unit root. In simple terms, a unit root is a feature of a stochastic process that gives rise to a persistent, unpredictable "random walk" like trend. Detecting a unit root is a formal way of identifying a specific, and very common, type of non-stationarity.
Statistical tests for stationarity are structured as hypothesis tests. They help us decide, with a certain level of statistical confidence, whether a series is stationary or if it contains a unit root. Let's dive into the first of these tests.
The Augmented Dickey-Fuller (ADF) Test
The Dickey-Fuller test is designed specifically to detect a unit root. It starts by assuming the time series can be described by a simple autoregressive model, . If the coefficient is exactly 1, the process has a unit root and is non-stationary. The "Augmented" version simply extends this test to handle more complex and realistic time series structures.
The following video provides an excellent explanation of the logic behind the ADF test, including its hypotheses and why it requires a special statistical distribution for its test statistic.
Time Series Talk : Augmented Dickey Fuller Test + Code
This video from the ritvikmath channel breaks down the ADF test, from its theoretical foundation in an AR(1) process to its practical implementation in Python.
Please watch the following segments to understand the mechanics of the test: Dickey-Fuller Hypotheses: This section explains the null hypothesis (H_0) that a unit root exists (\phi_1 = 1), making the series non-stationary. Test Statistic: Pay attention to how the equation is transformed and why the resulting t-statistic must be compared to a special "Dickey-Fuller distribution" instead of a standard t-distribution. Augmented Dickey-Fuller: This part explains how the test is extended to more complex processes. Python Implementation: Observe how the adfuller function is used and how the p-value is interpreted.
The key takeaway from the video is the setup of the ADF test's hypotheses:
- Null Hypothesis (): The series has a unit root (it is non-stationary).
- Alternative Hypothesis (): The series is stationary.
To interpret the test's output, you'll primarily look at the p-value. A common significance level is 5% (or 0.05).
- If p-value ≤ 0.05, you reject the null hypothesis. This means you have strong evidence that the series is stationary.
- If p-value > 0.05, you fail to reject the null hypothesis. You do not have enough evidence to say the series is stationary.
The Kwiatkowski-Phillips-Schmidt-Shin (KPSS) Test
The ADF test is powerful, but it's not the only tool we have. Another widely used test is the KPSS test. It's important because it approaches the problem from the opposite direction.
The hypotheses for the KPSS test are inverted compared to the ADF test:
- Null Hypothesis (): The series is stationary (or more specifically, stationary around a constant mean or a deterministic trend).
- Alternative Hypothesis (): The series has a unit root (it is non-stationary).
This reversal of hypotheses is what makes the KPSS test a valuable complement to the ADF test. For the KPSS test, the interpretation of the p-value is also reversed:
- If p-value ≤ 0.05, you reject the null hypothesis. This suggests the series is non-stationary.
- If p-value > 0.05, you fail to reject the null hypothesis. This supports the conclusion that the series is stationary.
The following blog post provides a clear, practical guide to implementing and interpreting both the ADF and KPSS tests.
Stationarity in Time Series — A Comprehensive Guide – Leonie Monigatti
This guide by Leonie Monigatti clearly lays out the hypotheses and interpretation for both the ADF and KPSS tests.
Please read the sections on the ADF and KPSS tests. Focus on: The section describing the ADF test, confirming the hypotheses and how to interpret the results from the statsmodels library in Python. The section describing the KPSS test, paying close attention to its opposite hypotheses and the use of the regression parameter ('c' for stationarity around a mean, 'ct' for stationarity around a trend).
For demand data in e-commerce, which often exhibits trends, you'll typically use regression='ct' with the KPSS test to check for stationarity around a linear trend.
When Tests Disagree: Reconciling ADF and KPSS
You now have two tests with opposite null hypotheses. What happens when they give contradictory results? This is not a failure of the tests; it's an opportunity for a more nuanced diagnosis. This scenario is common in practice and understanding how to interpret it is a key skill.
There are four possible outcomes when you run both tests:
-
Both tests conclude the series is stationary. (ADF rejects , KPSS fails to reject )
- Conclusion: The series is stationary. You can proceed with models that assume stationarity.
-
Both tests conclude the series is non-stationary. (ADF fails to reject , KPSS rejects )
- Conclusion: The series is non-stationary. You need to apply transformations to make it stationary.
-
KPSS indicates stationarity, but ADF indicates non-stationarity. (ADF fails to reject , KPSS fails to reject )
- Conclusion: The series is trend-stationary. This means the series has a deterministic trend (e.g., a straight line). Once you remove this trend, the remaining series is stationary. The correct action here is detrending.
-
KPSS indicates non-stationarity, but ADF indicates stationarity. (ADF rejects , KPSS rejects )
- Conclusion: The series is difference-stationary. This means the series has a stochastic trend (a unit root). The correct action here is differencing (i.e., computing the change from one period to the next).
This logic is crucial. Mistaking a trend-stationary series for a difference-stationary one (or vice versa) can lead you to apply the wrong transformation, which can harm your model's performance.
The following statsmodels guide provides a clear explanation and a practical code example of this reconciliation process.
Stationarity and detrending (ADF/KPSS)
This official statsmodels notebook provides an excellent walkthrough of applying both tests, interpreting conflicting results, and then applying a transformation to achieve stationarity.
Please read through the notebook with a focus on these parts: Review the code for the ADF test and KPSS test functions to see how they are implemented. Carefully study the section outlining the four possible outcomes. This is the core framework for reconciling the tests. Follow the example where the author applies differencing to the series and then re-runs both tests to confirm that the transformed series is now stationary.
A Practical Walkthrough
Let's apply these concepts using the Amazon quarterly revenue data, which is highly relevant to your goal of demand forecasting. The data clearly shows an upward trend, so we expect it to be non-stationary.
Time Series Non Stationary Statistical Test - KPSS and ADF
This video from the AIEngineering channel provides a step-by-step tutorial on testing a real-world revenue time series.
Watch the following segments to see the entire workflow in action: Loading and Plotting Data: Notice the clear upward trend, a visual sign of non-stationarity. Performing the KPSS Test: The author runs the KPSS test and, based on the low p-value, rejects the null hypothesis of stationarity. Note the use of regression='ct'. Performing the ADF Test: Next, the ADF test is performed. The p-value is high, so the author fails to reject the null hypothesis of non-stationarity.
In this example, both tests agree: the Amazon revenue series is non-stationary. Based on our 4-box matrix, this is "Case 2". This gives us a confident conclusion that we need to transform the data before modeling. The most likely transformation needed here would be differencing, possibly combined with a log transform to stabilize the increasing variance.
Conclusion
In this lesson, you have moved beyond subjective visual checks and learned how to use formal statistical tests to assess stationarity. This is a standard and non-negotiable step in building reliable classical forecasting models.
Here are the main takeaways:
- The Augmented Dickey-Fuller (ADF) and KPSS tests are two primary tools for testing stationarity.
- They work with opposite null hypotheses: ADF's null is non-stationarity (unit root), while KPSS's null is stationarity.
- Interpreting conflicting results is key: the combination of test outcomes helps you distinguish between a trend-stationary series (which needs detrending) and a difference-stationary series (which needs differencing).
- A low p-value (e.g., < 0.05) means you reject the null hypothesis. For ADF, this implies stationarity. For KPSS, this implies non-stationarity.
You now have the tools to diagnose whether a time series is stationary and to understand the nature of its non-stationarity. In our next lesson, we will put this diagnostic skill to use. We will explore the practical techniques—differencing, seasonal differencing, and transformations like the Box-Cox/log transform—that you can apply to stabilize a series and prepare it for modeling.