Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Conformal prediction under covariate shift: quantiles, scores, (weighted) exchangeability and conformal bands

Definition
ConformalCovariateShift_Basic

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

conformal-predictioncovariate-shiftdistribution-free-inferenceexchangeabilityquantiles

Objects of Sections 1-3 of Tibshirani, Barber, Candès and Ramdas (2019).

  1. Quantiles (Section 1.1). For a distribution FFF on the augmented real line R∪{∞}\mathbb{R}\cup\{\infty\}R∪{∞}, Quantile(β;F)=inf⁡{z:PZ∼F{Z≤z}≥β}\mathrm{Quantile}(\beta;F)=\inf\{z:\mathbb{P}_{Z\sim F}\{Z\le z\}\ge\beta\}Quantile(β;F)=inf{z:PZ∼F​{Z≤z}≥β}. A discrete distribution is ∑ipiδvi\sum_ip_i\delta_{v_i}∑i​pi​δvi​​. For values v1,…,vn+1v_1,\dots,v_{n+1}v1​,…,vn+1​, Quantile(β;v1:(n+1))\mathrm{Quantile}(\beta;v_{1:(n+1)})Quantile(β;v1:(n+1)​) is the quantile of 1n+1∑i=1n+1δvi\frac1{n+1}\sum_{i=1}^{n+1}\delta_{v_i}n+11​∑i=1n+1​δvi​​, and Quantile(β;v1:n∪{∞})\mathrm{Quantile}(\beta;v_{1:n}\cup\{\infty\})Quantile(β;v1:n​∪{∞}) is the quantile of 1n+1∑i=1nδvi+1n+1δ∞\frac1{n+1}\sum_{i=1}^{n}\delta_{v_i}+\frac1{n+1}\delta_\inftyn+11​∑i=1n​δvi​​+n+11​δ∞​. For weights p1,…,pn+1p_1,\dots,p_{n+1}p1​,…,pn+1​, the distribution ∑i=1npiδvi+pn+1δ∞\sum_{i=1}^np_i\delta_{v_i}+p_{n+1}\delta_\infty∑i=1n​pi​δvi​​+pn+1​δ∞​ is used in (8), (16) and Lemma 3.
  2. Scores (Section 1.2, eq. 4). Data points are (x,y)∈Rd×R(x,y)\in\mathbb{R}^d\times\mathbb{R}(x,y)∈Rd×R. A score function S((x,y),Z)\mathcal S((x,y),Z)S((x,y),Z) takes a point and a multiset ZZZ of points. For points z1,…,zmz_1,\dots,z_mz1​,…,zm​, the nonconformity scores are Vi=S(zi,z1:m∖{zi})V_i=\mathcal S(z_i,z_{1:m}\setminus\{z_i\})Vi​=S(zi​,z1:m​∖{zi​}), where z1:m∖{zi}z_{1:m}\setminus\{z_i\}z1:m​∖{zi​} is the multiset of the other points. For training points Z1,…,ZnZ_1,\dots,Z_nZ1​,…,Zn​ and a candidate (x,y)(x,y)(x,y), the scores Vi(x,y)V_i^{(x,y)}Vi(x,y)​ of (4) are those of the n+1n+1n+1 points Z1,…,Zn,(x,y)Z_1,\dots,Z_n,(x,y)Z1​,…,Zn​,(x,y).
  3. Exchangeability. Random variables V1,…,VmV_1,\dots,V_mV1​,…,Vm​ are exchangeable if (Vσ(1),…,Vσ(m))(V_{\sigma(1)},\dots,V_{\sigma(m)})(Vσ(1)​,…,Vσ(m)​) has the same law as (V1,…,Vm)(V_1,\dots,V_m)(V1​,…,Vm​) for every permutation σ\sigmaσ.
  4. Conformal band (eq. 5). C^n(x)={y∈R:Vn+1(x,y)≤Quantile(1−α;V1:n(x,y)∪{∞})}\widehat C_n(x)=\{y\in\mathbb{R}:V_{n+1}^{(x,y)}\le\mathrm{Quantile}(1-\alpha;V_{1:n}^{(x,y)}\cup\{\infty\})\}Cn​(x)={y∈R:Vn+1(x,y)​≤Quantile(1−α;V1:n(x,y)​∪{∞})}.
  5. Covariate shift (eqs. 7-8). For a weight function www on Rd\mathbb{R}^dRd, piw(x)=w(Xi)/(∑j=1nw(Xj)+w(x))p_i^w(x)=w(X_i)/(\sum_{j=1}^nw(X_j)+w(x))piw​(x)=w(Xi​)/(∑j=1n​w(Xj​)+w(x)) for i≤ni\le ni≤n and pn+1w(x)=w(x)/(∑j=1nw(Xj)+w(x))p_{n+1}^w(x)=w(x)/(\sum_{j=1}^nw(X_j)+w(x))pn+1w​(x)=w(x)/(∑j=1n​w(Xj​)+w(x)). The band (8) is {y:Vn+1(x,y)≤Quantile(1−α;∑i=1npiw(x)δVi(x,y)+pn+1w(x)δ∞)}\{y:V_{n+1}^{(x,y)}\le\mathrm{Quantile}(1-\alpha;\sum_{i=1}^np_i^w(x)\delta_{V_i^{(x,y)}}+p_{n+1}^w(x)\delta_\infty)\}{y:Vn+1(x,y)​≤Quantile(1−α;∑i=1n​piw​(x)δVi(x,y)​​+pn+1w​(x)δ∞​)}.
  6. Weighted exchangeability (Definition 1). Random variables Z1,…,ZmZ_1,\dots,Z_mZ1​,…,Zm​ with values in a measurable space EEE are weighted exchangeable with measurable, nonnegative weight functions w1,…,wmw_1,\dots,w_mw1​,…,wm​ if their joint law has a density fff with respect to a measure ν\nuν on EmE^mEm that is invariant under permutations of the coordinates, and f(z1,…,zm)=∏iwi(zi)⋅g(z1,…,zm)f(z_1,\dots,z_m)=\prod_iw_i(z_i)\cdot g(z_1,\dots,z_m)f(z1​,…,zm​)=∏i​wi​(zi​)⋅g(z1​,…,zm​) with ggg invariant under permutations of its arguments.
  7. Weighted band (eqs. 15-16). piw(z1,…,zn+1)=∑σ:σ(n+1)=i∏j=1n+1wj(zσ(j))/∑σ∏j=1n+1wj(zσ(j))p_i^w(z_1,\dots,z_{n+1})=\sum_{\sigma:\sigma(n+1)=i}\prod_{j=1}^{n+1}w_j(z_{\sigma(j)})\big/\sum_\sigma\prod_{j=1}^{n+1}w_j(z_{\sigma(j)})piw​(z1​,…,zn+1​)=∑σ:σ(n+1)=i​∏j=1n+1​wj​(zσ(j)​)/∑σ​∏j=1n+1​wj​(zσ(j)​), the sums over permutations of {1,…,n+1}\{1,\dots,n+1\}{1,…,n+1}. The band (16) is {y:Vn+1(x,y)≤Quantile(1−α;∑i=1npiw(Z1,…,Zn,(x,y))δVi(x,y)+pn+1w(Z1,…,Zn,(x,y))δ∞)}\{y:V_{n+1}^{(x,y)}\le\mathrm{Quantile}(1-\alpha;\sum_{i=1}^np_i^w(Z_1,\dots,Z_n,(x,y))\delta_{V_i^{(x,y)}}+p_{n+1}^w(Z_1,\dots,Z_n,(x,y))\delta_\infty)\}{y:Vn+1(x,y)​≤Quantile(1−α;∑i=1n​piw​(Z1​,…,Zn​,(x,y))δVi(x,y)​​+pn+1w​(Z1​,…,Zn​,(x,y))δ∞​)}.

