ReadDistortion.lean

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

/-
ReadDistortion.lean. Equation 0.10.

The read distortion is the error as the consumer experiences it. With a finite weighting
of error samples δ_ω and a read operator P, the expected quadratic form E[δ'Pδ] equals
tr(P Σ_δ), where Σ_δ = E[δδ'] is the error's second-moment matrix, its covariance when the
error is centered. For the identity reader, P = I, this is the trace of Σ_δ, which is mean
squared error. No symmetry of P and no centering are needed.
-/
import Mathlib

namespace DMO.ReadDistortion

open Finset Matrix

variable {Ω : Type*} [Fintype Ω] {n : ℕ}

/-- The quadratic form `v'Pv`. -/
def quad (P : Matrix (Fin n) (Fin n) ℝ) (v : Fin n → ℝ) : ℝ := v ⬝ᵥ P.mulVec v

/-- The weighted second-moment matrix of the error samples, `Σ_δ = Σ_ω w_ω δ_ω δ_ω'`. -/
def errorCov (w : Ω → ℝ) (δ : Ω → Fin n → ℝ) : Matrix (Fin n) (Fin n) ℝ :=
  ∑ ω, w ω • vecMulVec (δ ω) (δ ω)

/-- Trace of `P` times a rank-one matrix is the quadratic form. -/
lemma trace_mul_vecMulVec (P : Matrix (Fin n) (Fin n) ℝ) (d : Fin n → ℝ) :
    (P * vecMulVec d d).trace = quad P d := by
  simp only [quad, Matrix.trace, Matrix.diag_apply, Matrix.mul_apply, vecMulVec_apply,
    dotProduct, Matrix.mulVec, Finset.mul_sum]
  exact Finset.sum_congr rfl fun i _ => Finset.sum_congr rfl fun j _ => by ring

/-- **Equation 0.10.** The expected read distortion is the trace of the read operator times
the error covariance. -/
theorem read_distortion (P : Matrix (Fin n) (Fin n) ℝ) (w : Ω → ℝ) (δ : Ω → Fin n → ℝ) :
    ∑ ω, w ω * quad P (δ ω) = (P * errorCov w δ).trace := by
  unfold errorCov
  rw [Matrix.mul_sum, Matrix.trace_sum]
  refine Finset.sum_congr rfl fun ω _ => ?_
  rw [Matrix.mul_smul, Matrix.trace_smul, trace_mul_vecMulVec, smul_eq_mul]

/-- **The identity reader.** With `P = I` the read distortion is the trace of the error
covariance, the total variance of the error, which is mean squared error. -/
theorem identity_reader (w : Ω → ℝ) (δ : Ω → Fin n → ℝ) :
    ∑ ω, w ω * quad (1 : Matrix (Fin n) (Fin n) ℝ) (δ ω) = (errorCov w δ).trace := by
  rw [read_distortion, Matrix.one_mul]

/-- The identity reader's quadratic form is the squared length of the error. -/
theorem quad_one (d : Fin n → ℝ) : quad (1 : Matrix (Fin n) (Fin n) ℝ) d = d ⬝ᵥ d := by
  simp [quad]

end DMO.ReadDistortion