Mean-field Gaussian family, KL-optimality, and the kernel, permutation and Cholesky matrices of stochastic SVGD
DefinitionArxiv2210_03613_BasicObjects of Sections 2.1, 2.2 and 2.4 of Zhang, Lomas, Zhou, Zheng and Curtis (2022).
- Mean-field Gaussian family (eq. 9). On the unconstrained parameter space (a finite index set ), , the Gaussian with mean and independent coordinates of standard deviations .
- KL-optimal member (eq. 2). For a target probability measure , is KL-optimal if for all , .
- Kernel matrices (eqs. 22, 25). For a function and particles , is . The matrix acts on the augmented vector , with entries indexed by (particle, coordinate). has blocks and is indexed by (coordinate, particle).
- Permutation (eq. 26). maps , listed particle by particle, to the vector listing the first coordinates of all particles, then the second, and so on: .
- Cholesky factor. is the lower triangular Cholesky factor of if , is lower triangular for the order of the index set, and for every . The index set of is ordered lexicographically by (coordinate, particle).
- sSVGD noise (eq. 24). At step size , the noise term of stochastic SVGD has law .
Formalization Note Gaussian laws are Mathlib's multivariateGaussian on EuclideanSpace, and the KL divergence is Mathlib's klDiv, which takes values in . Vectors indexed by (coordinate, particle) carry Mathlib's lexicographic order (Lex), so that "lower triangular" refers to the coordinate-major ordering of the paper.
import Mathlib
/-!
Zhang, Lomas, Zhou, Zheng and Curtis, *3D Bayesian Variational Full Waveform Inversion*,
arXiv:2210.03613v2 (2022): the mean-field Gaussian family of ADVI and its KL-optimal members
(Sections 2.1-2.2, eqs. (2), (9)), and the kernel matrices, permutation, Cholesky factors and
noise law of stochastic SVGD (Section 2.4, eqs. (22), (24)-(27)).
-/
open MeasureTheory ProbabilityTheory InformationTheory
noncomputable section
namespace Arxiv2210_03613
section ADVI
variable {ι : Type*} [Fintype ι] [DecidableEq ι]
/-- Eq. (9), Section 2.2, p. 9: the factorised (mean-field) Gaussian variational family
`q(θ; ζ) = N(θ | μ, diag(exp(ω)²))` on the unconstrained space, with mean `μ` and standard
deviations `σ = exp(ω)`. -/
def meanFieldGaussian (μ : EuclideanSpace ℝ ι) (ω : ι → ℝ) : Measure (EuclideanSpace ℝ ι) :=
multivariateGaussian μ (Matrix.diagonal fun i => Real.exp (ω i) ^ 2)
/-- Eq. (2), Section 2.1, p. 7, with the family `Q` of eq. (9): the member `q(θ; μ, ω)` of the
mean-field Gaussian family minimises `KL[q || p]` over that family. -/
def IsMeanFieldKLMin (p : Measure (EuclideanSpace ℝ ι)) (μ : EuclideanSpace ℝ ι) (ω : ι → ℝ) :
Prop :=
∀ (μ' : EuclideanSpace ℝ ι) (ω' : ι → ℝ),
klDiv (meanFieldGaussian μ ω) p ≤ klDiv (meanFieldGaussian μ' ω') p
end ADVI
section SSVGD
variable {n d : ℕ}
/-- Section 2.4, p. 15 (below eq. (25)): the `n × n` kernel matrix of the particles,
`K̄_ij = k(m_i, m_j)`, for particles `m_1, …, m_n ∈ ℝ^d`. -/
def gramMat (k : (Fin d → ℝ) → (Fin d → ℝ) → ℝ) (m : Fin n → Fin d → ℝ) :
Matrix (Fin n) (Fin n) ℝ :=
fun i j => k (m i) (m j)
/-- Eq. (22), Section 2.4, p. 14: the `nd × nd` matrix
`K = (1/n) [k(m_i, m_j) I_{d×d}]_{i,j}` on the augmented space `z = (m_1, …, m_n)`, whose
entries are indexed by (particle, coordinate). -/
def svgdK (k : (Fin d → ℝ) → (Fin d → ℝ) → ℝ) (m : Fin n → Fin d → ℝ) :
Matrix (Fin n × Fin d) (Fin n × Fin d) ℝ :=
fun x y => (1 / (n : ℝ)) * (if x.2 = y.2 then k (m x.1) (m y.1) else 0)
/-- Eq. (26), Section 2.4, p. 15: the permutation matrix `P` that reorders `z` from the basis in
which the particles are listed one after another, (particle, coordinate), to the basis in which
the first coordinates of all particles are listed, then the second, and so on,
(coordinate, particle) in lexicographic order: `(P z)_(a, i) = z_(i, a)`. -/
def permP (n d : ℕ) : Matrix (Lex (Fin d × Fin n)) (Fin n × Fin d) ℝ :=
fun x y => if (ofLex x).1 = y.2 ∧ (ofLex x).2 = y.1 then 1 else 0
/-- Eq. (25), Section 2.4, p. 15: the block-diagonal matrix
`D_K = (1/n) diag(K̄, …, K̄)` with `d` diagonal blocks, indexed by (coordinate, particle) in
lexicographic order. -/
def blockDK (k : (Fin d → ℝ) → (Fin d → ℝ) → ℝ) (m : Fin n → Fin d → ℝ) :
Matrix (Lex (Fin d × Fin n)) (Lex (Fin d × Fin n)) ℝ :=
fun x y => (1 / (n : ℝ)) *
(if (ofLex x).1 = (ofLex y).1 then gramMat k m (ofLex x).2 (ofLex y).2 else 0)
/-- Section 2.4, p. 15 (below eq. (27)): `L` is the lower triangular Cholesky factor of `A`:
`L Lᵀ = A`, `L` is lower triangular for the order of the index set, and its diagonal is
positive. -/
def IsLowerCholesky {κ : Type*} [Fintype κ] [LinearOrder κ] (L A : Matrix κ κ ℝ) : Prop :=
L * L.transpose = A ∧ (∀ i j, i < j → L i j = 0) ∧ ∀ i, 0 < L i i
/-- Eq. (24), Section 2.4, p. 14: the law `N(0, 2 ε_t K)` of the noise term of stochastic SVGD
at step size `ε_t`. -/
def ssvgdNoise (ε : ℝ) (k : (Fin d → ℝ) → (Fin d → ℝ) → ℝ) (m : Fin n → Fin d → ℝ) :
Measure (EuclideanSpace ℝ (Fin n × Fin d)) :=
multivariateGaussian 0 ((2 * ε) • svgdK k m)
end SSVGD
end Arxiv2210_03613
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
meanFieldGaussian. Let be a finite index set with decidable equality; it may be empty. Given a mean vector (with the Euclidean structure) and an arbitrary real vector , this defines a measure on . It is the library's multivariate Gaussian with mean and diagonal covariance matrix
written . Every diagonal entry is strictly positive for every real , so the covariance is always positive definite. The coordinates are independent, and coordinate has standard deviation . No constraint is placed on or . If is empty, the space is a single point and the measure is the unit mass on it.
IsMeanFieldKLMin. Let be as above, and let be an arbitrary measure on . Nothing requires to be a probability measure, finite, or absolutely continuous with respect to anything. For a mean and a vector , the proposition asserts:
Here is the mean-field Gaussian defined above. In words, attains the minimum of the KL divergence to over the whole family of such Gaussians; the minimizer is not required to be unique.
is the library's Kullback–Leibler divergence, which takes values in the extended range . It equals unless and the log-likelihood ratio is -integrable. In the finite case it equals
truncated below at , where the total masses are taken as real numbers. This reduces to the usual KL divergence when is a probability measure.
Two edge cases follow from this:
- If every member of the family has infinite divergence from , every pair satisfies the proposition. This happens, for example, if is singular with respect to Lebesgue measure.
- If is empty, the family has only one member, so the condition is automatically satisfied.
gramMat. Fix natural numbers , either of which may be . Let be an arbitrary function, and let be a list of points ("particles"). This defines the real matrix
No symmetry, positivity, positive semidefiniteness, or regularity is assumed of , so need not be symmetric or positive semidefinite. If , it is the empty matrix.
svgdK. With as above, this defines a square real matrix . Its rows and columns are indexed by pairs , where is a particle index and is a coordinate index. The entries are
where is if and otherwise. In block form, with blocks indexed by particles, . No ordering of the index pairs is part of this definition. If or , the index set is empty and is the empty matrix, so the junk value never actually enters. is again arbitrary, so need not be symmetric or positive semidefinite.
permP. For natural numbers , this defines a real matrix whose two index sets differ:
- Rows are indexed by pairs , coordinate first and then particle, with and . This index set carries the lexicographic order: first by , then by .
- Columns are indexed by pairs , particle first and then coordinate.
The entries are
Hence, for a vector indexed by (particle, coordinate), . simply relabels the entries of in coordinate-major order. If or , both index sets are empty.
blockDK. With as above, this defines a square real matrix . Its rows and columns are indexed by pairs , coordinate first and then particle, under the lexicographic order. The entries are
where is the matrix from gramMat. In the lexicographic ordering this is the block-diagonal matrix
with copies of . The factor multiplies here; it is not included in itself. If or , it is the empty matrix.
IsLowerCholesky. Let be a finite index set equipped with a linear order. For square real matrices and indexed by , the proposition asserts all three of the following:
- ;
- whenever in the given order of , i.e. every entry strictly above the diagonal vanishes;
- for every .
Nothing is assumed about . If is not symmetric positive definite, no such exists, because with invertible lower triangular is always symmetric positive definite. "Lower triangular" refers to whatever linear order carries; for the lexicographically ordered (coordinate, particle) index set this is the lexicographic order. If is empty, the proposition holds trivially.
ssvgdNoise. For a real number , an arbitrary function , and particles , this defines a measure on the Euclidean space , with coordinates indexed by (particle, coordinate) pairs . It is the library's multivariate Gaussian construction applied to mean and matrix argument
where is the matrix from svgdK. This is nominally the law .
No hypothesis is imposed, and three cases fall outside the nominal reading:
- may be zero or negative. If , the matrix argument is the zero matrix.
- Because is arbitrary, need not be symmetric or positive semidefinite. In that case the measure produced is whatever the library's construction returns for such an argument, and it is not guaranteed to be a Gaussian whose covariance equals .
- If or , the space is a single point.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.