Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix MSTL where trend forecaster supports level #625

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions nbs/src/core/models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8976,7 +8976,7 @@
" res = self.trend_forecaster.predict(**kwargs)\n",
" seas = _predict_mstl_seas(self.model_, h=h, season_length=self.season_length)\n",
" res = {key: val + seas for key, val in res.items()}\n",
" if level is None:\n",
" if level is None or f'lo-{level[0]}' in res:\n",
" return res\n",
" level = sorted(level)\n",
" if self.trend_forecaster.prediction_intervals is not None:\n",
Expand Down Expand Up @@ -9067,7 +9067,7 @@
" key: val + (seas_insample if 'fitted' in key else seas_h) \\\n",
" for key, val in res.items()\n",
" }\n",
" if level is None:\n",
" if level is None or f'lo-{level[0]}' in res:\n",
" return res\n",
" level = sorted(level)\n",
" if self.trend_forecaster.prediction_intervals is not None:\n",
Expand Down Expand Up @@ -9170,6 +9170,26 @@
" test_forward=test_forward)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"# intervals without conformal\n",
"# trend fcst supports level\n",
"mstl_ok = MSTL(season_length=12, trend_forecaster=ARIMA(order=(0, 1, 0)))\n",
"res_fp = pd.DataFrame(mstl_ok.fit(y=ap).predict(h=24, level=[80, 95]))\n",
"res_fc = pd.DataFrame(mstl_ok.forecast(y=ap, h=24, level=[80, 95]))\n",
"pd.testing.assert_frame_equal(res_fp, res_fc)\n",
"\n",
"# trend fcst doesn't support level\n",
"mstl_bad = MSTL(season_length=12, trend_forecaster=CrostonClassic())\n",
"test_fail(lambda: mstl_bad.fit(y=ap).predict(h=24, level=[80, 95]), contains='prediction_intervals')\n",
"test_fail(lambda: mstl_bad.forecast(y=ap, h=24, level=[80, 95]), contains='prediction_intervals')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
4 changes: 2 additions & 2 deletions statsforecast/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4937,7 +4937,7 @@ def predict(
res = self.trend_forecaster.predict(**kwargs)
seas = _predict_mstl_seas(self.model_, h=h, season_length=self.season_length)
res = {key: val + seas for key, val in res.items()}
if level is None:
if level is None or f'lo-{level[0]}' in res:
jmoralez marked this conversation as resolved.
Show resolved Hide resolved
return res
level = sorted(level)
if self.trend_forecaster.prediction_intervals is not None:
Expand Down Expand Up @@ -5022,7 +5022,7 @@ def forecast(
key: val + (seas_insample if "fitted" in key else seas_h)
for key, val in res.items()
}
if level is None:
if level is None or f'lo-{level[0]}' in res:
return res
level = sorted(level)
if self.trend_forecaster.prediction_intervals is not None:
Expand Down
Loading