|
8 | 8 | __all__ = ["BaseForecaster"]
|
9 | 9 |
|
10 | 10 | from abc import abstractmethod
|
| 11 | +from typing import final |
11 | 12 |
|
12 | 13 | import numpy as np
|
13 | 14 | import pandas as pd
|
@@ -46,6 +47,7 @@ def __init__(self, horizon: int, axis: int):
|
46 | 47 | self.meta_ = None # Meta data related to y on the last fit
|
47 | 48 | super().__init__(axis)
|
48 | 49 |
|
| 50 | + @final |
49 | 51 | def fit(self, y, exog=None):
|
50 | 52 | """Fit forecaster to series y.
|
51 | 53 |
|
@@ -89,6 +91,7 @@ def fit(self, y, exog=None):
|
89 | 91 | @abstractmethod
|
90 | 92 | def _fit(self, y, exog=None): ...
|
91 | 93 |
|
| 94 | + @final |
92 | 95 | def predict(self, y=None, exog=None):
|
93 | 96 | """Predict the next horizon steps ahead.
|
94 | 97 |
|
@@ -117,6 +120,7 @@ def predict(self, y=None, exog=None):
|
117 | 120 | @abstractmethod
|
118 | 121 | def _predict(self, y=None, exog=None): ...
|
119 | 122 |
|
| 123 | + @final |
120 | 124 | def forecast(self, y, exog=None):
|
121 | 125 | """Forecast the next horizon steps ahead.
|
122 | 126 |
|
@@ -144,6 +148,106 @@ def _forecast(self, y, exog=None):
|
144 | 148 | self.fit(y, exog)
|
145 | 149 | return self._predict(y, exog)
|
146 | 150 |
|
| 151 | + @final |
| 152 | + def direct_forecast(self, y, prediction_horizon): |
| 153 | + """ |
| 154 | + Make ``prediction_horizon`` ahead forecasts using a fit for each horizon. |
| 155 | +
|
| 156 | + This is commonly called the direct strategy. The forecaster is trained to |
| 157 | + predict one ahead, then retrained to fit two ahead etc. Not all forecasters |
| 158 | + are capable of being used with direct forecasting. The ability to |
| 159 | + forecast on horizons greater than 1 is indicated by the tag |
| 160 | + "capability:horizon". If this tag is false this function raises a value |
| 161 | + error. This method cannot be overridden. |
| 162 | +
|
| 163 | + Parameters |
| 164 | + ---------- |
| 165 | + y : np.ndarray |
| 166 | + The time series to make forecasts about. |
| 167 | + prediction_horizon : int |
| 168 | + The number of future time steps to forecast. |
| 169 | +
|
| 170 | + predictions : np.ndarray |
| 171 | + An array of shape `(prediction_horizon,)` containing the forecasts for |
| 172 | + each horizon. |
| 173 | +
|
| 174 | + Raises |
| 175 | + ------ |
| 176 | + ValueError |
| 177 | + if ``"capability:horizon`` is False or `prediction_horizon` less than 1. |
| 178 | +
|
| 179 | + Examples |
| 180 | + -------- |
| 181 | + >>> from aeon.forecasting import RegressionForecaster |
| 182 | + >>> y = np.array([1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 2.0, 3.0, 4.0]) |
| 183 | + >>> f = RegressionForecaster(window=3) |
| 184 | + >>> f.direct_forecast(y,2) |
| 185 | + array([3., 2.]) |
| 186 | + """ |
| 187 | + horizon = self.get_tag("capability:horizon") |
| 188 | + if not horizon: |
| 189 | + raise ValueError( |
| 190 | + "This forecaster cannot be used with the direct strategy " |
| 191 | + "because it cannot be trained with a horizon > 1." |
| 192 | + ) |
| 193 | + if prediction_horizon < 1: |
| 194 | + raise ValueError( |
| 195 | + "The `prediction_horizon` must be greater than or equal to 1." |
| 196 | + ) |
| 197 | + |
| 198 | + preds = np.zeros(prediction_horizon) |
| 199 | + for i in range(0, prediction_horizon): |
| 200 | + self.horizon = i + 1 |
| 201 | + preds[i] = self.forecast(y) |
| 202 | + return preds |
| 203 | + |
| 204 | + def iterative_forecast(self, y, prediction_horizon): |
| 205 | + """ |
| 206 | + Forecast ``prediction_horizon`` prediction using a single model from `y`. |
| 207 | +
|
| 208 | + This function implements the iterative forecasting strategy (also called |
| 209 | + recursive or iterated). This involves a single model fit on y which is then |
| 210 | + used to make ``prediction_horizon`` ahead using its own predictions as |
| 211 | + inputs for future forecasts. This is done by taking |
| 212 | + the prediction at step ``i`` and feeding it back into the model to help |
| 213 | + predict for step ``i+1``. The basic contract of |
| 214 | + `iterative_forecast` is that `fit` is only ever called once. |
| 215 | +
|
| 216 | + y : np.ndarray |
| 217 | + The time series to make forecasts about. |
| 218 | + prediction_horizon : int |
| 219 | + The number of future time steps to forecast. |
| 220 | +
|
| 221 | + Returns |
| 222 | + ------- |
| 223 | + predictions : np.ndarray |
| 224 | + An array of shape `(prediction_horizon,)` containing the forecasts for |
| 225 | + each horizon. |
| 226 | +
|
| 227 | + Raises |
| 228 | + ------ |
| 229 | + ValueError |
| 230 | + if prediction_horizon` less than 1. |
| 231 | +
|
| 232 | + Examples |
| 233 | + -------- |
| 234 | + >>> from aeon.forecasting import RegressionForecaster |
| 235 | + >>> y = np.array([1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 2.0, 3.0, 4.0]) |
| 236 | + >>> f = RegressionForecaster(window=3) |
| 237 | + >>> f.iterative_forecast(y,2) |
| 238 | + array([3., 2.]) |
| 239 | + """ |
| 240 | + if prediction_horizon < 1: |
| 241 | + raise ValueError( |
| 242 | + "The `prediction_horizon` must be greater than or equal to 1." |
| 243 | + ) |
| 244 | + preds = np.zeros(prediction_horizon) |
| 245 | + self.fit(y) |
| 246 | + for i in range(0, prediction_horizon): |
| 247 | + preds[i] = self.predict(y) |
| 248 | + y = np.append(y, preds[i]) |
| 249 | + return preds |
| 250 | + |
147 | 251 | def _convert_y(self, y: VALID_SERIES_INNER_TYPES, axis: int):
|
148 | 252 | """Convert y to self.get_tag("y_inner_type")."""
|
149 | 253 | if axis > 1 or axis < 0:
|
|
0 commit comments