Download lean/MaxEntropyArithmetic.lean from Snapkitty/sov-kernel-monster: direct link, hf CLI and curl.
- Browser
- Download file 9.27 kB
-
https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/lean/MaxEntropyArithmetic.lean
- Command line
-
hf download hf://Snapkitty/sov-kernel-monster/lean/MaxEntropyArithmetic.lean
-
curl -L -o MaxEntropyArithmetic.lean https://huggingface.co/Snapkitty/sov-kernel-monster/resolve/main/lean/MaxEntropyArithmetic.lean
9.27 kB
Lean 4 Β· 12
declarations statementsfull
26Natural logarithm as power series: log(1+x) = x - xΒ²/2 + xΒ³/3 - ...
noncomputable def log_series (x : β) (n : β) : β :=
body Β· 1 line
(Finset.range n).sum (fun k => (-1)^k * x^(k+1) / (k+1))
30For our case, we only need log on (0, 2) which converges
axiom log_converges : β x, 0 < x β x < 2 β β L, β Ξ΅ > 0, β N, β n β₯ N,
|log_series x n - L| < Ξ΅
38Entropy term: -x log x
noncomputable def entropy_term (x : β) : β :=
body Β· 1 line
if x = 0 then 0 else -x * Real.log x
42Second derivative: dΒ²/dxΒ²(-x log x) = -1/x < 0 for x > 0
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
57Jensen's inequality for concave functions (arithmetic proof)
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
77Kullback-Leibler divergence: D(P || Q) = Ξ£ p_i log(p_i/q_i)
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
82Gibbs' inequality: D(P || Q) β₯ 0, with equality iff P = Q
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
112Shannon entropy: H(P) = -Ξ£ p_i log p_i
noncomputable def shannon_entropy (p : List β) : β :=
body Β· 1 line
-(p.map (fun pi => if pi = 0 then 0 else pi * Real.log pi)).sum
116Uniform distribution on n elements
def uniform_dist (n : β) : List β :=
body Β· 1 line
List.replicate n (1 / n)
120Entropy of uniform distribution = log n
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
137MAIN THEOREM: Uniform distribution maximizes entropy
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
192Born-rule maximum entropy (application to thermal window)
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
Outline