Formalization Note The augmented real line is EReal with ∞=⊤\infty=\top∞=⊤; quantiles take values there, and the infimum of an empty set is +∞+\infty+∞. Weights enter a discrete distribution through ENNReal.ofReal, which is the identity on the nonnegative weights used here. When a denominator in (7) or (15) is 000, Lean's division returns 000, all weights vanish and the quantile is +∞+\infty+∞; this happens only on a null event in every statement. The paper allows an arbitrary base measure in Definition 1; the definition requires it to be permutation invariant, because with an arbitrary base measure Lemma 3 is false (see the report).

Definition code
import Mathlib

/-!
Tibshirani, Barber, Candès and Ramdas, *Conformal Prediction Under Covariate Shift*,
arXiv:1904.06019v3 (2019): quantiles of discrete distributions on the augmented real line
(Section 1.1), nonconformity scores and the conformal band (Section 1.2, eqs. (4)-(5)), the
covariate-shift weights and band (Section 2.1, eqs. (7)-(8)), and weighted exchangeability with
the weights (15) and the weighted conformal band (16) (Sections 3.2-3.4).
-/

open MeasureTheory

noncomputable section

namespace ConformalCovariateShift

/-- Section 1.1, p. 2: `Quantile(β; F) = inf {z : P{Z ≤ z} ≥ β}` for `Z ∼ F`, where `F` is a
distribution on the augmented real line `ℝ ∪ {∞}`, here encoded in `EReal` (with `∞ = ⊤`). -/
def quantile (β : ℝ) (F : Measure EReal) : EReal :=
  sInf {z : EReal | ENNReal.ofReal β ≤ F (Set.Iic z)}

/-- Section 1.1, p. 2: the discrete distribution `∑_i p_i δ_{v_i}` on `ℝ ∪ {∞}`. With
`p_i = 1/m` for all `i` it is the empirical distribution of the multiset `v_{1:m}`. -/
def discreteDist {ι : Type*} [Fintype ι] (p : ι → ℝ) (v : ι → EReal) : Measure EReal :=
  ∑ i, ENNReal.ofReal (p i) • Measure.dirac (v i)

