Skip to content

Commit f34caf6

Browse files
authored
Merge a47e28d into 77b7d00
2 parents 77b7d00 + a47e28d commit f34caf6

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

arch/tests/unitroot/test_unitroot.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,23 +647,27 @@ def test_nc_warning():
647647

648648

649649
@pytest.mark.parametrize("nobs", np.arange(1, 11).tolist())
650-
@pytest.mark.parametrize("stat", [ADF, PhillipsPerron])
650+
@pytest.mark.parametrize("stat", [ADF, PhillipsPerron, KPSS])
651651
@pytest.mark.parametrize("trend", ["n", "c", "ct", "ctt"])
652652
def test_wrong_exceptions(stat, nobs, trend):
653-
y = np.random.standard_normal((nobs,))
654-
if stat is PhillipsPerron and trend == "ctt":
653+
if (stat in (PhillipsPerron, KPSS) and trend == "ctt") or (
654+
stat is KPSS and trend == "n"
655+
):
655656
return
657+
y = np.random.standard_normal((nobs,))
656658
try:
657659
stat(y, trend=trend).stat
658660
except InfeasibleTestException:
659661
pass
660662

661663

662664
@pytest.mark.parametrize("nobs", [2, 10, 100])
663-
@pytest.mark.parametrize("stat", [ADF, PhillipsPerron])
665+
@pytest.mark.parametrize("stat", [ADF, PhillipsPerron, KPSS])
664666
@pytest.mark.parametrize("trend", ["n", "c", "ct", "ctt"])
665667
def test_wrong_exceptions_nearly_constant_series(stat, nobs, trend):
666-
if stat is PhillipsPerron and trend == "ctt":
668+
if (stat in (PhillipsPerron, KPSS) and trend == "ctt") or (
669+
stat is KPSS and trend == "n"
670+
):
667671
return
668672
y = np.zeros((nobs,))
669673
y[-1] = 1.0
@@ -679,6 +683,12 @@ def test_phillips_perron_specifed_lag():
679683
PhillipsPerron(y, lags=12).stat
680684

681685

686+
def test_kpss_legacy():
687+
y = np.random.standard_normal(4)
688+
with pytest.raises(InfeasibleTestException, match="The number of observations 4"):
689+
KPSS(y, lags=-1).stat
690+
691+
682692
@pytest.mark.parametrize(
683693
"x", [np.ones((2, 10)), np.full((20, 2), np.nan), np.ones((20, 2))]
684694
)

arch/unitroot/unitroot.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,16 @@ def __init__(
12611261
self._alternative_hypothesis = "The process contains a unit root."
12621262
self._resids: Optional[ArrayLike1D] = None
12631263

1264+
def _check_specification(self) -> None:
1265+
trend_order = len(self._trend)
1266+
lag_len = 0 if self._lags is None else self._lags
1267+
required = max(1 + trend_order, lag_len)
1268+
if self._y.shape[0] < (required):
1269+
raise InfeasibleTestException(
1270+
f"A minmum of {required} observations are needed to run an ADF with "
1271+
f"trend {self.trend} and the user-specified number of lags."
1272+
)
1273+
12641274
def _compute_statistic(self) -> None:
12651275
# 1. Estimate model with trend
12661276
nobs, y, trend = self._nobs, self._y, self._trend
@@ -1274,6 +1284,12 @@ def _compute_statistic(self) -> None:
12741284
else:
12751285
self._autolag()
12761286
assert self._lags is not None
1287+
if u.shape[0] < self._lags:
1288+
raise InfeasibleTestException(
1289+
f"The number of observations {u.shape[0]} is less than the number of"
1290+
f"lags in the long-run covariance estimator, {self._lags}. You must have "
1291+
"lags <= nobs."
1292+
)
12771293
lam = cov_nw(u, self._lags, demean=False)
12781294
s = cumsum(u)
12791295
self._stat = 1 / (nobs ** 2.0) * sum(s ** 2.0) / lam
@@ -1305,6 +1321,12 @@ def _autolag(self) -> None:
13051321
resids_prod /= self._nobs / 2
13061322
s0 += resids_prod
13071323
s1 += i * resids_prod
1324+
if s0 <= 0:
1325+
raise InfeasibleTestException(
1326+
f"Residuals are all zero and so automatic bandwidth selection cannot "
1327+
f"be used. This is usually an indication that the series being testes "
1328+
f"is too small or have constant values."
1329+
)
13081330
s_hat = s1 / s0
13091331
pwr = 1.0 / 3.0
13101332
gamma_hat = 1.1447 * power(s_hat * s_hat, pwr)

doc/source/changes/4.0.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Version 4
44

55
Since 4.12
66
==========
7-
- Improved exceptions in :class:`~arch.unitroot.ADF` and
8-
:class:`~arch.unitroot.PhillipsPerron` when test specification is infeasible
7+
- Improved exceptions in :class:`~arch.unitroot.ADF`, :class:`~arch.unitroot.KPSS`,
8+
and :class:`~arch.unitroot.PhillipsPerron` when test specification is infeasible
99
to the time series being too short or the required regression model being
1010
reduced rank (:issue:`364`).
1111
- Fixed a bug when using "bca" confidence intervals with ``extra_kwargs`` (:issue:`366`).

0 commit comments

Comments
 (0)