Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Weighted quantiles, nonexchangeable conformal sets, jackknife+ intervals and swap distances

Definition
ConformalBeyondExchangeability_Basic

by Xeroth · Nov 24, 2026 · Mathlib 0df444a (Lean v4.33.1)

conformal-predictioncoverage-guaranteesdistribution-free-inferencejackknife-plustotal-variation

Objects of Barber, Candès, Ramdas and Tibshirani (2023), Sections 2-4 and Appendices B, C, E.

  1. Data. The data are Z=(Z1,…,Zn+1)Z=(Z_1,\dots,Z_{n+1})Z=(Z1​,…,Zn+1​) with Zi=(Xi,Yi)∈X×RZ_i=(X_i,Y_i)\in\mathcal X\times\mathbb RZi​=(Xi​,Yi​)∈X×R; points 1,…,n1,\dots,n1,…,n are the training data and Zn+1Z_{n+1}Zn+1​ is the test point. Their joint law PPP on (X×R)n+1(\mathcal X\times\mathbb R)^{n+1}(X×R)n+1 is arbitrary. For k∈[n+1]k\in[n+1]k∈[n+1], πk\pi_kπk​ is the transposition of kkk and n+1n+1n+1 (πn+1=id\pi_{n+1}=\mathrm{id}πn+1​=id), and Zk=(Zπk(1),…,Zπk(n+1))Z^k=(Z_{\pi_k(1)},\dots,Z_{\pi_k(n+1)})Zk=(Zπk​(1)​,…,Zπk​(n+1)​) is ZZZ with the test point and point kkk swapped (eq. (2)). For y∈Ry\in\mathbb Ry∈R, Yiy=YiY^y_i=Y_iYiy​=Yi​ for i≤ni\le ni≤n and Yn+1y=yY^y_{n+1}=yYn+1y​=y.
  2. Weighted quantiles. For masses pip_ipi​ at points vi∈[−∞,+∞]v_i\in[-\infty,+\infty]vi​∈[−∞,+∞], Qτ(∑ipiδvi)=inf⁡{q:∑i:vi≤qpi≥τ}\mathsf Q_\tau(\sum_ip_i\delta_{v_i})=\inf\{q:\sum_{i:v_i\le q}p_i\ge\tau\}Qτ​(∑i​pi​δvi​​)=inf{q:∑i:vi​≤q​pi​≥τ} is the smallest τ\tauτ-quantile (footnote 1). In the jackknife+ lower endpoint, Qα\mathsf Q_\alphaQα​ is the largest α\alphaα-quantile, sup⁡{q:∑i:vi<qpi≤α}\sup\{q:\sum_{i:v_i<q}p_i\le\alpha\}sup{q:∑i:vi​<q​pi​≤α} (footnote 7).
  3. Weights. Fixed weights w1,…,wn∈[0,1]w_1,\dots,w_n\in[0,1]w1​,…,wn​∈[0,1] give w~i=wi/(w1+⋯+wn+1)\tilde w_i=w_i/(w_1+\dots+w_n+1)w~i​=wi​/(w1​+⋯+wn​+1) for i≤ni\le ni≤n and w~n+1=1/(w1+⋯+wn+1)\tilde w_{n+1}=1/(w_1+\dots+w_n+1)w~n+1​=1/(w1​+⋯+wn​+1) (eq. (10)). The random index K∼∑iw~iδiK\sim\sum_i\tilde w_i\delta_iK∼∑i​w~i​δi​ is drawn independently of ZZZ (eq. (17)), so an event EEE has probability ∑kw~k P{Z∈Ek}\sum_k\tilde w_k\,P\{Z\in E_k\}∑k​w~k​P{Z∈Ek​}, where EkE_kEk​ is the event for K=kK=kK=k.
  4. Algorithms. A tagged algorithm A\mathcal AA maps any finite sequence of tagged points (xi,yi,ti)∈X×R×T(x_i,y_i,t_i)\in\mathcal X\times\mathbb R\times\mathcal T(xi​,yi​,ti​)∈X×R×T to a function μ^:X→R\hat\mu:\mathcal X\to\mathbb Rμ^​:X→R (eq. (16)); the tags t1,…,tn+1t_1,\dots,t_{n+1}t1​,…,tn+1​ are fixed. An untagged algorithm acts on sequences of points (xi,yi)(x_i,y_i)(xi​,yi​). It is symmetric if permuting its input does not change its output (eq. (6)).
  5. Residuals and the coverage-gap term. R(z)i=∣yi−μ^(xi)∣R(z)_i=|y_i-\hat\mu(x_i)|R(z)i​=∣yi​−μ^​(xi​)∣ with μ^=A((xi,yi,ti):i∈[n+1])\hat\mu=\mathcal A((x_i,y_i,t_i):i\in[n+1])μ^​=A((xi​,yi​,ti​):i∈[n+1]) (Section 4). For split conformal, R(z)i=∣yi−μ^(xi)∣R(z)_i=|y_i-\hat\mu(x_i)|R(z)i​=∣yi​−μ^​(xi​)∣ for the pre-fitted μ^\hat\muμ^​. The term ∑i=1nw~i dTV(R(Z),R(Zi))\sum_{i=1}^n\tilde w_i\,\mathsf d_{\mathsf{TV}}(R(Z),R(Z^i))∑i=1n​w~i​dTV​(R(Z),R(Zi)) uses the total variation distance dTV(μ,ν)=sup⁡A∣μ(A)−ν(A)∣\mathsf d_{\mathsf{TV}}(\mu,\nu)=\sup_A|\mu(A)-\nu(A)|dTV​(μ,ν)=supA​∣μ(A)−ν(A)∣ between the laws of R(Z)R(Z)R(Z) and R(Zi)R(Z^i)R(Zi).
  6. Conformal sets. Split conformal (eqs. (5), (11)) is C^n(Xn+1)={y:∣y−μ^(Xn+1)∣≤Q1−α(∑i=1nw~iδRi+w~n+1δ+∞)}\hat C_n(X_{n+1})=\{y:|y-\hat\mu(X_{n+1})|\le\mathsf Q_{1-\alpha}(\sum_{i=1}^n\tilde w_i\delta_{R_i}+\tilde w_{n+1}\delta_{+\infty})\}C^n​(Xn+1​)={y:∣y−μ^​(Xn+1​)∣≤Q1−α​(∑i=1n​w~i​δRi​​+w~n+1​δ+∞​)}. Nonexchangeable full conformal (eq. (18)) is {y:Rn+1y,K≤Q1−α(∑i=1n+1w~iδRiy,K)}\{y:R^{y,K}_{n+1}\le\mathsf Q_{1-\alpha}(\sum_{i=1}^{n+1}\tilde w_i\delta_{R^{y,K}_i})\}{y:Rn+1y,K​≤Q1−α​(∑i=1n+1​w~i​δRiy,K​​)}, where Riy,k=∣Yiy−μ^y,k(Xi)∣R^{y,k}_i=|Y^y_i-\hat\mu^{y,k}(X_i)|Riy,k​=∣Yiy​−μ^​y,k(Xi​)∣ and μ^y,k=A((Xπk(i),Yπk(i)y,ti):i∈[n+1])\hat\mu^{y,k}=\mathcal A((X_{\pi_k(i)},Y^y_{\pi_k(i)},t_i):i\in[n+1])μ^​y,k=A((Xπk​(i)​,Yπk​(i)y​,ti​):i∈[n+1]). For a symmetric algorithm, eq. (12) is the same set with μ^y=A((X1,Y1),…,(Xn,Yn),(Xn+1,y))\hat\mu^y=\mathcal A((X_1,Y_1),\dots,(X_n,Y_n),(X_{n+1},y))μ^​y=A((X1​,Y1​),…,(Xn​,Yn​),(Xn+1​,y)); with w≡1w\equiv1w≡1 it is full conformal (eq. (8)).
  7. Jackknife+. μ^−ij=A((xk,yk,tk):k∈[n+1]∖{i,j})\hat\mu_{-ij}=\mathcal A((x_k,y_k,t_k):k\in[n+1]\setminus\{i,j\})μ^​−ij​=A((xk​,yk​,tk​):k∈[n+1]∖{i,j}), and Rjack+(z)R_{\mathrm{jack+}}(z)Rjack+​(z) is the matrix with entries ∣yi−μ^−ij(xi)∣|y_i-\hat\mu_{-ij}(x_i)|∣yi​−μ^​−ij​(xi​)∣ for i≠ji\neq ji=j and zero diagonal. μ^−ik=A((Xπk(j),Yπk(j),tj):πk(j)∉{i,n+1})\hat\mu^k_{-i}=\mathcal A((X_{\pi_k(j)},Y_{\pi_k(j)},t_j):\pi_k(j)\notin\{i,n+1\})μ^​−ik​=A((Xπk​(j)​,Yπk​(j)​,tj​):πk​(j)∈/{i,n+1}) and Rik,LOO=∣Yi−μ^−ik(Xi)∣R^{k,\mathrm{LOO}}_i=|Y_i-\hat\mu^k_{-i}(X_i)|Rik,LOO​=∣Yi​−μ^​−ik​(Xi​)∣. The interval (eq. (34)) is [Qα(∑i≤nw~iδμ^−iK(Xn+1)−RiK,LOO+w~n+1δ−∞), Q1−α(∑i≤nw~iδμ^−iK(Xn+1)+RiK,LOO+w~n+1δ+∞)][\mathsf Q_\alpha(\sum_{i\le n}\tilde w_i\delta_{\hat\mu^K_{-i}(X_{n+1})-R^{K,\mathrm{LOO}}_i}+\tilde w_{n+1}\delta_{-\infty}),\ \mathsf Q_{1-\alpha}(\sum_{i\le n}\tilde w_i\delta_{\hat\mu^K_{-i}(X_{n+1})+R^{K,\mathrm{LOO}}_i}+\tilde w_{n+1}\delta_{+\infty})][Qα​(∑i≤n​w~i​δμ^​−iK​(Xn+1​)−RiK,LOO​​+w~n+1​δ−∞​), Q1−α​(∑i≤n​w~i​δμ^​−iK​(Xn+1​)+RiK,LOO​​+w~n+1​δ+∞​)]. Eq. (33) is the symmetric version, with leave-one-out models μ^−i\hat\mu_{-i}μ^​−i​ fitted on (Xj,Yj)(X_j,Y_j)(Xj​,Yj​), j∈[n]∖{i}j\in[n]\setminus\{i\}j∈[n]∖{i}.
  8. Strange points. S(r)={i:ri>Q1−α(∑jpjδrj)}\mathcal S(r)=\{i:r_i>\mathsf Q_{1-\alpha}(\sum_jp_j\delta_{r_j})\}S(r)={i:ri​>Q1−α​(∑j​pj​δrj​​)} for r∈Rn+1r\in\mathbb R^{n+1}r∈Rn+1 (eq. (24)), and S(r)={i:∑jpj1{rij>rji}≥1−α}\mathcal S(r)=\{i:\sum_jp_j\mathbb 1\{r_{ij}>r_{ji}\}\ge1-\alpha\}S(r)={i:∑j​pj​1{rij​>rji​}≥1−α} for a matrix rrr (eq. (39)).
  9. Contamination. dmix(D,D′)=inf⁡{t≥0:D=(1−t)D′+tD′′ for a distribution D′′}\mathsf d_{\mathrm{mix}}(D,D')=\inf\{t\ge0:D=(1-t)D'+tD''\text{ for a distribution }D''\}dmix​(D,D′)=inf{t≥0:D=(1−t)D′+tD′′ for a distribution D′′} (eq. (36)). With wˉi=wi/(w1+⋯+wn)\bar w_i=w_i/(w_1+\dots+w_n)wˉi​=wi​/(w1​+⋯+wn​), the contamination term is ∑i=1nwˉi dmix(Zi,Zn+1)\sum_{i=1}^n\bar w_i\,\mathsf d_{\mathrm{mix}}(Z_i,Z_{n+1})∑i=1n​wˉi​dmix​(Zi​,Zn+1​). E[f(B)]\mathbb E[f(B)]E[f(B)] for independent Bi∼Bernoulli(pi)B_i\sim\mathrm{Bernoulli}(p_i)Bi​∼Bernoulli(pi​) is the finite sum ∑b∈{0,1}n∏ipibi(1−pi)1−bif(b)\sum_{b\in\{0,1\}^n}\prod_ip_i^{b_i}(1-p_i)^{1-b_i}f(b)∑b∈{0,1}n​∏i​pibi​​(1−pi​)1−bi​f(b).

