ExplainedVariance.lean
Lean 4 source, lean/DataMiningAsObservation/ExplainedVariance.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
ExplainedVariance.lean. Chapter 4 section 4.1, explained variance.
The explained variance of the first `k` components is the variance they carry over the
total. It lies in the unit interval, it cannot fall as `k` grows, and it reaches one at the
full dimension. It is the identity reader's criterion. A consumer that weights the
directions by its sensitivities retains a different fraction, and with variances 9 and 1
and a consumer that reads only the second direction, keeping the first component explains
nine tenths of the variance and none of what the consumer reads.
-/
import Mathlib
namespace DMO.ExplainedVariance
open Finset
/-- The variance carried by the first `k` of `n` components with variances `lam`. -/
def kept (n : ℕ) (lam : Fin n → ℝ) (k : ℕ) : ℝ := ∑ i, if i.val < k then lam i else 0
/-- The explained variance of the first `k` components. -/
noncomputable def explained (n : ℕ) (lam : Fin n → ℝ) (k : ℕ) : ℝ := kept n lam k / ∑ i, lam i
/-- **In the unit interval** for a nonnegative spectrum with positive total. -/
theorem explained_mem_unit (n : ℕ) (lam : Fin n → ℝ) (hn : ∀ i, 0 ≤ lam i) (hpos : 0 < ∑ i, lam i)
(k : ℕ) : 0 ≤ explained n lam k ∧ explained n lam k ≤ 1 := by
unfold explained kept
constructor
· apply div_nonneg _ hpos.le
apply Finset.sum_nonneg
intro i _
split_ifs
· exact hn i
· exact le_rfl
· rw [div_le_one hpos]
apply Finset.sum_le_sum
intro i _
split_ifs
· exact le_rfl
· exact hn i
/-- **More components explain no less.** -/
theorem explained_mono (n : ℕ) (lam : Fin n → ℝ) (hn : ∀ i, 0 ≤ lam i) (hpos : 0 < ∑ i, lam i)
(k : ℕ) : explained n lam k ≤ explained n lam (k + 1) := by
unfold explained kept
apply div_le_div_of_nonneg_right _ hpos.le
apply Finset.sum_le_sum
intro i _
split_ifs with h1 h2
· exact le_rfl
· omega
· exact hn i
· exact le_rfl
/-- **All components explain everything.** -/
theorem explained_full (n : ℕ) (lam : Fin n → ℝ) (hpos : 0 < ∑ i, lam i) : explained n lam n = 1 := by
unfold explained kept
have : ∀ i : Fin n, (if i.val < n then lam i else 0) = lam i := fun i => if_pos i.isLt
simp only [this]
exact div_self hpos.ne'
/-- The fraction a consumer retains, weighting each direction's variance by its sensitivity. -/
noncomputable def retained (n : ℕ) (lam s : Fin n → ℝ) (k : ℕ) : ℝ :=
(∑ i, if i.val < k then s i * lam i else 0) / ∑ i, s i * lam i
/-- **Explained variance is the identity reader's criterion.** With unit sensitivities the
retained fraction is the explained variance. -/
theorem retained_identity (n : ℕ) (lam : Fin n → ℝ) (k : ℕ) :
retained n lam (fun _ => 1) k = explained n lam k := by
simp [retained, explained, kept]
/-- **The identity reader's criterion can miss the consumer entirely.** Variances 9 and 1, a
consumer reading only the second direction, and one component kept. -/
theorem retained_example :
explained 2 (![9, 1]) 1 = 0.9 ∧ retained 2 (![9, 1]) (![0, 1]) 1 = 0 := by
unfold explained retained kept
constructor
· simp [Fin.sum_univ_two]
norm_num
· simp [Fin.sum_univ_two]
end DMO.ExplainedVariance