Properties of Entropy
Entropy behaves in a few very predictable ways. These properties are used constantly in proofs and in sanity checks of code: if your computed entropy breaks one of them, there is a bug.
Bounds
For a random variable with possible values,
- exactly when one outcome has probability 1.
- exactly when is uniform.
The upper bound follows from Gibbs' inequality (Jensen's and Gibbs' Inequalities): , where is uniform.
import numpy as np
def H(p):
p = np.asarray(p, float); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
rng = np.random.default_rng(0)
N = 8
worst = max(H(rng.dirichlet(np.ones(N) * a)) for a in [0.1, 1, 10] for _ in range(2000))
print("largest entropy over 6000 random distributions:", round(worst, 4), " bound log2 N =", np.log2(N))
print("uniform:", H(np.ones(N) / N))largest entropy over 6000 random distributions: 2.9972 bound log2 N = 3.0
uniform: 3.0
Invariance under relabelling
Entropy depends only on the probabilities, not on the values. Renaming outcomes, or sorting the probabilities, leaves unchanged. A die with faces 1 to 6 and a die with faces red, green, ... have the same entropy.
Grouping (the chain rule for a decision)
Splitting a choice into stages adds entropies, weighted by how often each stage is reached. For three outcomes with probabilities , first decide "outcome 1 or not", then, if not, decide between 2 and 3:
Shannon took this grouping property, together with continuity and the requirement that uniform entropy grows with , as the axioms that pin down uniquely.
import numpy as np
def H(p):
p = np.asarray(p, float); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
p1, p2, p3 = 0.5, 0.3, 0.2
lhs = H([p1, p2, p3])
rhs = H([p1, 1 - p1]) + (1 - p1) * H([p2 / (1 - p1), p3 / (1 - p1)])
print(round(lhs, 6), round(rhs, 6))1.485475 1.485475
Concavity
Entropy is a concave function of the distribution: mixing two distributions never lowers the entropy below the average of their entropies,
Mixing adds uncertainty about which distribution produced the sample. Concavity is why maximum-entropy problems have unique solutions (The Maximum-Entropy Principle).
import numpy as np
def H(p):
p = np.asarray(p, float); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
rng = np.random.default_rng(1)
ok = True
for _ in range(5000):
P, Q = rng.dirichlet(np.ones(5)), rng.dirichlet(np.ones(5)); lam = rng.uniform()
ok &= H(lam * P + (1 - lam) * Q) >= lam * H(P) + (1 - lam) * H(Q) - 1e-12
print("concavity held in 5000 random tests:", bool(ok))concavity held in 5000 random tests: True
Functions of a random variable
Applying a function can only lose information: , with equality when is one-to-one on the support. Rounding a die roll to "low/high" drops from 2.585 to 1 bit.
import numpy as np
def H(p):
p = np.asarray(p, float); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
die = np.ones(6) / 6
parity = np.array([die[0::2].sum(), die[1::2].sum()]) # odd, even
mod3 = np.array([die[k::3].sum() for k in range(3)])
print("H(X) =", round(H(die), 4), " H(X mod 2) =", round(H(parity), 4), " H(X mod 3) =", round(H(mod3), 4))H(X) = 2.585 H(X mod 2) = 1.0 H(X mod 3) = 1.585
In software
- Unit tests for an entropy function: check , , , and invariance under
np.random.permutation. - Concavity is the reason a mixture of training sets has higher label entropy than either one, a common effect in dataset merging.
Exercises
Exercise 1: Maximum entropy
What is the largest possible entropy of a random byte (256 values)?
Answer
8 bits.
Solution
, reached by uniformly random bytes. This is why encrypted data looks incompressible.
Exercise 2: Grouping
Use the grouping rule to compute .
Answer
1.5 bits.
Solution
.
Exercise 3: Sum of two dice (code)
Compute for one die, and for the sum of two dice. Compare with .
Answer
, , which is less than .
Solution
import numpy as np
def H(p):
p = np.asarray(p, float); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
die = np.ones(6) / 6
S = np.convolve(die, die)
print(round(H(die), 3), round(H(S), 3), round(2 * H(die), 3))2.585 3.274 5.17
The sum is a function of the pair that is not one-to-one, so it carries less information.
Test yourself
Which distribution on 4 outcomes has the largest entropy?
- (0.25, 0.25, 0.25, 0.25)
- (0.4, 0.3, 0.2, 0.1)
- (0.7, 0.1, 0.1, 0.1)
- (1, 0, 0, 0)
Entropy as a function of the distribution is…
- concave
- convex
- linear
- neither
For any function f, is…
- at most
- at least
- always equal to
- always 0