Skip to content

Commit c7a2eb7

Browse files
authored
Merge pull request #831 from bashtage/fix-docs-adf
DOC: Fix documentation for ADF and related tests
2 parents f343c03 + 5ad6603 commit c7a2eb7

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

arch/tests/unitroot/test_unitroot.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,22 @@ def test_representations(trend):
432432
def test_unknown_method():
433433
rnd = np.random.RandomState(12345)
434434
y = np.cumsum(rnd.standard_normal(250))
435-
with pytest.raises(ValueError, match=r"Unknown method"):
435+
with pytest.raises(ValueError, match=r"method must be one"):
436436
assert np.isfinite(ADF(y, method="unknown").stat)
437+
with pytest.raises(TypeError, match=r"method must be a string"):
438+
assert np.isfinite(ADF(y, method=1).stat)
439+
440+
441+
@pytest.mark.parametrize("method", ["aic", "AIC", "BIC", "bic", "t-stat", "tstat", "t"])
442+
def test_acceptable_method(method):
443+
rnd = np.random.RandomState(12345)
444+
y = np.cumsum(rnd.standard_normal(250))
445+
used_method = ADF(y, method=method)._method
446+
if method in ("t-stat", "t", "tstat"):
447+
expected_method = "t-stat"
448+
else:
449+
expected_method = method.lower()
450+
assert used_method == expected_method
437451

438452

439453
def test_auto_low_memory():
@@ -593,12 +607,13 @@ def test_zivot_andrews(series_name):
593607
# Test results from package urca.ur.za (1.13-0)
594608
y = ZIVOT_ANDREWS_DATA[series_name].dropna()
595609
result = series[series_name]
610+
kwargs = {"method": result.method} if result.method is not None else {}
596611
za = ZivotAndrews(
597612
y,
598613
lags=result.lags,
599614
trend=result.trend,
600615
max_lags=result.max_lags,
601-
method=result.method,
616+
**kwargs,
602617
)
603618
assert_almost_equal(za.stat, result.stat, decimal=3)
604619
assert_almost_equal(za.pvalue, result.pvalue, decimal=3)

arch/unitroot/unitroot.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ def _select_best_ic(
187187
large_tstat = abs(tstat) >= stop
188188
lag = int(squeeze(argwhere(large_tstat)).max())
189189
icbest = float(tstat[lag])
190-
else:
191-
raise ValueError("Unknown method")
192190

193191
return icbest, lag
194192

@@ -548,6 +546,16 @@ def _compute_if_needed(self) -> None:
548546
self._check_specification()
549547
self._compute_statistic()
550548

549+
def _clean_method(self, method: str) -> Literal["aic", "bic", "t-stat"]:
550+
if not isinstance(method, str):
551+
raise TypeError("method must be a string")
552+
method = method.lower()
553+
if method in ("t", "tstat", "t-stat"):
554+
return "t-stat"
555+
elif method not in ("aic", "bic"):
556+
raise ValueError("method must be one of 'aic', 'bic' or 't-stat'")
557+
return method
558+
551559
@property
552560
def null_hypothesis(self) -> str:
553561
"""The null hypothesis"""
@@ -683,11 +691,11 @@ class ADF(UnitRootTest, metaclass=AbstractDocStringInheritor):
683691
684692
max_lags : int, optional
685693
The maximum number of lags to use when selecting lag length
686-
method : {"AIC", "BIC", "t-stat"}, optional
694+
method : {"aic", "bic", "t-stat"}, optional
687695
The method to use when selecting the lag length
688696
689-
- "AIC" - Select the minimum of the Akaike IC
690-
- "BIC" - Select the minimum of the Schwarz/Bayesian IC
697+
- "aic" - Select the minimum of the Akaike IC
698+
- "bic" - Select the minimum of the Schwarz/Bayesian IC
691699
- "t-stat" - Select the minimum of the Schwarz/Bayesian IC
692700
693701
low_memory : bool
@@ -763,7 +771,7 @@ def __init__(
763771
valid_trends = ("n", "c", "ct", "ctt")
764772
super().__init__(y, lags, trend, valid_trends)
765773
self._max_lags = max_lags
766-
self._method = method
774+
self._method = self._clean_method(method)
767775
self._test_name = "Augmented Dickey-Fuller"
768776
self._regression = None
769777
self._low_memory = bool(low_memory)
@@ -798,7 +806,7 @@ def _compute_statistic(self) -> None:
798806
y, trend, lags = self._y, self._trend, self._lags
799807
resols = _estimate_df_regression(y, cast("UnitRootTrend", trend), lags)
800808
self._regression = resols
801-
(self._stat, *_) = (stat, *_) = resols.tvalues
809+
self._stat, *_ = (stat, *_) = resols.tvalues
802810
self._nobs = int(resols.nobs)
803811
self._pvalue = mackinnonp(
804812
stat,
@@ -851,11 +859,11 @@ class DFGLS(UnitRootTest, metaclass=AbstractDocStringInheritor):
851859
The maximum number of lags to use when selecting lag length. When using
852860
automatic lag length selection, the lag is selected using OLS
853861
detrending rather than GLS detrending ([pq]_).
854-
method : {"AIC", "BIC", "t-stat"}, optional
862+
method : {"aic", "bic", "t-stat"}, optional
855863
The method to use when selecting the lag length
856864
857-
- "AIC" - Select the minimum of the Akaike IC
858-
- "BIC" - Select the minimum of the Schwarz/Bayesian IC
865+
- "aic" - Select the minimum of the Akaike IC
866+
- "bic" - Select the minimum of the Schwarz/Bayesian IC
859867
- "t-stat" - Select the minimum of the Schwarz/Bayesian IC
860868
861869
Notes
@@ -912,7 +920,7 @@ def __init__(
912920
valid_trends = ("c", "ct")
913921
super().__init__(y, lags, trend, valid_trends)
914922
self._max_lags = max_lags
915-
self._method = method
923+
self._method = self._clean_method(method)
916924
self._regression = None
917925
self._low_memory = low_memory
918926
if low_memory is None:
@@ -1403,11 +1411,11 @@ class ZivotAndrews(UnitRootTest, metaclass=AbstractDocStringInheritor):
14031411
calculation in range [0, 0.333] (default=0.15)
14041412
max_lags : int, optional
14051413
The maximum number of lags to use when selecting lag length
1406-
method : {"AIC", "BIC", "t-stat"}, optional
1414+
method : {"aic", "bic", "t-stat"}, optional
14071415
The method to use when selecting the lag length
14081416
1409-
- "AIC" - Select the minimum of the Akaike IC
1410-
- "BIC" - Select the minimum of the Schwarz/Bayesian IC
1417+
- "aic" - Select the minimum of the Akaike IC
1418+
- "bic" - Select the minimum of the Schwarz/Bayesian IC
14111419
- "t-stat" - Select the minimum of the Schwarz/Bayesian IC
14121420
14131421
Notes
@@ -1457,7 +1465,8 @@ def __init__(
14571465
raise ValueError("trim must be a float in range [0, 1/3]")
14581466
self._trim = trim
14591467
self._max_lags = max_lags
1460-
self._method = method
1468+
self._method = self._clean_method(method)
1469+
14611470
self._test_name = "Zivot-Andrews"
14621471
self._all_stats = full(self._y.shape[0], nan)
14631472
self._null_hypothesis = (

0 commit comments

Comments
 (0)