mapie.conformity_scores.BaseRegressionScore

class mapie.conformity_scores.BaseRegressionScore(sym: bool, consistency_check: bool = True, eps: float = 1e-08)[source]

Base conformity score class for regression task.

This class should not be used directly. Use derived classes instead.

Parameters
sym: bool

Whether to consider the conformity score as symmetrical or not.

consistency_check: bool, optional

Whether to check the consistency between the methods get_estimation_distribution and get_conformity_scores. If True, the following equality must be verified: ``self.get_estimation_distribution(

y_pred, self.get_conformity_scores(y, y_pred, **kwargs), **kwargs

) == y``

By default True.

eps: float, optional

Threshold to consider when checking the consistency between get_estimation_distribution and get_conformity_scores. It should be specified if consistency_check==True.

By default, it is defined by the default precision.

__init__(sym: bool, consistency_check: bool = True, eps: float = 1e-08)[source]
check_consistency(y: ndarray[Any, dtype[_ScalarType_co]], y_pred: ndarray[Any, dtype[_ScalarType_co]], conformity_scores: ndarray[Any, dtype[_ScalarType_co]], **kwargs) None[source]

Check consistency between the following methods: get_estimation_distribution and get_signed_conformity_scores

The following equality should be verified: ``self.get_estimation_distribution(

y_pred, self.get_conformity_scores(y, y_pred, **kwargs), **kwargs

) == y``

Parameters
y: NDArray of shape (n_samples,)

Observed target values.

y_pred: NDArray of shape (n_samples,)

Predicted target values.

conformity_scores: NDArray of shape (n_samples,)

Conformity scores.

Raises
ValueError

If the two methods are not consistent.

get_bounds(X: ndarray[Any, dtype[_ScalarType_co]], alpha_np: ndarray[Any, dtype[_ScalarType_co]], estimator: EnsembleRegressor, conformity_scores: ndarray[Any, dtype[_ScalarType_co]], ensemble: bool = False, method: str = 'base', optimize_beta: bool = False, allow_infinite_bounds: bool = False) Tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]][source]

Compute bounds of the prediction intervals from the observed values, the estimator of type EnsembleRegressor and the conformity scores.

Parameters
X: NDArray of shape (n_samples, n_features)

Observed feature values.

alpha_np: NDArray of shape (n_alpha,)

NDArray of floats between 0 and 1, represents the uncertainty of the confidence interval.

estimator: EnsembleRegressor

Estimator that is fitted to predict y from X.

conformity_scores: NDArray of shape (n_samples,)

Conformity scores.

ensemble: bool

Boolean determining whether the predictions are ensembled or not.

By default False.

method: str

Method to choose for prediction interval estimates. The "plus" method implies that the quantile is calculated after estimating the bounds, whereas the other methods (among the "naive", "base" or "minmax" methods, for example) do the opposite.

By default base.

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.

By default False.

Returns
Tuple[NDArray, NDArray, NDArray]
  • The predictions itself. (y_pred) of shape (n_samples,).

  • The lower bounds of the prediction intervals of shape

(n_samples, n_alpha). - The upper bounds of the prediction intervals of shape (n_samples, n_alpha).

Raises
ValueError

If beta optimisation with symmetrical conformity score function.

get_conformity_scores(y: ndarray[Any, dtype[_ScalarType_co]], y_pred: ndarray[Any, dtype[_ScalarType_co]], **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Get the conformity score considering the symmetrical property if so.

Parameters
y: NDArray of shape (n_samples,)

Observed target values.

y_pred: NDArray of shape (n_samples,)

Predicted target values.

Returns
NDArray of shape (n_samples,)

Conformity scores.

abstract get_estimation_distribution(y_pred: ndarray[Any, dtype[_ScalarType_co]], conformity_scores: ndarray[Any, dtype[_ScalarType_co]], **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Placeholder for get_estimation_distribution. Subclasses should implement this method!

Compute samples of the estimation distribution given the predicted targets and the conformity scores.

Parameters
y_pred: NDArray of shape (n_samples,)

Predicted target values.

conformity_scores: NDArray of shape (n_samples,)

Conformity scores.

Returns
NDArray of shape (n_samples,)

Observed values.

abstract get_signed_conformity_scores(y: ndarray[Any, dtype[_ScalarType_co]], y_pred: ndarray[Any, dtype[_ScalarType_co]], **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Placeholder for get_conformity_scores. Subclasses should implement this method!

Compute the sample conformity scores given the predicted and observed targets.

Parameters
y: NDArray of shape (n_samples,)

Observed target values.

y_pred: NDArray of shape (n_samples,)

Predicted target values.

Returns
NDArray of shape (n_samples,)

Signed conformity scores.

predict_set(X: ndarray[Any, dtype[_ScalarType_co]], alpha_np: ndarray[Any, dtype[_ScalarType_co]], **kwargs)[source]

Compute the prediction sets on new samples based on the uncertainty of the target confidence set.