ML.Linear
Roll-up of the linear-in-features regression family: ordinary least squares and ridge, including finite-sample optimization, closed-form normal-equation solutions, and population-risk target results.
Finite 3 core · 1 supporting The ordinary-least-squares objective on a finite design matrix X : Matrix Obs Param ℝ and its optimization property: any solution of the normal equations XᵀX β = Xᵀy minimizes the sum of squared errors. ★ ols_is_squaredLoss_ERM_of_normalEq
Linear least squares — finite design-matrix layer
The ordinary-least-squares objective on a finite design matrix `X : Matrix Obs
Param ℝ` and its optimization property: any solution of the normal equations
XᵀX β = Xᵀy minimizes the sum of squared errors. The bridge theorem
empiricalRisk_squaredLoss_linear identifies the spine's empiricalRisk
(squared loss, linear-in-features predictor) with (card)⁻¹ times this
objective, so finite OLS is a genuine ERM.
The linear prediction vector X β for a design matrix X and coefficients β.
Definition (Lean source)
The ordinary-least-squares objective: the sum of squared residuals ∑ᵢ (yᵢ − (Xβ)ᵢ)².
Definition (Lean source)
For any coefficient vector β̂ satisfying the normal equations XᵀX β̂ = Xᵀy built from a finite design matrix X and outcome vector y, that vector minimizes the sum of squared residuals over every coefficient vector β.
Formal statement
Proof (Lean source)
1 supporting declaration (lemmas, instances)
-
empiricalRisk_squaredLoss_lineartheorem — Bridge to the spine: the empirical squared-loss risk of the linear-in-features predictor x ↦ ⟪β, φ x⟫ equals (card ι)⁻¹ times the OLS objective of the design matrix Xᵢₖ = (φ xᵢ)ₖ.hypothesesconclusionempiricalRisk squaredLoss S (fun x => ∑ k, β k * φ.φ x k)= (Fintype.card ι : ℝ)⁻¹ * olsObjective (fun i k => φ.φ (S i).1 k) (fun i => (S i).2) βProof (Lean source)
theorem empiricalRisk_squaredLoss_linear {ι K X' : Type*} [Fintype ι] [Nonempty ι] [Fintype K] (φ : FeatureMap X' K) (S : ι → X' × ℝ) (β : K → ℝ) : empiricalRisk squaredLoss S (fun x => ∑ k, β k * φ.φ x k) = (Fintype.card ι : ℝ)⁻¹ * olsObjective (fun i k => φ.φ (S i).1 k) (fun i => (S i).2) β := by simp [empiricalRisk, squaredLoss, olsObjective, mulVec, dotProduct, mul_comm]
ClosedForm 2 core · 2 supporting The structural closed-form content of OLS. ★ olsCoef_normalEq
Linear least squares — closed form
The structural closed-form content of OLS. This file defines olsCoef, proves
ols_normalEq_of_minimizer from global optimality, and shows that invertible
XᵀX makes the normal-equation solution unique with closed form
β̂ = (XᵀX)⁻¹ Xᵀy.
The OLS closed-form coefficient (XᵀX)⁻¹ Xᵀy.
Definition (Lean source)
For a design matrix X and response vector y, if XᵀX is invertible, i.e. its determinant is a unit, then the closed-form OLS coefficient (XᵀX)⁻¹Xᵀy solves the normal equations (XᵀX)β = Xᵀy.
Formal statement
Proof (Lean source)
2 supporting declarations (lemmas, instances)
-
ols_normalEq_of_minimizertheorem — A minimizer of the least-squares objective solves the normal equations.hypothesesconclusion(Xᵀ * X) *ᵥ βhat = Xᵀ *ᵥ yProof (Lean source)
theorem ols_normalEq_of_minimizer (X : Matrix Obs Param ℝ) (y : Obs → ℝ) (βhat : Param → ℝ) (hmin : ∀ β, olsObjective X y βhat ≤ olsObjective X y β) : (Xᵀ * X) *ᵥ βhat = Xᵀ *ᵥ y := by classical let r : Obs → ℝ := fun i => y i - (X *ᵥ βhat) i let g : Param → ℝ := Xᵀ *ᵥ r have hdot_zero : ∀ v : Param → ℝ, g ⬝ᵥ v = 0 := by intro v let a : ℝ := ∑ i, (X *ᵥ v) i ^ 2 let c : ℝ := g ⬝ᵥ v have ha : 0 ≤ a := sum_nonneg fun i _hi => sq_nonneg ((X *ᵥ v) i) have hquad : ∀ t : ℝ, 0 ≤ -2 * t * c + t ^ 2 * a := by intro t let δ : Param → ℝ := t • v let z : Obs → ℝ := X *ᵥ δ have hres : ∀ i, y i - (X *ᵥ (βhat + t • v)) i = r i - z i := by intro i have hx : X *ᵥ (βhat + t • v) = X *ᵥ βhat + z := by change X *ᵥ (βhat + δ) = X *ᵥ βhat + z rw [Matrix.mulVec_add] rw [hx] simp [r, z] ring have hcross : r ⬝ᵥ z = t * c := by calc r ⬝ᵥ z = r ⬝ᵥ X *ᵥ δ := rfl _ = r ᵥ* X ⬝ᵥ δ := Matrix.dotProduct_mulVec r X δ _ = (Xᵀ *ᵥ r) ⬝ᵥ δ := by rw [Matrix.mulVec_transpose] _ = t * c := by simp [g, δ, c, dotProduct_smul, smul_eq_mul] have hzsum : ∑ i, z i ^ 2 = t ^ 2 * a := by simp [z, δ, a, Matrix.mulVec_smul, Pi.smul_apply, smul_eq_mul, Finset.mul_sum] ring_nf have htmp : olsObjective X y (βhat + t • v) = olsObjective X y βhat - 2 * (r ⬝ᵥ z) + ∑ i, z i ^ 2 := by unfold olsObjective simp_rw [hres] change (∑ i, (r i - z i) ^ 2) = (∑ i, r i ^ 2) - 2 * (r ⬝ᵥ z) + ∑ i, z i ^ 2 calc (∑ i, (r i - z i) ^ 2) = ∑ i, (r i ^ 2 - 2 * (r i * z i) + z i ^ 2) := by apply Finset.sum_congr rfl intro i _hi ring _ = (∑ i, r i ^ 2) - 2 * (r ⬝ᵥ z) + ∑ i, z i ^ 2 := by simp [dotProduct, Finset.sum_sub_distrib, Finset.sum_add_distrib, Finset.mul_sum] have hmin_t := hmin (βhat + t • v) nlinarith let t : ℝ := c / (a + 1) have hq := hquad t have hpos : 0 < a + 1 := by linarith have hpos2 : 0 < (a + 1) ^ 2 := sq_pos_of_pos hpos have hmul : 0 ≤ (-2 * t * c + t ^ 2 * a) * (a + 1) ^ 2 := mul_nonneg hq (le_of_lt hpos2) have hcalc : (-2 * t * c + t ^ 2 * a) * (a + 1) ^ 2 = - (a + 2) * c ^ 2 := by subst t field_simp [ne_of_gt hpos] ring have hc_nonpos : c ^ 2 ≤ 0 := by nlinarith [hmul, hcalc, sq_nonneg c] have hc : c = 0 := by nlinarith [sq_nonneg c] simpa [c] using hc have hg_zero : g = 0 := by ext j have hj := hdot_zero (single j 1) simpa [g, dotProduct, Pi.single_apply] using hj have hz : Xᵀ *ᵥ y - (Xᵀ * X) *ᵥ βhat = 0 := by have hg' : Xᵀ *ᵥ (y - X *ᵥ βhat) = 0 := by -- `y - X *ᵥ βhat` and `fun i => y i - (X *ᵥ βhat) i` are definitionally -- equal, but `simp` no longer bridges them, so close by `exact`. have h : Xᵀ *ᵥ r = 0 := hg_zero exact h rw [Matrix.mulVec_sub] at hg' simpa [Matrix.mulVec_mulVec] using hg' exact (sub_eq_zero.mp hz).symm -
olsCoef_uniquetheorem — When XᵀX is invertible, every normal-equation solution equals the closed form OLS coefficient.hypothesesconclusionβ = olsCoef X yProof (Lean source)
theorem olsCoef_unique [DecidableEq Param] (X : Matrix Obs Param ℝ) (y : Obs → ℝ) (hX : IsUnit (Xᵀ * X).det) {β : Param → ℝ} (hNE : (Xᵀ * X) *ᵥ β = Xᵀ *ᵥ y) : β = olsCoef X y := by unfold olsCoef rw [← hNE, Matrix.mulVec_mulVec, Matrix.nonsing_inv_mul _ hX, Matrix.one_mulVec]
Population 2 core · 0 supporting The best linear predictor: the coefficient vector whose residual is uncorrelated with every feature (the population normal equations) minimizes squared population risk over the linear-in-features class. ★ IsPopulationOLS★ bestLinearPredictor_minimizes_populationRisk
Linear least squares — population target
The best linear predictor: the coefficient vector whose residual is uncorrelated
with every feature (the population normal equations) minimizes squared population
risk over the linear-in-features class. This file formalizes the condition as
IsPopulationOLS and proves bestLinearPredictor_minimizes_populationRisk;
global optimality under correct specification is supplied by the spine theorem
square_loss_population_target_of_isL2Projection.
Population normal equations: the residual of the linear predictor with coefficients βstar is uncorrelated with every feature φ·ₖ.
Definition (Lean source)
For a probability measure P on features and outcome and a finite feature map φ, if the residual of the linear predictor with coefficients βstar is uncorrelated in expectation with every feature, the population squared-loss risks of the βstar- and β-predictors are both finite, and each feature is integrable against that residual, then the population squared risk of the βstar-predictor is at most that of any other linear-in-features predictor with coefficients β.