alkaline-ml / pmdarima

A statistical library designed to fill the void in Python's time series analysis capabilities, including the equivalent of R's auto.arima function.
https://www.alkaline-ml.com/pmdarima
MIT License
1.57k stars 231 forks source link

ValueError from `nsdiffs` but not from `OCSBTest().estimate_seasonal_differencing_term` #523

Open ftrifoglio opened 1 year ago

ftrifoglio commented 1 year ago

Describe the bug

I'm getting ValueError: zero-size array to reduction operation maximum which has no identity from nsdiffs with default test but not from OCSBTest().estimate_seasonal_differencing_term.

nsdiffs with ch test is also running fine.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [31], line 1
----> 1 nsdiffs(y, m=12)

File ~/lib/python3.8/site-packages/pmdarima/arima/utils.py:123, in nsdiffs(x, m, max_D, test, **kwargs)
    117         warnings.warn("Appropriate D value may not have been reached; "
    118                       "length of seasonally-differenced array (%i) is "
    119                       "shorter than m (%i). Using D=%i"
    120                       % (len(x), m, D))
    121         return D
--> 123     dodiff = testfunc(x)
    125 return D

File ~lib/python3.8/site-packages/pmdarima/arima/seasonality.py:597, in OCSBTest.estimate_seasonal_differencing_term(self, x)
    594 x = check_endog(x, dtype=DTYPE, preserve_series=False)
    596 # Get the critical value for m
--> 597 stat = self._compute_test_statistic(x)
    598 crit_val = self._calc_ocsb_crit_val(self.m)
    599 return int(stat > crit_val)

File ~lib/python3.8/site-packages/pmdarima/arima/seasonality.py:537, in OCSBTest._compute_test_statistic(self, x)
    535 for lag_term in range(1, maxlag + 1):  # 1 -> maxlag (incl)
    536     try:
--> 537         fit = self._fit_ocsb(x, m, lag_term, maxlag)
...
    280         umr_minimum(a, axis, None, None, keepdims),
    281         out
    282     )

ValueError: zero-size array to reduction operation maximum which has no identity

To Reproduce

from pmdarima.arima.utils import nsdiffs
import numpy as np
y = np.array([110000., 110000.,  74000.,  60500.,  60500.,  49500.,  60500.,
        60500.,  90500.,  74000.,  74000.,  90500.,  90500.,  74000.,
        74000.,  49500.,  49500.,  40500.,  49500.,  49500.,  74000.,
        60500.,  90500.,  90500.,  90500.,  90500.])
nsdiffs(y, m=12)
# ValueError: zero-size array to reduction operation maximum which has no identity
from pmdarima.arima.seasonality import OCSBTest
OCSBTest(m=12).estimate_seasonal_differencing_term(y)
# 1

Versions

System:
    python: 3.8.13 (default, Oct  8 2022, 22:34:43)  [Clang 13.0.0 (clang-1300.0.27.3)]
executable: /bin/python
   machine: macOS-12.3.1-arm64-i386-64bit

Python dependencies:
        pip: 22.2.2
 setuptools: 65.4.1
    sklearn: 1.1.2
statsmodels: 0.13.2
      numpy: 1.23.4
      scipy: 1.9.2
     Cython: 0.29.32
     pandas: 1.5.0
     joblib: 1.2.0
   pmdarima: 2.0.1
macOS-12.3.1-arm64-i386-64bit
Python 3.8.13 (default, Oct  8 2022, 22:34:43) 
[Clang 13.0.0 (clang-1300.0.27.3)]
pmdarima 2.0.1
NumPy 1.23.4
SciPy 1.9.2
Scikit-Learn 1.1.2
Statsmodels 0.13.2

Expected Behavior

No Value Error

Actual Behavior

Raise Value Error

Additional Context

No response