Mathlib.Analysis.InnerProductSpace
InnerProductSpace — Mathlib-shaped helpers: FrischWaughLovell, Gram, Projection, RKHS.
FrischWaughLovell 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.
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)
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.
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.
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 entry the inner product of the orthogonal residuals of the th and th regressors.
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.
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)
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)
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)
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
Proof (Lean source)
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
Proof (Lean source)
18 supporting declarations (lemmas, instances)
-
residualize_deflemma — Residualization is the original vector minus its orthogonal projection onto the nuisance subspace.hypothesesconclusionresidualize H v = v - H.orthogonalProjectionFn vProof (Lean source)
@[simp] lemma residualize_def (v : V) : residualize H v = v - H.orthogonalProjectionFn v := rfl -
residualizeL_applylemma — Applying the residual-maker linear map gives the residualized vector.hypothesesconclusionresidualizeL H v = residualize H vProof (Lean source)
@[simp] lemma residualizeL_apply (v : V) : residualizeL H v = residualize H v := by simp [residualizeL, residualize] -
inner_residualize_of_memlemma — Residual is orthogonal to every element of H. This is the defining property of the orthogonal residual.hypothesesconclusioninner ℝ (residualize H v) w = 0Proof (Lean source)
lemma inner_residualize_of_mem (v : V) {w : V} (hw : w ∈ H) : inner ℝ (residualize H v) w = 0 := by simpa [residualize] using Submodule.orthogonalProjectionFn_inner_eq_zero (K := H) v w hw -
residualize_inner_swap_rightlemma — Symmetric residualization in the right slot. Since M_H is self-adjoint and idempotent, ⟨X̃, Y⟩ = ⟨X̃, Ỹ⟩.hypothesesconclusioninner ℝ (residualize H v) w = inner ℝ (residualize H v) (residualize H w)Proof (Lean source)
lemma residualize_inner_swap_right (v w : V) : inner ℝ (residualize H v) w = inner ℝ (residualize H v) (residualize H w) := by -- `w = residualize H w + P_H w` and `P_H w ∈ H`, which is orthogonal -- to `residualize H v`. have hPw : H.orthogonalProjectionFn w ∈ H := Submodule.starProjection_apply_mem H w have horth : inner ℝ (residualize H v) (H.orthogonalProjectionFn w) = 0 := inner_residualize_of_mem H v hPw have hsplit : w = residualize H w + H.orthogonalProjectionFn w := by simp [residualize, sub_add_cancel] calc inner ℝ (residualize H v) w = inner ℝ (residualize H v) (residualize H w + H.orthogonalProjectionFn w) := by rw [← hsplit] _ = inner ℝ (residualize H v) (residualize H w) + inner ℝ (residualize H v) (H.orthogonalProjectionFn w) := by rw [inner_add_right] _ = inner ℝ (residualize H v) (residualize H w) := by rw [horth, add_zero] -
fittedValue_sublemma — Fitted values are linear in the coefficient vector.hypothesesconclusionProof (Lean source)
lemma fittedValue_sub (β γ : Fin K → ℝ) : fittedValue X (fun j => β j - γ j) = fittedValue X β - fittedValue X γ := by simp [fittedValue, sub_smul, Finset.sum_sub_distrib] -
residualize_fittedValuelemma — Residualization commutes with forming a fitted value.hypothesesconclusionresidualize H (fittedValue X β) = fittedValue (fun j => residualize H (X j)) βProof (Lean source)
lemma residualize_fittedValue (β : Fin K → ℝ) : residualize H (fittedValue X β) = fittedValue (fun j => residualize H (X j)) β := by rw [← residualizeL_apply] change residualizeL H (fittedValue X β) = fittedValue (fun j => residualizeL H (X j)) β simp [fittedValue] -
residualize_regressionResiduallemma — Residualization of the raw regression residual is the residualized outcome minus the fitted value of the residualized regressors.hypothesesconclusionresidualize H (Y - fittedValue X β)= residualize H Y - fittedValue (fun j => residualize H (X j)) βProof (Lean source)
lemma residualize_regressionResidual (Y : V) (β : Fin K → ℝ) : residualize H (Y - fittedValue X β) = residualize H Y - fittedValue (fun j => residualize H (X j)) β := by rw [← residualizeL_apply] change residualizeL H (Y - fittedValue X β) = residualizeL H Y - fittedValue (fun j => residualizeL H (X j)) β rw [map_sub] rw [show residualizeL H (fittedValue X β) = fittedValue (fun j => residualizeL H (X j)) β by simpa [residualizeL_apply] using residualize_fittedValue H X β] -
gramResid_applylemma — Each entry of the residualized Gram matrix is the inner product of two residualized regressors.hypothesesconclusiongramResid H X j k = inner ℝ (residualize H (X j)) (residualize H (X k))Proof (Lean source)
@[simp] lemma gramResid_apply (j k : Fin K) : gramResid H X j k = inner ℝ (residualize H (X j)) (residualize H (X k)) := rfl -
residInnerVec_applylemma — Each entry of the residualized right-hand side is the inner product of a residualized regressor with the outcome.hypothesesconclusionresidInnerVec H X Y j = inner ℝ (residualize H (X j)) YProof (Lean source)
@[simp] lemma residInnerVec_apply (Y : V) (j : Fin K) : residInnerVec H X Y j = inner ℝ (residualize H (X j)) Y := rfl -
gramResid_mulVec_fwlCoeflemma — Residualized normal equations (FWL coefficient form). If Q_{XX} = gramResid H X is invertible (equivalently, the residualized regressors are linearly independent), the FWL coefficient fwlCoef H X Y = Q_{XX}⁻¹ *ᵥ (fun j => ⟨X̃ j, Y⟩) satisfies the residualized normal equations Q_{XX} *ᵥ fwlCoef H X Y = (fun j => ⟨X̃ j, Y⟩).hypothesesV :sharedType u_1K :sharedℕY :VconclusionProof (Lean source)
lemma gramResid_mulVec_fwlCoef (Y : V) (hQ : IsUnit (gramResid H X).det) : (gramResid H X).mulVec (fwlCoef H X Y) = residInnerVec H X Y := by -- Pure matrix algebra: `A *ᵥ (A⁻¹ *ᵥ b) = b` when `A.det` is a unit. unfold fwlCoef rw [Matrix.mulVec_mulVec, Matrix.mul_nonsing_inv _ hQ, Matrix.one_mulVec] -
residInnerVec_eq_residualize_rightlemma — Symmetric residualization for the FWL right-hand side. The residualized inner products ⟨X̃ j, Y⟩ agree with ⟨X̃ j, Ỹ⟩, so the FWL coefficient depends only on the residualized response.hypothesesconclusionresidInnerVec H X Y = residInnerVec H X (residualize H Y)Proof (Lean source)
lemma residInnerVec_eq_residualize_right (Y : V) : residInnerVec H X Y = residInnerVec H X (residualize H Y) := by funext j simpa [residInnerVec] using residualize_inner_swap_right H (X j) Y -
inner_residualizedRegressor_fwlResiduallemma — The residual left by fwlCoef is orthogonal to every residualized regressor. This is the normal-equation form used in the completing-square argument.hypothesesconclusioninner ℝ (residualize H (X j)) (residualize H Y - fittedValue (fun k => residualize H (X k)) (fwlCoef H X Y))= 0Proof (Lean source)
lemma inner_residualizedRegressor_fwlResidual (Y : V) (hQ : IsUnit (gramResid H X).det) (j : Fin K) : inner ℝ (residualize H (X j)) (residualize H Y - fittedValue (fun k => residualize H (X k)) (fwlCoef H X Y)) = 0 := by have hne := congrFun (gramResid_mulVec_fwlCoef H X Y hQ) j rw [← sub_eq_zero] rw [inner_sub_right] rw [fittedValue] simp_rw [inner_sum, inner_smul_right] rw [← residualize_inner_swap_right H (X j) Y] have hsum : (∑ x, fwlCoef H X Y x * inner ℝ (residualize H (X j)) (residualize H (X x))) = ∑ x, inner ℝ (residualize H (X j)) (residualize H (X x)) * fwlCoef H X Y x := by apply Finset.sum_congr rfl intro x _hx ring rw [hsum] simpa [mulVec, dotProduct, gramResid, residInnerVec] using sub_eq_zero.mpr hne.symm -
residualizedObjective_eq_fwlCoef_add_errortheorem — Completing-square identity for the residualized objective around the FWL coefficient.hypothesesconclusionresidualizedObjective H X Y β= residualizedObjective H X Y (fwlCoef H X Y)+ inner ℝ (fittedValue (fun j => residualize H (X j)) (fun j => fwlCoef H X Y j - β j)) (fittedValue (fun j => residualize H (X j)) (fun j => fwlCoef H X Y j - β j))Proof (Lean source)
theorem residualizedObjective_eq_fwlCoef_add_error (Y : V) (hQ : IsUnit (gramResid H X).det) (β : Fin K → ℝ) : residualizedObjective H X Y β = residualizedObjective H X Y (fwlCoef H X Y) + inner ℝ (fittedValue (fun j => residualize H (X j)) (fun j => fwlCoef H X Y j - β j)) (fittedValue (fun j => residualize H (X j)) (fun j => fwlCoef H X Y j - β j)) := by let Xtilde : Fin K → V := fun j => residualize H (X j) let b : Fin K → ℝ := fwlCoef H X Y let e : V := residualize H Y - fittedValue Xtilde b let z : V := fittedValue Xtilde (fun j => b j - β j) have hz_eq : z = fittedValue Xtilde b - fittedValue Xtilde β := by dsimp [z, b] rw [fittedValue_sub] have hdecomp : residualize H Y - fittedValue Xtilde β = e + z := by dsimp [e] rw [hz_eq] abel have hcross' : inner ℝ z e = 0 := by dsimp [z, e, Xtilde, b] rw [fittedValue] simp_rw [sum_inner, inner_smul_left] apply Finset.sum_eq_zero intro j _hj have hjzero : inner ℝ (X j - H.starProjection (X j)) (Y - H.starProjection Y - fittedValue (fun j => X j - H.starProjection (X j)) (fwlCoef H X Y)) = 0 := by simpa [residualize] using inner_residualizedRegressor_fwlResidual H X Y hQ j rw [hjzero] simp have hcross : inner ℝ e z = 0 := by rw [real_inner_comm, hcross'] have hpyth : residualizedObjective H X Y β = residualizedObjective H X Y b + inner ℝ z z := by change inner ℝ (residualize H Y - fittedValue Xtilde β) (residualize H Y - fittedValue Xtilde β) = inner ℝ e e + inner ℝ z z rw [hdecomp] rw [inner_add_left, inner_add_right, inner_add_right, hcross, hcross'] ring simpa [z, Xtilde, b] using hpyth -
fwlCoef_residualized_minimizestheorem — The FWL coefficient minimizes the residualized least-squares objective.Proof (Lean source)
theorem fwlCoef_residualized_minimizes (Y : V) (hQ : IsUnit (gramResid H X).det) (β : Fin K → ℝ) : residualizedObjective H X Y (fwlCoef H X Y) ≤ residualizedObjective H X Y β := by rw [residualizedObjective_eq_fwlCoef_add_error H X Y hQ β] exact le_add_of_nonneg_right (by simp) -
originalObjective_eq_residualizedObjective_add_projectionErrortheorem — Pythagorean split of the original objective into the residualized objective plus the squared distance from h to the projection of the raw regression residual onto H.hypothesesconclusionoriginalObjective X Y β h= residualizedObjective H X Y β+ inner ℝ (H.orthogonalProjectionFn (Y - fittedValue X β) - h) (H.orthogonalProjectionFn (Y - fittedValue X β) - h)Proof (Lean source)
theorem originalObjective_eq_residualizedObjective_add_projectionError (Y : V) (β : Fin K → ℝ) {h : V} (hh : h ∈ H) : originalObjective X Y β h = residualizedObjective H X Y β + inner ℝ (H.orthogonalProjectionFn (Y - fittedValue X β) - h) (H.orthogonalProjectionFn (Y - fittedValue X β) - h) := by let v : V := Y - fittedValue X β let r : V := residualize H v let p : V := H.orthogonalProjectionFn v - h have hp_mem : p ∈ H := by exact Submodule.sub_mem H (Submodule.starProjection_apply_mem H v) hh have horth : inner ℝ r p = 0 := by exact inner_residualize_of_mem H v hp_mem have horth' : inner ℝ p r = 0 := by rw [real_inner_comm, horth] have hvh : v - h = r + p := by dsimp [r, p, v] simp have hres_vec : residualize H Y - fittedValue (fun j => residualize H (X j)) β = r := by dsimp [r, v] exact (residualize_regressionResidual H X Y β).symm have hres : residualizedObjective H X Y β = inner ℝ r r := by change inner ℝ (residualize H Y - fittedValue (fun j => residualize H (X j)) β) (residualize H Y - fittedValue (fun j => residualize H (X j)) β) = inner ℝ r r rw [hres_vec] dsimp [originalObjective] change inner ℝ (v - h) (v - h) = residualizedObjective H X Y β + inner ℝ p p rw [hvh, hres] rw [inner_add_left, inner_add_right, inner_add_right, horth, horth'] ring -
residualizedObjective_le_originalObjectivetheorem — For fixed β, the residualized objective is the minimum of the original objective over the nuisance subspace H.hypothesesconclusionresidualizedObjective H X Y β ≤ originalObjective X Y β hProof (Lean source)
theorem residualizedObjective_le_originalObjective (Y : V) (β : Fin K → ℝ) {h : V} (hh : h ∈ H) : residualizedObjective H X Y β ≤ originalObjective X Y β h := by rw [originalObjective_eq_residualizedObjective_add_projectionError H X Y β hh] exact le_add_of_nonneg_right (by simp) -
originalObjective_projection_eq_residualizedObjectivetheorem — At the nuisance projection, the original objective equals the residualized objective.hypothesesconclusionoriginalObjective X Y β (H.orthogonalProjectionFn (Y - fittedValue X β))= residualizedObjective H X Y βProof (Lean source)
theorem originalObjective_projection_eq_residualizedObjective (Y : V) (β : Fin K → ℝ) : originalObjective X Y β (H.orthogonalProjectionFn (Y - fittedValue X β)) = residualizedObjective H X Y β := by have hmem : H.orthogonalProjectionFn (Y - fittedValue X β) ∈ H := Submodule.starProjection_apply_mem H (Y - fittedValue X β) rw [originalObjective_eq_residualizedObjective_add_projectionError H X Y β hmem] simp -
fwlCoef_eq_of_original_minimizertheorem — Any minimizer of the original least-squares problem has coefficient block equal to the FWL coefficient. This is the uniqueness part of the standard FWL statement for the X-block.hypothesesV :sharedType u_1K :sharedℕY :Vβ :Fin K → ℝh :Vhh :h ∈ Hhmin :∀ (γ : Fin K → ℝ) {g : V}ifg ∈ HthenoriginalObjective X Y β h ≤ originalObjective X Y γ gconclusionβ = fwlCoef H X YProof (Lean source)
theorem fwlCoef_eq_of_original_minimizer (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 := by let Xtilde : Fin K → V := fun j => residualize H (X j) let b : Fin K → ℝ := fwlCoef H X Y let z : V := fittedValue Xtilde (fun j => b j - β j) let hp : V := H.orthogonalProjectionFn (Y - fittedValue X b) have hp_mem : hp ∈ H := by dsimp [hp] exact Submodule.starProjection_apply_mem H (Y - fittedValue X b) have hle₁ : residualizedObjective H X Y β ≤ residualizedObjective H X Y b := by calc residualizedObjective H X Y β ≤ originalObjective X Y β h := residualizedObjective_le_originalObjective H X Y β hh _ ≤ originalObjective X Y b hp := hmin b hp_mem _ = residualizedObjective H X Y b := by simpa [hp] using originalObjective_projection_eq_residualizedObjective H X Y b have hle₂ : residualizedObjective H X Y b ≤ residualizedObjective H X Y β := fwlCoef_residualized_minimizes H X Y hQ β have hres_eq : residualizedObjective H X Y β = residualizedObjective H X Y b := le_antisymm hle₁ hle₂ have hpyth := residualizedObjective_eq_fwlCoef_add_error H X Y hQ β have hz_inner : inner ℝ z z = 0 := by rw [hres_eq] at hpyth have hpyth_z : residualizedObjective H X Y b = residualizedObjective H X Y b + inner ℝ z z := by simpa [z, Xtilde, b] using hpyth linarith have hz0 : z = 0 := inner_self_eq_zero.mp hz_inner let δ : Fin K → ℝ := fun j => b j - β j have hmul : (gramResid H X).mulVec δ = 0 := by funext j have hinner : inner ℝ (residualize H (X j)) z = 0 := by rw [hz0, inner_zero_right] dsimp [z, Xtilde, δ] at hinner ⊢ rw [fittedValue] at hinner simp_rw [inner_sum, inner_smul_right] at hinner simpa [mulVec, dotProduct, gramResid, mul_comm] using hinner have hδ0 : δ = 0 := Matrix.eq_zero_of_mulVec_eq_zero hQ.ne_zero hmul funext j have hj := congrFun hδ0 j dsimp [δ, b] at hj linarith
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.
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-, column- entry is the kernel evaluated at the th and th sample points.
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)
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
Proof (Lean source)
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.
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
Proof (Lean source)
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.
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)
The reproducing identity f x = ⟪f, kₓ⟫.