darts icon indicating copy to clipboard operation
darts copied to clipboard

[BUG] LightGBM Model when generating historical forecasts doesn't train globally

Open TPreece101 opened this issue 1 year ago • 4 comments

Describe the bug I am experimenting with the LightGBM model and trying to backtest using the historical_forecasts method. However, it appears that when I use the historical_forecasts method it is treating each time series independently rather than training as a global model as it does when you use the fit method.

To Reproduce Here is the simplest possible reproduction of the issue

import pandas as pd
from darts import TimeSeries
from darts.models import LightGBMModel

times = pd.date_range(start=pd.Timestamp("20200101000000"), periods=12, freq="MS")
pd_series_1 = pd.Series(range(12), index=times)
ts_1 = TimeSeries.from_series(pd_series_1)
pd_series_2 = pd.Series(range(12), index=times)
ts_2 = TimeSeries.from_series(pd_series_2)

ts_list = [ts_1, ts_2]

lightgbm_model = LightGBMModel(
    lags=2,
    lags_future_covariates=[0],
    add_encoders={
        "cyclic": {"future": ["month"]}
    },
    multi_models=False,
)

lightgbm_model.fit(ts_list) # Standard out indicates using 20 data points in the training set 

lightgbm_model.historical_forecasts(ts_list, last_points_only=False) # Standard out indicates the maximum number of data points in training set is 9

Expected behavior Given the number of standard out sections, and the number of points it is saying it's training with, it looks like it's treating each of the series in the series list as an individual series when using historical_forecasts(). Whereas when you pass a sequence of series to the fit() method, it trains using multiple time series at once in a global fashion. I would have expected historical_forecasts() to have the same behaviour.

System (please complete the following information):

  • Python version: 3.11.2
  • darts version 0.26.0

Let me know if you need any further information 😊

TPreece101 avatar Nov 20 '23 17:11 TPreece101

Hi @TPreece101 it is correct, the model is retrained for each series when using historical forecasts. There are other issues related to this: #1909, and here.

The latter also describes a proposed solution, and why it is not so trivial to implement.

dennisbader avatar Nov 20 '23 18:11 dennisbader

Thanks for the speedy reply @dennisbader, that makes sense - I hope that you can get to it soon as it would be a really great feature!

TPreece101 avatar Nov 20 '23 18:11 TPreece101

Hi @pcgm-team. Yes, if you fit the model using multiple series before historical forecasting.

dennisbader avatar Dec 07 '23 06:12 dennisbader

Hi everyone,

I ran into the same issue, I understood it is not easy to take into account all cases ([https://github.com/unit8co/darts/issues/1538#issuecomment-1425563775](feature request)). So, waiting a solution, I have written a global_historical_forecasts function you can copy/paste in darts/models/forecasting/forecasting_model.py . Please, note that it will only work in the case all the series share the same time indexes. It computes the models for each historical test step only the first time (for the first series in the loop) and then use the already computed models for the following series since it will be the same models as they are global models.

global_historical_forecasting.txt

nvlaminc avatar Jan 19 '24 16:01 nvlaminc