/-- Eqs. (8), (16) and Lemma 3: `∑_{i=1}^n p_i δ_{v_i} + p_{n+1} δ_∞`, the distribution of
the first `n` values with weights `p_1, …, p_n` plus a point mass `p_{n+1}` at `∞` (the
`(n+1)`-st value is not used). -/
def augDist {n : ℕ} (p : Fin (n + 1) → ℝ) (v : Fin (n + 1) → ℝ) : Measure EReal :=
  discreteDist p (fun i => if i = Fin.last n then (⊤ : EReal) else (v i : EReal))

/-- Lemma 1 and eq. (5): `Quantile(β; v_{1:n} ∪ {∞})`, the level-`β` quantile of the empirical
distribution of the `n + 1` values `v_1, …, v_n, ∞` (the `(n+1)`-st value is not used). -/
def quantileWithInf {n : ℕ} (β : ℝ) (v : Fin (n + 1) → ℝ) : EReal :=
  quantile β (augDist (fun _ => 1 / ((n : ℝ) + 1)) v)

/-- Eq. (2): `Quantile(β; v_{1:(n+1)})`, the level-`β` quantile of the empirical distribution
of the `n + 1` real values `v_1, …, v_{n+1}`. -/
def empQuantile {n : ℕ} (β : ℝ) (v : Fin (n + 1) → ℝ) : EReal :=
  quantile β (discreteDist (fun _ => 1 / ((n : ℝ) + 1)) (fun i => (v i : EReal)))

/-- Section 1.2, p. 2: a data point `(x, y) ∈ ℝ^d × ℝ`. -/
abbrev DataPt (d : ℕ) : Type := (Fin d → ℝ) × ℝ

/-- Section 1.2, p. 2 and footnote 1: a score function `S((x, y), Z)` of a point and a multiset
`Z` of points (the points of `Z` are unordered). -/
abbrev ScoreFn (E : Type*) : Type _ := E → Multiset E → ℝ

/-- Eq. (4) and Lemma 3: the nonconformity scores `V_i = S(z_i, z_{1:m} \ {z_i})` of the points
`z_1, …, z_m`, where `z_{1:m} \ {z_i}` is the multiset of the other `m - 1` points. -/
def scores {E : Type*} {m : ℕ} (S : ScoreFn E) (z : Fin m → E) (i : Fin m) : ℝ :=
  S (z i) ((Finset.univ.erase i).val.map z)

/-- Section 1.2, p. 2: the `n + 1` points `Z_1, …, Z_n, (x, y)` formed by the `n` training points
and a candidate test point `(x, y)`; the scores of eq. (4) are `scores S (withTest Z (x, y))`. -/
def withTest {E : Type*} {n : ℕ} (data : Fin n → E) (p : E) : Fin (n + 1) → E :=
  Fin.snoc (α := fun _ => E) data p

/-- Section 1.1 and the proof of Theorem 1, p. 3: the random variables `V_1, …, V_m` (measurable
maps on a probability space) are exchangeable, i.e. `(V_{σ(1)}, …, V_{σ(m)})` and
`(V_1, …, V_m)` have the same joint law for every permutation `σ`. -/
def IsExchangeable {Omega E : Type*} [MeasurableSpace Omega] [MeasurableSpace E] {m : ℕ}
    (P : Measure Omega) (V : Fin m → Omega → E) : Prop :=
  (∀ i, Measurable (V i)) ∧
    ∀ σ : Equiv.Perm (Fin m),
      Measure.map (fun ω i => V (σ i) ω) P = Measure.map (fun ω i => V i ω) P

/-- Eq. (5), Theorem 1: the conformal band at `x` built from the training points
`Z_1, …, Z_n`: the set of `y ∈ ℝ` with `V_{n+1}^{(x,y)} ≤ Quantile(1 - α; V_{1:n}^{(x,y)} ∪ {∞})`. -/
def conformalBand {d n : ℕ} (S : ScoreFn (DataPt d)) (α : ℝ) (data : Fin n → DataPt d)
    (x : Fin d → ℝ) : Set ℝ :=
  {y | (scores S (withTest data (x, y)) (Fin.last n) : EReal) ≤
    quantileWithInf (1 - α) (scores S (withTest data (x, y)))}

/-- Eq. (7), Section 2.1, p. 4: the covariate-shift weights
`p_i^w(x) = w(X_i) / (∑_{j=1}^n w(X_j) + w(x))` for `i ≤ n` and
`p_{n+1}^w(x) = w(x) / (∑_{j=1}^n w(X_j) + w(x))`. -/
def covShiftWeights {d n : ℕ} (w : (Fin d → ℝ) → ℝ) (X : Fin n → (Fin d → ℝ))
    (x : Fin d → ℝ) : Fin (n + 1) → ℝ :=
  fun i => w (withTest X x i) / (∑ j, w (X j) + w x)

