ProductQuantization.lean

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

/-
ProductQuantization.lean. Chapter 11 section 11.3, product quantization.

Product quantization splits a vector into pieces and quantizes each piece with its own
codebook. The squared error of the whole is the sum of the pieces' squared errors, so the
best code for the whole is the best code for each piece taken separately, and a codebook of
`K` entries per piece over `M` pieces addresses `K^M` cells with `M log₂ K` bits. The file
checks the additivity, the separability of the minimum over two pieces, and the bit count.
-/
import Mathlib

namespace DMO.ProductQuantization

open Finset

variable {α β : Type*}

/-- **Additivity.** The squared error of a vector split into two pieces is the sum of the two
pieces' squared errors. -/
theorem sq_error_add {m n : ℕ} (x x' : Fin m → ℝ) (y y' : Fin n → ℝ) :
    ∑ i, (Fin.append (x - x') (y - y') i) ^ 2 = ∑ i, (x i - x' i) ^ 2 + ∑ j, (y j - y' j) ^ 2 := by
  rw [Fin.sum_univ_add]
  simp [Fin.append_left, Fin.append_right]

/-- **Separability.** The least total error over pairs of codes is the sum of the least errors
over each codebook. -/
theorem inf_add (A : Finset α) (B : Finset β) (hA : A.Nonempty) (hB : B.Nonempty) (f : α → ℝ)
    (g : β → ℝ) :
    (A ×ˢ B).inf' (hA.product hB) (fun p => f p.1 + g p.2) = A.inf' hA f + B.inf' hB g := by
  apply le_antisymm
  · obtain ⟨a, ha, hfa⟩ := Finset.exists_mem_eq_inf' hA f
    obtain ⟨b, hb, hgb⟩ := Finset.exists_mem_eq_inf' hB g
    rw [hfa, hgb]
    exact Finset.inf'_le _ (Finset.mk_mem_product ha hb)
  · rw [Finset.le_inf'_iff]
    intro p hp
    rw [Finset.mem_product] at hp
    exact add_le_add (Finset.inf'_le f hp.1) (Finset.inf'_le g hp.2)

/-- **The bit count.** `M` codebooks of `K` entries address `K^M` cells, which is `M log₂ K`
bits. -/
theorem bits_of_codebooks (K M : ℕ) :
    Real.logb 2 ((K : ℝ) ^ M) = M * Real.logb 2 K := by
  rw [Real.logb_pow]

/-- The chapter's table. Twenty-five subspaces of one byte each address `256^25` cells in
two hundred bits. -/
theorem table_row : Real.logb 2 ((256 : ℝ) ^ 25) = 200 := by
  rw [Real.logb_pow, show (256 : ℝ) = 2 ^ (8 : ℕ) by norm_num, Real.logb_pow, Real.logb_self_eq_one (by norm_num)]
  norm_num

end DMO.ProductQuantization