Empirical quantiles, exchangeability and the split CQR interval
DefinitionConformalizedQuantileRegression_BasicObjects of Sections 1 and 4 and Appendix A of Romano, Patterson and Candès (2019).
- Data. A sample is a pair . The training samples are and the test sample is .
- Empirical CDF and quantile (Appendix A). For real values , , with a finite index set, and , taken in the extended reals . In particular when . The order statistic is the -th smallest of the values, counted with multiplicity.
- Exchangeability. Random variables on a probability space are exchangeable if they are measurable and has the same joint law as for every permutation .
- Algorithm 1. The index set is split into a proper training set and a calibration set . A quantile regression algorithm maps the proper training data to two functions . It is called measurable if and are jointly measurable.
- Conformity score (eq. 9). .
- CQR interval (eqs. 10-11). is the -th empirical quantile of , and .
- Asymmetric interval (Theorem 2). With the one-sided scores and and levels , .
Formalization Note is written as functions with the product σ-algebra (the Borel σ-algebra of ). Empirical quantiles take values in the extended reals so that a level above gives and the interval is then all of ; this is the convention under which the paper's formulas (10)-(11) and Lemma 2 make sense when the inflated level exceeds . The fitted functions are a function of the proper training data only, which is how Algorithm 1 uses . The levels of the asymmetric interval are arguments, so the paper's printed choice and the inflated choice used by its proof are both expressible.
import Mathlib
/-!
Romano, Patterson and Candès, *Conformalized Quantile Regression*, arXiv:1905.03222v1 (2019):
empirical distribution functions, empirical quantiles and order statistics (Appendix A),
exchangeability (Section 1), the split of the data and the quantile regression algorithm
(Section 4, Algorithm 1), the conformity scores (eq. (9)), the conformalized prediction
interval (eqs. (10)-(11)), and the one-sided scores and asymmetric interval of Theorem 2.
-/
open MeasureTheory
noncomputable section
namespace ConformalizedQuantileRegression
/-- Section 1, p. 2: a feature vector `x ∈ ℝ^p`. -/
abbrev Feat (p : ℕ) : Type := Fin p → ℝ
/-- Section 1, p. 2 and Algorithm 1: a sample `(x, y) ∈ ℝ^p × ℝ`. -/
abbrev Sample (p : ℕ) : Type := Feat p × ℝ
/-- Appendix A, p. 18: the empirical CDF `F̂(y) = (1/|s|) ∑_{i ∈ s} 1{z_i ≤ y}` of the values
`z_i`, `i ∈ s` (for `s = {1, …, n}` this is `F̂_n`). -/
def empCDF {ι : Type*} (s : Finset ι) (z : ι → ℝ) (y : ℝ) : ℝ :=
((s.filter fun i => z i ≤ y).card : ℝ) / (s.card : ℝ)
/-- Appendix A, p. 18: the empirical quantile `Q̂(β) = inf {y ∈ ℝ : β ≤ F̂(y)}` of the values
`z_i`, `i ∈ s`, taken in the extended reals; it is `+∞` when the set is empty (level `β > 1`),
the convention needed by eqs. (10)-(11) and Lemma 2 when the inflated level exceeds `1`. -/
def empQuantile {ι : Type*} (s : Finset ι) (z : ι → ℝ) (β : ℝ) : EReal :=
⨅ (y : ℝ) (_ : β ≤ empCDF s z y), (y : EReal)
/-- Appendix A, p. 18: the order statistic `Z_(k)`, the `k`-th smallest of the values `z_i`,
`i ∈ s`, counted with multiplicity (used only for `1 ≤ k ≤ |s|`). -/
def orderStat {ι : Type*} (s : Finset ι) (z : ι → ℝ) (k : ℕ) : ℝ :=
((s.val.map z).sort (· ≤ ·)).getD (k - 1) 0
/-- Section 1, p. 2 and Appendix A: the random variables `V_1, …, V_m` (measurable maps on a
probability space) are exchangeable: `(V_{σ(1)}, …, V_{σ(m)})` and `(V_1, …, V_m)` have the
same joint law for every permutation `σ`. -/
def IsExchangeable {Ω E : Type*} [MeasurableSpace Ω] [MeasurableSpace E] {m : ℕ}
(P : Measure Ω) (V : Fin m → Ω → E) : Prop :=
(∀ i, Measurable (V i)) ∧
∀ σ : Equiv.Perm (Fin m),
Measure.map (fun ω i => V (σ i) ω) P = Measure.map (fun ω i => V i ω) P
/-- Section 4, p. 4 and Algorithm 1: a quantile regression algorithm `A` maps the proper
training set `{(X_i, Y_i) : i ∈ I₁}` to the pair `(q̂_lo, q̂_hi)` of fitted lower and upper
conditional quantile functions. -/
abbrev QRAlgorithm (p : ℕ) {n : ℕ} (I₁ : Finset (Fin n)) : Type :=
(↥I₁ → Sample p) → (Feat p → ℝ) × (Feat p → ℝ)
/-- Standing measurability convention for Algorithm 1: the maps
`(training set, x) ↦ q̂_lo(x)` and `(training set, x) ↦ q̂_hi(x)` are measurable. -/
def IsMeasurableAlgorithm {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) : Prop :=
Measurable (fun q : (↥I₁ → Sample p) × Feat p => (A q.1).1 q.2) ∧
Measurable (fun q : (↥I₁ → Sample p) × Feat p => (A q.1).2 q.2)
/-- Algorithm 1, p. 5: `{q̂_lo, q̂_hi} ← A({(X_i, Y_i) : i ∈ I₁})`, the quantile functions fitted
on the proper training set of the data `d = ((X_1, Y_1), …, (X_n, Y_n))`. -/
def fitQR {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) (d : Fin n → Sample p) :
(Feat p → ℝ) × (Feat p → ℝ) :=
A (fun i => d i.1)
/-- Eq. (9), p. 4: the conformity score `E = max {q̂_lo(X) - Y, Y - q̂_hi(X)}` of a sample
`(X, Y)`. -/
def conformityScore {p : ℕ} (qlo qhi : Feat p → ℝ) (z : Sample p) : ℝ :=
max (qlo z.1 - z.2) (z.2 - qhi z.1)
/-- Eq. (11), p. 5: `Q_{1-α}(E, I₂)`, the `(1 - α)(1 + 1/|I₂|)`-th empirical quantile of the
conformity scores `{E_i : i ∈ I₂}` of the calibration set. -/
def cqrCorrection {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) (I₂ : Finset (Fin n))
(α : ℝ) (d : Fin n → Sample p) : EReal :=
empQuantile I₂ (fun i => conformityScore (fitQR A d).1 (fitQR A d).2 (d i))
((1 - α) * (1 + 1 / (I₂.card : ℝ)))
/-- Eq. (10), p. 5 and Algorithm 1: the CQR prediction interval
`C(x) = [q̂_lo(x) - Q_{1-α}(E, I₂), q̂_hi(x) + Q_{1-α}(E, I₂)]`, as a set of reals (all of `ℝ`
when `Q_{1-α}(E, I₂) = +∞`). -/
def cqrInterval {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) (I₂ : Finset (Fin n))
(α : ℝ) (d : Fin n → Sample p) (x : Feat p) : Set ℝ :=
{y | ((fitQR A d).1 x : EReal) - cqrCorrection A I₂ α d ≤ (y : EReal) ∧
(y : EReal) ≤ ((fitQR A d).2 x : EReal) + cqrCorrection A I₂ α d}
/-- Theorem 2, p. 7: the lower one-sided score `q̂_lo(X) - Y` of a sample `(X, Y)`. -/
def loScore {p : ℕ} (qlo : Feat p → ℝ) (z : Sample p) : ℝ := qlo z.1 - z.2
/-- Theorem 2, p. 7: the upper one-sided score `Y - q̂_hi(X)` of a sample `(X, Y)`. -/
def hiScore {p : ℕ} (qhi : Feat p → ℝ) (z : Sample p) : ℝ := z.2 - qhi z.1
/-- Theorem 2, p. 7: `Q_β(E_lo, I₂)`, the `β`-th empirical quantile of
`{q̂_lo(X_i) - Y_i : i ∈ I₂}` (the paper uses the level `β = 1 - α_lo`). -/
def loCorrection {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) (I₂ : Finset (Fin n))
(β : ℝ) (d : Fin n → Sample p) : EReal :=
empQuantile I₂ (fun i => loScore (fitQR A d).1 (d i)) β
/-- Theorem 2, p. 7: `Q_β(E_hi, I₂)`, the `β`-th empirical quantile of
`{Y_i - q̂_hi(X_i) : i ∈ I₂}` (the paper uses the level `β = 1 - α_hi`). -/
def hiCorrection {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) (I₂ : Finset (Fin n))
(β : ℝ) (d : Fin n → Sample p) : EReal :=
empQuantile I₂ (fun i => hiScore (fitQR A d).2 (d i)) β
/-- Theorem 2, p. 7: the asymmetric prediction interval
`C(x) = [q̂_lo(x) - Q_{βlo}(E_lo, I₂), q̂_hi(x) + Q_{βhi}(E_hi, I₂)]` with separate levels for the
two tails. -/
def asymInterval {p n : ℕ} {I₁ : Finset (Fin n)} (A : QRAlgorithm p I₁) (I₂ : Finset (Fin n))
(βlo βhi : ℝ) (d : Fin n → Sample p) (x : Feat p) : Set ℝ :=
{y | ((fitQR A d).1 x : EReal) - loCorrection A I₂ βlo d ≤ (y : EReal) ∧
(y : EReal) ≤ ((fitQR A d).2 x : EReal) + hiCorrection A I₂ βhi d}
end ConformalizedQuantileRegression
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Feat p. For a natural number , is the type of all functions from the -element index set to , i.e. . The case is allowed, and then is a single point (the empty tuple).
Sample p. is the product . A sample is a pair with feature vector and real response .
empCDF. Let be any type, a finite subset of , and a family of reals. For the definition sets
Values of outside are ignored, and repeated values are counted once per index. When , the division by zero is evaluated by the convention , so for every . In all cases .
empQuantile. With , as above and an arbitrary real level (no restriction to ), the definition sets
with the infimum taken in the extended reals. The value depends on and as follows:
- Empty set. If the set of admissible is empty, the value is . This happens whenever , and also when and .
- . Every is admissible (because ), so the value is . This holds whether or not is empty.
- and . The value is a real number: the -th smallest of the values , , counted with multiplicity.
orderStat. With , as above and a natural number , form the list of the values , , with multiplicity, sorted in nondecreasing order. is the entry at zero-based position of this list, and it is if that position does not exist. Natural-number subtraction gives , which produces these edge cases:
- for , , the minimum;
- for , ;
- for , for every .
For it is the usual -th smallest value.
IsExchangeable. Let and be measurable spaces, a natural number, a measure on , and . The predicate holds exactly when both of the following are true:
- each is measurable;
- for every permutation of , the pushforward of under equals the pushforward of under , both as measures on with the product σ-algebra.
is only required to be a measure, not necessarily a probability (or even finite) measure. For or the permutation condition holds trivially.
QRAlgorithm p I₁. Fix and a subset . A "quantile regression algorithm" is any function
It takes a family of samples indexed by and returns a pair of real functions on . Nothing is required of it:
- there is no measurability or quantile-estimation property;
- there is no ordering such as ;
- may be empty.
IsMeasurableAlgorithm. For an algorithm as above, the predicate asserts that both maps
are measurable on . Here , and the domain carries the product of Borel σ-algebras.
fitQR. Given and a full data set with each , the definition sets
That is, is applied to the restriction of to the indices in .
conformityScore. For functions and a sample , the score is
cqrCorrection. Given (on ), a further subset , a real and data , let be the output of fitQR. For the scores are . The correction is the extended-real empirical quantile of these scores:
There are no constraints on the inputs: need not be disjoint from , and is any real number. By the rules for empQuantile:
- the value is whenever the level exceeds ;
- the value is whenever the level is (for instance whenever with );
- if , then is evaluated as , the level becomes , and the value is if and if .
cqrInterval. With the same inputs and a feature vector , write . The interval is the set of real numbers
Both inequalities are evaluated in the extended reals. Depending on , the set is:
- : all of ;
- : empty, because the lower bound becomes and the upper bound ;
- real: the closed interval , which is empty if .
The fitted functions and the correction depend only on . The point enters only through and .
loScore. For and a sample , the score is .
hiScore. For and a sample , the score is .
loCorrection. Given , , an arbitrary real level and data , the correction is
the extended-real empirical quantile at level exactly of the scores , , with fitted on . No finite-sample inflation factor is applied. The value is for (or with ) and for .
hiCorrection. This is the same as loCorrection, but applied to the scores , :
with the same edge cases and no inflation factor.
asymInterval. Given , , two arbitrary real levels , data and , write and . The interval is
with both comparisons in the extended reals. The two corrections act on separate sides:
- : removes the lower bound; makes the set empty.
- : removes the upper bound; makes the set empty.
- Both real: the set is empty whenever the lower bound exceeds the upper bound.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.