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.

def linearPredict reviewed
Causalean.ML

The linear prediction vector X β for a design matrix X and coefficients β.

Definition (Lean source)
def linearPredict (X : Matrix Obs Param ℝ) (β : Param → ℝ) : Obs → ℝ := X *ᵥ β
Causalean.ML.linearPredict · Causalean/ML/Linear/Finite.lean:26
def olsObjective reviewed
Causalean.ML

The ordinary-least-squares objective: the sum of squared residuals ∑ᵢ (yᵢ − (Xβ)ᵢ)².

Definition (Lean source)
noncomputable def olsObjective (X : Matrix Obs Param ℝ) (y : Obs → ℝ) (β : Param → ℝ) : ℝ := ∑ i, (y i - (X *ᵥ β) i) ^ 2
Causalean.ML.olsObjective · Causalean/ML/Linear/Finite.lean:29
theorem ols_is_squaredLoss_ERM_of_normalEq reviewed
Causalean.ML

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
X :
Matrix Obs Param ℝ
y :
Obs → ℝ
βhat :
Param → ℝ
hNE :
(Xᵀ * X) *ᵥ βhat = Xᵀ *ᵥ y
∀ β : Param → ℝ, olsObjective X y βhat ≤ olsObjective X y β
Proof (Lean source)
theorem ols_is_squaredLoss_ERM_of_normalEq (X : Matrix Obs Param ℝ) (y : Obs → ℝ) (βhat : Param → ℝ) (hNE : (Xᵀ * X) *ᵥ βhat = Xᵀ *ᵥ y) : ∀ β : Param → ℝ, olsObjective X y βhat ≤ olsObjective X y β := by intro β let δ : Param → ℝ := β - βhat let r : Obs → ℝ := fun i => y i - (X *ᵥ βhat) i let z : Obs → ℝ := X *ᵥ δ have hr : Xᵀ *ᵥ r = 0 := by have hNE' : Xᵀ *ᵥ (X *ᵥ βhat) = Xᵀ *ᵥ y := by simpa only [Matrix.mulVec_mulVec] using hNE change Xᵀ *ᵥ (y - X *ᵥ βhat) = 0 rw [Matrix.mulVec_sub, hNE'.symm] simp have hβ : β = βhat + δ := by ext k simp [δ] have hres : ∀ i, y i - (X *ᵥ β) i = r i - z i := by intro i have hx : X *ᵥ β = X *ᵥ βhat + z := by rw [hβ, Matrix.mulVec_add] rw [hx] simp [r, z] ring have hcross : r ⬝ᵥ z = 0 := by calc r ⬝ᵥ z = r ⬝ᵥ X *ᵥ δ := rfl _ = r ᵥ* X ⬝ᵥ δ := Matrix.dotProduct_mulVec r X δ _ = (Xᵀ *ᵥ r) ⬝ᵥ δ := by rw [Matrix.mulVec_transpose] _ = 0 := by simp [hr] have hobj : olsObjective X y β = 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 hnonneg : 0 ≤ ∑ i, z i ^ 2 := sum_nonneg fun i _hi => sq_nonneg (z i) nlinarith
Causalean.ML.ols_is_squaredLoss_ERM_of_normalEq · Causalean/ML/Linear/Finite.lean:34 · uses olsObjective
1 supporting declaration (lemmas, instances)
Closed­Form 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.

def olsCoef reviewed
Causalean.ML

The OLS closed-form coefficient (XᵀX)⁻¹ Xᵀy.

