Ensemble.lean
Lean 4 source, lean/DataMiningAsObservation/Ensemble.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Ensemble.lean. Chapter 7 section 7.1, the ensemble.
An ensemble averages several scorers. Its average lies between the smallest and the
largest member, and its squared error is at most the mean of the members' squared errors,
which is the arithmetic behind bagging reducing variance. Nothing here says when the
ensemble beats a formula, which chapter 6 measured.
-/
import Mathlib
namespace DMO.Ensemble
open Finset
variable {m : ℕ}
/-- The average of `m` member scores. -/
noncomputable def average (e : Fin m → ℝ) : ℝ := (∑ i, e i) / m
/-- **The average lies between the members' bounds.** -/
theorem average_between (e : Fin m → ℝ) (hm : 0 < m) (lo hi : ℝ) (hlo : ∀ i, lo ≤ e i)
(hhi : ∀ i, e i ≤ hi) : lo ≤ average e ∧ average e ≤ hi := by
unfold average
have hm' : (0 : ℝ) < m := by exact_mod_cast hm
constructor
· rw [le_div_iff₀ hm']
calc lo * m = ∑ _i : Fin m, lo := by simp [mul_comm]
_ ≤ ∑ i, e i := Finset.sum_le_sum fun i _ => hlo i
· rw [div_le_iff₀ hm']
calc ∑ i, e i ≤ ∑ _i : Fin m, hi := Finset.sum_le_sum fun i _ => hhi i
_ = hi * m := by simp [mul_comm]
/-- **The squared error of the average is at most the average squared error.** -/
theorem sq_average_le (e : Fin m → ℝ) (hm : 0 < m) :
average e ^ 2 ≤ average (fun i => e i ^ 2) := by
unfold average
have hm' : (0 : ℝ) < m := by exact_mod_cast hm
rw [div_pow, div_le_div_iff₀ (by positivity) hm']
have h := Finset.sum_mul_sq_le_sq_mul_sq Finset.univ (fun _ : Fin m => (1 : ℝ)) e
simp only [one_mul, one_pow, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul,
mul_one] at h
show (∑ i, e i) ^ 2 * (m : ℝ) ≤ (∑ i, e i ^ 2) * (m : ℝ) ^ 2
nlinarith [h, hm']
/-- **A constant ensemble is its constant.** -/
theorem average_const (hm : 0 < m) (c : ℝ) : average (fun _ : Fin m => c) = c := by
unfold average
have hm' : (m : ℝ) ≠ 0 := by exact_mod_cast hm.ne'
simp [Finset.sum_const, hm']
end DMO.Ensemble