Shannon entropy and Kullback–Leibler divergence
DefinitionShannon entropy#✓fully proved
The Shannon entropy in bits of a probability mass function \(p\) on a finite type \(\alpha\) is \(H(p) = -\sum_{a \in \alpha} p(a) \log_2 p(a)\), where a term with \(p(a) = 0\) contributes \(0\).
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
Sources
- the finite Shannon sum; Li, arXiv:2501.13736v4, entropy conventions.
Used by 36
TheoremA point mass has zero entropy#✓fully proved
For every outcome \(a\) of a finite alphabet, the point mass at \(a\) has Shannon entropy \(0\).
@[simp] theorem shannonEntropy_pure {α : Type*} [Fintype α] (a : α) :
shannonEntropy (PMF.pure a) = 0
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
Uses 1 · Used by 1
TheoremA constant observation has zero entropy#✓fully proved
For any probability mass function \(p\) on a type \(\alpha\) and any point \(b\) of a finite alphabet \(\beta\), the image of \(p\) under the constant map \(\alpha \to \beta\) with value \(b\) has Shannon entropy \(0\).
theorem shannonEntropy_map_const {α β : Type*} [Fintype β]
(p : PMF α) (b : β) :
shannonEntropy (p.map (Function.const α b)) = 0
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
Uses 2 · Used by 2
TheoremEntropy is invariant under relabeling#✓fully proved
For a probability mass function \(p\) on a finite alphabet \(\alpha\) and a bijection \(e : \alpha \to \beta\) onto a finite alphabet \(\beta\), the image of \(p\) under \(e\) has the same Shannon entropy as \(p\).
theorem shannonEntropy_map_equiv {α β : Type*} [Fintype α] [Fintype β]
(p : PMF α) (e : α ≃ β) : shannonEntropy (p.map e) = shannonEntropy p
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
Uses 1 · Used by 6
TheoremAppending a singleton coordinate preserves entropy#✓fully proved
For a probability mass function \(p\) on a finite alphabet \(\alpha\), the image of \(p\) under \(a \mapsto (a, 0)\) in \(\alpha \times \{0\}\) has the same Shannon entropy as \(p\).
theorem shannonEntropy_map_prod_singleton {α : Type*} [Fintype α] (p : PMF α) :
shannonEntropy (p.map (fun a => (a, (0 : Fin 1)))) = shannonEntropy p
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
Uses 2 · Used by 2
TheoremKullback–Leibler divergence on a finite space#✓fully proved
Let \(\mu\) and \(\nu\) be finite measures on a finite type \(\alpha\) in which every singleton is measurable, with \(\mu \ll \nu\) and \(\mu(\alpha) = \nu(\alpha)\). Then the Kullback–Leibler divergence \(\mathrm{KL}(\mu \parallel \nu)\), taken as a real number, equals \(\sum_{a \in \alpha} \mu(\{a\}) \log (\mu(\{a\}) / \nu(\{a\}))\), where \(\log\) is the natural logarithm and a term with \(\mu(\{a\}) = 0\) is \(0\).
theorem toReal_klDiv_eq_sum [Fintype α] [MeasurableSpace α] [MeasurableSingletonClass α]
{μ ν : Measure α} [IsFiniteMeasure μ] [IsFiniteMeasure ν] (hμν : μ ≪ ν)
(huniv : μ Set.univ = ν Set.univ) :
(klDiv μ ν).toReal = ∑ a, μ.real {a} * log (μ.real {a} / ν.real {a})
TheoremKullback–Leibler divergence of finite probability laws#✓fully proved
Let \(p\) and \(q\) be probability mass functions on a finite type \(\alpha\) in which every singleton is measurable, such that \(p(a) = 0\) whenever \(q(a) = 0\), and let \(P\) and \(Q\) be the measures they define. Then the Kullback–Leibler divergence \(\mathrm{KL}(P \parallel Q)\), taken as a real number, equals \(\sum_{a \in \alpha} p(a) \log (p(a) / q(a))\), where \(\log\) is the natural logarithm and a term with \(p(a) = 0\) is \(0\).
theorem toReal_klDiv_toMeasure [Fintype α] [MeasurableSpace α] [MeasurableSingletonClass α]
{p q : PMF α} (hpq : ∀ a, q a = 0 → p a = 0) :
(klDiv p.toMeasure q.toMeasure).toReal =
∑ a, (p a).toReal * log ((p a).toReal / (q a).toReal)
Uses 1 · Used by 5
DefinitionSurprisal#✓fully proved
The surprisal (information content) in bits of an outcome of probability \(q\) is \(-\log_2 q\), with value \(\infty\) when \(q = 0\). Formally \(q\) ranges over the extended nonnegative reals \([0, \infty]\): the surprisal of \(0\) is \(\infty\), the surprisal of \(q \in (0, \infty)\) is \(\max(0, -\log_2 q)\), and the surprisal of \(\infty\) is \(0\).
noncomputable def surprisal (q : ℝ≥0∞) : ℝ≥0∞ :=
if q = 0 then ∞ else ENNReal.ofReal (-logb 2 q.toReal)
Used by 7
DefinitionCross-entropy#✓fully proved
For probability mass functions \(p\) and \(q\) on a finite alphabet, the cross-entropy of \(q\) relative to \(p\) is \(H(p, q) = \sum_a p(a) \cdot (-\log_2 q(a))\) in bits, a value in \([0, \infty]\). Here \(-\log_2 q(a)\) is the surprisal of \(a\) under \(q\), which is \(\infty\) when \(q(a) = 0\), and a term with \(p(a) = 0\) contributes \(0\). So \(H(p, q)\) is \(\infty\) exactly when \(p\) charges an outcome that \(q\) rules out.
TheoremCross-entropy of a point mass#✓fully proved
For an outcome \(y\) of a finite alphabet and a probability mass function \(q\) on it, the cross-entropy \(H(\delta_y, q)\) of \(q\) relative to the point mass \(\delta_y\) at \(y\) equals the surprisal \(-\log_2 q(y)\) of \(y\) under \(q\), which is \(\infty\) when \(q(y) = 0\).
theorem crossEntropy_pure [Fintype α] (y : α) (q : PMF α) :
crossEntropy (PMF.pure y) q = surprisal (q y)
noncomputable def crossEntropy [Fintype α] (p q : PMF α) : ℝ≥0∞ := ∑ a, p a * surprisal (q a)
MathlibExt/InformationTheory/FiniteEntropy.lean:156
noncomputable def surprisal (q : ℝ≥0∞) : ℝ≥0∞ :=
if q = 0 then ∞ else ENNReal.ofReal (-logb 2 q.toReal)
MathlibExt/InformationTheory/FiniteEntropy.lean:141
Uses 2 · Used by 1
TheoremCross-entropy is expected surprisal#✓fully proved
For probability mass functions \(p\) and \(q\) on a finite alphabet, the expected surprisal \(\mathbb{E}_{a \sim p}[-\log_2 q(a)]\) (with \(-\log_2 0 = \infty\)), taken as the integral of a \([0, \infty]\)-valued function with respect to the law \(p\), equals the cross-entropy \(H(p, q)\).
theorem lintegral_surprisal [Fintype α] [MeasurableSpace α] [MeasurableSingletonClass α]
(p q : PMF α) : ∫⁻ a, surprisal (q a) ∂p.toMeasure = crossEntropy p q
noncomputable def surprisal (q : ℝ≥0∞) : ℝ≥0∞ :=
if q = 0 then ∞ else ENNReal.ofReal (-logb 2 q.toReal)
MathlibExt/InformationTheory/FiniteEntropy.lean:141
noncomputable def crossEntropy [Fintype α] (p q : PMF α) : ℝ≥0∞ := ∑ a, p a * surprisal (q a)
MathlibExt/InformationTheory/FiniteEntropy.lean:156
Uses 2
TheoremCross-entropy is entropy plus divergence#✓fully proved
For probability mass functions \(p\) and \(q\) on a finite alphabet, the cross-entropy \(H(p, q)\) in bits equals \(H(p) + D(p \parallel q) / \ln 2\), where \(H(p)\) is the Shannon entropy of \(p\) in bits and \(D(p \parallel q)\) is the Kullback–Leibler divergence of \(p\) from \(q\) with the natural logarithm. The identity holds in \([0, \infty]\) with no support hypothesis: when \(p\) charges an outcome that \(q\) rules out, both sides are \(\infty\).
theorem crossEntropy_eq_shannonEntropy_add_klDiv [Fintype α] [MeasurableSpace α]
[MeasurableSingletonClass α] (p q : PMF α) :
crossEntropy p q = ENNReal.ofReal (shannonEntropy p) +
klDiv p.toMeasure q.toMeasure / ENNReal.ofReal (log 2)
noncomputable def crossEntropy [Fintype α] (p q : PMF α) : ℝ≥0∞ := ∑ a, p a * surprisal (q a)
MathlibExt/InformationTheory/FiniteEntropy.lean:156
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
Uses 4 · Used by 2
TheoremGibbs' inequality#✓fully proved
The cross-entropy of any prediction q is at least the entropy of the
truth p.
theorem shannonEntropy_le_crossEntropy [Fintype α] [MeasurableSpace α]
[MeasurableSingletonClass α] (p q : PMF α) :
ENNReal.ofReal (shannonEntropy p) ≤ crossEntropy p q
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
noncomputable def crossEntropy [Fintype α] (p q : PMF α) : ℝ≥0∞ := ∑ a, p a * surprisal (q a)
MathlibExt/InformationTheory/FiniteEntropy.lean:156
Uses 3
TheoremEquality in Gibbs' inequality#✓fully proved
For probability mass functions \(p\) and \(q\) on a finite alphabet, the cross-entropy \(H(p, q)\) in bits equals the Shannon entropy \(H(p)\) in bits if and only if \(q = p\). Thus log loss is a strictly proper scoring rule.
theorem crossEntropy_eq_shannonEntropy_iff [Fintype α] [MeasurableSpace α]
[MeasurableSingletonClass α] (p q : PMF α) :
crossEntropy p q = ENNReal.ofReal (shannonEntropy p) ↔ q = p
noncomputable def crossEntropy [Fintype α] (p q : PMF α) : ℝ≥0∞ := ∑ a, p a * surprisal (q a)
MathlibExt/InformationTheory/FiniteEntropy.lean:156
noncomputable def shannonEntropy {α : Type*} [Fintype α] (p : PMF α) : ℝ :=
(∑ a, Real.negMulLog (p a).toReal) / Real.log 2
MathlibExt/InformationTheory/FiniteEntropy.lean:44
Uses 3
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.