diff --git a/Project.toml b/Project.toml index 925bf3b..63f9afe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ConformalPrediction" uuid = "98bfc277-1877-43dc-819b-a3e38c30242f" authors = ["Patrick Altmeyer"] -version = "0.1.9" +version = "0.1.10" [deps] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" diff --git a/README.ipynb b/README.ipynb deleted file mode 100644 index 196ce01..0000000 --- a/README.ipynb +++ /dev/null @@ -1,434 +0,0 @@ -{ - "cells": [ - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "---\n", - "format:\n", - " commonmark:\n", - " variant: '-raw_html'\n", - " wrap: none\n", - "execute:\n", - " freeze: auto\n", - " echo: true\n", - " eval: true\n", - " output: false\n", - "crossref:\n", - " fig-prefix: Figure\n", - " tbl-prefix: Table\n", - "bibliography: 'https://raw.githubusercontent.com/pat-alt/bib/main/bib.bib'\n", - "---" - ], - "id": "1a68c0e0" - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![](dev/logo/wide_logo.png)\n", - "\n", - "[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliatrustworthyai.github.io/ConformalPrediction.jl/stable/)\n", - "[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://juliatrustworthyai.github.io/ConformalPrediction.jl/dev/)\n", - "[![Build Status](https://github.com/juliatrustworthyai/ConformalPrediction.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/juliatrustworthyai/ConformalPrediction.jl/actions/workflows/CI.yml?query=branch%3Amain)\n", - "[![Coverage](https://codecov.io/gh/juliatrustworthyai/ConformalPrediction.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/juliatrustworthyai/ConformalPrediction.jl)\n", - "[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)\n", - " [![License](https://img.shields.io/github/license/juliatrustworthyai/ConformalPrediction.jl)](LICENSE) \n", - " [![Package Downloads](https://shields.io/endpoint?url=https://pkgs.genieframework.com/api/v1/badge/ConformalPrediction/)](https://pkgs.genieframework.com?packages=ConformalPrediction) \n", - "\n", - "# ConformalPrediction" - ], - "id": "7a45a074" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| echo: false\n", - "using Pkg; Pkg.activate(\"docs\")\n", - "using Plots\n", - "using TaijaPlotting\n", - "theme(:wong)\n", - "using Random\n", - "Random.seed!(123)\n", - "using ConformalPrediction" - ], - "id": "f4268f3d", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`ConformalPrediction.jl` is a package for Predictive 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/) [@blaom2020mlj]. Conformal Prediction is easy-to-understand, easy-to-use and model-agnostic and it works under minimal distributional assumptions. \n", - "\n", - "## πŸƒ Quick Tour\n", - "\n", - "> First time here? Take a quick interactive [tour](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl) to see what this package can do right on [JuliaHub](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl){target=\"_blank\"} (To run the notebook, hit login and then edit). \n", - "\n", - "This [`Pluto.jl`](https://github.com/fonsp/Pluto.jl){target=\"_blank\"} 🎈 notebook won the 2nd Price in the [JuliaCon 2023 Notebook Competition](https://info.juliahub.com/pluto-notebook-winner-23).\n", - "\n", - "### Local Tour\n", - "\n", - "To run the tour locally, just clone this repo and start `Pluto.jl` as follows:\n", - "\n", - "```{.julia}\n", - "] add Pluto\n", - "using Pluto\n", - "Pluto.run()\n", - "```\n", - "\n", - "All notebooks are contained in `docs/pluto`.\n", - "\n", - "## πŸ“– Background\n", - "\n", - "Don't worry, we're not about to deep-dive into methodology. But just to give you a high-level description of Conformal Prediction (CP) upfront:\n", - "\n", - "> Conformal prediction (a.k.a. conformal inference) is a user-friendly paradigm for creating statistically rigorous uncertainty sets/intervals for the predictions of such models. Critically, the sets are valid in a distribution-free sense: they possess explicit, non-asymptotic guarantees even without distributional assumptions or model assumptions.\n", - ">\n", - "> --- @angelopoulos2021gentle\n", - "\n", - "Intuitively, CP works under the premise of turning heuristic notions of uncertainty into rigorous uncertainty estimates through repeated sampling or the use of dedicated calibration data. \n", - "\n", - "![Conformal Prediction in action: prediction intervals at varying coverage rates. As coverage grows, so does the width of the prediction interval.](https://raw.githubusercontent.com/pat-alt/pat-alt.github.io/main/blog/posts/conformal-regression/www/medium.gif)\n", - "\n", - "The animation above is lifted from a small blog [post](https://www.paltmeyer.com/blog/posts/conformal-regression/) that introduces Conformal Prediction and this package in the context of regression. It shows how the prediction interval and the test points that it covers varies in size as the user-specified coverage rate changes.\n", - "\n", - "## 🚩 Installation \n", - "\n", - "You can install the latest stable release from the general registry:\n", - "\n", - "```{.julia}\n", - "using Pkg\n", - "Pkg.add(\"ConformalPrediction\")\n", - "```\n", - "\n", - "The development version can be installed as follows:\n", - "\n", - "```{.julia}\n", - "using Pkg\n", - "Pkg.add(url=\"https://github.com/juliatrustworthyai/ConformalPrediction.jl\")\n", - "```\n", - "\n", - "## πŸ” Usage Example \n", - "\n", - "To illustrate the intended use of the package, let's have a quick look at a simple regression problem. We first generate some synthetic data and then determine indices for our training and test data using [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/):" - ], - "id": "f57cee42" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "using MLJ\n", - "\n", - "# Inputs:\n", - "N = 600\n", - "xmax = 3.0\n", - "using Distributions\n", - "d = Uniform(-xmax, xmax)\n", - "X = rand(d, N)\n", - "X = reshape(X, :, 1)\n", - "\n", - "# Outputs:\n", - "noise = 0.5\n", - "fun(X) = sin(X)\n", - "Ξ΅ = randn(N) .* noise\n", - "y = @.(fun(X)) + Ξ΅\n", - "y = vec(y)\n", - "\n", - "# Partition:\n", - "train, test = partition(eachindex(y), 0.4, 0.4, shuffle=true)" - ], - "id": "c4c41fca", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We then import Symbolic Regressor ([`SymbolicRegression.jl`](https://github.com/MilesCranmer/SymbolicRegression.jl)) following the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) procedure." - ], - "id": "3368c957" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "regressor = @load SRRegressor pkg=SymbolicRegression\n", - "model = regressor(\n", - " niterations=50,\n", - " binary_operators=[+, -, *],\n", - " unary_operators=[sin],\n", - ")" - ], - "id": "d02dd42e", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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:" - ], - "id": "5e6b6edc" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "using ConformalPrediction\n", - "conf_model = conformal_model(model)\n", - "mach = machine(conf_model, X, y)\n", - "fit!(mach, rows=train)" - ], - "id": "f89ff4c3", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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. " - ], - "id": "b3713843" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| output: true\n", - "show_first = 5\n", - "Xtest = selectrows(X, test)\n", - "ytest = y[test]\n", - "yΜ‚ = predict(mach, Xtest)\n", - "yΜ‚[1:show_first]" - ], - "id": "269f00d5", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For simple models like this one, we can call a custom `Plots` recipe on our instance, fit result and data to generate the chart below:" - ], - "id": "b177964b" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| output: true\n", - "\n", - "using Plots\n", - "zoom = 0\n", - "plt = plot(mach.model, mach.fitresult, Xtest, ytest, lw=5, zoom=zoom, observed_lab=\"Test points\")\n", - "xrange = range(-xmax+zoom,xmax-zoom,length=N)\n", - "plot!(plt, xrange, @.(fun(xrange)), lw=2, ls=:dash, colour=:darkorange, label=\"Ground truth\")" - ], - "id": "f1820561", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| eval: false\n", - "#| echo: false\n", - "\n", - "theme(:lime)\n", - "\n", - "Xtest = selectrows(X, test)\n", - "ytest = y[test]\n", - "\n", - "max_z = 5\n", - "anim = @animate for z in 0:0.1:max_z\n", - "\n", - " z = -z\n", - "\n", - " # Test points:\n", - " xleft = -xmax + z\n", - " xright = xmax - z\n", - " global Xtest = vcat(xleft, Xtest, xright)\n", - " yleft = fun(xleft) .+ randn(1) .* noise\n", - " yright = fun(xright) .+ randn(1) .* noise\n", - " global ytest = vcat(yleft, ytest, yright)\n", - "\n", - " # Plot:\n", - " plt = plot(mach.model, mach.fitresult, Xtest, ytest, \n", - " lw=5, zoom=zoom, observed_lab=\"Test points\", dpi=300, legend=false, axis=true,\n", - " size=(800,400))\n", - " xrange = range(-xmax-max_z,xmax+max_z,length=N)\n", - " plot!(plt, xrange, @.(fun(xrange)), \n", - " lw=2, ls=:dash, label=\"Ground truth\", xlim=extrema(xrange), ylim=(-2.0,2.0))\n", - "end\n", - "\n", - "gif(anim, fps=10)" - ], - "id": "3ba27895", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can evaluate the conformal model using the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) workflow with a custom performance measure. You can use either `emp_coverage` for the overall empirical coverage (correctness) or `ssc` for the size-stratified coverage rate (adaptiveness)." - ], - "id": "02837116" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| output: true\n", - "\n", - "_eval = evaluate!(mach; measure=[emp_coverage, ssc], verbosity=0)\n", - "display(_eval)\n", - "println(\"Empirical coverage: $(round(_eval.measurement[1], digits=3))\")\n", - "println(\"SSC: $(round(_eval.measurement[2], digits=3))\")" - ], - "id": "6df6a6c4", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## πŸ“š Read on\n", - "\n", - "If after reading the usage example above you are just left with more questions about the topic, that's normal. Below we have have collected a number of further resources to help you get started with this package and the topic itself:\n", - "\n", - "1. Blog post introducing conformal classifiers: [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-prediction/)], [[TDS](https://medium.com/towards-data-science/conformal-prediction-in-julia-351b81309e30)], [[Forem](https://forem.julialang.org/patalt/conformal-prediction-in-julia-h9n)].\n", - "2. Blog post applying CP to a deep learning image classifier: [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-image-classifier/)], [[TDS](https://medium.com/towards-data-science/how-to-conformalize-a-deep-image-classifier-14ead4e1a5a0)], [[Forem](https://forem.julialang.org/patalt/how-to-conformalize-a-deep-image-classifier-50p2)].\n", - "3. The package [docs](https://juliatrustworthyai.github.io/ConformalPrediction.jl/dev/) and in particular the [FAQ](https://www.paltmeyer.com/ConformalPrediction.jl/dev/faq/).\n", - "\n", - "### External Resources\n", - "\n", - "- *A Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification* by @angelopoulos2021gentle ([pdf](https://arxiv.org/pdf/2107.07511.pdf)).\n", - "- *Predictive inference with the jackknife+* by @barber2021predictive ([pdf](https://projecteuclid.org/journals/annals-of-statistics/volume-49/issue-1/Predictive-inference-with-the-jackknife/10.1214/20-AOS1965.full))\n", - "- *Awesome Conformal Prediction* repository by Valery Manokhin ([repo](https://github.com/valeman/awesome-conformal-prediction)).\n", - "- [Documentation](https://mapie.readthedocs.io/en/latest/index.html) for the Python package MAPIE.\n", - "\n", - "## πŸ” Status \n", - "\n", - "This package is in its early stages of development and therefore still subject to changes to the core architecture and API. \n", - "\n", - "### Implemented Methodologies\n", - "\n", - "The following CP approaches have been implemented:\n", - "\n", - "**Regression**:\n", - "\n", - "- Inductive \n", - "- Naive Transductive \n", - "- Jackknife \n", - "- Jackknife+ \n", - "- Jackknife-minmax\n", - "- CV+\n", - "- CV-minmax\n", - "\n", - "**Classification**:\n", - "\n", - "- Inductive\n", - "- Naive Transductive \n", - "- Adaptive Inductive\n", - "\n", - "The package has been tested for the following supervised models offered by [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/).\n", - "\n", - "**Regression**:" - ], - "id": "51c8d581" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| output: true\n", - "keys(tested_atomic_models[:regression])" - ], - "id": "9b93b4cb", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Classification**:" - ], - "id": "043d50cb" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| output: true\n", - "keys(tested_atomic_models[:classification])" - ], - "id": "0f0aab54", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Implemented Evaluation Metrics\n", - "\n", - "To evaluate conformal predictors we are typically interested in correctness and adaptiveness. The former can be evaluated by looking at the empirical coverage rate, while the latter can be assessed through metrics that address the conditional coverage [@angelopoulos2021gentle]. To this end, the following metrics have been implemented:\n", - "\n", - "- `emp_coverage` (empirical coverage)\n", - "- `ssc` (size-stratified coverage)\n", - "\n", - "There is also a simple `Plots.jl` recipe that can be used to inspect the set sizes. In the regression case, the interval width is stratified into discrete bins for this purpose:" - ], - "id": "738ac9b6" - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "#| output: true\n", - "\n", - "bar(mach.model, mach.fitresult, X)" - ], - "id": "7989833b", - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## πŸ›  Contribute \n", - "\n", - "Contributions are welcome! A good place to start is the [list](https://github.com/juliatrustworthyai/ConformalPrediction.jl/issues) of outstanding issues. For more details, see also the [Contributor's Guide](https://www.paltmeyer.com/ConformalPrediction.jl/dev/contribute/). Please follow the [SciML ColPrac guide](https://github.com/SciML/ColPrac).\n", - "\n", - "## πŸ™ Thanks\n", - "\n", - "To build this package I have read and re-read both @angelopoulos2021gentle and @barber2021predictive. The Awesome Conformal Prediction [repository](https://github.com/valeman/awesome-conformal-prediction) [@manokhin2022awesome] has also been a fantastic place to get started. Thanks also to [\\@aangelopoulos](https://github.com/aangelopoulos), [\\@valeman](https://github.com/valeman) and others for actively contributing to discussions on here. Quite a few people have also recently started using and contributing to the package for which I am very grateful. Finally, many thanks to Anthony Blaom ([\\@ablaom](https://github.com/ablaom)) for many helpful discussions about how to interface this package to `MLJ.jl`.\n", - "\n", - "## πŸŽ“ References \n" - ], - "id": "9db36793" - } - ], - "metadata": { - "kernelspec": { - "name": "julia-1.9", - "language": "julia", - "display_name": "Julia 1.9.3" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} \ No newline at end of file diff --git a/README.md b/README.md index 0f75edc..a8de98c 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ ## πŸƒ Quick Tour -> First time here? Take a quick interactive [tour](https://binder.plutojl.org/v0.19.12/open?url=https%253A%252F%252Fraw.githubusercontent.com%252Fpat-alt%252FConformalPrediction.jl%252Fmain%252Fdocs%252Fpluto%252Fintro.jl) to see what this package can do: [![Binder](https://mybinder.org/badge_logo.svg)](https://binder.plutojl.org/v0.19.12/open?url=https%253A%252F%252Fraw.githubusercontent.com%252Fpat-alt%252FConformalPrediction.jl%252Fmain%252Fdocs%252Fpluto%252Fintro.jl) +> First time here? Take a quick interactive [tour](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl) to see what this package can do right on [JuliaHub](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl) (To run the notebook, hit login and then edit). -The button takes you to a [`Pluto.jl`](https://github.com/fonsp/Pluto.jl) 🎈 notebook hosted on [binder](https://mybinder.org/). In my own experience, this may take some time to load, certainly long enough to get yourself a hot beverage β˜•. Alternatively, you can run the notebook locally or skip the tour for now and read on below. +This [`Pluto.jl`](https://github.com/fonsp/Pluto.jl) 🎈 notebook won the 2nd Price in the [JuliaCon 2023 Notebook Competition](https://info.juliahub.com/pluto-notebook-winner-23). ### Local Tour @@ -111,11 +111,11 @@ yΜ‚[1:show_first] ``` 5-element Vector{Tuple{Float64, Float64}}: - (0.0458889297242715, 1.9182762960257687) - (-1.9174452847238976, -0.04505791842240037) - (-1.2544275358451678, 0.6179598304563294) - (-0.2818835218505735, 1.5905038444509236) - (0.01299565032151917, 1.8853830166230163) + (-0.04087262272113379, 1.8635644669554758) + (0.04647464096907805, 1.9509117306456876) + (-0.24248802236397216, 1.6619490673126376) + (-0.07841928163933476, 1.8260178080372749) + (-0.02268628324126465, 1.881750806435345) For simple models like this one, we can call a custom `Plots` recipe on our instance, fit result and data to generate the chart below: @@ -139,20 +139,20 @@ println("SSC: $(round(_eval.measurement[2], digits=3))") ``` PerformanceEvaluation object with these fields: - measure, operation, measurement, per_fold, + model, measure, operation, measurement, per_fold, per_observation, fitted_params_per_fold, - report_per_fold, train_test_rows + report_per_fold, train_test_rows, resampling, repeats Extract: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€ β”‚ measure β”‚ operation β”‚ measurement β”‚ 1.9 β‹― β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€ - β”‚ ConformalPrediction.emp_coverage β”‚ predict β”‚ 0.948 β”‚ 0.0 β‹― - β”‚ ConformalPrediction.size_stratified_coverage β”‚ predict β”‚ 0.948 β”‚ 0.0 β‹― + β”‚ ConformalPrediction.emp_coverage β”‚ predict β”‚ 0.953 β”‚ 0.0 β‹― + β”‚ ConformalPrediction.size_stratified_coverage β”‚ predict β”‚ 0.953 β”‚ 0.0 β‹― └──────────────────────────────────────────────┴───────────┴─────────────┴────── 2 columns omitted - Empirical coverage: 0.948 - SSC: 0.948 + Empirical coverage: 0.953 + SSC: 0.953 ## πŸ“š Read on @@ -240,14 +240,14 @@ Contributions are welcome! A good place to start is the [list](https://github.co ## πŸ™ Thanks -To build this package I have read and re-read both Angelopoulos and Bates (2021) and Barber et al. (2021). The Awesome Conformal Prediction [repository](https://github.com/valeman/awesome-conformal-prediction) (Manokhin, n.d.) has also been a fantastic place to get started. Thanks also to [@aangelopoulos](https://github.com/aangelopoulos), [@valeman](https://github.com/valeman) and others for actively contributing to discussions on here. Quite a few people have also recently started using and contributing to the package for which I am very grateful. Finally, many thanks to Anthony Blaom ([@ablaom](https://github.com/ablaom)) for many helpful discussions about how to interface this package to `MLJ.jl`. +To build this package I have read and re-read both Angelopoulos and Bates (2021) and Barber et al. (2021). The Awesome Conformal Prediction [repository](https://github.com/valeman/awesome-conformal-prediction) (Manokhin 2022) has also been a fantastic place to get started. Thanks also to [@aangelopoulos](https://github.com/aangelopoulos), [@valeman](https://github.com/valeman) and others for actively contributing to discussions on here. Quite a few people have also recently started using and contributing to the package for which I am very grateful. Finally, many thanks to Anthony Blaom ([@ablaom](https://github.com/ablaom)) for many helpful discussions about how to interface this package to `MLJ.jl`. ## πŸŽ“ References -Angelopoulos, Anastasios N., and Stephen Bates. 2021. β€œA Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification.” . +Angelopoulos, Anastasios N, and Stephen Bates. 2021. β€œA Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification.” *arXiv Preprint arXiv:2107.07511*. Barber, Rina Foygel, Emmanuel J. CandΓ¨s, Aaditya Ramdas, and Ryan J. Tibshirani. 2021. β€œPredictive Inference with the Jackknife+.” *The Annals of Statistics* 49 (1): 486–507. . 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. . -Manokhin, Valery. n.d. β€œAwesome Conformal Prediction.” +Manokhin, Valery. 2022. β€œAwesome Conformal Prediction.” Zenodo. . diff --git a/README_files/figure-commonmark/cell-12-output-1.svg b/README_files/figure-commonmark/cell-12-output-1.svg index 0ea75b9..20cb47e 100644 --- a/README_files/figure-commonmark/cell-12-output-1.svg +++ b/README_files/figure-commonmark/cell-12-output-1.svg @@ -1,49 +1,49 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/README_files/figure-commonmark/cell-7-output-1.svg b/README_files/figure-commonmark/cell-7-output-1.svg index b253af4..b943f97 100644 --- a/README_files/figure-commonmark/cell-7-output-1.svg +++ b/README_files/figure-commonmark/cell-7-output-1.svg @@ -1,288 +1,285 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/docs/src/index/execute-results/md.json b/_freeze/docs/src/index/execute-results/md.json index 0096fa8..09b922a 100644 --- a/_freeze/docs/src/index/execute-results/md.json +++ b/_freeze/docs/src/index/execute-results/md.json @@ -1,7 +1,7 @@ { "hash": "00c3ee841711180ac18233205c0fd8eb", "result": { - "markdown": "---\ntitle: ConformalPrediction\n---\n\n\n```@meta\nCurrentModule = ConformalPrediction\n```\n\n![](assets/wide_logo.png)\n\nDocumentation for [ConformalPrediction.jl](https://github.com/juliatrustworthyai/ConformalPrediction.jl).\n\n\n\n\n\n`ConformalPrediction.jl` is a package for Predictive 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/) [@blaom2020mlj]. Conformal Prediction is easy-to-understand, easy-to-use and model-agnostic and it works under minimal distributional assumptions. \n\n## πŸƒ Quick Tour\n\n> First time here? Take a quick interactive [tour](https://binder.plutojl.org/v0.19.12/open?url=https%253A%252F%252Fraw.githubusercontent.com%252Fpat-alt%252FConformalPrediction.jl%252Fmain%252Fdocs%252Fpluto%252Fintro.jl) to see what this package can do: [![Binder](https://mybinder.org/badge_logo.svg)](https://binder.plutojl.org/v0.19.12/open?url=https%253A%252F%252Fraw.githubusercontent.com%252Fpat-alt%252FConformalPrediction.jl%252Fmain%252Fdocs%252Fpluto%252Fintro.jl){target=\"_blank\"}\n\nThe button takes you to a [`Pluto.jl`](https://github.com/fonsp/Pluto.jl){target=\"_blank\"} 🎈 notebook hosted on [binder](https://mybinder.org/){target=\"_blank\"}. In my own experience, this may take some time to load, certainly long enough to get yourself a hot beverage β˜•. Alternatively, you can run the notebook locally or skip the tour for now and read on below. \n\n### Local Tour\n\nTo run the tour locally, just clone this repo and start `Pluto.jl` as follows:\n\n```{.julia}\n] add Pluto\nusing Pluto\nPluto.run()\n```\n\nAll notebooks are contained in `docs/pluto`.\n\n## πŸ“– Background\n\nDon't worry, we're not about to deep-dive into methodology. But just to give you a high-level description of Conformal Prediction (CP) upfront:\n\n> Conformal prediction (a.k.a. conformal inference) is a user-friendly paradigm for creating statistically rigorous uncertainty sets/intervals for the predictions of such models. Critically, the sets are valid in a distribution-free sense: they possess explicit, non-asymptotic guarantees even without distributional assumptions or model assumptions.\n>\n> --- @angelopoulos2021gentle\n\nIntuitively, CP works under the premise of turning heuristic notions of uncertainty into rigorous uncertainty estimates through repeated sampling or the use of dedicated calibration data. \n\n![Conformal Prediction in action: prediction intervals at varying coverage rates. As coverage grows, so does the width of the prediction interval.](https://raw.githubusercontent.com/pat-alt/pat-alt.github.io/main/blog/posts/conformal-regression/www/medium.gif)\n\nThe animation above is lifted from a small blog [post](https://www.paltmeyer.com/blog/posts/conformal-regression/) that introduces Conformal Prediction and this package in the context of regression. It shows how the prediction interval and the test points that it covers varies in size as the user-specified coverage rate changes.\n\n## 🚩 Installation \n\nYou can install the latest stable release from the general registry:\n\n```{.julia}\nusing Pkg\nPkg.add(\"ConformalPrediction\")\n```\n\nThe development version can be installed as follows:\n\n```{.julia}\nusing Pkg\nPkg.add(url=\"https://github.com/juliatrustworthyai/ConformalPrediction.jl\")\n```\n\n## πŸ” Usage Example \n\nTo illustrate the intended use of the package, let's have a quick look at a simple regression problem. We first generate some synthetic data and then determine indices for our training and test data using [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/):\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nusing MLJ\n\n# Inputs:\nN = 600\nxmax = 3.0\nusing Distributions\nd = Uniform(-xmax, xmax)\nX = rand(d, N)\nX = reshape(X, :, 1)\n\n# Outputs:\nnoise = 0.5\nfun(X) = X * sin(X)\nΞ΅ = randn(N) .* noise\ny = @.(fun(X)) + Ξ΅\ny = vec(y)\n\n# Partition:\ntrain, test = partition(eachindex(y), 0.4, 0.4, shuffle=true)\n```\n:::\n\n\nWe then import Symbolic Regressor ([`SymbolicRegression.jl`](https://github.com/MilesCranmer/SymbolicRegression.jl)) following the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) procedure.\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\nregressor = @load SRRegressor pkg=SymbolicRegression\nmodel = regressor(\n niterations=50,\n binary_operators=[+, -, *],\n unary_operators=[sin],\n)\n```\n:::\n\n\nTo 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:\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\nusing ConformalPrediction\nconf_model = conformal_model(model)\nmach = machine(conf_model, X, y)\nfit!(mach, rows=train)\n```\n:::\n\n\nPredictions 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. \n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nshow_first = 5\nXtest = selectrows(X, test)\nytest = y[test]\nyΜ‚ = predict(mach, Xtest)\nyΜ‚[1:show_first]\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\n5-element Vector{Tuple{Float64, Float64}}:\n (-0.40997718991694765, 1.449009293726001)\n (0.8484810430118421, 2.7074675266547907)\n (0.547852151594671, 2.4068386352376194)\n (-0.022697652913589494, 1.8362888307293592)\n (0.07435130847990101, 1.9333377921228496)\n```\n:::\n:::\n\n\nFor simple models like this one, we can call a custom `Plots` recipe on our instance, fit result and data to generate the chart below:\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\nusing Plots\nzoom = 0\nplt = plot(mach.model, mach.fitresult, Xtest, ytest, lw=5, zoom=zoom, observed_lab=\"Test points\")\nxrange = range(-xmax+zoom,xmax-zoom,length=N)\nplot!(plt, xrange, @.(fun(xrange)), lw=2, ls=:dash, colour=:darkorange, label=\"Ground truth\")\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n![](index_files/figure-commonmark/cell-7-output-1.svg){}\n:::\n:::\n\n\nWe can evaluate the conformal model using the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) workflow with a custom performance measure. You can use either `emp_coverage` for the overall empirical coverage (correctness) or `ssc` for the size-stratified coverage rate (adaptiveness).\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\n_eval = evaluate!(mach; measure=[emp_coverage, ssc], verbosity=0)\ndisplay(_eval)\nprintln(\"Empirical coverage: $(round(_eval.measurement[1], digits=3))\")\nprintln(\"SSC: $(round(_eval.measurement[2], digits=3))\")\n```\n\n::: {.cell-output .cell-output-stdout}\n```\nStarted!\n```\n:::\n\n::: {.cell-output .cell-output-display}\n```\nPerformanceEvaluation object with these fields:\n measure, operation, measurement, per_fold,\n per_observation, fitted_params_per_fold,\n report_per_fold, train_test_rows\nExtract:\nβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€\nβ”‚ measure β”‚ operation β”‚ measurement β”‚ 1.9 β‹―\nβ”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€\nβ”‚ ConformalPrediction.emp_coverage β”‚ predict β”‚ 0.945 β”‚ 0.0 β‹―\nβ”‚ ConformalPrediction.size_stratified_coverage β”‚ predict β”‚ 0.945 β”‚ 0.0 β‹―\n└──────────────────────────────────────────────┴───────────┴─────────────┴──────\n 2 columns omitted\n```\n:::\n:::\n\n\n## πŸ“š Read on\n\nIf after reading the usage example above you are just left with more questions about the topic, that's normal. Below we have have collected a number of further resources to help you get started with this package and the topic itself:\n\n1. Blog post introducing conformal classifiers: [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-prediction/)], [[TDS](https://medium.com/towards-data-science/conformal-prediction-in-julia-351b81309e30)], [[Forem](https://forem.julialang.org/patalt/conformal-prediction-in-julia-h9n)].\n2. Blog post applying CP to a deep learning image classifier: [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-image-classifier/)], [[TDS](https://medium.com/towards-data-science/how-to-conformalize-a-deep-image-classifier-14ead4e1a5a0)], [[Forem](https://forem.julialang.org/patalt/how-to-conformalize-a-deep-image-classifier-50p2)].\n3. The package [docs](https://juliatrustworthyai.github.io/ConformalPrediction.jl/dev/) and in particular the [FAQ](https://www.paltmeyer.com/ConformalPrediction.jl/dev/faq/).\n\n### External Resources\n\n- *A Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification* by @angelopoulos2021gentle ([pdf](https://arxiv.org/pdf/2107.07511.pdf)).\n- *Predictive inference with the jackknife+* by @barber2021predictive ([pdf](https://projecteuclid.org/journals/annals-of-statistics/volume-49/issue-1/Predictive-inference-with-the-jackknife/10.1214/20-AOS1965.full))\n- *Awesome Conformal Prediction* repository by Valery Manokhin ([repo](https://github.com/valeman/awesome-conformal-prediction)).\n- [Documentation](https://mapie.readthedocs.io/en/latest/index.html) for the Python package MAPIE.\n\n## πŸ” Status \n\nThis package is in its early stages of development and therefore still subject to changes to the core architecture and API. \n\n### Implemented Methodologies\n\nThe following CP approaches have been implemented:\n\n**Regression**:\n\n- Inductive \n- Naive Transductive \n- Jackknife \n- Jackknife+ \n- Jackknife-minmax\n- CV+\n- CV-minmax\n\n**Classification**:\n\n- Inductive\n- Naive Transductive \n- Adaptive Inductive\n\nThe package has been tested for the following supervised models offered by [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/).\n\n**Regression**:\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\nkeys(tested_atomic_models[:regression])\n```\n\n::: {.cell-output .cell-output-display execution_count=9}\n```\nKeySet for a Dict{Symbol, Expr} with 5 entries. Keys:\n :ridge\n :lasso\n :evo_tree\n :nearest_neighbor\n :linear\n```\n:::\n:::\n\n\n**Classification**:\n\n::: {.cell execution_count=9}\n``` {.julia .cell-code}\nkeys(tested_atomic_models[:classification])\n```\n\n::: {.cell-output .cell-output-display execution_count=10}\n```\nKeySet for a Dict{Symbol, Expr} with 3 entries. Keys:\n :nearest_neighbor\n :evo_tree\n :logistic\n```\n:::\n:::\n\n\n### Implemented Evaluation Metrics\n\nTo evaluate conformal predictors we are typically interested in correctness and adaptiveness. The former can be evaluated by looking at the empirical coverage rate, while the latter can be assessed through metrics that address the conditional coverage [@angelopoulos2021gentle]. To this end, the following metrics have been implemented:\n\n- `emp_coverage` (empirical coverage)\n- `ssc` (size-stratified coverage)\n\nThere is also a simple `Plots.jl` recipe that can be used to inspect the set sizes. In the regression case, the interval width is stratified into discrete bins for this purpose:\n\n::: {.cell execution_count=10}\n``` {.julia .cell-code}\nbar(mach.model, mach.fitresult, X)\n```\n\n::: {.cell-output .cell-output-display execution_count=11}\n![](index_files/figure-commonmark/cell-11-output-1.svg){}\n:::\n:::\n\n\n## πŸ›  Contribute \n\nContributions are welcome! A good place to start is the [list](https://github.com/juliatrustworthyai/ConformalPrediction.jl/issues) of outstanding issues. For more details, see also the [Contributor's Guide](https://www.paltmeyer.com/ConformalPrediction.jl/dev/contribute/). Please follow the [SciML ColPrac guide](https://github.com/SciML/ColPrac).\n\n## πŸ™ Thanks\n\nTo build this package I have read and re-read both @angelopoulos2021gentle and @barber2021predictive. The Awesome Conformal Prediction [repository](https://github.com/valeman/awesome-conformal-prediction) [@manokhin2022awesome] has also been a fantastic place to get started. Thanks also to [\\@aangelopoulos](https://github.com/aangelopoulos), [\\@valeman](https://github.com/valeman) and others for actively contributing to discussions on here. Quite a few people have also recently started using and contributing to the package for which I am very grateful. Finally, many thanks to Anthony Blaom ([\\@ablaom](https://github.com/ablaom)) for many helpful discussions about how to interface this package to `MLJ.jl`.\n\n## πŸŽ“ References \n\n", + "markdown": "---\ntitle: ConformalPrediction\n---\n\n\n```@meta\nCurrentModule = ConformalPrediction\n```\n\n![](assets/wide_logo.png)\n\nDocumentation for [ConformalPrediction.jl](https://github.com/juliatrustworthyai/ConformalPrediction.jl).\n\n\n\n\n\n`ConformalPrediction.jl` is a package for Predictive 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/) [@blaom2020mlj]. Conformal Prediction is easy-to-understand, easy-to-use and model-agnostic and it works under minimal distributional assumptions. \n\n## πŸƒ Quick Tour\n\n> First time here? Take a quick interactive [tour](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl) to see what this package can do right on [JuliaHub](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl){target=\"_blank\"} (To run the notebook, hit login and then edit). \n\nThis [`Pluto.jl`](https://github.com/fonsp/Pluto.jl){target=\"_blank\"} 🎈 notebook won the 2nd Price in the [JuliaCon 2023 Notebook Competition](https://info.juliahub.com/pluto-notebook-winner-23).\n\n### Local Tour\n\nTo run the tour locally, just clone this repo and start `Pluto.jl` as follows:\n\n```{.julia}\n] add Pluto\nusing Pluto\nPluto.run()\n```\n\nAll notebooks are contained in `docs/pluto`.\n\n## πŸ“– Background\n\nDon't worry, we're not about to deep-dive into methodology. But just to give you a high-level description of Conformal Prediction (CP) upfront:\n\n> Conformal prediction (a.k.a. conformal inference) is a user-friendly paradigm for creating statistically rigorous uncertainty sets/intervals for the predictions of such models. Critically, the sets are valid in a distribution-free sense: they possess explicit, non-asymptotic guarantees even without distributional assumptions or model assumptions.\n>\n> --- @angelopoulos2021gentle\n\nIntuitively, CP works under the premise of turning heuristic notions of uncertainty into rigorous uncertainty estimates through repeated sampling or the use of dedicated calibration data. \n\n![Conformal Prediction in action: prediction intervals at varying coverage rates. As coverage grows, so does the width of the prediction interval.](https://raw.githubusercontent.com/pat-alt/pat-alt.github.io/main/blog/posts/conformal-regression/www/medium.gif)\n\nThe animation above is lifted from a small blog [post](https://www.paltmeyer.com/blog/posts/conformal-regression/) that introduces Conformal Prediction and this package in the context of regression. It shows how the prediction interval and the test points that it covers varies in size as the user-specified coverage rate changes.\n\n## 🚩 Installation \n\nYou can install the latest stable release from the general registry:\n\n```{.julia}\nusing Pkg\nPkg.add(\"ConformalPrediction\")\n```\n\nThe development version can be installed as follows:\n\n```{.julia}\nusing Pkg\nPkg.add(url=\"https://github.com/juliatrustworthyai/ConformalPrediction.jl\")\n```\n\n## πŸ” Usage Example \n\nTo illustrate the intended use of the package, let's have a quick look at a simple regression problem. We first generate some synthetic data and then determine indices for our training and test data using [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/):\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nusing MLJ\n\n# Inputs:\nN = 600\nxmax = 3.0\nusing Distributions\nd = Uniform(-xmax, xmax)\nX = rand(d, N)\nX = reshape(X, :, 1)\n\n# Outputs:\nnoise = 0.5\nfun(X) = sin(X)\nΞ΅ = randn(N) .* noise\ny = @.(fun(X)) + Ξ΅\ny = vec(y)\n\n# Partition:\ntrain, test = partition(eachindex(y), 0.4, 0.4, shuffle=true)\n```\n:::\n\n\nWe then import Symbolic Regressor ([`SymbolicRegression.jl`](https://github.com/MilesCranmer/SymbolicRegression.jl)) following the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) procedure.\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\nregressor = @load SRRegressor pkg=SymbolicRegression\nmodel = regressor(\n niterations=50,\n binary_operators=[+, -, *],\n unary_operators=[sin],\n)\n```\n:::\n\n\nTo 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:\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\nusing ConformalPrediction\nconf_model = conformal_model(model)\nmach = machine(conf_model, X, y)\nfit!(mach, rows=train)\n```\n:::\n\n\nPredictions 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. \n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nshow_first = 5\nXtest = selectrows(X, test)\nytest = y[test]\nyΜ‚ = predict(mach, Xtest)\nyΜ‚[1:show_first]\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\n5-element Vector{Tuple{Float64, Float64}}:\n (-0.04087262272113379, 1.8635644669554758)\n (0.04647464096907805, 1.9509117306456876)\n (-0.24248802236397216, 1.6619490673126376)\n (-0.07841928163933476, 1.8260178080372749)\n (-0.02268628324126465, 1.881750806435345)\n```\n:::\n:::\n\n\nFor simple models like this one, we can call a custom `Plots` recipe on our instance, fit result and data to generate the chart below:\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\nusing Plots\nzoom = 0\nplt = plot(mach.model, mach.fitresult, Xtest, ytest, lw=5, zoom=zoom, observed_lab=\"Test points\")\nxrange = range(-xmax+zoom,xmax-zoom,length=N)\nplot!(plt, xrange, @.(fun(xrange)), lw=2, ls=:dash, colour=:darkorange, label=\"Ground truth\")\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n![](index_files/figure-commonmark/cell-7-output-1.svg){}\n:::\n:::\n\n\n\n\nWe can evaluate the conformal model using the standard [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/) workflow with a custom performance measure. You can use either `emp_coverage` for the overall empirical coverage (correctness) or `ssc` for the size-stratified coverage rate (adaptiveness).\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\n_eval = evaluate!(mach; measure=[emp_coverage, ssc], verbosity=0)\ndisplay(_eval)\nprintln(\"Empirical coverage: $(round(_eval.measurement[1], digits=3))\")\nprintln(\"SSC: $(round(_eval.measurement[2], digits=3))\")\n```\n\n::: {.cell-output .cell-output-display}\n```\nPerformanceEvaluation object with these fields:\n model, measure, operation, measurement, per_fold,\n per_observation, fitted_params_per_fold,\n report_per_fold, train_test_rows, resampling, repeats\nExtract:\nβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€\nβ”‚ measure β”‚ operation β”‚ measurement β”‚ 1.9 β‹―\nβ”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€\nβ”‚ ConformalPrediction.emp_coverage β”‚ predict β”‚ 0.953 β”‚ 0.0 β‹―\nβ”‚ ConformalPrediction.size_stratified_coverage β”‚ predict β”‚ 0.953 β”‚ 0.0 β‹―\n└──────────────────────────────────────────────┴───────────┴─────────────┴──────\n 2 columns omitted\n```\n:::\n\n::: {.cell-output .cell-output-stdout}\n```\nEmpirical coverage: 0.953\nSSC: 0.953\n```\n:::\n:::\n\n\n## πŸ“š Read on\n\nIf after reading the usage example above you are just left with more questions about the topic, that's normal. Below we have have collected a number of further resources to help you get started with this package and the topic itself:\n\n1. Blog post introducing conformal classifiers: [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-prediction/)], [[TDS](https://medium.com/towards-data-science/conformal-prediction-in-julia-351b81309e30)], [[Forem](https://forem.julialang.org/patalt/conformal-prediction-in-julia-h9n)].\n2. Blog post applying CP to a deep learning image classifier: [[Quarto](https://www.paltmeyer.com/blog/posts/conformal-image-classifier/)], [[TDS](https://medium.com/towards-data-science/how-to-conformalize-a-deep-image-classifier-14ead4e1a5a0)], [[Forem](https://forem.julialang.org/patalt/how-to-conformalize-a-deep-image-classifier-50p2)].\n3. The package [docs](https://juliatrustworthyai.github.io/ConformalPrediction.jl/dev/) and in particular the [FAQ](https://www.paltmeyer.com/ConformalPrediction.jl/dev/faq/).\n\n### External Resources\n\n- *A Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification* by @angelopoulos2021gentle ([pdf](https://arxiv.org/pdf/2107.07511.pdf)).\n- *Predictive inference with the jackknife+* by @barber2021predictive ([pdf](https://projecteuclid.org/journals/annals-of-statistics/volume-49/issue-1/Predictive-inference-with-the-jackknife/10.1214/20-AOS1965.full))\n- *Awesome Conformal Prediction* repository by Valery Manokhin ([repo](https://github.com/valeman/awesome-conformal-prediction)).\n- [Documentation](https://mapie.readthedocs.io/en/latest/index.html) for the Python package MAPIE.\n\n## πŸ” Status \n\nThis package is in its early stages of development and therefore still subject to changes to the core architecture and API. \n\n### Implemented Methodologies\n\nThe following CP approaches have been implemented:\n\n**Regression**:\n\n- Inductive \n- Naive Transductive \n- Jackknife \n- Jackknife+ \n- Jackknife-minmax\n- CV+\n- CV-minmax\n\n**Classification**:\n\n- Inductive\n- Naive Transductive \n- Adaptive Inductive\n\nThe package has been tested for the following supervised models offered by [MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/).\n\n**Regression**:\n\n::: {.cell execution_count=9}\n``` {.julia .cell-code}\nkeys(tested_atomic_models[:regression])\n```\n\n::: {.cell-output .cell-output-display execution_count=9}\n```\nKeySet for a Dict{Symbol, Expr} with 5 entries. Keys:\n :ridge\n :lasso\n :evo_tree\n :nearest_neighbor\n :linear\n```\n:::\n:::\n\n\n**Classification**:\n\n::: {.cell execution_count=10}\n``` {.julia .cell-code}\nkeys(tested_atomic_models[:classification])\n```\n\n::: {.cell-output .cell-output-display execution_count=10}\n```\nKeySet for a Dict{Symbol, Expr} with 3 entries. Keys:\n :nearest_neighbor\n :evo_tree\n :logistic\n```\n:::\n:::\n\n\n### Implemented Evaluation Metrics\n\nTo evaluate conformal predictors we are typically interested in correctness and adaptiveness. The former can be evaluated by looking at the empirical coverage rate, while the latter can be assessed through metrics that address the conditional coverage [@angelopoulos2021gentle]. To this end, the following metrics have been implemented:\n\n- `emp_coverage` (empirical coverage)\n- `ssc` (size-stratified coverage)\n\nThere is also a simple `Plots.jl` recipe that can be used to inspect the set sizes. In the regression case, the interval width is stratified into discrete bins for this purpose:\n\n::: {.cell execution_count=11}\n``` {.julia .cell-code}\nbar(mach.model, mach.fitresult, X)\n```\n\n::: {.cell-output .cell-output-display execution_count=11}\n![](index_files/figure-commonmark/cell-12-output-1.svg){}\n:::\n:::\n\n\n## πŸ›  Contribute \n\nContributions are welcome! A good place to start is the [list](https://github.com/juliatrustworthyai/ConformalPrediction.jl/issues) of outstanding issues. For more details, see also the [Contributor's Guide](https://www.paltmeyer.com/ConformalPrediction.jl/dev/contribute/). Please follow the [SciML ColPrac guide](https://github.com/SciML/ColPrac).\n\n## πŸ™ Thanks\n\nTo build this package I have read and re-read both @angelopoulos2021gentle and @barber2021predictive. The Awesome Conformal Prediction [repository](https://github.com/valeman/awesome-conformal-prediction) [@manokhin2022awesome] has also been a fantastic place to get started. Thanks also to [\\@aangelopoulos](https://github.com/aangelopoulos), [\\@valeman](https://github.com/valeman) and others for actively contributing to discussions on here. Quite a few people have also recently started using and contributing to the package for which I am very grateful. Finally, many thanks to Anthony Blaom ([\\@ablaom](https://github.com/ablaom)) for many helpful discussions about how to interface this package to `MLJ.jl`.\n\n## πŸŽ“ References \n\n", "supporting": [ "index_files/figure-commonmark" ], diff --git a/_freeze/docs/src/index/figure-commonmark/cell-12-output-1.svg b/_freeze/docs/src/index/figure-commonmark/cell-12-output-1.svg new file mode 100644 index 0000000..c459847 --- /dev/null +++ b/_freeze/docs/src/index/figure-commonmark/cell-12-output-1.svg @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/docs/src/index/figure-commonmark/cell-7-output-1.svg b/_freeze/docs/src/index/figure-commonmark/cell-7-output-1.svg index ea41553..5463e11 100644 --- a/_freeze/docs/src/index/figure-commonmark/cell-7-output-1.svg +++ b/_freeze/docs/src/index/figure-commonmark/cell-7-output-1.svg @@ -1,294 +1,285 @@ - + - + - + - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/src/index.md b/docs/src/index.md index b37fdc1..9a4b7ce 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -12,9 +12,9 @@ Documentation for [ConformalPrediction.jl](https://github.com/juliatrustworthyai ## πŸƒ Quick Tour -> First time here? Take a quick interactive [tour](https://binder.plutojl.org/v0.19.12/open?url=https%253A%252F%252Fraw.githubusercontent.com%252Fpat-alt%252FConformalPrediction.jl%252Fmain%252Fdocs%252Fpluto%252Fintro.jl) to see what this package can do: [![Binder](https://mybinder.org/badge_logo.svg)](https://binder.plutojl.org/v0.19.12/open?url=https%253A%252F%252Fraw.githubusercontent.com%252Fpat-alt%252FConformalPrediction.jl%252Fmain%252Fdocs%252Fpluto%252Fintro.jl) +> First time here? Take a quick interactive [tour](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl) to see what this package can do right on [JuliaHub](https://juliahub.com/ui/Notebooks/juliahub/Tutorials/ConformalPrediction.jl) (To run the notebook, hit login and then edit). -The button takes you to a [`Pluto.jl`](https://github.com/fonsp/Pluto.jl) 🎈 notebook hosted on [binder](https://mybinder.org/). In my own experience, this may take some time to load, certainly long enough to get yourself a hot beverage β˜•. Alternatively, you can run the notebook locally or skip the tour for now and read on below. +This [`Pluto.jl`](https://github.com/fonsp/Pluto.jl) 🎈 notebook won the 2nd Price in the [JuliaCon 2023 Notebook Competition](https://info.juliahub.com/pluto-notebook-winner-23). ### Local Tour @@ -75,7 +75,7 @@ X = reshape(X, :, 1) # Outputs: noise = 0.5 -fun(X) = X * sin(X) +fun(X) = sin(X) Ξ΅ = randn(N) .* noise y = @.(fun(X)) + Ξ΅ y = vec(y) @@ -115,11 +115,11 @@ yΜ‚[1:show_first] ``` 5-element Vector{Tuple{Float64, Float64}}: - (-0.40997718991694765, 1.449009293726001) - (0.8484810430118421, 2.7074675266547907) - (0.547852151594671, 2.4068386352376194) - (-0.022697652913589494, 1.8362888307293592) - (0.07435130847990101, 1.9333377921228496) + (-0.04087262272113379, 1.8635644669554758) + (0.04647464096907805, 1.9509117306456876) + (-0.24248802236397216, 1.6619490673126376) + (-0.07841928163933476, 1.8260178080372749) + (-0.02268628324126465, 1.881750806435345) For simple models like this one, we can call a custom `Plots` recipe on our instance, fit result and data to generate the chart below: @@ -142,21 +142,22 @@ println("Empirical coverage: $(round(_eval.measurement[1], digits=3))") println("SSC: $(round(_eval.measurement[2], digits=3))") ``` - Started! - PerformanceEvaluation object with these fields: - measure, operation, measurement, per_fold, + model, measure, operation, measurement, per_fold, per_observation, fitted_params_per_fold, - report_per_fold, train_test_rows + report_per_fold, train_test_rows, resampling, repeats Extract: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€ β”‚ measure β”‚ operation β”‚ measurement β”‚ 1.9 β‹― β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€ - β”‚ ConformalPrediction.emp_coverage β”‚ predict β”‚ 0.945 β”‚ 0.0 β‹― - β”‚ ConformalPrediction.size_stratified_coverage β”‚ predict β”‚ 0.945 β”‚ 0.0 β‹― + β”‚ ConformalPrediction.emp_coverage β”‚ predict β”‚ 0.953 β”‚ 0.0 β‹― + β”‚ ConformalPrediction.size_stratified_coverage β”‚ predict β”‚ 0.953 β”‚ 0.0 β‹― └──────────────────────────────────────────────┴───────────┴─────────────┴────── 2 columns omitted + Empirical coverage: 0.953 + SSC: 0.953 + ## πŸ“š Read on If after reading the usage example above you are just left with more questions about the topic, that’s normal. Below we have have collected a number of further resources to help you get started with this package and the topic itself: @@ -235,7 +236,7 @@ There is also a simple `Plots.jl` recipe that can be used to inspect the set siz bar(mach.model, mach.fitresult, X) ``` -![](index_files/figure-commonmark/cell-11-output-1.svg) +![](index_files/figure-commonmark/cell-12-output-1.svg) ## πŸ›  Contribute diff --git a/docs/src/index_files/figure-commonmark/cell-12-output-1.svg b/docs/src/index_files/figure-commonmark/cell-12-output-1.svg new file mode 100644 index 0000000..c459847 --- /dev/null +++ b/docs/src/index_files/figure-commonmark/cell-12-output-1.svg @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/src/index_files/figure-commonmark/cell-7-output-1.svg b/docs/src/index_files/figure-commonmark/cell-7-output-1.svg index ea41553..5463e11 100644 --- a/docs/src/index_files/figure-commonmark/cell-7-output-1.svg +++ b/docs/src/index_files/figure-commonmark/cell-7-output-1.svg @@ -1,294 +1,285 @@ - + - + - + - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +