Skip to content

Commit

Permalink
Feat/auto-regression and future values of past covariates documentati…
Browse files Browse the repository at this point in the history
…on (#2049)

* feat: added warning about usage of past covariates during auto-regression

* feat: slightly changed the docstring for output_chunk_length

* fix: forgot to add the new argument to some wrapper

* feat: display autoregression and past cov related warning only once when using historical forecast with a global model

* Apply suggestions from code review

Co-authored-by: Dennis Bader <[email protected]>

* feat: updated docstring for output_chunk_length in both regression and torch models, updated docstring about past/future covariates requirements in the torch models that were missing it

* update docs

---------

Co-authored-by: Dennis Bader <[email protected]>
  • Loading branch information
madtoinou and dennisbader authored Nov 16, 2023
1 parent c8f5948 commit 36a1c09
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 53 deletions.
11 changes: 9 additions & 2 deletions darts/models/forecasting/block_rnn_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,16 @@ def __init__(
Parameters
----------
input_chunk_length
The number of time steps that will be fed to the internal forecasting module
Number of time steps in the past to take as a model input (per chunk). Applies to the target
series, and past and/or future covariates (if the model supports it).
output_chunk_length
Number of time steps to be output by the internal forecasting module.
Number of time steps predicted at once (per chunk) by the internal model. Also, the number of future values
from future covariates to use as a model input (if the model supports future covariates). It is not the same
as forecast horizon `n` used in `predict()`, which is the desired number of prediction points generated
using either a one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents
auto-regression. This is useful when the covariates don't extend far enough into the future, or to prohibit
the model from using future values of past and / or future covariates for prediction (depending on the
model's covariate support).
model
Either a string specifying the RNN module type ("RNN", "LSTM" or "GRU"),
or a PyTorch module with the same specifications as
Expand Down
9 changes: 6 additions & 3 deletions darts/models/forecasting/catboost_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ def __init__(
`future` future lags (starting from 0 - the prediction time - up to `future - 1` included). Otherwise a list
of integers with lags is required.
output_chunk_length
Number of time steps predicted at once by the internal regression model. Does not have to equal the forecast
horizon `n` used in `predict()`. However, setting `output_chunk_length` equal to the forecast horizon may
be useful if the covariates don't extend far enough into the future.
Number of time steps predicted at once (per chunk) by the internal model. It is not the same as forecast
horizon `n` used in `predict()`, which is the desired number of prediction points generated using a
one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents auto-regression. This is
useful when the covariates don't extend far enough into the future, or to prohibit the model from using
future values of past and / or future covariates for prediction (depending on the model's covariate
support).
add_encoders
A large number of past and future covariates can be automatically generated with `add_encoders`.
This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that
Expand Down
16 changes: 12 additions & 4 deletions darts/models/forecasting/dlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,23 @@ def __init__(
):
"""An implementation of the DLinear model, as presented in [1]_.
This implementation is improved by allowing the optional use of past covariates,
future covariates and static covariates, and by making the model optionally probabilistic.
This implementation is improved by allowing the optional use of past covariates (known for
`input_chunk_length` points before prediction time), future covariates (known for `output_chunk_length`
points after prediction time) and static covariates, as well as supporting probabilistic forecasting.
Parameters
----------
input_chunk_length
The length of the input sequence fed to the model.
Number of time steps in the past to take as a model input (per chunk). Applies to the target
series, and past and/or future covariates (if the model supports it).
output_chunk_length
The length of the forecast of the model.
Number of time steps predicted at once (per chunk) by the internal model. Also, the number of future values
from future covariates to use as a model input (if the model supports future covariates). It is not the same
as forecast horizon `n` used in `predict()`, which is the desired number of prediction points generated
using either a one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents
auto-regression. This is useful when the covariates don't extend far enough into the future, or to prohibit
the model from using future values of past and / or future covariates for prediction (depending on the
model's covariate support).
shared_weights
Whether to use shared weights for all components of multivariate series.
Expand Down
2 changes: 2 additions & 0 deletions darts/models/forecasting/ensemble_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def predict(
num_samples: int = 1,
verbose: bool = False,
predict_likelihood_parameters: bool = False,
show_warnings: bool = True,
) -> Union[TimeSeries, Sequence[TimeSeries]]:
# ensure forecasting models all rely on the same series during inference
if series is None:
Expand All @@ -305,6 +306,7 @@ def predict(
num_samples=num_samples,
verbose=verbose,
predict_likelihood_parameters=predict_likelihood_parameters,
show_warnings=show_warnings,
)

# for single-level ensemble, probabilistic forecast is obtained directly from forecasting models
Expand Down
21 changes: 21 additions & 0 deletions darts/models/forecasting/forecasting_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,9 @@ def retrain_func(
else:
outer_iterator = _build_tqdm_iterator(series, verbose)

# deactivate the warning after displaying it once if show_warnings is True
show_predict_warnings = show_warnings

forecasts_list = []
for idx, series_ in enumerate(outer_iterator):
past_covariates_ = past_covariates[idx] if past_covariates else None
Expand Down Expand Up @@ -1061,8 +1064,11 @@ def retrain_func(
num_samples=num_samples,
verbose=verbose,
predict_likelihood_parameters=predict_likelihood_parameters,
show_warnings=show_predict_warnings,
**predict_kwargs,
)
show_predict_warnings = False

if forecast_components is None:
forecast_components = forecast.columns

Expand Down Expand Up @@ -2201,6 +2207,7 @@ def predict(
num_samples: int = 1,
verbose: bool = False,
predict_likelihood_parameters: bool = False,
show_warnings: bool = True,
) -> Union[TimeSeries, Sequence[TimeSeries]]:
"""Forecasts values for `n` time steps after the end of the series.
Expand Down Expand Up @@ -2239,6 +2246,8 @@ def predict(
If set to `True`, the model predict the parameters of its Likelihood parameters instead of the target. Only
supported for probabilistic models with a likelihood, `num_samples = 1` and `n<=output_chunk_length`.
Default: ``False``
show_warnings
Whether to show warnings related auto-regression and past covariates usage.
Returns
-------
Expand Down Expand Up @@ -2280,6 +2289,18 @@ def predict(
"must be embedded in the target `series` passed to `predict()`."
)
)
if (
show_warnings
and self.uses_past_covariates
and self.output_chunk_length is not None
and n > self.output_chunk_length
):
logger.warning(
"`predict()` was called with `n > output_chunk_length`: using auto-regression to forecast "
"the values after `output_chunk_length` points. The model will access `(n - output_chunk_length)` "
"future values of your `past_covariates` (relative to the first predicted time step). "
"To hide this warning, set `show_warnings=False`."
)

@property
def _supports_non_retrainable_historical_forecasts(self) -> bool:
Expand Down
9 changes: 6 additions & 3 deletions darts/models/forecasting/lgbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ def __init__(
'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
components are missing and the 'default_lags' key is not provided.
output_chunk_length
Number of time steps predicted at once by the internal regression model. Does not have to equal the forecast
horizon `n` used in `predict()`. However, setting `output_chunk_length` equal to the forecast horizon may
be useful if the covariates don't extend far enough into the future.
Number of time steps predicted at once (per chunk) by the internal model. It is not the same as forecast
horizon `n` used in `predict()`, which is the desired number of prediction points generated using a
one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents auto-regression. This is
useful when the covariates don't extend far enough into the future, or to prohibit the model from using
future values of past and / or future covariates for prediction (depending on the model's covariate
support).
add_encoders
A large number of past and future covariates can be automatically generated with `add_encoders`.
This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that
Expand Down
9 changes: 6 additions & 3 deletions darts/models/forecasting/linear_regression_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ def __init__(
'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
components are missing and the 'default_lags' key is not provided.
output_chunk_length
Number of time steps predicted at once by the internal regression model. Does not have to equal the forecast
horizon `n` used in `predict()`. However, setting `output_chunk_length` equal to the forecast horizon may
be useful if the covariates don't extend far enough into the future.
Number of time steps predicted at once (per chunk) by the internal model. It is not the same as forecast
horizon `n` used in `predict()`, which is the desired number of prediction points generated using a
one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents auto-regression. This is
useful when the covariates don't extend far enough into the future, or to prohibit the model from using
future values of past and / or future covariates for prediction (depending on the model's covariate
support).
add_encoders
A large number of past and future covariates can be automatically generated with `add_encoders`.
This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that
Expand Down
11 changes: 9 additions & 2 deletions darts/models/forecasting/nbeats.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,16 @@ def __init__(
Parameters
----------
input_chunk_length
The length of the input sequence fed to the model.
Number of time steps in the past to take as a model input (per chunk). Applies to the target
series, and past and/or future covariates (if the model supports it).
output_chunk_length
The length of the forecast of the model.
Number of time steps predicted at once (per chunk) by the internal model. Also, the number of future values
from future covariates to use as a model input (if the model supports future covariates). It is not the same
as forecast horizon `n` used in `predict()`, which is the desired number of prediction points generated
using either a one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents
auto-regression. This is useful when the covariates don't extend far enough into the future, or to prohibit
the model from using future values of past and / or future covariates for prediction (depending on the
model's covariate support).
generic_architecture
Boolean value indicating whether the generic architecture of N-BEATS is used.
If not, the interpretable architecture outlined in the paper (consisting of one trend
Expand Down
11 changes: 9 additions & 2 deletions darts/models/forecasting/nhits.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,16 @@ def __init__(
Parameters
----------
input_chunk_length
The length of the input sequence fed to the model.
Number of time steps in the past to take as a model input (per chunk). Applies to the target
series, and past and/or future covariates (if the model supports it).
output_chunk_length
The length of the forecast of the model.
Number of time steps predicted at once (per chunk) by the internal model. Also, the number of future values
from future covariates to use as a model input (if the model supports future covariates). It is not the same
as forecast horizon `n` used in `predict()`, which is the desired number of prediction points generated
using either a one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents
auto-regression. This is useful when the covariates don't extend far enough into the future, or to prohibit
the model from using future values of past and / or future covariates for prediction (depending on the
model's covariate support).
num_stacks
The number of stacks that make up the whole model.
num_blocks
Expand Down
16 changes: 12 additions & 4 deletions darts/models/forecasting/nlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,23 @@ def __init__(
):
"""An implementation of the NLinear model, as presented in [1]_.
This implementation is improved by allowing the optional use of past covariates,
future covariates and static covariates, and by making the model optionally probabilistic.
This implementation is improved by allowing the optional use of past covariates (known for
`input_chunk_length` points before prediction time), future covariates (known for `output_chunk_length`
points after prediction time) and static covariates, as well as supporting probabilistic forecasting.
Parameters
----------
input_chunk_length
The length of the input sequence fed to the model.
Number of time steps in the past to take as a model input (per chunk). Applies to the target
series, and past and/or future covariates (if the model supports it).
output_chunk_length
The length of the forecast of the model.
Number of time steps predicted at once (per chunk) by the internal model. Also, the number of future values
from future covariates to use as a model input (if the model supports future covariates). It is not the same
as forecast horizon `n` used in `predict()`, which is the desired number of prediction points generated
using either a one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents
auto-regression. This is useful when the covariates don't extend far enough into the future, or to prohibit
the model from using future values of past and / or future covariates for prediction (depending on the
model's covariate support).
shared_weights
Whether to use shared weights for all components of multivariate series.
Expand Down
11 changes: 9 additions & 2 deletions darts/models/forecasting/pl_forecasting_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,16 @@ def __init__(
Parameters
----------
input_chunk_length
Number of input past time steps per chunk.
Number of time steps in the past to take as a model input (per chunk). Applies to the target
series, and past and/or future covariates (if the model supports it).
output_chunk_length
Number of output time steps per chunk.
Number of time steps predicted at once (per chunk) by the internal model. Also, the number of future values
from future covariates to use as a model input (if the model supports future covariates). It is not the same
as forecast horizon `n` used in `predict()`, which is the desired number of prediction points generated
using either a one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents
auto-regression. This is useful when the covariates don't extend far enough into the future, or to prohibit
the model from using future values of past and / or future covariates for prediction (depending on the
model's covariate support).
train_sample_shape
Shape of the model's input, used to instantiate model without calling ``fit_from_dataset`` and
perform sanity check on new training/inference datasets used for re-training or prediction.
Expand Down
9 changes: 6 additions & 3 deletions darts/models/forecasting/random_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ def __init__(
'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
components are missing and the 'default_lags' key is not provided.
output_chunk_length
Number of time steps predicted at once by the internal regression model. Does not have to equal the forecast
horizon `n` used in `predict()`. However, setting `output_chunk_length` equal to the forecast horizon may
be useful if the covariates don't extend far enough into the future.
Number of time steps predicted at once (per chunk) by the internal model. It is not the same as forecast
horizon `n` used in `predict()`, which is the desired number of prediction points generated using a
one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents auto-regression. This is
useful when the covariates don't extend far enough into the future, or to prohibit the model from using
future values of past and / or future covariates for prediction (depending on the model's covariate
support).
add_encoders
A large number of past and future covariates can be automatically generated with `add_encoders`.
This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that
Expand Down
Loading

0 comments on commit 36a1c09

Please sign in to comment.