Discrete Shannon entropy under the uniform measure
DefinitionDiscreteEntropyThe Shannon entropy of a discrete random variable taking values in a finite set , distributed according to the uniform measure on a finite sample space , is
with the usual convention that a term with contributes to the sum (matching the limit as ).
This module fixes empiricalProb Z b as above and shannonEntropy Z as , together with the two basic structural facts that make empiricalProb a genuine probability distribution: it is nonnegative (empiricalProb_nonneg) and sums to over (sum_empiricalProb).
Shannon entropy of this kind underlies the entropy/pigeonhole method in combinatorics (Spencer's partial coloring lemma, Beck-Fiala type arguments): a random variable of low entropy must place a disproportionately large amount of probability mass on a single outcome, which is exactly the counting leverage such arguments exploit.
Formalization Note Entropy is taken base (in bits), matching the convention used throughout the combinatorics literature that applies it (e.g. Alon-Spencer, The Probabilistic Method). empiricalProb and shannonEntropy are defined for any random variable into a Fintype, under the uniform (counting) measure on a Fintype sample space; no independence or other distributional assumption is built into the definition itself.
import Mathlib
open Finset
noncomputable section
variable {Ω β : Type*} [Fintype Ω] [Fintype β] [DecidableEq β]
/-- The empirical probability that `Z : Ω → β` takes the value `b`, under the uniform
distribution on the finite sample space `Ω`. -/
def empiricalProb (Z : Ω → β) (b : β) : ℝ :=
((univ.filter (fun ω => Z ω = b)).card : ℝ) / (Fintype.card Ω : ℝ)
/-- The base-2 Shannon entropy of `Z : Ω → β`, computed from its empirical distribution under
the uniform measure on `Ω`, following the usual convention that a zero-probability outcome
contributes `0` to the sum (since `p log(1/p) → 0` as `p → 0⁺`). -/
def shannonEntropy (Z : Ω → β) : ℝ :=
∑ b : β, (fun p => if p = 0 then 0 else p * Real.logb 2 (1 / p)) (empiricalProb Z b)
lemma empiricalProb_nonneg (Z : Ω → β) (b : β) : 0 ≤ empiricalProb Z b := by
unfold empiricalProb
positivity
lemma sum_empiricalProb (Z : Ω → β) [Nonempty Ω] :
∑ b : β, empiricalProb Z b = 1 := by
unfold empiricalProb
rw [← Finset.sum_div]
have hsum : ∑ b : β, ((univ.filter (fun ω => Z ω = b)).card : ℝ) = (Fintype.card Ω : ℝ) := by
have h1 : (univ : Finset Ω).card
= ∑ b ∈ (univ : Finset β), (univ.filter (fun ω => Z ω = b)).card :=
Finset.card_eq_sum_card_fiberwise (fun a _ => mem_univ _)
rw [Fintype.card]
exact_mod_cast h1.symm
rw [hsum]
have hne : (Fintype.card Ω : ℝ) ≠ 0 := by
have := Fintype.card_pos (α := Ω)
positivity
field_simp
end