/-- Eq. (8), Corollary 1: the weighted conformal band under covariate shift: the set of `y ∈ ℝ`
with `V_{n+1}^{(x,y)} ≤ Quantile(1 - α; ∑_{i=1}^n p_i^w(x) δ_{V_i^{(x,y)}} + p_{n+1}^w(x) δ_∞)`. -/
def covShiftBand {d n : ℕ} (S : ScoreFn (DataPt d)) (α : ℝ) (w : (Fin d → ℝ) → ℝ)
    (data : Fin n → DataPt d) (x : Fin d → ℝ) : Set ℝ :=
  {y | (scores S (withTest data (x, y)) (Fin.last n) : EReal) ≤
    quantile (1 - α) (augDist (covShiftWeights w (fun i => (data i).1) x)
      (scores S (withTest data (x, y))))}

/-- Definition 1, Section 3.2, p. 12: the random variables `Z_1, …, Z_m` are weighted
exchangeable with weight functions `w_1, …, w_m` if their joint law has a density `f` with respect
to a base measure `ν` on `E^m` and `f(z_1, …, z_m) = ∏_i w_i(z_i) · g(z_1, …, z_m)` with `g`
invariant under permutations of its arguments. The `Z_i` are measurable, the weight functions are
measurable and nonnegative, and the base measure `ν` is required to be invariant under
permutations of the coordinates (the paper says "arbitrary base measure"; see the mission's
report for why invariance is needed). -/
def IsWeightedExchangeable {Omega E : Type*} [MeasurableSpace Omega] [MeasurableSpace E] {m : ℕ}
    (P : Measure Omega) (Z : Fin m → Omega → E) (w : Fin m → E → ℝ) : Prop :=
  (∀ i, Measurable (Z i)) ∧ (∀ i, Measurable (w i)) ∧ (∀ i z, 0 ≤ w i z) ∧
    ∃ (ν : Measure (Fin m → E)) (f g : (Fin m → E) → ENNReal),
      (∀ σ : Equiv.Perm (Fin m), Measure.map (fun z => z ∘ σ) ν = ν) ∧
      Measurable f ∧
      Measure.map (fun ω i => Z i ω) P = ν.withDensity f ∧
      (∀ z, f z = (∏ i, ENNReal.ofReal (w i (z i))) * g z) ∧
      (∀ (σ : Equiv.Perm (Fin m)) (z : Fin m → E), g (z ∘ σ) = g z)

/-- Eq. (15), Lemma 3, p. 12: `p_i^w(z_1, …, z_{n+1}) =
(∑_{σ : σ(n+1) = i} ∏_{j=1}^{n+1} w_j(z_{σ(j)})) / (∑_σ ∏_{j=1}^{n+1} w_j(z_{σ(j)}))`, the sums
running over permutations `σ` of `1, …, n+1`. -/
def permWeight {E : Type*} {n : ℕ} (w : Fin (n + 1) → E → ℝ) (z : Fin (n + 1) → E)
    (i : Fin (n + 1)) : ℝ :=
  (∑ σ ∈ Finset.univ.filter (fun σ : Equiv.Perm (Fin (n + 1)) => σ (Fin.last n) = i),
      ∏ j, w j (z (σ j))) /
    ∑ σ : Equiv.Perm (Fin (n + 1)), ∏ j, w j (z (σ j))

/-- Eq. (16), Theorem 2, p. 13: the weighted conformal band: the set of `y ∈ ℝ` with
`V_{n+1}^{(x,y)} ≤ Quantile(1 - α; ∑_{i=1}^n p_i^w(Z_1, …, Z_n, (x, y)) δ_{V_i^{(x,y)}}
+ p_{n+1}^w(Z_1, …, Z_n, (x, y)) δ_∞)`. -/
def weightedConformalBand {d n : ℕ} (S : ScoreFn (DataPt d)) (α : ℝ)
    (w : Fin (n + 1) → DataPt d → ℝ) (data : Fin n → DataPt d) (x : Fin d → ℝ) : Set ℝ :=
  {y | (scores S (withTest data (x, y)) (Fin.last n) : EReal) ≤
    quantile (1 - α) (augDist (permWeight w (withTest data (x, y)))
      (scores S (withTest data (x, y))))}

end ConformalCovariateShift
Source
Tibshirani, Barber, Candès and Ramdas, Conformal Prediction Under Covariate Shift, arXiv:1904.06019v3 (2019), Section 1.1 (quantiles, p. 2), Section 1.2 (eqs. (4)-(5), pp. 2-3), Section 2.1 (eqs. (7)-(8), p. 4), Section 3.2 (Definition 1, p. 12), Sections 3.3-3.4 (eqs. (15)-(16), pp. 12-13)
Read-back

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

