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-FIX] RegressionEnsembleModel : Error Furture covariates/Past Covariates… #1660

Merged
merged 17 commits into from
Apr 10, 2023

Conversation

Rajesh4AI
Copy link
Contributor

@Rajesh4AI Rajesh4AI commented Mar 21, 2023

… FutureCovariates | Suggested With Code Fix

[BUG-FIX] RegressionEnsembleModel : Error when trying to predict with FutureCovariates | Suggested With Code Fix #1621

Fixes #.

  1. In regression_ensemble_model.py : While fitting the model the code did not fit with past/future covariates.
  2. : Code has been modified to check if Covariates Exist and Model supports. If yes then fit with same.

Summary

Other Information

To reproduce the error try this:

from darts.models import RegressionEnsembleModel,AutoARIMA,Prophet
from darts.datasets import AirPassengersDataset
import numpy as np
series_air = AirPassengersDataset().load()
models = [Prophet(),AutoARIMA()]
ensemble_model = RegressionEnsembleModel(forecasting_models=models,regression_train_n_points=12)

Fit would run

ensemble_model.fit(series=series_air[:-36]
,future_covariates=series_air
)

Error: Predict would fail as Fit doesn't fit the models on future covariate due to bug

pred_air=ensemble_model.predict(n=36,series=series_air[:-36],future_covariates=series_air)

@Rajesh4AI Rajesh4AI requested a review from dennisbader as a code owner March 21, 2023 16:54
@Rajesh4AI Rajesh4AI changed the title [BUG-FIX] RegressionEnsembleModel : Error when trying to predict with… [BUG-FIX] RegressionEnsembleModel : Error Furture covariates/Past Covariates… Mar 22, 2023
@Rajesh4AI
Copy link
Contributor Author

@madtoinou Fixed the lint issue. Do let me know if there is anything else I need to check.

Copy link
Collaborator

@madtoinou madtoinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing to darts, I did a little suggestion to simplify the modification.

Could you please also add a case in the unit test to cover this scenario (EnsembleModel containing LocalForecastingModels and using future covariates during training/prediction)?

elif (past_covariates is not None) and model.supports_past_covariates:
model.fit(series=series, past_covariates=past_covariates)
else:
model.fit(self.training_series)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the if self.is_global_ensemble condition is not verified, it indicates that the EnsembleModel contains only LocalForecastingModels which do not support past covariates, you can remove this if-else case.

Also, you could probably also remove the check on past_covariate since a value of None will not disrupt the code-flow (as it's the default value in the fit() method).

A simpler approach would probably be to reuse the approach for the global_ensemble with something like:

if self.is_global_ensemble:
   kwargs = dict(series=series)
   if model.supports_past_covariates:
      kwargs["past_covariates"] = past_covariates
else:
   kwargs = dict(series=self.training_series)

if model.supports_future_covariates:
   kwargs["future_covariates"] = future_covariates

model.fit(**kwargs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noice!! This is much clean... Will update it and write the test too, Thank you.

@codecov-commenter
Copy link

codecov-commenter commented Mar 23, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.04%. Comparing base (ebb9eb6) to head (d888a37).
Report is 278 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1660      +/-   ##
==========================================
- Coverage   94.11%   94.04%   -0.07%     
==========================================
  Files         125      125              
  Lines       11447    11452       +5     
==========================================
- Hits        10773    10770       -3     
- Misses        674      682       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

for global ensemble it will check if supports past covariates.  For both local and global model it will future covariates supports
@Rajesh4AI
Copy link
Contributor Author

Rajesh4AI commented Mar 23, 2023

Working on the test case.
Moved kwargs above if condition.

            **kwargs = dict(series=series)**
            if self.is_global_ensemble:
                if model.supports_past_covariates:
                    kwargs["past_covariates"] = past_covariates
            if model.supports_future_covariates:
                kwargs["future_covariates"] = future_covariates
            model.fit(**kwargs)```

Tests with local models supports future covariates along with models which don't support. Prophet() is used to represent one which support future covariate
Added blank line before comment
Linted code with black
@madtoinou
Copy link
Collaborator

@Rajesh4AI, you can find a section at the bottom of the contribution guidelines explaining how to install the dev tools and automate the linting

fixed library import order
@Rajesh4AI
Copy link
Contributor Author

@Rajesh4AI, you can find a section at the bottom of the contribution guidelines explaining how to install the dev tools and automate the lining

@madtoinou Sure, fixed the import sort manually in test_resgression_ensemble_model.py . For some reason auto linting is not working on my VScode (black - provider, isort = for sorting) . Will try from different system if it fails this time too.

@Rajesh4AI
Copy link
Contributor Author

Rajesh4AI commented Mar 26, 2023

@madtoinou all linting issues have been fixed. Do let me know if i need to do anything else

Copy link
Collaborator

@madtoinou madtoinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing to darts!

Suggested a minor modification, otherwise the code looks good 🚀

ensemble_models = self.get_local_models()

# append model which supports future covariates
ensemble_models.append(Prophet())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are discussion to make Prophet optional (imported from another library, installation is not necessarily straight-forward): it would probably to use a simpler model to test this feature such as LinearRegressionModel or RandomForest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makse sense.. will modify it

Copy link
Collaborator

@madtoinou madtoinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you for adapting the code so promptly!

Copy link
Collaborator

@dennisbader dennisbader left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks :) I think we can even simplify a bit more :)

@@ -140,17 +140,13 @@ def fit(
self.models = [model.untrained_model() for model in self.models]

for model in self.models:
kwargs = dict(series=series)
if self.is_global_ensemble:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we just fully ignore the self.is_global_ensemble and do the following for any model:

kwargs = dict(series=series)
if model.supports_past_covariates:
    kwargs["past_covariates"] = past_covariates
if model.supports_future_covariates:
    kwargs["future_covariates"] = future_covariates
model.fit(**kwargs)

Copy link
Collaborator

@dennisbader dennisbader left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Rajesh4AI , looks good 🚀

@dennisbader dennisbader merged commit 0401fe3 into unit8co:master Apr 10, 2023
alexcolpitts96 pushed a commit to alexcolpitts96/darts that referenced this pull request May 31, 2023
…ariates… (unit8co#1660)

* [BUG-FIX] RegressionEnsembleModel : Error when trying to predict with FutureCovariates | Suggested With Code Fix

* linted the code

added space after comma

* simplified future covariate check

for global ensemble it will check if supports past covariates.  For both local and global model it will future covariates supports

* test RegressionEnsemble Model with future Covariates

Tests with local models supports future covariates along with models which don't support. Prophet() is used to represent one which support future covariate

* Fixed possible lint issue

Added blank line before comment

* Lint correction with Black

Linted code with black

* isort fix manual

fixed library import order

* Replace Prophet with altenatives

* simplify covariate usage

---------

Co-authored-by: madtoinou <32447896+madtoinou@users.noreply.github.com>
Co-authored-by: dennisbader <dennis.bader@gmx.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
4 participants