-
Notifications
You must be signed in to change notification settings - Fork 127
Conformal prediction with conditional guarantees #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Damien-Bouet
wants to merge
181
commits into
master
Choose a base branch
from
449-cp-with-conditional-guarantees
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
181 commits
Select commit
Hold shift + click to select a range
4a367bd
ADD: first implementation of the CCP method
fffd511
UPD: increase test_results_with_constant_sample_weights assert_allclo…
Damien-Bouet 56c67cf
MOVE PhiFunction into utils folder
Damien-Bouet 765c70a
ADD Polynomial and Gaussian PhiFunctions
Damien-Bouet c8c004d
UPD docstrings and return self on fit and calibrate
Damien-Bouet 1767c1e
FIX: tests
Damien-Bouet 8a46555
ADD: Paper simulations reproduction
Damien-Bouet d75a48b
FIX: tests and coverage
Damien-Bouet e29d0d7
FIX: paper simulation
Damien-Bouet fb4dbcb
Remove Literal for python 3.7 compatibility
Damien-Bouet 16c7069
UPD: sample_weight_test tol values
Damien-Bouet b32626e
RMV: seaborn from paper simulation imports
Damien-Bouet 4f4447e
FIX: linting
Damien-Bouet 015bdb7
FIX: useless seaborn grid style
Damien-Bouet 22a901d
FIX: Gaussian exp formula
Damien-Bouet db23dc0
MOVE: check_parameters in outside of init
Damien-Bouet 7f2cf56
FIX: Gaussian exp formula test
Damien-Bouet 24d8e19
MOVE: PhiFunctions import from regression to regression.utils
Damien-Bouet 023207f
UPD: Add fit/calib _attributes and Base classes inheritence
Damien-Bouet 79efe4e
UPD: Improve parameters checks
Damien-Bouet c278273
MOVE: check_estimator into utils
Damien-Bouet 595b037
UPD: CCP docstrings
Damien-Bouet b6735c2
UPD: Return predictions only if alpha is None
Damien-Bouet e48a8e6
UPD: Improve and functions
Damien-Bouet 5158462
UPD: Convert PhiFunction into a Abstract class
Damien-Bouet 2fcc380
ADD: CustomPhiFunction
Damien-Bouet 88e9273
UPD: Tests
Damien-Bouet a376dd5
reduce Gibbs paper simulation runtime
Damien-Bouet c2481f3
RENAME: move CCP on the template fit/predict (with fit_estimator, fit…
Damien-Bouet 3885491
FIX: forgot to stage a line from 'UPD: Convert PhiFunction into a Abs…
Damien-Bouet f728d77
FIX: array error for np.std
Damien-Bouet aeb7979
FIX: some forgotten fit_calibrator renaming
Damien-Bouet f848813
ADD: _is_fitted function (almost the copy of the private sklearn.util…
Damien-Bouet dc19045
typing
Damien-Bouet 4fdd852
typing again...
Damien-Bouet b0e22ec
UPD: CustomPhiFunction can now take PhiFunction instances in function…
Damien-Bouet 293f085
Merge branch 'master' into 449-cp-with-conditional-guarantees
809a39f
RENAME: check_estimator_regression
Damien-Bouet 4cf1a9f
RMV: Exemple from MapieCCPRegressor
Damien-Bouet c588ec8
UPD: make fit method mandatory and use check_is_fitted from sklearn
Damien-Bouet 8048220
MOVE: Externalise some utils functions
Damien-Bouet 1adf14d
MOVE: PhiFunctions into phi_function folder
Damien-Bouet ec4ce10
FIX: Coverage
Damien-Bouet b8be35e
ADD: PhiFunction multiplication
Damien-Bouet 99ea3fe
FIX: coverage
Damien-Bouet db360f9
MOVE and RENAME: PhiFunctions in calibrators/ccp
Damien-Bouet 0a7ec52
ADD: Abstract 'Calibrator' class
Damien-Bouet e9e0f43
UPD: externalise MapieCCPRegressor into abstract SplitMapie and move …
Damien-Bouet f7e8296
RENAME tests
Damien-Bouet e45bf48
ADD: Draft of Classification, to assess the generaliation capacities …
Damien-Bouet f08f8ae
UPD: improve abstract calibrator class signature
Damien-Bouet d437750
FIX: Coverage
Damien-Bouet 33573a3
UPD: docstring
Damien-Bouet 9168201
UPD: docstrings and rename
Damien-Bouet 328130c
ADD: demo notebook CCP Regression
Damien-Bouet 61e52b1
MERGE master
Damien-Bouet fbd8bd0
UPD: linting, tests and coverage
Damien-Bouet cfe299f
UPD: move reg_param into init
Damien-Bouet 6db9867
ADD: author
Damien-Bouet 68f704d
FIX: has no attribute
Damien-Bouet e8914a0
REMOVE: CCP Docstring example
Damien-Bouet afacd1a
REMOVE: example results
Damien-Bouet 2538ca0
FIX: ccp_regression_demo
Damien-Bouet 73c3a29
ADD: tutorial_ccp_CandC.ipynb
Damien-Bouet cb80f3b
ADD: ccp_tutorial notebook in readthedocs
Damien-Bouet f1e3be4
UPD: ccp tuto
Damien-Bouet 950f02d
FIX: remove seaborn import
Damien-Bouet a7a3297
FIX: isort imports
Damien-Bouet cc41209
UPD: ccp tutorial
Damien-Bouet 3cfdba1
UPD: remove multipliers from CCPCalibrator init and remove assert
Damien-Bouet ef8e378
DEL: ccp notebook moved in the doc, not usefull anymore
Damien-Bouet 2ddc543
FIX: typo in docstrings
Damien-Bouet 2e5918f
MOVE: check_calibrator in calibrators.utils
Damien-Bouet 64be82e
UPD: docstrings and minor fix
Damien-Bouet cad8e28
ADD: notebook tutorial_ccp_CandC in regression notebooks doc
Damien-Bouet dca25d3
ADD: test to check equivalence of new and old implementation of stand…
Damien-Bouet b5ed289
UPD: typos
Damien-Bouet 9bb8863
ADD: perfect width in ccp tutorial plots
Damien-Bouet 9115a87
UPD: Change regularization from L2 to L1
Damien-Bouet 1eed6de
FIX: ccp tuto plot
Damien-Bouet 8247e30
FIX: ccp tuto
Damien-Bouet e04ab4a
UPD: only sample gaussian points where multipliers values are not zer…
Damien-Bouet 909ec93
UPD: gaussian default value set to 20
Damien-Bouet 60bda1a
FIX: calib_kwargs bug fix
Damien-Bouet d14fa1b
MOVE: ccp null feature warning call
Damien-Bouet 89e31b9
UPD: calib kwargs docstring
Damien-Bouet 43dd443
UPD: multiply default sigma value by dnum of dimensions
Damien-Bouet 0ab0d77
UPD: docstrings and some renaming
Damien-Bouet f3d272a
FIX: calib_kwargs bug and linting
Damien-Bouet 907aec6
RMV warning in optional arg in custim ccp
Damien-Bouet 3de4ef3
FIX: multiplier impact on normalize and sigma
Damien-Bouet e704700
UPD: ccp tutorial
Damien-Bouet 2900605
ADD: ccp in api doc
Damien-Bouet a54aad8
UPD: add ccp tutorial conclusion and remove typo
Damien-Bouet 8f12ac4
FIX typo in ccp_tuto
Damien-Bouet 44c78fa
ADD: CCP theoretical description doc
Damien-Bouet fea21c4
FIX: ccp theory doc
Damien-Bouet 28ff85b
RENAME compile_functions_warnings_errors utils function and update er…
Damien-Bouet 4c560fd
RENAME ccp calibrator test functions
Damien-Bouet b31609c
Try to fix the doc
Damien-Bouet 2babfa2
UPD: ccp_CandC notebook
Damien-Bouet 71eaa1c
MOVE CCP doc into a new section
Damien-Bouet c67bb57
ADD tuto papier reference link
Damien-Bouet 391f529
ADD: calibrator doc
Damien-Bouet ddc9b52
UPD: minor corrections in the doc
Damien-Bouet f3e0d32
MOVE: BaseCalibrator import
Damien-Bouet 00bfd27
UPD docstrings and add ccp reference
Damien-Bouet c1a00dc
RMV not reproductible warning
Damien-Bouet fefd068
Merge branch 'master' into 449-cp-with-conditional-guarantees
Damien-Bouet 20037f7
REFACTO: Adapte the PR to the new Classifier refacto
Damien-Bouet 8ac6ae9
FIX: tests
Damien-Bouet add96e6
UNDO changes in sets.utils
Damien-Bouet 4cb0aba
Linting
Damien-Bouet 1afcf27
UPD: change naive by standard in doc
Damien-Bouet 09881aa
ADD: classification main class and tuto
Damien-Bouet b31f9da
ADD: SplitCPClassifier tests
Damien-Bouet 6d94658
FIX: coverage
Damien-Bouet 24ab0fc
FIX: coverage
Damien-Bouet 360cb39
FIX test
Damien-Bouet 269546d
FIX docstring example
Damien-Bouet 9c0b09f
UPD: change optimizer to SLSQP
Damien-Bouet 20e94b2
UPD: change optimize to SLSQP
Damien-Bouet 66fe957
UPD: update CandC notebook after changing optimizer
Damien-Bouet abe9bf9
FIX: tests
Damien-Bouet 3c0f1c7
FIX tests
Damien-Bouet e82b1c7
UPD: fix coverage
Damien-Bouet 42e1c60
Merge branch '449-cp-with-conditional-guarantees' into 499-adaptation…
Damien-Bouet 40bf93a
FIX: typo
Damien-Bouet d53be61
FIX typo
Damien-Bouet ced2086
UPD: theoretical description
Damien-Bouet 0562e79
ADD: reference in README
Damien-Bouet a6cf257
UPD: HISTORY with new CCP content
Damien-Bouet 8ca56d9
UPD: theoretical description
Damien-Bouet 2fc54fd
ADD: reference in README
Damien-Bouet 26f07da
UPD: HISTORY with new CCP content
Damien-Bouet c33ed18
UPD: theoretical doc update and typo
Damien-Bouet db375a3
UPD: remove sample_weights and corrected alpha in the calibration ste…
Damien-Bouet 53837bd
UPD: Typos in the doc
Damien-Bouet 57e15f8
linting
Damien-Bouet 9fa15fe
UPD: test values
Damien-Bouet 926d3b6
Merge branch '449-cp-with-conditional-guarantees' into 506-ccp-reopti…
Damien-Bouet 0ecabba
UPD: re optimize at inference time and add unsafe mode for old method
Damien-Bouet ab14963
Merge branch 'master' into 449-cp-with-conditional-guarantees
Damien-Bouet 744d56f
typo
Damien-Bouet b4f06e8
Merge branch '449-cp-with-conditional-guarantees' into 499-adaptation…
Damien-Bouet 171cd6d
Merge branch '449-cp-with-conditional-guarantees' into 506-ccp-reopti…
Damien-Bouet 1de9cf4
tests
Damien-Bouet 1feb27b
RMV: example from SplitCPClassifier doc
Damien-Bouet 2b43867
FIX: tests
Damien-Bouet b054bfa
REMOVE warning about stochastic behavior
Damien-Bouet 096b4b4
UPD: add :class: tag in docstrings
Damien-Bouet 1ded613
UPD: optimize starting from n points value
Damien-Bouet 600047b
UPD: remove commented section
Damien-Bouet 14e05b9
UPD: add :class: tag in docstrings
Damien-Bouet 10a419e
UPD: doc
Damien-Bouet 0e5930b
Merge branch '449-cp-with-conditional-guarantees' into 499-adaptation…
Damien-Bouet 90f865f
UPD: shorten giibs result reproduction duration
Damien-Bouet 038f863
UPD: activate unsafe_approximation in the doc example
Damien-Bouet f9af89a
Apply suggestions from code review
thibaultcordier 49080cc
Merge pull request #500 from scikit-learn-contrib/499-adaptation-ccp-…
thibaultcordier 2b7207d
Merge branch 'master' into 449-cp-with-conditional-guarantees
thibaultcordier 2788dbd
Merge branch '449-cp-with-conditional-guarantees' into 506-ccp-reopti…
6a35307
Add approximation boolean for classification
48b8730
UPD: typo
thibaultcordier a3664f5
Merge pull request #507 from scikit-learn-contrib/506-ccp-reoptimize-…
thibaultcordier 6421813
Move HISTORY.rst new features block
6c4a3b2
Move calibrators into future + Add details in theorical explanations
0a8bc05
Change path access of the classes
6827b6d
FIX: change import path
ccc4057
FIX: change import path
4976082
Merge branch 'master' into 449-cp-with-conditional-guarantees
jawadhussein462 237aa09
Merge branch 'master' into 449-cp-with-conditional-guarantees
vincentblot28 ff843b0
ENH: add new point in calibraiton optim to speed up test
vincentblot28 51b6cd3
ENH: remove choice for cs bound
vincentblot28 bda687a
FIX: linting
vincentblot28 06bc0e1
FIX: LINTING
vincentblot28 2d0a3ef
ENH: remove cs_bound argument in predict
vincentblot28 3f8804b
FIX: typing
vincentblot28 a4814e5
FIX: remove old cs_bound tests
vincentblot28 c04fdeb
IFX: linting
vincentblot28 fb41f0a
ENH: remove new point un calib as no effect on time
vincentblot28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ Contributors | |
| * Ambros Marzetta <ambrosm> | ||
| * Carl McBride Ellis <Carl-McBride-Ellis> | ||
| * Baptiste Calot <[email protected]> | ||
| * Damien Bouet <[email protected]> | ||
| * Leonardo Garma <[email protected]> | ||
| * Mohammed Jawhar <[email protected]> | ||
| * Syed Affan <[email protected]> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| .. title:: Calibrators : contents | ||
|
|
||
| .. _theoretical_description_calibrators: | ||
|
|
||
| ############### | ||
| Calibrators | ||
| ############### | ||
|
|
||
| In Mapie, the conformalisation step is done directly inside | ||
| :class:`~mapie.regression.MapieRegressor` or :class:`~mapie.classification.MapieClassifier`, | ||
| depending on the ``method`` argument. | ||
| However, when implementing the new CCP method, we decided to externalize the conformalisation | ||
| step into a new object named ``calibrator``, to have more freedom and possible customisation. | ||
|
|
||
| The new classes (:class:`~mapie.future.split.SplitCPRegressor` and :class:`~mapie.future.split.SplitCPClassifier`) have 3 steps: | ||
|
|
||
| 1. ``fit_predictor``, which fit the sklearn estimator | ||
| 2. ``fit_calibrator``, which do the conformalisation (calling ``calibrator.fit``) | ||
| 3. ``predict``, which compute the predictions and call ``calibrator.predict`` to create the prediction intervals | ||
|
|
||
| Thus, the calibrators, based on :class:`~mapie.future.calibrators.base.BaseCalibrator`, | ||
| must have the two methods: ``fit`` and ``predict``. | ||
|
|
||
| Mapie currently implements calibrators for the CCP method (and the standard method), | ||
| but any conformal prediction method can be implemented by the user as | ||
| a subclass of :class:`~mapie.future.calibrators.base.BaseCalibrator`. | ||
|
|
||
| Example of standard split CP: | ||
| ------------------------------ | ||
|
|
||
| For instance, the :class:`~mapie.future.calibrators.StandardCalibrator` implements | ||
| the :ref:`standard split method<theoretical_description_regression_standard>`: | ||
|
|
||
| * ``.fit`` computes :math:`\hat{q}_{n, \alpha}^+`, the :math:`(1-\alpha)` quantile of the distribution | ||
| * ``.predict`` comptues the prediction intervals with: :math:`\hat{\mu}(X_{n+1}) \pm \hat{q}_{n, \alpha}^+` | ||
|
|
||
|
|
||
| The CCP calibrators: | ||
| --------------------- | ||
| For the CCP method (see :ref:`theoretical description<theoretical_description_ccp>`), | ||
| :class:`~mapie.future.calibrators.ccp.CCPCalibrator` implements: | ||
|
|
||
| * ``.fit`` solve the optimization problem (see :ref:`step 2<theoretical_description_ccp_control_steps>`) to find the optimal :math:`\hat{g}` | ||
| * ``.predict`` comptues the prediction intervals using :math:`\hat{g}` (see :ref:`step 3<theoretical_description_ccp_control_steps>`) | ||
|
|
||
| We just need a way to define our :math:`\Phi` function (see :ref:`step 1<theoretical_description_ccp_control_steps>`). | ||
|
|
||
| Multiple subclasses are implemented to facilitate the definition of the :math:`\Phi` function, | ||
| but other could be implemented by the user as a subclass of :class:`~mapie.future.calibrators.ccp.CCPCalibrator`. | ||
|
|
||
| 1. :class:`~mapie.future.calibrators.ccp.CustomCCP` | ||
|
|
||
| This class allows to define by hand the :math:`\Phi` function, as a | ||
| concatenation of other functions which create features of ``X`` (or potentially ``y_pred`` or any exogenous variable ``z``) | ||
|
|
||
| It can also be used to concatenate other :class:`~mapie.future.calibrators.ccp.CCPCalibrator` instances. | ||
|
|
||
| 2. :class:`~mapie.future.calibrators.ccp.PolynomialCCP` | ||
|
|
||
| It create some polynomial features of ``X`` (or potentially ``y_pred`` or any exogenous variable ``z``). | ||
| It could be created by hand using `CustomCCP`, it is just a way simplify the creation of :math:`\Phi`. | ||
|
|
||
| 3. :class:`~mapie.future.calibrators.ccp.GaussianCCP` | ||
|
|
||
| It create gaussian kernels, as done in the method's paper :ref:`[1]<theoretical_description_calibrators_references>`. | ||
| It samples random points from the :math:`\{ X_i \}_i`, then compute gaussian distances | ||
| between each point and :math:`X_{n+1}` with a given standard deviation :math:`\sigma` | ||
| (which can be optimized using cross-validation), following the formula: | ||
|
|
||
| .. math:: | ||
| \forall j \in \{ \text{sampled index} \}, \quad \Phi(X)_j = exp \left( -\frac{(X_{n+1} - X_j)^2}{2\sigma ^2} \right) | ||
|
|
||
|
|
||
| .. _theoretical_description_calibrators_references: | ||
|
|
||
| References | ||
| ========== | ||
|
|
||
| [1] Isaac Gibbs, John J. Cherian, and Emmanuel J. Candès, | ||
| "Conformal Prediction With Conditional Guarantees", `arXiv <https://arxiv.org/abs/2305.12616>`_, 2023. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| .. title:: Theoretical Description : contents | ||
|
|
||
| .. _theoretical_description_ccp: | ||
|
|
||
| ######################## | ||
| Theoretical Description | ||
| ######################## | ||
|
|
||
| The Conditional Conformal Prediction (CCP) method :ref:`[1]<theoretical_description_ccp_references>` is a model agnostic conformal prediction method which | ||
| can create adaptative prediction intervals. | ||
|
|
||
| In MAPIE, this method has a lot of advantages: | ||
|
|
||
| - It is model agnostic (it doesn’t depend on the model but only on the predictions, unlike CQR) | ||
| - It can create very adaptative intervals (with a varying width which truly reflects the model uncertainty) | ||
| - while providing coverage guarantee on all sub-groups of interest (avoiding biases) | ||
| - with the possibility to inject prior knowledge about the data or the model | ||
|
|
||
| However, we will also see its disadvantages: | ||
| - The adaptativity depends on the calibrator we use: It can be difficult to choose the correct calibrator, | ||
| with the best parameters. | ||
| - The calibration and even more the inference are much longer than for the other methods. | ||
| We can reduce the inference time using ``unsafe_approximation=True``, | ||
| but we lose the strong theoretical guarantees and risk a small miscoverage | ||
| (even if, most of the time, the coverage is achieved). | ||
|
|
||
| To conclude, it can create more adaptative intervals than the other methods, | ||
| but it can be difficult to find the best settings (calibrator type and parameters) | ||
| and can have a big computational time. | ||
|
|
||
| How does it works? | ||
| ==================== | ||
|
|
||
| Method's intuition | ||
| -------------------- | ||
|
|
||
| We recall that the `standard split method` estimates the absolute residuals by a constant :math:`\hat{q}_{n, \alpha}^+` | ||
| (which is the quantile of :math:`{|Y_i-\hat{\mu}(X_i)|}_{1 \leq i \leq n}`). Then, the prediction interval is: | ||
|
|
||
| .. math:: \hat{C}_{n, \alpha}^{\textrm split}(X_{n+1}) = \hat{\mu}(X_{n+1}) \pm \hat{q}_{n, \alpha}^+ | ||
|
|
||
| The idea of the `CCP` method, is to learn, not a constant, but a function :math:`q(X)`, | ||
| to have a different interval width depending on the :math:`X` value. Then, we would have: | ||
|
|
||
| .. math:: \hat{C}_{n, \alpha}^{\textrm CCP}(X_{n+1}) = \hat{\mu}(X_{n+1}) \pm \hat{q}(X_{n+1}) | ||
|
|
||
| To be able to find the best function, while having some coverage guarantees, | ||
| we should select this function inside some defined class of functions :math:`\mathcal{F}`. | ||
|
|
||
| This method is motivated by the following equivalence: | ||
|
|
||
| .. math:: | ||
| \begin{array}{c} | ||
| \mathbb{P}(Y_{n+1} \in \hat{C} \; | \; X_{n+1}=x) = 1 - \alpha, \quad \text{for all x} \\ | ||
| \textstyle \Longleftrightarrow \\ | ||
| \mathbb{E} \left[ f(X_{n+1}) \mathbb{I} \left\{ Y_{n+1} \in \hat{C}(X_{n+1}) \right\} \right] = 0, \quad \text{for all measurable f} \\ | ||
| \end{array} | ||
|
|
||
| This is the equation corresponding to the perfect conditional coverage, which is theoretically impossible to obtain. | ||
| Then, relaxing this objective by replacing "all measurable f" with "all f belonging to some class :math:`\mathcal{F}`" | ||
| seems a way to get close to the perfect conditional coverage. | ||
|
|
||
|
|
||
| .. _theoretical_description_ccp_control_steps: | ||
|
|
||
| The method follow 3 steps: | ||
| ---------------------------- | ||
|
|
||
| 1. Choose a class of functions. The simple approach is to choose a class a finite dimension :math:`d \in \mathbb{N}`, | ||
| using, for any :math:`\Phi \; : \; \mathbb{R}^d \to \mathbb{R}` (chosen by the user) | ||
|
|
||
| .. math:: | ||
| \mathcal{F} = \left\{ \Phi (\cdot)^T \beta : \beta \in \mathbb{R}^d \right\} | ||
|
|
||
| 2. Find the best function of this class by solving the following optimization problem: | ||
|
|
||
| .. note:: It is actually a quantile regression between the transformation :math:`\Phi (X)` and the conformity scores `S`. | ||
|
|
||
| Considering an upper bound :math:`M` of the conformity scores, | ||
| such as :math:`S_{n+1} < M`: | ||
|
|
||
| .. math:: | ||
| \hat{g}_M^{n+1} := \text{arg}\min_{g \in \mathcal{F}} \; \frac{1}{n+1} \sum_{i=1}^n{l_{\alpha} (g(X_i), S_i)} \; + \frac{1}{n+1}l_{\alpha} (g(X_{n+1}), M) | ||
|
|
||
| .. warning:: | ||
| In the :ref:`API<api>`, we use by default :math:`M=max(\{S_i\}_{i\leq n})`, | ||
| the maximum conformity score of the calibration set, | ||
| but you can specify it yourself if a bound is known, considering your data, | ||
| model and conformity score. | ||
|
|
||
| Moreover, it means that there is still small computations which are done | ||
| for each test point :math:`X_{n+1}`. If you want to avoid that, you can | ||
| use ``unsafe_approximation=True``, which only consider: | ||
|
|
||
| .. math:: | ||
| \hat{g} := \text{arg}\min_{g \in \mathcal{F}} \; \frac{1}{n} \sum_{i=1}^n{l_{\alpha} (g(X_i), S_i)} | ||
|
|
||
| However, it may result in a small miscoverage. | ||
| It is recommanded to empirically check the resulting coverage on the test set. | ||
|
|
||
| 3. We use this optimized function :math:`\hat{g}_M^{n+1}` to compute the prediction intervals: | ||
|
|
||
| .. math:: | ||
| \hat{C}_M^{n+1}(X_{n+1}) = \{ y : S(X_{n+1}, \: y) \leq \hat{g}_M^{n+1}(X_{n+1}) \} | ||
|
|
||
| .. note:: The formulas are generic and work with all conformity scores. But in the case of the absolute residuals, we get: | ||
|
|
||
| .. math:: | ||
| \hat{C}(X_{n+1}) = \hat{\mu}(X_{n+1}) \pm \hat{g}_M^{n+1}(X_{n+1}) | ||
|
|
||
| .. _theoretical_description_ccp_control_coverage: | ||
|
|
||
| Coverage guarantees: | ||
| ----------------------- | ||
|
|
||
| .. warning:: | ||
| The following guarantees assume that the approximation described above is not used, and that | ||
| the chosen bound M is indeed such as :math:`\forall \text{ test index }i, \; S_i < M` | ||
|
|
||
| Following this steps, we have the coverage guarantee: | ||
| :math:`\forall f \in \mathcal{F},` | ||
|
|
||
| .. math:: | ||
| \mathbb{P}_f(Y_{n+1} \in \hat{C}_M^{n+1}(X_{n+1})) \geq 1 - \alpha | ||
|
|
||
| .. math:: | ||
| \text{and} \quad \left | \mathbb{E} \left[ f(X_{n+1}) \left(\mathbb{I} \left\{ Y_{n+1} \in \hat{C}_M^{n+1}(X_{n+1}) \right\} - (1 - \alpha) \right) \right] \right | | ||
| \leq \frac{d}{n+1} \mathbb{E} \left[ \max_{1 \leq i \leq n+1} \left|f(X_i)\right| \right] | ||
|
|
||
| .. note:: | ||
| If we want to have a homogenous coverage on some given groups in :math:`\mathcal{G}`, we can use | ||
| :math:`\mathcal{F} = \{ x \mapsto \sum _{G \in \mathcal{G}} \; \beta_G \mathbb{I} \{ x \in G \} : \beta_G \in \mathbb{R} \}`, | ||
| then we have :math:`\forall G \in \mathcal{G}`: | ||
|
|
||
| .. math:: | ||
| 1 - \alpha | ||
| \leq \mathbb{P} \left( Y_{n+1} \in \hat{C}_M^{n+1}(X_{n+1}) \; | \; X_{n+1} \in G \right) | ||
| \leq 1- \alpha + \frac{|\mathcal{G}|}{(n+1) \mathbb{P}(X_{n+1} \in G)} \\ | ||
| = 1- \alpha + \frac{\text{number of groups in } \mathcal{G}}{\text{number of samples of } \{X_i\} \text{ in G}} | ||
|
|
||
| How to use it in practice? | ||
| ============================ | ||
|
|
||
| Creating a class a function adapted to our needs | ||
| -------------------------------------------------- | ||
|
|
||
| The following will provide some tips on how to use the method (for more practical examples, see | ||
| :doc:`examples_regression/4-tutorials/plot_ccp_tutorial` or | ||
| `How to leverage the CCP method on real data | ||
| <https://github.com/scikit-learn-contrib/MAPIE/tree/master/notebooks/regression/tutorial_ccp_CandC.ipynb>`_ | ||
| ). | ||
|
|
||
| 1. If you want a generally adaptative interval and you don't have prior | ||
| knowledge about your data, you can use gaussian kernels, implemented in Mapie | ||
| in :class:`~mapie.future.calibrators.ccp.GaussianCCP`. See the API doc for more information. | ||
|
|
||
| 2. If you want to avoid bias on sub-groups and ensure an homogenous coverage on those, | ||
| you can add indicator functions corresponding to those groups. | ||
|
|
||
| 3. You can inject prior knowledge in the method using :class:`~mapie.future.calibrators.ccp.CustomCCP`, | ||
| if you have information about the conformity scores distribution | ||
| (domains with different biavior, expected model uncertainty depending on a given feature, etc). | ||
|
|
||
| 4. Empirically test obtained coverage on a test set, to make sure that the expected coverage is achieved. | ||
|
|
||
|
|
||
| Avoid miscoverage | ||
| -------------------- | ||
|
|
||
| - | To guarantee marginal coverage, you need to have an intercept term in the :math:`\Phi` function (meaning, a feature equal to :math:`1` for all :math:`X_i`). | ||
| | It correspond, in the :ref:`API<api>`, to ``bias=True``. | ||
|
|
||
| - | Some miscoverage can come from the optimization process, which is | ||
| solved with numerical methods, and may fail to find the global minimum. | ||
| If the target coverage is not achieved, you can try adding regularization, | ||
| to help the optimization process. You can also try reducing the number of dimensions :math:`d` | ||
| or using a smoother :math:`\Phi` function, such as with gaussian kernels | ||
| (indeed, using only indicator functions makes the optimization difficult). | ||
|
|
||
| .. warning:: | ||
| Adding some regularization will theoretically induce a miscoverage, | ||
| as the objective function will slightly increase, to minimize the regularization term. | ||
|
|
||
| In practice, it may increase the coverage (as it helps the optimization convergence), | ||
| but it can also decrease it. Always empirically check the resulting coverage | ||
| and avoid too big regularization terms (below :math:`10^{-4}` is usually recommanded). | ||
|
|
||
|
|
||
| - | Finally, if you have coverage issues because the optimisation is difficult, | ||
| you can artificially enforce higher coverage by reducing the value of :math:`\alpha`. | ||
| Evaluating the best adjusted :math:`\alpha` using cross-validation will ensure | ||
| the same coverage on the test set (subject to variability due to the finite number of samples). | ||
|
|
||
|
|
||
| .. _theoretical_description_ccp_references: | ||
|
|
||
| References | ||
| ========== | ||
|
|
||
| [1] Isaac Gibbs, John J. Cherian, and Emmanuel J. Candès, | ||
| "Conformal Prediction With Conditional Guarantees", `arXiv <https://arxiv.org/abs/2305.12616>`_, 2023. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.