Domains, risks, learnability and conditions for OOD detection (Fang et al. 2022)
DefinitionOODLearnable_BasicObjects of Sections 2-5 and Appendices D-J of Fang, Li, Lu, Dong, Han and Liu (NeurIPS 2022).
- Spaces and loss. The feature space is a set with its Borel -algebra and subspace topology. The ID labels are , all OOD data carry the label , and . A loss is with if and only if . A hypothesis space is a set of functions .
- Domains. A domain is given by a probability measure on , a probability measure on and a class prior . is the -marginal of , and is the same domain with prior .
- Risks. , , for , and (eq. 1).
- Learnability (Definitions 1-2). An algorithm maps every sample , , to an element of . OOD detection is learnable in a domain space for if there are an algorithm and a non-increasing sequence with
It is strongly learnable if the same holds with in place of for every , with one algorithm and one sequence. Learnability at rate means that in addition for some constant . 5. Domain spaces. is priori-unknown if whenever and (Definition 3). For any , . The single-distribution space of is , the total space consists of all domains, the separate space of all domains with , and a finite-ID-distribution space is a priori-unknown space containing only finitely many distinct . 6. Conditions. Condition 1 (linear condition): for every and . Condition 2: for all . Condition 3 (compatibility): for every and some satisfies and for every with (Definition 5: such are ID-consistent). 7. Overlap (Definition 4). has overlap if some -finite measure with gives positive mass to , where are the Radon-Nikodym derivatives of with respect to . 8. Assumption 1 and VC dimension. is separate for OOD if every has some with . maps ID labels to and to , , and is the largest size of a finite set on which a binary class realises every labelling ( if unbounded). 9. Finite discrete domains and . A domain is finite discrete if and are concentrated on finite sets. For and , consists of the with if and otherwise, for some , (eq. 4).
Formalization Note The ID label is Fin.castSucc (i-1) in Fin (K+1) and the OOD label is Fin.last K; the binary labels are in Fin 2. A domain is stored as the triple , because and depend on even when . Risks take values in (they are finite because is bounded and the measures are probability measures). In Definitions 1-2 the map (resp. ) is required to be almost-everywhere measurable, so that the expectation is defined. Supports are topological supports in . All infima are taken in .
import Mathlib
/-!
Fang, Li, Lu, Dong, Han and Liu, *Is Out-of-Distribution Detection Learnable?*, NeurIPS 2022,
arXiv:2210.14707v3: domains, risks, learnability, domain spaces, conditions and assumptions of
Sections 2-5 and Appendices D-J.
Conventions. The feature space is a measurable space `X` (in every statement, a subset of
`ℝ^d` with its Borel structure). The label space `𝒴_all = {1, …, K+1}` is `Fin (K + 1)`: the
in-distribution (ID) label `i ∈ {1, …, K}` is `Fin.castSucc (i - 1)` and the out-of-distribution
(OOD) label `K + 1` is `Fin.last K`. ID samples are labelled by `Fin K`.
-/
open MeasureTheory Filter Topology
open scoped ENNReal NNReal
noncomputable section
namespace OODLearnable
variable {X : Type*} [MeasurableSpace X] {K : ℕ}
/-- Section 2, "Loss and Risks": a loss `ℓ : 𝒴_all × 𝒴_all → ℝ_{≥0}` with `ℓ(y₁, y₂) = 0` if and
only if `y₁ = y₂`. -/
def IsOODLoss (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) : Prop :=
∀ y₁ y₂, ℓ y₁ y₂ = 0 ↔ y₁ = y₂
/-- Section 2: a domain `D_XY = (1 - π^out) D_{X_I Y_I} + π^out D_{X_O Y_O}`, recorded as its ID
joint distribution `D_{X_I Y_I}` on `X × {1, …, K}`, its OOD distribution `D_{X_O}` on `X` (all OOD
data carry the label `K + 1`) and its class-prior `π^out ∈ [0, 1)`. -/
structure Domain (X : Type*) [MeasurableSpace X] (K : ℕ) where
/-- The ID joint distribution `D_{X_I Y_I}`. -/
DI : ProbabilityMeasure (X × Fin K)
/-- The OOD marginal distribution `D_{X_O}`. -/
DO : ProbabilityMeasure X
/-- The class-prior probability `π^out ∈ [0, 1)`. -/
piOut : Set.Ico (0 : ℝ) 1
/-- Section 2: the ID marginal distribution `D_{X_I}`. -/
def marginalIn (D : Domain X K) : Measure X :=
(D.DI : Measure (X × Fin K)).map Prod.fst
/-- Definition 3 (Section 3): `D^α_XY = (1 - α) D_{X_I Y_I} + α D_{X_O Y_O}` for `α ∈ [0, 1)`. -/
def withPrior (D : Domain X K) (α : Set.Ico (0 : ℝ) 1) : Domain X K :=
{ D with piOut := α }
/-- Section 2: the ID risk `R^in_D(h) = E_{(x,y)∼D_{X_I Y_I}} ℓ(h(x), y)`. -/
def riskIn (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (D : Domain X K) (h : X → Fin (K + 1)) : ℝ≥0∞ :=
∫⁻ p, (ℓ (h p.1) (Fin.castSucc p.2) : ℝ≥0∞) ∂(D.DI : Measure (X × Fin K))
/-- Section 2: the OOD risk `R^out_D(h) = E_{x∼D_{X_O}} ℓ(h(x), K + 1)`. -/
def riskOut (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (D : Domain X K) (h : X → Fin (K + 1)) :
ℝ≥0∞ :=
∫⁻ x, (ℓ (h x) (Fin.last K) : ℝ≥0∞) ∂(D.DO : Measure X)
/-- Section 2: the `α`-risk `R^α_D(h) = (1 - α) R^in_D(h) + α R^out_D(h)`, `α ∈ [0, 1]`. -/
def alphaRisk (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (D : Domain X K) (α : ℝ)
(h : X → Fin (K + 1)) : ℝ≥0∞ :=
ENNReal.ofReal (1 - α) * riskIn ℓ D h + ENNReal.ofReal α * riskOut ℓ D h
/-- Eq. (1) (Section 2): the risk `R_D(h) = E_{(x,y)∼D_XY} ℓ(h(x), y)`, which equals
`(1 - π^out) R^in_D(h) + π^out R^out_D(h)`. -/
def risk (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (D : Domain X K) (h : X → Fin (K + 1)) : ℝ≥0∞ :=
alphaRisk ℓ D D.piOut h
/-- Definition 1, footnote 2 (Section 2): an algorithm maps a training sample
`S ∈ (X × 𝒴)^n` to a hypothesis function (only sample sizes `n ≥ 1` are used). -/
abbrev Algorithm (X : Type*) (K : ℕ) : Type _ :=
(n : ℕ) → (Fin n → X × Fin K) → X → Fin (K + 1)
/-- Section 2: the law `D^n_{X_I Y_I}` of an i.i.d. training sample of size `n`. -/
def sampleMeasure (D : Domain X K) (n : ℕ) : Measure (Fin n → X × Fin K) :=
Measure.pi fun _ : Fin n => (D.DI : Measure (X × Fin K))
/-- Definition 1 (Section 2), eq. (2): the algorithm `A` takes values in `H` and is consistent
with respect to the domain space `𝒟` with the monotonically decreasing sequence `ε → 0`:
`E_{S∼D^n_{X_I Y_I}} R_D(A(S)) ≤ inf_{h∈H} R_D(h) + ε(n)` for every `D ∈ 𝒟` and `n ≥ 1`. The
expectation is required to be defined, i.e. `S ↦ R_D(A(S))` is a.e.-measurable. -/
def IsConsistent (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (𝒟 : Set (Domain X K))
(H : Set (X → Fin (K + 1))) (A : Algorithm X K) (ε : ℕ → ℝ) : Prop :=
(∀ n, 1 ≤ n → ∀ S, A n S ∈ H) ∧ Antitone ε ∧ Tendsto ε atTop (𝓝 0) ∧
∀ D ∈ 𝒟, ∀ n, 1 ≤ n →
AEMeasurable (fun S => risk ℓ D (A n S)) (sampleMeasure D n) ∧
∫⁻ S, risk ℓ D (A n S) ∂(sampleMeasure D n) ≤
(⨅ h ∈ H, risk ℓ D h) + ENNReal.ofReal (ε n)
/-- Definition 1 (Section 2): OOD detection is learnable in `𝒟` for `H`. -/
def IsLearnable (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (𝒟 : Set (Domain X K))
(H : Set (X → Fin (K + 1))) : Prop :=
∃ (A : Algorithm X K) (ε : ℕ → ℝ), IsConsistent ℓ 𝒟 H A ε
/-- Definition 2 (Section 2), eq. (3): OOD detection is strongly learnable in `𝒟` for `H`: one
algorithm and one sequence `ε` give `E_S R^α_D(A(S)) ≤ inf_{h∈H} R^α_D(h) + ε(n)` for every
`α ∈ [0, 1]`, every `D ∈ 𝒟` and every `n ≥ 1` (each expectation being defined). -/
def IsStronglyLearnable (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (𝒟 : Set (Domain X K))
(H : Set (X → Fin (K + 1))) : Prop :=
∃ (A : Algorithm X K) (ε : ℕ → ℝ),
(∀ n, 1 ≤ n → ∀ S, A n S ∈ H) ∧ Antitone ε ∧ Tendsto ε atTop (𝓝 0) ∧
∀ D ∈ 𝒟, ∀ α ∈ Set.Icc (0 : ℝ) 1, ∀ n, 1 ≤ n →
AEMeasurable (fun S => alphaRisk ℓ D α (A n S)) (sampleMeasure D n) ∧
∫⁻ S, alphaRisk ℓ D α (A n S) ∂(sampleMeasure D n) ≤
(⨅ h ∈ H, alphaRisk ℓ D α h) + ENNReal.ofReal (ε n)
/-- Theorem 8 (Section 5): OOD detection is learnable in `𝒟` for `H` with a learning rate
`ε_cons(n) = O(r(n))`. -/
def IsLearnableAtRate (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (𝒟 : Set (Domain X K))
(H : Set (X → Fin (K + 1))) (r : ℕ → ℝ) : Prop :=
∃ (A : Algorithm X K) (ε : ℕ → ℝ) (C : ℝ), IsConsistent ℓ 𝒟 H A ε ∧ ∀ n, 1 ≤ n → ε n ≤ C * r n
/-- Definition 3 (Section 3): `𝒟` is a priori-unknown space: `D^α_XY ∈ 𝒟` for every `D_XY ∈ 𝒟`
and every `α ∈ [0, 1)`. -/
def IsPriorUnknown (𝒟 : Set (Domain X K)) : Prop :=
∀ D ∈ 𝒟, ∀ α : Set.Ico (0 : ℝ) 1, withPrior D α ∈ 𝒟
/-- Theorem 1 (Section 3): `𝒟' = {D^α_XY : D_XY ∈ 𝒟, α ∈ [0, 1)}`. -/
def priorClosure (𝒟 : Set (Domain X K)) : Set (Domain X K) :=
{D' | ∃ D ∈ 𝒟, ∃ α : Set.Ico (0 : ℝ) 1, D' = withPrior D α}
/-- Section 3: the single-distribution space `𝒟^{D_XY} = {D^α_XY : α ∈ [0, 1)}`. -/
def singleSpace (D : Domain X K) : Set (Domain X K) :=
Set.range (withPrior D)
/-- Section 3: the total space `𝒟^all`, which consists of all domains. -/
def totalSpace : Set (Domain X K) :=
Set.univ
/-- Section 3: the separate space `𝒟^s` of all domains with `supp D_{X_O} ∩ supp D_{X_I} = ∅`
(supports in the topology of the feature space). -/
def separateSpace [TopologicalSpace X] : Set (Domain X K) :=
{D | Disjoint (marginalIn D).support (D.DO : Measure X).support}
/-- Section 3: the finite-ID-distribution space `𝒟^F`: a priori-unknown space containing only
finitely many distinct ID joint distributions `D_{X_I Y_I}`. -/
def IsFiniteIDSpace (𝒟 : Set (Domain X K)) : Prop :=
IsPriorUnknown 𝒟 ∧ (Domain.DI '' 𝒟).Finite
/-- Condition 1 (Linear Condition, Section 4): for every `D_XY ∈ 𝒟` and `α ∈ [0, 1)`,
`inf_{h∈H} R^α_D(h) = (1 - α) inf_{h∈H} R^in_D(h) + α inf_{h∈H} R^out_D(h)`. -/
def LinearCondition (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (𝒟 : Set (Domain X K))
(H : Set (X → Fin (K + 1))) : Prop :=
∀ D ∈ 𝒟, ∀ α ∈ Set.Ico (0 : ℝ) 1,
(⨅ h ∈ H, alphaRisk ℓ D α h) =
ENNReal.ofReal (1 - α) * (⨅ h ∈ H, riskIn ℓ D h) +
ENNReal.ofReal α * (⨅ h ∈ H, riskOut ℓ D h)
/-- Definition 4 (Section 4): `D_XY` has overlap between ID and OOD distributions: there is a
σ-finite measure `μ̃` with `D_{X_I}, D_{X_O} ≪ μ̃` such that `μ̃(A_overlap) > 0`, where
`A_overlap = {x : f_I(x) > 0 and f_O(x) > 0}` and `f_I = dD_{X_I}/dμ̃`, `f_O = dD_{X_O}/dμ̃`. -/
def HasOverlap (D : Domain X K) : Prop :=
∃ μ : Measure X, SigmaFinite μ ∧ marginalIn D ≪ μ ∧ (D.DO : Measure X) ≪ μ ∧
0 < μ {x | 0 < (marginalIn D).rnDeriv μ x ∧ 0 < (D.DO : Measure X).rnDeriv μ x}
/-- Assumption 1 (Section 4): `H` is separate for OOD data: every `x ∈ X` is sent to the OOD
label `K + 1` by some `h_x ∈ H`. -/
def SeparateForOOD (H : Set (X → Fin (K + 1))) : Prop :=
∀ x, ∃ h ∈ H, h x = Fin.last K
/-- Theorems 4 and 5 (Section 4): the map `φ` sending the ID labels to `1` and the OOD label to
`2`, written with values `0` (ID) and `1` (OOD) in `Fin 2`. -/
def phi (K : ℕ) (y : Fin (K + 1)) : Fin 2 :=
if y = Fin.last K then 1 else 0
/-- Theorems 4 and 5 (Section 4): the binary hypothesis space `φ ∘ H = {φ ∘ h : h ∈ H}`. -/
def phiComp (H : Set (X → Fin (K + 1))) : Set (X → Fin 2) :=
(fun h => phi K ∘ h) '' H
/-- Theorem 5 (Section 4): a finite set `C ⊆ X` is shattered by a class `F` of binary
functions: every labelling of `C` is realised by some `f ∈ F`. -/
def VCShatters {X : Type*} (F : Set (X → Fin 2)) (C : Finset X) : Prop :=
∀ g : X → Fin 2, ∃ f ∈ F, ∀ x ∈ C, f x = g x
/-- Theorem 5 (Section 4): the VC dimension of a class of binary functions, the largest size of
a shattered finite set (`⊤` if there is none). -/
def vcDim {X : Type*} (F : Set (X → Fin 2)) : ℕ∞ :=
⨆ (C : Finset X) (_ : VCShatters F C), (C.card : ℕ∞)
/-- Lemma 4 (Appendix H): a finite discrete domain: `D_{X_I Y_I}` and `D_{X_O}` are concentrated
on finite sets. -/
def IsFiniteDiscrete (D : Domain X K) : Prop :=
(∃ s : Finset (X × Fin K), (D.DI : Measure (X × Fin K)) (↑s)ᶜ = 0) ∧
∃ t : Finset X, (D.DO : Measure X) (↑t)ᶜ = 0
/-- Eq. (4) (Section 5): `H^in • H^b`, the hypotheses `h` built from `h^in ∈ H^in` and
`h^b ∈ H^b` by `h(x) = h^in(x)` if `h^b(x) = 1` (ID) and `h(x) = K + 1` otherwise. The binary
labels `1` (ID) and `2` (OOD) are written `0` and `1` in `Fin 2`. -/
def dotSpace (Hin : Set (X → Fin K)) (Hb : Set (X → Fin 2)) : Set (X → Fin (K + 1)) :=
{h | ∃ hin ∈ Hin, ∃ hb ∈ Hb,
∀ x, h x = if hb x = 0 then Fin.castSucc (hin x) else Fin.last K}
/-- Condition 2 (Section 5): `ℓ(y₂, y₁) ≤ ℓ(K + 1, y₁)` for all ID labels `y₁, y₂`. -/
def LossCondition2 (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) : Prop :=
∀ y₁ y₂ : Fin K, ℓ (Fin.castSucc y₂) (Fin.castSucc y₁) ≤ ℓ (Fin.last K) (Fin.castSucc y₁)
/-- Condition 3 (Compatibility, Section 5), with the ID-consistency classes of Definition 5:
for every `D'_XY ∈ 𝒟` and `ε > 0` there is `h_ε ∈ H` which, for every `D_XY ∈ 𝒟` with
`D_{X_I Y_I} = D'_{X_I Y_I}`, satisfies `R^out_D(h_ε) ≤ inf_{h∈H} R^out_D(h) + ε` and
`R^in_D(h_ε) ≤ inf_{h∈H} R^in_D(h) + ε`. -/
def Compatibility (ℓ : Fin (K + 1) → Fin (K + 1) → ℝ≥0) (𝒟 : Set (Domain X K))
(H : Set (X → Fin (K + 1))) : Prop :=
∀ D' ∈ 𝒟, ∀ ε : ℝ, 0 < ε → ∃ hε ∈ H, ∀ D ∈ 𝒟, D.DI = D'.DI →
riskOut ℓ D hε ≤ (⨅ h ∈ H, riskOut ℓ D h) + ENNReal.ofReal ε ∧
riskIn ℓ D hε ≤ (⨅ h ∈ H, riskIn ℓ D h) + ENNReal.ofReal ε
end OODLearnable
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Common setting (applies to every declaration below). is an arbitrary set with a σ-algebra. It is not required to be a subset of , and no topology is assumed unless stated. is a natural number, and is allowed.
- The full label set is .
- The "ID labels" are , viewed inside .
- The "OOD label" is .
- Binary labels are the two-element set .
Every product carries the product σ-algebra, with the discrete σ-algebra on the finite factor. Throughout:
- integrals are lower Lebesgue integrals of -valued functions, which are defined even when the integrand is not measurable;
- infima over an empty index set equal ;
- is written ;
- in the convention holds.
IsOODLoss. For a function , this is the property
No symmetry, triangle inequality or other property is required. In all risk definitions below the first argument is the predicted label and the second is the true label.
Domain. A domain is a triple of three independent pieces of data:
- is a probability measure on ;
- is a probability measure on ;
- is a real number with .
No mixture measure on is formed or stored, and no relation between the three components is imposed. Two domains are equal exactly when all three components are equal.
When , the space is empty, so it carries no probability measure and there are no domains at all. The same holds when is empty.
marginalIn. For a domain , is the push-forward of under the projection :
for measurable . It is returned as a general measure on , though it has total mass .
withPrior. For a domain and a real , is the domain with the same and as , and with replaced by .
riskIn. For a loss , a domain and an arbitrary function (no measurability required):
Here the ID label is regarded as an element of . Because takes only finitely many values and is a probability measure, this is at most , and so finite.
riskOut. With the same data:
This is again a lower integral, with arbitrary.
alphaRisk. For a loss , a domain , an arbitrary real number and a function :
- For this is .
- For it equals .
- For it equals .
The component of is not used.
risk. , the -risk at equal to the domain's own prior. Since :
No measure on is involved.
Algorithm. An algorithm is any assignment that sends each (including ) and each finite sequence in to a function . There is no measurability or computability requirement. Training samples contain only ID-labelled points.
sampleMeasure. For a domain and , is the -fold product measure of on , i.e. the law of i.i.d. draws. For it is the unit point mass on the empty sequence.
IsConsistent. Fix a loss , a set of domains , a set of functions , an algorithm and a real sequence . The property is the conjunction of four conditions:
- for every and every sample of size . Nothing is required at .
- is non-increasing: . This holds on all of , including .
- as . Together with condition 2 this forces .
- For every and every , both of the following hold:
- the map agrees -almost everywhere with a measurable function;
- the expected risk satisfies
Degenerate cases:
- If is empty or , there are no samples of size and no domains. Conditions 1 and 4 are then vacuous, even for .
- If , condition 4 is vacuous.
IsLearnable. There exist an algorithm and a real sequence such that is consistent with respect to , , and , as defined above.
IsStronglyLearnable. There exist a single algorithm and a single real sequence satisfying conditions 1–3 of IsConsistent, together with the following:
- for every , every real (closed interval, so is included) and every , the map is -a.e. measurable;
- and
The same and must work for all and all simultaneously. The domain's own plays no role.
IsLearnableAtRate. Given additionally an arbitrary real sequence , there exist an algorithm , a real sequence and a real constant such that:
- is consistent with respect to , , and ;
- for every .
There is no absolute value, and no sign constraint on or . Because , this in effect requires for all .
IsPriorUnknown. A set of domains has this property if for every and every real , the domain (same ID and OOD measures, prior replaced by ) also lies in . The empty set qualifies.
priorClosure. For a set of domains :
singleSpace. For a domain :
the set of all domains sharing 's ID and OOD measures, with any prior in .
totalSpace. is the set of all domains over and .
separateSpace. This requires an additional topology on . That topology is arbitrary and is not assumed to be related to the σ-algebra; in particular it need not generate it. is the set of domains with
The support of a measure is the set of points such that every open neighbourhood of that is measurable... more precisely, every open neighbourhood of has positive -measure. The prior is unrestricted.
IsFiniteIDSpace. A set of domains has this property if both:
- it is prior-unknown (see
IsPriorUnknown); - the set of ID joint measures occurring in is finite.
No finiteness is imposed on the OOD measures. The empty set qualifies.
LinearCondition. For , and : for every and every real ,
The equation is read in . If , both sides equal (with at ), so the condition holds for every .
HasOverlap. A domain has overlap if there exists a σ-finite measure on with and such that
Here and are specific fixed versions of the Radon–Nikodym derivatives and (the library's canonical choice). The prior and the labels play no role.
SeparateForOOD. A set of functions has this property if for every there exists with . Different points may use different . If is empty, this holds for every , including .
phi. The map is defined by
So means ID and means OOD.
phiComp. For a set of functions :
a set of functions .
VCShatters. This is stated for an arbitrary set (no σ-algebra needed), a set of functions and a finite subset . shatters if for every function there is some with for all . The empty set is shattered exactly when is non-empty.
vcDim. For as above:
- The value is if arbitrarily large finite sets are shattered.
- The value is if no finite set is shattered, which happens exactly when , since the supremum of the empty set is .
IsFiniteDiscrete. A domain is finite-discrete if both:
- there is a finite set with ;
- there is a finite set with .
The prior is unrestricted.
dotSpace. This takes a set of functions and a set of functions . is the set of all for which there exist and with, for all ,
The result is empty if either or is empty.
LossCondition2. For a loss : for all ID labels ,
That is, predicting any ID label for a true ID label costs no more than predicting . This includes , and it is vacuous when .
Compatibility. For , and : for every and every real , there exists a single such that, for every with , both
and
The quantifier order is: is chosen before ranges over all domains in sharing 's ID joint measure, with any OOD measure and any prior. This class includes itself. If is non-empty and , the condition fails; if , it holds vacuously.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.