Trace.lean
Lean 4 source, lean/DataMiningAsObservation/Trace.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Trace.lean. Equations 0.3 and 0.10, the trace.
The trace is the sum of a matrix's diagonal. It is linear, the trace of the identity is the
dimension, the trace of an outer product is the squared length of the vector, the trace of
a product does not depend on the order, and the trace of a read operator times a rank-one
error is the read distortion of that error, which is the identity the book's central
quantity rests on. For a covariance the trace is the total variance.
-/
import Mathlib
import DataMiningAsObservation.ReadDistortion
namespace DMO.Trace
open Matrix Finset
variable {n : ℕ}
/-- **The trace is additive.** -/
theorem trace_add (A B : Matrix (Fin n) (Fin n) ℝ) : (A + B).trace = A.trace + B.trace :=
Matrix.trace_add A B
/-- **The trace scales.** -/
theorem trace_smul (c : ℝ) (A : Matrix (Fin n) (Fin n) ℝ) : (c • A).trace = c * A.trace :=
Matrix.trace_smul c A
/-- **The trace of the identity is the dimension.** -/
theorem trace_one : (1 : Matrix (Fin n) (Fin n) ℝ).trace = n := by
rw [Matrix.trace_one, Fintype.card_fin]
/-- **The trace of an outer product is the squared length.** -/
theorem trace_vecMulVec (v : Fin n → ℝ) : (vecMulVec v v).trace = v ⬝ᵥ v := by
simp [Matrix.trace, vecMulVec_apply, dotProduct]
/-- **The trace of a product does not depend on the order.** -/
theorem trace_mul_comm (A B : Matrix (Fin n) (Fin n) ℝ) : (A * B).trace = (B * A).trace :=
Matrix.trace_mul_comm A B
/-- **Equation 0.10 for one error.** The trace of the read operator times a rank-one error is
the read distortion of that error. -/
theorem trace_read (P : Matrix (Fin n) (Fin n) ℝ) (d : Fin n → ℝ) :
(P * vecMulVec d d).trace = DMO.ReadDistortion.quad P d :=
DMO.ReadDistortion.trace_mul_vecMulVec P d
/-- **The identity reader's trace is the squared error.** -/
theorem trace_identity_read (d : Fin n → ℝ) :
((1 : Matrix (Fin n) (Fin n) ℝ) * vecMulVec d d).trace = d ⬝ᵥ d := by
rw [Matrix.one_mul, trace_vecMulVec]
end DMO.Trace