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.
Linear prediction assigns every observation its design-row score. It combines a design matrix on observation and coefficient indices with the chosen coefficients.
Definition (Lean source)
The ordinary least-squares objective is the residual sum of squares. It evaluates a coefficient vector against a design and outcome vector indexed by finite observation and coefficient sets.
Definition (Lean source)
Every normal-equation solution globally minimizes residual sum of squares. The result applies to the candidate coefficient vector under the normal equations built from the design and outcome vectors over finite observation and coefficient indices.
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 closed-form ordinary least-squares coefficient is the inverse Gram applied to the design--outcome cross-product. It uses a design matrix and outcome vector over finite observation and decidable coefficient indices; the totalized inverse agrees with the ordinary inverse when the cross-product matrix is invertible.
Definition (Lean source)
The closed-form OLS coefficient solves the normal equations for the design and response vectors over finite observation and coefficient indices when the design cross-product matrix is invertible.
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 := Finset.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 → ℝhX :IsUnit (Xᵀ * X).detβ :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]
L2BallRate 1 core · 1 supporting The statistical rate for empirical risk minimization over an L²-norm-bounded class of linear predictors a ↦ ⟪w, a⟫. ★ rademacherComplexity_l2_ball_le
L²-ball linear predictors — Rademacher rate
The statistical rate for empirical risk minimization over an L²-norm-bounded class of
linear predictors a ↦ ⟪w, a⟫. The theorem
rademacherComplexity_l2_ball_le bounds the expected Rademacher complexity of the
W-ball class over Xb-bounded features by Xb·W/√n; l2Ball_erm_excess_rate
combines that bound with the generic ERM oracle inequality for the displayed linear
functional. It is not a squared-loss theorem; the squared-loss excess-risk result is
l2Ball_erm_squaredLoss_excess_rate in Linear/L2BallSquaredLoss.lean.
Built on FoML's linear_predictor_l2_bound' (empirical Rademacher bound for the L²-ball,
lifted here to the expected rademacherComplexity).
Rademacher complexity of the L²-ball linear class. If the feature bound Xb is nonnegative, the weight bound W is nonnegative, and every feature vector has Euclidean norm at most Xb, then the expected Rademacher complexity of the class of linear predictors with weight norm at most W, on a sample of size n, is at most Xb·W/√n.
Formal statement
Proof (Lean source)
1 supporting declaration (lemmas, instances)
-
l2Ball_erm_excess_ratetheorem — L²-ball linear-functional ERM deviation rate. For features valued in the closed Xb-ball and linear predictors indexed by the closed W-ball, if the feature bound Xb is nonnegative, the weight bound W is nonnegative, the feature map X is measurable, the constant t satisfies the calibration t·(Xb·W)² ≤ 1/2, the tolerance ε is nonnegative, and the estimator ŵ attains an empirical linear functional no larger than that of the comparator wstar, then the probability that the corresponding excess population linear functional of ŵ over wstar exceeds 4·Xb·W/√n + 2ε is at most exp(-ε²tn).hypothesesd n :ℕΩ :Xb W :ℝhXb :0 ≤ XbhW :0 ≤ WX :hX :t :ℝht' :t * (Xb * W) ^ 2 ≤ 1 / 2ε :ℝhε :0 ≤ εŵ :wstar :hERM :∀ ω : Fin n → Ω,≤ (n : ℝ)⁻¹ * ∑ k, inner ℝ (wstar : EuclideanSpace ℝ (Fin d)) ((X (ω k)) : EuclideanSpace ℝ (Fin d))conclusion(Measure.pi (fun _ : Fin n => μ) (fun ω => 4 * (Xb * W / sqrt (n : ℝ)) + 2 * ε < μ[fun ω' => inner ℝ ((ŵ ω) : EuclideanSpace ℝ (Fin d)) ((X ω') : EuclideanSpace ℝ (Fin d))] - μ[fun ω' => inner ℝ (wstar : EuclideanSpace ℝ (Fin d)) ((X ω') : EuclideanSpace ℝ (Fin d))])).toReal≤ (- ε ^ 2 * t * n).expProof (Lean source)
theorem l2Ball_erm_excess_rate {d n : ℕ} {Ω : Type*} [MeasurableSpace Ω] {μ : Measure Ω} [IsProbabilityMeasure μ] {Xb W : ℝ} (hXb : 0 ≤ Xb) (hW : 0 ≤ W) (X : Ω → closedBall (0 : EuclideanSpace ℝ (Fin d)) Xb) (hX : Measurable X) {t : ℝ} (ht' : t * (Xb * W) ^ 2 ≤ 1 / 2) {ε : ℝ} (hε : 0 ≤ ε) (ŵ : (Fin n → Ω) → closedBall (0 : EuclideanSpace ℝ (Fin d)) W) (wstar : closedBall (0 : EuclideanSpace ℝ (Fin d)) W) (hERM : ∀ ω : Fin n → Ω, (n : ℝ)⁻¹ * ∑ k, inner ℝ ((ŵ ω) : EuclideanSpace ℝ (Fin d)) ((X (ω k)) : EuclideanSpace ℝ (Fin d)) ≤ (n : ℝ)⁻¹ * ∑ k, inner ℝ (wstar : EuclideanSpace ℝ (Fin d)) ((X (ω k)) : EuclideanSpace ℝ (Fin d))) : (Measure.pi (fun _ : Fin n => μ) (fun ω => 4 * (Xb * W / sqrt (n : ℝ)) + 2 * ε < μ[fun ω' => inner ℝ ((ŵ ω) : EuclideanSpace ℝ (Fin d)) ((X ω') : EuclideanSpace ℝ (Fin d))] - μ[fun ω' => inner ℝ (wstar : EuclideanSpace ℝ (Fin d)) ((X ω') : EuclideanSpace ℝ (Fin d))])).toReal ≤ (- ε ^ 2 * t * n).exp := by classical let 𝒳 := closedBall (0 : EuclideanSpace ℝ (Fin d)) Xb let ι := closedBall (0 : EuclideanSpace ℝ (Fin d)) W let f : ι → 𝒳 → ℝ := fun w a => inner ℝ (w : EuclideanSpace ℝ (Fin d)) (a : EuclideanSpace ℝ (Fin d)) haveI : Nonempty 𝒳 := ⟨⟨0, by simpa [Metric.mem_closedBall, dist_self, 𝒳] using hXb⟩⟩ haveI : Nonempty ι := ⟨⟨0, by simpa [Metric.mem_closedBall, dist_self, ι] using hW⟩⟩ have hb : 0 ≤ Xb * W := mul_nonneg hXb hW have hf : ∀ w : ι, Measurable (f w) := by intro w dsimp [f] fun_prop have hf' : ∀ w : ι, ∀ a : 𝒳, |f w a| ≤ Xb * W := by intro w a have hw : ‖(w : EuclideanSpace ℝ (Fin d))‖ ≤ W := by simpa [ι] using (mem_closedBall_zero_iff.mp w.2) have ha : ‖(a : EuclideanSpace ℝ (Fin d))‖ ≤ Xb := by simpa [𝒳] using (mem_closedBall_zero_iff.mp a.2) calc |f w a| ≤ ‖(w : EuclideanSpace ℝ (Fin d))‖ * ‖(a : EuclideanSpace ℝ (Fin d))‖ := by dsimp [f] exact abs_real_inner_le_norm _ _ _ ≤ W * Xb := mul_le_mul hw ha (norm_nonneg _) hW _ = Xb * W := by ring have hf'' : ∀ a : 𝒳, Continuous fun w : ι => f w a := by intro a dsimp [f] fun_prop have hRC : rademacherComplexity n f μ X ≤ Xb * W / sqrt (n : ℝ) := by have hpoint : ∀ ω : Fin n → Ω, empiricalRademacherComplexity n f (X ∘ ω) ≤ Xb * W / sqrt (n : ℝ) := by intro ω let Y' : Fin n → closedBall (0 : EuclideanSpace ℝ (Fin d)) Xb := X ∘ ω have h := linear_predictor_l2_bound' (d := d) (n := n) (ι := ι) (W := W) (X := Xb) hXb hW Y' id exact h have hnonneg : ∀ ω : Fin n → Ω, 0 ≤ empiricalRademacherComplexity n f (X ∘ ω) := by intro ω unfold empiricalRademacherComplexity refine mul_nonneg ?_ ?_ · positivity · refine Finset.sum_nonneg ?_ intro σ _ refine Real.iSup_nonneg ?_ intro w exact abs_nonneg _ unfold rademacherComplexity calc ∫ ω : Fin n → Ω, empiricalRademacherComplexity n f (X ∘ ω) ∂(Measure.pi fun _ : Fin n => μ) ≤ ∫ _ω : Fin n → Ω, Xb * W / sqrt (n : ℝ) ∂(Measure.pi fun _ : Fin n => μ) := by apply MeasureTheory.integral_mono_of_nonneg · exact Filter.Eventually.of_forall hnonneg · exact integrable_const _ · exact Filter.Eventually.of_forall hpoint _ = Xb * W / sqrt (n : ℝ) := by simp have key := erm_oracle_inequality_separable (μ := μ) (n := n) (f := f) hf X hX (b := Xb * W) hb hf' hf'' ht' hε ŵ wstar hERM refine le_trans ?_ key refine ENNReal.toReal_mono (measure_ne_top _ _) (measure_mono ?_) intro ω hω have hthreshold : 4 • rademacherComplexity n f μ X + 2 * ε ≤ 4 * (Xb * W / sqrt (n : ℝ)) + 2 * ε := by simp [nsmul_eq_mul] nlinarith [hRC] exact lt_of_le_of_lt hthreshold hω
L2BallSquaredLoss 3 core · 0 supporting The genuine squared-loss statistical rate for empirical risk minimization over an L²-norm-bounded class of linear predictors a ↦ ⟪w, a⟫. ★ l2Ball_erm_squaredLoss_excess_rate
L²-ball linear predictors — squared-loss excess-risk rate
The genuine squared-loss statistical rate for empirical risk minimization over an
L²-norm-bounded class of linear predictors a ↦ ⟪w, a⟫. Unlike the
bare-linear-functional version, the loss here is the regression loss (⟪w, x⟩ − y)².
The Rademacher complexity of the squared-loss class is bounded by decomposing the centered
loss (⟪w,x⟩ − y)² − y² = ⟪w,x⟩² − 2y⟪w,x⟩:
* the quadratic part ⟪w,x⟩² is handled by the (infinite-index) Ledoux–Talagrand
contraction with the clamped square as the Lipschitz surrogate, reducing to the linear
L²-ball bound;
* the cross part 2y⟪w,x⟩ = ⟪w, 2y·x⟩ is itself a linear class over rescaled features,
handled directly by the linear L²-ball bound.
Combined with the generic ERM oracle inequality (erm_oracle_inequality_separable) this gives
the O(1/√n) squared-loss excess-risk rate l2Ball_erm_squaredLoss_excess_rate.
The bounded feature--response space restricts features and responses to their closed balls. Its feature dimension, feature-radius bound, and response-radius bound specify the learning domain.
Definition (Lean source)
The constrained weight space is the centered closed Euclidean ball, in the chosen feature dimension with weight-radius bound.
Definition (Lean source)
L²-ball linear ERM squared-loss excess-risk rate. For data pairing features in the closed Xb-ball with responses in [-Yb, Yb], and linear predictors indexed by the closed W-ball, if Xb is nonnegative, Yb is nonnegative, W is nonnegative, the data map X is measurable, the constant t satisfies the calibration t·((Xb·W)² + 2·Yb·Xb·W)² ≤ 1/2, the tolerance ε is nonnegative, and the estimator ŵ attains empirical squared loss no larger than that of the comparator wstar, then for the squared loss (⟪w,x⟩ − y)², the probability that the excess population risk of ŵ over wstar exceeds 4·(4(XbW)² + 2·Yb·Xb·W)/√n + 2ε is at most exp(-ε²tn).
Formal statement
Proof (Lean source)
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_isResidualOrthogonal.
The population ordinary-least-squares condition requires residual orthogonality to every feature. It evaluates a coefficient vector and finite feature map under a joint covariate--response law.
Definition (Lean source)
Residual orthogonality makes a linear predictor population-risk optimal. The comparison uses the law, feature map, and comparator on the covariate and feature spaces, assuming residual orthogonality, finite risks for both predictors, and integrable residual--feature products.