|
| 1 | +from abc import ABCMeta, abstractmethod |
| 2 | + |
| 3 | +from sklearn.utils import deprecated |
| 4 | + |
| 5 | +from mapie.conformity_scores.regression import BaseConformityScore |
| 6 | +from mapie._machine_precision import EPSILON |
| 7 | +from mapie._typing import NDArray |
| 8 | + |
| 9 | + |
| 10 | +@deprecated( |
| 11 | + "WARNING: Deprecated path to import ConformityScore. " |
| 12 | + "Please prefer the new path: " |
| 13 | + "[from mapie.conformity_scores import BaseRegressionScore]." |
| 14 | +) |
| 15 | +class ConformityScore(BaseConformityScore, metaclass=ABCMeta): |
| 16 | + """ |
| 17 | + Base conformity score class for regression task. |
| 18 | +
|
| 19 | + This class should not be used directly. Use derived classes instead. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + sym: bool |
| 24 | + Whether to consider the conformity score as symmetrical or not. |
| 25 | +
|
| 26 | + consistency_check: bool, optional |
| 27 | + Whether to check the consistency between the methods |
| 28 | + ``get_estimation_distribution`` and ``get_conformity_scores``. |
| 29 | + If ``True``, the following equality must be verified: |
| 30 | + ``self.get_estimation_distribution( |
| 31 | + y_pred, self.get_conformity_scores(y, y_pred, **kwargs), **kwargs |
| 32 | + ) == y`` |
| 33 | +
|
| 34 | + By default ``True``. |
| 35 | +
|
| 36 | + eps: float, optional |
| 37 | + Threshold to consider when checking the consistency between |
| 38 | + ``get_estimation_distribution`` and ``get_conformity_scores``. |
| 39 | + It should be specified if ``consistency_check==True``. |
| 40 | +
|
| 41 | + By default, it is defined by the default precision. |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + sym: bool, |
| 47 | + consistency_check: bool = True, |
| 48 | + eps: float = float(EPSILON), |
| 49 | + ): |
| 50 | + super().__init__() |
| 51 | + self.sym = sym |
| 52 | + self.consistency_check = consistency_check |
| 53 | + self.eps = eps |
| 54 | + |
| 55 | + @abstractmethod |
| 56 | + def get_signed_conformity_scores( |
| 57 | + self, |
| 58 | + y: NDArray, |
| 59 | + y_pred: NDArray, |
| 60 | + **kwargs |
| 61 | + ) -> NDArray: |
| 62 | + """ |
| 63 | + Placeholder for ``get_conformity_scores``. |
| 64 | + Subclasses should implement this method! |
| 65 | +
|
| 66 | + Compute the sample conformity scores given the predicted and |
| 67 | + observed targets. |
| 68 | +
|
| 69 | + Parameters |
| 70 | + ---------- |
| 71 | + y: NDArray of shape (n_samples,) |
| 72 | + Observed target values. |
| 73 | +
|
| 74 | + y_pred: NDArray of shape (n_samples,) |
| 75 | + Predicted target values. |
| 76 | +
|
| 77 | + Returns |
| 78 | + ------- |
| 79 | + NDArray of shape (n_samples,) |
| 80 | + Signed conformity scores. |
| 81 | + """ |
| 82 | + |
| 83 | + @abstractmethod |
| 84 | + def get_conformity_scores( |
| 85 | + self, |
| 86 | + y: NDArray, |
| 87 | + y_pred: NDArray, |
| 88 | + **kwargs |
| 89 | + ) -> NDArray: |
| 90 | + """ |
| 91 | + Placeholder for ``get_conformity_scores``. |
| 92 | + Subclasses should implement this method! |
| 93 | +
|
| 94 | + Compute the sample conformity scores given the predicted and |
| 95 | + observed targets. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + y: NDArray of shape (n_samples,) |
| 100 | + Observed target values. |
| 101 | +
|
| 102 | + y_pred: NDArray of shape (n_samples,) |
| 103 | + Predicted target values. |
| 104 | +
|
| 105 | + Returns |
| 106 | + ------- |
| 107 | + NDArray of shape (n_samples,) |
| 108 | + Conformity scores. |
| 109 | + """ |
| 110 | + |
| 111 | + @abstractmethod |
| 112 | + def get_estimation_distribution( |
| 113 | + self, |
| 114 | + y_pred: NDArray, |
| 115 | + conformity_scores: NDArray, |
| 116 | + **kwargs |
| 117 | + ) -> NDArray: |
| 118 | + """ |
| 119 | + Placeholder for ``get_estimation_distribution``. |
| 120 | + Subclasses should implement this method! |
| 121 | +
|
| 122 | + Compute samples of the estimation distribution given the predicted |
| 123 | + targets and the conformity scores. |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + y_pred: NDArray of shape (n_samples,) |
| 128 | + Predicted target values. |
| 129 | +
|
| 130 | + conformity_scores: NDArray of shape (n_samples,) |
| 131 | + Conformity scores. |
| 132 | +
|
| 133 | + Returns |
| 134 | + ------- |
| 135 | + NDArray of shape (n_samples,) |
| 136 | + Observed values. |
| 137 | + """ |
| 138 | + |
| 139 | + @abstractmethod |
| 140 | + def predict_set( |
| 141 | + self, |
| 142 | + X: NDArray, |
| 143 | + alpha_np: NDArray, |
| 144 | + **kwargs |
| 145 | + ): |
| 146 | + """ |
| 147 | + Compute the prediction sets on new samples based on the uncertainty of |
| 148 | + the target confidence set. |
| 149 | +
|
| 150 | + Parameters: |
| 151 | + ----------- |
| 152 | + X: NDArray of shape (n_samples,) |
| 153 | + The input data or samples for prediction. |
| 154 | +
|
| 155 | + alpha_np: NDArray of shape (n_alpha, ) |
| 156 | + Represents the uncertainty of the confidence set to produce. |
| 157 | +
|
| 158 | + **kwargs: dict |
| 159 | + Additional keyword arguments. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + -------- |
| 163 | + The output structure depend on the subclass. |
| 164 | + The prediction sets for each sample and each alpha level. |
| 165 | + """ |
0 commit comments