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

[ENH] direct/recursive forecasting #2410

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
53 changes: 53 additions & 0 deletions aeon/forecasting/_multiple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Container for direct forecasting."""

import numpy as np

from aeon.forecasting.base import BaseForecaster


def direct_forecasting(
forecaster: BaseForecaster, y: np.ndarray, steps_ahead: int, exog=None
):
"""Forecast steps_ahead points from X using forecaster.

Parameters
----------
forecaster : BaseForecaster class or object.
Forecaster object with forecast method.
y : 1D np.ndarray
Time series to train forecasters on.
steps_ahead : int
Number of steps ahead to forecast.
window_size : int
Fits a different forecaster to each horizon

Returns
-------
np.ndarray
Length steps_ahead array of forecasts.
"""
if not isinstance(forecaster, BaseForecaster):
raise ValueError("Forecaster must be a BaseForecaster object.")
preds = np.zeros(steps_ahead)
for i in range(1, steps_ahead + 1):
f = forecaster.__class__(horizon=i, **forecaster.params)
preds[i - 1] = f.forecast(y, exog)
return preds


def recursive_forecasting(
forecaster: BaseForecaster, y: np.ndarray, steps_ahead: int, window=1, exog=None
):
"""Forecast steps_ahead points from X using forecaster."""
if not isinstance(forecaster, BaseForecaster):
raise ValueError("Forecaster must be a BaseForecaster object.")
if hasattr(forecaster, "window"):
forecaster = forecaster.__class__(horizon=1, **forecaster.params)
forecaster.fit(y, exog)
preds = np.zeros(steps_ahead)
y = y[-window:]
for i in range(1, steps_ahead + 1):
preds[i - 1] = forecaster.predict(y)
y[:-1] = y[1:]
y[window - 1] = preds[i - 1]
return preds
Loading