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

Feat/conformal prediction #2552

Merged
merged 92 commits into from
Dec 20, 2024
Merged

Feat/conformal prediction #2552

merged 92 commits into from
Dec 20, 2024

Conversation

dennisbader
Copy link
Collaborator

@dennisbader dennisbader commented Oct 3, 2024

Checklist before merging this PR:

  • Mentioned all issues that this PR fixes or addresses.
  • Summarized the updates of this PR under Summary.
  • Added an entry under Unreleased in the Changelog.

Fixes #1704, fixes #2161.

Short Summary

  • Adds the first two Conformal Prediction Models: ConformalNaiveModel, and ConformalQRModel (read more below).
  • Adds 3 new quantile interval metrics (plus their aggregated versions):
    • Interval Winkler Score iws(), and Mean Interval Winkler Scores miws() (time-aggregated) (source)
    • Interval Coverage ic() (binary if observation is within the quantile interval), and Mean Interval Covarage mic() (time-aggregated)
    • Interval Non-Conformity Score for Quantile Regression incs_qr(), and Mean ... mincs_qr() (time-aggregated) (source)
  • Adds support for overlap_end=True in ForecastingModel.residuals(). This computes historical forecasts and residuals that can extend further than the end of the target series. With this, all returned residual values have the same length per forecast (the last residuals will contain missing values, if the forecasts extended further into the future than the end of the target series).

Summary

Adds first conformal prediction models to Darts. Conformal models can be applied to any of Darts' global forecasting model, as long as the model has been fitted before. In general the workflow of the models to produce one forecast/prediction is as follows:

  • Extract a calibration set: The calibration set for each conformal forecast is automatically extracted from
    the past of your input series relative to the forecast start point. The number of calibration examples
    (forecast errors / non-conformity scores) to consider can be defined at model creation
    with parameter cal_length. Note that when using cal_stride>1, a longer history is required since
    the calibration examples are generated with stridden historical forecasts.
  • Generate historical forecasts on the calibration set (using the forecasting model) with a stride cal_stride.
  • Compute the errors/non-conformity scores (specific to each conformal model) on these historical forecasts
  • Compute the quantile values from the errors / non-conformity scores (using our desired quantiles set at model
    creation with parameter quantiles).
  • Compute the conformal prediction: Using these quantile values, add calibrated intervals to (or adjust the
    existing intervals of) the forecasting model's predictions.

Notes:

  • When computing historical_forecasts(), backtest(), residuals(), ... the above is applied for each forecast
  • For multi-horizon forecasts, the above is applied for each step in the horizon separately
  • Focus was put on keeping it as efficient as possible using mostly "vectorized" operations

Input Support

All added conformal models support the following input (depending on the fitted forecasting model):

  • uni/multivariate target series
  • past/future/static covariates
  • single/multiple series

Forecast/Output Support

All models support the following prediction modes:

  • single/multi-horizon forecasts. For multi-horizon, the calibration process is repeated per step in the forecast horizon.
  • single/mutliple quantile intervals: It can be any number of quantile intervals as long as they are centered around the median (e.g., quantiles=[0.05, 0.2, 0.5, 0.8, 0.95]).
  • historical forecasts with expanding or rolling calibration sets with parameter cal_length (to make the algorithm adaptive)
  • direct quantile predictions using predict_likelihood_parameters=True, num_samples=1 in all prediction methods.
  • sampled predictions from these quantile predictions using num_samples>>1 in all prediction methods.

Requirements to use a conformal model:

  • Any pre-trained GlobalForecastingModel (global baselines, all regression models, all torch models)
  • A long enough calibration set, depending on the forecast horizon n. It must be possible to generate at least n + cal_length historical forecasts from the calibration input series.

Added Algorithms

Added two algorithms each with two symmetry modes:

  • ConformalNaiveModel: Adds calibrated intervals around the median forecast from the forecasting model.
    • symmetric=True:
      • The lower and upper interval bounds are calibrated by the same magnitude.
      • Non-conformity scores: uses metric ae() (absolute error) to compute the non-conformity scores
    • symmetric=False
      • The lower and upper interval bounds are calibrated separately
      • Non-conformity scores: uses metric err() (error) to compute the non-conformity scores of the upper bounds, an -err() for the lower bounds.
  • ConformalQRModel (Conformalized Quantile Regression, source): Calibrates the quantile predictions from a probabilistic forecasting model.
    • symmetric=True:
      • The lower and upper interval bounds are calibrated by the same magnitude.
      • Non-conformity scores: uses metric incs_qr(symmetric=True) (Quantile Regression Non-Conformity Score) to compute the non-conformity scores
    • symmetric=False
      • The lower and upper interval bounds are calibrated separately
      • Non-conformity scores: uses metric incs_qr(symmetric=False) (Quantile Regression Non-Conformity Score) to compute the non-conformity scores for the upper and lower bound separately.

darts/metrics/metrics.py Outdated Show resolved Hide resolved
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.

It looks really nice, the notebook require a few adjustment/improvement and I think that this is ready to be merged.

There are a few "WIP" keyword in some docstring and in the notebook, let's maybe get ride of them ;)

darts/models/forecasting/conformal_models.py Outdated Show resolved Hide resolved
@dennisbader dennisbader merged commit fc244ac into master Dec 20, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding bootstrapping functionnality from residuals of a model Conformal Predictions
3 participants