I am using StatsForecast library. I am using combination of models to fit the forecast. My data is sparse (demand is zero for most of the values) and I want to use sparse model which is there in statsforecast (https://github.com/Nixtla/statsforecast)
The issue with these models are, insample forecast is not available for these methods. It is available for AutoArima and Naive Method. The below code is working fine for AutoARIMA and Naive Model
sf = StatsForecast(df=df_train_sf,
models=[AutoARIMA(season_length = 52),
Naive(),
],
freq='W')
y_pred = sf.forecast(
X_df = df_val_sf.drop('y', axis=1),
fitted = True,
h=h)
insample_forecasts = sf.forecast_fitted_values().reset_index()
insample_forecasts.head(1)
But if I am using model such as CrostonClassic(), IMAPA(),ADIDA() then insample forecasting is not available.
sf = StatsForecast(df=df_train_sf,
models=[CrostonClassic(),
IMAPA(),
ADIDA(),
],
freq='W')
y_pred = sf.forecast(
X_df = df_val_sf.drop('y', axis=1),
fitted = True,
h=h)
Please let me know how to resolve this? One Hack is to use the training data and forecast on the same data. But I am not sure that is doable in forecasting as for method like Naive, this is completely a wrong approach as last obs of training data will get forecasted for the entire training data.
I am looking for hack or use other alternative libraries with the same speed and methods implemented
I am pasting the error below in case you want to dig deeper.
NotImplementedError Traceback (most recent call
last) Cell In[170], line 12 2 df_val_sf = df_val_sf[df_val_sf.unique_id=='290~1-290~112146371'] 4 sf = StatsForecast(df=df_train_sf, 5 models=[CrostonClassic(), 6 IMAPA(), 7 ADIDA(), 8 ], 9 freq='W') ---> 12 y_pred = sf.forecast( 13 X_df = df_val_sf.drop('y', axis=1), 14 fitted = True, 15 h=h)
File /opt/conda/envs/tensorflow/lib/python3.10/site-packages/statsforecast/core.py:1899, in StatsForecast.forecast(self, h, df, X_df, level, fitted, sort_df, prediction_intervals) 1895 raise ValueError( 1896
"You must specifylevel
when usingprediction_intervals
" 1897
) 1898 if self._is_native(df=df): -> 1899 return super().forecast( 1900 h=h, 1901 df=df, 1902 X_df=X_df, 1903 level=level, 1904 fitted=fitted, 1905 sort_df=sort_df, 1906
prediction_intervals=prediction_intervals, 1907 ) 1908 assert df is not None 1909 engine = make_execution_engine(infer_by=[df])File /opt/conda/envs/tensorflow/lib/python3.10/site-packages/statsforecast/core.py:1087, in _StatsForecast.forecast(self, h, df, X_df, level, fitted, sort_df, prediction_intervals) 1085 X, level = self._parse_X_level(h=h, X=X_df, level=level) 1086 if self.n_jobs == 1: -> 1087 res_fcsts = self.ga.forecast( 1088 models=self.models, 1089 h=h, 1090
fallback_model=self.fallback_model, 1091 fitted=fitted,
1092 X=X, 1093 level=level, 1094
verbose=self.verbose, 1095 ) 1096 else: 1097
res_fcsts = self._forecast_parallel(h=h, fitted=fitted, X=X, level=level)File /opt/conda/envs/tensorflow/lib/python3.10/site-packages/statsforecast/core.py:207, in GroupedArray.forecast(self, models, h, fallback_model, fitted, X, level, verbose) 198 res_i = fallback_model.forecast( 199 h=h, 200 y=y_train, (...) 204 **kwargs, 205 ) 206 else: --> 207 raise error 208 cols_m = [ 209 key 210 for key in res_i.keys() 211 if any(key.startswith(m) for m in matches) 212 ] 213 fcsts_i = np.vstack([res_i[key] for key in cols_m]).T
File /opt/conda/envs/tensorflow/lib/python3.10/site-packages/statsforecast/core.py:193, in GroupedArray.forecast(self, models, h, fallback_model, fitted, X, level, verbose) 191 kwargs["level"] = level 192 try: --> 193 res_i = model.forecast( 194 h=h, y=y_train, X=X_train, X_future=X_f, fitted=fitted, **kwargs 195 ) 196 except Exception as error: 197 if fallback_model is not None:
File /opt/conda/envs/tensorflow/lib/python3.10/site-packages/statsforecast/models.py:4074, in CrostonClassic.forecast(self, y, h, X, X_future, level, fitted)
4039 def forecast( 4040 self, 4041 y: np.ndarray,
(...) 4046 fitted: bool = False, 4047 ): 4048
"""Memory Efficient CrostonClassic predictions. 4049 4050
This method avoids memory burden due from object storage. (...)
4072 Dictionary with entriesmean
for point predictions andlevel_*
for probabilistic predictions. 4073 """ -> 4074 res = _croston_classic(y=y, h=h, fitted=fitted) 4075 if level is None: 4076 return resFile /opt/conda/envs/tensorflow/lib/python3.10/site-packages/statsforecast/models.py:3915, in _croston_classic() 3908 @njit 3909 def _croston_classic(
3910 y: np.ndarray, # time series 3911 h: int, # forecasting horizon 3912 fitted: bool, # fitted values 3913 ): 3914 if fitted: -> 3915 raise NotImplementedError("return fitted") 3916 yd = _demand(y) 3917 yi = _intervals(y)NotImplementedError: return fitted