Alignment.lean

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

/-
Alignment.lean. Equation 4.6, the alignment and the coupling null.

In the diagonal two-dimensional setting of Flip.lean, with the read operator diag(p₁, p₂) and
the source covariance diag(v₁, v₂), both with nonnegative entries, the alignment is the
normalized overlap `(p₁v₁ + p₂v₂) / √((p₁² + p₂²)(v₁² + v₂²))`. It lies between zero and one,
and it equals one when the read operator is a positive multiple of the covariance. In that
case, the coupling null, the read distortion of every code, the trace of the read operator
times the code's error covariance, is a fixed positive multiple of its covariance-weighted
distortion, so the two rankings agree and no flip is possible.
-/
import Mathlib

namespace DMO.Alignment

/-- The alignment of equation 4.6 in the diagonal two-dimensional case, the cosine between
the two operators read as vectors of their diagonal entries. -/
noncomputable def alignment (p₁ p₂ v₁ v₂ : ℝ) : ℝ :=
  (p₁ * v₁ + p₂ * v₂) / Real.sqrt ((p₁ ^ 2 + p₂ ^ 2) * (v₁ ^ 2 + v₂ ^ 2))

/-- Cauchy–Schwarz for two coordinates. -/
theorem overlap_sq_le (p₁ p₂ v₁ v₂ : ℝ) :
    (p₁ * v₁ + p₂ * v₂) ^ 2 ≤ (p₁ ^ 2 + p₂ ^ 2) * (v₁ ^ 2 + v₂ ^ 2) := by
  nlinarith [sq_nonneg (p₁ * v₂ - p₂ * v₁)]

/-- **Alignment is at most one.** -/
theorem alignment_le_one (p₁ p₂ v₁ v₂ : ℝ) : alignment p₁ p₂ v₁ v₂ ≤ 1 := by
  unfold alignment
  rcases eq_or_lt_of_le (Real.sqrt_nonneg ((p₁ ^ 2 + p₂ ^ 2) * (v₁ ^ 2 + v₂ ^ 2))) with h | h
  · rw [← h, div_zero]; norm_num
  · rw [div_le_one h]
    apply Real.le_sqrt_of_sq_le
    exact overlap_sq_le p₁ p₂ v₁ v₂

/-- **Alignment is nonnegative** for positive semidefinite operators, here nonnegative
diagonals. -/
theorem alignment_nonneg (p₁ p₂ v₁ v₂ : ℝ) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hv₁ : 0 ≤ v₁)
    (hv₂ : 0 ≤ v₂) : 0 ≤ alignment p₁ p₂ v₁ v₂ := by
  unfold alignment
  apply div_nonneg
  · nlinarith [mul_nonneg hp₁ hv₁, mul_nonneg hp₂ hv₂]
  · exact Real.sqrt_nonneg _

/-- **The coupling null has alignment one.** When the read operator is a positive multiple of
the covariance, the alignment is exactly one. -/
theorem alignment_eq_one_of_proportional (c v₁ v₂ : ℝ) (hc : 0 < c)
    (hv : v₁ ^ 2 + v₂ ^ 2 ≠ 0) : alignment (c * v₁) (c * v₂) v₁ v₂ = 1 := by
  unfold alignment
  have hpos : 0 < v₁ ^ 2 + v₂ ^ 2 := lt_of_le_of_ne (by positivity) (Ne.symm hv)
  have e : ((c * v₁) ^ 2 + (c * v₂) ^ 2) * (v₁ ^ 2 + v₂ ^ 2) = (c * (v₁ ^ 2 + v₂ ^ 2)) ^ 2 := by
    ring
  rw [e, Real.sqrt_sq (by positivity)]
  field_simp

/-- The read distortion of the diagonal error covariance diag(a, b) under the diagonal read
operator diag(p₁, p₂), the trace of their product, equation 0.10. -/
def readTrace (p₁ p₂ a b : ℝ) : ℝ := p₁ * a + p₂ * b

/-- **The coupling null.** When the read operator is a positive multiple of the covariance, one
code is read as worse than another exactly when its covariance-weighted distortion is
larger, so the code that is best in the covariance's own weighting is also the
read-preserving one, and no flip is possible. -/
theorem coupling_null (c v₁ v₂ a₁ b₁ a₂ b₂ : ℝ) (hc : 0 < c) :
    readTrace (c * v₁) (c * v₂) a₁ b₁ < readTrace (c * v₁) (c * v₂) a₂ b₂
      ↔ readTrace v₁ v₂ a₁ b₁ < readTrace v₁ v₂ a₂ b₂ := by
  unfold readTrace
  constructor
  · intro h; nlinarith
  · intro h; nlinarith

end DMO.Alignment