ReadOperator.lean

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

/-
ReadOperator.lean. Equations 0.8, 0.9, 0.11, and 0.12.

The read operator is the workload average of the outer product of the consumer's sensitivity,
`P = Σ_ω w_ω g_ω g_ω'`. This file checks what chapter 0 says about it. Its diagonal is the
weighted mean squared sensitivity to each coordinate and its off-diagonal entries are
co-sensitivities. Its quadratic form is a weighted sum of squared directional derivatives, so
the operator is positive semidefinite. A direction is in its kernel exactly when it is in the
kernel of every row's sensitivity, so the nuisance of the average is the intersection of the
local nuisances. One row reads one direction. An affine consumer does not change along a
direction its gradient does not read, which is the global equivalence of equation 0.12, and
the consumer that reads the squared length of a row has a full-rank averaged operator on the
standard basis rows, which is why that equivalence is local for a nonlinear consumer.
-/
import Mathlib

namespace DMO.ReadOperator

open Finset Matrix

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

/-- The workload-averaged read operator of equation 0.9, with `g ω` the sensitivity of the
consumer at row `ω` and `w ω` the weight of that row. -/
def readOp (w : Ω → ℝ) (g : Ω → Fin n → ℝ) : Matrix (Fin n) (Fin n) ℝ :=
  ∑ ω, w ω • vecMulVec (g ω) (g ω)

/-- One rank-one term reads one direction. Its action on any vector is a multiple of `g`. -/
theorem rank_one_reads_one_direction (g v : Fin n → ℝ) :
    vecMulVec g g *ᵥ v = (g ⬝ᵥ v) • g := by
  ext i
  simp only [mulVec, dotProduct, vecMulVec_apply, Pi.smul_apply, smul_eq_mul, Finset.sum_mul]
  exact Finset.sum_congr rfl fun j _ => by ring

/-- The averaged operator acting on a vector is the weighted sum of the rows' sensitivities,
each scaled by its directional derivative along the vector. -/
theorem readOp_mulVec (w : Ω → ℝ) (g : Ω → Fin n → ℝ) (v : Fin n → ℝ) :
    readOp w g *ᵥ v = ∑ ω, (w ω * (g ω ⬝ᵥ v)) • g ω := by
  unfold readOp
  rw [Matrix.sum_mulVec]
  refine Finset.sum_congr rfl fun ω _ => ?_
  ext i
  simp only [mulVec, dotProduct, Matrix.smul_apply, vecMulVec_apply, Pi.smul_apply, smul_eq_mul]
  rw [Finset.mul_sum, Finset.sum_mul]
  exact Finset.sum_congr rfl fun j _ => by ring

/-- **Equation 0.11.** The quadratic form of the read operator along `v` is the weighted mean
of the squared directional derivative along `v`. -/
theorem quad_readOp (w : Ω → ℝ) (g : Ω → Fin n → ℝ) (v : Fin n → ℝ) :
    v ⬝ᵥ (readOp w g *ᵥ v) = ∑ ω, w ω * (g ω ⬝ᵥ v) ^ 2 := by
  rw [readOp_mulVec, dotProduct_sum]
  refine Finset.sum_congr rfl fun ω _ => ?_
  rw [dotProduct_smul, smul_eq_mul, dotProduct_comm]
  ring

/-- **Positive semidefinite.** With nonnegative weights the quadratic form is nonnegative. -/
theorem quad_readOp_nonneg (w : Ω → ℝ) (hw : ∀ ω, 0 ≤ w ω) (g : Ω → Fin n → ℝ)
    (v : Fin n → ℝ) : 0 ≤ v ⬝ᵥ (readOp w g *ᵥ v) := by
  rw [quad_readOp]
  exact Finset.sum_nonneg fun ω _ => mul_nonneg (hw ω) (sq_nonneg _)

/-- **The nuisance of the average is the intersection of the local nuisances.** With positive
weights, a direction is in the kernel of the averaged operator exactly when every row's
sensitivity is orthogonal to it. -/
theorem readOp_mulVec_eq_zero_iff (w : Ω → ℝ) (hw : ∀ ω, 0 < w ω) (g : Ω → Fin n → ℝ)
    (v : Fin n → ℝ) : readOp w g *ᵥ v = 0 ↔ ∀ ω, g ω ⬝ᵥ v = 0 := by
  constructor
  · intro h
    have hq : ∑ ω, w ω * (g ω ⬝ᵥ v) ^ 2 = 0 := by
      rw [← quad_readOp, h, dotProduct_zero]
    have hterm := (Finset.sum_eq_zero_iff_of_nonneg
      (fun ω _ => mul_nonneg (hw ω).le (sq_nonneg (g ω ⬝ᵥ v)))).1 hq
    intro ω
    have := hterm ω (Finset.mem_univ ω)
    rcases mul_eq_zero.1 this with h0 | h0
    · exact absurd h0 (hw ω).ne'
    · exact pow_eq_zero_iff (two_ne_zero) |>.1 h0
  · intro h
    rw [readOp_mulVec]
    exact Finset.sum_eq_zero fun ω _ => by rw [h ω, mul_zero, zero_smul]

