Table of Contents
The Augmented Dickey-Fuller Test is a statistical test used to determine whether a time series data is stationary or not. This test is widely used in econometrics and time series analysis. In order to implement this test in Python, one can use the “adfuller” function from the “statsmodels” library. This function takes in a time series data as input and returns the test statistic, p-value, and critical values. A p-value less than 0.05 indicates that the data is stationary. An example of implementing the Augmented Dickey-Fuller Test in Python would be as follows:
import pandas as pd
from statsmodels.tsa.stattools import adfuller
#loading the time series data
data = pd.read_csv(“data.csv”)
#implementing the Augmented Dickey-Fuller Test
result = adfuller(data)
#printing the test statistic, p-value, and critical values
print(“ADF Statistic:”, result[0])
print(“p-value:”, result[1])
print(“Critical Values:”, result[4])
#checking if data is stationary
if result[1] < 0.05:
print(“Data is stationary”)
else:
print(“Data is non-stationary”)
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 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.pyplotas plt plt.plot(data)

To perform an augmented Dickey-Fuller test, we can use the 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.stattoolsimport 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.
Cite this article
stats writer (2024). How can the Augmented Dickey-Fuller Test be implemented in Python, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-augmented-dickey-fuller-test-be-implemented-in-python-and-can-you-provide-an-example/
stats writer. "How can the Augmented Dickey-Fuller Test be implemented in Python, and can you provide an example?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-the-augmented-dickey-fuller-test-be-implemented-in-python-and-can-you-provide-an-example/.
stats writer. "How can the Augmented Dickey-Fuller Test be implemented in Python, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-augmented-dickey-fuller-test-be-implemented-in-python-and-can-you-provide-an-example/.
stats writer (2024) 'How can the Augmented Dickey-Fuller Test be implemented in Python, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-augmented-dickey-fuller-test-be-implemented-in-python-and-can-you-provide-an-example/.
[1] stats writer, "How can the Augmented Dickey-Fuller Test be implemented in Python, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.
stats writer. How can the Augmented Dickey-Fuller Test be implemented in Python, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
