Key ideas
Information = reduction of uncertainty. A message carries as much information as it removes uncertainty: entropy before minus entropy after; a decision-tree split's information gain is the label entropy of the parent minus the weighted entropy of the children7. Rare events are more surprising, and a near-certain event tells you almost nothing1.
Self-information of an outcome with probability : bits (use for nats; 1 nat bits)1. A fair die roll carries bits.
Entropy is the expected self-information, 1:
- for discrete ; for outcomes, with equality only for the uniform distribution1. Terms with contribute 0 ().
- It is a lower bound on the average number of bits per symbol needed to encode 1. With equally likely symbols a fixed-length code spends bits — more than unless is a power of 2. Coding symbols together needs bits per symbol, which approaches . With -ary symbols, messages need symbols, and finding one of equally likely items takes yes/no questions.
- Randomness per character of a source: bits per choice divided by characters per choice, e.g. over the average word length.
- Differential entropy of a continuous variable can be negative ( has bit)1.
- Binary entropy peaks at 1 bit for ; .
Two variables2: joint entropy satisfies , with the upper bound reached iff 2. Conditional entropy is what remains unknown about after seeing 2; the uncertainty about what was sent that remains after receiving the signal, , is called the equivocation6. If then , and a one-to-one relabelling of a pair keeps its joint entropy.
Mutual information2:
It is symmetric, , and iff and are independent2. Unlike correlation it sees non-linear dependence: if is a function of , even when their correlation is zero. The information gain of a split is the mutual information between the label and the split attribute7. Mixing distributions raises entropy: tossing a randomly chosen biased coin has more entropy than the average entropy of the coins, by exactly the information the toss gives about the coin.
KL divergence 3:
- (Gibbs' inequality, via Jensen), iff 3,5.
- Not symmetric, so not a distance3.
- Infinite if for some with — a problem for count-based estimates with zeros (smoothing helps)3,5.
- .
Cross-entropy is the expected surprisal of someone who believes while data come from 3,4. Since and is fixed by the data, minimising cross-entropy over is minimising KL, and for one-hot labels it is maximising the log-likelihood: the loss is for the true class 3,4. With softmax outputs this is for logits 4. For fixed it is smallest at .
Formulas
With equal variances this is in both directions5. Entropy is concave: the entropy of a mixture is at least the average entropy of its components.
Worked example
: bits. Against the uniform : , so bits, while bits — different. Mutual information of a fair bit sent through a channel that flips it with probability 0.25: bits.
In code
import numpy as np
from scipy.stats import entropy
import torch
import torch.nn.functional as F
p = np.array([0.5, 0.3, 0.2])
q = np.ones(3) / 3
print(round(entropy(p, base=2), 4)) # 1.4855
print(round(entropy(p, q, base=2), 4)) # 0.0995 KL(p||q)
print(round(entropy(q, p, base=2), 4)) # 0.1013 KL(q||p)
logits, y = torch.tensor([[2.0, 1.0, 0.1]]), torch.tensor([0])
print(round(F.cross_entropy(logits, y).item(), 4)) # 0.417 = -log softmax(logits)[y]
Common pitfalls
- Units: gives bits, gives nats — don't mix them in one calculation.
- , never (except with equality under independence)2.
- KL is asymmetric and can be infinite; it is not a metric3.
- Zero correlation does not imply zero mutual information.
- Differential entropy can be negative; discrete entropy cannot1.
- Cross-entropy ≥ entropy: , with equality only when 3.
References
- Dive into Deep Learning — Sec. 22.11.1 Information (Self-information); 22.11.2 Entropy (22.11.2.3 Interpretations; 22.11.2.4 Properties of Entropy)
- Dive into Deep Learning — Sec. 22.11.3 Mutual Information (Joint Entropy; Conditional Entropy; Mutual Information; Properties of Mutual Information)
- Dive into Deep Learning — Sec. 22.11.4 Kullback–Leibler Divergence (KL Divergence Properties); 22.11.5 Cross-Entropy
- Dive into Deep Learning — Sec. 4.1.2 Loss Function (Log-Likelihood; Softmax and Cross-Entropy Loss); 4.1.3 Information Theory Basics
- Wikipedia: Kullback–Leibler divergence — Definition; Properties; Examples — Multivariate normal distributions (univariate case)
- Wikipedia: Conditional entropy — Definition (equivocation); Properties
- Wikipedia: Information gain (decision tree) — Introduction; General definition; Example (entropy of the parent minus average entropy of the children)