# \[ANN\] ConformalPrediction.jl: Uncertainty quantification through conformal prediction for machine learning models trained in MLJ

**URL:** <https://discourse.julialang.org/t/ann-conformalprediction-jl-uncertainty-quantification-through-conformal-prediction-for-machine-learning-models-trained-in-mlj/89348>\
**Category:** Package Announcements\
**Tags:** package, machine-learning, mlj, uncertainty-quantifi, conformal-prediction\
**Created:** [October 27, 2022, 8:18am UTC](https://discourse.julialang.org/t/ann-conformalprediction-jl-uncertainty-quantification-through-conformal-prediction-for-machine-learning-models-trained-in-mlj/89348 "2022-10-27T08:18:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)\
**Post date:** [October 27, 2022, 8:18am UTC](https://discourse.julialang.org/t/ann-conformalprediction-jl-uncertainty-quantification-through-conformal-prediction-for-machine-learning-models-trained-in-mlj/89348/1 "2022-10-27T08:18:06Z")

</div>

Very excited to announce [`ConformalPrediction.jl`](https://github.com/pat-alt/ConformalPrediction.jl) 🎉

# ConformalPrediction

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://pat-alt.github.io/ConformalPrediction.jl/stable/) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://pat-alt.github.io/ConformalPrediction.jl/dev/) [![Build Status](https://github.com/pat-alt/ConformalPrediction.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/pat-alt/ConformalPrediction.jl/actions/workflows/CI.yml?query=branch%3Amain) [![Coverage](https://codecov.io/gh/pat-alt/ConformalPrediction.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/pat-alt/ConformalPrediction.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) [![ColPrac: Contributor’s Guide on Collaborative Practices for Community Packages](https://global.discourse-cdn.com/julialang/original/3X/3/c/3cd6f96b9aa36710fac73ce8997a697cddba00f1.png)](https://github.com/SciML/ColPrac) [![Twitter Badge](https://img.shields.io/twitter/url/https/twitter.com/paltmey.svg?style=social&label=Follow%20%40paltmey)](https://twitter.com/paltmey)

`ConformalPrediction.jl` is a package for Uncertainty Quantification (UQ) through Conformal Prediction (CP) in Julia. It is designed to work with supervised models trained in [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) Blaom et al. (2020). Conformal Prediction is distribution-free, easy-to-understand, easy-to-use and model-agnostic.

# 📖 Background

Conformal Prediction is a scalable frequentist approach to uncertainty quantification and coverage control. It promises to be an easy-to-understand, distribution-free and model-agnostic way to generate statistically rigorous uncertainty estimates. Interestingly, it can even be used to complement Bayesian methods.

The animation below is lifted from a small blog post that introduces the topic and the package ([[TDS](https://towardsdatascience.com/conformal-prediction-in-julia-351b81309e30)], [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-prediction/#fig-anim)]). It shows conformal prediction sets for two different samples and changing coverage rates. Standard conformal classifiers produce set-valued predictions: for ambiguous samples these sets are typically large (for high coverage) or empty (for low coverage).

![Conformal Prediction in action: Prediction sets for two different samples and changing coverage rates. As coverage grows, so does the size of the prediction sets.](https://global.discourse-cdn.com/julialang/original/3X/0/f/0f387fc015b6d11b627bdbe837323af0331fd732.gif)

## 🚩 Installation

You can install the latest stable release from the general registry:

```julia
using Pkg
Pkg.add("ConformalPrediction")

```

The development version can be installed as follows:

```julia
using Pkg
Pkg.add(url="https://github.com/pat-alt/ConformalPrediction.jl")

```

## 🔁 Status

This package is in its early stages of development and therefore still subject to changes to the core architecture and API. The following CP approaches have been implemented in the development version:

**Regression** :

- Inductive
- Naive Transductive
- Jackknife
- Jackknife+
- Jackknife-minmax
- CV+
- CV-minmax

**Classification** :

- Inductive (LABEL (Sadinle, Lei, and Wasserman 2019))
- Adaptive Inductive

The package has been tested for the following supervised models offered by [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/).

**Regression** :

```julia
using ConformalPrediction
keys(tested_atomic_models[:regression])

```

```
KeySet for a Dict{Symbol, Expr} with 4 entries. Keys:
  :nearest_neighbor
  :evo_tree
  :light_gbm
  :decision_tree

```

**Classification** :

```julia
keys(tested_atomic_models[:classification])

```

```
KeySet for a Dict{Symbol, Expr} with 4 entries. Keys:
  :nearest_neighbor
  :evo_tree
  :light_gbm
  :decision_tree

```

## 🔍 Usage Example

To illustrate the intended use of the package, let’s have a quick look at a simple regression problem. Using [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) we first generate some synthetic data and then determine indices for our training, calibration and test data:

```julia
using MLJ
X, y = MLJ.make_regression(1000, 2)
train, test = partition(eachindex(y), 0.4, 0.4)

```

We then import a decision tree ([`EvoTrees.jl`](https://github.com/Evovest/EvoTrees.jl)) following the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) procedure.

```julia
EvoTreeRegressor = @load EvoTreeRegressor pkg=EvoTrees
model = EvoTreeRegressor() 

```

To turn our conventional model into a conformal model, we just need to declare it as such by using `conformal_model` wrapper function. The generated conformal model instance can wrapped in data to create a _machine_. Finally, we proceed by fitting the machine on training data using the generic `fit!` method:

```julia
using ConformalPrediction
conf_model = conformal_model(model)
mach = machine(conf_model, X, y)
fit!(mach, rows=train)

```

Predictions can then be computed using the generic `predict` method. The code below produces predictions for the first `n` samples. Each tuple contains the lower and upper bound for the prediction interval.

```julia
n = 10
Xtest = selectrows(X, first(test,n))
ytest = y[first(test,n)]
predict(mach, Xtest)

```

```
╭────────────────────────────────────────────────────────────────╮
│ │
│ (1) ([-1.0398755385842378], [1.174649631946424]) │
│ (2) ([-0.8812446360021866], [1.333280534528475]) │
│ (3) ([-1.0186882105711579], [1.1958369599595038]) │
│ (4) ([-1.8854818442600265], [0.32904332627063515]) │
│ (5) ([-1.5473925987675485], [0.6671325717631131]) │
│ (6) ([-1.7896211025024724], [0.42490406802818925]) │
│ (7) ([-1.9246506093872306], [0.289874561143431]) │
│ (8) ([-0.9791712385383624], [1.2353539319922993]) │
│ (9) ([-1.7526388729209201], [0.4618862976097414]) │
│ (10) ([-0.5015897849914924], [1.7129353855391694]) │
│ │
│ │
╰─────────────────────────────────────────────────── 10 items ───╯

```

## 🛠 Contribute

Contributions are welcome! Please follow the [SciML ColPrac guide](https://github.com/SciML/ColPrac).

## 🎓 References

Blaom, Anthony D., Franz Kiraly, Thibaut Lienart, Yiannis Simillides, Diego Arenas, and Sebastian J. Vollmer. 2020. “MLJ: A Julia Package for Composable Machine Learning.” _Journal of Open Source Software_ 5 (55): 2704. [https://doi.org/10.21105/joss.02704](https://doi.org/10.21105/joss.02704).

Sadinle, Mauricio, Jing Lei, and Larry Wasserman. 2019. “Least Ambiguous Set-Valued Classifiers with Bounded Error Levels.” _Journal of the American Statistical Association_ 114 (525): 223–34.

---

<div class="post-metadata">

**Author:** ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)\
**Post date:** [October 27, 2022, 8:52am UTC](https://discourse.julialang.org/t/ann-conformalprediction-jl-uncertainty-quantification-through-conformal-prediction-for-machine-learning-models-trained-in-mlj/89348/2 "2022-10-27T08:52:55Z")

</div>

For anyone interested in contributing to this, please see also [here](https://github.com/alan-turing-institute/MLJ.jl/issues/978)
