Test the stationarity of a given Time-Series Dataset🔗
Introduction🔗
Summary
As stated by Robert Nau, Duke University:
A stationary time series is one whose statistical properties such as mean, variance, autocorrelation, etc. are all constant over time. Most statistical forecasting methods are based on the assumption that the time series can be rendered approximately stationary (i.e., "stationarized") through the use of mathematical transformations.
For more info, see: Introduction to ARIMA models.
Info
There are two primary libraries used to implement these tests, ensuring both breadth of coverage and numerical reliability:
| library | category | algorithm | short | import script | url |
|---|---|---|---|---|---|
| statsmodels | Stationarity | Augmented Dickey-Fuller | ADF | from statsmodels.tsa.stattools import adfuller |
https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.adfuller.html |
| Stationarity | Kwiatkowski-Phillips-Schmidt-Shin | KPSS | from statsmodels.tsa.stattools import kpss |
https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.kpss.html | |
| Unit-Root | Zivot-Andrews structural-break unit-root test | ZA | from statsmodels.tsa.stattools import zivot_andrews |
https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.zivot_andrews.html | |
| Stationarity | Range unit-root test for stationarity | RUR | from statsmodels.tsa.stattools import range_unit_root_test |
https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.range_unit_root_test.html | |
| arch | Stationarity | Phillips-Perron | PP | from arch.unitroot import PhillipsPerron |
https://arch.readthedocs.io/en/latest/unitroot/generated/arch.unitroot.PhillipsPerron.html |
| Stationarity | Elliott-Rothenberg-Stock (ERS) de-trended DF | ERS | from arch.unitroot import DFGLS |
https://arch.readthedocs.io/en/latest/unitroot/generated/arch.unitroot.DFGLS.html | |
| Unit-Root | Variance Ratio (VR) test | VR | from arch.unitroot import VarianceRatio |
https://arch.readthedocs.io/en/latest/unitroot/generated/arch.unitroot.VarianceRatio.html |
For more info, see: Statsmodels TSA and Arch Unit Roots.
Source Library
The statsmodels and arch packages were chosen because they provide robust, industry-standard implementations of unit root and stationarity tests. Specifically, statsmodels offers the classic ADF and KPSS tests along with specialized ones like Zivot-Andrews and RUR, while arch provides high-performance implementations of Phillips-Perron, ERS, and Variance Ratio tests, ensuring a comprehensive suite of tools for detecting non-stationarity and random walks.
Source Module
All of the source code can be found within these modules:
Modules🔗
ts_stat_tests.stationarity.tests
🔗
Summary
This module contains convenience functions and tests for stationarity measures, allowing for easy access to different unit root and stationarity algorithms.
STATIONARITY_ITEM
module-attribute
🔗
STATIONARITY_ITEM = Union[
float,
int,
dict[str, float],
NDArray[float64],
ResultsStore,
]
Type alias for the items in the stationarity test result tuple.
STATIONARITY_RETURN_TYPE
module-attribute
🔗
STATIONARITY_RETURN_TYPE = tuple[STATIONARITY_ITEM, ...]
Type alias for the return type of stationarity tests.
stationarity
🔗
stationarity(
x: ArrayLike,
algorithm: str = "adf",
**kwargs: Union[float, int, str, bool, ArrayLike, None]
) -> STATIONARITY_RETURN_TYPE
Summary
Perform a stationarity test on the given data.
Details
This function is a convenience wrapper around multiple underlying algorithms:
- adf()
- kpss()
- pp()
- za()
- ers()
- vr()
- rur()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data to be checked. |
required |
algorithm
|
str
|
Which stationarity algorithm to use. |
'adf'
|
kwargs
|
Union[float, int, str, bool, ArrayLike, None]
|
Additional arguments to pass to the underlying algorithm. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When the given value for |
Returns:
| Type | Description |
|---|---|
tuple[Union[float, int, dict, ResultsStore, None], ...]
|
The result of the stationarity test. |
Credit
Calculations are performed by statsmodels, arch, and pmdarima.
Examples
| Setup | |
|---|---|
1 2 3 | |
| Example 1: Augmented Dickey-Fuller | |
|---|---|
1 2 3 | |
| Example 2: Kwiatkowski-Phillips-Schmidt-Shin | |
|---|---|
1 2 3 | |
| Example 3: Phillips-Perron | |
|---|---|
1 2 3 | |
| Example 4: Zivot-Andrews | |
|---|---|
1 2 3 | |
| Example 5: Elliot-Rothenberg-Stock | |
|---|---|
1 2 3 | |
| Example 6: Variance Ratio | |
|---|---|
1 2 3 | |
| Example 7: Range Unit Root | |
|---|---|
1 2 3 | |
| Example 8: Invalid algorithm | |
|---|---|
1 2 3 4 | |
Source code in src/ts_stat_tests/stationarity/tests.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
is_stationary
🔗
is_stationary(
x: ArrayLike,
algorithm: str = "adf",
alpha: float = 0.05,
**kwargs: Union[float, int, str, bool, ArrayLike, None]
) -> dict[str, Union[str, bool, STATIONARITY_ITEM, None]]
Summary
Test whether a given data set is stationary or not.
Details
This function checks the results of a stationarity test against a significance level alpha.
Note that different tests have different null hypotheses: - For ADF, PP, ZA, ERS, VR, RUR: H0 is non-stationarity (unit root). Stationary if p-value < alpha. - For KPSS: H0 is stationarity. Stationary if p-value > alpha.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data to be checked. |
required |
algorithm
|
str
|
Which stationarity algorithm to use. Defaults to |
'adf'
|
alpha
|
float
|
The significance level for the test. Defaults to |
0.05
|
kwargs
|
Union[float, int, str, bool, ArrayLike, None]
|
Additional arguments to pass to the underlying algorithm. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Union[str, float, bool, None]]
|
A dictionary containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 | |
| Example 1: ADF test on stationary data | |
|---|---|
1 2 3 4 5 | |
| Example 2: KPSS test on stationary data | |
|---|---|
1 2 3 4 5 | |
| Example 3: RUR test | |
|---|---|
1 2 3 4 5 | |
Source code in src/ts_stat_tests/stationarity/tests.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
ts_stat_tests.stationarity.algorithms
🔗
Summary
Stationarity tests are statistical tests used to determine whether a time series is stationary or not. A stationary time series is one whose statistical properties, such as mean and variance, do not change over time. Stationarity is an important assumption in many time series forecasting models, as it allows for the use of techniques such as autoregression and moving averages.
There are several different types of stationarity tests, including the Augmented Dickey-Fuller (ADF) test, the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test, the Phillips-Perron (PP) test, the Elliott-Rothenberg-Stock (ERS) test, and the Variance Ratio (VR) test. Each of these tests has its own strengths and weaknesses, and the choice of which test to use will depend on the specific characteristics of the time series being analyzed.
Overall, stationarity tests are an important tool in time series analysis and forecasting, as they help identify whether a time series is stationary or non-stationary, which can have implications for the choice of forecasting models and methods.
For a really good article on ADF & KPSS tests, check: When A Time Series Only Quacks Like A Duck: Testing for Stationarity Before Running Forecast Models. With Python. And A Duckling Picture.
VALID_ADF_REGRESSION_OPTIONS
module-attribute
🔗
VALID_ADF_REGRESSION_OPTIONS = Literal[
"c", "ct", "ctt", "n"
]
VALID_ADF_AUTOLAG_OPTIONS
module-attribute
🔗
VALID_ADF_AUTOLAG_OPTIONS = Literal['AIC', 'BIC', 't-stat']
VALID_ZA_REGRESSION_OPTIONS
module-attribute
🔗
VALID_ZA_REGRESSION_OPTIONS = Literal['c', 't', 'ct']
VALID_ZA_AUTOLAG_OPTIONS
module-attribute
🔗
VALID_ZA_AUTOLAG_OPTIONS = Literal['AIC', 'BIC', 't-stat']
VALID_ERS_METHOD_OPTIONS
module-attribute
🔗
VALID_ERS_METHOD_OPTIONS = Literal['aic', 'bic', 't-stat']
adf
🔗
adf(
x: ArrayLike,
maxlag: Optional[int] = None,
regression: VALID_ADF_REGRESSION_OPTIONS = "c",
*,
autolag: Optional[VALID_ADF_AUTOLAG_OPTIONS] = "AIC",
store: Literal[True],
regresults: bool = False
) -> tuple[float, float, dict, ResultsStore]
adf(
x: ArrayLike,
maxlag: Optional[int] = None,
regression: VALID_ADF_REGRESSION_OPTIONS = "c",
*,
autolag: None,
store: Literal[False] = False,
regresults: bool = False
) -> tuple[float, float, int, int, dict]
adf(
x: ArrayLike,
maxlag: Optional[int] = None,
regression: VALID_ADF_REGRESSION_OPTIONS = "c",
*,
autolag: VALID_ADF_AUTOLAG_OPTIONS = "AIC",
store: Literal[False] = False,
regresults: bool = False
) -> tuple[float, float, int, int, dict, float]
adf(
x: ArrayLike,
maxlag: Optional[int] = None,
regression: VALID_ADF_REGRESSION_OPTIONS = "c",
*,
autolag: Optional[VALID_ADF_AUTOLAG_OPTIONS] = "AIC",
store: bool = False,
regresults: bool = False
) -> Union[
tuple[float, float, dict, ResultsStore],
tuple[float, float, int, int, dict],
tuple[float, float, int, int, dict, float],
]
Summary
The Augmented Dickey-Fuller test can be used to test for a unit root in a univariate process in the presence of serial correlation.
Details
The Augmented Dickey-Fuller (ADF) test is a statistical test used to determine whether a time series is stationary or not. Stationarity refers to the property of a time series where the statistical properties, such as mean and variance, remain constant over time. Stationarity is important for time series forecasting as it allows for the use of many popular forecasting models, such as ARIMA.
The ADF test is an extension of the Dickey-Fuller test and involves regressing the first-difference of the time series on its lagged values, and then testing whether the coefficient of the lagged first-difference term is statistically significant. If it is, then the time series is considered non-stationary.
The null hypothesis of the ADF test is that the time series has a unit root, which means that it is non-stationary. The alternative hypothesis is that the time series is stationary. If the p-value of the test is less than a chosen significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is stationary.
In practical terms, if a time series is found to be non-stationary by the ADF test, one can apply differencing to the time series until it becomes stationary. This involves taking the difference between consecutive observations and potentially repeating this process until the time series is stationary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data series to test. |
required |
maxlag
|
Optional[int]
|
Maximum lag which is included in test, default value of \(12 \times (\frac{nobs}{100})^{\frac{1}{4}}\) is used when |
None
|
regression
|
VALID_ADF_REGRESSION_OPTIONS
|
Constant and trend order to include in regression.
Default: |
'c'
|
autolag
|
Optional[VALID_ADF_AUTOLAG_OPTIONS]
|
Method to use when automatically determining the lag length among the values \(0, 1, ..., maxlag\).
Default: |
'AIC'
|
store
|
bool
|
If |
False
|
regresults
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float, dict, ResultsStore], tuple[float, float, int, int, dict], tuple[float, float, int, int, dict, float]]
|
Depending on parameters, returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 2: Airline Passengers Data | |
|---|---|
1 2 3 | |
| Example 3: Store Result Instance | |
|---|---|
1 2 3 | |
| Example 4: No Autolag | |
|---|---|
1 2 3 | |
Calculation
The mathematical equation for the Augmented Dickey-Fuller (ADF) test for stationarity in time series forecasting is:
where:
- \(y_t\) is the value of the time series at time \(t\).
- \(\Delta y_t\) is the first difference of \(y_t\), which is defined as \(\Delta y_t = y_t - y_{t-1}\).
- \(\alpha\) is the constant term.
- \(\beta\) is the coefficient on \(y_{t-1}\).
- \(\delta_i\) are the coefficients on the lagged differences of \(y\).
- \(\epsilon_t\) is the error term.
The ADF test involves testing the null hypothesis that \(\beta = 0\), or equivalently, that the time series has a unit root. If \(\beta\) is significantly different from \(0\), then the null hypothesis can be rejected and the time series is considered stationary.
Here are the detailed steps for how to calculate the ADF test:
-
Collect your time series data and plot it to visually check for any trends, seasonal patterns, or other patterns that could make the data non-stationary. If you detect any such patterns, you will need to pre-process your data (e.g., detrending, deseasonalizing, etc.) to remove these effects.
-
Calculate the first differences of the time series, which is simply the difference between each observation and the previous observation. This step is performed to transform the original data into a stationary process. The first difference of \(y_t\) is defined as \(\Delta y_t = y_t - y_{t-1}\).
-
Estimate the parameters \(\alpha\), \(\beta\), and \(\delta_i\) using the least squares method. This involves regressing \(\Delta y_t\) on its lagged values, \(y_{t-1}\), and the lagged differences of \(y, \Delta y_{t-1}, \Delta y_{t-2}, \dots, \Delta y_{t-p}\), where \(p\) is the number of lags to include in the model. The estimated equation is:
\[ \Delta y_t = \alpha + \beta y_{t-1} + \sum_{i=1}^p \delta_i \Delta y_{t-i} + \epsilon_t \] -
Calculate the test statistic, which is given by:
\[ ADF = \frac {\beta-1}{SE(\beta)} \]- where \(SE(\beta)\) is the standard error of the coefficient on \(y_{t-1}\).
The test statistic measures the number of standard errors by which \(\beta\) deviates from \(1\). If ADF is less than the critical values from the ADF distribution table, we can reject the null hypothesis and conclude that the time series is stationary.
-
Compare the test statistic to the critical values in the ADF distribution table to determine the level of significance. The critical values depend on the sample size, the level of significance, and the number of lags in the model.
-
Finally, interpret the results and draw conclusions about the stationarity of the time series. If the null hypothesis is rejected, then the time series is stationary and can be used for forecasting. If the null hypothesis is not rejected, then the time series is non-stationary and requires further pre-processing before it can be used for forecasting.
Notes
The null hypothesis of the Augmented Dickey-Fuller is that there is a unit root, with the alternative that there is no unit root. If the p-value is above a critical size, then we cannot reject that there is a unit root.
The p-values are obtained through regression surface approximation from MacKinnon 1994, but using the updated 2010 tables. If the p-value is close to significant, then the critical values should be used to judge whether to reject the null.
The autolag option and maxlag for it are described in Greene.
Credit
- All credit goes to the
statsmodelslibrary.
References
- Baum, C.F. (2004). ZANDREWS: Stata module to calculate Zivot-Andrews unit root test in presence of structural break," Statistical Software Components S437301, Boston College Department of Economics, revised 2015.
- Schwert, G.W. (1989). Tests for unit roots: A Monte Carlo investigation. Journal of Business & Economic Statistics, 7: 147-159.
- Zivot, E., and Andrews, D.W.K. (1992). Further evidence on the great crash, the oil-price shock, and the unit-root hypothesis. Journal of Business & Economic Studies, 10: 251-270.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
kpss
🔗
kpss(
x: ArrayLike,
regression: VALID_KPSS_REGRESSION_OPTIONS = "c",
nlags: Optional[
Union[VALID_KPSS_NLAGS_OPTIONS, int]
] = None,
*,
store: Literal[True]
) -> tuple[float, float, int, dict, ResultsStore]
kpss(
x: ArrayLike,
regression: VALID_KPSS_REGRESSION_OPTIONS = "c",
nlags: Optional[
Union[VALID_KPSS_NLAGS_OPTIONS, int]
] = None,
*,
store: Literal[False] = False
) -> tuple[float, float, int, dict]
kpss(
x: ArrayLike,
regression: VALID_KPSS_REGRESSION_OPTIONS = "c",
nlags: Optional[
Union[VALID_KPSS_NLAGS_OPTIONS, int]
] = None,
*,
store: bool = False
) -> Union[
tuple[float, float, int, dict, ResultsStore],
tuple[float, float, int, dict],
]
Summary
Computes the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test for the null hypothesis that x is level or trend stationary.
Details
The Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test is another statistical test used to determine whether a time series is stationary or not. The KPSS test is the opposite of the Augmented Dickey-Fuller (ADF) test, which tests for the presence of a unit root in the time series.
The KPSS test involves regressing the time series on a constant and a time trend. The null hypothesis of the test is that the time series is stationary. The alternative hypothesis is that the time series has a unit root, which means that it is non-stationary.
The test statistic is calculated by taking the sum of the squared residuals of the regression. If the test statistic is greater than a critical value at a given significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is non-stationary. If the test statistic is less than the critical value, then we fail to reject the null hypothesis and conclude that the time series is stationary.
In practical terms, if a time series is found to be non-stationary by the KPSS test, one can apply differencing to the time series until it becomes stationary. This involves taking the difference between consecutive observations and potentially repeating this process until the time series is stationary.
Overall, the ADF and KPSS tests are both important tools in time series analysis and forecasting, as they help identify whether a time series is stationary or non-stationary, which can have implications for the choice of forecasting models and methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data series to test. |
required |
regression
|
VALID_KPSS_REGRESSION_OPTIONS
|
The null hypothesis for the KPSS test.
Defaults to |
'c'
|
nlags
|
Optional[Union[VALID_KPSS_NLAGS_OPTIONS, int]]
|
Indicates the number of lags to be used.
Defaults to |
None
|
store
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float, int, dict, ResultsStore], tuple[float, float, int, dict]]
|
Returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 2: Airline Passengers Data | |
|---|---|
1 2 3 | |
Calculation
The mathematical equation for the KPSS test for stationarity in time series forecasting is:
where:
- \(y_t\) is the value of the time series at time \(t\).
- \(\mu_t\) is the trend component of the time series.
- \(\epsilon_t\) is the error term.
The KPSS test involves testing the null hypothesis that the time series is trend stationary, which means that the trend component of the time series is stationary over time. If the null hypothesis is rejected, then the time series is non-stationary and requires further pre-processing before it can be used for forecasting.
Here are the detailed steps for how to calculate the KPSS test:
-
Collect your time series data and plot it to visually check for any trends, seasonal patterns, or other patterns that could make the data non-stationary. If you detect any such patterns, you will need to pre-process your data (e.g., detrending, deseasonalizing, etc.) to remove these effects.
-
Divide your time series data into multiple overlapping windows of equal size. The length of each window depends on the length of your time series and the level of detail you want to capture.
-
Calculate the trend component \(\mu_t\) for each window using a trend estimation method. There are several methods for estimating the trend component, such as the Hodrick-Prescott filter, the Christiano-Fitzgerald filter, or simple linear regression. The choice of method depends on the characteristics of your data and the level of accuracy you want to achieve.
-
Calculate the residual series \(\epsilon_t\) by subtracting the trend component from the original time series:
\[ \epsilon_t = y_t - \mu_t \] -
Estimate the variance of the residual series using a suitable estimator, such as the Newey-West estimator or the Bartlett kernel estimator. This step is necessary to correct for any serial correlation in the residual series.
-
Calculate the test statistic, which is given by:
\[ KPSS = T \times \sum_{t=1}^T \frac {S_t^2} {\sigma^2} \]where:
- \(T\) is the number of observations in the time series.
- \(S_t\) is the cumulative sum of the residual series up to time \(t\), i.e., \(S_t = \sum_{i=1}^t \epsilon_i\).
- \(\sigma^2\) is the estimated variance of the residual series.
The test statistic measures the strength of the trend component relative to the residual series. If KPSS is greater than the critical values from the KPSS distribution table, we can reject the null hypothesis and conclude that the time series is non-stationary.
-
Finally, interpret the results and draw conclusions about the stationarity of the time series. If the null hypothesis is rejected, then the time series is non-stationary and requires further pre-processing before it can be used for forecasting. If the null hypothesis is not rejected, then the time series is trend stationary and can be used for forecasting.
Notes
To estimate \(\sigma^2\) the Newey-West estimator is used. If lags is "legacy", the truncation lag parameter is set to \(int(12 \times (\frac{n}{100})^{\frac{1}{4}})\), as outlined in Schwert (1989). The p-values are interpolated from Table 1 of Kwiatkowski et al. (1992). If the computed statistic is outside the table of critical values, then a warning message is generated.
Missing values are not handled.
Credit
- All credit goes to the
statsmodelslibrary.
References
- Andrews, D.W.K. (1991). Heteroskedasticity and autocorrelation consistent covariance matrix estimation. Econometrica, 59: 817-858.
- Hobijn, B., Frances, B.H., & Ooms, M. (2004). Generalizations of the KPSS-test for stationarity. Statistica Neerlandica, 52: 483-502.
- Kwiatkowski, D., Phillips, P.C.B., Schmidt, P., & Shin, Y. (1992). Testing the null hypothesis of stationarity against the alternative of a unit root. Journal of Econometrics, 54: 159-178.
- Newey, W.K., & West, K.D. (1994). Automatic lag selection in covariance matrix estimation. Review of Economic Studies, 61: 631-653.
- Schwert, G. W. (1989). Tests for unit roots: A Monte Carlo investigation. Journal of Business and Economic Statistics, 7 (2): 147-159.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
rur
🔗
rur(
x: ArrayLike, *, store: Literal[True]
) -> tuple[float, float, dict, ResultsStore]
rur(
x: ArrayLike, *, store: Literal[False] = False
) -> tuple[float, float, dict]
rur(x: ArrayLike, *, store: bool = False) -> Union[
tuple[float, float, dict, ResultsStore],
tuple[float, float, dict],
]
Summary
Computes the Range Unit-Root (RUR) test for the null hypothesis that x is stationary.
Details
The Range Unit-Root (RUR) test is a statistical test used to determine whether a time series is stationary or not. It is based on the range of the time series and does not require any knowledge of the underlying stochastic process.
The RUR test involves dividing the time series into non-overlapping windows of a fixed size and calculating the range of each window. Then, the range of the entire time series is calculated. If the time series is stationary, the range of the entire time series should be proportional to the square root of the window size. If the time series is non-stationary, the range of the entire time series will grow with the window size.
The null hypothesis of the RUR test is that the time series is non-stationary (unit root). The alternative hypothesis is that the time series is stationary. If the test statistic is less than a critical value at a given significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is stationary. If the test statistic is greater than the critical value, then we fail to reject the null hypothesis and conclude that the time series is non-stationary.
In practical terms, if a time series is found to be non-stationary by the RUR test, one can apply differencing to the time series until it becomes stationary. This involves taking the difference between consecutive observations and potentially repeating this process until the time series is stationary.
The RUR test is a simple and computationally efficient test for stationarity, but it may not be as powerful as other unit root tests in detecting non-stationarity in some cases. It is important to use multiple tests to determine the stationarity of a time series, as no single test is perfect in all situations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data series to test. |
required |
store
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float, dict, ResultsStore], tuple[float, float, dict]]
|
Returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 2: Trend-Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 3: Seasonal Series | |
|---|---|
1 2 3 4 5 | |
| Example 4: Real-World Time Series | |
|---|---|
1 2 3 4 5 | |
Calculation
The mathematical equation for the RUR test is:
where:
- \(y_t\) is the value of the time series at time \(t\).
- \(\rho\) is the parameter of the unit root process.
- \(y_{t-1}\) is the value of the time series at time \(t-1\).
- \(\epsilon_t\) is a stationary error term with mean zero and constant variance.
The null hypothesis of the RUR test is that the time series is stationary, and the alternative hypothesis is that the time series is non-stationary with a unit root.
Here are the detailed steps for how to calculate the RUR test:
-
Collect your time series data and plot it to visually check for any trends, seasonal patterns, or other patterns that could make the data non-stationary. If you detect any such patterns, you will need to pre-process your data (e.g., detrending, deseasonalizing, etc.) to remove these effects.
-
Estimate the parameter \(\rho\) using the ordinary least squares method. This involves regressing \(y_t\) on \(y_{t-1}\). The estimated equation is:
\[ y_t = \alpha + \rho y_{t-1} + \epsilon_t \]where:
- \(\alpha\) is the intercept.
- \(\epsilon_t\) is the error term.
-
Calculate the range of the time series, which is the difference between the maximum and minimum values of the time series:
\[ R = \max(y_t) - \min(y_t) \] -
Calculate the expected range of the time series under the null hypothesis of stationarity, which is given by:
\[ E(R) = \frac {T - 1} {2 \sqrt{T}} \]where:
- \(T\) is the sample size.
-
Calculate the test statistic, which is given by:
\[ RUR = \frac {R - E(R)} {E(R)} \] -
Compare the test statistic to the critical values in the RUR distribution table to determine the level of significance. The critical values depend on the sample size and the level of significance.
-
Finally, interpret the results and draw conclusions about the stationarity of the time series. If the null hypothesis is rejected, then the time series is non-stationary with a unit root. If the null hypothesis is not rejected, then the time series is stationary.
In practice, the RUR test is often conducted using software packages such as R, Python, or MATLAB, which automate the estimation of parameters and calculation of the test statistic.
Notes
The p-values are interpolated from Table 1 of Aparicio et al. (2006). If the computed statistic is outside the table of critical values, then a warning message is generated.
Missing values are not handled.
Credit
- All credit goes to the
statsmodelslibrary.
References
- Aparicio, F., Escribano A., Sipols, A.E. (2006). Range Unit-Root (RUR) tests: robust against nonlinearities, error distributions, structural breaks and outliers. Journal of Time Series Analysis, 27 (4): 545-576.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | |
za
🔗
za(
x: ArrayLike,
trim: float = 0.15,
maxlag: Optional[int] = None,
regression: VALID_ZA_REGRESSION_OPTIONS = "c",
autolag: Optional[VALID_ZA_AUTOLAG_OPTIONS] = "AIC",
) -> tuple[float, float, dict, int, int]
Summary
The Zivot-Andrews (ZA) test tests for a unit root in a univariate process in the presence of serial correlation and a single structural break.
Details
The Zivot-Andrews (ZA) test is a statistical test used to determine whether a time series is stationary or not in the presence of structural breaks. Structural breaks refer to significant changes in the underlying stochastic process of the time series, which can cause non-stationarity.
The ZA test involves running a regression of the time series on a constant and a linear time trend, and testing whether the residuals of the regression are stationary or not. The null hypothesis of the test is that the time series is stationary with a single break point, while the alternative hypothesis is that the time series is non-stationary with a single break point.
The test statistic is calculated by first estimating the break point using a likelihood ratio test. Then, the test statistic is calculated based on the estimated break point and the residuals of the regression. If the test statistic is greater than a critical value at a given significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is non-stationary with a structural break. If the test statistic is less than the critical value, then we fail to reject the null hypothesis and conclude that the time series is stationary with a structural break.
In practical terms, if a time series is found to be non-stationary with a structural break by the ZA test, one can apply methods to account for the structural break, such as including dummy variables in the regression or using time series models that allow for structural breaks.
Overall, the ZA test is a useful tool in time series analysis and forecasting when there is a suspicion of structural breaks in the data. However, it is important to note that the test may not detect multiple break points or breaks that are not well-separated in time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data series to test. |
required |
trim
|
float
|
The percentage of series at begin/end to exclude.
Default: |
0.15
|
maxlag
|
Optional[int]
|
The maximum lag which is included in test.
Default: |
None
|
regression
|
VALID_ZA_REGRESSION_OPTIONS
|
Constant and trend order to include in regression.
Default: |
'c'
|
autolag
|
Optional[VALID_ZA_AUTOLAG_OPTIONS]
|
The method to select the lag length.
Default: |
'AIC'
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, dict, int, int]
|
Returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 2: Noisy Series | |
|---|---|
1 2 3 4 5 | |
| Example 3: Real-World Time Series | |
|---|---|
1 2 3 4 5 | |
Calculation
The mathematical equation for the Zivot-Andrews test is:
where:
- \(y_t\) is the value of the time series at time \(t\).
- \(\alpha\) is the intercept.
- \(\beta\) is the slope coefficient of the time trend.
- \(\gamma\) is the coefficient of the lagged dependent variable.
- \(D_t\) is a dummy variable that takes a value of 1 after the suspected structural break point, and 0 otherwise.
- \(\delta_1\) and \(\delta_2\) are the coefficients of the dummy variable and the interaction term of the dummy variable and time trend, respectively.
- \(\epsilon_t\) is a stationary error term with mean zero and constant variance.
The null hypothesis of the Zivot-Andrews test is that the time series is non-stationary, and the alternative hypothesis is that the time series is stationary with a single structural break.
Here are the detailed steps for how to calculate the Zivot-Andrews test:
-
Collect your time series data and plot it to visually check for any trends, seasonal patterns, or other patterns that could make the data non-stationary. If you detect any such patterns, you will need to pre-process your data (e.g., detrending, deseasonalizing, etc.) to remove these effects.
-
Estimate the parameters of the model using the least squares method. This involves regressing \(y_t\) on \(t\), \(y_{t-1}\), \(D_t\), and \(t D_t\). The estimated equation is:
\[ y_t = \alpha + \beta t + \gamma y_{t-1} + \delta_1 D_t + \delta_2 t D_t + \epsilon_t \] -
Perform a unit root test on the residuals to check for stationarity. The most commonly used unit root tests for this purpose are the Augmented Dickey-Fuller (ADF) test and the Phillips-Perron (PP) test.
-
Calculate the test statistic, which is based on the largest root of the following equation:
\[ \Delta y_t = \alpha + \beta t + \gamma y_{t-1} + \delta_1 D_t + \delta_2 t D_t + \epsilon_t \]where:
- \(\Delta\) is the first difference operator.
-
Determine the critical values of the test statistic from the Zivot-Andrews distribution table. The critical values depend on the sample size, the level of significance, and the number of lagged dependent variables in the model.
-
Finally, interpret the results and draw conclusions about the stationarity of the time series. If the null hypothesis is rejected, then the time series is stationary with a structural break. If the null hypothesis is not rejected, then the time series is non-stationary and may require further processing to make it stationary.
In practice, the Zivot-Andrews test is often conducted using software packages such as R, Python, or MATLAB, which automate the estimation of parameters and calculation of the test statistic.
Notes
H0 = unit root with a single structural break
Algorithm follows Baum (2004/2015) approximation to original Zivot-Andrews method. Rather than performing an autolag regression at each candidate break period (as per the original paper), a single autolag regression is run up-front on the base model (constant + trend with no dummies) to determine the best lag length. This lag length is then used for all subsequent break-period regressions. This results in significant run time reduction but also slightly more pessimistic test statistics than the original Zivot-Andrews method, although no attempt has been made to characterize the size/power trade-off.
Credit
- All credit goes to the
statsmodelslibrary.
References
- Baum, C.F. (2004). ZANDREWS: Stata module to calculate Zivot-Andrews unit root test in presence of structural break," Statistical Software Components S437301, Boston College Department of Economics, revised 2015.
- Schwert, G.W. (1989). Tests for unit roots: A Monte Carlo investigation. Journal of Business & Economic Statistics, 7: 147-159.
- Zivot, E., and Andrews, D.W.K. (1992). Further evidence on the great crash, the oil-price shock, and the unit-root hypothesis. Journal of Business & Economic Studies, 10: 251-270.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 | |
pp
🔗
pp(
x: ArrayLike,
lags: Optional[int] = None,
trend: VALID_PP_TREND_OPTIONS = "c",
test_type: VALID_PP_TEST_TYPE_OPTIONS = "tau",
) -> tuple[float, float, int, dict]
Summary
Conduct a Phillips-Perron (PP) test for stationarity.
In statistics, the Phillips-Perron test (named after Peter C. B. Phillips and Pierre Perron) is a unit root test. It is used in time series analysis to test the null hypothesis that a time series is integrated of order \(1\). It builds on the Dickey-Fuller test of the null hypothesis \(p=0\).
Details
The Phillips-Perron (PP) test is a statistical test used to determine whether a time series is stationary or not. It is similar to the Augmented Dickey-Fuller (ADF) test, but it has some advantages, especially in the presence of autocorrelation and heteroscedasticity.
The PP test involves regressing the time series on a constant and a linear time trend, and testing whether the residuals of the regression are stationary or not. The null hypothesis of the test is that the time series is non-stationary, while the alternative hypothesis is that the time series is stationary.
The test statistic is calculated by taking the sum of the squared residuals of the regression, which is adjusted for autocorrelation and heteroscedasticity. The PP test also accounts for the bias in the standard errors of the test statistic, which can lead to incorrect inference in small samples.
If the test statistic is less than a critical value at a given significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is stationary. If the test statistic is greater than the critical value, then we fail to reject the null hypothesis and conclude that the time series is non-stationary.
In practical terms, if a time series is found to be non-stationary by the PP test, one can apply differencing to the time series until it becomes stationary. This involves taking the difference between consecutive observations and potentially repeating this process until the time series is stationary.
Overall, the PP test is a powerful and robust test for stationarity, and it is widely used in time series analysis and forecasting. However, it is important to use multiple tests and diagnostic tools to determine the stationarity of a time series, as no single test is perfect in all situations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
The data series to test. |
required |
lags
|
Optional[int]
|
The number of lags to use in the Newey-West estimator of the variance. If omitted or |
None
|
trend
|
VALID_PP_TREND_OPTIONS
|
The trend component to include in the test.
Defaults to |
'c'
|
test_type
|
VALID_PP_TEST_TYPE_OPTIONS
|
The type of test statistic to compute:
Defaults to |
'tau'
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, int, dict]
|
Returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 2: Trend-Stationary Series | |
|---|---|
1 2 3 | |
| Example 3: Seasonal Series | |
|---|---|
1 2 3 4 5 | |
| Example 4: Real-World Time Series | |
|---|---|
1 2 3 4 5 | |
| Example 5: PP test with excessive lags (coverage check) | |
|---|---|
1 2 3 4 5 6 | |
Calculation
The Phillips-Perron (PP) test is a commonly used test for stationarity in time series forecasting. The mathematical equation for the PP test is:
where:
- \(y_t\) is the value of the time series at time \(t\).
- \(\delta\) is a constant term.
- \(\pi\) is a coefficient that captures the trend in the data.
- \(\rho\) is a coefficient that captures the autocorrelation in the data.
- \(y_{t-1}\) is the lagged value of the time series at time \(t-1\).
- \(\epsilon_t\) is a stationary error term with mean zero and constant variance.
The PP test is based on the idea that if the time series is stationary, then the coefficient \(\rho\) should be equal to zero. Therefore, the null hypothesis of the PP test is that the time series is stationary, and the alternative hypothesis is that the time series is non-stationary with a non-zero value of \(\rho\).
Here are the detailed steps for how to calculate the PP test:
-
Collect your time series data and plot it to visually check for any trends, seasonal patterns, or other patterns that could make the data non-stationary. If you detect any such patterns, you will need to pre-process your data (e.g., detrending, deseasonalizing, etc.) to remove these effects.
-
Estimate the regression model by regressing \(y_t\) on a constant, a linear trend, and the lagged value of \(y_{t-1}\). The regression equation is:
\[ y_t = \delta + \pi t + \rho y_{t-1} + \epsilon_t \] -
Calculate the test statistic, which is based on the following equation:
\[ z = \left( T^{-\frac{1}{2}} \right) \times \left( \sum_{t=1}^T \left( y_t - \delta - \pi t - \rho y_{t-1} \right) - \left( \frac{1}{T} \right) \times \sum_{t=1}^T \sum_{s=1}^T K \left( \frac{s-t}{h} \right) (y_s - \delta - \pi s - \rho y_{s-1}) \right) \]where:
- \(T\) is the sample size.
- \(K(\dots)\) is the kernel function, which determines the weight of each observation in the smoothed series. The choice of the kernel function depends on the degree of serial correlation in the data. Typically, a Gaussian kernel or a Bartlett kernel is used.
- \(h\) is the bandwidth parameter, which controls the degree of smoothing of the series. The optimal value of \(h\) depends on the sample size and the noise level of the data.
-
Determine the critical values of the test statistic from the PP distribution table. The critical values depend on the sample size and the level of significance.
-
Finally, interpret the results and draw conclusions about the stationarity of the time series. If the null hypothesis is rejected, then the time series is non-stationary with a non-zero value of \(\rho\). If the null hypothesis is not rejected, then the time series is stationary.
In practice, the PP test is often conducted using software packages such as R, Python, or MATLAB, which automate the estimation of the regression model and calculation of the test statistic.
Notes
This test is generally used indirectly via the pmdarima.arima.ndiffs() function, which computes the differencing term, d.
The R code allows for two types of tests: 'Z(alpha)' and 'Z(t_alpha)'. Since sklearn does not allow extraction of std errors from the linear model fit, t_alpha is much more difficult to achieve, so we do not allow that variant.
Credit
- All credit goes to the
archlibrary.
References
- Phillips, P. C. B.; Perron, P. (1988). Testing for a Unit Root in Time Series Regression. Biometrika. 75 (2): 335-346.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 | |
ers
🔗
ers(
y: ArrayLike,
lags: Optional[int] = None,
trend: VALID_ERS_TREND_OPTIONS = "c",
max_lags: Optional[int] = None,
method: VALID_ERS_METHOD_OPTIONS = "aic",
low_memory: Optional[bool] = None,
) -> tuple[float, float, int, dict]
Summary
Elliott, Rothenberg and Stock's GLS detrended Dickey-Fuller.
Details
The Elliott-Rothenberg-Stock (ERS) test is a statistical test used to determine whether a time series is stationary or not. It is a robust test that is able to handle a wide range of non-stationary processes, including ones with structural breaks, heteroscedasticity, and autocorrelation.
The ERS test involves fitting a local-to-zero regression of the time series on a constant and a linear time trend, using a kernel function to weight the observations. The test statistic is then calculated based on the sum of the squared residuals of the local-to-zero regression, which is adjusted for the bandwidth of the kernel function and for the correlation of the residuals.
If the test statistic is less than a critical value at a given significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is stationary. If the test statistic is greater than the critical value, then we fail to reject the null hypothesis and conclude that the time series is non-stationary.
In practical terms, if a time series is found to be non-stationary by the ERS test, one can apply differencing to the time series until it becomes stationary. This involves taking the difference between consecutive observations and potentially repeating this process until the time series is stationary.
Overall, the ERS test is a powerful and flexible test for stationarity, and it is widely used in time series analysis and forecasting. However, it is important to use multiple tests and diagnostic tools to determine the stationarity of a time series, as no single test is perfect in all situations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ArrayLike
|
The data to test for a unit root. |
required |
lags
|
Optional[int]
|
The number of lags to use in the ADF regression. If omitted or |
None
|
trend
|
VALID_ERS_TREND_OPTIONS
|
The trend component to include in the test
Defaults to |
'c'
|
max_lags
|
Optional[int]
|
The maximum number of lags to use when selecting lag length. When using automatic lag length selection, the lag is selected using OLS detrending rather than GLS detrending. |
None
|
method
|
VALID_ERS_METHOD_OPTIONS
|
The method to use when selecting the lag length
Defaults to |
'aic'
|
low_memory
|
Optional[bool]
|
Flag indicating whether to use the low-memory algorithm for lag-length selection.
Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, int, dict]
|
Returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 | |
| Example 2: Noisy Series | |
|---|---|
1 2 3 4 5 | |
| Example 3: Real-World Time Series | |
|---|---|
1 2 3 4 5 | |
Calculation
The mathematical equation for the ERS test is:
where:
- \(y_t\) is the value of the time series at time \(t\).
- \(\mu_t\) is a time-varying mean function.
- \(\epsilon_t\) is a stationary error term with mean zero and constant variance.
The ERS test is based on the idea that if the time series is stationary, then the mean function should be a constant over time. Therefore, the null hypothesis of the ERS test is that the time series is non-stationary (unit root), and the alternative hypothesis is that the time series is stationary.
Here are the detailed steps for how to calculate the ERS test:
-
Collect your time series data and plot it to visually check for any trends, seasonal patterns, or other patterns that could make the data non-stationary. If you detect any such patterns, you will need to pre-process your data (e.g., detrending, deseasonalizing, etc.) to remove these effects.
-
Estimate the time-varying mean function using a local polynomial regression method. The choice of the polynomial degree depends on the complexity of the mean function and the sample size. Typically, a quadratic or cubic polynomial is used. The estimated mean function is denoted as \(\mu_t\).
-
Calculate the test statistic, which is based on the following equation:
\[ z = \left( \frac {T-1} {( \frac {1} {12\pi^2 \times \Delta^2} )} \right) ^{\frac{1}{2}} \times \left( \sum_{t=1}^T \frac {(y_t - \mu_t)^2} {T-1} \right) \]where:
- \(T\) is the sample size
- \(\Delta\) is the bandwidth parameter, which controls the degree of smoothing of the mean function. The optimal value of \(\Delta\) depends on the sample size and the noise level of the data.
- \(\pi\) is the constant pi.
-
Determine the critical values of the test statistic from the ERS distribution table. The critical values depend on the sample size and the level of significance.
-
Finally, interpret the results and draw conclusions about the stationarity of the time series. If the null hypothesis is rejected, then the time series is non-stationary with a time-varying mean function. If the null hypothesis is not rejected, then the time series is stationary.
In practice, the ERS test is often conducted using software packages such as R, Python, or MATLAB, which automate the estimation of the time-varying mean function and calculation of the test statistic.
Notes
The null hypothesis of the Dickey-Fuller GLS is that there is a unit root, with the alternative that there is no unit root. If the p-value is above a critical size, then the null cannot be rejected and the series appears to be a unit root.
DFGLS differs from the ADF test in that an initial GLS detrending step is used before a trend-less ADF regression is run.
Critical values and p-values when trend is "c" are identical to the ADF. When trend is set to "ct", they are from Elliott, Rothenberg, and Stock (1996).
Credit
- All credit goes to the
archlibrary.
References
- Elliott, G. R., T. J. Rothenberg, and J. H. Stock. 1996. Efficient bootstrap for an autoregressive unit root. Econometrica 64: 813-836.
- Perron, P., & Qu, Z. (2007). A simple modification to improve the finite sample properties of Ng and Perron’s unit root tests. Economics letters, 94(1), 12-19.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 | |
vr
🔗
vr(
y: ArrayLike,
lags: int = 2,
trend: VALID_VR_TREND_OPTIONS = "c",
debiased: bool = True,
robust: bool = True,
overlap: bool = True,
) -> tuple[float, float, float]
Summary
Variance Ratio test of a random walk.
Details
The Variance Ratio (VR) test is a statistical test used to determine whether a time series is stationary or not based on the presence of long-term dependence in the series. It is a non-parametric test that can be used to test for the presence of a unit root or a trend in the series.
The VR test involves calculating the ratio of the variance of the differences of the logarithms of the time series over different time intervals. The variance of the differences of the logarithms is a measure of the volatility of the series, and the ratio of the variances over different intervals is a measure of the long-term dependence in the series.
If the series is stationary, then the variance ratio will be close to one for all intervals. If the series is non-stationary, then the variance ratio will tend to increase as the length of the interval increases, reflecting the presence of long-term dependence in the series.
The VR test involves comparing the observed variance ratio to the distribution of variance ratios expected under the null hypothesis of a random walk (non-stationary). If the test statistic is less than a critical value at a given significance level, typically 0.05, then we reject the null hypothesis and conclude that the time series is stationary. If the test statistic is greater than the critical value, then we fail to reject the null hypothesis and conclude that the time series is non-stationary.
In practical terms, if a time series is found to be non-stationary by the VR test, one can apply differencing to the time series until it becomes stationary. This involves taking the difference between consecutive observations and potentially repeating this process until the time series is stationary.
Overall, the VR test is a useful and relatively simple test for stationarity that can be applied to a wide range of time series. However, it is important to use multiple tests and diagnostic tools to confirm the stationarity of a time series, as no single test is perfect in all situations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ArrayLike
|
The data to test for a random walk. |
required |
lags
|
int
|
The number of periods to used in the multi-period variance, which is the numerator of the test statistic. Must be at least 2. |
2
|
trend
|
VALID_VR_TREND_OPTIONS
|
|
'c'
|
debiased
|
bool
|
Indicates whether to use a debiased version of the test. Only applicable if |
True
|
robust
|
bool
|
Indicates whether to use heteroskedasticity robust inference. |
True
|
overlap
|
bool
|
Indicates whether to use all overlapping blocks. If |
True
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, float]
|
Returns a tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 | |
| Example 1: Stationary Series | |
|---|---|
1 2 3 4 5 6 7 | |
| Example 2: Noisy Series | |
|---|---|
1 2 3 4 5 6 7 | |
| Example 3: Seasonal Series | |
|---|---|
1 2 3 4 5 6 7 | |
| Example 4: Real-World Time Series | |
|---|---|
1 2 3 4 5 6 7 | |
Calculation
The Variance Ratio (VR) test is a statistical test for stationarity in time series forecasting that is based on the idea that if the time series is stationary, then the variance of the returns should be constant over time. The mathematical equation for the VR test is:
where:
- \(VR(k)\) is the variance ratio for the time series over \(k\) periods.
- \(\sigma^2(k)\) is the variance of the returns over \(k\) periods.
- \(\sigma^2(1)\) is the variance of the returns over \(1\) period.
The VR test involves comparing the variance ratio to a critical value, which is derived from the null distribution of the variance ratio under the assumption of a random walk with drift.
Here are the detailed steps for how to calculate the VR test:
-
Collect your time series data and compute the log returns, which are defined as:
\[ r_t = \log(y_t) - \log(y_{t-1}) \]where:
- \(y_t\) is the value of the time series at time \(t\).
-
Compute the variance of the returns over \(k\) periods, which is defined as:
\[ \sigma^2(k) = \left( \frac {1} {n-k} \right) \times \sum_{t=k+1}^n (r_t - \mu_k)^2 \]where:
- \(n\) is the sample size.
-
\(\mu_k\) is the mean of the returns over \(k\) periods, which is defined as:
\(\mu_k = \left( \frac{1} {n-k} \right) \times \sum_{t=k+1}^n r_t\)
-
Compute the variance of the returns over \(1\) period, which is defined as:
\[ \sigma^2(1) = \left( \frac{1} {n-1} \right) \times \sum_{t=2}^n (r_t - \mu_1)^2 \]where:
-
\(\mu_1\) is the mean of the returns over \(1\) period, which is defined as:
\(\mu_1 = \left( \frac{1} {n-1} \right) \times \sum_{t=2}^n r_t\)
-
-
Compute the variance ratio for each value of \(k\), which is defined as:
\[ VR(k) = \frac {\sigma^2(k)} {k\sigma^2(1)} \] -
Determine the critical values of the variance ratio from the null distribution table of the VR test, which depend on the sample size, the level of significance, and the lag length \(k\).
-
Finally, compare the variance ratio to the critical value. If the variance ratio is greater than the critical value, then the null hypothesis of a random walk with drift is rejected, and the time series is considered stationary. If the variance ratio is less than or equal to the critical value, then the null hypothesis cannot be rejected, and the time series is considered non-stationary.
In practice, the VR test is often conducted using software packages such as R, Python, or MATLAB, which automate the calculation of the variance ratio and the determination of the critical value.
Notes
The null hypothesis of a VR is that the process is a random walk, possibly plus drift. Rejection of the null with a positive test statistic indicates the presence of positive serial correlation in the time series.
Credit
- All credit goes to the
archlibrary.
References
- Campbell, John Y., Lo, Andrew W. and MacKinlay, A. Craig. (1997) The Econometrics of Financial Markets. Princeton, NJ: Princeton University Press.
See Also
statsmodels.tsa.stattools.adfuller: Augmented Dickey-Fuller unit root test.statsmodels.tsa.stattools.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.statsmodels.tsa.stattools.range_unit_root_test: Range Unit-Root test.statsmodels.tsa.stattools.zivot_andrews: Zivot-Andrews structural break test.pmdarima.arima.PPTest: Phillips-Perron unit root test.arch.unitroot.DFGLS: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller.arch.unitroot.VarianceRatio: Variance Ratio test of a random walk.ts_stat_tests.stationarity.algorithms.adf: Augmented Dickey-Fuller unit root test.ts_stat_tests.stationarity.algorithms.kpss: Kwiatkowski-Phillips-Schmidt-Shin stationarity test.ts_stat_tests.stationarity.algorithms.rur: Range Unit-Root test of stationarity.ts_stat_tests.stationarity.algorithms.za: Zivot-Andrews structural break unit root test.ts_stat_tests.stationarity.algorithms.pp: Phillips-Perron unit root test.ts_stat_tests.stationarity.algorithms.ers: Elliot, Rothenberg and Stock's GLS-detrended Dickey-Fuller test.ts_stat_tests.stationarity.algorithms.vr: Variance Ratio test of a random walk.
Source code in src/ts_stat_tests/stationarity/algorithms.py
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 | |