Formalization Note Indices run over Fin(n+1)\mathrm{Fin}(n+1)Fin(n+1), with the last index the test point. Algorithms take Lean lists, which are finite sequences of any length. Quantiles take values in the extended reals [−∞,+∞][-\infty,+\infty][−∞,+∞], so the infimum of an empty set is +∞+\infty+∞ and the atoms at ±∞\pm\infty±∞ are represented exactly. The probability over the data and the independent index KKK is written as the finite sum ∑kw~k P{⋅}\sum_k\tilde w_k\,P\{\cdot\}∑k​w~k​P{⋅}. Total variation is a real supremum over measurable sets. dmix\mathsf d_{\mathrm{mix}}dmix​ restricts ttt to [0,1][0,1][0,1], the only values for which (1−t)D′(1-t)D'(1−t)D′ is a measure.

Definition code
import Mathlib

/-!
Barber, Candès, Ramdas and Tibshirani, *Conformal prediction beyond exchangeability*,
arXiv:2202.13415v5 (2023): weighted quantiles (Section 2, footnote 1; Appendix B.1, footnote 7),
normalized weights (eq. (10)), the swapped data sequence `Z^i` (eq. (2)), the total variation
distance, split and full conformal prediction (eqs. (5), (8), (11), (12)), tagged nonsymmetric
algorithms and nonexchangeable full conformal prediction with the random swap index `K`
(eqs. (16)-(18)), the residual map `R` (Section 4), the strange-point sets (eqs. (24), (39)),
the jackknife+ (Appendix B, eqs. (33)-(34)), the contamination distance `d_mix` (eq. (36)) and
expectations over independent Bernoulli variables (Lemma 3).

Data points are `Z_i = (X_i, Y_i) ∈ X × ℝ`, indexed by `Fin (n + 1)`; index `Fin.last n` is the
test point `n + 1`, and `Fin.castSucc i` (for `i : Fin n`) is the training point `i + 1`.
-/

open MeasureTheory

noncomputable section

namespace ConformalBeyondExchangeability

/-- Section 2, eq. (5) and footnote 1: `Q_τ(∑_i p_i δ_{v_i})`, the smallest `τ`-quantile of the
discrete distribution with masses `p_i` at the points `v_i ∈ [-∞, +∞]`:
`inf {q : ∑_{i : v_i ≤ q} p_i ≥ τ}` (the infimum of the empty set is `+∞`). -/
def wQuantile {m : ℕ} (p : Fin m → ℝ) (v : Fin m → EReal) (τ : ℝ) : EReal :=
  sInf {q : EReal | τ ≤ ∑ i, if v i ≤ q then p i else 0}

/-- Appendix B.1, footnote 7: `Q_τ(∑_i p_i δ_{v_i})` in the lower endpoint of the jackknife+
interval, the largest `τ`-quantile: `sup {q : ∑_{i : v_i < q} p_i ≤ τ}`. -/
def wQuantileUpper {m : ℕ} (p : Fin m → ℝ) (v : Fin m → EReal) (τ : ℝ) : EReal :=
  sSup {q : EReal | (∑ i, if v i < q then p i else 0) ≤ τ}

/-- Eq. (10): the normalized weights `w̃_i = w_i / (w_1 + ⋯ + w_n + 1)` for `i ≤ n` and
`w̃_{n+1} = 1 / (w_1 + ⋯ + w_n + 1)`. -/
def normWeights {n : ℕ} (w : Fin n → ℝ) : Fin (n + 1) → ℝ :=
  fun i => (if h : (i : ℕ) < n then w ⟨i, h⟩ else 1) / (∑ j, w j + 1)

/-- Eq. (3) and Lemma 1: the total variation distance
`d_TV(μ, ν) = sup_A |μ(A) - ν(A)|` over measurable sets `A`. -/
def tvDist {Ω : Type*} [MeasurableSpace Ω] (μ ν : Measure Ω) : ℝ :=
  sSup {r | ∃ A : Set Ω, MeasurableSet A ∧ r = |μ.real A - ν.real A|}

/-- Section 3.2: `π_k`, the permutation of `[n+1]` swapping `k` and `n + 1` (the identity when
`k = n + 1`). -/
def swapIdx {n : ℕ} (k : Fin (n + 1)) : Equiv.Perm (Fin (n + 1)) :=
  Equiv.swap k (Fin.last n)

/-- Eq. (2): `Z^k = (Z_1, …, Z_{k-1}, Z_{n+1}, Z_{k+1}, …, Z_n, Z_k)`, the data sequence with
the test point and the `k`-th point swapped, i.e. `(Z^k)_i = Z_{π_k(i)}`. -/
def swapData {E : Type*} {n : ℕ} (k : Fin (n + 1)) (z : Fin (n + 1) → E) : Fin (n + 1) → E :=
  fun i => z (swapIdx k i)

/-- Section 3.2: the data `(X_1, Y^y_1), …, (X_{n+1}, Y^y_{n+1})` with `Y^y_i = Y_i` for `i ≤ n`
and `Y^y_{n+1} = y`, i.e. the training data together with the hypothesized test point
`(X_{n+1}, y)`. -/
def withTest {X : Type*} {n : ℕ} (z : Fin (n + 1) → X × ℝ) (y : ℝ) : Fin (n + 1) → X × ℝ :=
  Function.update z (Fin.last n) ((z (Fin.last n)).1, y)