Definition (Lean source)
noncomputable def olsCoef [DecidableEq Param] (X : Matrix Obs Param ℝ) (y : Obs → ℝ) : Param → ℝ := (Xᵀ * X)⁻¹ *ᵥ (Xᵀ *ᵥ y)
theorem olsCoef_normalEq reviewed
Causalean.ML

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
X :
Matrix Obs Param ℝ
y :
Obs → ℝ
hX :
IsUnit (Xᵀ * X).det
(Xᵀ * X) *ᵥ olsCoef X y = Xᵀ *ᵥ y
Proof (Lean source)
theorem olsCoef_normalEq [DecidableEq Param] (X : Matrix Obs Param ℝ) (y : Obs → ℝ) (hX : IsUnit (Xᵀ * X).det) : (Xᵀ * X) *ᵥ olsCoef X y = Xᵀ *ᵥ y := by unfold olsCoef rw [Matrix.mulVec_mulVec, Matrix.mul_nonsing_inv _ hX, Matrix.one_mulVec]
Causalean.ML.olsCoef_normalEq · Causalean/ML/Linear/ClosedForm.lean:110 · uses olsCoef
2 supporting declarations (lemmas, instances)
  • ols_normalEq_of_minimizer theorem — A minimizer of the least-squares objective solves the normal equations.
    X :
    Matrix Obs Param ℝ
    y :
    Obs → ℝ
    βhat :
    Param → ℝ
    hmin :
    ∀ β, olsObjective X y βhat ≤ olsObjective X y β
    (Xᵀ * X) *ᵥ βhat = Xᵀ *ᵥ y
    Proof (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
    Causalean.ML.ols_normalEq_of_minimizer · Causalean/ML/Linear/ClosedForm.lean:27
  • olsCoef_unique theorem — When XᵀX is invertible, every normal-equation solution equals the closed form OLS coefficient.
    X :
    Matrix Obs Param ℝ
    y :
    Obs → ℝ
    hX :
    IsUnit (Xᵀ * X).det
    β :
    Param → ℝ
    hNE :
    (Xᵀ * X) *ᵥ β = Xᵀ *ᵥ y
    β = olsCoef X y
    Proof (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]
    Causalean.ML.olsCoef_unique · Causalean/ML/Linear/ClosedForm.lean:119
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.

def IsPopulationOLS reviewed
Causalean.ML

Population normal equations: the residual of the linear predictor with coefficients βstar is uncorrelated with every feature φ·ₖ.

Definition (Lean source)
def IsPopulationOLS (P : Measure (X' × ℝ)) (φ : FeatureMap X' K) (βstar : K → ℝ) : Prop := ∀ k, ∫ z, (z.2 - ∑ j, βstar j * φ.φ z.1 j) * φ.φ z.1 k ∂P = 0
Causalean.ML.IsPopulationOLS · Causalean/ML/Linear/Population.lean:25 · uses FeatureMap
theorem bestLinearPredictor_minimizes_populationRisk reviewed
Causalean.ML

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 β.

Formal statement
P :
Measure (X' × ℝ)
φ :
βstar :
K → ℝ
hortho :
IsPopulationOLS P φ βstar
β :
K → ℝ
hint_star :
HasFinitePopulationRisk squaredLoss P (fun x => ∑ k, βstar k * φ.φ x k)
hint_β :
HasFinitePopulationRisk squaredLoss P (fun x => ∑ k, β k * φ.φ x k)
hcross :
∀ k, Integrable (fun z => (z.2 - ∑ j, βstar j * φ.φ z.1 j) * φ.φ z.1 k) P
populationRisk squaredLoss P (fun x => ∑ k, βstar k * φ.φ x k)
populationRisk squaredLoss P (fun x => ∑ k, β k * φ.φ x k)
Proof (Lean source)
theorem bestLinearPredictor_minimizes_populationRisk (P : Measure (X' × ℝ)) (φ : FeatureMap X' K) {βstar : K → ℝ} (hortho : IsPopulationOLS P φ βstar) (β : K → ℝ) (hint_star : HasFinitePopulationRisk squaredLoss P (fun x => ∑ k, βstar k * φ.φ x k)) (hint_β : HasFinitePopulationRisk squaredLoss P (fun x => ∑ k, β k * φ.φ x k)) (hcross : ∀ k, Integrable (fun z => (z.2 - ∑ j, βstar j * φ.φ z.1 j) * φ.φ z.1 k) P) : populationRisk squaredLoss P (fun x => ∑ k, βstar k * φ.φ x k) ≤ populationRisk squaredLoss P (fun x => ∑ k, β k * φ.φ x k) := by let m : X' → ℝ := fun x => ∑ k, βstar k * φ.φ x k let h : X' → ℝ := fun x => ∑ k, β k * φ.φ x k have hint_m' : Integrable (fun z : X' × ℝ => (z.2 - m z.1) ^ 2) P := by simpa [HasFinitePopulationRisk, squaredLoss, m] using hint_star have hint_h' : Integrable (fun z : X' × ℝ => (z.2 - h z.1) ^ 2) P := by simpa [HasFinitePopulationRisk, squaredLoss, h] using hint_β have hdiff : Integrable (fun z : X' × ℝ => (z.2 - h z.1) ^ 2 - (z.2 - m z.1) ^ 2) P := hint_h'.sub hint_m' have hmh_expand : ∀ z : X' × ℝ, m z.1 - h z.1 = ∑ k, (βstar k - β k) * φ.φ z.1 k := by intro z calc m z.1 - h z.1 = (∑ k, βstar k * φ.φ z.1 k) - ∑ k, β k * φ.φ z.1 k := rfl _ = ∑ k, (βstar k * φ.φ z.1 k - β k * φ.φ z.1 k) := by rw [Finset.sum_sub_distrib] _ = ∑ k, (βstar k - β k) * φ.φ z.1 k := by exact Finset.sum_congr rfl (fun k _ => by ring) have hcross_mh : Integrable (fun z : X' × ℝ => (z.2 - m z.1) * (m z.1 - h z.1)) P := by have hsum_int : Integrable (fun z : X' × ℝ => ∑ k, (βstar k - β k) * ((z.2 - m z.1) * φ.φ z.1 k)) P := by exact integrable_finset_sum (s := univ) (fun k _ => (hcross k).const_mul (βstar k - β k)) refine hsum_int.congr ?_ filter_upwards with z rw [hmh_expand z] rw [Finset.mul_sum] refine Finset.sum_congr rfl ?_ intro k _ ring have horth : ∫ z, (z.2 - m z.1) * (m z.1 - h z.1) ∂P = 0 := by calc ∫ z, (z.2 - m z.1) * (m z.1 - h z.1) ∂P = ∫ z, ∑ k, (βstar k - β k) * ((z.2 - m z.1) * φ.φ z.1 k) ∂P := by apply integral_congr_ae filter_upwards with z rw [hmh_expand z] rw [Finset.mul_sum] refine Finset.sum_congr rfl ?_ intro k _ ring _ = ∑ k, ∫ z, (βstar k - β k) * ((z.2 - m z.1) * φ.φ z.1 k) ∂P := by rw [integral_finset_sum] intro k _ exact (hcross k).const_mul (βstar k - β k) _ = ∑ k, (βstar k - β k) * ∫ z, (z.2 - m z.1) * φ.φ z.1 k ∂P := by refine Finset.sum_congr rfl ?_ intro k _ rw [integral_const_mul] _ = 0 := by rw [Finset.sum_eq_zero] intro k _ rw [hortho k] ring have hsq_int : Integrable (fun z : X' × ℝ => (m z.1 - h z.1) ^ 2) P := by have htmp : Integrable (fun z : X' × ℝ => ((z.2 - h z.1) ^ 2 - (z.2 - m z.1) ^ 2) - 2 * ((z.2 - m z.1) * (m z.1 - h z.1))) P := hdiff.sub (hcross_mh.const_mul 2) convert htmp using 1 funext z ring have hdiff_nonneg : 0 ≤ ∫ z, ((z.2 - h z.1) ^ 2 - (z.2 - m z.1) ^ 2) ∂P := by calc 0 ≤ ∫ z, (m z.1 - h z.1) ^ 2 ∂P := by exact integral_nonneg (fun z => sq_nonneg _) _ = 2 * ∫ z, (z.2 - m z.1) * (m z.1 - h z.1) ∂P + ∫ z, (m z.1 - h z.1) ^ 2 ∂P := by simp [horth] _ = ∫ z, 2 * ((z.2 - m z.1) * (m z.1 - h z.1)) + (m z.1 - h z.1) ^ 2 ∂P := by rw [integral_add] · rw [integral_const_mul] · exact hcross_mh.const_mul 2 · exact hsq_int _ = ∫ z, ((z.2 - h z.1) ^ 2 - (z.2 - m z.1) ^ 2) ∂P := by apply integral_congr_ae filter_upwards with z ring have hle : ∫ z, (z.2 - m z.1) ^ 2 ∂P ≤ ∫ z, (z.2 - h z.1) ^ 2 ∂P := by have hnonneg_sub : 0 ≤ ∫ z, (z.2 - h z.1) ^ 2 ∂P - ∫ z, (z.2 - m z.1) ^ 2 ∂P := by rw [← integral_sub hint_h' hint_m'] exact hdiff_nonneg linarith simpa [populationRisk, squaredLoss, m, h] using hle