ReconstructionError.lean

Lean 4 source, lean/DataMiningAsObservation/ReconstructionError.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
ReconstructionError.lean. Chapter 1 section 1.4 and chapter 4 section 4.1, the
reconstruction error.

The reconstruction error of a code is the squared length of the difference between a row
and its approximation, the identity reader's distortion. It is nonnegative, zero exactly
when the approximation is the row, a sum of per-coordinate squared errors, and for a
projection onto a unit direction it is the squared length less the squared component along
that direction, which is what the identity reader reads and no other reader does.
-/
import Mathlib
import DataMiningAsObservation.Projection

namespace DMO.ReconstructionError

open DMO.Projection

variable {n : ℕ}

/-- The reconstruction error. -/
def recon (x xhat : Fin n → ℝ) : ℝ := (x - xhat) ⬝ᵥ (x - xhat)

/-- **The error is nonnegative.** -/
theorem recon_nonneg (x xhat : Fin n → ℝ) : 0 ≤ recon x xhat := by
  unfold recon dotProduct
  exact Finset.sum_nonneg fun i _ => mul_self_nonneg _

/-- **The error is zero exactly when the approximation is the row.** -/
theorem recon_eq_zero_iff (x xhat : Fin n → ℝ) : recon x xhat = 0 ↔ xhat = x := by
  unfold recon
  rw [dotProduct_self_eq_zero]
  exact sub_eq_zero.trans eq_comm

/-- **The error is a sum of per-coordinate squared errors.** -/
theorem recon_sum (x xhat : Fin n → ℝ) : recon x xhat = ∑ i, (x i - xhat i) ^ 2 := by
  unfold recon dotProduct
  exact Finset.sum_congr rfl fun i _ => by simp [sq]

/-- **A projection's error is the squared length less the squared component.** -/
theorem recon_proj (u x : Fin n → ℝ) (hu : u ⬝ᵥ u = 1) :
    recon x (proj u x) = x ⬝ᵥ x - (u ⬝ᵥ x) ^ 2 := by
  unfold recon proj
  simp only [sub_dotProduct, dotProduct_sub, dotProduct_smul, smul_dotProduct, smul_eq_mul, hu,
    dotProduct_comm x u]
  ring

end DMO.ReconstructionError