quantile. For a real number β\betaβ and an arbitrary measure FFF on the extended real line R‾=[−∞,+∞]\overline{\mathbb R} = [-\infty, +\infty]R=[−∞,+∞], which may be infinite and need not be a probability measure,

Quantile(β;F)  =  inf⁡{ z∈[−∞,+∞]  :  max⁡(β,0)≤F([−∞,z]) },\mathrm{Quantile}(\beta; F) \;=\; \inf\bigl\{\, z \in [-\infty,+\infty] \;:\; \max(\beta,0) \le F\bigl([-\infty, z]\bigr) \,\bigr\},Quantile(β;F)=inf{z∈[−∞,+∞]:max(β,0)≤F([−∞,z])},

where the infimum is taken in [−∞,+∞][-\infty,+\infty][−∞,+∞]. Here [−∞,z][-\infty,z][−∞,z] is the set of all extended reals ≤z\le z≤z, including −∞-\infty−∞. The level enters only through max⁡(β,0)\max(\beta,0)max(β,0).

  • If β≤0\beta \le 0β≤0, every zzz qualifies and the quantile is −∞-\infty−∞.
  • If no zzz qualifies, for example when β\betaβ exceeds the total mass F(R‾)F(\overline{\mathbb R})F(R), the infimum of the empty set is +∞+\infty+∞.

discreteDist. Let ι\iotaι be any finite index set, possibly empty. Given real weights (pi)i∈ι(p_i)_{i\in\iota}(pi​)i∈ι​ and points (vi)i∈ι(v_i)_{i\in\iota}(vi​)i∈ι​ in [−∞,+∞][-\infty,+\infty][−∞,+∞], this is the measure

∑i∈ιmax⁡(pi,0) δvi\sum_{i\in\iota} \max(p_i,0)\,\delta_{v_i}i∈ι∑​max(pi​,0)δvi​​

on [−∞,+∞][-\infty,+\infty][−∞,+∞].

  • Negative weights are replaced by 000.
  • The weights are not normalized, so the total mass is ∑imax⁡(pi,0)\sum_i \max(p_i,0)∑i​max(pi​,0). That total may differ from 111, and it is 000 when ι\iotaι is empty.

augDist. For a natural number n≥0n \ge 0n≥0, real weights p1,…,pn+1p_1,\dots,p_{n+1}p1​,…,pn+1​ and real values v1,…,vn+1v_1,\dots,v_{n+1}v1​,…,vn+1​, this is the measure

∑i=1nmax⁡(pi,0) δvi  +  max⁡(pn+1,0) δ+∞\sum_{i=1}^{n} \max(p_i,0)\,\delta_{v_i} \;+\; \max(p_{n+1},0)\,\delta_{+\infty}i=1∑n​max(pi​,0)δvi​​+max(pn+1​,0)δ+∞​

on [−∞,+∞][-\infty,+\infty][−∞,+∞].

  • The value vn+1v_{n+1}vn+1​ is ignored. Its slot is replaced by the point +∞+\infty+∞.
  • Negative weights are truncated to 000, and there is no normalization.
  • When n=0n=0n=0, the measure is just max⁡(p1,0) δ+∞\max(p_1,0)\,\delta_{+\infty}max(p1​,0)δ+∞​.

quantileWithInf. For n≥0n\ge 0n≥0, a real level β\betaβ and real values v1,…,vn+1v_1,\dots,v_{n+1}v1​,…,vn+1​, this is Quantile(β;⋅)\mathrm{Quantile}(\beta; \cdot)Quantile(β;⋅) (as defined above) of the probability measure

1n+1∑i=1nδvi  +  1n+1 δ+∞.\frac{1}{n+1}\sum_{i=1}^{n}\delta_{v_i} \;+\; \frac{1}{n+1}\,\delta_{+\infty}.n+11​i=1∑n​δvi​​+n+11​δ+∞​.

The value vn+1v_{n+1}vn+1​ is not used.

  • If β≤0\beta\le 0β≤0, the result is −∞-\infty−∞.
  • If β>1\beta>1β>1, the defining set is empty and the result is +∞+\infty+∞.
  • When n=0n=0n=0, all mass sits at +∞+\infty+∞, so the result is +∞+\infty+∞ for every β>0\beta>0β>0.

empQuantile. For n≥0n\ge 0n≥0, a real level β\betaβ and real values v1,…,vn+1v_1,\dots,v_{n+1}v1​,…,vn+1​, this is Quantile(β;⋅)\mathrm{Quantile}(\beta;\cdot)Quantile(β;⋅) of the empirical probability measure

1n+1∑i=1n+1δvi\frac{1}{n+1}\sum_{i=1}^{n+1}\delta_{v_i}n+11​i=1∑n+1​δvi​​

on [−∞,+∞][-\infty,+\infty][−∞,+∞].

  • If β≤0\beta\le 0β≤0, the result is −∞-\infty−∞.
  • If β>1\beta>1β>1, the result is +∞+\infty+∞.

DataPt. For a natural number ddd (possibly 000), a data point is a pair (x,y)(x,y)(x,y) with x∈Rdx\in\mathbb R^dx∈Rd and y∈Ry\in\mathbb Ry∈R.

ScoreFn. For any type EEE, a score function is an arbitrary map

S:E×{finite multisets of elements of E}→R.S : E \times \{\text{finite multisets of elements of } E\} \to \mathbb R .S:E×{finite multisets of elements of E}→R.

No measurability, symmetry or other regularity is required.

scores. Given a score function SSS on EEE, a number m≥0m\ge 0m≥0, points z1,…,zm∈Ez_1,\dots,z_m\in Ez1​,…,zm​∈E and an index i∈{1,…,m}i\in\{1,\dots,m\}i∈{1,…,m}, the score of point iii is

Vi  =  S(zi, { ⁣{zj:j≠i} ⁣}).V_i \;=\; S\bigl(z_i,\ \{\!\{ z_j : j\neq i \}\!\}\bigr).Vi​=S(zi​, {{zj​:j=i}}).

Here the second argument is the multiset of the other m−1m-1m−1 points, with repetitions kept. When m=0m=0m=0, there are no indices.

withTest. Given n≥0n\ge 0n≥0 points Z1,…,Zn∈EZ_1,\dots,Z_n\in EZ1​,…,Zn​∈E and a further point p∈Ep\in Ep∈E, this is the (n+1)(n+1)(n+1)-tuple

(Z1,…,Zn, p),(Z_1,\dots,Z_n,\,p),(Z1​,…,Zn​,p),

with ppp placed in the last position n+1n+1n+1.

IsExchangeable. Let PPP be any measure on a measurable space Ω\OmegaΩ; it is not required to be a probability measure, nor even finite. Let V1,…,Vm:Ω→EV_1,\dots,V_m : \Omega\to EV1​,…,Vm​:Ω→E be maps into a measurable space EEE, with m≥0m\ge 0m≥0. The statement "V1,…,VmV_1,\dots,V_mV1​,…,Vm​ are exchangeable under PPP" means both of the following:

  1. Each ViV_iVi​ is measurable.
  2. For every permutation σ\sigmaσ of {1,…,m}\{1,\dots,m\}{1,…,m}, the image measure of PPP under ω↦(Vσ(1)(ω),…,Vσ(m)(ω))\omega\mapsto (V_{\sigma(1)}(\omega),\dots,V_{\sigma(m)}(\omega))ω↦(Vσ(1)​(ω),…,Vσ(m)​(ω)) equals the image measure of PPP under ω↦(V1(ω),…,Vm(ω))\omega\mapsto (V_1(\omega),\dots,V_m(\omega))ω↦(V1​(ω),…,Vm​(ω)). Both are measures on EmE^mEm with the product σ\sigmaσ-algebra.

When m≤1m\le 1m≤1, condition 2 is automatic.

conformalBand. The inputs are:

  • dimensions d,n≥0d,n\ge 0d,n≥0;
  • an arbitrary score function SSS on data points in Rd×R\mathbb R^d\times\mathbb RRd×R;
  • a real number α\alphaα, which is not restricted to (0,1)(0,1)(0,1);
  • training points Z1,…,Zn∈Rd×RZ_1,\dots,Z_n\in\mathbb R^d\times\mathbb RZ1​,…,Zn​∈Rd×R;
  • a point x∈Rdx\in\mathbb R^dx∈Rd.

For each y∈Ry\in\mathbb Ry∈R, let V1(x,y),…,Vn+1(x,y)V^{(x,y)}_1,\dots,V^{(x,y)}_{n+1}V1(x,y)​,…,Vn+1(x,y)​ be the scores of the n+1n+1n+1 points (Z1,…,Zn,(x,y))(Z_1,\dots,Z_n,(x,y))(Z1​,…,Zn​,(x,y)). That is, for i≤ni\le ni≤n,

Vi(x,y)=S(Zi, { ⁣{Zj:j≤n, j≠i} ⁣}∪{ ⁣{(x,y)} ⁣}),Vn+1(x,y)=S((x,y), { ⁣{Z1,…,Zn} ⁣}).V^{(x,y)}_i = S\bigl(Z_i,\ \{\!\{Z_j : j\le n,\ j\ne i\}\!\}\cup\{\!\{(x,y)\}\!\}\bigr), \qquad V^{(x,y)}_{n+1} = S\bigl((x,y),\ \{\!\{Z_1,\dots,Z_n\}\!\}\bigr).Vi(x,y)​=S(Zi​, {{Zj​:j≤n, j=i}}∪{{(x,y)}}),Vn+1(x,y)​=S((x,y), {{Z1​,…,Zn​}}).

The band is the set of y∈Ry\in\mathbb Ry∈R with

Vn+1(x,y)  ≤  Quantile(1−α; 1n+1∑i=1nδVi(x,y)+1n+1δ+∞),V^{(x,y)}_{n+1} \;\le\; \mathrm{Quantile}\Bigl(1-\alpha;\ \tfrac{1}{n+1}\textstyle\sum_{i=1}^{n}\delta_{V^{(x,y)}_i}+\tfrac{1}{n+1}\delta_{+\infty}\Bigr),Vn+1(x,y)​≤Quantile(1−α; n+11​∑i=1n​δVi(x,y)​​+n+11​δ+∞​),

with the comparison made in [−∞,+∞][-\infty,+\infty][−∞,+∞].

  • If α≥1\alpha\ge 1α≥1, the quantile is −∞-\infty−∞ and the band is empty.
  • If α<0\alpha<0α<0, the quantile is +∞+\infty+∞ and the band is all of R\mathbb RR.
  • If α∈(0,1)\alpha\in(0,1)α∈(0,1) but (1−α)(n+1)>n(1-\alpha)(n+1)>n(1−α)(n+1)>n, the quantile is +∞+\infty+∞ and the band is all of R\mathbb RR. This includes every α∈(0,1)\alpha\in(0,1)α∈(0,1) when n=0n=0n=0.

covShiftWeights. The inputs are:

  • an arbitrary real-valued function www on Rd\mathbb R^dRd, which may be negative and need not be measurable;
  • covariates X1,…,Xn∈RdX_1,\dots,X_n\in\mathbb R^dX1​,…,Xn​∈Rd;
  • a point x∈Rdx\in\mathbb R^dx∈Rd.

Write D=∑j=1nw(Xj)+w(x)D = \sum_{j=1}^n w(X_j)+w(x)D=∑j=1n​w(Xj​)+w(x). The n+1n+1n+1 real weights are

piw(x)=w(Xi)D(i≤n),pn+1w(x)=w(x)D.p^w_i(x) = \frac{w(X_i)}{D}\quad (i\le n), \qquad p^w_{n+1}(x) = \frac{w(x)}{D}.piw​(x)=Dw(Xi​)​(i≤n),pn+1w​(x)=Dw(x)​.
  • If D=0D=0D=0, division by zero is defined to give 000, so all weights equal 000.
  • If www takes negative values, DDD and the individual weights can be negative.

covShiftBand. The inputs are d,n,S,α,Z1,…,Zn,xd,n,S,\alpha,Z_1,\dots,Z_n,xd,n,S,α,Z1​,…,Zn​,x as in conformalBand, together with an arbitrary real function www on Rd\mathbb R^dRd. Let Vi(x,y)V^{(x,y)}_iVi(x,y)​ be the scores defined in conformalBand. Let piw(x)p^w_i(x)piw​(x) be the weights of covShiftWeights, computed from the covariates XiX_iXi​ (the first components of the ZiZ_iZi​) and xxx; these weights do not depend on yyy. The band is the set of y∈Ry\in\mathbb Ry∈R with

Vn+1(x,y)  ≤  Quantile(1−α; ∑i=1nmax⁡(piw(x),0) δVi(x,y)+max⁡(pn+1w(x),0) δ+∞).V^{(x,y)}_{n+1}\;\le\;\mathrm{Quantile}\Bigl(1-\alpha;\ \textstyle\sum_{i=1}^{n}\max\bigl(p^w_i(x),0\bigr)\,\delta_{V^{(x,y)}_i}+\max\bigl(p^w_{n+1}(x),0\bigr)\,\delta_{+\infty}\Bigr).Vn+1(x,y)​≤Quantile(1−α; ∑i=1n​max(piw​(x),0)δVi(x,y)​​+max(pn+1w​(x),0)δ+∞​).

Because negative weights are truncated, the measure's total mass need not be 111.

  • If α≥1\alpha\ge1α≥1, the band is empty.
  • If ∑jw(Xj)+w(x)=0\sum_j w(X_j)+w(x)=0∑j​w(Xj​)+w(x)=0, the measure is zero. The quantile is then +∞+\infty+∞ whenever 1−α>01-\alpha>01−α>0, so the band is all of R\mathbb RR.

IsWeightedExchangeable. Let PPP be any measure on Ω\OmegaΩ, not required to be a probability measure. Let Z1,…,Zm:Ω→EZ_1,\dots,Z_m:\Omega\to EZ1​,…,Zm​:Ω→E be maps, and w1,…,wm:E→Rw_1,\dots,w_m:E\to\mathbb Rw1​,…,wm​:E→R functions. The statement "the ZiZ_iZi​ are weighted exchangeable with weights wiw_iwi​" means all of the following:

  1. Each ZiZ_iZi​ is measurable.
  2. Each wiw_iwi​ is measurable and satisfies wi≥0w_i\ge0wi​≥0 everywhere.
  3. There exist a measure ν\nuν on EmE^mEm (product σ\sigmaσ-algebra) and functions f,g:Em→[0,∞]f,g:E^m\to[0,\infty]f,g:Em→[0,∞] such that:
    • ν\nuν is invariant under every coordinate permutation: the image of ν\nuν under z↦(zσ(1),…,zσ(m))z\mapsto(z_{\sigma(1)},\dots,z_{\sigma(m)})z↦(zσ(1)​,…,zσ(m)​) is ν\nuν;
    • fff is measurable;
    • the image of PPP under ω↦(Z1(ω),…,Zm(ω))\omega\mapsto(Z_1(\omega),\dots,Z_m(\omega))ω↦(Z1​(ω),…,Zm​(ω)) equals the measure with density fff with respect to ν\nuν;
    • for every z∈Emz\in E^mz∈Em (pointwise, not almost everywhere), f(z)=∏i=1mwi(zi)⋅g(z)f(z)=\prod_{i=1}^m w_i(z_i)\cdot g(z)f(z)=∏i=1m​wi​(zi​)⋅g(z), using the convention 0⋅∞=00\cdot\infty=00⋅∞=0;
    • for every permutation σ\sigmaσ and every zzz, g(zσ(1),…,zσ(m))=g(z)g(z_{\sigma(1)},\dots,z_{\sigma(m)})=g(z)g(zσ(1)​,…,zσ(m)​)=g(z).

ggg is not required to be measurable. Some degenerate cases:

  • If PPP is the zero measure, the conditions are satisfied with ν=0\nu=0ν=0, provided the measurability and nonnegativity conditions on the ZiZ_iZi​ and wiw_iwi​ hold.
  • If some wiw_iwi​ is identically 000, then f=0f=0f=0, so the joint law must be the zero measure.

permWeight. Let EEE be any type, n≥0n\ge 0n≥0, and let w1,…,wn+1:E→Rw_1,\dots,w_{n+1}:E\to\mathbb Rw1​,…,wn+1​:E→R be arbitrary real functions, which may be negative. For points z1,…,zn+1∈Ez_1,\dots,z_{n+1}\in Ez1​,…,zn+1​∈E and an index i∈{1,…,n+1}i\in\{1,\dots,n+1\}i∈{1,…,n+1},

piw(z1,…,zn+1)=∑σ: σ(n+1)=i ∏j=1n+1wj(zσ(j))∑σ ∏j=1n+1wj(zσ(j)).p^w_i(z_1,\dots,z_{n+1})=\frac{\displaystyle\sum_{\sigma:\ \sigma(n+1)=i}\ \prod_{j=1}^{n+1} w_j\bigl(z_{\sigma(j)}\bigr)}{\displaystyle\sum_{\sigma}\ \prod_{j=1}^{n+1} w_j\bigl(z_{\sigma(j)}\bigr)}.piw​(z1​,…,zn+1​)=σ∑​ j=1∏n+1​wj​(zσ(j)​)σ: σ(n+1)=i∑​ j=1∏n+1​wj​(zσ(j)​)​.

Both sums run over permutations σ\sigmaσ of {1,…,n+1}\{1,\dots,n+1\}{1,…,n+1}. If the denominator is 000, the value is 000 for every iii.

weightedConformalBand. The inputs are d,n,S,α,Z1,…,Zn,xd,n,S,\alpha,Z_1,\dots,Z_n,xd,n,S,α,Z1​,…,Zn​,x as in conformalBand, together with n+1n+1n+1 arbitrary real functions w1,…,wn+1w_1,\dots,w_{n+1}w1​,…,wn+1​ on Rd×R\mathbb R^d\times\mathbb RRd×R. For each yyy:

  • let Vi(x,y)V^{(x,y)}_iVi(x,y)​ be the scores defined in conformalBand;
  • let pi=piw(Z1,…,Zn,(x,y))p_i = p^w_i\bigl(Z_1,\dots,Z_n,(x,y)\bigr)pi​=piw​(Z1​,…,Zn​,(x,y)) be the weights of permWeight; these weights depend on yyy.

The band is the set of y∈Ry\in\mathbb Ry∈R with

Vn+1(x,y)  ≤  Quantile(1−α; ∑i=1nmax⁡(pi,0) δVi(x,y)+max⁡(pn+1,0) δ+∞).V^{(x,y)}_{n+1}\;\le\;\mathrm{Quantile}\Bigl(1-\alpha;\ \textstyle\sum_{i=1}^{n}\max(p_i,0)\,\delta_{V^{(x,y)}_i}+\max(p_{n+1},0)\,\delta_{+\infty}\Bigr).Vn+1(x,y)​≤Quantile(1−α; ∑i=1n​max(pi​,0)δVi(x,y)​​+max(pn+1​,0)δ+∞​).

Negative weights are truncated to 000, and the total mass need not be 111.

  • If α≥1\alpha\ge1α≥1, the band is empty.
  • If the permutation sum in the denominator vanishes at some yyy and 1−α>01-\alpha>01−α>0, the measure there is zero and its quantile is +∞+\infty+∞, so that yyy belongs to the band.
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