unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
8.1k stars 882 forks source link

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

Closed TPreece101 closed 2 months ago

TPreece101 commented 1 year ago

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):

Let me know if you need any further information 😊

dennisbader commented 1 year ago

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.

TPreece101 commented 1 year ago

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!

dennisbader commented 11 months ago

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

nvlaminc commented 10 months ago

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

madtoinou commented 2 months ago

Closing this issue to avoid duplicates as the feature request is already tracked by #1538.