Metrics¶
Evaluation metrics for conformal prediction.
Regression¶
mapie.metrics.regression.regression_coverage_score
¶
Effective coverage obtained by the prediction intervals.
Intervals given by the predict_interval method can be passed directly
to the y_intervals argument (see example below).
Beside this intended use, this function also works with:
y_trueof shape (n_sample,) andy_intervalsof shape (n_sample, 2)y_trueof shape (n_sample, n) andy_intervalsof shape (n_sample, 2, n)
The effective coverage is obtained by computing the fraction of true labels that lie within the prediction intervals.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_intervals
|
Lower and upper bound of prediction intervals
with different confidence levels, given by the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
Effective coverage obtained by the prediction intervals for each confidence level. |
Examples:
>>> from mapie.metrics.regression import regression_coverage_score
>>> from mapie.regression import SplitConformalRegressor
>>> 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 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)
>>> coverage = regression_coverage_score(y_test, predicted_intervals)[0]
Source code in mapie/metrics/regression.py
mapie.metrics.regression.regression_mean_width_score
¶
Effective mean width score obtained by the prediction intervals.
| PARAMETER | DESCRIPTION |
|---|---|
y_intervals
|
Lower and upper bound of prediction intervals
with different confidence levels, given by the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
Effective mean width of the prediction intervals for each confidence level. |
Examples:
>>> import numpy as np
>>> from mapie.metrics.regression import regression_mean_width_score
>>> y_intervals = np.array([[[4, 6, 8], [6, 9, 11]],
... [[9, 10, 11], [10, 12, 14]],
... [[8.5, 9.5, 10], [12.5, 12, 13]],
... [[7, 8, 9], [8.5, 9.5, 10]],
... [[5, 6, 7], [6.5, 8, 9]]])
>>> print(regression_mean_width_score(y_intervals))
[2. 2.2 2.4]
Source code in mapie/metrics/regression.py
mapie.metrics.regression.regression_ssc
¶
Compute Size-Stratified Coverage metrics proposed in [3] that is the conditional coverage conditioned by the size of the intervals. The intervals are ranked by their size (ascending) and then divided into num_bins groups: one value of coverage by groups is computed.
Warning: This metric should be used only with non constant intervals (intervals of different sizes), with constant intervals the result may be misinterpreted.
[3] Angelopoulos, A. N., & Bates, S. (2021). A gentle introduction to conformal prediction and distribution-free uncertainty quantification. arXiv preprint arXiv:2107.07511.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_intervals
|
Prediction intervals given by booleans of labels.
TYPE:
|
num_bins
|
Number of groups. Should be less than the number of different interval widths.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level, num_bins)
|
|
Examples:
>>> from mapie.metrics.regression import regression_ssc
>>> import numpy as np
>>> y_true = np.array([5, 7.5, 9.5])
>>> y_intervals = np.array([
... [4, 6],
... [6.0, 9.0],
... [9, 10.0]
... ])
>>> print(regression_ssc(y_true, y_intervals, num_bins=2))
[[1. 1.]]
Source code in mapie/metrics/regression.py
mapie.metrics.regression.regression_ssc_score
¶
Aggregate by the minimum for each confidence level the Size-Stratified Coverage [3]: returns the maximum violation of the conditional coverage (with the groups defined).
Warning: This metric should be used only with non constant intervals (intervals of different sizes), with constant intervals the result may be misinterpreted.
[3] Angelopoulos, A. N., & Bates, S. (2021). A gentle introduction to conformal prediction and distribution-free uncertainty quantification. arXiv preprint arXiv:2107.07511.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_intervals
|
Prediction intervals given by booleans of labels.
TYPE:
|
num_bins
|
Number of groups. Should be less than the number of different interval widths.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
|
Examples:
>>> from mapie.metrics.regression import regression_ssc_score
>>> import numpy as np
>>> y_true = np.array([5, 7.5, 9.5])
>>> y_intervals = np.array([
... [[4, 4], [6, 7.5]],
... [[6.0, 8], [9.0, 10]],
... [[9, 9], [10.0, 10.0]]
... ])
>>> print(regression_ssc_score(y_true, y_intervals, num_bins=2))
[1. 0.5]
Source code in mapie/metrics/regression.py
mapie.metrics.regression.hsic
¶
Compute the square root of the hsic coefficient. HSIC is Hilbert-Schmidt independence criterion that is a correlation measure. Here we use it as proposed in [4], to compute the correlation between the indicator of coverage and the interval size.
If hsic is 0, the two variables (the indicator of coverage and the interval size) are independant.
Warning: This metric should be used only with non constant intervals (intervals of different sizes), with constant intervals the result may be misinterpreted.
[4] Feldman, S., Bates, S., & Romano, Y. (2021). Improving conditional coverage via orthogonal quantile regression. Advances in Neural Information Processing Systems, 34, 2060-2071.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_intervals
|
Prediction sets given by booleans of labels.
TYPE:
|
kernel_sizes
|
The variance (sigma) for each variable (the indicator of coverage and the interval size), this coefficient controls the width of the curve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
One hsic correlation coefficient by confidence level. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If kernel_sizes has a length different from 2 and if it has negative or null values. |
Examples:
>>> from mapie.metrics.regression import hsic
>>> import numpy as np
>>> y_true = np.array([9.5, 10.5, 12.5])
>>> y_intervals = np.array([
... [[9, 9], [10.0, 10.0]],
... [[8.5, 9], [12.5, 12]],
... [[10.5, 10.5], [12.0, 12]]
... ])
>>> print(hsic(y_true, y_intervals))
[0.31787614 0.2962914 ]
Source code in mapie/metrics/regression.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
mapie.metrics.regression.coverage_width_based
¶
coverage_width_based(
y_true: ArrayLike,
y_pred_low: ArrayLike,
y_pred_up: ArrayLike,
eta: float,
confidence_level: float,
) -> float
Coverage Width-based Criterion (CWC) obtained by the prediction intervals.
The effective coverage score is a criterion used to evaluate the quality of prediction intervals (PIs) based on their coverage and width.
Khosravi, Abbas, Saeid Nahavandi, and Doug Creighton. "Construction of optimal prediction intervals for load forecasting problems." IEEE Transactions on Power Systems 25.3 (2010): 1496-1503.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_pred_low
|
Lower bound of the prediction intervals.
TYPE:
|
y_pred_up
|
Upper bound of the prediction intervals.
TYPE:
|
eta
|
A user-defined parameter that balances the contributions of mean width score and coverage score in the CWC calculation.
TYPE:
|
confidence_level
|
A user-defined parameter representing the designed confidence level of the PI.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Effective coverage score (CWC) obtained by the prediction intervals. |
Notes
The effective coverage score (CWC) is calculated using the following formula: CWC = (1 - Mean Width Score) * exp(-eta * (Coverage score - (1-alpha))**2)
The CWC penalizes under- and overcoverage in the same way and summarizes the quality of the prediction intervals in a single value.
High Eta (Large Positive Value):
When eta is a high positive value, it will strongly emphasize the contribution of (1-Mean Width Score). This means that the algorithm will prioritize reducing the average width of the prediction intervals (Mean Width Score) over achieving a high coverage probability (Coverage score). The exponential term np.exp(-eta(Coverage score - (1-alpha))*2) will have a sharp decline as Coverage score deviates from (1-alpha). So, achieving a high Coverage score becomes less important compared to minimizing Mean Width Score. The impact will be narrower prediction intervals on average, which may result in more precise but less conservative predictions.
Low Eta (Small Positive Value):
When eta is a low positive value, it will still prioritize reducing the average width of the prediction intervals (Mean Width Score) but with less emphasis compared to higher eta values. The exponential term will be less steep, meaning that deviations of Coverage score from (1-alpha) will have a moderate impact. You'll get a balance between prediction precision and coverage, but the exact balance will depend on the specific value of eta.
Negative Eta (Any Negative Value):
When eta is negative, it will have a different effect on the formula. Negative values of eta will cause the exponential term np.exp(-eta(Coverage score - (1-alpha))*2) to become larger as Coverage score deviates from (1-alpha). This means that a negative eta prioritizes achieving a high coverage probability (Coverage score) over minimizing Mean Width Score. In this case, the algorithm will aim to produce wider prediction intervals to ensure a higher likelihood of capturing the true values within those intervals, even if it sacrifices precision. Negative eta values might be used in scenarios where avoiding errors or outliers is critical.
Null Eta (Eta = 0):
Specifically, when eta is zero, the CWC score becomes equal to (1 - Mean Width Score), which is equivalent to (1 - average width of the prediction intervals). Therefore, in this case, the CWC score is primarily based on the size of the prediction interval.
Examples:
>>> from mapie.metrics.regression import coverage_width_based
>>> import numpy as np
>>> y_true = np.array([5, 7.5, 9.5, 10.5, 12.5])
>>> y_preds_low = np.array([4, 6, 9, 8.5, 10.5])
>>> y_preds_up = np.array([6, 9, 10, 12.5, 12])
>>> eta = 0.01
>>> confidence_level = 0.9
>>> cwb = coverage_width_based(
... y_true, y_preds_low, y_preds_up, eta, confidence_level
... )
>>> print(np.round(cwb ,2))
0.69
Source code in mapie/metrics/regression.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
mapie.metrics.regression.regression_mwi_score
¶
The Winkler score, proposed by Winkler (1972), is a measure used to evaluate prediction intervals, combining the length of the interval with a penalty that increases proportionally to the distance of an observation outside the interval.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
Ground truth values
TYPE:
|
y_pis
|
Lower and upper bounds of prediction intervals output from a MAPIE regressor
TYPE:
|
confidence_level
|
The value of confidence_level
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The mean Winkler interval score |
References
[1] Robert L. Winkler "A Decision-Theoretic Approach to Interval Estimation", Journal of the American Statistical Association, volume 67, pages 187-191 (1972) (https://doi.org/10.1080/01621459.1972.10481224) [2] Tilmann Gneiting and Adrian E Raftery "Strictly Proper Scoring Rules, Prediction, and Estimation", Journal of the American Statistical Association, volume 102, pages 359-378 (2007) (https://doi.org/10.1198/016214506000001437) (Section 6.2)
Source code in mapie/metrics/regression.py
Classification¶
mapie.metrics.classification.classification_coverage_score
¶
Effective coverage score obtained by the prediction sets.
The effective coverage is obtained by estimating the fraction of true labels that lie within the prediction sets.
Prediction sets obtained by the predict method can be passed directly to the
y_pred_set argument (see example below).
Beside this intended use, this function also works with:
y_trueof shape (n_sample,) andy_pred_setof shape (n_sample, n_class)y_trueof shape (n_sample, n) andy_pred_setof shape (n_sample, n_class, n)
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_pred_set
|
Prediction sets with different confidence levels, given by booleans of labels
with the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
Effective coverage obtained by the prediction sets for each confidence level. |
Examples:
>>> from mapie.metrics.classification import classification_coverage_score
>>> from mapie.classification import SplitConformalClassifier
>>> from mapie.utils import train_conformalize_test_split
>>> from sklearn.datasets import make_classification
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.neighbors import KNeighborsClassifier
>>> X, y = make_classification(n_samples=500)
>>> (
... 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_classifier = SplitConformalClassifier(
... estimator=KNeighborsClassifier(),
... confidence_level=[0.9, 0.95, 0.99],
... prefit=False,
... ).fit(X_train, y_train).conformalize(X_conformalize, y_conformalize)
>>> predicted_points, predicted_sets = mapie_classifier.predict_set(X_test)
>>> coverage = classification_coverage_score(y_test, predicted_sets)[0]
Source code in mapie/metrics/classification.py
mapie.metrics.classification.classification_mean_width_score
¶
Mean width of prediction set output by
mapie.classification._MapieClassifier.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred_set
|
Prediction sets given by booleans of labels.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
Mean width of the prediction sets for each confidence level. |
Examples:
>>> import numpy as np
>>> from mapie.metrics.classification import classification_mean_width_score
>>> y_pred_set = np.array([
... [[False, False], [False, True], [True, True]],
... [[False, True], [True, False], [True, True]],
... [[True, False], [True, True], [True, False]],
... [[False, False], [True, True], [True, True]],
... [[True, True], [False, True], [True, False]]
... ])
>>> print(classification_mean_width_score(y_pred_set))
[2. 1.8]
Source code in mapie/metrics/classification.py
mapie.metrics.classification.classification_ssc
¶
classification_ssc(
y_true: NDArray,
y_pred_set: NDArray,
num_bins: Union[int, None] = None,
) -> NDArray
Compute Size-Stratified Coverage metrics proposed in [3] that is the conditional coverage conditioned by the size of the predictions sets. The sets are ranked by their size (ascending) and then divided into num_bins groups: one value of coverage by groups is computed.
[3] Angelopoulos, A. N., & Bates, S. (2021). A gentle introduction to conformal prediction and distribution-free uncertainty quantification. arXiv preprint arXiv:2107.07511.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_pred_set
|
Prediction sets given by booleans of labels.
TYPE:
|
num_bins
|
Number of groups. If None, one value of coverage by possible size of sets (n_classes +1) is computed. Should be less than the number of different set sizes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level, num_bins)
|
|
Examples:
>>> from mapie.metrics.classification import classification_ssc
>>> import numpy as np
>>> y_true = y_true_class = np.array([3, 3, 1, 2, 2])
>>> y_pred_set = np.array([
... [True, True, True, True],
... [False, True, False, True],
... [True, True, True, False],
... [False, False, True, True],
... [True, True, False, True]])
>>> print(classification_ssc(y_true, y_pred_set, num_bins=2))
[[1. 0.66666667]]
Source code in mapie/metrics/classification.py
mapie.metrics.classification.classification_ssc_score
¶
classification_ssc_score(
y_true: NDArray,
y_pred_set: NDArray,
num_bins: Union[int, None] = None,
) -> NDArray
Aggregate by the minimum for each confidence level the Size-Stratified Coverage [3]: returns the maximum violation of the conditional coverage (with the groups defined).
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
True labels.
TYPE:
|
y_pred_set
|
Prediction sets given by booleans of labels.
TYPE:
|
num_bins
|
Number of groups. If None, one value of coverage by possible size of sets (n_classes +1) is computed. Should be less than the number of different set sizes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_confidence_level,)
|
|
Examples:
>>> from mapie.metrics.classification import classification_ssc_score
>>> import numpy as np
>>> y_true = y_true_class = np.array([3, 3, 1, 2, 2])
>>> y_pred_set = np.array([
... [True, True, True, True],
... [False, True, False, True],
... [True, True, True, False],
... [False, False, True, True],
... [True, True, False, True]])
>>> print(classification_ssc_score(y_true, y_pred_set, num_bins=2))
[0.66666667]
Source code in mapie/metrics/classification.py
Calibration¶
mapie.metrics.calibration.expected_calibration_error
¶
expected_calibration_error(
y_true: ArrayLike,
y_scores: ArrayLike,
num_bins: int = 50,
split_strategy: Optional[str] = None,
) -> float
The expected calibration error, which is the difference between the confidence scores and accuracy per bin [1].
[1] Naeini, Mahdi Pakdaman, Gregory Cooper, and Milos Hauskrecht. "Obtaining well calibrated probabilities using bayesian binning." Twenty-Ninth AAAI Conference on Artificial Intelligence. 2015.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
The target values for the calibrator.
TYPE:
|
y_scores
|
The predictions scores.
TYPE:
|
num_bins
|
Number of bins to make the split in the y_score. The allowed values are num_bins above 0.
TYPE:
|
split_strategy
|
The way of splitting the predictions into different bins. The allowed split strategies are "uniform", "quantile" and "array split".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The score of ECE (Expected Calibration Error). |
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.top_label_ece
¶
top_label_ece(
y_true: ArrayLike,
y_scores: ArrayLike,
y_score_arg: Optional[ArrayLike] = None,
num_bins: int = 50,
split_strategy: Optional[str] = None,
classes: Optional[ArrayLike] = None,
) -> float
The Top-Label ECE which is a method adapted to fit the ECE to a Top-Label setting [2].
[2] Gupta, Chirag, and Aaditya K. Ramdas. "Top-label calibration and multiclass-to-binary reductions." arXiv preprint arXiv:2107.08353 (2021).
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
The target values for the calibrator.
TYPE:
|
y_scores
|
The predictions scores, either the maximum score and the argmax needs to be inputted or in the form of the prediction probabilities.
TYPE:
|
y_score_arg
|
If only the maximum is provided in the y_scores, the argmax must be provided here. This is optional and could be directly infered from the y_scores.
TYPE:
|
num_bins
|
Number of bins to make the split in the y_score. The allowed values are num_bins above 0.
TYPE:
|
split_strategy
|
The way of splitting the predictions into different bins. The allowed split strategies are "uniform", "quantile" and "array split".
TYPE:
|
classes
|
The different classes, in order of the indices that would be present in a pred_proba.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The ECE score adapted in the top label setting. |
Source code in mapie/metrics/calibration.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
mapie.metrics.calibration.kolmogorov_smirnov_statistic
¶
Compute Kolmogorov-smirnov's statistic for calibration test.
Also called ECCE-MAD
(Estimated Cumulative Calibration Errors - Maximum Absolute Deviation).
The closer to zero, the better the scores are calibrated.
Indeed, if the scores are perfectly calibrated,
the cumulative differences between y_true and y_score
should share the same properties of a standard Brownian motion
asymptotically.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
An array of ground truth.
TYPE:
|
y_score
|
An array of scores..
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Kolmogorov-smirnov's statistic. |
References
Arrieta-Ibarra I, Gujral P, Tannen J, Tygert M, Xu C. Metrics of calibration for probabilistic predictions. The Journal of Machine Learning Research. 2022 Jan 1;23(1):15886-940.
Examples:
>>> import numpy as np
>>> from mapie.metrics.calibration import kolmogorov_smirnov_statistic
>>> y_true = np.array([0, 1, 0, 1, 0])
>>> y_score = np.array([0.1, 0.9, 0.21, 0.9, 0.5])
>>> print(np.round(kolmogorov_smirnov_statistic(y_true, y_score), 3))
0.978
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.kolmogorov_smirnov_cdf
¶
Compute the Kolmogorov-smirnov cumulative distribution function (CDF) for the float x. This is interpreted as the CDF of the maximum absolute value of the standard Brownian motion over the unit interval [0, 1]. The function is approximated by its power series, truncated so as to hit machine precision error.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The float x to compute the cumulative distribution function on.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The Kolmogorov-smirnov cumulative distribution function. |
References
Tygert M. Calibration of P-values for calibration and for deviation of a subpopulation from the full population. arXiv preprint arXiv:2202.00100. 2022 Jan 31.
D. A. Darling. A. J. F. Siegert. The First Passage Problem for a Continuous Markov Process. Ann. Math. Statist. 24 (4) 624 - 639, December, 1953.
Examples:
>>> import numpy as np
>>> from mapie.metrics.calibration import kolmogorov_smirnov_cdf
>>> print(np.round(kolmogorov_smirnov_cdf(1), 4))
0.3708
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.kolmogorov_smirnov_p_value
¶
Compute Kolmogorov Smirnov p-value. Deduced from the corresponding statistic and CDF. It represents the probability of the observed statistic under the null hypothesis of perfect calibration.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
An array of ground truth.
TYPE:
|
y_score
|
An array of scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The Kolmogorov Smirnov p-value. |
References
Tygert M. Calibration of P-values for calibration and for deviation of a subpopulation from the full population. arXiv preprint arXiv:2202.00100. 2022 Jan 31.
D. A. Darling. A. J. F. Siegert. The First Passage Problem for a Continuous Markov Process. Ann. Math. Statist. 24 (4) 624 - 639, December, 1953.
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from mapie.metrics.calibration import kolmogorov_smirnov_p_value
>>> y_true = np.array([1, 0, 1, 0, 1, 0])
>>> y_score = np.array([0.8, 0.3, 0.5, 0.5, 0.7, 0.1])
>>> ks_p_value = kolmogorov_smirnov_p_value(y_true, y_score)
>>> print(np.round(ks_p_value, 4))
0.7857
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.kuiper_statistic
¶
Compute Kuiper's statistic for calibration test.
Also called ECCE-R (Estimated Cumulative Calibration Errors - Range).
The closer to zero, the better the scores are calibrated.
Indeed, if the scores are perfectly calibrated,
the cumulative differences between y_true and y_score
should share the same properties of a standard Brownian motion
asymptotically.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
An array of ground truth.
TYPE:
|
y_score
|
An array of scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Kuiper's statistic. |
References
Arrieta-Ibarra I, Gujral P, Tannen J, Tygert M, Xu C. Metrics of calibration for probabilistic predictions. The Journal of Machine Learning Research. 2022 Jan 1;23(1):15886-940.
Examples:
>>> import numpy as np
>>> from mapie.metrics.calibration import kuiper_statistic
>>> y_true = np.array([0, 1, 0, 1, 0])
>>> y_score = np.array([0.1, 0.9, 0.21, 0.9, 0.5])
>>> print(np.round(kuiper_statistic(y_true, y_score), 3))
0.857
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.kuiper_cdf
¶
Compute the Kuiper cumulative distribution function (CDF) for the float x. This is interpreted as the CDF of the range of the standard Brownian motion over the unit interval [0, 1]. The function is approximated by its power series, truncated so as to hit machine precision error.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The float x to compute the cumulative distribution function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The Kuiper cumulative distribution function. |
References
Tygert M. Calibration of P-values for calibration and for deviation of a subpopulation from the full population. arXiv preprint arXiv:2202.00100. 2022 Jan 31.
William Feller. The Asymptotic Distribution of the Range of Sums of Independent Random Variables. Ann. Math. Statist. 22 (3) 427 - 432 September, 1951.
Examples:
>>> import numpy as np
>>> from mapie.metrics.calibration import kuiper_cdf
>>> print(np.round(kuiper_cdf(1), 4))
0.0634
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.kuiper_p_value
¶
Compute Kuiper statistic p-value. Deduced from the corresponding statistic and CDF. It represents the probability of the observed statistic under the null hypothesis of perfect calibration.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
An array of ground truth.
TYPE:
|
y_score
|
An array of scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The Kuiper p-value. |
References
Tygert M. Calibration of P-values for calibration and for deviation of a subpopulation from the full population. arXiv preprint arXiv:2202.00100. 2022 Jan 31.
William Feller. The Asymptotic Distribution of the Range of Sums of Independent Random Variables. Ann. Math. Statist. 22 (3) 427 - 432 September, 1951.
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from mapie.metrics.calibration import kuiper_p_value
>>> y_true = np.array([1, 0, 1, 0, 1, 0])
>>> y_score = np.array([0.8, 0.3, 0.5, 0.5, 0.7, 0.1])
>>> ku_p_value = kuiper_p_value(y_true, y_score)
>>> print(np.round(ku_p_value, 4))
0.9684
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.spiegelhalter_statistic
¶
Compute Spiegelhalter's statistic for calibration test. The closer to zero, the better the scores are calibrated. Indeed, if the scores are perfectly calibrated, the Brier score simplifies to an expression whose expectancy and variance are easy to compute. The statistic is no more that a z-score on this normalized expression.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
An array of ground truth.
TYPE:
|
y_score
|
An array of scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Spiegelhalter's statistic. |
References
Spiegelhalter DJ. Probabilistic prediction in patient management and clinical trials. Statistics in medicine. 1986 Sep;5(5):421-33.
Examples:
>>> import numpy as np
>>> from mapie.metrics.calibration import spiegelhalter_statistic
>>> y_true = np.array([0, 1, 0, 1, 0])
>>> y_score = np.array([0.1, 0.9, 0.21, 0.9, 0.5])
>>> print(np.round(spiegelhalter_statistic(y_true, y_score), 3))
-0.757
Source code in mapie/metrics/calibration.py
mapie.metrics.calibration.spiegelhalter_p_value
¶
Compute Spiegelhalter statistic p-value. Deduced from the corresponding statistic and CDF, which is no more than the normal distribution. It represents the probability of the observed statistic under the null hypothesis of perfect calibration.
| PARAMETER | DESCRIPTION |
|---|---|
y_true
|
An array of ground truth.
TYPE:
|
y_score
|
An array of scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The Spiegelhalter statistic p_value. |
References
Spiegelhalter DJ. Probabilistic prediction in patient management and clinical trials. Statistics in medicine. 1986 Sep;5(5):421-33.
Examples:
>>> import numpy as np
>>> from mapie.metrics.calibration import spiegelhalter_p_value
>>> y_true = np.array([1, 0, 1, 0, 1, 0])
>>> y_score = np.array([0.8, 0.3, 0.5, 0.5, 0.7, 0.1])
>>> sp_p_value = spiegelhalter_p_value(y_true, y_score)
>>> print(np.round(sp_p_value, 4))
0.8486