Introduction¶
All tests expect a 1-d series as the first input. The input can be any array that can squeeze into a 1-d array, a pandas Series or a pandas DataFrame that contains a single variable.
All tests share a common structure. The key elements are:
stat - Returns the test statistic
pvalue - Returns the p-value of the test statistic
lags - Returns the number of lags used in the model. It is set when the test is constructed, and in most tests it can be
Noneto trigger automatic selection.trend - Returns the trend used in the model. It is set when the test is constructed, and the properties of a test cannot be changed afterwards. Create a new test to use a different specification, e.g.,
ADF(y, trend='ct'). Supported trends vary by model, but include:‘n’: No constant
‘c’: Constant
‘ct’: Constant and time trend
‘ctt’: Constant, time trend and quadratic time trend
summary() - Returns a summary object that can be printed to get a formatted table
Basic Example¶
This basic example show the use of the Augmented-Dickey fuller to test whether the default premium, defined as the difference between the yields of large portfolios of BAA and AAA bonds. This example uses a constant and time trend.
import arch.data.default
from arch.unitroot import ADF
default_data = arch.data.default.load()
default = default_data['BAA'] - default_data['AAA']
default = default.loc['1919-01-01':'2014-01-01']
adf = ADF(default, trend='ct')
print(adf.summary())
which yields
Augmented Dickey-Fuller Results
=====================================
Test Statistic -3.448
P-value 0.045
Lags 21
-------------------------------------
Trend: Constant and Linear Time Trend
Critical Values: -3.97 (1%), -3.41 (5%), -3.13 (10%)
Null Hypothesis: The process contains a unit root.
Alternative Hypothesis: The process is weakly stationary.
Lag Length Selection¶
ADF, DFGLS and
ZivotAndrews include lagged differences of the series to
account for serial correlation in the errors. When lags is None, the number
of lags is selected from 0, 1, …, max_lags lags using the method. The
choices, which are not case sensitive, are:
"aic": minimize the Akaike information criterion (the default);"bic": minimize the Schwarz/Bayesian information criterion;"t-stat": use the longest lag with a t-statistic that is significant, an absolute value of at least 1.645 ("t"and"tstat"are accepted as aliases); and"maic": minimize the modified AIC of Ng and Perron (2001).
Ng and Perron showed that the information criteria tend to select too few lags in the presence of a large negative moving average root in the errors, which produces tests that reject a true unit root too often. Their modified AIC adds a term to the penalty that depends on the estimated coefficient on the lagged level of the series, \(\hat{\beta}_0\), which is larger in absolute value when the data look stationary,
\(\hat{\sigma}_k^2\) is the estimated variance of the errors using \(k\) lags, and \(\tilde{y}_{t-1}\) is the lagged level after the deterministic terms in the regression have been removed. All lag lengths use the same \(T - k_{max}\) observations. The modified AIC can select a different number of lags than the AIC and BIC.
In [1]: import numpy as np
In [2]: import arch.data.crude
In [3]: from arch.unitroot import ADF, DFGLS
In [4]: brent = np.log(arch.data.crude.load()["Brent"])
In [5]: for method in ("aic", "bic", "maic"):
...: adf = ADF(brent, trend="c", method=method)
...: print(f"{method:>4}: lags={adf.lags:2d} stat={adf.stat:0.3f} p={adf.pvalue:0.3f}")
...:
aic: lags= 1 stat=-1.655 p=0.458
bic: lags= 1 stat=-1.655 p=0.458
maic: lags= 6 stat=-1.286 p=0.640
In [6]: dfgls = DFGLS(brent, method="maic")
In [7]: print(f"DFGLS maic: lags={dfgls.lags} stat={dfgls.stat:0.3f}")
DFGLS maic: lags=6 stat=-0.516
Ng, S., & Perron, P. (2001). Lag length selection and the construction of unit root tests with good size and power. Econometrica, 69(6), 1519-1554.
Regressions with a Constant¶
The regressions of ADF,
PhillipsPerron and KPSS, and the
cointegrating regressions of the cointegration tests and estimators, are estimated with the dependent variable and the regressors
other than the constant centered on their means when the trend includes a constant.
Centering only changes the parametrization of the regression: the residuals and the
estimated coefficients of the other regressors, and their covariance, are
unchanged, and only the constant becomes the intercept at the means. The results
are then reported for the original variables. The test statistics, the estimates,
including the constant, and their standard errors, for example in regression of
ADF, are those of the regression on the original
regressors, up to rounding error.
Centering removes the collinearity between the constant and a regressor whose
variation is small compared with its level, such as the lagged level of a series
that varies little around a large mean. statsmodels decides the rank of a
regression from the singular values of its regressors, and from version 0.15.1
treats a regression as rank-deficient when the smallest singular value is below
max(nobs, ncols) times the machine precision times the largest. Without
centering, the regression of a series equal to 5.3 plus noise with a standard
deviation of 1e-12 would be treated as rank-deficient, although the noise is more
than 1000 times larger than the rounding error of the series. The test would then
raise InfeasibleTestException.