mapie.regression.MapieTimeSeriesRegressor

class mapie.regression.MapieTimeSeriesRegressor(estimator: Optional[RegressorMixin] = None, method: str = 'enbpi', cv: Optional[Union[int, str, BaseCrossValidator]] = None, n_jobs: Optional[int] = None, agg_function: Optional[str] = 'mean', verbose: int = 0, conformity_score: Optional[BaseRegressionScore] = None, random_state: Optional[Union[int, RandomState]] = None)[source]

Prediction intervals with out-of-fold residuals for time series. This class only has two valid method : "enbpi" or "aci"

The prediction intervals are calibrated on a split of the trained data. Both strategies are estimating prediction intervals on single-output time series.

EnbPI allows you to update conformal scores using the partial_fit function. It will replace the oldest one with the newest scores. It will keep the same amount of total scores

Actually, EnbPI only corresponds to MapieTimeSeriesRegressor if the cv argument is of type BlockBootstrap.

The ACI strategy allows you to adapt the conformal inference (i.e the quantile). If the real values are not in the coverage, the size of the intervals will grow. Conversely, if the real values are in the coverage, the size of the intervals will decrease. You can use a gamma coefficient to adjust the strength of the correction. If the quantile is equal to zero, the method will produce an infinite set size.

References

Chen Xu, and Yao Xie. “Conformal prediction for dynamic time-series.” https://arxiv.org/abs/2010.09107

Isaac Gibbs, Emmanuel Candes “Adaptive conformal inference under distribution shift” https://proceedings.neurips.cc/paper/2021/file/0d441de75945e5acbc865406fc9a2559-Paper.pdf

Margaux Zaffran et al. “Adaptive Conformal Predictions for Time Series” https://arxiv.org/pdf/2202.07282.pdf

__init__(estimator: Optional[RegressorMixin] = None, method: str = 'enbpi', cv: Optional[Union[int, str, BaseCrossValidator]] = None, n_jobs: Optional[int] = None, agg_function: Optional[str] = 'mean', verbose: int = 0, conformity_score: Optional[BaseRegressionScore] = None, random_state: Optional[Union[int, RandomState]] = None) None[source]
adapt_conformal_inference(X: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], y: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], gamma: float, alpha: Optional[Union[float, Iterable[float]]] = None, ensemble: bool = False, optimize_beta: bool = False) MapieTimeSeriesRegressor[source]

Adapt the alpha_t attribute when new data with known labels are available.

Parameters
X: ArrayLike of shape (n_samples, n_features)

Input data.

y: ArrayLike of shape (n_samples_test,)

Input labels.

ensemble: bool

Boolean determining whether the predictions are ensembled or not. If False, predictions are those of the model trained on the whole training set. If True, predictions from perturbed models are aggregated by the aggregation function specified in the agg_function attribute. If cv is "prefit" or "split", ensemble is ignored.

By default False.

gamma: float

Coefficient that decides the correction of the conformal inference. If it equals 0, there are no corrections.

alpha: Optional[Union[float, Iterable[float]]]

Between 0 and 1, represents the uncertainty of the confidence interval.

By default None.

optimize_beta: bool

Whether to optimize the PIs’ width or not.

By default False.

Returns
MapieTimeSeriesRegressor

The model itself.

Raises
ValueError

If the length of y is greater than the length of the training set.

partial_fit(X: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], y: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], ensemble: bool = False) MapieTimeSeriesRegressor[source]

Update the conformity_scores_ attribute when new data with known labels are available. Note: Don’t use partial_fit with samples of the training set.

Parameters
X: ArrayLike of shape (n_samples_test, n_features)

Input data.

y: ArrayLike of shape (n_samples_test,)

Input labels.

ensemble: bool

Boolean determining whether the predictions are ensembled or not. If False, predictions are those of the model trained on the whole training set. If True, predictions from perturbed models are aggregated by the aggregation function specified in the agg_function attribute. If cv is "prefit" or "split", ensemble is ignored.

By default False.

Returns
MapieTimeSeriesRegressor

The model itself.

Raises
ValueError

