Skip to content

Regression

Conformal prediction methods for regression tasks.

mapie.regression.SplitConformalRegressor

SplitConformalRegressor(
    estimator: RegressorMixin = LinearRegression(),
    confidence_level: Union[float, Iterable[float]] = 0.9,
    conformity_score: Union[
        str, BaseRegressionScore
    ] = "absolute",
    prefit: bool = True,
    n_jobs: Optional[int] = None,
    verbose: int = 0,
)

Computes prediction intervals using the split conformal regression technique:

  1. The fit method (optional) fits the base regressor to the training data.
  2. The conformalize method estimates the uncertainty of the base regressor by computing conformity scores on the conformalization set.
  3. The predict_interval method predicts points and intervals.
PARAMETER DESCRIPTION
estimator

The base regressor used to predict points.

TYPE: RegressorMixin DEFAULT: LinearRegression()

confidence_level

The confidence level(s) for the prediction intervals, indicating the desired coverage probability of the prediction intervals. If a float is provided, it represents a single confidence level. If a list, multiple prediction intervals for each specified confidence level are returned.

TYPE: Union[float, List[float]] DEFAULT: 0.9

conformity_score

The method used to compute conformity scores

Valid options:

  • "absolute"
  • "gamma"
  • "residual_normalized"
  • Any subclass of BaseRegressionScore

A custom score function inheriting from BaseRegressionScore may also be provided.

See theoretical description (conformity scores).

TYPE: Union[str, BaseRegressionScore] DEFAULT: "absolute"

prefit

If True, the base regressor must be fitted, and the fit method must be skipped.

If False, the base regressor will be fitted during the fit method.

TYPE: bool DEFAULT: True

n_jobs

The number of jobs to run in parallel when applicable.

TYPE: Optional[int] DEFAULT: None

verbose

Controls the verbosity level. Higher values increase the output details.

TYPE: int DEFAULT: 0

Examples:

>>> from mapie.regression import SplitConformalRegressor
>>> from mapie.utils import train_conformalize_test_split
>>> from sklearn.datasets import make_regression
>>> from sklearn.linear_model import Ridge
>>> X, y = make_regression(n_samples=500, n_features=2, noise=1.0)
>>> (
...     X_train, X_conformalize, X_test,
...     y_train, y_conformalize, y_test
... ) = train_conformalize_test_split(
...     X, y, train_size=0.6, conformalize_size=0.2, test_size=0.2, random_state=1
... )
>>> mapie_regressor = SplitConformalRegressor(
...     estimator=Ridge(),
...     confidence_level=0.95,
...     prefit=False,
... ).fit(X_train, y_train).conformalize(X_conformalize, y_conformalize)
>>> predicted_points, predicted_intervals = mapie_regressor.predict_interval(X_test)
Source code in mapie/regression/regression.py
def __init__(
    self,
    estimator: RegressorMixin = LinearRegression(),
    confidence_level: Union[float, Iterable[float]] = 0.9,
    conformity_score: Union[str, BaseRegressionScore] = "absolute",
    prefit: bool = True,
    n_jobs: Optional[int] = None,
    verbose: int = 0,
) -> None:
    _check_estimator_fit_predict(estimator)
    self._estimator = estimator
    self._prefit = prefit
    self._is_fitted = prefit
    self._is_conformalized = False
    self._conformity_score = check_and_select_conformity_score(
        conformity_score,
        BaseRegressionScore,
    )

    # Note to developers: to implement this v1 class without touching the
    # v0 backend, we're for now using a hack. We always set cv="prefit",
    # and we fit the estimator if needed. See the .fit method below.
    self._mapie_regressor = _MapieRegressor(
        estimator=self._estimator,
        method="base",
        cv="prefit",
        n_jobs=n_jobs,
        verbose=verbose,
        conformity_score=self._conformity_score,
    )

    self._alphas = _transform_confidence_level_to_alpha_list(confidence_level)
    self._predict_params: dict = {}

fit

fit(
    X_train: ArrayLike,
    y_train: ArrayLike,
    fit_params: Optional[dict] = None,
) -> SplitConformalRegressor

Fits the base regressor to the training data.

PARAMETER DESCRIPTION
X_train

Training data features.

TYPE: ArrayLike

y_train

Training data targets.

TYPE: ArrayLike

fit_params

Parameters to pass to the fit method of the base regressor.

TYPE: Optional[dict] DEFAULT: None

RETURNS DESCRIPTION
Self

The fitted SplitConformalRegressor instance.

Source code in mapie/regression/regression.py
def fit(
    self,
    X_train: ArrayLike,
    y_train: ArrayLike,
    fit_params: Optional[dict] = None,
) -> SplitConformalRegressor:
    """
    Fits the base regressor to the training data.

    Parameters
    ----------
    X_train : ArrayLike
        Training data features.

    y_train : ArrayLike
        Training data targets.

    fit_params : Optional[dict], default=None
        Parameters to pass to the `fit` method of the base regressor.

    Returns
    -------
    Self
        The fitted SplitConformalRegressor instance.
    """
    _raise_error_if_fit_called_in_prefit_mode(self._prefit)
    _raise_error_if_method_already_called("fit", self._is_fitted)

    cloned_estimator = clone(self._estimator)
    fit_params_ = _prepare_params(fit_params)
    cloned_estimator.fit(X_train, y_train, **fit_params_)
    self._mapie_regressor.estimator = cloned_estimator

    self._is_fitted = True
    return self

conformalize

conformalize(
    X_conformalize: ArrayLike,
    y_conformalize: ArrayLike,
    predict_params: Optional[dict] = None,
) -> SplitConformalRegressor

Estimates the uncertainty of the base regressor by computing conformity scores on the conformalization set.

PARAMETER DESCRIPTION
X_conformalize

Features of the conformalization set.

TYPE: ArrayLike

y_conformalize

Targets of the conformalization set.

TYPE: ArrayLike

predict_params

Parameters to pass to the predict method of the base regressor. These parameters will also be used in the predict_interval and predict methods of this SplitConformalRegressor.

TYPE: Optional[dict] DEFAULT: None

RETURNS DESCRIPTION
Self

The conformalized SplitConformalRegressor instance.