/-- Eq. (16): an algorithm `A : ∪_{m ≥ 0} (X × ℝ × T)^m → {μ̂ : X → ℝ}` mapping a finite
sequence of tagged data points `(X_i, Y_i, t_i)` to a fitted regression function. -/
abbrev TaggedAlg (X T : Type*) : Type _ := List (X × ℝ × T) → X → ℝ

/-- Section 2, full conformal prediction: an algorithm `A : ∪_{m ≥ 0} (X × ℝ)^m → {μ̂ : X → ℝ}`
on untagged data points `(X_i, Y_i)`. -/
abbrev Alg (X : Type*) : Type _ := List (X × ℝ) → X → ℝ

/-- Eq. (6): the algorithm treats the data points symmetrically,
`A((x_{π(1)}, y_{π(1)}), …, (x_{π(m)}, y_{π(m)})) = A((x_1, y_1), …, (x_m, y_m))` for every
`m` and every permutation `π`. -/
def IsSymmetric {X : Type*} (alg : Alg X) : Prop :=
  ∀ l l' : List (X × ℝ), l.Perm l' → alg l = alg l'

/-- Theorem 1: the data points `Z_1, …, Z_{n+1}` are exchangeable: their joint law `P` is
invariant under every permutation of the indices. -/
def IsExchangeable {E : Type*} [MeasurableSpace E] {m : ℕ} (P : Measure (Fin m → E)) : Prop :=
  ∀ σ : Equiv.Perm (Fin m), P.map (fun z i => z (σ i)) = P

/-- Section 3.2: the tagged sequence `((x_1, y_1, t_1), …, (x_{n+1}, y_{n+1}, t_{n+1}))`. -/
def tagged {X T : Type*} {n : ℕ} (t : Fin (n + 1) → T) (z : Fin (n + 1) → X × ℝ) :
    List (X × ℝ × T) :=
  List.ofFn fun i => ((z i).1, (z i).2, t i)

