Skip to content
ml-recall

Topic notes

Information Theory

Information as reduction of uncertainty — self-information, entropy, joint and conditional entropy, mutual information, KL divergence and the cross-entropy loss used to train classifiers.

2-min read AI-checked

Practise 36 questions Take a test

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 pp: I=−log⁡2pI=-\log_2p bits (use ln⁡\ln for nats; 1 nat =1/ln⁡2≈1.443=1/\ln2\approx1.443 bits)1. A fair die roll carries log⁡26≈2.585\log_2 6\approx2.585 bits.

Entropy is the expected self-information, H(X)=−∑xp(x)log⁡p(x)H(X)=-\sum_x p(x)\log p(x)1:

Two variables2: joint entropy H(X,Y)H(X,Y) satisfies max⁡(H(X),H(Y))≤H(X,Y)≤H(X)+H(Y)\max(H(X),H(Y))\le H(X,Y)\le H(X)+H(Y), with the upper bound reached iff X⊥YX\perp Y2. Conditional entropy H(Y∣X)=H(X,Y)−H(X)H(Y\mid X)=H(X,Y)-H(X) is what remains unknown about YY after seeing XX2; the uncertainty about what was sent that remains after receiving the signal, H(sent∣received)H(\text{sent}\mid\text{received}), is called the equivocation6. If Y=f(X)Y=f(X) then H(X,Y)=H(X)H(X,Y)=H(X), and a one-to-one relabelling of a pair keeps its joint entropy.

Mutual information2:

I(X,Y)=H(X)−H(X∣Y)=H(Y)−H(Y∣X)=H(X)+H(Y)−H(X,Y)=DKL(p(x,y) ∥ p(x)p(y))I(X,Y)=H(X)-H(X\mid Y)=H(Y)-H(Y\mid X)=H(X)+H(Y)-H(X,Y)=D_{\mathrm{KL}}\big(p(x,y)\,\|\,p(x)p(y)\big)

It is symmetric, ≥0\ge0, and 00 iff XX and YY are independent2. Unlike correlation it sees non-linear dependence: if YY is a function of XX, I(X,Y)=H(Y)I(X,Y)=H(Y) 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 DKL(P∥Q)=∑xP(x)log⁡P(x)Q(x)D_{\mathrm{KL}}(P\|Q)=\sum_xP(x)\log\frac{P(x)}{Q(x)}3:

Cross-entropy H(P,Q)=−∑xP(x)log⁡Q(x)H(P,Q)=-\sum_xP(x)\log Q(x) is the expected surprisal of someone who believes QQ while data come from PP3,4. Since H(P,Q)=H(P)+DKL(P∥Q)H(P,Q)=H(P)+D_{\mathrm{KL}}(P\|Q) and H(P)H(P) is fixed by the data, minimising cross-entropy over QQ is minimising KL, and for one-hot labels it is maximising the log-likelihood: the loss is −log⁡qy-\log q_y for the true class yy3,4. With softmax outputs this is −zy+log⁡∑jezj-z_y+\log\sum_je^{z_j} for logits zz4. For fixed PP it is smallest at Q=PQ=P.

Formulas

DKL(N(μ0,σ02) ∥ N(μ1,σ12))=ln⁡σ1σ0+σ02+(μ0−μ1)22σ12−12D_{\mathrm{KL}}\big(\mathcal N(\mu_0,\sigma_0^2)\,\|\,\mathcal N(\mu_1,\sigma_1^2)\big)=\ln\frac{\sigma_1}{\sigma_0}+\frac{\sigma_0^2+(\mu_0-\mu_1)^2}{2\sigma_1^2}-\frac12

With equal variances this is (μ0−μ1)2/(2σ2)(\mu_0-\mu_1)^2/(2\sigma^2) in both directions5. Entropy is concave: the entropy of a mixture is at least the average entropy of its components.

Worked example

P=(0.5,0.3,0.2)P=(0.5,0.3,0.2): H(P)=0.5(1)+0.3(1.737)+0.2(2.322)≈1.485H(P)=0.5(1)+0.3(1.737)+0.2(2.322)\approx1.485 bits. Against the uniform Q=(13,13,13)Q=(\tfrac13,\tfrac13,\tfrac13): H(P,Q)=log⁡23≈1.585H(P,Q)=\log_23\approx1.585, so DKL(P∥Q)≈1.585−1.485=0.099D_{\mathrm{KL}}(P\|Q)\approx1.585-1.485=0.099 bits, while DKL(Q∥P)≈0.101D_{\mathrm{KL}}(Q\|P)\approx0.101 bits — different. Mutual information of a fair bit sent through a channel that flips it with probability 0.25: I=1−H(0.25)=1−0.811≈0.189I=1-H(0.25)=1-0.811\approx0.189 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

References

  1. 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)
  2. Dive into Deep Learning — Sec. 22.11.3 Mutual Information (Joint Entropy; Conditional Entropy; Mutual Information; Properties of Mutual Information)
  3. Dive into Deep Learning — Sec. 22.11.4 Kullback–Leibler Divergence (KL Divergence Properties); 22.11.5 Cross-Entropy
  4. Dive into Deep Learning — Sec. 4.1.2 Loss Function (Log-Likelihood; Softmax and Cross-Entropy Loss); 4.1.3 Information Theory Basics
  5. Wikipedia: Kullback–Leibler divergence — Definition; Properties; Examples — Multivariate normal distributions (univariate case)
  6. Wikipedia: Conditional entropy — Definition (equivocation); Properties
  7. Wikipedia: Information gain (decision tree) — Introduction; General definition; Example (entropy of the parent minus average entropy of the children)