Source code in mapie/regression/regression.py
def conformalize(
    self,
    X_conformalize: ArrayLike,
    y_conformalize: ArrayLike,
    predict_params: Optional[dict] = None,
) -> SplitConformalRegressor:
    """
    Estimates the uncertainty of the base regressor by computing
    conformity scores on the conformalization set.

    Parameters
    ----------
    X_conformalize : ArrayLike
        Features of the conformalization set.

    y_conformalize : ArrayLike
        Targets of the conformalization set.

    predict_params : Optional[dict], default=None
        Parameters to pass to the `predict` method of the base regressor.
        These parameters will also be used in the `predict_interval`
        and `predict` methods of this SplitConformalRegressor.

    Returns
    -------
    Self
        The conformalized SplitConformalRegressor instance.
    """
    _raise_error_if_previous_method_not_called(
        "conformalize",
        "fit",
        self._is_fitted,
    )
    _raise_error_if_method_already_called(
        "conformalize",
        self._is_conformalized,
    )

    self._predict_params = _prepare_params(predict_params)
    self._mapie_regressor.fit(
        X_conformalize, y_conformalize, predict_params=self._predict_params
    )

    self._is_conformalized = True
    return self

predict_interval

predict_interval(
    X: ArrayLike,
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray]

Predicts points (using the base regressor) and intervals.

If several confidence levels were provided during initialisation, several intervals will be predicted for each sample. See the return signature.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

minimize_interval_width

If True, attempts to minimize the intervals width.

TYPE: bool DEFAULT: False

allow_infinite_bounds

If True, allows prediction intervals with infinite bounds.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Tuple[NDArray, NDArray]

Two arrays:

  • Prediction points, of shape (n_samples,)
  • Prediction intervals, of shape (n_samples, 2, n_confidence_levels)
Source code in mapie/regression/regression.py
def predict_interval(
    self,
    X: ArrayLike,
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray]:
    """
    Predicts points (using the base regressor) and intervals.

    If several confidence levels were provided during initialisation, several
    intervals will be predicted for each sample. See the return signature.

    Parameters
    ----------
    X : ArrayLike
        Features

    minimize_interval_width : bool, default=False
        If True, attempts to minimize the intervals width.

    allow_infinite_bounds : bool, default=False
        If True, allows prediction intervals with infinite bounds.

    Returns
    -------
    Tuple[NDArray, NDArray]
        Two arrays:

        - Prediction points, of shape `(n_samples,)`
        - Prediction intervals, of shape `(n_samples, 2, n_confidence_levels)`
    """
    _raise_error_if_previous_method_not_called(
        "predict_interval",
        "conformalize",
        self._is_conformalized,
    )
    predictions = self._mapie_regressor.predict(
        X,
        alpha=self._alphas,
        optimize_beta=minimize_interval_width,
        allow_infinite_bounds=allow_infinite_bounds,
        **self._predict_params,
    )
    return _cast_predictions_to_ndarray_tuple(predictions)

predict

predict(X: ArrayLike) -> NDArray

Predicts points.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

RETURNS DESCRIPTION
NDArray

Array of point predictions, with shape (n_samples,).

Source code in mapie/regression/regression.py
def predict(
    self,
    X: ArrayLike,
) -> NDArray:
    """
    Predicts points.

    Parameters
    ----------
    X : ArrayLike
        Features

    Returns
    -------
    NDArray
        Array of point predictions, with shape (n_samples,).
    """
    _raise_error_if_previous_method_not_called(
        "predict",
        "conformalize",
        self._is_conformalized,
    )
    predictions = self._mapie_regressor.predict(
        X, alpha=None, **self._predict_params
    )
    return _cast_point_predictions_to_ndarray(predictions)

mapie.regression.CrossConformalRegressor

CrossConformalRegressor(
    estimator: RegressorMixin = LinearRegression(),
    confidence_level: Union[float, Iterable[float]] = 0.9,
    conformity_score: Union[
        str, BaseRegressionScore
    ] = "absolute",
    method: str = "plus",
    cv: Union[int, BaseCrossValidator] = 5,
    n_jobs: Optional[int] = None,
    verbose: int = 0,
    random_state: Optional[Union[int, RandomState]] = None,
)

Computes prediction intervals using the cross conformal regression technique:

  1. The fit_conformalize method estimates the uncertainty of the base regressor in a cross-validation style. It fits the base regressor on folds of the dataset and computes conformity scores on the out-of-fold data.
  2. The predict_interval computes prediction points and intervals.
PARAMETER DESCRIPTION
estimator

The base regressor used to predict points.

TYPE: RegressorMixin DEFAULT: LinearRegression()

confidence_level

The confidence level(s) for the prediction intervals, indicating the desired coverage probability of the prediction intervals. If a float is provided, it represents a single confidence level. If a list, multiple prediction intervals for each specified confidence level are returned.

TYPE: Union[float, List[float]] DEFAULT: 0.9

conformity_score

The method used to compute conformity scores Valid options:

  • "absolute"
  • "gamma"
  • The corresponding subclasses of BaseRegressionScore

A custom score function inheriting from BaseRegressionScore may also be provided.

See theoretical description (conformity scores).

TYPE: Union[str, BaseRegressionScore] DEFAULT: "absolute"

method

The method used to compute prediction intervals. Options are:

  • "base": Based on the conformity scores from each fold.
  • "plus": Based on the conformity scores from each fold and the test set predictions.
  • "minmax": Based on the conformity scores from each fold and the test set predictions, using the minimum and maximum among each fold models.

TYPE: str DEFAULT: "plus"

cv

The cross-validator used to compute conformity scores. Valid options:

  • integer, to specify the number of folds
  • any sklearn.model_selection.BaseCrossValidator suitable for regression, or a custom cross-validator inheriting from it.

Main variants in the cross conformal setting are:

  • sklearn.model_selection.KFold (vanilla cross conformal)
  • sklearn.model_selection.LeaveOneOut (jackknife)

TYPE: Union[int, BaseCrossValidator] DEFAULT: 5

n_jobs

The number of jobs to run in parallel when applicable.

TYPE: Optional[int] DEFAULT: None

verbose

Controls the verbosity level. Higher values increase the output details.

TYPE: int DEFAULT: 0

random_state

A seed or random state instance to ensure reproducibility in any random operations within the regressor.

TYPE: Optional[Union[int, RandomState]] DEFAULT: None

Examples:

>>> from mapie.regression import CrossConformalRegressor
>>> from sklearn.datasets import make_regression
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.linear_model import Ridge
>>> X_full, y_full = make_regression(n_samples=500,n_features=2,noise=1.0)
>>> X, X_test, y, y_test = train_test_split(X_full, y_full)
>>> mapie_regressor = CrossConformalRegressor(
...     estimator=Ridge(),
...     confidence_level=0.95,
...     cv=10
... ).fit_conformalize(X, y)
>>> predicted_points, predicted_intervals = mapie_regressor.predict_interval(X_test)
Source code in mapie/regression/regression.py
def __init__(
    self,
    estimator: RegressorMixin = LinearRegression(),
    confidence_level: Union[float, Iterable[float]] = 0.9,
    conformity_score: Union[str, BaseRegressionScore] = "absolute",
    method: str = "plus",
    cv: Union[int, BaseCrossValidator] = 5,
    n_jobs: Optional[int] = None,
    verbose: int = 0,
    random_state: Optional[Union[int, np.random.RandomState]] = None,
) -> None:
    _check_if_param_in_allowed_values(
        method, "method", CrossConformalRegressor._VALID_METHODS
    )
    _check_cv_not_string(cv)

    self._mapie_regressor = _MapieRegressor(
        estimator=estimator,
        method=method,
        cv=cv,
        n_jobs=n_jobs,
        verbose=verbose,
        conformity_score=check_and_select_conformity_score(
            conformity_score,
            BaseRegressionScore,
        ),
        random_state=random_state,
    )

    self._alphas = _transform_confidence_level_to_alpha_list(confidence_level)
    self.is_fitted_and_conformalized = False

    self._predict_params: dict = {}

fit_conformalize

fit_conformalize(
    X: ArrayLike,
    y: ArrayLike,
    groups: Optional[ArrayLike] = None,
    fit_params: Optional[dict] = None,
    predict_params: Optional[dict] = None,
) -> CrossConformalRegressor

Estimates the uncertainty of the base regressor in a cross-validation style: fits the base regressor on different folds of the dataset and computes conformity scores on the corresponding out-of-fold data.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

y

Targets

TYPE: ArrayLike

groups

Groups to pass to the cross-validator.

TYPE: Optional[ArrayLike] DEFAULT: None

fit_params

Parameters to pass to the fit method of the base regressor.

TYPE: Optional[dict] DEFAULT: None

predict_params

Parameters to pass to the predict method of the base regressor. These parameters will also be used in the predict_interval and predict methods of this CrossConformalRegressor.

TYPE: Optional[dict] DEFAULT: None

RETURNS DESCRIPTION
Self

This CrossConformalRegressor instance, fitted and conformalized.

Source code in mapie/regression/regression.py
def fit_conformalize(
    self,
    X: ArrayLike,
    y: ArrayLike,
    groups: Optional[ArrayLike] = None,
    fit_params: Optional[dict] = None,
    predict_params: Optional[dict] = None,
) -> CrossConformalRegressor:
    """
    Estimates the uncertainty of the base regressor in a cross-validation style:
    fits the base regressor on different folds of the dataset
    and computes conformity scores on the corresponding out-of-fold data.

    Parameters
    ----------
    X : ArrayLike
        Features

    y : ArrayLike
        Targets

    groups: Optional[ArrayLike] of shape (n_samples,), default=None
        Groups to pass to the cross-validator.

    fit_params : Optional[dict], default=None
        Parameters to pass to the `fit` method of the base regressor.

    predict_params : Optional[dict], default=None
        Parameters to pass to the `predict` method of the base regressor.
        These parameters will also be used in the `predict_interval`
        and `predict` methods of this CrossConformalRegressor.

    Returns
    -------
    Self
        This CrossConformalRegressor instance, fitted and conformalized.
    """
    _raise_error_if_method_already_called(
        "fit_conformalize",
        self.is_fitted_and_conformalized,
    )

    fit_params_, sample_weight = _prepare_fit_params_and_sample_weight(fit_params)
    self._predict_params = _prepare_params(predict_params)
    self._mapie_regressor.fit(
        X,
        y,
        sample_weight,
        groups,
        fit_params=fit_params_,
        predict_params=self._predict_params,
    )

    self.is_fitted_and_conformalized = True
    return self

predict_interval

predict_interval(
    X: ArrayLike,
    aggregate_predictions: Optional[str] = "mean",
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray]

Predicts points and intervals.

If several confidence levels were provided during initialisation, several intervals will be predicted for each sample. See the return signature.

By default, points are predicted using an aggregation. See the ensemble parameter.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

aggregate_predictions

The method to predict a point. Options:

  • None: a point is predicted using the regressor trained on the entire data
  • "mean": Averages the predictions of the regressors trained on each cross-validation fold
  • "median": Aggregates (using median) the predictions of the regressors trained on each cross-validation fold

TYPE: Optional[str] DEFAULT: "mean"

minimize_interval_width

If True, attempts to minimize the interval width.

TYPE: bool DEFAULT: False

allow_infinite_bounds

If True, allows prediction intervals with infinite bounds.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Tuple[NDArray, NDArray]

Two arrays:

  • Prediction points, of shape (n_samples,)
  • Prediction intervals, of shape (n_samples, 2, n_confidence_levels)
Source code in mapie/regression/regression.py
def predict_interval(
    self,
    X: ArrayLike,
    aggregate_predictions: Optional[str] = "mean",
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray]:
    """
    Predicts points and intervals.

    If several confidence levels were provided during initialisation, several
    intervals will be predicted for each sample. See the return signature.

    By default, points are predicted using an aggregation.
    See the `ensemble` parameter.

    Parameters
    ----------
    X : ArrayLike
        Features

    aggregate_predictions : Optional[str], default="mean"
        The method to predict a point. Options:

        - None: a point is predicted using the regressor trained on the entire data
        - "mean": Averages the predictions of the regressors trained on each
          cross-validation fold
        - "median": Aggregates (using median) the predictions of the regressors
          trained on each cross-validation fold

    minimize_interval_width : bool, default=False
        If True, attempts to minimize the interval width.

    allow_infinite_bounds : bool, default=False
        If True, allows prediction intervals with infinite bounds.

    Returns
    -------
    Tuple[NDArray, NDArray]
        Two arrays:

        - Prediction points, of shape `(n_samples,)`
        - Prediction intervals, of shape `(n_samples, 2, n_confidence_levels)`
    """
    _raise_error_if_previous_method_not_called(
        "predict_interval",
        "fit_conformalize",
        self.is_fitted_and_conformalized,
    )

    ensemble = self._set_aggregate_predictions_and_return_ensemble(
        aggregate_predictions
    )
    predictions = self._mapie_regressor.predict(
        X,
        alpha=self._alphas,
        optimize_beta=minimize_interval_width,
        allow_infinite_bounds=allow_infinite_bounds,
        ensemble=ensemble,
        **self._predict_params,
    )
    return _cast_predictions_to_ndarray_tuple(predictions)

