Nixtla / statsforecast

Lightning ⚡️ fast forecasting with statistical and econometric models.
https://nixtlaverse.nixtla.io/statsforecast
Apache License 2.0
3.98k stars 282 forks source link

Exception: no model able to be fitted Error on AutoARIMA #182

Closed shravankoninti closed 2 years ago

shravankoninti commented 2 years ago

I am trying to solve a timeseries problem with intermittent zero demand in the timeframe(Monthly data). I am getting this warning/error.

/opt/conda/lib/python3.7/site-packages/statsforecast/arima.py:866: RuntimeWarning: divide by zero encountered in log return 0.5 * np.log(res) /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:443: RuntimeWarning: divide by zero encountered in double_scalars l0 = l0 / b0 /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:443: RuntimeWarning: divide by zero encountered in double_scalars l0 = l0 / b0 /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:448: RuntimeWarning: invalid value encountered in float_scalars b0 = max(y_sa[1] / y_sa[0], 1e-3) /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:448: RuntimeWarning: invalid value encountered in float_scalars b0 = max(y_sa[1] / y_sa[0], 1e-3) /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:443: RuntimeWarning: divide by zero encountered in double_scalars l0 = l0 / b0 /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:448: RuntimeWarning: invalid value encountered in float_scalars b0 = max(y_sa[1] / y_sa[0], 1e-3) /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:443: RuntimeWarning: divide by zero encountered in double_scalars l0 = l0 / b0 /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:448: RuntimeWarning: invalid value encountered in float_scalars b0 = max(y_sa[1] / y_sa[0], 1e-3) /opt/conda/lib/python3.7/site-packages/statsforecast/ets.py:448: RuntimeWarning: invalid value encountered in double_scalars b0 = max(y_sa[1] / y_sa[0], 1e-3)

and throws an error showing

Exception: no model able to be fitted

Any thoughts on how this can be resolved? How can I use your package for this?

Regards Shravan

shravankoninti commented 2 years ago

I am getting similar errors even if I run all other models in the package like for e.g.# (auto_arima, season_length),

(ets, season_length, 'ZMZ'),

(seasonal_naive, season_length), naive,

                      croston_classic, croston_sba, croston_optimized
colorado-mike commented 2 years ago

Also experiencing this issue while executing an ETS model. Any suggestions?

AzulGarza commented 2 years ago

Hi @shravankoninti, @colorado-mike!

Usually, models such as AutoARIMA and ETS tend to have problems with intermittent data (mainly because of the zeros associated with this type of data).

A preprocessing step usually works is adding a constant to the target variable to avoid zeros and removing it in the forecasts. For example,

constant = 10
# add constant to avoid errors
Y_df['y'] += constant

fcst = StatsForecast(df=df, models=[AutoARIMA(), ETS()])
Y_hat_df = fcst.forecast(h=12)
#remove constant
Y_hat_df[['AutoARIMA', 'ETS']] -= constant

Note that this preprocessing step is only valid for models such as ETS and AutoARIMA. Intermittent models such as Croston and ADIDA require not altering the underlying intermittency.

colorado-mike commented 2 years ago

@FedericoGarza - Thank you! Looks like this solved my issue!

AzulGarza commented 2 years ago

Nice @colorado-mike.

I'm closing the issue. Feel free to reopen it if necessary.

iamyihwa commented 1 year ago

Sure @FedericoGarza


data = {'unique_id':   [1]*139, 
         'ds': pd.date_range(start = '2019-01-01', end = '2021-09-01', freq = 'W'),
         'y':  np.array([0.0]*4 + [19.68] + [0.0]*134) } 
test_df = pd.DataFrame(data)

fcst = StatsForecast(df = test_df, 
                    models=[ AutoARIMA(season_length = season_length)
                    ], 
                    freq= cur_freq, 
                    n_jobs=-1)
Y_hat_df = fcst.forecast(h=horizon_length, fitted=True)

This is a data with a single value present and the rest 0. I get the error when I forecast with this data.

/anaconda/envs/jupyter_env/lib/python3.8/site-packages/statsforecast/arima.py:896: RuntimeWarning:

divide by zero encountered in log


ZeroDivisionError Traceback (most recent call last) Cell In[82], line 11 4 test_df = pd.DataFrame(data) 6 fcst = StatsForecast(df = test_df, 7 models=[ AutoARIMA(season_length = season_length) 8 ], 9 freq= cur_freq, 10 n_jobs=-1) ---> 11 Y_hat_df = fcst.forecast(h=horizon_length, fitted=True)

File /anaconda/envs/jupyter_env/lib/python3.8/site-packages/statsforecast/core.py:1486, in StatsForecast.forecast(self, h, df, X_df, level, fitted, sort_df) 1476 def forecast( 1477 self, 1478 h: int, (...) 1483 sort_df: bool = True, 1484 ): 1485 if self._is_native(df=df): -> 1486 return super().forecast( 1487 h=h, df=df, X_df=X_df, level=level, fitted=fitted, sort_df=sort_df 1488 ) 1489 assert df is not None 1490 with fa.engine_context(infer_by=[df]) as e:

File /anaconda/envs/jupyter_env/lib/python3.8/site-packages/statsforecast/core.py:741, in _StatsForecast.forecast(self, h, df, X_df, level, fitted, sort_df) 739 X, level = self._parse_X_level(h=h, X=X_df, level=level) 740 if self.n_jobs == 1: --> 741 res_fcsts = self.ga.forecast( 742 models=self.models, 743 h=h, 744 fallback_model=self.fallback_model, 745 fitted=fitted, 746 X=X, 747 level=level, 748 verbose=self.verbose, 749 ) 750 else: 751 res_fcsts = self._forecast_parallel(h=h, fitted=fitted, X=X, level=level)

File /anaconda/envs/jupyter_env/lib/python3.8/site-packages/statsforecast/core.py:203, in GroupedArray.forecast(self, models, h, fallback_model, fitted, X, level, verbose) 194 res_i = fallback_model.forecast( 195 h=h, 196 y=y_train, (...) 200 **kwargs, 201 ) 202 else: --> 203 raise error 204 cols_m = [ 205 key 206 for key in res_i.keys() 207 if any(key.startswith(m) for m in matches) 208 ] 209 fcsts_i = np.vstack([res_i[key] for key in cols_m]).T

File /anaconda/envs/jupyter_env/lib/python3.8/site-packages/statsforecast/core.py:189, in GroupedArray.forecast(self, models, h, fallback_model, fitted, X, level, verbose) 187 kwargs["level"] = level 188 try: --> 189 res_i = model.forecast( 190 h=h, y=y_train, X=X_train, X_future=X_f, fitted=fitted, **kwargs 191 ) 192 except Exception as error: 193 if fallback_model is not None:

File /anaconda/envs/jupyter_env/lib/python3.8/site-packages/statsforecast/models.py:374, in AutoARIMA.forecast(self, y, h, X, Xfuture, level, fitted) 347 """Memory Efficient AutoARIMA predictions. 348 349 This method avoids memory burden due from object storage. (...) 371 Dictionary with entries mean for point predictions and `level*` for probabilistic predictions. 372 """ 373 with np.errstate(invalid="ignore"): --> 374 mod = auto_arima_f( 375 x=y, 376 d=self.d, ...

AzulGarza commented 1 year ago

Thank you, @iamyihwa! I've included your example in #434 to follow the issue there. :)