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

Fixes bug when using historical forecast with discrete lags < -1 #2715

Merged
merged 17 commits into from
Mar 7, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
{},
(5, 3),
),
(LinearRegressionModel, {"lags": [-5]}, {}, (5, 1)),
(LinearRegressionModel, {"lags": [-5], "output_chunk_shift": 1}, {}, (5, 2)),
]
if not isinstance(CatBoostModel, NotImportedModule):
models_reg_no_cov_cls_kwargs.append((
Expand Down Expand Up @@ -665,7 +667,7 @@ def test_historical_forecasts_negative_rangeindex(self):
def test_historical_forecasts(self, config):
"""Tests historical forecasts with retraining for expected forecast lengths and times"""
forecast_horizon = 8
# if no fit and retrain=false, should fit at fist iteration
# if no fit and retrain=false, should fit at first iteration
model_cls, kwargs, model_kwarg, bounds = config
model = model_cls(**kwargs, **model_kwarg)
# set train length to be the minimum required training length
Expand Down
11 changes: 8 additions & 3 deletions darts/utils/historical_forecasts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,7 @@ def _adjust_historical_forecasts_time_index(
show_warnings: bool,
) -> TimeIndex:
"""
Shrink the beginning and end of the historical forecasts time index based on the values of `start`,
`forecast_horizon` and `overlap_end`.
Shrink the beginning and end of the historical forecasts time index based on the value of `start`.
"""
# retrieve actual start
# when applicable, shift the start of the forecastable index based on `start`
Expand Down Expand Up @@ -1020,7 +1019,13 @@ def _get_historical_forecast_boundaries(
hist_fct_tgt_start, hist_fct_tgt_end = historical_forecasts_time_index
if min_target_lag is not None:
hist_fct_tgt_start += min_target_lag * freq
hist_fct_tgt_end -= 1 * freq

# target lag has a gap between the max lag and the present
if hasattr(model, "lags") and model._get_lags("target") and not overlap_end:
hist_fct_tgt_end += 1 * freq * model._get_lags("target")[-1]
else:
hist_fct_tgt_end -= 1 * freq

# past lags are <= 0
hist_fct_pc_start, hist_fct_pc_end = historical_forecasts_time_index
if min_past_cov_lag is not None:
Expand Down
Loading