Augmented Dickey-Fuller Test in Python (With Example)


A time series is said to be “stationary” if it has no trend, exhibits constant variance over time, and has a constant autocorrelation structure over time.

One way to test whether a time series is stationary is to perform an augmented Dickey-Fuller test, which uses the following null and alternative hypotheses:

H0: The time series is non-stationary. In other words, it has some time-dependent structure and does not have constant variance over time.

HA: The time series is stationary.

If the p-value from the test is less than some significance level (e.g. α = .05), then we can reject the null hypothesis and conclude that the time series is stationary.

The following step-by-step example shows how to perform an augmented Dickey-Fuller test in Python for a given time series.

Example: Augmented Dickey-Fuller Test in Python

Suppose we have the following time series data in Python:

data = [3, 4, 4, 5, 6, 7, 6, 6, 7, 8, 9, 12, 10]

Before we perform an augmented Dickey-Fuller test on the data, we can create a quick plot to visualize the data:

import matplotlib.pyplot as plt

plt.plot(data)

To perform an augmented Dickey-Fuller test, we can use the adfuller() function from the statsmodels library. First, we need to install statsmodels:

pip install statsmodels

Next, we can use the following code to perform the augmented Dickey-Fuller test:

from statsmodels.tsa.stattools import adfuller

#perform augmented Dickey-Fuller test
adfuller(data)

(-0.9753836234744063,
 0.7621363564361013,
 0,
 12,
 {'1%': -4.137829282407408,
  '5%': -3.1549724074074077,
  '10%': -2.7144769444444443},
 31.2466098872313)

Here’s how to interpret the most important values in the output:

  • Test statistic: -0.97538
  • P-value: 0.7621

Since the p-value is not less than .05, we fail to reject the null hypothesis.

This means the time series is non-stationary. In other words, it has some time-dependent structure and does not have constant variance over time.

Additional Resources

How to Plot a Time Series in Matplotlib
How to Plot Multiple Series from a Pandas DataFrame
How to Perform a Mann-Kendall Trend Test in Python

Leave a Reply

Your email address will not be published.