Skip to content

Commit

Permalink
[BUG] statsforecast 1.6.0 compatibility - fix argument differences …
Browse files Browse the repository at this point in the history
…between `sktime` and `statsforecast` (#5393)

#### Reference Issues/PRs
Fixes #5295
Depends on #5317, merge after #5317


#### What does this implement/fix? Explain your changes.
Adds parameters `stl_kwargs` and `pred_int_kwargs`, keeping up with
`statsforecast` new release

#### Did you add any tests for the change?
Added test params to `StatsForecastMSTL`, all tests in `check_estimator`
pass
  • Loading branch information
luca-miniati committed Oct 9, 2023
1 parent d8f9ecc commit c1fbe65
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sktime/forecasting/base/adapters/_generalised_statsforecast.py
Expand Up @@ -447,10 +447,17 @@ def __init__(self, estimator):
super().__init__()

self.estimator = estimator
self.prediction_intervals = None

def __repr__(self):
return "StatsForecastBackAdapter"

def new(self):
"""Make new instance of back-adapter."""
_self = type(self).__new__(type(self))
_self.__dict__.update(self.__dict__)
return _self

def fit(self, y, X=None):
"""Fit to training data.
Expand Down Expand Up @@ -489,6 +496,12 @@ def predict(self, h, X=None, level=None):
mean = self.estimator.predict(fh=range(1, h + 1), X=X)[:, 0]
if level is None:
return {"mean": mean}
# if a level is passed, and if prediction_intervals has not been instantiated
# yet
elif self.prediction_intervals is None:
from statsforecast.utils import ConformalIntervals

self.prediction_intervals = ConformalIntervals(h=h)

level = sorted(level)
coverage = [round(1 - (_l / 100), 2) for _l in level]
Expand Down
24 changes: 24 additions & 0 deletions sktime/forecasting/statsforecast.py
Expand Up @@ -595,6 +595,12 @@ class StatsForecastMSTL(_GeneralisedStatsForecastAdapter):
trend_forecaster : estimator, optional, default=StatsForecastAutoETS()
Sktime estimator used to make univariate forecasts. Multivariate estimators are
not supported.
stl_kwargs : dict, optional
Extra arguments to pass to [`statsmodels.tsa.seasonal.STL`]
(https://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.STL.html#statsmodels.tsa.seasonal.STL).
The `period` and `seasonal` arguments are reserved.
pred_int_kwargs : dict, optional
Extra arguments to pass to [`statsforecast.utils.ConformalIntervals`].
References
----------
Expand Down Expand Up @@ -623,6 +629,8 @@ def __init__(
self,
season_length: Union[int, List[int]],
trend_forecaster=None,
stl_kwargs: Optional[Dict] = None,
pred_int_kwargs: Optional[Dict] = None,
):
super().__init__()

Expand All @@ -634,6 +642,8 @@ def __init__(
self._trend_forecaster = clone(trend_forecaster)
else:
self._trend_forecaster = StatsForecastAutoETS(model="ZZN")
self.stl_kwargs = stl_kwargs
self.pred_int_kwargs = pred_int_kwargs

# checks if trend_forecaster is already wrapped with
# StatsForecastBackAdapter
Expand All @@ -650,6 +660,14 @@ def __init__(
"forecaster."
)

# check if prediction interval kwargs are passed
if self.pred_int_kwargs:
from statsforecast.utils import ConformalIntervals

self._trend_forecaster.prediction_intervals = ConformalIntervals(
**self.pred_int_kwargs
)

def _get_statsforecast_class(self):
from statsforecast.models import MSTL

Expand Down Expand Up @@ -695,6 +713,12 @@ def get_test_params(cls, parameter_set="default"):
{
"season_length": 4,
},
{
"season_length": 4,
"pred_int_kwargs": {
"n_windows": 2,
},
},
]
except ModuleNotFoundError:
from sktime.forecasting.naive import NaiveForecaster
Expand Down

0 comments on commit c1fbe65

Please sign in to comment.