Multi-view data, density ratio, contrastive losses, landmark and direct embeddings
DefinitionMultiViewRedundancy_BasicObjects of Sections 2–5 of Tosh, Krishnamurthy and Hsu (2021).
- Multi-view distribution. The views take values in measurable spaces and (with standard Borel where conditional laws are needed), and the label in . A law of is a probability measure on . Its view marginal has marginals and .
- Regression functions (Section 2.4). , and are the means of the conditional laws of (disintegrations of ). The predictor through the other view is , the mean of under the conditional law of given . The redundancy quantities are and , and .
- Density ratio (Section 2.3). , the Radon–Nikodym derivative of with respect to .
- Contrastive distribution and losses (Sections 2.2, 2.3, 4). is with with probability , and with otherwise. The losses are (eq. 1) and for (eq. 2). The excess losses are and .
- Landmark embedding (Section 3). For landmarks and a bivariate , ; uses . The landmark variance is with , valued in , and for a block size .
- Direct embeddings (Section 4). For , : with , valued in .
- Risk (eq. 3). , valued in .
- Hidden variables (Section 4.2). renders and conditionally independent with conditional laws , if . The hidden-variable variance is with , valued in .
- Gaussian model (Section 3.3). , , , ; the law of is that of with independent and .
Formalization Note The paper assumes that all variables have probability mass functions or densities. The formalization works with general laws: densities and density ratios become Radon–Nikodym derivatives, and conditional means become means of disintegration kernels, which agree with the paper's formulas in both cases. Quantities that may be infinite in the paper (variances, , risks) take values in . is written in its equal odds form , which stays correct where . The block size of is a parameter; each statement says which it uses.
import Mathlib
/-!
Tosh, Krishnamurthy and Hsu, *Contrastive learning, multi-view redundancy, and linear models*,
arXiv:2008.10150v2 (2021): the multi-view distribution and its regression functions (Section 2.1,
2.4), the density ratio and the contrastive losses (Sections 2.2, 2.3, 4, eq. (1), eq. (2)), the
landmark embedding (Section 3), direct embeddings and hidden variables (Section 4), the risk
(eq. (3)) and the Gaussian model (Section 3.3).
A law `ρ` of the triple `(X, Z, Y)` is a measure on `X × Z × ℝ`; a law `ν` of the pair of views
`(X, Z)` is a measure on `X × Z`. Conditional means are means of the disintegration kernels
(`Measure.condKernel`); density ratios are Radon–Nikodym derivatives, which covers both the
probability-mass-function and the density case of Section 2.1.
-/
open MeasureTheory ProbabilityTheory
open scoped ENNReal NNReal
noncomputable section
namespace MultiViewRedundancy
variable {X Z : Type*} [MeasurableSpace X] [MeasurableSpace Z]
/-- Section 2.1: the joint law `p_{X,Z}` of the two views, for a law `ρ` of `(X, Z, Y)`.
Its marginals `(lawXZ ρ).fst` and `(lawXZ ρ).snd` are `p_X` and `p_Z`. -/
abbrev lawXZ (ρ : Measure (X × Z × ℝ)) : Measure (X × Z) :=
ρ.map (fun t => (t.1, t.2.1))
/-- Section 2.3: the density ratio `g⋆(x, z) = p_{X,Z}(x, z) / (p_X(x) p_Z(z))`, the
Radon–Nikodym derivative of `p_{X,Z}` with respect to `p_X ⊗ p_Z`. -/
def densityRatio (ν : Measure (X × Z)) (x : X) (z : Z) : ℝ :=
(ν.rnDeriv (ν.fst.prod ν.snd) (x, z)).toReal
/-- Section 2.4: the regression function `E[Y | X = x]`, the mean of the conditional law of `Y`
given `X = x`. -/
def regX (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] (x : X) : ℝ :=
∫ y, y ∂((ρ.map (fun t => (t.1, t.2.2))).condKernel x)
/-- Section 2.4: the regression function `E[Y | Z = z]`. -/
def regZ (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] (z : Z) : ℝ :=
∫ y, y ∂((ρ.map (fun t => (t.2.1, t.2.2))).condKernel z)
/-- Section 2.4: the regression function `E[Y | (X, Z) = (x, z)]`. -/
def regXZ (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] (x : X) (z : Z) : ℝ :=
∫ y, y ∂((ρ.map (fun t => ((t.1, t.2.1), t.2.2))).condKernel (x, z))
/-- Section 2.4: `μ(x) = E[E[Y | Z] | X = x]`, the mean of `z ↦ E[Y | Z = z]` under the
conditional law of `Z` given `X = x`. -/
def muFn [StandardBorelSpace Z] [Nonempty Z] (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ]
(x : X) : ℝ :=
∫ z, regZ ρ z ∂((lawXZ ρ).condKernel x)
/-- Section 2.4: `ε_X = E[(E[Y | X] - E[Y | X, Z])²]`. -/
def epsX (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] : ℝ :=
∫ t, (regX ρ t.1 - regXZ ρ t.1 t.2.1) ^ 2 ∂ρ
/-- Section 2.4: `ε_Z = E[(E[Y | Z] - E[Y | X, Z])²]`. -/
def epsZ (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] : ℝ :=
∫ t, (regZ ρ t.2.1 - regXZ ρ t.1 t.2.1) ^ 2 ∂ρ
/-- Lemma 1: `ε_μ := ε_X + 2 √(ε_X ε_Z) + ε_Z`. -/
def epsMu (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] : ℝ :=
epsX ρ + 2 * Real.sqrt (epsX ρ * epsZ ρ) + epsZ ρ
/-- Section 3: the landmark embedding `x ↦ (g(x, Z₁), …, g(x, Z_m))` of a bivariate function `g`
with landmarks `zs = (Z₁, …, Z_m)`; `φ⋆` is the case `g = g⋆`. -/
def landmarkEmbedding {m : ℕ} (g : X → Z → ℝ) (zs : Fin m → Z) (x : X) : Fin m → ℝ :=
fun i => g x (zs i)
/-- Lemma 2: `var(E[Y₁ | Z₁] g⋆(X, Z₁))` with `(X, Z₁) ∼ p_X ⊗ p_Z`, as an element of `[0, ∞]`. -/
def landmarkVar (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] : ℝ≥0∞ :=
evariance (fun p : X × Z => regZ ρ p.2 * densityRatio (lawXZ ρ) p.1 p.2)
((lawXZ ρ).fst.prod (lawXZ ρ).snd)
/-- Lemma 2: `ε_lm := 2 var(E[Y₁ | Z₁] g⋆(X, Z₁)) / n` for the block size `n`; the paper takes
`n = ⌊m / log₂(1/δ)⌋`. -/
def epsLm (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ] (n : ℕ) : ℝ :=
2 * (landmarkVar ρ).toReal / n
/-- Eq. (3): the risk `R(φ) = inf_{w ∈ ℝ^m} E[(wᵀφ(X) - μ(X))²]` of an embedding `φ : X → ℝ^m`,
as an element of `[0, ∞]`. -/
def risk [StandardBorelSpace Z] [Nonempty Z] (ρ : Measure (X × Z × ℝ)) [IsFiniteMeasure ρ]
{m : ℕ} (φ : X → Fin m → ℝ) : ℝ≥0∞ :=
⨅ w : Fin m → ℝ, ∫⁻ x, ENNReal.ofReal ((∑ i, w i * φ x i - muFn ρ x) ^ 2) ∂(lawXZ ρ).fst
/-- Section 2.2: the contrastive distribution `D_contrast` of `(X_c, Z_c, Y_c)`: with probability
`1/2` a pair `(X, Z) ∼ p_{X,Z}` with `Y_c = 1`, otherwise `(X, Z̃) ∼ p_X ⊗ p_Z` with `Y_c = -1`. -/
def contrastDist (ν : Measure (X × Z)) : Measure (X × Z × ℝ) :=
(2⁻¹ : ℝ≥0∞) • ν.map (fun p => (p.1, p.2, (1 : ℝ))) +
(2⁻¹ : ℝ≥0∞) • (ν.fst.prod ν.snd).map (fun p => (p.1, p.2, (-1 : ℝ)))
/-- Eq. (1): the logistic contrastive loss `L_lm(f) = E[log(1 + exp(-Y_c f(X_c, Z_c)))]`. -/
def lossLm (ν : Measure (X × Z)) (f : X → Z → ℝ) : ℝ :=
∫ t, Real.log (1 + Real.exp (-(t.2.2 * f t.1 t.2.1))) ∂(contrastDist ν)
/-- Eq. (2): the direct contrastive loss `L_direct(f) = E[log(1 + 1 / f(X_c, Z_c)^{Y_c})]`, for
`f > 0`. -/
def lossDirect (ν : Measure (X × Z)) (f : X → Z → ℝ) : ℝ :=
∫ t, Real.log (1 + 1 / f t.1 t.2.1 ^ t.2.2) ∂(contrastDist ν)
/-- Section 4: the bivariate function `(x, z) ↦ η(x)ᵀψ(z)` of a pair of embeddings. -/
def innerEmb {m : ℕ} (η : X → Fin m → ℝ) (ψ : Z → Fin m → ℝ) (x : X) (z : Z) : ℝ :=
∑ i, η x i * ψ z i
/-- Lemma 6: `ε_direct(η, ψ) = E[(η(X)ᵀψ(Z̃) - g⋆(X, Z̃))²]` with `(X, Z̃) ∼ p_X ⊗ p_Z`, as an
element of `[0, ∞]`. -/
def epsDirect (ν : Measure (X × Z)) {m : ℕ} (η : X → Fin m → ℝ) (ψ : Z → Fin m → ℝ) : ℝ≥0∞ :=
∫⁻ p, ENNReal.ofReal ((innerEmb η ψ p.1 p.2 - densityRatio ν p.1 p.2) ^ 2) ∂(ν.fst.prod ν.snd)
/-- Theorem 10: the excess landmark loss `ε_opt,lm = L_lm(f) - L_lm(log ∘ g⋆)`. The term
`L_lm(log ∘ g⋆)` is written in odds form as `L_direct(g⋆)`, which equals it and keeps the
correct value `0` of the loss at points where `g⋆ = 0`. -/
def epsOptLm (ν : Measure (X × Z)) (f : X → Z → ℝ) : ℝ :=
lossLm ν f - lossDirect ν (densityRatio ν)
/-- Theorem 11: the excess direct loss `ε_opt,direct = L_direct((x, z) ↦ η(x)ᵀψ(z)) - L_direct(g⋆)`. -/
def epsOptDirect (ν : Measure (X × Z)) {m : ℕ} (η : X → Fin m → ℝ) (ψ : Z → Fin m → ℝ) : ℝ :=
lossDirect ν (innerEmb η ψ) - lossDirect ν (densityRatio ν)
section Hidden
variable {H : Type*} [MeasurableSpace H]
/-- Section 4: `H ∼ ρH` renders `X` and `Z` conditionally independent, with conditional laws
`κX h` of `X` and `κZ h` of `Z` given `H = h`: `p_{X,Z} = ∫ p_{X|H}(· | h) ⊗ p_{Z|H}(· | h) dp_H(h)`. -/
def IsHiddenVariable (ν : Measure (X × Z)) (ρH : Measure H) (κX : Kernel H X) (κZ : Kernel H Z)
[IsSFiniteKernel κX] [IsSFiniteKernel κZ] : Prop :=
ν = ρH.bind (κX ×ₖ κZ)
/-- Lemma 7: the variance of `p_{X|H}(X | H̄) p_{Z|H}(Z̃ | H̄) / (p_X(X) p_Z(Z̃))` with
`(X, Z̃, H̄) ∼ p_X ⊗ p_Z ⊗ p_H`, as an element of `[0, ∞]`; each ratio `p_{X|H}(x | h) / p_X(x)` is
the Radon–Nikodym derivative of `κX h` with respect to `p_X`. -/
def hiddenVar [MeasurableSpace.CountableOrCountablyGenerated H X]
[MeasurableSpace.CountableOrCountablyGenerated H Z] (ν : Measure (X × Z)) (ρH : Measure H)
(κX : Kernel H X) (κZ : Kernel H Z) : ℝ≥0∞ :=
evariance
(fun t : X × Z × H =>
(Kernel.rnDeriv κX (Kernel.const H ν.fst) t.2.2 t.1).toReal *
(Kernel.rnDeriv κZ (Kernel.const H ν.snd) t.2.2 t.2.1).toReal)
(ν.fst.prod (ν.snd.prod ρH))
end Hidden
/-- Section 3.3: the Gaussian model `H ∼ N(0, σ²)`, `X | H ∼ N(H, 1)`, `Z | H ∼ N(H, 1)`,
`X ⊥ Z | H`, with `v = σ²`: the law of `(H + E₁, H + E₂)` for independent `H ∼ N(0, σ²)`,
`E₁, E₂ ∼ N(0, 1)`. -/
def gaussianModel (v : ℝ≥0) : Measure (ℝ × ℝ) :=
((gaussianReal 0 v).prod ((gaussianReal 0 1).prod (gaussianReal 0 1))).map
(fun t => (t.1 + t.2.1, t.1 + t.2.2))
end MultiViewRedundancy
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Setting (applies to every declaration below). and are arbitrary measurable spaces with no further assumptions unless a declaration states otherwise. always denotes a measure on , with points written . always denotes a measure on .
None of these measures is required to be a probability measure. Some declarations require to be finite, and this is stated where it applies.
Integration conventions:
- "" is the Bochner integral, which returns whenever the integrand is not integrable.
- "" is the lower Lebesgue integral of a -valued function, which always has a value.
Arithmetic conventions:
- .
- means , with .
- Converting to a real number gives .
lawXZ. For a measure on , is the pushforward of under the projection . It has the same total mass as .
Its two marginals are written as follows:
- is the pushforward of to .
- is the pushforward of to .
densityRatio. For a measure on , let and be its marginals on and . For and ,
converted to a real number. The derivative is the library's fixed version of the Radon–Nikodym derivative of with respect to the product of its marginals. Several caveats apply:
- No absolute continuity assumed. If is not absolutely continuous with respect to , this is the derivative of the absolutely continuous part only.
- Non-decomposable case. If no Lebesgue decomposition exists, the derivative is everywhere.
- Infinite values. Points where the derivative equals give the value .
- Pointwise values are a fixed choice. The derivative is determined only almost everywhere, so the value at a particular point is a fixed but arbitrary choice.
- No normalisation. If for a probability measure , then , so almost everywhere.
regX. Let be a finite measure on . Let be its pushforward under , and let be the library's chosen conditional kernel of . This is a Markov kernel from to satisfying ; it is determined only for -almost every . For ,
The value is if is not integrable under .
regZ. Let be a finite measure on . Let be the library's chosen conditional kernel, a Markov kernel from to , of the pushforward of under . It is determined only for almost every with respect to the -marginal. For ,
The value is if is not integrable under .
regXZ. Let be a finite measure on . Let be the library's chosen conditional kernel, a Markov kernel from to , of the pushforward of under . It is determined only for -almost every . For and ,
The value is if is not integrable under .
muFn. Assume is a nonempty standard Borel space and is a finite measure on . Let be the library's chosen conditional kernel, a Markov kernel from to , of , the pushforward of to . It is determined only for -almost every . For ,
Here is the mean of the chosen conditional law of given , as in regZ. The value is if is not integrable under .
epsX. For a finite measure on ,
The ingredients are:
- is the mean of the chosen conditional law of given .
- is the mean of the chosen conditional law of given .
These conditional means are taken from the library's chosen disintegration kernels, with wherever is not integrable. The integral is taken against itself, not a normalised version. It is if the integrand is not integrable, and it is always .
epsZ. For a finite measure on ,
The ingredients are:
- is the mean of the chosen conditional law of given .
- is the mean of the chosen conditional law of given .
Both come from the library's chosen disintegration kernels, with wherever is not integrable. The integral is taken against unnormalised , is if the integrand is not integrable, and is always .
epsMu. For a finite measure on ,
Here and are defined as follows (Bochner integrals, if not integrable):
- .
- .
The functions , , are the means of the library-chosen conditional laws of given , given , and given , respectively.
landmarkEmbedding. Given:
- a natural number ,
- a function ,
- points ,
the map is
When , is the empty vector.
landmarkVar. Let be a finite measure on , with its pushforward to and , the marginals. Define , where:
- is the mean of the library-chosen conditional law of given (and if is not integrable).
- is the real-valued Radon–Nikodym derivative , with the caveats of
densityRatio: absolutely continuous part only, , fixed representative.
The value, in , is
The mean is if is not integrable. The product measure is not normalised; it has total mass equal to the square of 's mass.
epsLm. For a finite measure on and a natural number ,
is the quantity of landmarkVar, namely
is converted to a real number, so yields . When the value is (division by zero).
risk. Assume is a nonempty standard Borel space, is a finite measure on , , and is an arbitrary function (no measurability assumed). The value, in , is
The ingredients are:
- is the (unnormalised) -marginal of .
- is the function of
muFn: , using the library-chosen conditional kernels.
When , the sum is empty and .
contrastDist. For a measure on with marginals and , is the measure on defined by
It is a probability measure when is one. In general, if has total mass , then has total mass .
lossLm. For a measure on and an arbitrary ,
is the measure of contrastDist, namely . When the integrand is integrable with respect to both parts, this equals
The value is if the integrand is not integrable with respect to .
lossDirect. For a measure on and an arbitrary (no positivity assumed),
Here is the measure of contrastDist, and is the real power.
Only occur, and the real power satisfies and . Combined with the conventions , and , the integrand is:
- on the -part ();
- on the -part ().
At points where , both expressions equal . When integrable, the value equals
It is if the integrand is not integrable with respect to .
innerEmb. For , and ,
This is identically when .
epsDirect. For a measure on , , and , the value, in , is
is the real-valued Radon–Nikodym derivative , with the caveats of densityRatio: absolutely continuous part only, , fixed representative, no normalisation.
epsOptLm. For a measure on and an arbitrary ,
The three ingredients are:
- .
- is the real-valued Radon–Nikodym derivative , as in
densityRatio. - , when integrable, equals
with , so points where contribute .
Each loss is separately if its integrand is not integrable with respect to , the measure of contrastDist.
epsOptDirect. For a measure on , , and ,
Here is defined by
It reduces to when integrable, and to otherwise. The conventions and apply. The inner product is not required to be positive. is the real-valued Radon–Nikodym derivative of with respect to .
IsHiddenVariable. Let:
- be a measurable space,
- be a measure on ,
- be any measure on (not required to be a probability measure),
- be an s-finite kernel from to ,
- be an s-finite kernel from to .
These kernels need not be Markov. The proposition asserts the equality of measures
Equivalently, for every measurable .
hiddenVar. Let be a measurable space such that each of the pairs and satisfies the library's condition " is countable or the target space is countably generated". Let be a measure on , a measure on , and , kernels from to and to .
No relation among , , , is assumed; in particular IsHiddenVariable is not assumed. Define
where each factor is the library's jointly measurable kernel Radon–Nikodym derivative of (resp. ) with respect to the constant kernel (resp. ). For each , this agrees almost everywhere with the Radon–Nikodym derivative of the absolutely continuous part. Each factor is converted to a real number, with . The value, in , is
The mean is if is not integrable. The product measure is not normalised.
gaussianModel. For a real number , this is the law on of , where , , are independent with
Formally, it is the pushforward of under . It is a probability measure. When , is the point mass at , so the law is .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.