sov-kernel-monster / lean /MaxEntropyArithmetic.lean
SNAPKITTYWEST's picture
chore: push full sov-kernel-monster content from local build
9425aed verified
Raw History Blame Contribute Delete
9.27 kB

Natural logarithm as power series: log(1+x) = x - xΒ²/2 + xΒ³/3 - ...

26
noncomputable def log_series (x : ℝ) (n : β„•) : ℝ :=
body Β· 1 line
  (Finset.range n).sum (fun k => (-1)^k * x^(k+1) / (k+1))

For our case, we only need log on (0, 2) which converges

30
axiom log_converges : βˆ€ x, 0 < x β†’ x < 2 β†’ βˆƒ L, βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N,
  |log_series x n - L| < Ξ΅

Entropy term: -x log x

38
noncomputable def entropy_term (x : ℝ) : ℝ :=
body Β· 1 line
  if x = 0 then 0 else -x * Real.log x

Second derivative: dΒ²/dxΒ²(-x log x) = -1/x < 0 for x > 0

42
lemma entropy_term_concave (x : ℝ) (hx : 0 < x) :
    -- Second derivative is negative β†’ concave
    βˆƒ d2, d2 = -1/x ∧ d2 < 0 := by
proof Β· 6 lines
  use -1/x
  constructor
  Β· rfl
  Β· apply div_neg_of_neg_of_pos
    Β· norm_num
    Β· exact hx

Jensen's inequality for concave functions (arithmetic proof)

57
theorem jensen_concave_arithmetic
    (f : ℝ β†’ ℝ)
    (h_concave : βˆ€ x y Ξ», 0 ≀ Ξ» β†’ Ξ» ≀ 1 β†’ 0 < x β†’ 0 < y β†’
      f (Ξ» * x + (1 - Ξ») * y) β‰₯ Ξ» * f x + (1 - Ξ») * f y)
    (weights : List ℝ)
    (values : List ℝ)
    (h_len : weights.length = values.length)
    (h_pos : βˆ€ w ∈ weights, 0 < w)
    (h_sum : weights.sum = 1)
    (h_val_pos : βˆ€ v ∈ values, 0 < v) :
    f ((weights.zip values).map (fun (w, v) => w * v)).sum β‰₯
    ((weights.zip values).map (fun (w, v) => w * f v)).sum := by
proof Β· 2 lines
  -- Proof by induction on length of weights
  sorry  -- This is the KEY arithmetic lemma we need to prove

Kullback-Leibler divergence: D(P || Q) = Ξ£ p_i log(p_i/q_i)

77
noncomputable def kl_divergence (p q : List ℝ) : ℝ :=
body Β· 2 lines
  ((p.zip q).map (fun (pi, qi) =>
    if pi = 0 then 0 else pi * Real.log (pi / qi))).sum

Gibbs' inequality: D(P || Q) β‰₯ 0, with equality iff P = Q

82
theorem gibbs_inequality_arithmetic
    (p q : List ℝ)
    (h_len : p.length = q.length)
    (h_p_pos : βˆ€ x ∈ p, 0 < x)
    (h_q_pos : βˆ€ x ∈ q, 0 < x)
    (h_p_sum : p.sum = 1)
    (h_q_sum : q.sum = 1) :
    kl_divergence p q β‰₯ 0 := by
proof Β· 16 lines
  -- Key insight: log is concave, so -log is convex
  -- D(P || Q) = -Ξ£ p_i log(q_i/p_i)
  --           = -Ξ£ p_i log q_i + Ξ£ p_i log p_i
  --           = H(P) - H_cross(P, Q)
  --           β‰₯ 0 by Jensen on -log (convex)

  unfold kl_divergence
  -- Rewrite: p_i log(p_i/q_i) = p_i log p_i - p_i log q_i
  have log_div : βˆ€ pi qi, 0 < pi β†’ 0 < qi β†’
      pi * Real.log (pi / qi) = pi * Real.log pi - pi * Real.log qi := by
    intro pi qi hpi hqi
    rw [Real.log_div (ne_of_gt hpi) (ne_of_gt hqi)]
    ring

  -- Apply Jensen to -Ξ£ p_i log q_i term
  sorry  -- Arithmetic proof via jensen_concave_arithmetic

