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.
For a set of observations, a finite coefficient index set, a design matrix, and a coefficient vector, the linear prediction assigns to each observation its design-row weighted sum of coefficients.
Definition (Lean source)
For a finite set of observations, a finite coefficient index set, a design matrix, an outcome vector, and a coefficient vector, the ordinary least-squares objective is the sum over observations of squared differences between the observed outcome and its linear prediction.
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.
For a finite set of observations, a finite coefficient index set whose equality can be decided, a design matrix, and an outcome vector, the closed-form ordinary least-squares coefficient vector is the product of the totalized inverse of the design matrix's cross-product matrix—equal to its ordinary inverse when that matrix is invertible—and the design matrix transposed times the outcome vector.
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.hypothesesObs :sharedType u_1Param :sharedType u_2X :Matrix Obs Param ℝy :Obs → ℝβhat :Param → ℝhmin :∀ β, olsObjective X y βhat ≤ olsObjective X y βconclusion(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.hypothesesObs :sharedType u_1X :Matrix Obs Param ℝy :Obs → ℝβ :Param → ℝhNE :(Xᵀ * X) *ᵥ β = Xᵀ *ᵥ yconclusionβ = 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.
For a measurable covariate space, a finite feature index set, a joint covariate–response measure, a feature map, and a coefficient vector, the population ordinary-least-squares condition holds exactly when the integral of the product of the corresponding linear-predictor residual and each feature coordinate is zero.
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 β.