/-- **Equation 0.8 read on the diagonal.** The diagonal entry for coordinate `i` is the
weighted mean of the squared sensitivity to that coordinate. -/
theorem readOp_diag (w : Ω → ℝ) (g : Ω → Fin n → ℝ) (i : Fin n) :
    readOp w g i i = ∑ ω, w ω * g ω i ^ 2 := by
  simp [readOp, Matrix.sum_apply, vecMulVec_apply, sq]

/-- An off-diagonal entry is a co-sensitivity, the weighted mean of the product of the two
sensitivities, and not an interaction term. -/
theorem readOp_offdiag (w : Ω → ℝ) (g : Ω → Fin n → ℝ) (i j : Fin n) :
    readOp w g i j = ∑ ω, w ω * (g ω i * g ω j) := by
  simp [readOp, Matrix.sum_apply, vecMulVec_apply]

/-- The read operator is symmetric. -/
theorem readOp_symm (w : Ω → ℝ) (g : Ω → Fin n → ℝ) (i j : Fin n) :
    readOp w g i j = readOp w g j i := by
  rw [readOp_offdiag, readOp_offdiag]
  exact Finset.sum_congr rfl fun ω _ => by ring

/-- **The same read operator is not the same observer.** Reversing the sign of every
sensitivity, which reverses every ranking the consumer produces, leaves the read operator
unchanged, so the operator fixes the read geometry and not the consumer. -/
theorem readOp_neg (w : Ω → ℝ) (g : Ω → Fin n → ℝ) :
    readOp w (fun ω => -g ω) = readOp w g := by
  unfold readOp
  refine Finset.sum_congr rfl fun ω _ => ?_
  rw [vecMulVec_neg, neg_vecMulVec, neg_neg]

/-! ### The affine consumer and the global equivalence of equation 0.12 -/

/-- An affine consumer, `x ↦ a ⬝ x + b`, whose sensitivity is `a` at every row. -/
def affine (a : Fin n → ℝ) (b : ℝ) (x : Fin n → ℝ) : ℝ := a ⬝ᵥ x + b

/-- **Global equivalence for an affine consumer.** Moving along a direction the gradient does
not read leaves the output unchanged at every input, so the quotient is exact. -/
theorem affine_const_along_nuisance (a : Fin n → ℝ) (b : ℝ) {v : Fin n → ℝ}
    (hv : a ⬝ᵥ v = 0) (x : Fin n → ℝ) : affine a b (x + v) = affine a b x := by
  unfold affine
  rw [dotProduct_add, hv, add_zero]

/-- The averaged operator of an affine consumer is rank one whatever the workload, the total
weight times the outer product of its gradient. -/
theorem readOp_affine (w : Ω → ℝ) (a : Fin n → ℝ) :
    readOp w (fun _ => a) = (∑ ω, w ω) • vecMulVec a a := by
  unfold readOp
  rw [Finset.sum_smul]

/-! ### The squared-length consumer, whose averaged operator has full rank -/

/-- The consumer that reads the squared length of a row. Its sensitivity at `x` is `2 x`. -/
def sqLength (x : Fin n → ℝ) : ℝ := x ⬝ᵥ x

/-- Along the standard basis rows with unit weight, the averaged operator of the
squared-length consumer is four times the identity, of full rank, although each row reads only
its own direction. The global equivalence of equation 0.12 therefore fails for it. -/
theorem readOp_sqLength_basis :
    readOp (fun _ : Fin n => (1 : ℝ)) (fun k : Fin n => (2 : ℝ) • Pi.single k 1)
      = (4 : ℝ) • (1 : Matrix (Fin n) (Fin n) ℝ) := by
  ext i j
  simp only [readOp, Matrix.sum_apply, Matrix.smul_apply, vecMulVec_apply, Pi.smul_apply,
    Pi.single_apply, smul_eq_mul, Matrix.one_apply, one_mul]
  by_cases hij : i = j
  · subst hij
    simp
    norm_num
  · rw [if_neg hij, mul_zero]
    apply Finset.sum_eq_zero
    intro k _
    by_cases hk : i = k
    · subst hk; simp [Ne.symm hij]
    · simp [hk]

end DMO.ReadOperator