/-- Section 4: the residual vector `R(z)`, `R(z)_i = |y_i - μ̂(x_i)|` with
`μ̂ = A((x_i, y_i, t_i) : i ∈ [n+1])`. -/
def residuals {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (z : Fin (n + 1) → X × ℝ) : Fin (n + 1) → ℝ :=
  fun i => |(z i).2 - alg (tagged t z) (z i).1|

/-- Section 3.2: the residuals `R^{y,k}_i = |Y^y_i - μ̂^{y,k}(X_i)|` of the model
`μ̂^{y,k} = A((X_{π_k(i)}, Y^y_{π_k(i)}, t_i) : i ∈ [n+1])`. -/
def swapResiduals {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (z : Fin (n + 1) → X × ℝ) (y : ℝ) (k : Fin (n + 1)) : Fin (n + 1) → ℝ :=
  fun i => |(withTest z y i).2 - alg (tagged t (swapData k (withTest z y))) (withTest z y i).1|

/-- Eq. (18): the nonexchangeable full conformal prediction set for the value `K = k` of the
random index, `{y : R^{y,k}_{n+1} ≤ Q_{1-α}(∑_{i=1}^{n+1} w̃_i δ_{R^{y,k}_i})}`. It depends on
`z` only through `(X_1, Y_1), …, (X_n, Y_n), X_{n+1}`. -/
def fullSet {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (w : Fin n → ℝ) (α : ℝ) (z : Fin (n + 1) → X × ℝ) (k : Fin (n + 1)) : Set ℝ :=
  {y | (swapResiduals alg t z y k (Fin.last n) : EReal) ≤
    wQuantile (normWeights w) (fun i => (swapResiduals alg t z y k i : EReal)) (1 - α)}

/-- Eq. (17): the probability of an event depending on the data `Z ∼ P` and on the random index
`K ∼ ∑_i w̃_i δ_i`, drawn independently of `Z`: `P{E} = ∑_k w̃_k P{Z ∈ E_k}`, where `E_k` is the
event for `K = k`. -/
def randIndexProb {Ω : Type*} [MeasurableSpace Ω] {n : ℕ} (P : Measure Ω) (w : Fin n → ℝ)
    (E : Fin (n + 1) → Set Ω) : ℝ :=
  ∑ k, normWeights w k * P.real (E k)

/-- Theorem 2: the coverage probability `P{Y_{n+1} ∈ Ĉ_n(X_{n+1})}` of nonexchangeable full
conformal prediction (eq. (18)), over the data `Z ∼ P` and the independent index `K`. -/
def fullCoverage {X T : Type*} [MeasurableSpace X] {n : ℕ} (P : Measure (Fin (n + 1) → X × ℝ))
    (alg : TaggedAlg X T) (t : Fin (n + 1) → T) (w : Fin n → ℝ) (α : ℝ) : ℝ :=
  randIndexProb P w (fun k => {z | (z (Fin.last n)).2 ∈ fullSet alg t w α z k})

/-- Section 2 and eq. (11): the residuals `|Y_i - μ̂(X_i)|`, `i = 1, …, n+1`, of a pre-fitted
model `μ̂` (the residual vector `R(Z)` of split conformal prediction). -/
def splitResiduals {X : Type*} {n : ℕ} (μ : X → ℝ) (z : Fin (n + 1) → X × ℝ) :
    Fin (n + 1) → ℝ :=
  fun i => |(z i).2 - μ (z i).1|

/-- Eq. (11): the nonexchangeable split conformal interval
`μ̂(X_{n+1}) ± Q_{1-α}(∑_{i=1}^n w̃_i δ_{R_i} + w̃_{n+1} δ_{+∞})`, written as the set of `y` with
`|y - μ̂(X_{n+1})| ≤ Q_{1-α}(…)`. With `w_1 = ⋯ = w_n = 1` it is the split conformal interval
of eq. (5). -/
def splitSet {X : Type*} {n : ℕ} (μ : X → ℝ) (w : Fin n → ℝ) (α : ℝ)
    (z : Fin (n + 1) → X × ℝ) : Set ℝ :=
  {y | ((|y - μ (z (Fin.last n)).1| : ℝ) : EReal) ≤
    wQuantile (normWeights w)
      (fun i => if i = Fin.last n then ⊤ else (splitResiduals μ z i : EReal)) (1 - α)}

/-- Theorems 1-3: the coverage probability `P{Y_{n+1} ∈ Ĉ_n(X_{n+1})}` of split conformal
prediction (eq. (11)). -/
def splitCoverage {X : Type*} [MeasurableSpace X] {n : ℕ} (P : Measure (Fin (n + 1) → X × ℝ))
    (μ : X → ℝ) (w : Fin n → ℝ) (α : ℝ) : ℝ :=
  P.real {z | (z (Fin.last n)).2 ∈ splitSet μ w α z}

/-- Theorems 2, 3 and 5: the coverage-gap term `∑_{i=1}^n w̃_i · d_TV(R(Z), R(Z^i))` for a
map `R` of the data sequence (a residual vector or matrix), where `Z ∼ P`. -/
def swapGap {X E : Type*} [MeasurableSpace X] [MeasurableSpace E] {n : ℕ}
    (P : Measure (Fin (n + 1) → X × ℝ)) (w : Fin n → ℝ) (R : (Fin (n + 1) → X × ℝ) → E) : ℝ :=
  ∑ i : Fin n, normWeights w i.castSucc *
    tvDist (P.map R) (P.map fun z => R (swapData i.castSucc z))

/-- Eq. (24): the strange points of `r ∈ ℝ^{n+1}`,
`S(r) = {i : r_i > Q_{1-α}(∑_{j=1}^{n+1} p_j δ_{r_j})}` (with `p = w̃`). -/
def strangeSet {m : ℕ} (p : Fin m → ℝ) (α : ℝ) (r : Fin m → ℝ) : Finset (Fin m) :=
  Finset.univ.filter fun i => wQuantile p (fun j => (r j : EReal)) (1 - α) < (r i : EReal)

/-- Section 2, eqs. (7)-(8) and Section 6.1: the residual vector of a symmetric algorithm,
`R(z)_i = |y_i - μ̂(x_i)|` with `μ̂ = A((x_1, y_1), …, (x_{n+1}, y_{n+1}))`. -/
def symResiduals {X : Type*} {n : ℕ} (alg : Alg X) (z : Fin (n + 1) → X × ℝ) :
    Fin (n + 1) → ℝ :=
  fun i => |(z i).2 - alg (List.ofFn z) (z i).1|

/-- Eq. (12): the nonexchangeable full conformal set for a symmetric algorithm,
`{y : R^y_{n+1} ≤ Q_{1-α}(∑_{i=1}^{n+1} w̃_i δ_{R^y_i})}`, with `R^y` the residuals (eq. (7)) of
`μ̂^y = A((X_1, Y_1), …, (X_n, Y_n), (X_{n+1}, y))`. With `w_1 = ⋯ = w_n = 1` it is the full
conformal set of eq. (8). -/
def symFullSet {X : Type*} {n : ℕ} (alg : Alg X) (w : Fin n → ℝ) (α : ℝ)
    (z : Fin (n + 1) → X × ℝ) : Set ℝ :=
  {y | (symResiduals alg (withTest z y) (Fin.last n) : EReal) ≤
    wQuantile (normWeights w) (fun i => (symResiduals alg (withTest z y) i : EReal)) (1 - α)}

/-- Appendix B.3: the leave-two-out model
`μ̂_{-ij} = A((x_k, y_k, t_k) : k ∈ [n+1] \ {i, j})`, the remaining points kept in order. -/
def ltoModel {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (z : Fin (n + 1) → X × ℝ) (i j : Fin (n + 1)) : X → ℝ :=
  alg (((List.finRange (n + 1)).filter fun k => decide (k ≠ i ∧ k ≠ j)).map
    fun k => ((z k).1, (z k).2, t k))

/-- Appendix B.3: the residual matrix `R_jack+(z)`, with entries `|y_i - μ̂_{-ij}(x_i)|` for
`i ≠ j` and zeros on the diagonal. -/
def jackResiduals {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (z : Fin (n + 1) → X × ℝ) : Fin (n + 1) → Fin (n + 1) → ℝ :=
  fun i j => if i = j then 0 else |(z i).2 - ltoModel alg t z i j (z i).1|

/-- Appendix B.2: the model `μ̂^k_{-i} = A((X_{π_k(j)}, Y_{π_k(j)}, t_j) : j ∈ [n+1],
π_k(j) ∉ {i, n+1})`, fitted on the training data without point `i`, with point `k` given the
tag `t_{n+1}`. -/
def looSwapModel {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (z : Fin (n + 1) → X × ℝ) (k i : Fin (n + 1)) : X → ℝ :=
  alg (((List.finRange (n + 1)).filter
      fun j => decide (swapIdx k j ≠ i ∧ swapIdx k j ≠ Fin.last n)).map
    fun j => ((z (swapIdx k j)).1, (z (swapIdx k j)).2, t j))

/-- Appendix B.2: the leave-one-out residual `R^{k,LOO}_i = |Y_i - μ̂^k_{-i}(X_i)|`. -/
def looSwapResidual {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (z : Fin (n + 1) → X × ℝ) (k i : Fin (n + 1)) : ℝ :=
  |(z i).2 - looSwapModel alg t z k i (z i).1|

/-- Eq. (34): the nonexchangeable jackknife+ interval for the value `K = k` of the random index,
`[Q_α(∑_{i=1}^n w̃_i δ_{μ̂^k_{-i}(X_{n+1}) - R^{k,LOO}_i} + w̃_{n+1} δ_{-∞}),
Q_{1-α}(∑_{i=1}^n w̃_i δ_{μ̂^k_{-i}(X_{n+1}) + R^{k,LOO}_i} + w̃_{n+1} δ_{+∞})]`, with the largest
`α`-quantile at the left end and the smallest `(1-α)`-quantile at the right end. -/
def jackInterval {X T : Type*} {n : ℕ} (alg : TaggedAlg X T) (t : Fin (n + 1) → T)
    (w : Fin n → ℝ) (α : ℝ) (z : Fin (n + 1) → X × ℝ) (k : Fin (n + 1)) : Set ℝ :=
  {y | wQuantileUpper (normWeights w)
        (fun i => if i = Fin.last n then ⊥ else
          ((looSwapModel alg t z k i (z (Fin.last n)).1 - looSwapResidual alg t z k i : ℝ) :
            EReal)) α ≤ (y : EReal) ∧
      (y : EReal) ≤ wQuantile (normWeights w)
        (fun i => if i = Fin.last n then ⊤ else
          ((looSwapModel alg t z k i (z (Fin.last n)).1 + looSwapResidual alg t z k i : ℝ) :
            EReal)) (1 - α)}

/-- Theorem 5: the coverage probability `P{Y_{n+1} ∈ Ĉ_n(X_{n+1})}` of the nonexchangeable
jackknife+ (eq. (34)), over the data `Z ∼ P` and the independent index `K`. -/
def jackCoverage {X T : Type*} [MeasurableSpace X] {n : ℕ} (P : Measure (Fin (n + 1) → X × ℝ))
    (alg : TaggedAlg X T) (t : Fin (n + 1) → T) (w : Fin n → ℝ) (α : ℝ) : ℝ :=
  randIndexProb P w (fun k => {z | (z (Fin.last n)).2 ∈ jackInterval alg t w α z k})

/-- Eq. (39): the weighted strange points of a matrix `r`,
`S(r) = {i : ∑_j p_j 1{r_ij > r_ji} ≥ 1 - α}`. -/
def tournamentStrangeSet {m : ℕ} (p : Fin m → ℝ) (α : ℝ) (r : Fin m → Fin m → ℝ) :
    Finset (Fin m) :=
  Finset.univ.filter fun i => 1 - α ≤ ∑ j, if r j i < r i j then p j else 0

/-- Eq. (31): the leave-one-out model `μ̂_{-i} = A((X_j, Y_j) : j ∈ [n] \ {i})` of a symmetric
algorithm, fitted on the training data without point `i`. -/
def looModel {X : Type*} {n : ℕ} (alg : Alg X) (z : Fin (n + 1) → X × ℝ) (i : Fin (n + 1)) :
    X → ℝ :=
  alg (((List.finRange (n + 1)).filter fun j => decide (j ≠ i ∧ j ≠ Fin.last n)).map z)

/-- Eq. (33): the nonexchangeable jackknife+ interval for a symmetric algorithm,
`[Q_α(∑_{i=1}^n w̃_i δ_{μ̂_{-i}(X_{n+1}) - R^{LOO}_i} + w̃_{n+1} δ_{-∞}),
Q_{1-α}(∑_{i=1}^n w̃_i δ_{μ̂_{-i}(X_{n+1}) + R^{LOO}_i} + w̃_{n+1} δ_{+∞})]`, with
`R^{LOO}_i = |Y_i - μ̂_{-i}(X_i)|`. -/
def symJackInterval {X : Type*} {n : ℕ} (alg : Alg X) (w : Fin n → ℝ) (α : ℝ)
    (z : Fin (n + 1) → X × ℝ) : Set ℝ :=
  {y | wQuantileUpper (normWeights w)
        (fun i => if i = Fin.last n then ⊥ else
          ((looModel alg z i (z (Fin.last n)).1 - |(z i).2 - looModel alg z i (z i).1| : ℝ) :
            EReal)) α ≤ (y : EReal) ∧
      (y : EReal) ≤ wQuantile (normWeights w)
        (fun i => if i = Fin.last n then ⊤ else
          ((looModel alg z i (z (Fin.last n)).1 + |(z i).2 - looModel alg z i (z i).1| : ℝ) :
            EReal)) (1 - α)}

/-- Appendix B.3 for a symmetric algorithm: the residual matrix with entries
`|y_i - μ̂_{-ij}(x_i)|`, `μ̂_{-ij} = A((x_k, y_k) : k ∈ [n+1] \ {i, j})`, for `i ≠ j`, and zeros
on the diagonal. -/
def symJackResiduals {X : Type*} {n : ℕ} (alg : Alg X) (z : Fin (n + 1) → X × ℝ) :
    Fin (n + 1) → Fin (n + 1) → ℝ :=
  fun i j => if i = j then 0 else
    |(z i).2 - alg (((List.finRange (n + 1)).filter fun k => decide (k ≠ i ∧ k ≠ j)).map z)
      (z i).1|

/-- Eq. (36): `d_mix(D, D') = inf {t ≥ 0 : D = (1 - t) D' + t D'' for some distribution D''}`
for probability measures `D, D'` (only `t ≤ 1` can occur, since `(1 - t) D'` must be a
measure). -/
def dmix {Ω : Type*} [MeasurableSpace Ω] (D D' : Measure Ω) : ℝ :=
  sInf {t : ℝ | 0 ≤ t ∧ t ≤ 1 ∧ ∃ D'' : Measure Ω, IsProbabilityMeasure D'' ∧
    D = ENNReal.ofReal (1 - t) • D' + ENNReal.ofReal t • D''}

/-- Theorem 6: the contamination term `∑_{i=1}^n w̄_i · d_mix(Z_i, Z_{n+1})`, with
`w̄_i = w_i / (w_1 + ⋯ + w_n)` and `Z_i ∼ μ_i`. -/
def mixTerm {X : Type*} [MeasurableSpace X] {n : ℕ} (μ : Fin (n + 1) → Measure (X × ℝ))
    (w : Fin n → ℝ) : ℝ :=
  ∑ i : Fin n, w i / (∑ j, w j) * dmix (μ i.castSucc) (μ (Fin.last n))

/-- Lemma 3: `E[f(B_1, …, B_n)]` for independent `B_i ∼ Bernoulli(p_i)`,
`∑_{b ∈ {0,1}^n} ∏_i p_i^{b_i} (1 - p_i)^{1 - b_i} f(b)`. -/
def bernoulliExp {n : ℕ} (p : Fin n → ℝ) (f : (Fin n → Bool) → ℝ) : ℝ :=
  ∑ b : Fin n → Bool, (∏ i, if b i then p i else 1 - p i) * f b

/-- Lemma 3: the ratio `(a_1 + ⋯ + a_n + 1) / (a_1 b_1 + ⋯ + a_n b_n + 1)` at `b ∈ {0,1}^n`. -/
def invBernoulliRatio {n : ℕ} (a : Fin n → ℝ) (b : Fin n → Bool) : ℝ :=
  (∑ i, a i + 1) / (∑ i, a i * (if b i then 1 else 0) + 1)

end ConformalBeyondExchangeability
Source
Barber, Candès, Ramdas and Tibshirani, Conformal prediction beyond exchangeability, arXiv:2202.13415v5 (2023), Section 2 (eqs. (5)-(8), footnote 1), Section 3 (eqs. (10)-(12), (16)-(18)), Section 4 (residual map R), Section 6.2 (eq. (24)), Appendix B (eqs. (31)-(34), footnote 3), Appendix C (eq. (36)), Appendix E.4.2 (eq. (39)), Appendix E.5 (Lemma 3)
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

Conventions used throughout.

  • R‾=[−∞,+∞]\overline{\mathbb{R}} = [-\infty, +\infty]R=[−∞,+∞] is the extended real line with its usual order.
  • Indices of (n+1)(n+1)(n+1)-tuples are written 1,…,n+11, \dots, n+11,…,n+1. Position n+1n+1n+1 is the last index, called the "test" position; positions 1,…,n1, \dots, n1,…,n are the "training" positions.
  • A data tuple is z=(z1,…,zn+1)z = (z_1, \dots, z_{n+1})z=(z1​,…,zn+1​) with zi=(xi,yi)∈X×Rz_i = (x_i, y_i) \in \mathcal{X} \times \mathbb{R}zi​=(xi​,yi​)∈X×R.
  • 1{⋅}\mathbb{1}\{\cdot\}1{⋅} denotes an indicator.
  • When a measure PPP is evaluated "as a real number" on a set AAA, this means the real number P(A)P(A)P(A) when P(A)<∞P(A) < \inftyP(A)<∞. It is 000 when P(A)=+∞P(A) = +\inftyP(A)=+∞. For non-measurable AAA it is the outer measure.

wQuantile — the lower weighted quantile QτQ_\tauQτ​. Given:

  • a length m≥0m \ge 0m≥0,
  • arbitrary real weights p1,…,pmp_1, \dots, p_mp1​,…,pm​ (not required to be nonnegative or to sum to 111),
  • points v1,…,vm∈R‾v_1, \dots, v_m \in \overline{\mathbb{R}}v1​,…,vm​∈R,
  • a real level τ\tauτ,

it is defined as

Qτ(p;v)  =  inf⁡{ q∈R‾  :  τ≤∑i : vi≤qpi},Q_\tau(p; v) \;=\; \inf\Big\{\, q \in \overline{\mathbb{R}} \;:\; \tau \le \sum_{i \,:\, v_i \le q} p_i \Big\},Qτ​(p;v)=inf{q∈R:τ≤i:vi​≤q∑​pi​},

with the infimum taken in R‾\overline{\mathbb{R}}R. Edge cases:

  • If no qqq qualifies, the value is +∞+\infty+∞.
  • q=−∞q = -\inftyq=−∞ is allowed. When τ≤0\tau \le 0τ≤0 and no viv_ivi​ equals −∞-\infty−∞, the empty sum 0≥τ0 \ge \tau0≥τ makes q=−∞q = -\inftyq=−∞ qualify, so the value is −∞-\infty−∞.
  • If m=0m = 0m=0, every sum is 000. The value is −∞-\infty−∞ if τ≤0\tau \le 0τ≤0 and +∞+\infty+∞ if τ>0\tau > 0τ>0.
  • With negative weights, the cumulative sum need not be monotone in qqq, so the qualifying set need not be an interval.

wQuantileUpper — the upper weighted quantile Qτ+Q^{+}_\tauQτ+​. With the same kind of inputs (arbitrary real weights pip_ipi​, points vi∈R‾v_i \in \overline{\mathbb{R}}vi​∈R, real τ\tauτ), it is defined as

Qτ+(p;v)  =  sup⁡{ q∈R‾  :  ∑i : vi<qpi≤τ},Q^{+}_\tau(p; v) \;=\; \sup\Big\{\, q \in \overline{\mathbb{R}} \;:\; \sum_{i \,:\, v_i < q} p_i \le \tau \Big\},Qτ+​(p;v)=sup{q∈R:i:vi​<q∑​pi​≤τ},

with the supremum taken in R‾\overline{\mathbb{R}}R. Note the strict inequality vi<qv_i < qvi​<q and the condition "≤τ\le \tau≤τ". Edge cases:

  • If no qqq qualifies, the value is −∞-\infty−∞.
  • For q=−∞q = -\inftyq=−∞ the sum is empty, i.e. 000. So whenever τ≥0\tau \ge 0τ≥0 the set contains −∞-\infty−∞ and is nonempty.
  • With m=0m = 0m=0, the value is +∞+\infty+∞ if τ≥0\tau \ge 0τ≥0 and −∞-\infty−∞ if τ<0\tau < 0τ<0.

normWeights — normalized weights w~\tilde ww~. Given arbitrary real w1,…,wnw_1, \dots, w_nw1​,…,wn​ (no positivity assumed), it produces n+1n+1n+1 numbers:

w~i=wiw1+⋯+wn+1(1≤i≤n),w~n+1=1w1+⋯+wn+1.\tilde w_i = \frac{w_i}{w_1 + \cdots + w_n + 1} \quad (1 \le i \le n), \qquad \tilde w_{n+1} = \frac{1}{w_1 + \cdots + w_n + 1}.w~i​=w1​+⋯+wn​+1wi​​(1≤i≤n),w~n+1​=w1​+⋯+wn​+11​.
  • If w1+⋯+wn=−1w_1 + \cdots + w_n = -1w1​+⋯+wn​=−1, division by zero is interpreted as 000, so all w~i=0\tilde w_i = 0w~i​=0.
  • If the wiw_iwi​ are negative, some w~i\tilde w_iw~i​ may be negative.
  • When n=0n = 0n=0, the single weight is w~1=1\tilde w_1 = 1w~1​=1.

tvDist — total variation distance. For two arbitrary measures μ,ν\mu, \nuμ,ν on a measurable space Ω\OmegaΩ (not assumed to be probability or even finite measures),

dTV(μ,ν)=sup⁡{ ∣μ(A)−ν(A)∣  :  A⊆Ω measurable}.d_{TV}(\mu, \nu) = \sup\big\{\, |\mu(A) - \nu(A)| \;:\; A \subseteq \Omega \text{ measurable} \big\}.dTV​(μ,ν)=sup{∣μ(A)−ν(A)∣:A⊆Ω measurable}.
  • Here μ(A)\mu(A)μ(A) and ν(A)\nu(A)ν(A) are taken as real numbers, so a set of infinite measure contributes the value 000.
  • The supremum is a real supremum. The set always contains 000 (from A=∅A = \emptysetA=∅).
  • If the set is unbounded above, the real supremum is by convention 000.
  • There is no factor 12\tfrac1221​.

swapIdx — the transposition πk\pi_kπk​. For an index k∈{1,…,n+1}k \in \{1, \dots, n+1\}k∈{1,…,n+1}, πk\pi_kπk​ is the permutation of {1,…,n+1}\{1, \dots, n+1\}{1,…,n+1} that exchanges kkk and n+1n+1n+1 and fixes everything else. It is the identity when k=n+1k = n+1k=n+1.

swapData — the swapped sequence zkz^kzk. For a tuple z=(z1,…,zn+1)z = (z_1, \dots, z_{n+1})z=(z1​,…,zn+1​) with entries in an arbitrary set EEE and an index kkk, define

(zk)i=zπk(i).(z^k)_i = z_{\pi_k(i)}.(zk)i​=zπk​(i)​.

That is, zkz^kzk is zzz with entries kkk and n+1n+1n+1 exchanged; it equals zzz when k=n+1k = n+1k=n+1.

withTest — inserting a hypothesized test response. For z∈(X×R)n+1z \in (\mathcal{X} \times \mathbb{R})^{n+1}z∈(X×R)n+1 and y∈Ry \in \mathbb{R}y∈R, z(y)z^{(y)}z(y) is zzz with its last entry replaced by (xn+1,y)(x_{n+1}, y)(xn+1​,y). The covariate xn+1x_{n+1}xn+1​ is kept; only the response yn+1y_{n+1}yn+1​ is replaced by yyy. All other entries are unchanged.

TaggedAlg — tagged algorithms. A tagged algorithm A\mathcal{A}A is an arbitrary function that assigns a function X→R\mathcal{X} \to \mathbb{R}X→R to every finite ordered list (of any length, including empty) of triples (x,y,t)∈X×R×T(x, y, t) \in \mathcal{X} \times \mathbb{R} \times T(x,y,t)∈X×R×T. Here TTT is an arbitrary set of tags. No measurability, symmetry or other regularity is assumed.

Alg — untagged algorithms. An algorithm A\mathcal{A}A is an arbitrary function that assigns a function X→R\mathcal{X} \to \mathbb{R}X→R to every finite ordered list (of any length, including empty) of pairs (x,y)∈X×R(x, y) \in \mathcal{X} \times \mathbb{R}(x,y)∈X×R. No regularity is assumed.

IsSymmetric — symmetric algorithms. An untagged algorithm A\mathcal{A}A is called symmetric if, for every pair of finite lists ℓ,ℓ′\ell, \ell'ℓ,ℓ′ of points in X×R\mathcal{X} \times \mathbb{R}X×R such that ℓ′\ell'ℓ′ is a rearrangement of ℓ\ellℓ (same entries with the same multiplicities, any length), the fitted functions A(ℓ)\mathcal{A}(\ell)A(ℓ) and A(ℓ′)\mathcal{A}(\ell')A(ℓ′) are equal as functions on X\mathcal{X}X.

IsExchangeable — exchangeability. Let PPP be an arbitrary measure on EmE^mEm, where EEE is a measurable space and EmE^mEm carries the product σ\sigmaσ-algebra. PPP is not assumed to be a probability measure. PPP is called exchangeable if, for every permutation σ\sigmaσ of {1,…,m}\{1, \dots, m\}{1,…,m}, the pushforward of PPP under the coordinate permutation

(z1,…,zm)↦(zσ(1),…,zσ(m))(z_1, \dots, z_m) \mapsto (z_{\sigma(1)}, \dots, z_{\sigma(m)})(z1​,…,zm​)↦(zσ(1)​,…,zσ(m)​)

equals PPP. For m=0m = 0m=0 or m=1m = 1m=1 this holds for every PPP.

tagged — the tagged list. Given tags t1,…,tn+1∈Tt_1, \dots, t_{n+1} \in Tt1​,…,tn+1​∈T and z∈(X×R)n+1z \in (\mathcal{X} \times \mathbb{R})^{n+1}z∈(X×R)n+1, this is the ordered list

((x1,y1,t1),…,(xn+1,yn+1,tn+1))\big((x_1, y_1, t_1), \dots, (x_{n+1}, y_{n+1}, t_{n+1})\big)((x1​,y1​,t1​),…,(xn+1​,yn+1​,tn+1​))

of length n+1n+1n+1.

residuals — residual vector R(z)R(z)R(z) of a tagged algorithm. Given a tagged algorithm A\mathcal{A}A, tags ttt, and z∈(X×R)n+1z \in (\mathcal{X} \times \mathbb{R})^{n+1}z∈(X×R)n+1, let μ^=A((x1,y1,t1),…,(xn+1,yn+1,tn+1))\hat\mu = \mathcal{A}\big((x_1, y_1, t_1), \dots, (x_{n+1}, y_{n+1}, t_{n+1})\big)μ^​=A((x1​,y1​,t1​),…,(xn+1​,yn+1​,tn+1​)). Then

R(z)i=∣yi−μ^(xi)∣,i=1,…,n+1.R(z)_i = |y_i - \hat\mu(x_i)|, \qquad i = 1, \dots, n+1.R(z)i​=∣yi​−μ^​(xi​)∣,i=1,…,n+1.

swapResiduals — residuals Ry,kR^{y,k}Ry,k. Given:

  • a tagged algorithm A\mathcal{A}A and tags t1,…,tn+1t_1, \dots, t_{n+1}t1​,…,tn+1​,
  • data zzz, a real yyy, and an index kkk,

let z(y)z^{(y)}z(y) be zzz with last response replaced by yyy, with entries (xi,yi(y))(x_i, y^{(y)}_i)(xi​,yi(y)​). Here yi(y)=yiy^{(y)}_i = y_iyi(y)​=yi​ for i≤ni \le ni≤n and yn+1(y)=yy^{(y)}_{n+1} = yyn+1(y)​=y. Fit

μ^y,k=A((xπk(i), yπk(i)(y), ti)i=1,…,n+1).\hat\mu^{y,k} = \mathcal{A}\Big(\big(x_{\pi_k(i)},\, y^{(y)}_{\pi_k(i)},\, t_i\big)_{i = 1, \dots, n+1}\Big).μ^​y,k=A((xπk​(i)​,yπk​(i)(y)​,ti​)i=1,…,n+1​).

Data entries are permuted by πk\pi_kπk​ (swap of kkk and n+1n+1n+1), while tags stay in their original positions. Then

Riy,k=∣yi(y)−μ^y,k(xi)∣,i=1,…,n+1.R^{y,k}_i = \big|y^{(y)}_i - \hat\mu^{y,k}(x_i)\big|, \qquad i = 1, \dots, n+1.Riy,k​=​yi(y)​−μ^​y,k(xi​)​,i=1,…,n+1.

The residuals are evaluated at the unswapped points of z(y)z^{(y)}z(y).

fullSet — full conformal set C^k\hat C^kC^k for index kkk. The inputs are:

  • a tagged algorithm A\mathcal{A}A and tags ttt,
  • real weights w1,…,wnw_1, \dots, w_nw1​,…,wn​ with normalized weights w~\tilde ww~ as above,
  • an arbitrary real α\alphaα,
  • data zzz and an index kkk.

The set is

C^k(z)={ y∈R  :  Rn+1y,k≤Q1−α(w~; (R1y,k,…,Rn+1y,k))},\hat C^k(z) = \Big\{\, y \in \mathbb{R} \;:\; R^{y,k}_{n+1} \le Q_{1-\alpha}\big(\tilde w;\, (R^{y,k}_1, \dots, R^{y,k}_{n+1})\big) \Big\},C^k(z)={y∈R:Rn+1y,k​≤Q1−α​(w~;(R1y,k​,…,Rn+1y,k​))},

with the comparison made in R‾\overline{\mathbb{R}}R and QQQ the lower weighted quantile defined above. Because the last response of zzz is overwritten by yyy, the set does not depend on yn+1y_{n+1}yn+1​.

randIndexProb — probability with an independent random index. Let:

  • PPP be an arbitrary measure on a measurable space Ω\OmegaΩ,
  • w1,…,wnw_1, \dots, w_nw1​,…,wn​ be real weights with normalized weights w~1,…,w~n+1\tilde w_1, \dots, \tilde w_{n+1}w~1​,…,w~n+1​,
  • E1,…,En+1⊆ΩE_1, \dots, E_{n+1} \subseteq \OmegaE1​,…,En+1​⊆Ω be arbitrary sets (not assumed measurable).

Then

P{E}=∑k=1n+1w~k P(Ek),\mathbb{P}\{E\} = \sum_{k=1}^{n+1} \tilde w_k \, P(E_k),P{E}=k=1∑n+1​w~k​P(Ek​),

with each P(Ek)P(E_k)P(Ek​) taken as a real number. No requirement is made that PPP be a probability measure or that the w~k\tilde w_kw~k​ be nonnegative.

fullCoverage — coverage of nonexchangeable full conformal prediction. Let:

  • PPP be an arbitrary measure on (X×R)n+1(\mathcal{X} \times \mathbb{R})^{n+1}(X×R)n+1 (product σ\sigmaσ-algebra, Borel on R\mathbb{R}R),
  • A\mathcal{A}A be a tagged algorithm with tags ttt,
  • www be real weights and α\alphaα a real number.

The coverage is

∑k=1n+1w~k  P{z:yn+1∈C^k(z)},\sum_{k=1}^{n+1} \tilde w_k \; P\big\{ z : y_{n+1} \in \hat C^k(z) \big\},k=1∑n+1​w~k​P{z:yn+1​∈C^k(z)},

where C^k\hat C^kC^k is the full conformal set above and each probability is taken as a real number (outer measure if the event is not measurable). PPP is not assumed to be a probability measure.

splitResiduals — split residuals. For a fixed function μ^:X→R\hat\mu : \mathcal{X} \to \mathbb{R}μ^​:X→R and data zzz,

Ri=∣yi−μ^(xi)∣,i=1,…,n+1.R_i = |y_i - \hat\mu(x_i)|, \qquad i = 1, \dots, n+1.Ri​=∣yi​−μ^​(xi​)∣,i=1,…,n+1.

splitSet — nonexchangeable split conformal set. Given a fixed function μ^:X→R\hat\mu : \mathcal{X} \to \mathbb{R}μ^​:X→R, real weights www with normalized weights w~\tilde ww~, a real α\alphaα, and data zzz, form the points

vi=∣yi−μ^(xi)∣ (1≤i≤n),vn+1=+∞.v_i = |y_i - \hat\mu(x_i)| \ (1 \le i \le n), \qquad v_{n+1} = +\infty.vi​=∣yi​−μ^​(xi​)∣ (1≤i≤n),vn+1​=+∞.

The set is

C^(z)={ y∈R  :  ∣y−μ^(xn+1)∣≤Q1−α(w~;v)},\hat C(z) = \Big\{\, y \in \mathbb{R} \;:\; |y - \hat\mu(x_{n+1})| \le Q_{1-\alpha}(\tilde w; v) \Big\},C^(z)={y∈R:∣y−μ^​(xn+1​)∣≤Q1−α​(w~;v)},

compared in R‾\overline{\mathbb{R}}R. If the quantile is +∞+\infty+∞, this is all of R\mathbb{R}R; if it is −∞-\infty−∞, the set is empty. The set does not depend on yn+1y_{n+1}yn+1​.

splitCoverage — split conformal coverage. For an arbitrary measure PPP on (X×R)n+1(\mathcal{X} \times \mathbb{R})^{n+1}(X×R)n+1, a fixed μ^\hat\muμ^​, real weights www and real α\alphaα, the coverage is the real number

P{z:yn+1∈C^(z)},P\big\{ z : y_{n+1} \in \hat C(z) \big\},P{z:yn+1​∈C^(z)},

with C^\hat CC^ the split conformal set above. PPP is not assumed to be a probability measure.

swapGap — coverage-gap term. Let:

  • PPP be an arbitrary measure on (X×R)n+1(\mathcal{X} \times \mathbb{R})^{n+1}(X×R)n+1,
  • www be real weights with normalized weights w~\tilde ww~,
  • R:(X×R)n+1→ER : (\mathcal{X} \times \mathbb{R})^{n+1} \to ER:(X×R)n+1→E be an arbitrary map into a measurable space EEE.

The term is

∑i=1nw~i  dTV(R#P,  (R∘si)#P),\sum_{i=1}^{n} \tilde w_i \; d_{TV}\big(R_\# P,\; (R \circ s_i)_\# P\big),i=1∑n​w~i​dTV​(R#​P,(R∘si​)#​P),

where:

  • si(z)=zis_i(z) = z^isi​(z)=zi is the sequence with entries iii and n+1n+1n+1 swapped,
  • R#PR_\# PR#​P denotes the pushforward measure,
  • dTVd_{TV}dTV​ is the total variation distance defined above.

If RRR (respectively R∘siR \circ s_iR∘si​) is not almost-everywhere measurable with respect to PPP, its pushforward is by convention the zero measure. Only training indices i=1,…,ni = 1, \dots, ni=1,…,n appear; w~n+1\tilde w_{n+1}w~n+1​ is not used.

strangeSet — strange points of a vector. For real weights p1,…,pmp_1, \dots, p_mp1​,…,pm​, a real α\alphaα, and r∈Rmr \in \mathbb{R}^mr∈Rm,

S(r)={ i∈{1,…,m}  :  ri>Q1−α(p; (r1,…,rm))},S(r) = \Big\{\, i \in \{1, \dots, m\} \;:\; r_i > Q_{1-\alpha}\big(p;\, (r_1, \dots, r_m)\big) \Big\},S(r)={i∈{1,…,m}:ri​>Q1−α​(p;(r1​,…,rm​))},

with the comparison made in R‾\overline{\mathbb{R}}R using the lower weighted quantile. The inequality is strict.

symResiduals — residuals of an untagged algorithm on the full data. For an untagged algorithm A\mathcal{A}A and data zzz, let μ^=A((x1,y1),…,(xn+1,yn+1))\hat\mu = \mathcal{A}\big((x_1, y_1), \dots, (x_{n+1}, y_{n+1})\big)μ^​=A((x1​,y1​),…,(xn+1​,yn+1​)), fitted on the list in index order. Then

R(z)i=∣yi−μ^(xi)∣,i=1,…,n+1.R(z)_i = |y_i - \hat\mu(x_i)|, \qquad i = 1, \dots, n+1.R(z)i​=∣yi​−μ^​(xi​)∣,i=1,…,n+1.

Symmetry of A\mathcal{A}A is not assumed here.

symFullSet — full conformal set for an untagged algorithm. Given an untagged algorithm A\mathcal{A}A, real weights www with normalized weights w~\tilde ww~, a real α\alphaα, and data zzz, let Ry=R(z(y))R^y = R(z^{(y)})Ry=R(z(y)). That is, take the residuals above computed on zzz with last response replaced by yyy, so the fit uses (x1,y1),…,(xn,yn),(xn+1,y)(x_1, y_1), \dots, (x_n, y_n), (x_{n+1}, y)(x1​,y1​),…,(xn​,yn​),(xn+1​,y). The set is

{ y∈R  :  Rn+1y≤Q1−α(w~; (R1y,…,Rn+1y))},\Big\{\, y \in \mathbb{R} \;:\; R^y_{n+1} \le Q_{1-\alpha}\big(\tilde w;\, (R^y_1, \dots, R^y_{n+1})\big) \Big\},{y∈R:Rn+1y​≤Q1−α​(w~;(R1y​,…,Rn+1y​))},

compared in R‾\overline{\mathbb{R}}R. The algorithm is not required to be symmetric.

ltoModel — leave-two-out tagged model. For a tagged algorithm A\mathcal{A}A, tags ttt, data zzz, and indices i,j∈{1,…,n+1}i, j \in \{1, \dots, n+1\}i,j∈{1,…,n+1},

μ^−ij=A(((xk,yk,tk))k∈{1,…,n+1}∖{i,j}),\hat\mu_{-ij} = \mathcal{A}\Big(\big((x_k, y_k, t_k)\big)_{k \in \{1, \dots, n+1\} \setminus \{i, j\}}\Big),μ^​−ij​=A(((xk​,yk​,tk​))k∈{1,…,n+1}∖{i,j}​),

listed in increasing order of kkk. When i=ji = ji=j, only one point is removed. Any of the removed indices may be n+1n+1n+1.

jackResiduals — tagged leave-two-out residual matrix. For a tagged algorithm A\mathcal{A}A, tags ttt, and data zzz, this is the (n+1)×(n+1)(n+1) \times (n+1)(n+1)×(n+1) matrix with entries

(Rjack+)ij={0i=j,∣yi−μ^−ij(xi)∣i≠j,(R_{\mathrm{jack+}})_{ij} = \begin{cases} 0 & i = j, \\ |y_i - \hat\mu_{-ij}(x_i)| & i \ne j, \end{cases}(Rjack+​)ij​={0∣yi​−μ^​−ij​(xi​)∣​i=j,i=j,​

where μ^−ij\hat\mu_{-ij}μ^​−ij​ is the leave-two-out model above.

looSwapModel — swapped leave-one-out tagged model. For a tagged algorithm A\mathcal{A}A, tags ttt, data zzz, and indices k,i∈{1,…,n+1}k, i \in \{1, \dots, n+1\}k,i∈{1,…,n+1}, set

μ^−ik=A(((xπk(j),yπk(j),tj))j : πk(j)∉{i, n+1}).\hat\mu^k_{-i} = \mathcal{A}\Big(\big((x_{\pi_k(j)}, y_{\pi_k(j)}, t_j)\big)_{j \,:\, \pi_k(j) \notin \{i,\, n+1\}}\Big).μ^​−ik​=A(((xπk​(j)​,yπk​(j)​,tj​))j:πk​(j)∈/{i,n+1}​).

Here:

  • πk\pi_kπk​ swaps kkk and n+1n+1n+1;
  • the list runs over positions j=1,…,n+1j = 1, \dots, n+1j=1,…,n+1 in increasing order, keeping those with πk(j)≠i\pi_k(j) \ne iπk​(j)=i and πk(j)≠n+1\pi_k(j) \ne n+1πk​(j)=n+1;
  • each kept position jjj carries the data point zπk(j)z_{\pi_k(j)}zπk​(j)​ with the tag tjt_jtj​ of that position.

Consequences of this definition:

  • The test point zn+1z_{n+1}zn+1​ never enters the fit.
  • When k≤nk \le nk≤n and k≠ik \ne ik=i, the point zkz_kzk​ appears at position n+1n+1n+1 with tag tn+1t_{n+1}tn+1​.
  • When i=n+1i = n+1i=n+1, only the test point is excluded.

looSwapResidual — swapped leave-one-out residual. With μ^−ik\hat\mu^k_{-i}μ^​−ik​ the swapped leave-one-out model above,

Rik,LOO=∣yi−μ^−ik(xi)∣.R^{k,\mathrm{LOO}}_i = |y_i - \hat\mu^k_{-i}(x_i)|.Rik,LOO​=∣yi​−μ^​−ik​(xi​)∣.

jackInterval — tagged nonexchangeable jackknife+ set for index kkk. Given a tagged algorithm A\mathcal{A}A, tags ttt, real weights www with normalized weights w~\tilde ww~, a real α\alphaα, data zzz, and an index kkk, form for 1≤i≤n1 \le i \le n1≤i≤n

vi−=μ^−ik(xn+1)−Rik,LOO,vi+=μ^−ik(xn+1)+Rik,LOO,v^-_i = \hat\mu^k_{-i}(x_{n+1}) - R^{k,\mathrm{LOO}}_i, \qquad v^+_i = \hat\mu^k_{-i}(x_{n+1}) + R^{k,\mathrm{LOO}}_i,vi−​=μ^​−ik​(xn+1​)−Rik,LOO​,vi+​=μ^​−ik​(xn+1​)+Rik,LOO​,

and set vn+1−=−∞v^-_{n+1} = -\inftyvn+1−​=−∞ and vn+1+=+∞v^+_{n+1} = +\inftyvn+1+​=+∞. The set is

C^k(z)={ y∈R  :  Qα+(w~;v−)≤y≤Q1−α(w~;v+)},\hat C^k(z) = \Big\{\, y \in \mathbb{R} \;:\; Q^{+}_{\alpha}(\tilde w; v^-) \le y \le Q_{1-\alpha}(\tilde w; v^+) \Big\},C^k(z)={y∈R:Qα+​(w~;v−)≤y≤Q1−α​(w~;v+)},

compared in R‾\overline{\mathbb{R}}R:

  • the left endpoint is the upper weighted quantile at level α\alphaα (strict "<<<", "≤α\le \alpha≤α" convention);
  • the right endpoint is the lower weighted quantile at level 1−α1-\alpha1−α;
  • both inequalities are non-strict;
  • the set may be empty if the left endpoint exceeds the right one.

It depends on the test point only through xn+1x_{n+1}xn+1​, not yn+1y_{n+1}yn+1​.

jackCoverage — jackknife+ coverage. For an arbitrary measure PPP on (X×R)n+1(\mathcal{X} \times \mathbb{R})^{n+1}(X×R)n+1, a tagged algorithm A\mathcal{A}A, tags ttt, real weights www and real α\alphaα, the coverage is

∑k=1n+1w~k  P{z:yn+1∈C^k(z)},\sum_{k=1}^{n+1} \tilde w_k \; P\big\{ z : y_{n+1} \in \hat C^k(z) \big\},k=1∑n+1​w~k​P{z:yn+1​∈C^k(z)},

with C^k\hat C^kC^k the jackknife+ set above and each probability taken as a real number. PPP is not required to be a probability measure.

tournamentStrangeSet — strange points of a matrix. For real weights p1,…,pmp_1, \dots, p_mp1​,…,pm​, a real α\alphaα, and a real m×mm \times mm×m matrix rrr,

S(r)={ i  :  ∑j=1mpj 1{rij>rji}≥1−α}.S(r) = \Big\{\, i \;:\; \sum_{j=1}^{m} p_j \, \mathbb{1}\{ r_{ij} > r_{ji} \} \ge 1 - \alpha \Big\}.S(r)={i:j=1∑m​pj​1{rij​>rji​}≥1−α}.

The j=ij = ij=i term always contributes 000, and the comparison rij>rjir_{ij} > r_{ji}rij​>rji​ is strict.

looModel — untagged leave-one-out model. For an untagged algorithm A\mathcal{A}A, data zzz, and an index iii,

μ^−i=A((zj)j∈{1,…,n+1}∖{i, n+1}),\hat\mu_{-i} = \mathcal{A}\big( (z_j)_{j \in \{1, \dots, n+1\} \setminus \{i,\, n+1\}} \big),μ^​−i​=A((zj​)j∈{1,…,n+1}∖{i,n+1}​),

listed in increasing order of jjj. The test point is always excluded. If i=n+1i = n+1i=n+1, this is the fit on all nnn training points.

symJackInterval — untagged nonexchangeable jackknife+ set. Given an untagged algorithm A\mathcal{A}A, real weights www with normalized weights w~\tilde ww~, a real α\alphaα, and data zzz, set for 1≤i≤n1 \le i \le n1≤i≤n

RiLOO=∣yi−μ^−i(xi)∣,R^{\mathrm{LOO}}_i = |y_i - \hat\mu_{-i}(x_i)|,RiLOO​=∣yi​−μ^​−i​(xi​)∣, vi−=μ^−i(xn+1)−RiLOO,vi+=μ^−i(xn+1)+RiLOO,v^-_i = \hat\mu_{-i}(x_{n+1}) - R^{\mathrm{LOO}}_i, \qquad v^+_i = \hat\mu_{-i}(x_{n+1}) + R^{\mathrm{LOO}}_i,vi−​=μ^​−i​(xn+1​)−RiLOO​,vi+​=μ^​−i​(xn+1​)+RiLOO​,

with μ^−i\hat\mu_{-i}μ^​−i​ the leave-one-out model above, and vn+1−=−∞v^-_{n+1} = -\inftyvn+1−​=−∞, vn+1+=+∞v^+_{n+1} = +\inftyvn+1+​=+∞. The set is

{ y∈R  :  Qα+(w~;v−)≤y≤Q1−α(w~;v+)},\Big\{\, y \in \mathbb{R} \;:\; Q^{+}_{\alpha}(\tilde w; v^-) \le y \le Q_{1-\alpha}(\tilde w; v^+) \Big\},{y∈R:Qα+​(w~;v−)≤y≤Q1−α​(w~;v+)},

compared in R‾\overline{\mathbb{R}}R, with the upper weighted quantile on the left and the lower one on the right. Symmetry of A\mathcal{A}A is not assumed.

symJackResiduals — untagged leave-two-out residual matrix. For an untagged algorithm A\mathcal{A}A and data zzz, this is the (n+1)×(n+1)(n+1) \times (n+1)(n+1)×(n+1) matrix with zero diagonal and, for i≠ji \ne ji=j,

∣yi−μ^−ij(xi)∣,μ^−ij=A((zk)k∈{1,…,n+1}∖{i,j}),|y_i - \hat\mu_{-ij}(x_i)|, \qquad \hat\mu_{-ij} = \mathcal{A}\big( (z_k)_{k \in \{1, \dots, n+1\} \setminus \{i, j\}} \big),∣yi​−μ^​−ij​(xi​)∣,μ^​−ij​=A((zk​)k∈{1,…,n+1}∖{i,j}​),

with the list in increasing order of kkk.

dmix — contamination distance. For arbitrary measures D,D′D, D'D,D′ on Ω\OmegaΩ (not assumed to be probability measures),

dmix(D,D′)=inf⁡{ t∈[0,1]  :  D=(1−t) D′+t D′′ for some probability measure D′′},d_{\mathrm{mix}}(D, D') = \inf\Big\{\, t \in [0, 1] \;:\; D = (1-t)\, D' + t\, D'' \text{ for some probability measure } D'' \Big\},dmix​(D,D′)=inf{t∈[0,1]:D=(1−t)D′+tD′′ for some probability measure D′′},

taken as a real infimum.

  • If no such ttt exists, the infimum of the empty set is by convention 000. This can happen, for example, when DDD is not a probability measure, or when D′D'D′ has infinite mass.
  • If DDD is a probability measure, t=1t = 1t=1 (with D′′=DD'' = DD′′=D) is always admissible.

mixTerm — contamination term. For arbitrary measures μ1,…,μn+1\mu_1, \dots, \mu_{n+1}μ1​,…,μn+1​ on X×R\mathcal{X} \times \mathbb{R}X×R and real weights w1,…,wnw_1, \dots, w_nw1​,…,wn​, the term is

∑i=1nwiw1+⋯+wn  dmix(μi,μn+1),\sum_{i=1}^{n} \frac{w_i}{w_1 + \cdots + w_n}\; d_{\mathrm{mix}}(\mu_i, \mu_{n+1}),i=1∑n​w1​+⋯+wn​wi​​dmix​(μi​,μn+1​),

with dmixd_{\mathrm{mix}}dmix​ the contamination distance above. If w1+⋯+wn=0w_1 + \cdots + w_n = 0w1​+⋯+wn​=0, each fraction is 000 by the division-by-zero convention, so the term is 000. No positivity of the weights is assumed.

bernoulliExp — Bernoulli expectation. For real numbers p1,…,pnp_1, \dots, p_np1​,…,pn​ (not required to lie in [0,1][0,1][0,1]) and f:{0,1}n→Rf : \{0,1\}^n \to \mathbb{R}f:{0,1}n→R,

∑b∈{0,1}n(∏i=1n(pi if bi=1, else 1−pi))f(b).\sum_{b \in \{0,1\}^n} \Big( \prod_{i=1}^{n} \big( p_i \text{ if } b_i = 1, \text{ else } 1 - p_i \big) \Big) f(b).b∈{0,1}n∑​(i=1∏n​(pi​ if bi​=1, else 1−pi​))f(b).

For n=0n = 0n=0 this equals fff evaluated at the empty tuple.

invBernoulliRatio — Bernoulli ratio. For real a1,…,ana_1, \dots, a_na1​,…,an​ and b∈{0,1}nb \in \{0,1\}^nb∈{0,1}n,

a1+⋯+an+1a1b1+⋯+anbn+1.\frac{a_1 + \cdots + a_n + 1}{a_1 b_1 + \cdots + a_n b_n + 1}.a1​b1​+⋯+an​bn​+1a1​+⋯+an​+1​.

If the denominator is 000, the value is 000 by convention.

Human review
  • Endorsed by Shuze Chen · Nov 24, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Xeroth · Nov 24, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me