Mathlib.Analysis.Inner­Product­Space

InnerProductSpace — Mathlib-shaped helpers: FrischWaughLovell, Gram, Projection, RKHS.

Pos­Def 6 core · 9 supporting · 1 submodule PosDef — Mathlib-shaped helpers: Sqrt.
Frisch­Waugh­Lovell 10 core · 18 supporting This file formalizes the finite-dimensional Frisch--Waugh--Lovell residualization theorem on a real inner-product space with a nuisance subspace admitting an orthogonal projection. ★ fwlCoef_original_minimizes★ original_minimizer_eq_fwlCoef_projection

Frisch-Waugh-Lovell Algebra

This file formalizes the finite-dimensional Frisch--Waugh--Lovell residualization theorem on a real inner-product space with a nuisance subspace admitting an orthogonal projection. It is a causal-agnostic linear-algebra layer: downstream estimand-characterization modules instantiate this result, but the statements here only mention inner products, finite regressor tuples, Gram matrices, and least-squares objectives.

Main definitions: * residualize H v is the orthogonal residual v - P_H v. * fittedValue X β is the finite linear combination of regressors X. * gramResid H X is the Gram matrix of the residualized regressors. * residInnerVec H X Y is the right-hand side of the residualized normal equations. * fwlCoef H X Y is the coefficient vector obtained by multiplying that right-hand side by the nonsingular inverse of gramResid H X. * residualizedObjective and originalObjective are the least-squares objectives after residualization and before residualizing out the nuisance term.

Main results: * residualize_inner_swap_right shows that the outcome can be residualized in inner products against a residualized regressor. * gramResid_mulVec_fwlCoef gives the residualized normal equations. * fwlCoef_residualized_minimizes proves that fwlCoef minimizes the residualized objective. * fwlCoef_original_minimizes lifts that optimizer to the original objective with an explicit nuisance term in H. * fwlCoef_eq_of_original_minimizer and original_minimizer_eq_fwlCoef_projection give the coefficient and full optimizer uniqueness characterizations.

def residualize reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a subspace that admits an orthogonal projection, and a vector, the orthogonal residual is the vector minus its orthogonal projection onto that subspace.

Definition (Lean source)
V :
Type u_1
shared
H :
Submodule ℝ V
shared
v :
V
residualize H v :
V
v - H.orthogonalProjectionFn v
def residualizeL reviewed
Causalean.Mathlib.FWL

For a real inner-product space and a subspace that admits an orthogonal projection, the residual-maker linear map sends each vector to its orthogonal residual after removal of its projection onto that subspace.

Definition (Lean source)
V :
Type u_1
shared
H :
Submodule ℝ V
shared
residualizeL H :
V →L[ℝ] V
id ℝ V - H.starProjection
def fittedValue reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a finite number of regressors, a tuple of regressor vectors, and a vector of real coefficients, the fitted value is the sum of each regressor multiplied by its corresponding coefficient.

Definition (Lean source)
V :
Type u_1
shared
K :
ℕ
shared
X :
Fin K → V
shared
β :
Fin K → ℝ
fittedValue X β :
V
∑ j, β j • X j
def gramResid reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a subspace that admits an orthogonal projection, a finite number of regressors, and a tuple of regressor vectors, the residualized Gram matrix has as its (j,k)(j,k) entry the inner product of the orthogonal residuals of the jjth and kkth regressors.

Definition (Lean source)
V :
Type u_1
shared
H :
Submodule ℝ V
shared
K :
ℕ
shared
X :
Fin K → V
shared
gramResid H X :
Matrix (Fin K) (Fin K) ℝ
fun j k => inner ℝ (residualize H (X j)) (residualize H (X k))
def residInnerVec reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a subspace that admits an orthogonal projection, a finite number of regressors, a tuple of regressor vectors, and an outcome vector, the residualized inner-product vector assigns to each regressor index the inner product of that regressor's orthogonal residual with the outcome.

