WaterFilling.lean

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

/-
WaterFilling.lean. Equations 0.13 and 4.2.

The distortion of the high-rate model is a sum over directions of the sensitivity-weighted
variance times four to the power of minus the bits. Two facts are checked. With the
closed-form allocation, half the log of the weighted variance over the water level, every
direction above the water contributes exactly the water level, which is the equal-distortion
property that gives the allocation its name. And for two directions sharing a budget the
distortion is never below twice the geometric mean of the two terms, with equality when the
two terms are equal, which is the two-direction case of the allocation's optimality.
-/
import Mathlib

namespace DMO.WaterFilling

open Real

/-- The bits equation 0.13 gives a direction of weighted variance `z` above the water level
`θ`. -/
noncomputable def bits (z θ : ℝ) : ℝ := (1 / 2) * logb 2 (z / θ)

/-- **Equal distortion.** A direction above the water, given its closed-form bits, contributes
exactly the water level. -/
theorem contribution_eq_water (z θ : ℝ) (hz : 0 < z) (hθ : 0 < θ) :
    z * (4 : ℝ) ^ (-bits z θ) = θ := by
  unfold bits
  have h4 : (4 : ℝ) = (2 : ℝ) ^ (2 : ℝ) := by norm_num
  rw [h4, ← rpow_mul (by norm_num)]
  have e : (2 : ℝ) * -(1 / 2 * logb 2 (z / θ)) = -logb 2 (z / θ) := by ring
  rw [e, rpow_neg (by norm_num), rpow_logb (by norm_num) (by norm_num) (by positivity)]
  field_simp

/-- The two-direction distortion of equation 4.2 at bits `b` and `B − b`. -/
noncomputable def distortion2 (z₁ z₂ B b : ℝ) : ℝ :=
  z₁ * (4 : ℝ) ^ (-b) + z₂ * (4 : ℝ) ^ (-(B - b))

/-- The product of the two terms does not depend on the split. -/
theorem terms_mul (z₁ z₂ B b : ℝ) :
    (z₁ * (4 : ℝ) ^ (-b)) * (z₂ * (4 : ℝ) ^ (-(B - b))) = z₁ * z₂ * (4 : ℝ) ^ (-B) := by
  have : (4 : ℝ) ^ (-b) * (4 : ℝ) ^ (-(B - b)) = (4 : ℝ) ^ (-B) := by
    rw [← rpow_add (by norm_num)]; ring_nf
  calc (z₁ * (4 : ℝ) ^ (-b)) * (z₂ * (4 : ℝ) ^ (-(B - b)))
      = z₁ * z₂ * ((4 : ℝ) ^ (-b) * (4 : ℝ) ^ (-(B - b))) := by ring
    _ = z₁ * z₂ * (4 : ℝ) ^ (-B) := by rw [this]

/-- Two nonnegative numbers sum to at least twice the square root of their product. -/
theorem two_sqrt_le (x y : ℝ) (hx : 0 ≤ x) (hy : 0 ≤ y) : 2 * Real.sqrt (x * y) ≤ x + y := by
  have h := sq_nonneg (Real.sqrt x - Real.sqrt y)
  rw [sub_sq, sq_sqrt hx, sq_sqrt hy] at h
  rw [Real.sqrt_mul hx]
  linarith

/-- **The two-direction lower bound.** Whatever the split, the distortion is at least twice
the geometric mean of the two terms, a quantity fixed by the budget alone. -/
theorem distortion2_ge (z₁ z₂ B b : ℝ) (h₁ : 0 ≤ z₁) (h₂ : 0 ≤ z₂) :
    2 * Real.sqrt (z₁ * z₂ * (4 : ℝ) ^ (-B)) ≤ distortion2 z₁ z₂ B b := by
  unfold distortion2
  rw [← terms_mul]
  exact two_sqrt_le _ _ (by positivity) (by positivity)

/-- **The bound is attained at equal distortion.** When the split makes the two terms equal,
the distortion equals the lower bound. -/
theorem distortion2_eq_of_equal (z₁ z₂ B b : ℝ) (h₁ : 0 ≤ z₁)
    (heq : z₁ * (4 : ℝ) ^ (-b) = z₂ * (4 : ℝ) ^ (-(B - b))) :
    distortion2 z₁ z₂ B b = 2 * Real.sqrt (z₁ * z₂ * (4 : ℝ) ^ (-B)) := by
  unfold distortion2
  rw [← terms_mul, ← heq, Real.sqrt_mul_self (by positivity)]
  ring

end DMO.WaterFilling