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

feat: Forward method TBATS/AutoTBATS #778

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Automatic forecasting tools search for the best parameters and select the best p
|[AutoETS](https://nixtla.github.io/statsforecast/src/core/models.html#autoets)|✅|✅|✅|✅||
|[AutoCES](https://nixtla.github.io/statsforecast/src/core/models.html#autoces)|✅|✅|✅|✅||
|[AutoTheta](https://nixtla.github.io/statsforecast/src/core/models.html#autotheta)|✅|✅|✅|✅||
|[AutoTBATS](https://nixtla.github.io/statsforecast/src/core/models.html#autotbats)|✅|✅|✅|✅||

### ARIMA Family
These models exploit the existing autocorrelations in the time series.
Expand All @@ -140,6 +141,7 @@ Suited for signals with more than one clear seasonality. Useful for low-frequenc
|Model | Point Forecast | Probabilistic Forecast | Insample fitted values | Probabilistic fitted values |Exogenous features|
|:------|:-------------:|:----------------------:|:---------------------:|:----------------------------:|:----------------:|
|[MSTL](https://nixtla.github.io/statsforecast/src/core/models.html#mstl)|✅|✅|✅|✅|If trend forecaster supports|
|[TBATS](https://nixtla.github.io/statsforecast/src/core/models.html#tbats)|✅|✅|✅|✅||

### GARCH and ARCH Models
Suited for modeling time series that exhibit non-constant volatility over time. The ARCH model is a particular case of GARCH.
Expand Down
88 changes: 82 additions & 6 deletions nbs/src/core/models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@
"from statsforecast.garch import (\n",
" garch_model, garch_forecast\n",
")\n",
"from statsforecast.tbats import tbats_selection, tbats_forecast, _compute_sigmah\n",
"from statsforecast.tbats import (\n",
" tbats_selection, \n",
" tbats_forecast, \n",
" _compute_sigmah, \n",
" forward_tbats\n",
")\n",
"from statsforecast.utils import (\n",
" _calculate_sigma,\n",
" _calculate_intervals,\n",
Expand Down Expand Up @@ -9953,7 +9958,60 @@
" res_trans = {k : inv_boxcox(v, mod['BoxCox_lambda']) for k, v in res.items()}\n",
" else:\n",
" res_trans = res\n",
" return res_trans "
" return res_trans \n",
" \n",
" def forward(\n",
" self,\n",
" y: np.ndarray,\n",
" h: int,\n",
" X: Optional[np.ndarray] = None,\n",
" X_future: Optional[np.ndarray] = None,\n",
" level: Optional[List[int]] = None,\n",
" fitted: bool = False,\n",
" ):\n",
" \"\"\"Apply fitted TBATS model to a new time series.\n",
"\n",
" Parameters\n",
" ----------\n",
" y : numpy.array \n",
" Clean time series of shape (n, ). \n",
" h : int \n",
" Forecast horizon.\n",
" X : array-like \n",
" Optional insample exogenpus of shape (t, n_x). \n",
" X_future : array-like \n",
" Optional exogenous of shape (h, n_x). \n",
" level : List[float] \n",
" Confidence levels for prediction intervals.\n",
" fitted : bool \n",
" Whether or not to return insample predictions.\n",
"\n",
" Returns\n",
" -------\n",
" forecasts : dict \n",
" Dictionary with entries `mean` for point predictions and `level_*` for probabilistic predictions.\n",
" \"\"\"\n",
" if not hasattr(self, 'model_'):\n",
" raise Exception('You have to use the `fit` method first')\n",
" mod = forward_tbats(self.model_, y)\n",
" fcst = tbats_forecast(mod, h)\n",
" res = {'mean': fcst['mean']}\n",
" if fitted:\n",
" res['fitted'] = mod['fitted'].ravel()\n",
" if level is not None:\n",
" level = sorted(level)\n",
" sigmah = _compute_sigmah(mod, h)\n",
" pred_int = _calculate_intervals(res, level, h, sigmah)\n",
" res = {**res, **pred_int}\n",
" if fitted:\n",
" se = _calculate_sigma(mod['errors'], mod['errors'].shape[1])\n",
" fitted_pred_int = _add_fitted_pi(res, se, level)\n",
" res = {**res, **fitted_pred_int}\n",
" if mod['BoxCox_lambda'] is not None:\n",
" res_trans = {k : inv_boxcox(v, mod['BoxCox_lambda']) for k, v in res.items()}\n",
" else:\n",
" res_trans = res\n",
" return res_trans"
]
},
{
Expand Down Expand Up @@ -10012,6 +10070,15 @@
"show_doc(TBATS.forecast, title_level=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"show_doc(TBATS.forward, title_level=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -10115,7 +10182,16 @@
"metadata": {},
"outputs": [],
"source": [
"show_doc(AutoTBATS.fit, title_level=3)"
"show_doc(AutoTBATS.fit, name='AutoTBATS.fit', title_level=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"show_doc(AutoTBATS.predict, name='AutoTBATS.predict', title_level=3)"
]
},
{
Expand All @@ -10124,7 +10200,7 @@
"metadata": {},
"outputs": [],
"source": [
"show_doc(AutoTBATS.predict, title_level=3)"
"show_doc(AutoTBATS.predict_in_sample, name='AutoTBATS.predict_in_sample', title_level=3)"
]
},
{
Expand All @@ -10133,7 +10209,7 @@
"metadata": {},
"outputs": [],
"source": [
"show_doc(AutoTBATS.predict_in_sample, title_level=3)"
"show_doc(AutoTBATS.forecast, name='AutoTBATS.forecast', title_level=3)"
]
},
{
Expand All @@ -10142,7 +10218,7 @@
"metadata": {},
"outputs": [],
"source": [
"show_doc(AutoTBATS.forecast, title_level=3)"
"show_doc(AutoTBATS.forward, name='AutoTBATS.forward', title_level=3)"
]
},
{
Expand Down
Loading
Loading