Nonlinear ICA with auxiliary variables: model, contrastive logistic loss, Assumption of Variability, conditional exponentiality
DefinitionNonlinearICAAux_BasicObjects of Sections 3 and 4 and Supplementary A-D of Hyvärinen, Sasaki and Turner (2019).
- Model (eqs. 1, 4). An auxiliary variable has a probability law . Given , the sources have the density , so they are conditionally independent; the are jointly measurable and for every . The observation is , where is invertible and both and have continuous second derivatives (a diffeomorphism). The marginal source density is required to be a finite integral at every .
- Densities of the two classes (eq. 5). is the density of , and is the density of with independent of , both with respect to Lebesgue measure times . The log-density ratio is .
- Logistic regression (eqs. 5, 6). The population logistic loss of a regression function is . The constrained regression function of eq. (6) is .
- Assumption of Variability (eqs. 8, 9). . The assumption holds if for every there are with , , linearly independent.
- Alternative Assumption of Variability (eqs. 41, 42). collects and . The assumption holds if for every there are an index and with linearly independent.
- Conditional exponentiality (Definition 1, eq. 7). is conditionally exponential of order given if its conditional density equals for all and , with and linearly independent functions.
- Objects of Theorem 3 (eqs. 10-12). ; ; is the matrix with entry in row and column ; the regression function of eq. (11) is .
- Partial derivatives. is the derivative of in the direction of the -th basis vector.
Formalization Note The auxiliary variable is taken in as in Section 3.1, and the conditional densities are given for every ; statements quantify over all rather than over the support of . The finiteness of and the joint measurability of the are implicit in the paper, which takes and expectations freely. The logistic loss is valued in so that no non-integrable case is silently set to .
import Mathlib
/-!
Hyvärinen, Sasaki and Turner, *Nonlinear ICA Using Auxiliary Variables and Generalized
Contrastive Learning*, arXiv:1805.08651v3 (AISTATS 2019): the nonlinear ICA model with auxiliary
variables (Section 3.1, eqs. (1), (4)), the contrastive logistic-regression problem (Section 3.2,
eqs. (5), (6)), the Assumption of Variability (Section 4.2, Theorem 1, eqs. (8), (9)) and its
alternative form (Supplementary D, eqs. (41), (42)), conditional exponentiality (Section 4.1,
Definition 1, eq. (7)) and the objects of Theorem 3 (Section 4.3, eqs. (10)-(12)).
Sources live in `ℝ^n = Fin n → ℝ`, the auxiliary variable in `ℝ^m = Fin m → ℝ`, and the
mixing `x = f(s)` is an equivalence of `ℝ^n` with `g = f⁻¹ = f.symm`.
-/
open MeasureTheory
open scoped ENNReal
noncomputable section
namespace NonlinearICAAux
variable {n m k : ℕ}
/-- Section 3.1 (p. 3): a map `ℝ^n → ℝ^n` that is invertible, has continuous second derivatives,
and whose inverse has continuous second derivatives. -/
def IsC2Diffeo (e : (Fin n → ℝ) ≃ (Fin n → ℝ)) : Prop :=
ContDiff ℝ 2 e ∧ ContDiff ℝ 2 e.symm
/-- Eq. (4) (Section 3.1, p. 3): the conditional log-density of the sources,
`log p(s | u) = ∑ᵢ qᵢ(sᵢ, u)`. -/
def condLogDensityS (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (s : Fin n → ℝ) (u : Fin m → ℝ) : ℝ :=
∑ i, q i (s i) u
/-- Section 3.1, eqs. (1) and (4) (p. 3): the nonlinear ICA model with auxiliary variables. The
auxiliary variable `u ∈ ℝ^m` has law `μ`; given `u`, the sources `s ∈ ℝ^n` have the density
`exp (∑ᵢ qᵢ(sᵢ, u))` (integrable with integral `1` for every `u`); the observation is
`x = f(s)` with `f` a `C²` diffeomorphism. The `qᵢ` are jointly measurable, and the marginal
density of the sources, `p_s(s) = ∫ exp (∑ᵢ qᵢ(sᵢ, u)) dμ(u)`, is a finite integral at every `s`
(its logarithm is used in the proofs of Theorems 1 and 3, Supplementary A and C). -/
def IsAuxICAModel (μ : Measure (Fin m → ℝ)) (q : Fin n → ℝ → (Fin m → ℝ) → ℝ)
(f : (Fin n → ℝ) ≃ (Fin n → ℝ)) : Prop :=
IsC2Diffeo f ∧ (∀ i, Measurable (fun p : ℝ × (Fin m → ℝ) => q i p.1 p.2)) ∧
(∀ u, Integrable (fun s => Real.exp (condLogDensityS q s u)) ∧
∫ s, Real.exp (condLogDensityS q s u) = 1) ∧
(∀ s, Integrable (fun u => Real.exp (condLogDensityS q s u)) μ)
/-- Supplementary A (p. 11): the marginal density of the sources, `p_s(s) = ∫ p(s | u) dμ(u)`. -/
def margDensityS (μ : Measure (Fin m → ℝ)) (q : Fin n → ℝ → (Fin m → ℝ) → ℝ)
(s : Fin n → ℝ) : ℝ :=
∫ u, Real.exp (condLogDensityS q s u) ∂μ
/-- Supplementary A (p. 11): the absolute Jacobian determinant `|det J g(x)|` of a map
`g : ℝ^n → ℝ^n`. -/
def absJacDet (g : (Fin n → ℝ) → (Fin n → ℝ)) (x : Fin n → ℝ) : ℝ :=
|LinearMap.det ((fderiv ℝ g x : (Fin n → ℝ) →L[ℝ] (Fin n → ℝ)) :
(Fin n → ℝ) →ₗ[ℝ] (Fin n → ℝ))|
/-- Supplementary A (p. 11): the conditional density of the observation given the auxiliary
variable, `p(x | u) = p(g(x) | u) |det J g(x)|` with `g = f⁻¹`; it is the density of the pair
`x̃ = (x, u)` of eq. (5) with respect to Lebesgue measure times the law of `u`. -/
def condDensityX (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (f : (Fin n → ℝ) ≃ (Fin n → ℝ))
(x : Fin n → ℝ) (u : Fin m → ℝ) : ℝ :=
Real.exp (condLogDensityS q (f.symm x) u) * absJacDet f.symm x
/-- Supplementary A (p. 11): the marginal density of the observation,
`p(x) = p_s(g(x)) |det J g(x)|`; it is the density of the pair `x̃* = (x, u*)` of eq. (5), with
`u*` independent of `x`, with respect to Lebesgue measure times the law of `u`. -/
def margDensityX (μ : Measure (Fin m → ℝ)) (q : Fin n → ℝ → (Fin m → ℝ) → ℝ)
(f : (Fin n → ℝ) ≃ (Fin n → ℝ)) (x : Fin n → ℝ) : ℝ :=
margDensityS μ q (f.symm x) * absJacDet f.symm x
/-- Supplementary A (p. 11): the difference of the log-densities of the two classes `x̃` and `x̃*`
of eq. (5), `log p(x, u) - log p(x) - log p(u) = log p(x | u) - log p(x)`. -/
def logRatio (μ : Measure (Fin m → ℝ)) (q : Fin n → ℝ → (Fin m → ℝ) → ℝ)
(f : (Fin n → ℝ) ≃ (Fin n → ℝ)) (x : Fin n → ℝ) (u : Fin m → ℝ) : ℝ :=
Real.log (condDensityX q f x u) - Real.log (margDensityX μ q f x)
/-- Section 3.2 (p. 3): the population logistic loss of a regression function `r(x, u)` that
discriminates `x̃ = (x, u)` (class 1, posterior `1 / (1 + exp (-r))`) from `x̃* = (x, u*)`
(class 0), the two classes having equal weight:
`E_{x̃} log (1 + e^{-r}) + E_{x̃*} log (1 + e^{r})`, valued in `[0, ∞]`. -/
def logisticLoss (μ : Measure (Fin m → ℝ)) (q : Fin n → ℝ → (Fin m → ℝ) → ℝ)
(f : (Fin n → ℝ) ≃ (Fin n → ℝ)) (r : (Fin n → ℝ) × (Fin m → ℝ) → ℝ) : ℝ≥0∞ :=
∫⁻ p, (ENNReal.ofReal (Real.log (1 + Real.exp (-r p)) * condDensityX q f p.1 p.2) +
ENNReal.ofReal (Real.log (1 + Real.exp (r p)) * margDensityX μ q f p.1))
∂((volume : Measure (Fin n → ℝ)).prod μ)
/-- Eq. (6) (Section 3.2, p. 3): the regression function `r(x, u) = ∑ᵢ ψᵢ(hᵢ(x), u)`. -/
def regFun (ψ : Fin n → ℝ → (Fin m → ℝ) → ℝ) (h : (Fin n → ℝ) → (Fin n → ℝ))
(x : Fin n → ℝ) (u : Fin m → ℝ) : ℝ :=
∑ i, ψ i (h x i) u
/-- Eq. (9) (Theorem 1, p. 5): `w(y, u) ∈ ℝ^{2n}`, the first derivatives `∂qᵢ(yᵢ, u)/∂yᵢ`
(left block) followed by the second derivatives `∂²qᵢ(yᵢ, u)/∂yᵢ²` (right block). -/
def wVec (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (y : Fin n → ℝ) (u : Fin m → ℝ) :
Fin n ⊕ Fin n → ℝ :=
Sum.elim (fun i => deriv (fun s => q i s u) (y i))
(fun i => iteratedDeriv 2 (fun s => q i s u) (y i))
/-- Theorem 1, Assumption 3, eq. (8) (p. 5), the Assumption of Variability: for every
`y ∈ ℝ^n` there are `2n + 1` values `u₀, …, u_{2n}` such that the `2n` vectors
`w(y, u_j) - w(y, u₀)`, `j = 1, …, 2n`, are linearly independent. -/
def Variability (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) : Prop :=
∀ y : Fin n → ℝ, ∃ u₀ : Fin m → ℝ, ∃ us : Fin (2 * n) → (Fin m → ℝ),
LinearIndependent ℝ (fun j => wVec q y (us j) - wVec q y u₀)
/-- Eq. (42) (Supplementary D, p. 13): `w̃(y, u)`, the derivatives with respect to the
coordinate `u_j` of the entries of `w(y, u)`: `∂²qᵢ(yᵢ, u)/∂yᵢ∂u_j` (left block) and
`∂³qᵢ(yᵢ, u)/∂yᵢ²∂u_j` (right block). -/
def wAltVec (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (y : Fin n → ℝ) (u : Fin m → ℝ) (j : Fin m) :
Fin n ⊕ Fin n → ℝ :=
Sum.elim (fun i => fderiv ℝ (fun u' => deriv (fun s => q i s u') (y i)) u (Pi.single j 1))
(fun i => fderiv ℝ (fun u' => iteratedDeriv 2 (fun s => q i s u') (y i)) u (Pi.single j 1))
/-- Supplementary D, eq. (41) (p. 13), the Alternative Assumption of Variability: for every
`y ∈ ℝ^n` there are an index `j` of the auxiliary variable and `2n` values `u₁, …, u_{2n}`
such that the vectors `w̃(y, u_l)` (derivatives in `u_j`) are linearly independent. -/
def AltVariability (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) : Prop :=
∀ y : Fin n → ℝ, ∃ j : Fin m, ∃ us : Fin (2 * n) → (Fin m → ℝ),
LinearIndependent ℝ (fun l => wAltVec q y (us l) j)
/-- Definition 1 (p. 4): the conditional density `p(sᵢ | u)` of the single source `sᵢ` in the
model of eq. (4), `exp qᵢ(sᵢ, u) / ∫ exp qᵢ(t, u) dt`. -/
def condMarginal (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (i : Fin n) (u : Fin m → ℝ) (s : ℝ) : ℝ :=
Real.exp (q i s u) / ∫ t, Real.exp (q i t u)
/-- Definition 1, eq. (7) (p. 4), with its data named: `sᵢ` is conditionally exponential of
order `k` given `u` with base `Qᵢ > 0`, normaliser `Zᵢ > 0`, linearly independent sufficient
statistics `q̃ᵢ₁, …, q̃ᵢₖ` and natural parameters `λᵢ₁(u), …, λᵢₖ(u)`:
`p(sᵢ | u) = Qᵢ(sᵢ) / Zᵢ(u) · exp (∑ⱼ q̃ᵢⱼ(sᵢ) λᵢⱼ(u))`. -/
def IsCondExpWith (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (i : Fin n) (Q : ℝ → ℝ)
(Z : (Fin m → ℝ) → ℝ) (qt : Fin k → ℝ → ℝ) (lam : Fin k → (Fin m → ℝ) → ℝ) : Prop :=
(∀ s, 0 < Q s) ∧ (∀ u, 0 < Z u) ∧ LinearIndependent ℝ qt ∧
∀ u s, condMarginal q i u s = Q s / Z u * Real.exp (∑ j, qt j s * lam j u)
/-- Definition 1 (p. 4): `sᵢ` is conditionally exponential of order `k` given `u`. -/
def IsCondExp (q : Fin n → ℝ → (Fin m → ℝ) → ℝ) (i : Fin n) (k : ℕ) : Prop :=
∃ (Q : ℝ → ℝ) (Z : (Fin m → ℝ) → ℝ) (qt : Fin k → ℝ → ℝ) (lam : Fin k → (Fin m → ℝ) → ℝ),
IsCondExpWith q i Q Z qt lam
/-- Theorem 2 (ii) (p. 5): the vector `(λᵢⱼ(u))_{(i,j)} ∈ ℝ^{nk}` of all natural parameters. -/
def lamVec (lam : Fin n → Fin k → (Fin m → ℝ) → ℝ) (u : Fin m → ℝ) : Fin n × Fin k → ℝ :=
fun p => lam p.1 p.2 u
/-- Eq. (12) (Theorem 3, p. 6): the vector of all sufficient statistics
`(q̃₁₁(s₁), q̃₁₂(s₁), …, q̃ₙₖ(sₙ))`. -/
def sstatVec (qt : Fin n → Fin k → ℝ → ℝ) (s : Fin n → ℝ) : Fin n × Fin k → ℝ :=
fun p => qt p.1 p.2 (s p.1)
/-- Eq. (10) (Theorem 3, Assumption 3, p. 5): the `nk × nk` matrix `L̄` with row `(i, j)` and
column `l` equal to `λᵢⱼ(u_l) - λᵢⱼ(u₀)`. -/
def lamBar (lam : Fin n → Fin k → (Fin m → ℝ) → ℝ) (u₀ : Fin m → ℝ)
(us : Fin n × Fin k → (Fin m → ℝ)) : Matrix (Fin n × Fin k) (Fin n × Fin k) ℝ :=
fun p l => lam p.1 p.2 (us l) - lam p.1 p.2 u₀
/-- Eq. (11) (Theorem 3, p. 5): the regression function `h̃(x)ᵀ v(u) + a(x) + b(u)` with
`h̃ : ℝ^n → ℝ^{nk}` and `v : ℝ^m → ℝ^{nk}`. -/
def regExp (ht : (Fin n → ℝ) → (Fin n × Fin k → ℝ)) (v : (Fin m → ℝ) → (Fin n × Fin k → ℝ))
(a : (Fin n → ℝ) → ℝ) (b : (Fin m → ℝ) → ℝ) (x : Fin n → ℝ) (u : Fin m → ℝ) : ℝ :=
∑ p, ht x p * v u p + a x + b u
/-- Supplementary A (p. 11): the partial derivative `∂F(y)/∂y_j` of `F : ℝ^n → ℝ`. -/
def partialDeriv (F : (Fin n → ℝ) → ℝ) (j : Fin n) (y : Fin n → ℝ) : ℝ :=
fderiv ℝ F y (Pi.single j 1)
end NonlinearICAAux
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Throughout, are natural numbers (any of them may be ). means real -tuples with the product Lebesgue measure. For this is a one-point space of measure . A family always denotes arbitrary functions , written , with no regularity unless a definition imposes it. Whenever a derivative is taken of a function that is not differentiable at the relevant point, the value used is . A Bochner integral of a non-integrable function is likewise taken to be .
IsC2Diffeo. For a bijection given together with its inverse , this is the property that and are both twice continuously differentiable () on all of .
condLogDensityS. For and ,
This is the empty sum when .
IsAuxICAModel. Take a measure on , a family , and a bijection of . Nothing requires to be a probability measure, finite, or -finite, and is allowed. The property is the conjunction of four conditions:
- satisfies
IsC2Diffeo. - Each map is Borel measurable on .
- For every , the function is Lebesgue integrable on and .
- For every , the function is -integrable. This holds automatically if is finite and the integrand is bounded, and trivially if .
No condition links to or .
margDensityS.
This equals if the integrand is not -integrable, and in particular when .
absJacDet. For an arbitrary map and a point , this is , the absolute determinant of the (Fréchet) derivative of at . Edge cases:
- If is not differentiable at , the derivative is taken to be the zero map. The value is then when .
- When the value is always .
condDensityX. With ,
margDensityX. With ,
Here is the margDensityS above, built from .
logRatio.
The logarithm follows the convention (and for , which does not arise here). So when either density is , including the junk cases above, the corresponding term is rather than .
logisticLoss. For an arbitrary function , with no measurability required, this is the -valued lower Lebesgue integral
- is Lebesgue measure on .
- Each of the two products is truncated at from below; both products are in fact nonnegative.
- The integral is taken against the product of and itself.
- There is no factor or other normalisation.
regFun. Given functions and ,
where is the -th coordinate of .
wVec. For and , this is the vector whose two blocks are indexed by :
- first block, entry : ;
- second block, entry : .
Both are ordinary derivatives of at , with the value wherever the derivative does not exist.
Variability. The property holds if, for every , there exist points such that the vectors
are linearly independent in . They then form a basis, and in particular are pairwise distinct and nonzero. The points may depend on . For the family is empty and the property holds for every .
wAltVec. For , and an index , this is the vector with two blocks indexed by :
- first block, entry : the derivative at , in the direction of the -th unit vector , of ;
- second block, entry : the same for .
Each entry is the full (Fréchet) derivative in applied to . It is whenever that map is not Fréchet-differentiable at , even if the partial derivative in exists. The inner -derivatives also use the -junk convention.
AltVariability. The property holds if, for every , there exist an index and points such that the vectors
are linearly independent in . The same is used for all , though and the may depend on . Edge cases:
- If , no index exists and the property is false for every (and every ).
- If and , it is true for every .
condMarginal. For , and , this is
It is simply the normalised exponential of alone, not an integral of the joint density over the other coordinates. If is not integrable, the denominator is and the value is (division by zero yields ).
IsCondExpWith. For an index , this is a property of the data , , and . It holds when all four conditions below hold:
- for all .
- for all .
- are linearly independent as elements of the vector space of all functions .
- For all and ,
No measurability or variability is required of . Because the right side is strictly positive, condition 4 forces to be integrable for every . For , condition 3 is vacuous and condition 4 reads .
IsCondExp. For an index and an order , this holds if there exist , , and of the above types satisfying IsCondExpWith.
lamVec. For functions with and , this is the vector in (indexed by pairs ) with entries .
sstatVec. For functions , this is the vector indexed by pairs with entries . Each entry depends only on the -th coordinate of .
lamBar. Take a point and points , one for each pair . The result is the square matrix , with rows and columns both indexed by such pairs, whose entries are
regExp. For , , and ,
where the sum runs over all pairs .
partialDeriv. For , an index and a point , this is , the Fréchet derivative of at applied to the -th unit vector. It equals whenever is not Fréchet-differentiable at , even if the partial derivative exists there.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.