KL.lean

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

/-
KL.lean. Equation 0.24, the Kullback–Leibler divergence.

The divergence of one distribution from another is the expected log ratio of their masses
under the first. It is nonnegative, which is Gibbs' inequality and follows from the
logarithm lying below its tangent at one, and it is zero when the two distributions agree.
It is not symmetric, and the file records that with a pair of numbers.
-/
import Mathlib

namespace DMO.KL

open Finset

variable {ι : Type*}

/-- **Equation 0.24.** The divergence of `p` from `q` over the outcomes `S`. -/
noncomputable def kl (S : Finset ι) (p q : ι → ℝ) : ℝ := ∑ i ∈ S, p i * Real.log (p i / q i)

/-- **Gibbs' inequality.** With positive masses summing to one on both sides, the divergence
is nonnegative. -/
theorem kl_nonneg (S : Finset ι) (p q : ι → ℝ) (hp : ∀ i ∈ S, 0 < p i) (hq : ∀ i ∈ S, 0 < q i)
    (hps : ∑ i ∈ S, p i = 1) (hqs : ∑ i ∈ S, q i = 1) : 0 ≤ kl S p q := by
  unfold kl
  have key : ∀ i ∈ S, p i - q i ≤ p i * Real.log (p i / q i) := by
    intro i hi
    have hpi := hp i hi
    have hqi := hq i hi
    have hlog : Real.log (q i / p i) ≤ q i / p i - 1 := Real.log_le_sub_one_of_pos (by positivity)
    have hinv : Real.log (p i / q i) = -Real.log (q i / p i) := by
      rw [← Real.log_inv, inv_div]
    rw [hinv]
    have : p i * (q i / p i - 1) = q i - p i := by field_simp
    nlinarith [mul_le_mul_of_nonneg_left hlog hpi.le]
  calc (0 : ℝ) = ∑ i ∈ S, (p i - q i) := by rw [Finset.sum_sub_distrib, hps, hqs, sub_self]
    _ ≤ ∑ i ∈ S, p i * Real.log (p i / q i) := Finset.sum_le_sum key

/-- **A distribution has zero divergence from itself.** -/
theorem kl_self (S : Finset ι) (p : ι → ℝ) (hp : ∀ i ∈ S, 0 < p i) : kl S p p = 0 := by
  unfold kl
  exact Finset.sum_eq_zero fun i hi => by rw [div_self (hp i hi).ne', Real.log_one, mul_zero]

/-- **The divergence is not symmetric.** For the distributions (1/2, 1/4, 1/8, 1/8) and
(1/8, 1/2, 1/4, 1/8) the two directions are five eighths and one half of the logarithm of
two. -/
theorem kl_not_symm :
    kl (Finset.univ : Finset (Fin 4)) (![1 / 2, 1 / 4, 1 / 8, 1 / 8]) (![1 / 8, 1 / 2, 1 / 4, 1 / 8])
      ≠ kl (Finset.univ : Finset (Fin 4)) (![1 / 8, 1 / 2, 1 / 4, 1 / 8]) (![1 / 2, 1 / 4, 1 / 8, 1 / 8]) := by
  unfold kl
  simp only [Fin.sum_univ_four, Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.head_cons,
    Matrix.cons_val_two, Matrix.tail_cons, Matrix.cons_val_three]
  have l4 : Real.log ((1 / 2 : ℝ) / (1 / 8)) = 2 * Real.log 2 := by
    rw [show ((1 / 2 : ℝ) / (1 / 8)) = 2 ^ 2 by norm_num, Real.log_pow]; norm_num
  have lh : Real.log ((1 / 4 : ℝ) / (1 / 2)) = -Real.log 2 := by
    rw [show ((1 / 4 : ℝ) / (1 / 2)) = 2⁻¹ by norm_num, Real.log_inv]
  have lh' : Real.log ((1 / 8 : ℝ) / (1 / 4)) = -Real.log 2 := by
    rw [show ((1 / 8 : ℝ) / (1 / 4)) = 2⁻¹ by norm_num, Real.log_inv]
  have l1 : Real.log ((1 / 8 : ℝ) / (1 / 8)) = 0 := by
    rw [div_self (by norm_num)]; exact Real.log_one
  have lq : Real.log ((1 / 8 : ℝ) / (1 / 2)) = -(2 * Real.log 2) := by
    rw [show ((1 / 8 : ℝ) / (1 / 2)) = (2 ^ 2)⁻¹ by norm_num, Real.log_inv, Real.log_pow]; norm_num
  have l2 : Real.log ((1 / 2 : ℝ) / (1 / 4)) = Real.log 2 := by
    rw [show ((1 / 2 : ℝ) / (1 / 4)) = 2 by norm_num]
  have l2' : Real.log ((1 / 4 : ℝ) / (1 / 8)) = Real.log 2 := by
    rw [show ((1 / 4 : ℝ) / (1 / 8)) = 2 by norm_num]
  rw [l4, lh, lh', l1, lq, l2, l2']
  have := Real.log_pos (by norm_num : (1 : ℝ) < 2)
  intro h
  linarith

end DMO.KL