Codebook.lean

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

/-
Codebook.lean. Chapter 0 section 0.11 and chapter 4 section 4.2, the codebook.

A codebook is the small set of allowed values a quantizer replaces each input with, and the
nearest-codeword rule assigns each input the codeword at least distance. The distortion of
an input is its distance to the nearest codeword, so it is nonnegative, zero when the input
is itself a codeword, and never larger for a larger codebook. Which codebook is best for a
consumer is the flip's question and not this file's, and the codebook confound of the
ledger is the case where two arms had unequal codebooks.
-/
import Mathlib

namespace DMO.Codebook

open Finset

variable {α : Type*}

/-- The distortion of an input `x` under a codebook `C` with distance `d`, the least distance
to a codeword. -/
noncomputable def distortion (C : Finset α) (hC : C.Nonempty) (d : α → α → ℝ) (x : α) : ℝ :=
  C.inf' hC fun c => d x c

/-- **Distortion is nonnegative** for a nonnegative distance. -/
theorem distortion_nonneg (C : Finset α) (hC : C.Nonempty) (d : α → α → ℝ) (hd : ∀ x c, 0 ≤ d x c)
    (x : α) : 0 ≤ distortion C hC d x := by
  unfold distortion
  rw [Finset.le_inf'_iff]
  intro c _
  exact hd x c

/-- **A codeword has zero distortion** when the distance from a point to itself is zero. -/
theorem distortion_codeword (C : Finset α) (hC : C.Nonempty) (d : α → α → ℝ) (hd : ∀ x c, 0 ≤ d x c)
    (hself : ∀ x, d x x = 0) (x : α) (hx : x ∈ C) : distortion C hC d x = 0 := by
  apply le_antisymm
  · unfold distortion
    exact (Finset.inf'_le _ hx).trans (le_of_eq (hself x))
  · exact distortion_nonneg C hC d hd x

/-- **A larger codebook never increases distortion.** -/
theorem distortion_anti (C C' : Finset α) (hC : C.Nonempty) (hCC' : C ⊆ C') (d : α → α → ℝ) (x : α) :
    distortion C' (hC.mono hCC') d x ≤ distortion C hC d x :=
  Finset.inf'_mono _ hCC' hC

/-- **The nearest codeword is achieved.** Some codeword attains the distortion. -/
theorem exists_nearest (C : Finset α) (hC : C.Nonempty) (d : α → α → ℝ) (x : α) :
    ∃ c ∈ C, d x c = distortion C hC d x := by
  obtain ⟨c, hc, h⟩ := Finset.exists_mem_eq_inf' hC fun c => d x c
  exact ⟨c, hc, h.symm⟩

end DMO.Codebook