Hello! Welcome to your fifth lesson in the "Time Series Fundamentals" module.
In our previous lesson, you learned how to decompose a time series into its trend, seasonal, and residual components. We saw that trend and seasonality are key structural patterns that describe how a series evolves. The presence of these components is directly related to the topic of today's lesson.
This lesson addresses a critical concept in time series analysis: stationarity. We will define what it means for a time series to be stationary, understand why this property is so important for forecasting models, and learn how to use a formal statistical test—the Augmented Dickey-Fuller (ADF) test—to check for it.
1. What is Stationarity?
Many time series models, from classical methods like ARIMA to certain deep learning architectures, are designed with the assumption that the underlying statistical properties of the data do not change over time. A series that exhibits this property is called stationary.
While there is a "strict" definition of stationarity that is quite restrictive, in practice we are almost always concerned with a more relaxed definition known as weak-sense stationarity (or covariance stationarity). A time series is considered stationary if it meets three criteria:
- Constant Mean: The average of the series is not a function of time. The series should not exhibit a clear trend.
- Constant Variance: The volatility or spread of the series is constant over time. The series shouldn't get wildly more or less spread out.
- Constant Autocovariance: The relationship between an observation and a lagged observation (e.g., the value
ksteps before it) does not depend on time. This implies the absence of seasonality.
A non-stationary series, by contrast, has statistical properties that change over time, making it difficult for a model to learn stable patterns.
Time Series Talk : Stationarity
To build a strong intuition for these three criteria, watch the following segment from the "Time Series Talk" series by ritvikmath. It provides clear visual examples for each condition.
Watch from 00:39 to 03:24. The video first lists the three conditions for stationarity and then shows three different time series plots, each violating one of these conditions. This will help you learn to spot signs of non-stationarity visually.
In essence, a stationary time series is one whose statistical character remains consistent. This consistency is crucial because it allows a model to learn patterns from the past that are applicable to the future. A non-stationary series is a "moving target," making reliable forecasting much more challenging.
2. How to Test for Stationarity
Now that we know what stationarity is, how do we check for it? There are two main approaches.
Visual Inspection
Your first step should always be to plot the data. As you saw in the video, a visual check can often reveal obvious signs of non-stationarity:
- A clear upward or downward trend violates the constant mean condition.
- Widening or narrowing fluctuations over time violate the constant variance condition.
- A repeating, predictable pattern (seasonality) violates the constant autocovariance condition.
The decomposition plots you created in the last lesson are also excellent tools for this. If the trend component is not flat or the seasonal component is strong, the series is likely non-stationary.
Statistical Tests
While visual inspection is a good start, it can be subjective. To get a definitive answer, we use statistical hypothesis tests. The most common of these is the Augmented Dickey-Fuller (ADF) test.
The ADF test is a type of "unit root test." The underlying theory involves testing coefficients in an autoregressive model, but for practical purposes, you can think of a "unit root" as a characteristic of a time series that makes it non-stationary.
The hypotheses for the ADF test are:
- Null Hypothesis (H₀): The time series has a unit root (it is non-stationary).
- Alternative Hypothesis (H₁): The time series does not have a unit root (it is stationary).
Our goal is to find enough evidence to reject the null hypothesis and conclude that our series is stationary.
To get a more formal understanding of how this test works, the next video provides a concise theoretical background. Given your technical background, you may find the connection to autoregressive models interesting.
Time Series Talk : Augmented Dickey Fuller Test + Code
Let's dive into the mechanics of the ADF test. This video, also from ritvikmath, explains the theory behind the basic Dickey-Fuller test and its extension to the 'Augmented' version.
Watch from 01:01 to 05:20. The video explains that the test checks for a 'unit root' (a coefficient of 1 in an AR(1) model), which indicates non-stationarity. It also clarifies why a special distribution (the Dickey-Fuller distribution) is used for the test statistic. Don't worry about memorizing the equations; focus on the logic of the hypothesis.
3. The ADF Test in Practice with Python
With the theory in place, let's see how to implement the ADF test in Python. The statsmodels library, which you used for decomposition, provides a simple function for this: adfuller.
How to Check if Time Series Data is Stationary with Python
The article 'How to Check if Time Series Data is Stationary with Python' from Machine Learning Mastery provides a clear, code-first guide to using and interpreting the adfuller function. This will be our primary guide for the practical implementation.
Read the section titled 'Augmented Dickey-Fuller test'. It explains how to interpret the test's output and provides two complete Python examples: one on a stationary dataset (Daily Female Births) and one on a non-stationary dataset (Airline Passengers). Pay close attention to how the conclusion is drawn in each case.
As you read, note that there are two key pieces of information in the output of adfuller that we use to make our decision:
-
The p-value: This is the most direct way to interpret the result.
- If p-value ≤ 0.05, we reject the null hypothesis (H₀). The series is likely stationary.
- If p-value > 0.05, we fail to reject the null hypothesis. The series is likely non-stationary.
(Note: 0.05 is a standard significance level, but can be adjusted.)
-
The ADF Statistic and Critical Values: This is an alternative way to reach the same conclusion.
- If the ADF Statistic is more negative than the critical values (e.g., at the 1%, 5%, or 10% levels), we reject the null hypothesis. The series is likely stationary.
Let's look at the example of the non-stationary airline passengers dataset from the article:
from pandas import read_csv
from statsmodels.tsa.stattools import adfuller
series = read_csv('international-airline-passengers.csv', header=0, index_col=0, squeeze=True)
X = series.values
result = adfuller(X)
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])
print('Critical Values:')
for key, value in result[4].items():
print('\t%s: %.3f' % (key, value))
The output is:
ADF Statistic: 0.815369
p-value: 0.991880
Critical Values:
1%: -3.482
5%: -2.884
10%: -2.579
Interpretation:
- The p-value (0.99) is much greater than 0.05.
- The ADF Statistic (0.815) is not more negative than any of the critical values.
Both indicators lead to the same conclusion: we fail to reject the null hypothesis. The data is non-stationary.
Test your understanding!
You run an ADF test on a new time series and get the following output:
ADF Statistic: -4.213
p-value: 0.0006
Critical Values:
1%: -3.45
5%: -2.87
10%: -2.57
Is this time series stationary or non-stationary? Explain your reasoning based on both the p-value and the ADF statistic.
Show answer
The time series is stationary.
Here's the reasoning:
- p-value: The p-value of 0.0006 is much less than the standard significance level of 0.05. This allows us to reject the null hypothesis (that the series is non-stationary).
- ADF Statistic: The ADF statistic of -4.213 is more negative than all the critical values (e.g., it is less than -3.45 at the 1% level). This also leads us to reject the null hypothesis.
Since we reject the null hypothesis, we conclude that the series is stationary.
Conclusion
In this lesson, you've learned about one of the most important properties of a time series: stationarity. Understanding and testing for it is a foundational skill for forecasting.
Key Takeaways:
- A stationary time series has a constant mean, constant variance, and constant autocovariance. Its statistical properties do not change over time.
- Stationarity is a crucial assumption for many classical forecasting models, and making a series stationary can also improve the performance of deep learning models.
- You can perform a quick visual check for stationarity by looking for trends and seasonality in your plots.
- The Augmented Dickey-Fuller (ADF) test is a formal statistical test for stationarity. Its null hypothesis (H₀) is that the series is non-stationary.
- To conclude that a series is stationary, you need a p-value ≤ 0.05 or an ADF Statistic that is more negative than the critical values.
So, what happens when the ADF test tells you that your data is non-stationary? You can't just give up! In our next lesson, we will explore common techniques like differencing and logging to transform non-stationary data into stationary data, making it ready for modeling.