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

How do you get residuals from Darts models in general? #2612

Open
franc703 opened this issue Dec 3, 2024 · 1 comment
Open

How do you get residuals from Darts models in general? #2612

franc703 opened this issue Dec 3, 2024 · 1 comment
Labels
question Further information is requested

Comments

@franc703
Copy link

franc703 commented Dec 3, 2024

Why is it so hard to get residuals, right after fitting the model? I want to see the in-sample residuals, but I need to use the method residuals, which takes time to compute.

@franc703 franc703 added question Further information is requested triage Issue waiting for triaging labels Dec 3, 2024
@dennisbader
Copy link
Collaborator

dennisbader commented Dec 8, 2024

Hi @franc703, we want to provide a unified API, so that all models have the same support.

  • Not all models support the in-sample residuals out of the box (e.g. torch models, regression models). Therefore we offer the residuals() method to compute them with any model.
  • If you know that your specific models supports it, you can always access the model with ForecastingModel.model after training.
  • Our residuals() method offers much more support than the fitted values (residuals on a historic simulation of re-applying the model, multi-horizon residuals, ...).
  • With global pre-trained models, computing the residuals is quite fast, since we use optimized routines to generate the historical forecasts.

We recently updated the documentation of residuals(), which should explain what's happening behind the hood (and why we can't just return fitted values).

"""Compute the residuals that the model produced for historical forecasts on (potentially multiple) `series`.

This function computes the difference (or one of Darts' "per time step" metrics) between the actual
observations from `series` and the fitted values obtained by training the model on `series` (or using a
pre-trained model with `retrain=False`). Not all models support fitted values, so we use historical forecasts
as an approximation for them.

In sequence this method performs:

- use pre-computed `historical_forecasts` or compute historical forecasts for each series (see
  :meth:`~darts.models.forecasting.forecasting_model.ForecastingModel.historical_forecasts` for more details).
  How the historical forecasts are generated can be configured with parameters `num_samples`, `train_length`,
  `start`, `start_format`, `forecast_horizon`, `stride`, `retrain`, `last_points_only`, `fit_kwargs`, and
  `predict_kwargs`.
- compute a backtest using a "per time step" `metric` between the historical forecasts and `series` per
  component/column and time step (see
  :meth:`~darts.models.forecasting.forecasting_model.ForecastingModel.backtest` for more details). By default,
  uses the residuals :func:`~darts.metrics.metrics.err` (error) as a `metric`.
- create and return `TimeSeries` (or simply a np.ndarray with `values_only=True`) with the time index from
  historical forecasts, and values from the metrics per component and time step.

This method works for single or multiple univariate or multivariate series.
It uses the median prediction (when dealing with stochastic forecasts).
"""

@madtoinou madtoinou removed the triage Issue waiting for triaging label Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants