AI / ML Theory
Chapter 0 · 22 min

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 pp occurs, the information it carries is

I(p)=−log⁡p.I(p) = -\log p.

The minus sign makes II non-negative (since p≤1p \le 1, log⁡p≤0\log p \le 0). Three sanity checks:

  • I(1)=0I(1) = 0: a certain outcome carries zero information.
  • I(p)→∞I(p) \to \infty as p→0p \to 0: an impossible-looking outcome that nonetheless occurs is maximally informative.
  • I(p1p2)=I(p1)+I(p2)I(p_1 p_2) = I(p_1) + I(p_2): information from two independent events adds. This additivity is what fixes the form of II uniquely up to a log base.

Base of the log sets the units: base 2 gives bits, base ee 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 pp:

  H[p]  =  EX∼p[−log⁡p(X)]  =  −∑xp(x) log⁡p(x).  \boxed{\;H[p] \;=\; \mathbb{E}_{X \sim p}[-\log p(X)] \;=\; -\sum_x p(x)\,\log p(x).\;}

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:

  • H[p]≥0H[p] \ge 0 for discrete pp (each term −plog⁡p-p \log p is non-negative on [0,1][0, 1], with the convention 0log⁡0=00 \log 0 = 0).
  • H[p]H[p] is maximised by the uniform distribution. Intuition: the uniform distribution is "maximally unpredictable", carrying the most information per draw.
  • HH is concave in pp — a fact we won't need today but that explains why "averaging two distributions doesn't reduce uncertainty".

3. Cross-entropy

If pp is the true distribution and qq is the one you code with (a model), the cross-entropy is

H[p,q]  =  EX∼p[−log⁡q(X)]  =  −∑xp(x) log⁡q(x).H[p, q] \;=\; \mathbb{E}_{X \sim p}[-\log q(X)] \;=\; -\sum_x p(x)\,\log q(x).

Operationally: H[p,q]H[p, q] is the expected number of bits (or nats) you spend encoding draws from pp using an optimal code for qq. When q=pq = p, you've made the right choice and H[p,q]=H[p]H[p, q] = H[p]. When q≠pq \neq p, you waste bits — and the wasted bits are what KL divergence measures.

4. KL divergence

The Kullback–Leibler divergence of pp from qq is

  KL(p ∥ q)  =  ∑xp(x) log⁡p(x)q(x)  =  EX∼p ⁣[log⁡p(X)q(X)].  \boxed{\;\mathrm{KL}(p\,\|\,q) \;=\; \sum_x p(x)\,\log\frac{p(x)}{q(x)} \;=\; \mathbb{E}_{X \sim p}\!\left[\log\frac{p(X)}{q(X)}\right].\;}

It satisfies the identity

KL(p ∥ q)=H[p,q]−H[p],\mathrm{KL}(p\,\|\,q) = H[p, q] - H[p],

which makes the "wasted bits" reading precise. Worked out:

H[p,q]−H[p]=−∑xp(x)log⁡q(x)  −  (−∑xp(x)log⁡p(x))=∑xp(x)[log⁡p(x)−log⁡q(x)]=∑xp(x)log⁡p(x)q(x)=KL(p ∥ q).\begin{aligned} H[p, q] - H[p] &= -\sum_x p(x)\log q(x) \;-\; \big(-\sum_x p(x)\log p(x)\big) \\ &= \sum_x p(x)\big[\log p(x) - \log q(x)\big] \\ &= \sum_x p(x)\log\frac{p(x)}{q(x)} \\ &= \mathrm{KL}(p\,\|\,q). \end{aligned}

KL is not symmetric: KL(p ∥ q)≠KL(q ∥ p)\mathrm{KL}(p\,\|\,q) \neq \mathrm{KL}(q\,\|\,p) 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 KL(p ∥ q)≥0\mathrm{KL}(p\,\|\,q) \ge 0, with equality iff p=qp = q.

The key tool is Jensen's inequality for a concave function φ\varphi and a random variable ZZ:

E[φ(Z)]≤φ(E[Z]).\mathbb{E}[\varphi(Z)] \le \varphi(\mathbb{E}[Z]).

The function log⁡\log is concave on (0,∞)(0, \infty). So:

−KL(p ∥ q)=−∑xp(x)log⁡p(x)q(x)  =  ∑xp(x)log⁡q(x)p(x)=EX∼p ⁣[log⁡q(X)p(X)]≤log⁡ EX∼p ⁣[q(X)p(X)](Jensen, log⁡ concave)=log⁡∑xp(x) q(x)p(x)=log⁡∑xq(x)=log⁡1=0.\begin{aligned} -\mathrm{KL}(p\,\|\,q) &= -\sum_x p(x)\log\frac{p(x)}{q(x)} \;=\; \sum_x p(x)\log\frac{q(x)}{p(x)} \\ &= \mathbb{E}_{X\sim p}\!\left[\log\frac{q(X)}{p(X)}\right] \\ &\le \log\,\mathbb{E}_{X\sim p}\!\left[\frac{q(X)}{p(X)}\right] \quad \text{(Jensen, $\log$ concave)} \\ &= \log\sum_x p(x)\,\frac{q(x)}{p(x)} \\ &= \log\sum_x q(x) \\ &= \log 1 = 0. \end{aligned}

Therefore KL(p ∥ q)≥0\mathrm{KL}(p\,\|\,q) \ge 0. Equality holds iff q(x)/p(x)q(x)/p(x) is constant under pp — which forces q=pq = p 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. I(X;Y)=KL(pXY ∥ pXpY)=H[X]−H[X∣Y]I(X; Y) = \mathrm{KL}(p_{XY}\,\|\,p_X p_Y) = H[X] - H[X \mid Y]. Measures how much knowing YY reduces uncertainty about XX. Non-negative; zero iff X⊥YX \perp Y.
  • Conditional entropy. H[X∣Y]=−E(X,Y)[log⁡p(X∣Y)]H[X \mid Y] = -\mathbb{E}_{(X,Y)}[\log p(X \mid Y)]. Average uncertainty about XX remaining after YY is observed.
  • Chain rule of entropy. H[X,Y]=H[X]+H[Y∣X]=H[Y]+H[X∣Y]H[X, Y] = H[X] + H[Y \mid X] = H[Y] + H[X \mid Y]. 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 H[pdata,qθ]H[p_{\text{data}}, q_{\theta}], and its gradient with respect to θ\theta doesn't see the data-only term H[pdata]H[p_{\text{data}}] — 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.

Self-test

Let p=(0.5,0.5)p = (0.5, 0.5) and q=(0.9,0.1)q = (0.9, 0.1) be distributions over two outcomes.

  1. Compute H[p]H[p], H[q]H[q], H[p,q]H[p, q], and KL(p ∥ q)\mathrm{KL}(p\,\|\,q) in nats.
  2. Compute KL(q ∥ p)\mathrm{KL}(q\,\|\,p) and verify it differs from KL(p ∥ q)\mathrm{KL}(p\,\|\,q).
  3. Sanity check: H[p,q]−H[p]=KL(p ∥ q)H[p, q] - H[p] = \mathrm{KL}(p\,\|\,q).

References

  1. [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).

    ↩