Skip to content

Commit

Permalink
Create new conformity score
Browse files Browse the repository at this point in the history
  • Loading branch information
2018jabere committed Jul 28, 2023
1 parent 878e56e commit 6f3f458
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
6 changes: 4 additions & 2 deletions mapie/conformity_scores/conformity_scores.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABCMeta, abstractmethod

import numpy as np
from typing import Tuple
from typing import Tuple, Union

from mapie._compatibility import np_nanquantile
from mapie._typing import ArrayLike, NDArray
Expand Down Expand Up @@ -59,6 +59,7 @@ def get_signed_conformity_scores(
X: ArrayLike,
y: ArrayLike,
y_pred: ArrayLike,
y_std: Union[ArrayLike, None],
) -> NDArray:
"""
Placeholder for ``get_signed_conformity_scores``.
Expand Down Expand Up @@ -182,6 +183,7 @@ def get_conformity_scores(
X: ArrayLike,
y: ArrayLike,
y_pred: ArrayLike,
y_std: Union[ArrayLike, None]
) -> NDArray:
"""
Get the conformity score considering the symmetrical property if so.
Expand All @@ -202,7 +204,7 @@ def get_conformity_scores(
NDArray of shape (n_samples,)
Conformity scores.
"""
conformity_scores = self.get_signed_conformity_scores(X, y, y_pred)
conformity_scores = self.get_signed_conformity_scores(X, y, y_pred, y_std)
if self.consistency_check:
self.check_consistency(X, y, y_pred, conformity_scores)
if self.sym:
Expand Down
54 changes: 53 additions & 1 deletion mapie/conformity_scores/residual_conformity_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ def get_signed_conformity_scores(
self,
X: ArrayLike,
y: ArrayLike,
y_pred: ArrayLike
y_pred: ArrayLike,
y_std: Union[ArrayLike, None]
) -> NDArray:
"""
Computes the signed conformity score = (y - y_pred) / r_pred.
Expand Down Expand Up @@ -666,3 +667,54 @@ def get_estimation_distribution(
# import pdb; pdb.set_trace()
print(conformity_scores)
return np.add(y_pred, np.multiply(conformity_scores, r_pred.reshape(-1, 1)))



class GPCrossConformityScore(ConformityScore):
"""
Absolute conformity score.
The signed conformity score = y - y_pred.
The conformity score is symmetrical.
This is appropriate when the confidence interval is symmetrical and
its range is approximatively the same over the range of predicted values.
"""

def __init__(
self,
) -> None:
super().__init__(sym=True, consistency_check=True)

def get_signed_conformity_scores(
self,
X: ArrayLike,
y: ArrayLike,
y_pred: ArrayLike,
y_std: Union[ArrayLike, None]
) -> NDArray:
"""
Compute the signed conformity scores from the predicted values
and the observed ones, from the following formula:
signed conformity score = y - y_pred
"""
y_std = np.maximum(self.eps, y_std)
return np.subtract(y, y_pred) / y_std


def get_estimation_distribution(
self,
X: ArrayLike,
y_pred: ArrayLike,
conformity_scores: ArrayLike
) -> NDArray:
"""
Compute samples of the estimation distribution from the predicted
values and the conformity scores, from the following formula:
signed conformity score = y - y_pred
<=> y = y_pred + signed conformity score
``conformity_scores`` can be either the conformity scores or
the quantile of the conformity scores.
"""
return np.add(y_pred, conformity_scores)
3 changes: 2 additions & 1 deletion mapie/regression/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.model_selection import train_test_split
from mapie.regression import MapieRegressor
from mapie.conformity_scores.residual_conformity_scores import GPCrossConformityScore

def g(x):
return (3 * x * np.sin(x) - 2 * x * np.cos(x) + ( x ** 3) / 40 - .5 * x ** 2 - 10 * x)
Expand All @@ -11,5 +12,5 @@ def g(x):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2)
gp = GaussianProcessRegressor().fit(X_train, y_train)

mapie = MapieRegressor(estimator=gp, method="plus", cv=-1)
mapie = MapieRegressor(estimator=gp, method="plus", cv=-1, conformity_score=GPCrossConformityScore(consistency_check=False))
mapie.fit(X_train, y_train)

0 comments on commit 6f3f458

Please sign in to comment.