0.6 — Entropy and KL divergence
Goal: State H[p] and KL(p‖q), prove KL ≥ 0 via Jensen's inequality, and read KL as 'extra bits from coding p with a code optimal for q'.
1. Shannon information of a single outcome
If an event with probability occurs, the information it carries is
The minus sign makes non-negative (since , ). Three sanity checks:
- : a certain outcome carries zero information.
- as : an impossible-looking outcome that nonetheless occurs is maximally informative.
- : information from two independent events adds. This additivity is what fixes the form of uniquely up to a log base.
Base of the log sets the units: base 2 gives bits, base gives nats. In ML we use natural log throughout (gradient-friendly); the formulas are identical up to a constant factor.[1]
2. Entropy
Entropy is the expected information of a draw from :
For continuous distributions, replace the sum with an integral; the quantity is then called differential entropy and has subtler properties (it can be negative, isn't invariant under change of variables) — but the formula is the same shape.
Properties:
- for discrete (each term is non-negative on , with the convention ).
- is maximised by the uniform distribution. Intuition: the uniform distribution is "maximally unpredictable", carrying the most information per draw.
- is concave in — a fact we won't need today but that explains why "averaging two distributions doesn't reduce uncertainty".
3. Cross-entropy
If is the true distribution and is the one you code with (a model), the cross-entropy is
Operationally: is the expected number of bits (or nats) you spend encoding draws from using an optimal code for . When , you've made the right choice and . When , you waste bits — and the wasted bits are what KL divergence measures.
4. KL divergence
The Kullback–Leibler divergence of from is
It satisfies the identity
which makes the "wasted bits" reading precise. Worked out:
KL is not symmetric: in general. It is not a distance — it's a divergence. The first argument is "the truth"; the second is "your model".
5. KL is non-negative (Jensen's inequality)
We want to show , with equality iff .
The key tool is Jensen's inequality for a concave function and a random variable :
The function is concave on . So:
Therefore . Equality holds iff is constant under — which forces since both are probability distributions.
This is the fundamental inequality of probabilistic ML. Almost every "loss is bounded below by zero" or "evidence lower bound" argument in the field passes through it.
6. Information-theoretic identities you will use
A handful of identities that follow by direct expansion and that we'll invoke without re-deriving from here on:
- Mutual information. . Measures how much knowing reduces uncertainty about . Non-negative; zero iff .
- Conditional entropy. . Average uncertainty about remaining after is observed.
- Chain rule of entropy. . Mirror of the product rule of probability.
7. Why this matters for what's next
Cross-entropy as a training loss (§0.7) is exactly , and its gradient with respect to doesn't see the data-only term — making it operationally identical to negative log-likelihood. KL appears explicitly in variational autoencoders, in RLHF's KL penalty against a reference policy, and as the constraint in trust-region methods (TRPO, PPO). Mutual information is the lens used to discuss compression, the information-bottleneck theory of neural networks, and self-supervised contrastive losses. One vocabulary, used everywhere.
Let and be distributions over two outcomes.
- Compute , , , and in nats.
- Compute and verify it differs from .
- Sanity check: .
References
- [1]
Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory (2nd ed.). Wiley. — Used in 0.6 (entropy, KL divergence) and 0.7 (cross-entropy as MLE).
↩