predict

predict(
    X: ArrayLike,
    aggregate_predictions: Optional[str] = "mean",
) -> NDArray

Predicts points.

By default, points are predicted using an aggregation. See the ensemble parameter.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

aggregate_predictions

The method to predict a point. Options:

  • None: a point is predicted using the regressor trained on the entire data
  • "mean": Averages the predictions of the regressors trained on each cross-validation fold
  • "median": Aggregates (using median) the predictions of the regressors trained on each cross-validation fold

TYPE: Optional[str] DEFAULT: "mean"

RETURNS DESCRIPTION
NDArray

Array of point predictions, with shape (n_samples,).

Source code in mapie/regression/regression.py
def predict(
    self,
    X: ArrayLike,
    aggregate_predictions: Optional[str] = "mean",
) -> NDArray:
    """
    Predicts points.

    By default, points are predicted using an aggregation.
    See the `ensemble` parameter.

    Parameters
    ----------
    X : ArrayLike
        Features

    aggregate_predictions : Optional[str], default="mean"
        The method to predict a point. Options:

        - None: a point is predicted using the regressor trained on the entire data
        - "mean": Averages the predictions of the regressors trained on each
          cross-validation fold
        - "median": Aggregates (using median) the predictions of the regressors
          trained on each cross-validation fold

    Returns
    -------
    NDArray
        Array of point predictions, with shape `(n_samples,)`.
    """
    _raise_error_if_previous_method_not_called(
        "predict",
        "fit_conformalize",
        self.is_fitted_and_conformalized,
    )

    ensemble = self._set_aggregate_predictions_and_return_ensemble(
        aggregate_predictions
    )
    predictions = self._mapie_regressor.predict(
        X,
        alpha=None,
        ensemble=ensemble,
        **self._predict_params,
    )
    return _cast_point_predictions_to_ndarray(predictions)

mapie.regression.JackknifeAfterBootstrapRegressor

JackknifeAfterBootstrapRegressor(
    estimator: RegressorMixin = LinearRegression(),
    confidence_level: Union[float, Iterable[float]] = 0.9,
    conformity_score: Union[
        str, BaseRegressionScore
    ] = "absolute",
    method: str = "plus",
    resampling: Union[int, Subsample] = 30,
    aggregation_method: str = "mean",
    n_jobs: Optional[int] = None,
    verbose: int = 0,
    random_state: Optional[Union[int, RandomState]] = None,
)

Computes prediction intervals using the jackknife-after-bootstrap technique:

  1. The fit_conformalize method estimates the uncertainty of the base regressor using bootstrap sampling. It fits the base regressor on samples of the dataset and computes conformity scores on the out-of-sample data.
  2. The predict_interval computes prediction points and intervals.
PARAMETER DESCRIPTION
estimator

The base regressor used to predict points.

TYPE: RegressorMixin DEFAULT: LinearRegression()

confidence_level

The confidence level(s) for the prediction intervals, indicating the desired coverage probability of the prediction intervals. If a float is provided, it represents a single confidence level. If a list, multiple prediction intervals for each specified confidence level are returned.

TYPE: Union[float, List[float]] DEFAULT: 0.9

conformity_score

The method used to compute conformity scores

Valid options:

  • "absolute"
  • "gamma"
  • The corresponding subclasses of BaseRegressionScore

A custom score function inheriting from BaseRegressionScore may also be provided.

See theoretical description (conformity scores).

TYPE: Union[str, BaseRegressionScore] DEFAULT: "absolute"

method

The method used to compute prediction intervals. Options are:

  • "plus": Based on the conformity scores from each bootstrap sample and the testing prediction.
  • "minmax": Based on the minimum and maximum conformity scores from each bootstrap sample.

Note: The "base" method is not mentioned in the conformal inference literature for Jackknife after bootstrap strategies, hence not provided here.

TYPE: str DEFAULT: "plus"

resampling

Number of bootstrap resamples or an instance of Subsample for custom sampling strategy.

TYPE: Union[int, Subsample] DEFAULT: 30

aggregation_method

Aggregation method for predictions across bootstrap samples. Options:

  • "mean"
  • "median"

TYPE: str DEFAULT: "mean"

n_jobs

The number of jobs to run in parallel when applicable.

TYPE: Optional[int] DEFAULT: None

verbose

Controls the verbosity level. Higher values increase the output details.

TYPE: int DEFAULT: 0

random_state

A seed or random state instance to ensure reproducibility in any random operations within the regressor.

TYPE: Optional[Union[int, RandomState]] DEFAULT: None

Examples:

>>> from mapie.regression import JackknifeAfterBootstrapRegressor
>>> from sklearn.datasets import make_regression
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.linear_model import Ridge
>>> X_full, y_full = make_regression(n_samples=500,n_features=2,noise=1.0)
>>> X, X_test, y, y_test = train_test_split(X_full, y_full)
>>> mapie_regressor = JackknifeAfterBootstrapRegressor(
...     estimator=Ridge(),
...     confidence_level=0.95,
...     resampling=25,
... ).fit_conformalize(X, y)
>>> predicted_points, predicted_intervals = mapie_regressor.predict_interval(X_test)
Source code in mapie/regression/regression.py
def __init__(
    self,
    estimator: RegressorMixin = LinearRegression(),
    confidence_level: Union[float, Iterable[float]] = 0.9,
    conformity_score: Union[str, BaseRegressionScore] = "absolute",
    method: str = "plus",
    resampling: Union[int, Subsample] = 30,
    aggregation_method: str = "mean",
    n_jobs: Optional[int] = None,
    verbose: int = 0,
    random_state: Optional[Union[int, np.random.RandomState]] = None,
) -> None:
    _check_if_param_in_allowed_values(
        method, "method", JackknifeAfterBootstrapRegressor._VALID_METHODS
    )
    _check_if_param_in_allowed_values(
        aggregation_method,
        "aggregation_method",
        JackknifeAfterBootstrapRegressor._VALID_AGGREGATION_METHODS,
    )

    cv = self._check_and_convert_resampling_to_cv(resampling)

    self._mapie_regressor = _MapieRegressor(
        estimator=estimator,
        method=method,
        cv=cv,
        n_jobs=n_jobs,
        verbose=verbose,
        agg_function=aggregation_method,
        conformity_score=check_and_select_conformity_score(
            conformity_score,
            BaseRegressionScore,
        ),
        random_state=random_state,
    )

    self._alphas = _transform_confidence_level_to_alpha_list(confidence_level)

    self.is_fitted_and_conformalized = False
    self._predict_params: dict = {}