Definition (Lean source)
V :
Type u_1
shared
H :
Submodule ℝ V
shared
K :
ℕ
shared
X :
Fin K → V
shared
Y :
V
residInnerVec H X Y :
Fin K → ℝ
fun j => inner ℝ (residualize H (X j)) Y
def fwlCoef reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a subspace that admits an orthogonal projection, a finite number of regressors, a tuple of regressor vectors, and an outcome vector, the Frisch–Waugh–Lovell coefficient vector is the inverse residualized Gram matrix multiplied by the vector of inner products between residualized regressors and the outcome.

Definition (Lean source)
V :
Type u_1
shared
H :
Submodule ℝ V
shared
K :
ℕ
shared
X :
Fin K → V
shared
Y :
V
fwlCoef H X Y :
Fin K → ℝ
(gramResid H X)⁻¹.mulVec (residInnerVec H X Y)
def residualizedObjective reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a subspace that admits an orthogonal projection, a finite number of regressors, a tuple of regressor vectors, an outcome vector, and a vector of real coefficients, the residualized least-squares objective is the squared norm of the orthogonal residual of the outcome minus the fitted value formed from the orthogonal residuals of the regressors.

Definition (Lean source)
V :
Type u_1
shared
H :
Submodule ℝ V
shared
K :
ℕ
shared
X :
Fin K → V
shared
Y :
V
β :
Fin K → ℝ
residualizedObjective H X Y β :
ℝ
inner ℝ (residualize H Y - fittedValue (fun j => residualize H (X j)) β) (residualize H Y - fittedValue (fun j => residualize H (X j)) β)
def originalObjective reviewed
Causalean.Mathlib.FWL

For a real inner-product space, a finite number of regressors, a tuple of regressor vectors, an outcome vector, a vector of real coefficients, and a nuisance vector, the original least-squares objective is the squared norm of the outcome minus its fitted value and minus the nuisance vector.

Definition (Lean source)
V :
Type u_1
shared
K :
ℕ
shared
X :
Fin K → V
shared
Y :
V
β :
Fin K → ℝ
h :
V
originalObjective X Y β h :
ℝ
inner ℝ (Y - fittedValue X β - h) (Y - fittedValue X β - h)
theorem fwlCoef_original_minimizes reviewed
Causalean.Mathlib.FWL

Standard finite-dimensional Frisch–Waugh–Lovell theorem. On a real inner-product space with a nuisance subspace H admitting an orthogonal projection, and a finite tuple of regressors X, if the residualized Gram matrix of X has nonzero determinant, i.e. is invertible and a candidate nuisance vector h lies in H, then the original least-squares objective — evaluated at the FWL coefficient together with the orthogonal-projection nuisance term of its raw residual — is at most the original objective at any other coefficient vector β and nuisance term h.

Formal statement
V :
Type u_1
shared
H :
Submodule ℝ V
shared
K :
ℕ
shared
X :
Fin K → V
shared
Y :
V
hQ :
IsUnit (gramResid H X).det
β :
Fin K → ℝ
h :
V
hh :
h ∈ H
originalObjective X Y (fwlCoef H X Y) (H.orthogonalProjectionFn (Y - fittedValue X (fwlCoef H X Y)))
≤ originalObjective X Y β h
Proof (Lean source)
theorem fwlCoef_original_minimizes (Y : V) (hQ : IsUnit (gramResid H X).det) (β : Fin K → ℝ) {h : V} (hh : h ∈ H) : originalObjective X Y (fwlCoef H X Y) (H.orthogonalProjectionFn (Y - fittedValue X (fwlCoef H X Y))) ≤ originalObjective X Y β h := by calc originalObjective X Y (fwlCoef H X Y) (H.orthogonalProjectionFn (Y - fittedValue X (fwlCoef H X Y))) = residualizedObjective H X Y (fwlCoef H X Y) := by rw [originalObjective_projection_eq_residualizedObjective] _ ≤ residualizedObjective H X Y β := fwlCoef_residualized_minimizes H X Y hQ β _ ≤ originalObjective X Y β h := residualizedObjective_le_originalObjective H X Y β hh
theorem original_minimizer_eq_fwlCoef_projection reviewed
Causalean.Mathlib.FWL

Uniqueness of the original least-squares minimizer. If the residualized Gram matrix of X is invertible, a candidate nuisance vector h lies in H, and the pair (β, h) minimizes the original least-squares objective over all coefficient vectors and nuisance terms in H, then β equals the FWL coefficient and h equals the orthogonal-projection nuisance term of the FWL coefficient's raw residual.

Formal statement
V :
Type u_1
shared
H :
Submodule ℝ V
shared
K :
ℕ
shared
X :
Fin K → V
shared
Y :
V
hQ :
IsUnit (gramResid H X).det
β :
Fin K → ℝ
h :
V
hh :
h ∈ H
hmin :
∀ (γ : Fin K → ℝ) {g : V}
if
g ∈ H
then
originalObjective X Y β h ≤ originalObjective X Y γ g
conclusion 1
β = fwlCoef H X Y
conclusion 2
h = H.orthogonalProjectionFn (Y - fittedValue X (fwlCoef H X Y))
Proof (Lean source)
theorem original_minimizer_eq_fwlCoef_projection (Y : V) (hQ : IsUnit (gramResid H X).det) (β : Fin K → ℝ) {h : V} (hh : h ∈ H) (hmin : ∀ (γ : Fin K → ℝ) {g : V}, g ∈ H → originalObjective X Y β h ≤ originalObjective X Y γ g) : β = fwlCoef H X Y ∧ h = H.orthogonalProjectionFn (Y - fittedValue X (fwlCoef H X Y)) := by have hβ : β = fwlCoef H X Y := fwlCoef_eq_of_original_minimizer H X Y hQ β hh hmin have hp : h = H.orthogonalProjectionFn (Y - fittedValue X β) := by let p : V := H.orthogonalProjectionFn (Y - fittedValue X β) have hp_mem : p ∈ H := by dsimp [p] exact Submodule.starProjection_apply_mem H (Y - fittedValue X β) have hle : originalObjective X Y β h ≤ originalObjective X Y β p := hmin β hp_mem have hsplit := originalObjective_eq_residualizedObjective_add_projectionError H X Y β hh have hproj : originalObjective X Y β p = residualizedObjective H X Y β := by simpa [p] using originalObjective_projection_eq_residualizedObjective H X Y β rw [hsplit, hproj] at hle have hnonneg : 0 ≤ inner ℝ (H.orthogonalProjectionFn (Y - fittedValue X β) - h) (H.orthogonalProjectionFn (Y - fittedValue X β) - h) := by simp have hz : inner ℝ (H.orthogonalProjectionFn (Y - fittedValue X β) - h) (H.orthogonalProjectionFn (Y - fittedValue X β) - h) = 0 := by linarith have hzero : H.orthogonalProjectionFn (Y - fittedValue X β) - h = 0 := inner_self_eq_zero.mp hz rw [sub_eq_zero] at hzero exact hzero.symm constructor · exact hβ · rw [hp, hβ]
18 supporting declarations (lemmas, instances)
Gram 3 core · 0 supporting The file defines the Gram matrix gram k x, the positive-semidefinite-kernel predicate IsPSDkernel, and the regularized-Gram positive-definiteness theorem gram_add_lambda_posDef: if k is positive semidefinite and λ > 0, t ★ IsPSDkernel★ gram_add_lambda_posDef

Gram matrices of a kernel

The file defines the Gram matrix gram k x, the positive-semidefinite-kernel predicate IsPSDkernel, and the regularized-Gram positive-definiteness theorem gram_add_lambda_posDef: if k is positive semidefinite and λ > 0, then K + λI ≻ 0. This is the finite-sample invertibility fact used by kernel ridge regression.

def gram reviewed
Causalean.Mathlib.Analysis.InnerProductSpace

For an input space, a sample size, a real-valued kernel on that space, and a sample of that size, the Gram matrix is the matrix whose row-ii, column-jj entry is the kernel evaluated at the iith and jjth sample points.

Definition (Lean source)
X :
Type*
n :
ℕ
k :
X → X → ℝ
x :
Fin n → X
gram k x :
Matrix (Fin n) (Fin n) ℝ
fun i j => k (x i) (x j)
Causalean.Mathlib.Analysis.InnerProductSpace.gram · Causalean/Mathlib/Analysis/InnerProductSpace/Gram.lean:26
def IsPSDkernel reviewed
Causalean.Mathlib.Analysis.InnerProductSpace

For an input space and a real-valued kernel on that space, the positive-semidefinite-kernel property holds exactly when, for every finite sample, its Gram matrix is positive semidefinite.

Definition (Lean source)
X :
Type*
k :
X → X → ℝ
n :
ℕ
x :
Fin n → X
IsPSDkernel k :
Prop
(gram k x).PosSemidef
Causalean.Mathlib.Analysis.InnerProductSpace.IsPSDkernel · Causalean/Mathlib/Analysis/InnerProductSpace/Gram.lean:33
theorem gram_add_lambda_posDef reviewed
Causalean.Mathlib.Analysis.InnerProductSpace

For a finite sample x and kernel k, if k is positive semidefinite, i.e. every Gram matrix it produces is PSD and the regularization level lam is strictly positive, then the regularized Gram matrix gram k x + lam·I is positive definite, hence invertible.

Formal statement
X :
Type*
n :
ℕ
k :
X → X → ℝ
hk :
x :
Fin n → X
lam :
ℝ
hlam :
0 < lam
(gram k x + lam • (1 : Matrix (Fin n) (Fin n) ℝ)).PosDef
Proof (Lean source)
theorem gram_add_lambda_posDef {X : Type*} {n : ℕ} {k : X → X → ℝ} (hk : IsPSDkernel k) (x : Fin n → X) {lam : ℝ} (hlam : 0 < lam) : (gram k x + lam • (1 : Matrix (Fin n) (Fin n) ℝ)).PosDef := by have hpsd : (gram k x).PosSemidef := hk n x have hI : (lam • (1 : Matrix (Fin n) (Fin n) ℝ)).PosDef := by exact Matrix.PosDef.smul Matrix.PosDef.one hlam exact Matrix.PosDef.posSemidef_add hpsd hI
Causalean.Mathlib.Analysis.InnerProductSpace.gram_add_lambda_posDef · Causalean/Mathlib/Analysis/InnerProductSpace/Gram.lean:39 · uses IsPSDkernel , gram
Projection 1 core · 0 supporting This file proves exists_orthogonalProjection_of_posSemidef: every finite-dimensional subspace of a vector space over a linearly ordered field admits a linear map into that subspace whose residual is orthogonal, with resp ★ exists_orthogonalProjection_of_posSemidef

Semidefinite Normal-Equation Maps

This file proves exists_orthogonalProjection_of_posSemidef: every finite-dimensional subspace of a vector space over a linearly ordered field admits a linear map into that subspace whose residual is orthogonal, with respect to a symmetric positive-semidefinite bilinear form, to every vector in the subspace. The result supplies the linear-algebra substrate for weighted normal-equation arguments where the inner product may be degenerate.

lemma exists_orthogonalProjection_of_posSemidef reviewed
Causalean.Mathlib

In a vector space over a linearly ordered field equipped with a bilinear form B that is symmetric and positive-semidefinite, every finite-dimensional linear subspace H admits a linear self-map P of the ambient space, valued in H, whose residual X - P X is B-orthogonal to every vector of H, for every X.

Formal statement
K :
Type u_1
shared
V :
Type u_2
shared
B :
hsymm :
∀ x y, B x y = B y x
hpos :
∀ x, 0 ≤ B x x
∃ P : V →ₗ[K] V,
conclusion 1
X :
P X ∈ H
conclusion 2
X :
h ∈ H :
B (X - P X) h = 0
Proof (Lean source)
lemma exists_orthogonalProjection_of_posSemidef (B : BilinForm K V) (hsymm : ∀ x y, B x y = B y x) (hpos : ∀ x, 0 ≤ B x x) (H : Submodule K V) [FiniteDimensional K H] : ∃ P : V →ₗ[K] V, (∀ X, P X ∈ H) ∧ (∀ X, ∀ h ∈ H, B (X - P X) h = 0) := by classical let A : H →ₗ[K] Module.Dual K H := LinearMap.mk₂ K (fun x y : H => B x.1 y.1) (by intro x y z simp) (by intro a x z simp) (by intro x y z simp) (by intro a x z simp) let L : V →ₗ[K] Module.Dual K H := LinearMap.mk₂ K (fun (x : V) (y : H) => B x y.1) (by intro x y z simp) (by intro a x z simp) (by intro x y z simp) (by intro a x z simp) have hBsymm : B.IsSymm := ⟨fun x y => hsymm x y⟩ have hAflip : A.flip = A := by ext x y simpa [A] using (hsymm x.1 y.1).symm have hL_mem_range : ∀ X, L X ∈ range A := by intro X have hL_ann : L X ∈ (ker A).dualAnnihilator := by rw [Submodule.mem_dualAnnihilator] intro x hx have hxself : B x.1 x.1 = 0 := by have hAx : A x = 0 := by simpa [LinearMap.mem_ker] using hx have := congrArg (fun f : Module.Dual K H => f x) hAx simpa [A] using this have hxker : x.1 ∈ ker B := by exact (B.apply_apply_same_eq_zero_iff hpos hBsymm).mp hxself have hxX : B x.1 X = 0 := by have hBx : B x.1 = 0 := by simpa [LinearMap.mem_ker] using hxker have := congrArg (fun f : Module.Dual K V => f X) hBx simpa using this simpa [L, hsymm X x.1] using hxX rwa [LinearMap.dualAnnihilator_ker_eq_range_flip, hAflip] at hL_ann let Lrange : V →ₗ[K] range A := L.codRestrict (range A) hL_mem_range letI : Module.Free K (range A) := Module.Free.of_divisionRing K (range A) obtain ⟨S, hS⟩ := A.rangeRestrict.exists_rightInverse_of_surjective (LinearMap.range_rangeRestrict A) let PH : V →ₗ[K] H := S.comp Lrange refine ⟨H.subtype.comp PH, ?_, ?_⟩ · intro X exact (PH X).2 · intro X h hH let hh : H := ⟨h, hH⟩ have hEq : A (PH X) hh = L X hh := by have hRangeEq : A.rangeRestrict (S (Lrange X)) = Lrange X := by exact congrArg (fun f : range A →ₗ[K] range A => f (Lrange X)) hS have hValEq : (A.rangeRestrict (S (Lrange X)) : Module.Dual K H) = (Lrange X : Module.Dual K H) := congrArg (fun y : range A => (y : Module.Dual K H)) hRangeEq exact congrArg (fun f : Module.Dual K H => f hh) hValEq have hB_eq : B (PH X).1 h = B X h := by simpa [A, L, PH, hh] using hEq simp [hB_eq]
Causalean.Mathlib.exists_orthogonalProjection_of_posSemidef · Causalean/Mathlib/Analysis/InnerProductSpace/Projection.lean:45
RKHS 1 core · 0 supporting Mathlib's RKHS class describes Hilbert spaces continuously embedded in a function space and constructs kernel functions under completeness assumptions. ★ HasReproducingRepresenters

Abstract reproducing representers

Mathlib's RKHS class describes Hilbert spaces continuously embedded in a function space and constructs kernel functions under completeness assumptions. The HasReproducingRepresenters proposition here records only the identity f x = ⟪f, kₓ⟫ for a supplied evaluation and representer map. It does not assert that evaluation embeds H into a function space. This is the interface consumed by the representer theorem and the representer-ball Rademacher bound.

structure HasReproducingRepresenters reviewed
Causalean.ML

A real inner-product space has reproducing representers for a domain, an evaluation map, and a representer map when evaluation equals inner product against the point's representer. This does not assert that evaluation embeds the space into a function space.

Definition (Lean source)
feval :
H → X → ℝ
representer :
X → H
reproducing :
∀ (f : H) (x : X), feval f x = inner ℝ f (representer x)

The reproducing identity f x = ⟪f, kₓ⟫.

Causalean.ML.HasReproducingRepresenters · Causalean/Mathlib/Analysis/InnerProductSpace/RKHS.lean:24