VII.3 · Information Theory
How uncertain is the whole distribution?
Change explicit weights, inspect the normalized rational masses, and see each mass-weighted surprise contribution before they sum to entropy.
Expected surprise of a distribution
Horizontal: outcome · vertical: probability mass from the exact normalized distribution
| Outcome | Exact p | Surprise | Contribution (nats) |
|---|---|---|---|
| 1 | 0.5 | ≈ 0.693147 | ≈ 0.346574 |
| 2 | 0.25 | ≈ 1.38629 | ≈ 0.346574 |
| 3 | 0.25 | ≈ 1.38629 | ≈ 0.346574 |
- Outcome 1: ≈ 0.346574
- Outcome 2: ≈ 0.346574
- Outcome 3: ≈ 0.346574
Bar length = absolute contribution on a shared scale; sign and infinities appear in the labels.
Exact normalized masses [0.5, 0.25, 0.25]. Entropy ≈ 1.03972 nats; maximum ≈ 1.09861 for 3 fixed outcomes. Entropy averages surprise across possible observations.
- Entropy (nats)
- 1.039721
- Maximum (nats)
- 1.098612
- Exact total mass
- 1
= exact rational or mathematical boundary · ≈ approximate binary64 evaluation
Edit nonnegative integer weights. The table openly normalizes them; these controls are weights, not already normalized probabilities.
from 0 to 100, whole numbers only
from 0 to 100, whole numbers only
from 0 to 100, whole numbers only
Exact normalized masses [0.5, 0.25, 0.25]. Entropy ≈ 1.03972 nats; maximum ≈ 1.09861 for 3 fixed outcomes. Entropy averages surprise across possible observations.
Normalize declared weights
Weights [2, 1, 1] become the exact masses in the table.
Intuition
A point mass always produces the same outcome and has no uncertainty. Spreading fixed total mass over several outcomes creates uncertainty. Entropy asks how surprising an observation is on average under that distribution.
Definition
For a finite discrete distribution with and , . Natural logs give nats; base-2 logs give bits. Define the zero-mass contribution by .
Mechanics
The controls are nonnegative integer weights. The displayed rule pᵢ=wᵢ/Σwⱼ normalizes those weights once into authoritative exact masses. The probability graph and contribution bars both use that derived distribution; bar lengths encode actual pᵢIᵢ values, which also appear in the table. An all-zero weight vector is rejected.
Derivation
Take the expectation of surprise: each occurs with probability . The limit follows by writing , so . Every positive-mass surprise is nonnegative, so . Equality requires all positive mass to be at probability 1. For the upper bound, strict concavity of log gives . Equality at fixed support n requires every .
Worked examples
For , surprises are bits and contributions are bits. Thus bits. For a uniform three-outcome distribution, bits. For , every contribution is zero.
Worked example: Expected surprise of a distribution
Horizontal: outcome · vertical: probability mass from the exact normalized distribution
| Outcome | Exact p | Surprise | Contribution (nats) |
|---|---|---|---|
| 1 | 0.5 | ≈ 0.693147 | ≈ 0.346574 |
| 2 | 0.25 | ≈ 1.38629 | ≈ 0.346574 |
| 3 | 0.25 | ≈ 1.38629 | ≈ 0.346574 |
- Outcome 1: ≈ 0.346574
- Outcome 2: ≈ 0.346574
- Outcome 3: ≈ 0.346574
Bar length = absolute contribution on a shared scale; sign and infinities appear in the labels.
Exact normalized masses [0.5, 0.25, 0.25]. Entropy ≈ 1.03972 nats; maximum ≈ 1.09861 for 3 fixed outcomes. Entropy averages surprise across possible observations.
- Entropy (nats)
- 1.039721
- Maximum (nats)
- 1.098612
- Exact total mass
- 1
= exact rational or mathematical boundary · ≈ approximate binary64 evaluation
Edit nonnegative integer weights. The table openly normalizes them; these controls are weights, not already normalized probabilities.
from 0 to 100, whole numbers only
from 0 to 100, whole numbers only
from 0 to 100, whole numbers only
Exact normalized masses [0.5, 0.25, 0.25]. Entropy ≈ 1.03972 nats; maximum ≈ 1.09861 for 3 fixed outcomes. Entropy averages surprise across possible observations.
Normalize declared weights
Weights [2, 1, 1] become the exact masses in the table.
Executable reference code
Run the standalone pedagogical version to see each operation; the NumPy companion shows the practical array form. Both include assertions so you can check the worked examples as you run them.
"""Exact normalization, then an explicit expected-surprise sum."""
from fractions import Fraction
from math import log, fsum
def entropy(weights,unit='nats'):
if not 1<=len(weights)<=6 or any(type(w) is not int or not 0<=w<=1000000 for w in weights) or sum(weights)==0 or unit not in ('nats','bits'):
raise ValueError('Nonnegative integer weights with positive total; nats or bits')
masses=[Fraction(w,sum(weights)) for w in weights]
terms=[0.0 if not p else float(p)*(log(p.denominator)-log(p.numerator))/(log(2) if unit=='bits' else 1) for p in masses]
return masses,terms,fsum(terms)
if __name__=='__main__':
assert entropy([1,0,0],'bits')[2]==0
assert entropy([2,1,1],'bits')[2]==1.5
assert entropy([1,1],'bits')[2]==1
print(entropy([2,1,1],'bits'))"""Normalize weights openly; mask zeros before logarithms, never 0 × infinity."""
import numpy as np
def entropy(weights,unit='nats'):
w=np.asarray(weights,dtype=float)
if w.ndim!=1 or not 1<=w.size<=6 or not np.all(np.isfinite(w)) or np.any((w<0)|(w>1000000)|(w!=np.floor(w))) or w.sum()==0 or unit not in ('nats','bits'):raise ValueError('Invalid weights or unit')
p=w/w.sum();positive=p>0;terms=np.zeros_like(p)
terms[positive]=-p[positive]*np.log(p[positive])/(np.log(2) if unit=='bits' else 1)
return p,terms,float(terms.sum())
if __name__=='__main__':
assert entropy([1,0,0],'bits')[2]==0
assert entropy([2,1,1],'bits')[2]==1.5
print(entropy([2,1,1],'bits'))Explore
Compare Point mass and Uniform masses without changing support size. Then move weight into one outcome and identify the contribution that changes. Make one weight zero and explain why its infinite single-event surprise contributes zero to the expectation.
Common mistakes and counterexamples
Entropy belongs to a distribution; surprise belongs to one observation. A rare event can have huge surprise while contributing little to entropy. Entropy does not ignore probabilities and merely count labels. A zero-weight label still belongs to the fixed support used for the maximum comparison.
Numerical considerations
Supported distributions have 1–6 outcomes with integer weights up to 1,000,000 in the model (controls use 0–100). Exact rational normalization preserves total mass exactly. Zero terms are branched before evaluating products; no 0×∞ is computed. Logarithms and entropy sums use binary64 and are checked against exact Fraction/Decimal100 sources at 2e−12 scaled absolute/relative budget. Numerical examples verify implementation; concavity supplies the mathematical maximum proof.
Engineering and ML connections
Entropy is the best distribution-averaged log loss attainable by predicting the target distribution itself. Cross entropy will measure a prediction distribution against this baseline, and KL will expose the excess loss.
Continue from the preceding mathematical connection.
Exercises
Predict, compute and explain the actual quantities. Each question offers corrective feedback and a worked solution.
Summary and checkpoints
A point mass always produces the same outcome and has no uncertainty. Spreading fixed total mass over several outcomes creates uncertainty. Entropy asks how surprising an observation is on average under that distribution. Check the assumptions, unit and boundary before interpreting the computed result.