fit_conformalize

fit_conformalize(
    X: ArrayLike,
    y: ArrayLike,
    fit_params: Optional[dict] = None,
    predict_params: Optional[dict] = None,
) -> JackknifeAfterBootstrapRegressor

Estimates the uncertainty of the base regressor using bootstrap sampling: fits the base regressor on (potentially overlapping) samples of the dataset, and computes conformity scores on the corresponding out of samples data.

PARAMETER DESCRIPTION
X

Features. Must be the same X used in .fit

TYPE: ArrayLike

y

Targets. Must be the same y used in .fit

TYPE: ArrayLike

fit_params

Parameters to pass to the fit method of the base regressor.

TYPE: Optional[dict] DEFAULT: None

predict_params

Parameters to pass to the predict method of the base regressor. These parameters will also be used in the predict_interval and predict methods of this JackknifeAfterBootstrapRegressor.

TYPE: Optional[dict] DEFAULT: None

RETURNS DESCRIPTION
Self

This JackknifeAfterBootstrapRegressor instance, fitted and conformalized.

Source code in mapie/regression/regression.py
def fit_conformalize(
    self,
    X: ArrayLike,
    y: ArrayLike,
    fit_params: Optional[dict] = None,
    predict_params: Optional[dict] = None,
) -> JackknifeAfterBootstrapRegressor:
    """
    Estimates the uncertainty of the base regressor using bootstrap sampling:
    fits the base regressor on (potentially overlapping) samples of the dataset,
    and computes conformity scores on the corresponding out of samples data.

    Parameters
    ----------
    X : ArrayLike
        Features. Must be the same X used in .fit

    y : ArrayLike
        Targets. Must be the same y used in .fit

    fit_params : Optional[dict], default=None
        Parameters to pass to the `fit` method of the base regressor.

    predict_params : Optional[dict], default=None
        Parameters to pass to the `predict` method of the base regressor.
        These parameters will also be used in the `predict_interval`
        and `predict` methods of this JackknifeAfterBootstrapRegressor.

    Returns
    -------
    Self
        This JackknifeAfterBootstrapRegressor instance, fitted and conformalized.
    """
    _raise_error_if_method_already_called(
        "fit_conformalize",
        self.is_fitted_and_conformalized,
    )

    fit_params_, sample_weight = _prepare_fit_params_and_sample_weight(fit_params)
    self._predict_params = _prepare_params(predict_params)
    self._mapie_regressor.fit(
        X,
        y,
        sample_weight,
        fit_params=fit_params_,
        predict_params=self._predict_params,
    )

    self.is_fitted_and_conformalized = True
    return self

predict_interval

predict_interval(
    X: ArrayLike,
    ensemble: bool = True,
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray]

Predicts points and intervals.

If several confidence levels were provided during initialisation, several intervals will be predicted for each sample. See the return signature.

By default, points are predicted using an aggregation. See the ensemble parameter.

PARAMETER DESCRIPTION
X

Test data for prediction intervals.

TYPE: ArrayLike

ensemble

If True, a predicted point is an aggregation of the predictions of the regressors trained on each bootstrap samples. This aggregation depends on the aggregation_method provided during initialisation.

If False, a point is predicted using the regressor trained on the entire data

TYPE: bool DEFAULT: True

minimize_interval_width

If True, attempts to minimize the interval width.

TYPE: bool DEFAULT: False

allow_infinite_bounds

If True, allows prediction intervals with infinite bounds.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Tuple[NDArray, NDArray]

Two arrays:

  • Prediction points, of shape (n_samples,)
  • Prediction intervals, of shape (n_samples, 2, n_confidence_levels)
Source code in mapie/regression/regression.py
def predict_interval(
    self,
    X: ArrayLike,
    ensemble: bool = True,
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray]:
    """
    Predicts points and intervals.

    If several confidence levels were provided during initialisation, several
    intervals will be predicted for each sample. See the return signature.

    By default, points are predicted using an aggregation.
    See the `ensemble` parameter.

    Parameters
    ----------
    X : ArrayLike
        Test data for prediction intervals.

    ensemble : bool, default=True
        If True, a predicted point is an aggregation of the predictions of the
        regressors trained on each bootstrap samples. This aggregation depends on
        the `aggregation_method` provided during initialisation.

        If False, a point is predicted using the regressor trained on the entire
        data

    minimize_interval_width : bool, default=False
        If True, attempts to minimize the interval width.

    allow_infinite_bounds : bool, default=False
        If True, allows prediction intervals with infinite bounds.

    Returns
    -------
    Tuple[NDArray, NDArray]
        Two arrays:

        - Prediction points, of shape `(n_samples,)`
        - Prediction intervals, of shape `(n_samples, 2, n_confidence_levels)`
    """
    _raise_error_if_previous_method_not_called(
        "predict_interval",
        "fit_conformalize",
        self.is_fitted_and_conformalized,
    )

    predictions = self._mapie_regressor.predict(
        X,
        alpha=self._alphas,
        optimize_beta=minimize_interval_width,
        allow_infinite_bounds=allow_infinite_bounds,
        ensemble=ensemble,
        **self._predict_params,
    )
    return _cast_predictions_to_ndarray_tuple(predictions)

predict

predict(X: ArrayLike, ensemble: bool = True) -> NDArray

Predicts points.

By default, points are predicted using an aggregation. See the ensemble parameter.

PARAMETER DESCRIPTION
X

Data features for generating point predictions.

TYPE: ArrayLike

ensemble

If True, a predicted point is an aggregation of the predictions of the regressors trained on each bootstrap samples. This aggregation depends on the aggregation_method provided during initialisation. If False, a point is predicted using the regressor trained on the entire data

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
NDArray

Array of point predictions, with shape (n_samples,).

Source code in mapie/regression/regression.py
def predict(
    self,
    X: ArrayLike,
    ensemble: bool = True,
) -> NDArray:
    """
    Predicts points.

    By default, points are predicted using an aggregation.
    See the `ensemble` parameter.

    Parameters
    ----------
    X : ArrayLike
        Data features for generating point predictions.

    ensemble : bool, default=True
        If True, a predicted point is an aggregation of the predictions of the
        regressors trained on each bootstrap samples. This aggregation depends on
        the `aggregation_method` provided during initialisation.
        If False, a point is predicted using the regressor trained on the entire
        data

    Returns
    -------
    NDArray
        Array of point predictions, with shape `(n_samples,)`.
    """
    _raise_error_if_previous_method_not_called(
        "predict",
        "fit_conformalize",
        self.is_fitted_and_conformalized,
    )

    predictions = self._mapie_regressor.predict(
        X,
        alpha=None,
        ensemble=ensemble,
        **self._predict_params,
    )
    return _cast_point_predictions_to_ndarray(predictions)

mapie.regression.ConformalizedQuantileRegressor

ConformalizedQuantileRegressor(
    estimator: Optional[
        Union[
            RegressorMixin,
            Pipeline,
            List[Union[RegressorMixin, Pipeline]],
        ]
    ] = None,
    confidence_level: float = 0.9,
    prefit: bool = False,
)

Computes prediction intervals using the conformalized quantile regression technique:

  1. The fit method fits three models to the training data using the provided regressor: a model to predict the target, and models to predict upper and lower quantiles around the target.
  2. The conformalize method estimates the uncertainty of the quantile models using the conformalization set.
  3. The predict_interval computes prediction points and intervals.
PARAMETER DESCRIPTION
estimator

The regressor used to predict points and quantiles.

When prefit=False (default), a single regressor that supports the quantile loss must be passed. Valid options:

  • sklearn.linear_model.QuantileRegressor
  • sklearn.ensemble.GradientBoostingRegressor
  • sklearn.ensemble.HistGradientBoostingRegressor
  • lightgbm.LGBMRegressor

When prefit=True, a list of three fitted quantile regressors predicting the lower, upper, and median quantiles must be passed (in that order). These quantiles must be:

  • lower quantile = (1 - confidence_level) / 2
  • upper quantile = (1 + confidence_level) / 2
  • median quantile = 0.5

TYPE: Union[`RegressorMixin`, `Pipeline`, `List[Union[RegressorMixin, Pipeline]]`] DEFAULT: None

confidence_level

The confidence level for the prediction intervals, indicating the desired coverage probability of the prediction intervals.

TYPE: float default=0.9 DEFAULT: 0.9

prefit

If True, three fitted quantile regressors must be provided, and the fit method must be skipped.

If False, the three regressors will be fitted during the fit method.

TYPE: bool DEFAULT: False

Examples:

>>> from mapie.regression import ConformalizedQuantileRegressor
>>> from mapie.utils import train_conformalize_test_split
>>> from sklearn.datasets import make_regression
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.linear_model import QuantileRegressor
>>> X, y = make_regression(n_samples=500, n_features=2, noise=1.0)
>>> (
...     X_train, X_conformalize, X_test,
...     y_train, y_conformalize, y_test
... ) = train_conformalize_test_split(
...     X, y, train_size=0.6, conformalize_size=0.2, test_size=0.2, random_state=1
... )
>>> mapie_regressor = ConformalizedQuantileRegressor(
...     estimator=QuantileRegressor(),
...     confidence_level=0.95,
... ).fit(X_train, y_train).conformalize(X_conformalize, y_conformalize)
>>> predicted_points, predicted_intervals = mapie_regressor.predict_interval(X_test)
Source code in mapie/regression/quantile_regression.py
def __init__(
    self,
    estimator: Optional[
        Union[RegressorMixin, Pipeline, List[Union[RegressorMixin, Pipeline]]]
    ] = None,
    confidence_level: float = 0.9,
    prefit: bool = False,
) -> None:
    self._alpha = _transform_confidence_level_to_alpha(confidence_level)
    self._prefit = prefit
    self._is_fitted = prefit
    self._is_conformalized = False

    self._mapie_quantile_regressor = _MapieQuantileRegressor(
        estimator=estimator,
        method="quantile",
        cv="prefit" if prefit else "split",
        alpha=self._alpha,
    )

    self._sample_weight: Optional[ArrayLike] = None
    self._predict_params: dict = {}

fit

fit(
    X_train: ArrayLike,
    y_train: ArrayLike,
    fit_params: Optional[dict] = None,
) -> ConformalizedQuantileRegressor

Fits three models using the regressor provided at initialisation:

  • a model to predict the target
  • a model to predict the upper quantile of the target
  • a model to predict the lower quantile of the target
PARAMETER DESCRIPTION
X_train

Training data features.

TYPE: ArrayLike

y_train

Training data targets.

TYPE: ArrayLike

fit_params

Parameters to pass to the fit method of the regressors.

TYPE: Optional[dict] DEFAULT: None

RETURNS DESCRIPTION
Self

The fitted ConformalizedQuantileRegressor instance.

Source code in mapie/regression/quantile_regression.py
def fit(
    self,
    X_train: ArrayLike,
    y_train: ArrayLike,
    fit_params: Optional[dict] = None,
) -> ConformalizedQuantileRegressor:
    """
    Fits three models using the regressor provided at initialisation:

    - a model to predict the target
    - a model to predict the upper quantile of the target
    - a model to predict the lower quantile of the target

    Parameters
    ----------
    X_train : ArrayLike
        Training data features.

    y_train : ArrayLike
        Training data targets.

    fit_params : Optional[dict], default=None
        Parameters to pass to the `fit` method of the regressors.

    Returns
    -------
    Self
        The fitted ConformalizedQuantileRegressor instance.
    """
    _raise_error_if_fit_called_in_prefit_mode(self._prefit)
    _raise_error_if_method_already_called("fit", self._is_fitted)

    fit_params_, self._sample_weight = _prepare_fit_params_and_sample_weight(
        fit_params
    )
    self._mapie_quantile_regressor._initialize_fit_conformalize()
    self._mapie_quantile_regressor._fit_estimators(
        X=X_train,
        y=y_train,
        sample_weight=self._sample_weight,
        **fit_params_,
    )

    self._is_fitted = True
    return self

conformalize

conformalize(
    X_conformalize: ArrayLike,
    y_conformalize: ArrayLike,
    predict_params: Optional[dict] = None,
) -> ConformalizedQuantileRegressor

Estimates the uncertainty of the quantile regressors by computing conformity scores on the conformalization set.

PARAMETER DESCRIPTION
X_conformalize

Features of the conformalization set.

TYPE: ArrayLike

y_conformalize

Targets of the conformalization set.

TYPE: ArrayLike

predict_params

Parameters to pass to the predict method of the regressors. These parameters will also be used in the predict_interval and predict methods of this SplitConformalRegressor.

TYPE: Optional[dict] DEFAULT: None

RETURNS DESCRIPTION
Self

The ConformalizedQuantileRegressor instance.

Source code in mapie/regression/quantile_regression.py
def conformalize(
    self,
    X_conformalize: ArrayLike,
    y_conformalize: ArrayLike,
    predict_params: Optional[dict] = None,
) -> ConformalizedQuantileRegressor:
    """
    Estimates the uncertainty of the quantile regressors by computing
    conformity scores on the conformalization set.

    Parameters
    ----------
    X_conformalize : ArrayLike
        Features of the conformalization set.

    y_conformalize : ArrayLike
        Targets of the conformalization set.

    predict_params : Optional[dict], default=None
        Parameters to pass to the `predict` method of the regressors.
        These parameters will also be used in the `predict_interval`
        and `predict` methods of this SplitConformalRegressor.

    Returns
    -------
    Self
        The ConformalizedQuantileRegressor instance.
    """
    _raise_error_if_previous_method_not_called(
        "conformalize",
        "fit",
        self._is_fitted,
    )
    _raise_error_if_method_already_called(
        "conformalize",
        self._is_conformalized,
    )

    self._predict_params = _prepare_params(predict_params)
    self._mapie_quantile_regressor.conformalize(
        X_conformalize, y_conformalize, **self._predict_params
    )

    self._is_conformalized = True
    return self

predict_interval

predict_interval(
    X: ArrayLike,
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
    symmetric_correction: bool = False,
) -> Tuple[NDArray, NDArray]

Predicts points (using the base regressor) and intervals.

The returned NDArray containing the prediction intervals is of shape (n_samples, 2, 1). The third dimension is unnecessary, but kept for consistency with the other conformal regression methods available in MAPIE.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

minimize_interval_width

If True, attempts to minimize the intervals width.

TYPE: bool DEFAULT: False

allow_infinite_bounds

If True, allows prediction intervals with infinite bounds.

TYPE: bool DEFAULT: False

symmetric_correction

To produce prediction intervals, the conformalized quantile regression technique corrects the predictions of the upper and lower quantile regressors by adding a constant.

If symmetric_correction is set to False , this constant is different for the upper and the lower quantile predictions. If set to True, this constant is the same for both.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Tuple[NDArray, NDArray]

Two arrays:

  • Prediction points, of shape (n_samples,)
  • Prediction intervals, of shape (n_samples, 2, 1)
Source code in mapie/regression/quantile_regression.py
def predict_interval(
    self,
    X: ArrayLike,
    minimize_interval_width: bool = False,
    allow_infinite_bounds: bool = False,
    symmetric_correction: bool = False,
) -> Tuple[NDArray, NDArray]:
    """
    Predicts points (using the base regressor) and intervals.

    The returned NDArray containing the prediction intervals is of shape
    (n_samples, 2, 1). The third dimension is unnecessary, but kept for consistency
    with the other conformal regression methods available in MAPIE.

    Parameters
    ----------
    X : ArrayLike
        Features

    minimize_interval_width : bool, default=False
        If True, attempts to minimize the intervals width.

    allow_infinite_bounds : bool, default=False
        If True, allows prediction intervals with infinite bounds.

    symmetric_correction : bool, default=False
        To produce prediction intervals, the conformalized quantile regression
        technique corrects the predictions of the upper and lower quantile
        regressors by adding a constant.

        If `symmetric_correction` is set to `False` , this constant is different
        for the upper and the lower quantile predictions. If set to `True`,
        this constant is the same for both.

    Returns
    -------
    Tuple[NDArray, NDArray]
        Two arrays:

        - Prediction points, of shape `(n_samples,)`
        - Prediction intervals, of shape `(n_samples, 2, 1)`
    """
    _raise_error_if_previous_method_not_called(
        "predict_interval",
        "conformalize",
        self._is_conformalized,
    )

    predictions = self._mapie_quantile_regressor.predict(
        X,
        optimize_beta=minimize_interval_width,
        allow_infinite_bounds=allow_infinite_bounds,
        symmetry=symmetric_correction,
        **self._predict_params,
    )
    return _cast_predictions_to_ndarray_tuple(predictions)

predict

predict(X: ArrayLike) -> NDArray

Predicts points.

PARAMETER DESCRIPTION
X

Features

TYPE: ArrayLike

RETURNS DESCRIPTION
NDArray

Array of point predictions with shape (n_samples,).

Source code in mapie/regression/quantile_regression.py
def predict(
    self,
    X: ArrayLike,
) -> NDArray:
    """
    Predicts points.

    Parameters
    ----------
    X : ArrayLike
        Features

    Returns
    -------
    NDArray
        Array of point predictions with shape `(n_samples,)`.
    """
    _raise_error_if_previous_method_not_called(
        "predict",
        "conformalize",
        self._is_conformalized,
    )

    estimator = self._mapie_quantile_regressor
    predictions, _ = estimator.predict(X, **self._predict_params)
    return predictions

mapie.regression.TimeSeriesRegressor

TimeSeriesRegressor(
    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,
)

Bases: _MapieRegressor

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 update 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 TimeSeriesRegressor 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

Source code in mapie/regression/time_series_regression.py
def __init__(
    self,
    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, np.random.RandomState]] = None,
) -> None:
    super().__init__(
        estimator=estimator,
        method=method,
        cv=cv,
        n_jobs=n_jobs,
        agg_function=agg_function,
        verbose=verbose,
        conformity_score=conformity_score,
        random_state=random_state,
    )

adapt_conformal_inference

adapt_conformal_inference(
    X: ArrayLike,
    y: ArrayLike,
    gamma: float,
    confidence_level: Optional[
        Union[float, Iterable[float]]
    ] = None,
    ensemble: bool = False,
    optimize_beta: bool = False,
) -> TimeSeriesRegressor

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

PARAMETER DESCRIPTION
X

Input data.

TYPE: ArrayLike

y

Input labels.

TYPE: ArrayLike

ensemble

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.

TYPE: bool DEFAULT: False

gamma

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

TYPE: float

confidence_level

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

By default None.

TYPE: Optional[Union[float, Iterable[float]]] DEFAULT: None

optimize_beta

Whether to optimize the PIs' width or not.

By default False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
TimeSeriesRegressor

The model itself.

RAISES DESCRIPTION
ValueError

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

Source code in mapie/regression/time_series_regression.py
def adapt_conformal_inference(
    self,
    X: ArrayLike,
    y: ArrayLike,
    gamma: float,
    confidence_level: Optional[Union[float, Iterable[float]]] = None,
    ensemble: bool = False,
    optimize_beta: bool = False,
) -> TimeSeriesRegressor:
    """
    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.

    confidence_level: Optional[Union[float, Iterable[float]]]
        Between `0` and `1`, represents the confidence level of the interval.

        By default `None`.

    optimize_beta: bool
        Whether to optimize the PIs' width or not.

        By default `False`.

    Returns
    -------
    TimeSeriesRegressor
        The model itself.

    Raises
    ------
    ValueError
        If the length of `y` is greater than
        the length of the training set.
    """
    if self.method != "aci":
        raise AttributeError(
            "This method can be called only with method='aci', "
            f"not with '{self.method}'."
        )

    check_is_fitted(self)
    self._check_gamma(gamma)
    X, y = cast(NDArray, X), cast(NDArray, y)

    self._get_alpha()
    alpha = self._transform_confidence_level_to_alpha_array(confidence_level)
    if alpha is None:
        alpha = np.array(list(self.current_alpha.keys()))
    alpha_np = cast(NDArray, alpha)

    for x_row, y_row in zip(X, y):
        x = np.expand_dims(x_row, axis=0)
        _, y_pred_bounds = self.predict(
            x,
            ensemble=ensemble,
            confidence_level=1 - alpha_np,
            optimize_beta=optimize_beta,
            allow_infinite_bounds=True,
        )

        for alpha_ix, alpha_0 in enumerate(alpha_np):
            alpha_t = self.current_alpha[alpha_0]

            is_lower_bounded = y_row > y_pred_bounds[:, 0, alpha_ix]
            is_upper_bounded = y_row < y_pred_bounds[:, 1, alpha_ix]
            is_not_bounded = not (is_lower_bounded and is_upper_bounded)

            new_alpha_t = alpha_t + gamma * (alpha_0 - is_not_bounded)
            self.current_alpha[alpha_0] = np.clip(new_alpha_t, 0, 1)

    return self

update

update(
    X: ArrayLike,
    y: ArrayLike,
    ensemble: bool = False,
    confidence_level: Optional[
        Union[float, Iterable[float]]
    ] = None,
    gamma: float = 0.0,
    optimize_beta: bool = False,
) -> TimeSeriesRegressor

Update conformity scores

PARAMETER DESCRIPTION
X

Input data.

TYPE: ArrayLike

y

Input labels.

TYPE: ArrayLike

ensemble

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.

TYPE: bool DEFAULT: False

confidence_level

(deprecated) Between 0 and 1, represents the confidence level of the interval.

By default None.

TYPE: Optional[Union[float, Iterable[float]]] DEFAULT: None

gamma

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

By default 0..

TYPE: float DEFAULT: 0.0

optimize_beta

(deprecated) Whether to optimize the PIs' width or not.

By default False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
TimeSeriesRegressor

The model itself.

RAISES DESCRIPTION
ValueError

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

Source code in mapie/regression/time_series_regression.py
def update(
    self,
    X: ArrayLike,
    y: ArrayLike,
    ensemble: bool = False,
    confidence_level: Optional[Union[float, Iterable[float]]] = None,
    gamma: float = 0.0,
    optimize_beta: bool = False,
) -> TimeSeriesRegressor:
    """
    Update conformity scores

    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`.

    confidence_level: Optional[Union[float, Iterable[float]]]
        (deprecated)
        Between `0` and `1`, represents the confidence level of the interval.

        By default `None`.

    gamma: float
        (deprecated)
        Coefficient that decides the correction of the conformal inference.
        If it equals 0, there are no corrections.

        By default `0.`.

    optimize_beta: bool
        (deprecated)
        Whether to optimize the PIs' width or not.

        By default `False`.

    Returns
    -------
    TimeSeriesRegressor
        The model itself.

    Raises
    ------
    ValueError
        If the length of `y` is greater than
        the length of the training set.
    """
    warn("""
    This function behavior has been changed to allow updating the scores even when using ACI.
    Currently the parameters confidence_level and optimize_beta have no effect. They are kept
    for API stability and will be removed in a future release.
    If you want to adapt confidence level, use adapt_conformal_inference instead.
    """)
    self._check_method(self.method)
    if self.method not in ["enbpi", "aci"]:
        raise ValueError(
            f"Invalid method. Allowed values are {self.valid_methods_}."
        )

    return self._update_conformity_scores_with_ensemble(X, y, ensemble=ensemble)

predict

predict(
    X: ArrayLike,
    ensemble: bool = False,
    confidence_level: Optional[
        Union[float, Iterable[float]]
    ] = None,
    optimize_beta: bool = False,
    allow_infinite_bounds: bool = False,
    **predict_params,
) -> Union[NDArray, Tuple[NDArray, NDArray]]

Predict target on new samples with confidence intervals.

PARAMETER DESCRIPTION
X

Test data.

TYPE: ArrayLike

ensemble

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.

TYPE: bool DEFAULT: False

confidence_level

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

By default None.

TYPE: Optional[Union[float, Iterable[float]]] DEFAULT: None

optimize_beta

Whether to optimize the PIs' width or not.

By default False.

TYPE: bool DEFAULT: False

allow_infinite_bounds

Allow infinite prediction intervals to be produced.

TYPE: bool DEFAULT: False

predict_params

Additional predict parameters.

TYPE: dict DEFAULT: {}

RETURNS DESCRIPTION
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.
Source code in mapie/regression/time_series_regression.py
def predict(  # type: ignore[override]
    self,
    X: ArrayLike,
    ensemble: bool = False,
    confidence_level: Optional[Union[float, Iterable[float]]] = None,
    optimize_beta: bool = False,
    allow_infinite_bounds: bool = False,
    **predict_params,
) -> Union[NDArray, Tuple[NDArray, NDArray]]:
    """
    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`.

    confidence_level: Optional[Union[float, Iterable[float]]]
        Between `0` and `1`, represents the confidence level of the 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_params : dict
        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.
    """
    alpha = self._transform_confidence_level_to_alpha_array(confidence_level)
    if alpha is None:
        super().predict(
            X,
            ensemble=ensemble,
            alpha=alpha,
            optimize_beta=optimize_beta,
            **predict_params,
        )
    if self.method == "aci":
        alpha = self._get_alpha(alpha)

    return super().predict(
        X,
        ensemble=ensemble,
        alpha=alpha,
        optimize_beta=optimize_beta,
        allow_infinite_bounds=allow_infinite_bounds,
        **predict_params,
    )