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

[BUG]: NaiveEnsemble().historical_forecast() cannot handle multiple past_covariates #2664

Open
nvidia-wtruong opened this issue Feb 6, 2025 · 1 comment
Labels
feature request Use this label to request a new feature question Further information is requested

Comments

@nvidia-wtruong
Copy link

nvidia-wtruong commented Feb 6, 2025

Describe the bug
A clear and concise description of what the bug is.
NaiveEnsembleModel.historical_forecasts() does not handle multiple sets of past_covariates

To Reproduce

from darts.models import LinearRegressionModel, NaiveEnsembleModel
from darts.datasets import AirPassengersDataset, MonthlyMilkDataset
from darts.utils.timeseries_generation import datetime_attribute_timeseries

# Load two different time series datasets
ts_air = AirPassengersDataset().load()  # Example: Air passengers dataset
ts_milk = MonthlyMilkDataset().load()   # Example: Milk production dataset

# Expand the datasets to be multivariate by adding additional features
# For ts_air, add a datetime attribute (e.g., month as a numeric feature)
ts_air_multivariate = ts_air.stack(
    datetime_attribute_timeseries(ts_air.time_index, attribute="month", one_hot=False)
)

# For ts_milk, add a datetime attribute (e.g., year as a numeric feature)
ts_milk_multivariate = ts_milk.stack(
    datetime_attribute_timeseries(ts_milk.time_index, attribute="year", one_hot=False)
)

# Define two Linear Regression models with multivariate past covariates
# Use lags_past_covariates as dictionaries with variable names and lag values
model1 = LinearRegressionModel(
    lags=12,
    lags_past_covariates={"#Passengers": [-1], "month": [-1, -2, -3]}  # Use "month" from ts_air_multivariate
)

model2 = LinearRegressionModel(
    lags=6,
    lags_past_covariates={"Pounds per cow": [-1, -2], "year": [-1]}  # Use "milk_production" and "year" from ts_milk_multivariate
)

# Create a NaiveEnsembleModel with the two models
ensemble = NaiveEnsembleModel(forecasting_models=[model1, model2])

# Generate historical forecasts using the ensemble model
backtest = ensemble.historical_forecasts(
    series=ts_air,
    past_covariates=[ts_air_multivariate, ts_milk_multivariate],  # Provide both sets of multivariate covariates
    start=0.6,  
    forecast_horizon=3
)
ERROR:darts.models.forecasting.regression_model:ValueError: The `lags_past_covariates` dictionary specifies lags for components that are not present in the series : ['Pounds per cow', 'year']. They must be removed to avoid any ambiguity.
ValueError: The `lags_past_covariates` dictionary specifies lags for components that are not present in the series : ['Pounds per cow', 'year']. They must be removed to avoid any ambiguity.
File <command-7831122302366366>, line 33
     30 ensemble = NaiveEnsembleModel(forecasting_models=[model1, model2])
     32 # Generate historical forecasts using the ensemble model
---> 33 backtest = ensemble.historical_forecasts(
     34     series=ts_air,
     35     past_covariates=[ts_air_multivariate, ts_milk_multivariate],  # Provide both sets of multivariate covariates
     36     start=0.6,  # Start forecasting after 60% of the series
     37     forecast_horizon=3  # Forecast horizon of 3 steps
     38 )

Expected behavior
Given an ensembled model that takes different sets of covariates and the goal of running .historical_forecasts(), the argument past_covariates should be able to distinguish which covariate timeseries belongs to which model

System (please complete the following information):

  • Python version: Python 3.11.0rc1
  • darts version 0.32.0

Additional context
Add any other context about the problem here.

@nvidia-wtruong nvidia-wtruong added bug Something isn't working triage Issue waiting for triaging labels Feb 6, 2025
@madtoinou
Copy link
Collaborator

Hi @nvidia-wtruong,

The EnsembleModels in Darts pass all the series to all the models, i.e. the forecasting models have access to exactly the same information in order to generate the forecasts. In your use-case, since you use component-wise lags, the training/prediction data cannot be generated for the components that are not defined in the model.

The way you call historical_forecasts is not supported by Darts; the number of covariates should be identical to the number of series to predict (one for each), it's not intended to be dispatched across models.

@madtoinou madtoinou added feature request Use this label to request a new feature question Further information is requested and removed bug Something isn't working triage Issue waiting for triaging labels Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Use this label to request a new feature question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants