Conformal risk control: exchangeable monotone losses and the calibrated parameter
DefinitionConformalRiskControl_BasicObjects of Sections 1.1, 2, 4 and Appendix A of Angelopoulos, Bates, Fisch, Lei and Schuster, Conformal Risk Control (arXiv:2208.02814v4).
- Parameter set and losses. The parameter set is a nonempty compact set with . A loss is a function . It is right-continuous if at every it is continuous within . It satisfies eq. (5) if and .
- Exchangeable and i.i.d. random losses. Random losses on a probability space are exchangeable if every is a random variable and the joint law of , as random elements of with the product -algebra, is invariant under every permutation of the indices. They are i.i.d. if they are independent and identically distributed as random elements of .
- Expectation. For a random variable almost surely, , where is a (lower) Lebesgue integral of a nonnegative function. This is the usual expectation, and equals when .
- Conformal risk control (eq. 4). With ,
and when the set is empty. The same convention (infimum of the empty set is ) is used for every parameter below. 5. Jump function (Section 2.2). . 6. Monotonization. (Corollary 1 and Appendix A). Appendix A sets and . 7. Covariate shift (Section 4.1). With weights , . The total variation distance is over measurable sets . 8. Quantiles (Section 4.2). , and . 9. U-risk (Section 4.5). For -subsets with losses ,
The family is exchangeable if its joint law is invariant under every relabelling of , acting by .
Formalization Note Losses are functions of which only the values on are used; exchangeability and independence concern the restrictions to . The paper does not give a topology for . Compactness is assumed so that every infimum above lies in , as the proofs require. The expectation is written as in the extended reals because the losses of Theorem 1 are only bounded above. Losses are indexed from : the calibration losses are and the test loss is .
import Mathlib
/-!
Angelopoulos, Bates, Fisch, Lei and Schuster, *Conformal Risk Control*, arXiv:2208.02814v4:
the parameter set, random loss functions, exchangeability, the conformal risk control parameter
`λ̂` of eq. (4), the jump function of Section 2.2, and the modified parameters of Corollary 1,
Section 4 (Propositions 2, 3, 4, 7) and Appendix A (Theorem A.1).
-/
open MeasureTheory ProbabilityTheory
noncomputable section
namespace ConformalRiskControl
variable {Ω : Type*} [MeasurableSpace Ω]
/-- Section 1.1, p. 2: `f` is right-continuous on the parameter set `Λ ⊆ ℝ`, i.e. at every
`x ∈ Λ` it is continuous within `Λ ∩ [x, ∞)`. -/
def IsRightContOn (Λ : Set ℝ) (f : ℝ → ℝ) : Prop :=
∀ x ∈ Λ, ContinuousWithinAt f (Λ ∩ Set.Ici x) x
/-- Eq. (5), p. 3: `L(λ_max) ≤ α` and `sup_{λ ∈ Λ} L(λ) ≤ B`, with `λ_max = sup Λ`. -/
def LossBounds (Λ : Set ℝ) (B α : ℝ) (f : ℝ → ℝ) : Prop :=
(∀ x ∈ Λ, f x ≤ B) ∧ f (sSup Λ) ≤ α
/-- Section 1.1, p. 2: the random functions `L_i : Λ → ℝ` form an exchangeable collection:
each `L_i(λ)` is a random variable, and the joint law of `(L_i)_i`, as random elements of
`Λ → ℝ` with the product σ-algebra, is invariant under every permutation of the indices. -/
def IsExchangeable {ι : Type*} (P : Measure Ω) (Λ : Set ℝ) (L : ι → Ω → ℝ → ℝ) : Prop :=
(∀ i, ∀ x ∈ Λ, Measurable fun ω => L i ω x) ∧
∀ σ : Equiv.Perm ι,
P.map (fun ω i (x : Λ) => L (σ i) ω x) = P.map (fun ω i (x : Λ) => L i ω x)
/-- Theorem 2, p. 4 and Theorem A.1, p. 15: the random functions `L_i : Λ → ℝ` are i.i.d. as
random elements of `Λ → ℝ` with the product σ-algebra. -/
def IsIID {ι : Type*} (P : Measure Ω) (Λ : Set ℝ) (L : ι → Ω → ℝ → ℝ) : Prop :=
(∀ i, ∀ x ∈ Λ, Measurable fun ω => L i ω x) ∧
iIndepFun (fun i ω (x : Λ) => L i ω x) P ∧
∀ i j, IdentDistrib (fun ω (x : Λ) => L i ω x) (fun ω (x : Λ) => L j ω x) P P
/-- The expectation `E[X] ∈ [-∞, B]` of a random variable bounded above by `B`, written as
`B - E[B - X]` with `E[B - X] ∈ [0, ∞]` a lower Lebesgue integral. For `X ≤ B` almost surely and
`P` a probability measure it is the usual expectation, and equals `-∞` when `E[X⁻] = ∞`; it does
not depend on the choice of the upper bound `B`. -/
def upperExp (P : Measure Ω) (B : ℝ) (X : Ω → ℝ) : EReal :=
(B : EReal) - ((∫⁻ ω, ENNReal.ofReal (B - X ω) ∂P : ENNReal) : EReal)
/-- Section 1.1, p. 2: the empirical risk `R̂_m(λ) = (L_1(λ) + ⋯ + L_m(λ)) / m`. -/
def empRisk {m : ℕ} (Ls : Fin m → ℝ → ℝ) (x : ℝ) : ℝ :=
(∑ i, Ls i x) / m
open Classical in
/-- Eq. (4), p. 2: the infimum of a set of parameters, with the convention that the infimum of
the empty set is `λ_max = sup Λ`. -/
def infOrMax (Λ S : Set ℝ) : ℝ :=
if S.Nonempty then sInf S else sSup Λ
/-- Eq. (4), p. 2: the conformal risk control parameter
`λ̂ = inf {λ ∈ Λ : n/(n+1) · R̂_n(λ) + B/(n+1) ≤ α}`, equal to `λ_max` when the set is empty;
`Ls` are the calibration losses `L_1, …, L_n`. -/
def lhat (Λ : Set ℝ) (B α : ℝ) {n : ℕ} (Ls : Fin n → ℝ → ℝ) : ℝ :=
infOrMax Λ {x ∈ Λ | (n : ℝ) / (n + 1) * empRisk Ls x + B / (n + 1) ≤ α}
/-- Section 2.2, p. 4: the jump function `J(l, λ) = lim_{ε → 0⁺} l(λ - ε) - l(λ)`. -/
def jump (l : ℝ → ℝ) (x : ℝ) : ℝ :=
Function.leftLim l x - l x
/-- Corollary 1, p. 5 and Appendix A, p. 15: the monotonization `sup_{λ' ∈ Λ, λ' ≥ λ} f(λ')`. -/
def supRight (Λ : Set ℝ) (f : ℝ → ℝ) (x : ℝ) : ℝ :=
⨆ t : {t : ℝ // t ∈ Λ ∧ x ≤ t}, f t
/-- Section 4.1, p. 10: the weighted parameter under covariate shift,
`λ̂(x) = inf {λ : (∑_i w(X_i) L_i(λ) + w(x) B) / (∑_i w(X_i) + w(x)) ≤ α}`, equal to `max Λ`
when the set is empty; `Ls` and `Xs` are the calibration losses and covariates. -/
def weightedLhat {X : Type*} (Λ : Set ℝ) (B α : ℝ) (w : X → ℝ) {n : ℕ}
(Ls : Fin n → ℝ → ℝ) (Xs : Fin n → X) (x : X) : ℝ :=
infOrMax Λ {t ∈ Λ | (∑ i, w (Xs i) * Ls i t + w x * B) / (∑ i, w (Xs i) + w x) ≤ α}
/-- Section 4.1, p. 10: total variation distance `TV(μ, ν) = sup_A |μ(A) - ν(A)|` over
measurable sets `A`. -/
def tvDist {Z : Type*} [MeasurableSpace Z] (μ ν : Measure Z) : ℝ :=
sSup {r | ∃ A : Set Z, MeasurableSet A ∧ r = |(μ A).toReal - (ν A).toReal|}
/-- Section 4.2, p. 11: `Quantile_β(X) = inf {x : P(X ≤ x) ≥ β}`. -/
def quantile (P : Measure Ω) (X : Ω → ℝ) (β : ℝ) : ℝ :=
sInf {x : ℝ | β ≤ (P {ω | X ω ≤ x}).toReal}
open Classical in
/-- Section 4.2, p. 11: `λ̂_β = inf {λ ∈ Λ : (1/(n+1)) ∑_{i ≤ n} 1{L_i(λ) > α} + 1/(n+1) ≤ 1 - β}`,
equal to `λ_max` when the set is empty. -/
def lhatQuantile (Λ : Set ℝ) (α β : ℝ) {n : ℕ} (Ls : Fin n → ℝ → ℝ) : ℝ :=
infOrMax Λ {x ∈ Λ |
1 / ((n : ℝ) + 1) * ∑ i, (if α < Ls i x then (1 : ℝ) else 0) + 1 / ((n : ℝ) + 1) ≤ 1 - β}
/-- Section 4.5, p. 12: the `k`-subsets `S ⊆ {1, …, n+k}` are indexed by finsets of `Fin (n+k)`;
the calibration subsets are those contained in the first `n` indices. -/
def calibSubsets (n k : ℕ) : Finset (Finset (Fin (n + k))) :=
(Finset.univ.filter fun i : Fin (n + k) => i.val < n).powersetCard k
/-- Section 4.5, p. 12: the test subset `{n+1, …, n+k}`, i.e. the last `k` indices. -/
def testSubset (n k : ℕ) : Finset (Fin (n + k)) :=
Finset.univ.filter fun i : Fin (n + k) => n ≤ i.val
/-- Eq. (16), p. 12: `λ̂_k = inf {λ : k! n!/(n+k)! ∑_{S ⊆ {1..n}, |S| = k} L_S(λ)
+ B (1 - (n!)² / ((n+k)! (n-k)!)) ≤ α}`, equal to `λ_max` when the set is empty. -/
def lhatU (Λ : Set ℝ) (B α : ℝ) (n k : ℕ) (L : Finset (Fin (n + k)) → ℝ → ℝ) : ℝ :=
infOrMax Λ {x ∈ Λ |
((k.factorial * n.factorial : ℕ) : ℝ) / ((n + k).factorial : ℝ) *
∑ S ∈ calibSubsets n k, L S x +
B * (1 - ((n.factorial ^ 2 : ℕ) : ℝ) / (((n + k).factorial * (n - k).factorial : ℕ) : ℝ))
≤ α}
/-- Section 4.5, p. 12: the collection `(L_S)_{|S| = k}` is exchangeable under relabelling of the
`n + k` data points: each `L_S(λ)` is a random variable, and for every permutation `σ` of
`{1, …, n+k}` the joint law of `(L_{σ(S)})_S` equals that of `(L_S)_S`. -/
def IsExchangeableU (P : Measure Ω) (Λ : Set ℝ) (n k : ℕ)
(L : Finset (Fin (n + k)) → Ω → ℝ → ℝ) : Prop :=
(∀ S : Finset (Fin (n + k)), S.card = k → ∀ x ∈ Λ, Measurable fun ω => L S ω x) ∧
∀ σ : Equiv.Perm (Fin (n + k)),
P.map (fun ω (S : {S : Finset (Fin (n + k)) // S.card = k}) (x : Λ) =>
L (S.1.map σ.toEmbedding) ω x) =
P.map (fun ω (S : {S : Finset (Fin (n + k)) // S.card = k}) (x : Λ) => L S.1 ω x)
/-- Appendix A, p. 15 (unnumbered displays before Theorem A.1):`λ̂⁺_n = inf {λ ∈ Λ : R̂⁺_n(λ) ≤ α}` with the monotonized
empirical risk `R̂⁺_n(λ) = sup_{t ≥ λ} R̂_n(t)`, equal to `λ_max` when the set is empty. -/
def lhatPlus (Λ : Set ℝ) (α : ℝ) {n : ℕ} (Ls : Fin n → ℝ → ℝ) : ℝ :=
infOrMax Λ {x ∈ Λ | supRight Λ (empRisk Ls) x ≤ α}
end ConformalRiskControl
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
. The inputs are a set and a function defined on all of . The property holds when, for every , is continuous at relative to the set :
If no points of other than approach from the right, the condition at holds automatically. If , the property holds vacuously.
. The inputs are a set , reals and , and a function . The property is the conjunction of two conditions:
- for every ;
- , where .
Here follows the real-number convention: it equals when is empty or not bounded above. The point need not belong to , so condition 1 need not cover it. Nothing relates to , and nothing requires to be nonempty or bounded.
. The inputs are:
- a measurable space with an arbitrary measure (not required to be a probability measure);
- an arbitrary index type , possibly infinite;
- a set ;
- a family for , , .
The property is the conjunction of:
- For every and every , the map is measurable.
- For every bijection (not only finitely supported ones), two pushforward measures coincide. Both live on the space of families of functions , with the product σ-algebra. The first is the pushforward of under . The second is the pushforward under .
Only the values of at parameters in enter the condition.
. The setting is the same as for . The property is the conjunction of:
- For every and every , the map is measurable.
- The random elements , valued in with the product σ-algebra, are mutually independent under . That is, for every finite set of indices and every choice of measurable events, of the intersection equals the product of the probabilities. Applied to the empty set of indices, this condition forces .
- For every pair , the random elements and are identically distributed under : both are almost-everywhere measurable and their pushforward laws on are equal.
. The inputs are a measure on (arbitrary), a real , and a function (not assumed measurable). The result is the extended real
Here is the lower Lebesgue integral of a function valued in .
- If that integral is , the value is .
- Wherever , the integrand is truncated to , so such points contribute nothing.
- The formula involves explicitly, and no hypothesis such as or is imposed.
. The inputs are and functions . At , the value is
When this is , which is defined to be .
. The inputs are sets , with no relation required between them. The value is if is nonempty, and if is empty. The real-number conventions apply:
- when is nonempty but not bounded below;
- when is empty or not bounded above.
. The inputs are , reals and , , and functions . The value is
where is the empirical risk defined above and the infimum/supremum conventions of apply.
- When , the defining condition reduces to . So if , and otherwise.
- No monotonicity, boundedness, or other property of the is assumed.
. The inputs are a function and a point . The value is , where
- is the left limit if it exists;
- if the left limit does not exist, is taken to be , so the jump is .
The left limit uses the values of on all of , with no restriction to any parameter set.
. The inputs are , , and . The value is
By the real-number convention, this supremum equals if the set is empty (no with ) or if the values are unbounded above.
. The inputs are:
- an arbitrary type and a weight function , with no sign or positivity condition;
- and reals and ;
- , functions , points , and a point .
The value is if and otherwise (conventions of ), where
If the denominator is , the quotient is and the condition becomes .
. The inputs are two arbitrary measures on a measurable space . The value is
with two real-valued conventions:
- each measure value , is replaced by when it is ;
- the supremum is taken in , so it equals if these differences are unbounded.
No factor appears.
. The inputs are a measure on (arbitrary), a function (not assumed measurable), and a real . The value is
The following conventions apply:
- A measure value of is read as .
- If the set is empty (e.g. for a probability measure), the infimum is .
- If the set is not bounded below (e.g. ), the infimum is also .
. The inputs are , reals and , , and functions . The value is if and otherwise (conventions of ), where
The indicator uses the strict inequality . When , the condition is , i.e. .
. The inputs are . Label the index set . The value is the collection of all -element subsets of the first indices . This collection is empty when , and equals when .
. The inputs are , with the same labelling of . The value is the set of the last indices, . It is empty when .
. The inputs are:
- , reals and , and ;
- a function assigned to every subset , of any size.
The value is if and otherwise (conventions of ), where
Here is truncated subtraction: it is when , so in that case. Two cases are worth noting:
- When , the sum is empty and equals .
- When , the condition reduces to .
. The inputs are:
- a measure on (arbitrary);
- and ;
- for every subset , a function of and .
The property is the conjunction of:
- For every with and every , the map is measurable.
- For every permutation of , two pushforward measures coincide. Both live on the space of families indexed by the -element subsets of functions , with the product σ-algebra. The first is the pushforward of under , where is the image set of . The second is the pushforward under .
Subsets of size other than , and parameters outside , play no role.
. The inputs are , a real , , and functions . The value is if and otherwise (conventions of ), where
Here is the empirical risk defined above, and the supremum is defined to be when the values are unbounded above. When , , so and the condition becomes .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.