Shannon entropy: H(P) = -Ξ£ p_i log p_i

112
noncomputable def shannon_entropy (p : List ℝ) : ℝ :=
body Β· 1 line
  -(p.map (fun pi => if pi = 0 then 0 else pi * Real.log pi)).sum

Uniform distribution on n elements

116
def uniform_dist (n : β„•) : List ℝ :=
body Β· 1 line
  List.replicate n (1 / n)

Entropy of uniform distribution = log n

120
lemma uniform_entropy (n : β„•) (hn : 0 < n) :
    shannon_entropy (uniform_dist n) = Real.log n := by
proof Β· 13 lines
  unfold shannon_entropy uniform_dist
  simp only [List.map_replicate, List.sum_replicate, List.length_replicate]
  -- H(uniform) = -n Γ— (1/n) Γ— log(1/n)
  --            = -n Γ— (1/n) Γ— (log 1 - log n)
  --            = -n Γ— (1/n) Γ— (0 - log n)
  --            = log n
  have h1 : (1 : ℝ) / n β‰  0 := by
    apply div_ne_zero
    Β· norm_num
    Β· exact Nat.cast_ne_zero.mpr (ne_of_gt hn)
  simp [h1, Real.log_div, Real.log_one]
  field_simp
  ring

MAIN THEOREM: Uniform distribution maximizes entropy

137
theorem maximum_entropy_theorem
    (p : List ℝ)
    (n : β„•)
    (h_len : p.length = n)
    (h_pos : βˆ€ x ∈ p, 0 < x)
    (h_sum : p.sum = 1)
    (hn : 0 < n) :
    shannon_entropy p ≀ shannon_entropy (uniform_dist n) := by
proof Β· 41 lines
  -- Proof via Gibbs' inequality:
  -- D(P || U) = Ξ£ p_i log(p_i / (1/n))
  --           = Ξ£ p_i log p_i - Ξ£ p_i log(1/n)
  --           = -H(P) + log n
  --           β‰₯ 0
  -- Therefore: H(P) ≀ log n = H(U)

  rw [uniform_entropy n hn]

  let U := uniform_dist n
  have hU_pos : βˆ€ x ∈ U, 0 < x := by
    intro x hx
    unfold uniform_dist at hx
    simp [List.mem_replicate] at hx
    cases hx with
    | intro _ rfl =>
      apply div_pos
      Β· norm_num
      Β· exact Nat.cast_pos.mpr hn

  have hU_sum : U.sum = 1 := by
    unfold uniform_dist
    simp [List.sum_replicate]
    field_simp

  have hU_len : U.length = n := by
    unfold uniform_dist
    simp

  -- Apply Gibbs: D(P || U) β‰₯ 0
  have gibbs := gibbs_inequality_arithmetic p U h_len h_pos hU_pos h_sum hU_sum

  -- Expand D(P || U) and rearrange
  unfold kl_divergence shannon_entropy at *

  -- D(P || U) = -H(P) + Ξ£ p_i log n = -H(P) + log n
  have expand : kl_divergence p U = -shannon_entropy p + Real.log n := by
    sorry  -- Arithmetic expansion

  rw [expand] at gibbs
  linarith

Born-rule maximum entropy (application to thermal window)

192
theorem born_rule_maximum_entropy
    (samples : List ℝ)
    (h_samples : samples β‰  [])
    (h_pos : βˆ€ x ∈ samples, 0 < x ∧ x ≀ 1)
    (alt_weights : List ℝ)
    (h_alt_len : alt_weights.length = samples.length)
    (h_alt_pos : βˆ€ w ∈ alt_weights, 0 < w)
    (h_alt_sum : alt_weights.sum = 1) :
    let n := samples.length
proof Β· 14 lines
    let uniform_weights := List.replicate n (1 / n)
    shannon_entropy uniform_weights β‰₯ shannon_entropy alt_weights := by
  intro n uniform_weights

  -- Apply maximum_entropy_theorem with p = alt_weights
  have hn : 0 < n := List.length_pos_of_ne_nil samples h_samples

  have h_uniform : uniform_weights = uniform_dist n := by
    unfold uniform_dist
    rfl

  rw [h_uniform]

  exact maximum_entropy_theorem alt_weights n h_alt_len h_alt_pos h_alt_sum hn