If the length of y is greater than the length of the training set.

predict(X: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], ensemble: bool = False, alpha: Optional[Union[float, Iterable[float]]] = None, optimize_beta: bool = False, allow_infinite_bounds: bool = False, **predict_params) Union[ndarray[Any, dtype[_ScalarType_co]], Tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]]][source]

Predict target on new samples with confidence intervals.

Parameters
X: ArrayLike of shape (n_samples, n_features)

Test data.

ensemble: bool

Boolean determining whether the predictions are ensembled or not. If False, predictions are those of the model trained on the whole training set. If True, predictions from perturbed models are aggregated by the aggregation function specified in the agg_function attribute. If cv is "prefit" or "split", ensemble is ignored.

By default False.

alpha: Optional[Union[float, Iterable[float]]]

Between 0 and 1, represents the uncertainty of the confidence interval.

By default None.

optimize_beta: bool

Whether to optimize the PIs’ width or not.

By default False.

allow_infinite_bounds: bool

Allow infinite prediction intervals to be produced.

predict_paramsdict

Additional predict parameters.

Returns
Union[NDArray, Tuple[NDArray, NDArray]]
  • NDArray of shape (n_samples,) if alpha is None.

  • Tuple[NDArray, NDArray] of shapes (n_samples,) and (n_samples, 2, n_alpha) if alpha is not None.

    • [:, 0, :]: Lower bound of the prediction interval.

    • [:, 1, :]: Upper bound of the prediction interval.

set_fit_request(*, groups: Union[bool, None, str] = '$UNCHANGED$', sample_weight: Union[bool, None, str] = '$UNCHANGED$') MapieTimeSeriesRegressor

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters
groupsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for groups parameter in fit.

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns
selfobject

The updated object.

set_partial_fit_request(*, ensemble: Union[bool, None, str] = '$UNCHANGED$') MapieTimeSeriesRegressor

Request metadata passed to the partial_fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to partial_fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to partial_fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters
ensemblestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for ensemble parameter in partial_fit.

Returns
selfobject

The updated object.

set_predict_request(*, allow_infinite_bounds: Union[bool, None, str] = '$UNCHANGED$', alpha: Union[bool, None, str] = '$UNCHANGED$', ensemble: Union[bool, None, str] = '$UNCHANGED$', optimize_beta: Union[bool, None, str] = '$UNCHANGED$') MapieTimeSeriesRegressor

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters
allow_infinite_boundsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for allow_infinite_bounds parameter in predict.

alphastr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for alpha parameter in predict.

ensemblestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for ensemble parameter in predict.

optimize_betastr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for optimize_beta parameter in predict.

Returns
selfobject

The updated object.

set_score_request(*, sample_weight: Union[bool, None, str] = '$UNCHANGED$') MapieTimeSeriesRegressor

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns
selfobject

The updated object.

update(X: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], y: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], ensemble: bool = False, alpha: Optional[Union[float, Iterable[float]]] = None, gamma: float = 0.0, optimize_beta: bool = False) MapieTimeSeriesRegressor[source]

Update with respect to the used method. method="enbpi" will call partial_fit method and method="aci" will call adapt_conformal_inference method.

Parameters
X: ArrayLike of shape (n_samples, n_features)

Input data.

y: ArrayLike of shape (n_samples_test,)

Input labels.

ensemble: bool

Boolean determining whether the predictions are ensembled or not. If False, predictions are those of the model trained on the whole training set. If True, predictions from perturbed models are aggregated by the aggregation function specified in the agg_function attribute. If cv is "prefit" or "split", ensemble is ignored.

By default False.

alpha: Optional[Union[float, Iterable[float]]]

Between 0 and 1, represents the uncertainty of the confidence interval.

By default None.

gamma: float

Coefficient that decides the correction of the conformal inference. If it equals 0, there are no corrections.

By default 0..

optimize_beta: bool

Whether to optimize the PIs’ width or not.

By default False.

Returns
MapieTimeSeriesRegressor

The model itself.

Raises
ValueError

If the length of y is greater than the length of the training set.