DEV Community

Cover image for ADF vs KPSS in Python: How to Test Time Series Stationarity Before Forecasting
Ahmed Gadallh for Vertex Frontier

Posted on

ADF vs KPSS in Python: How to Test Time Series Stationarity Before Forecasting

Stationarity is one of the most important concepts in time series analysis. Before building forecasting models such as ARIMA or SARIMA, you need to understand whether your data behaves consistently over time.

Two widely used statistical tests for this purpose are the Augmented Dickey-Fuller (ADF) test and the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test.

Although both tests investigate stationarity, they approach the problem from opposite directions. Understanding their differences can help you make better decisions when preparing time series data for forecasting.

1. What Is Stationarity in Time Series?

A time series is generally considered weakly stationary when its statistical properties remain stable over time.

These properties include:

  • Constant mean.
  • Constant variance.
  • Autocovariance that depends on the time lag rather than the specific time period.

Stationarity matters because many traditional forecasting techniques rely on assumptions about the statistical behavior of the underlying data.

2. Understanding the ADF Test

The Augmented Dickey-Fuller test checks whether a time series contains a unit root.

Its hypotheses are:

  • Null hypothesis (H0): The series has a unit root and is non-stationary.
  • Alternative hypothesis (H1): The series does not have a unit root.

At a 0.05 significance level:

  • A p-value below 0.05 provides evidence against the unit-root null hypothesis.
  • A p-value above 0.05 means there is insufficient evidence to reject the null hypothesis.

Let's implement the ADF test in Python:

import numpy as np
from statsmodels.tsa.stattools import adfuller

np.random.seed(42)
data = np.random.normal(0, 1, 200)

result = adfuller(data, autolag="AIC")

print("ADF Statistic:", result[0])
print("P-value:", result[1])

if result[1] <= 0.05:
    print("Evidence against a unit root.")
else:
    print("Insufficient evidence to reject a unit root.")
Enter fullscreen mode Exit fullscreen mode

3. Understanding the KPSS Test

The KPSS test takes a different approach. Its null hypothesis assumes that the series is stationary under a specified deterministic component.

  • Null hypothesis (H0): The series is stationary.
  • Alternative hypothesis (H1): The series is non-stationary under the selected specification.

KPSS supports two common specifications:

  • regression="c": Stationarity around a constant.
  • regression="ct": Stationarity around a constant and deterministic trend.

Here is a Python example:

from statsmodels.tsa.stattools import kpss

result = kpss(
    data,
    regression="c",
    nlags="auto"
)

print("KPSS Statistic:", result[0])
print("P-value:", result[1])
print("Lags:", result[2])

if result[1] <= 0.05:
    print("Reject the stationarity null hypothesis.")
else:
    print("Fail to reject the stationarity null hypothesis.")
Enter fullscreen mode Exit fullscreen mode

A p-value above 0.05 does not prove that the series is stationary. It simply means there is insufficient evidence to reject the stationarity hypothesis.

For a more detailed explanation of KPSS hypotheses, p-values, implementation, and interpretation, read this practical guide:

KPSS Test in Python: How to Run It and Interpret the Results

4. ADF vs KPSS: What Is the Difference?

The most important difference between these tests is their null hypotheses.

Feature ADF Test KPSS Test
Main purpose Detect a unit root Test stationarity
Null hypothesis Non-stationarity Stationarity
Small p-value Evidence against a unit root Evidence against stationarity
Large p-value Fail to reject unit root Fail to reject stationarity
Python library statsmodels statsmodels

Because their null hypotheses are different, using both tests can provide a more informative diagnostic picture than relying on only one.

5. Combining ADF and KPSS Results

Running both tests on the same dataset can produce four possible combinations:

ADF Result KPSS Result Possible Interpretation
Reject H0 Fail to reject H0 Evidence consistent with stationarity
Fail to reject H0 Reject H0 Evidence consistent with non-stationarity
Fail to reject H0 Fail to reject H0 Inconclusive; investigate test power and specifications
Reject H0 Reject H0 Conflicting evidence; investigate trends, breaks, and assumptions

These outcomes are diagnostic clues, not automatic classifications. Structural breaks, seasonality, changing variance, and different deterministic specifications can explain conflicting results.

6. What Should You Do If Your Time Series Is Non-Stationary?

If your tests suggest non-stationarity, investigate the data before choosing a transformation.

1. Differencing

Useful when the series behaves like a difference-stationary process.

differenced = data_series.diff().dropna()
Enter fullscreen mode Exit fullscreen mode

2. Detrending

Useful when the series contains a deterministic trend.

3. Seasonal Differencing

Useful when recurring seasonal patterns contribute to non-stationarity.

4. Structural Break Analysis

Sudden changes in the data-generating process can affect stationarity tests and should be investigated separately.

After applying a transformation, rerun the tests and inspect the resulting series.

7. A Practical Python Workflow

Here is a simple workflow for real-world time series projects:

  1. Load and clean your time series data.
  2. Sort observations chronologically.
  3. Visualize the series.
  4. Run the ADF test.
  5. Run the KPSS test.
  6. Compare their results.
  7. Investigate trends, seasonality, and structural breaks.
  8. Apply an appropriate transformation if necessary.
  9. Run the tests again.
  10. Evaluate forecasting models using time-aware validation.

Conclusion

The ADF and KPSS tests are complementary tools for investigating time series stationarity in Python.

ADF focuses on detecting a unit root, while KPSS tests stationarity as its null hypothesis.

Using both tests together provides a stronger diagnostic foundation before moving toward forecasting models such as ARIMA and SARIMA.

The key is not simply obtaining a p-value. It is understanding the assumptions behind each test and interpreting the results in the context of your data.

Further Reading


This tutorial is intended for Python developers, data scientists, and anyone working with statistical time series forecasting.

Top comments (0)