Skip to main content

Mutual Information and Kullback-Leibler Divergence

Mutual information I(X;Y): how much Y reduces uncertainty about X. Kullback-Leibler divergence: measure of distance between distributions.

What do you think about this article?

Mutual Information and Kullback-Leibler Divergence
Information Theoryfedericocalo.dev

At a Glance

Mutual information I(X;Y): how much Y reduces uncertainty about X. Kullback-Leibler divergence: measure of distance between distributions.

Mutual Information and Kullback-Leibler Divergence

Mutual information I(X;Y) measures how much uncertainty X shares with Y - that is, how much Y reduces uncertainty about X. Computed as I(X;Y) = H(X) - H(X|Y): the entropy reduction on X when Y is known. If X and Y are independent, I = 0 (Y doesn't help). If Y completely determines X, I = H(X) (maximum).

The Kullback-Leibler divergence (KL divergence) D_KL(p || q) = Σ p(x) log(p(x)/q(x)) measures how much information is lost when using distribution q to approximate true distribution p. It is asymmetric (D_KL(p || q) ≠ D_KL(q || p)), always ≥ 0, zero only if p = q. It is the key tool for measuring "distance" between distributions - a crucial concept in machine learning where we minimize KL between true distribution and model predictions (cross-entropy loss function).

Historical context and motivation

Mutual information emerges naturally from Shannon's theory when asking: "If two sources X and Y are correlated, how much information does one give me about the other?" Solomon Kullback and Richard Leibler introduce KL divergence in 1951 (paper On Information and Sufficiency) to quantify "information loss" when approximating a true distribution with a model. In the following decade, both measures (mutual information and KL divergence) become foundations of information statistics and hypothesis testing theory.

In modern machine learning, KL divergence reappears as the most used loss function for classification (cross-entropy loss, which is KL between true one-hot distribution and model predictions). Computer scientists don't always remember the name "Kullback-Leibler," but they use the divergence every time they train a classifier - evidenced by millions of neural models trained with cross-entropy in recent decades.

Article outline

This article in the Information Theory series introduces concepts of dependence between sources (mutual information) and distance between distributions (KL divergence). See series index.

  1. Mutual information: definition, formula, symmetry property
  2. Kullback-Leibler divergence: definition, asymmetry, properties
  3. Jensen's inequality: mathematical foundation of concavity
  4. Relationship between I(X;Y) and KL divergence
  5. Numerical example: concrete calculation on discrete distribution
  6. Common errors: confusing symmetry, not recognizing KL asymmetry

Mutual information: uncertainty reduction

If knowing Y reduces uncertainty about X, then X and Y are correlated. Mutual information quantifies this relationship:

I(X;Y) = H(X) - H(X|Y)

Equivalently (by chain rule):

I(X;Y) = H(X) + H(Y) - H(X,Y) = H(Y) - H(Y|X)

Interpretation: H(X) is uncertainty about X without information about Y. H(X|Y) is uncertainty about X after knowing Y. The difference is how much Y reduces uncertainty - that is, how much information Y provides about X.

Symmetry property: I(X;Y) = I(Y;X). If Y tells me something about X, then X tells me the same something about Y. This symmetry is a crucial difference with KL divergence (which is asymmetric).

Extremes:** - Minimum: I(X;Y) = 0 if and only if X and Y are independent (Y doesn't reduce uncertainty about X). - Maximum: I(X;Y) = H(X) if H(X|Y) = 0, meaning Y completely determines X (e.g., Y = f(X) deterministic function).

Numerical example: Fair coin X in {H, T} and fair die Y in {1,2,3,4,5,6}, independent. H(X) = 1 bit, H(Y) = log₂ 6 ≈ 2.585 bits. H(X|Y) = H(X) = 1 bit (independence). Then I(X;Y) = 1 - 1 = 0. Meaning: the die tells me nothing about the coin.

Alternative scenario: coin X, biased die Y that always shows 6 if X = H, otherwise uniformly 1-5. Then:

P(X=H, Y=6) = 0.5 × 1 = 0.5
P(X=T, Y=1..5) = 0.5 × (1/5 each) = 0.1 each
H(X|Y): If you see Y=6, you know X=H (H=0). If you see Y in {1,2,3,4,5}, you know X=T (H=0).
So H(X|Y) = 0.5 × 0 + 0.5 × 0 = 0.
I(X;Y) = H(X) - H(X|Y) = 1 - 0 = 1 bit.

The die provides 1 bit of information about X.

Kullback-Leibler divergence: distance between distributions

The Kullback-Leibler divergence (KL divergence) measures how much information is lost when using model q(x) to approximate true distribution p(x):

D_KL(p || q) = Σ_x p(x) log(p(x) / q(x)) = Σ_x p(x) log p(x) - Σ_x p(x) log q(x)

Interpretation: Each outcome x has a "true surprise" of log(1/p(x)) and a "predicted surprise" of log(1/q(x)). KL divergence is the expected value of the difference in surprise, weighted by true distribution p.

Crucial properties:** 1. D_KL(p || q) ≥ 0, with equality if and only if p = q (always non-negative). 2. D_KL(p || q) ≠ D_KL(q || p) (asymmetric, unlike mutual information). 3. D_KL is 0 if and only if p and q are identical. 4. D_KL is not a metric (violates triangle inequality and symmetry).

Numerical example:** - p = [0.5, 0.5] (fair coin) - q = [0.9, 0.1] (biased coin predicted) D_KL(p || q) = 0.5 log(0.5/0.9) + 0.5 log(0.5/0.1) = 0.5 × (-0.848) + 0.5 × (2.322) ≈ 0.737 nats (≈ 1.064 bits, if you convert to log₂) D_KL(q || p) = 0.9 log(0.9/0.5) + 0.1 log(0.1/0.5) = 0.9 × (0.693) + 0.1 × (-1.609) ≈ 0.461 nats Observation: D_KL(p || q) > D_KL(q || p). Using an overconfident distribution (q) as approximation of a true more uncertain one (p) costs more penalty than the reverse. This asymmetry has practical implications: in machine learning, we penalize false positives (overconfident claims) more than false negatives (excessive doubt).

Jensen's inequality and mathematical foundations

Why D_KL ≥ 0? The answer rests on Jensen's inequality for concave functions. Here is the complete proof.

Jensen's inequality (general form): If f is a concave function (f'' ≤ 0), then for any random variable X:

E[f(X)] ≤ f(E[X])

In our case, f(x) = log(x) is concave (second derivative is f''(x) = -1/x² < 0 for x > 0). By Jensen's inequality with f = log:

E[log X] ≤ log(E[X])

Now apply this to show D_KL(p || q) ≥ 0. Consider the random variable Y = q(X)/p(X), where X is distributed according to p. Then:

E_p[Y] = Σ_x p(x) × (q(x)/p(x)) = Σ_x q(x) = 1

(because q is a probability distribution). By Jensen's inequality with f = log:

E_p[log(q(X)/p(X))] ≤ log(E_p[q(X)/p(X)]) = log(1) = 0

Rearrange:

E_p[log q(X) - log p(X)] ≤ 0
E_p[log q(X)] ≤ E_p[log p(X)]
-E_p[log q(X)] ≥ -E_p[log p(X)]
-Σ p(x) log q(x) ≥ -Σ p(x) log p(x)
Σ p(x) [log p(x) - log q(x)] ≥ 0
Σ p(x) log(p(x)/q(x)) ≥ 0
D_KL(p || q) ≥ 0  ✓

Equality D_KL(p || q) = 0 holds **if and only if** the argument of Jensen's inequality is a constant, i.e., q(X)/p(X) = constant almost surely. Since E[q(X)/p(X)] = 1, the constant must be 1, meaning q(x) = p(x) for all x where p(x) > 0. Therefore D_KL = 0 iff p = q. ✓

Relationship between mutual information and KL divergence

The two concepts are linked by an elegant formula. Mutual information can be expressed as KL divergence from the independent distribution:

I(X;Y) = D_KL(p(x,y) || p(x)p(y))

Interpretation: Mutual information measures how much the true joint distribution p(x,y) deviates from the distribution we'd have if X and Y were independent p(x)p(y). If the pair is well described by independence, I is small. If the pair is strongly correlated, I is large.

Numerical explanation: Consider language corpus from previous lesson (cat/dog, run/sleep): - p(cat, run) = 0.4 - p(cat, sleep) = 0.1 - p(dog, run) = 0.3 - p(dog, sleep) = 0.2 Marginal distributions: p(cat) = 0.5, p(dog) = 0.5, p(run) = 0.7, p(sleep) = 0.3. If independent: - p(cat) × p(run) = 0.5 × 0.7 = 0.35 (vs true 0.4) - p(cat) × p(sleep) = 0.5 × 0.3 = 0.15 (vs true 0.1) - etc. D_KL(p || independent) = 0.4 log(0.4/0.35) + 0.1 log(0.1/0.15) + 0.3 log(0.3/0.35) + 0.2 log(0.2/0.15) ≈ 0.054 nats This is I(X;Y) - the amount of information X and Y share beyond independence.

Properties and related formulas

Cross-entropy loss in machine learning: When training a classifier, we minimize H(p, q) = -Σ p(x) log q(x), where p is true distribution (one-hot on correct label) and q is model prediction. Since H(p) = 0 for one-hot distribution (entropy of certainty), minimizing H(p, q) equals minimizing D_KL(p || q). So the "cross-entropy" loss function is actually KL divergence - no accident of nomenclature, a direct connection.

Relative Information: A historically used synonym for KL divergence is "relative entropy" or "divergence measure." Shannon used "information divergence." Today, D_KL is the standard.

Why KL is not a metric: numerical counterexample

KL divergence **is not a metric** because it violates two fundamental metric properties.

1. Asymmetry: A metric must satisfy symmetry d(x, y) = d(y, x). But D_KL(p || q) ≠ D_KL(q || p). We already saw a numerical example with p = [0.5, 0.5] and q = [0.9, 0.1]:

D_KL(p || q) ≈ 0.737 nats  (p is indecisive, q is confident - costly to approximate indecision with certainty)
D_KL(q || p) ≈ 0.461 nats  (q is confident, p is indecisive - less costly to approximate certainty with indecision)

Asymmetry reflects practical reality: in machine learning, when training a model, we use **D_KL(p_true || q_model)**, severely penalizing the model if it assigns low probability to observed outcomes (dangerous false negatives). If we used the opposite, we'd penalize less, potentially leading to overconfident predictions.

2. Triangle inequality violation: A metric must satisfy d(x, z) ≤ d(x, y) + d(y, z). Show KL violates it. Take three distributions:

p = [0.5, 0.5]    (fair coin)
q = [0.7, 0.3]    (slightly biased)
r = [1.0, 0.0]    (completely biased)

Computing:

D_KL(p || r) = 0.5 log(0.5/1.0) + 0.5 log(0.5/0.0)
             = 0.5 × (-1) + 0.5 × ∞ = ∞

D_KL(p || q) = 0.5 log(0.5/0.7) + 0.5 log(0.5/0.3)
             ≈ 0.5 × (-0.470) + 0.5 × (0.737)
             ≈ 0.134 nats

D_KL(q || r) = 0.7 log(0.7/1.0) + 0.3 log(0.3/0.0)
             = 0.7 × (-0.357) + 0.3 × ∞ = ∞

Divergence becomes infinite when r assigns probability 0 to an outcome where p has probability > 0 (different supports). A more solid counterexample with identical supports: p=[0.9, 0.1], q=[0.7, 0.3], r=[0.6, 0.4] gives finite numbers but still D_KL(p || r) > D_KL(p || q) + D_KL(q || r) in general (triangle violated).

Conclusion: KL divergence is a "divergence" (non-negative, zero iff p = q) but not a metric. To get a true metric, use **Jensen-Shannon divergence** (symmetric) or Hellinger distance (metric, weaker than KL).

The bridge: Cross-entropy, KL and entropy

There's an elegant relationship uniting all three concepts and explaining KL's crucial role in machine learning:

H(p, q) = H(p) + D_KL(p || q)

where H(p, q) is the **cross-entropy** between p and q. Expanded:

Cross-entropy H(p, q) = -Σ p(x) log q(x)
Entropy H(p) = -Σ p(x) log p(x)
KL divergence D_KL(p || q) = Σ p(x) log(p(x)/q(x)) = Σ p(x) log p(x) - Σ p(x) log q(x)
                          = H(p) - (-Σ p(x) log q(x))
                          = H(p) - H(p, q)

Therefore: H(p, q) = H(p) + D_KL(p || q)  ✓

Interpretation in machine learning: When training a neural network for classification, you minimize cross-entropy loss:

loss = H(p_true, p_model)

where p_true is the true distribution (typically one-hot on the correct label) and p_model is the prediction. Since H(p_true) is fixed (for one-hot, H = 0), minimizing H(p_true, p_model) is equivalent to minimizing D_KL(p_true || p_model). In other words, you're minimizing the divergence between true distribution and model - **not** cross-entropy directly, but the KL "hiding behind" the name "cross-entropy loss".

The information-theoretic Venn diagram

Imagine three rectangles representing entropies:

Description in words (to sketch mentally):

  • Draw a large rectangle for H(X), total uncertainty on X.
  • Draw a second large rectangle for H(Y), total uncertainty on Y.
  • The two rectangles overlap (intersect) in a central region.
  • In the intersection (overlapping region): the **mutual information I(X; Y)** - the portion of uncertainty that X and Y share.
  • In the left part of H(X) not overlapping: H(X|Y) - uncertainty on X remaining even after knowing Y.
  • In the right part of H(Y) not overlapping: H(Y|X) - uncertainty on Y remaining even after knowing X.
  • If you draw a small rectangle around both: H(X, Y) - total uncertainty of the pair (all area covered by both rectangles).

Formulas visible in the diagram:

H(X, Y) = H(X) + H(Y|X) = H(Y) + H(X|Y) = H(X) + H(Y) - I(X; Y)

Total area = H(X, Y)
Intersection = I(X; Y)
Left (X only) = H(X|Y)
Right (Y only) = H(Y|X)
Full X = H(X) = I(X;Y) + H(X|Y)
Full Y = H(Y) = I(X;Y) + H(Y|X)

When X and Y are independent, the intersection (I(X; Y)) disappears and the diagram shows two separate rectangles with no overlap.

Exposition trade-off

I chose to introduce mutual information before KL divergence because I is more intuitive (answers "how correlated are two things?"), while D_KL requires an "approximation" mindset (answers "how bad is approximating p with q?"). However, the two concepts are deeply linked - their connection is the formula I(X;Y) = D_KL(p(x,y) || p(x)p(y)), which unifies everything. If I taught in reverse (D_KL first, I after), you'd lose the intuition of "information sharing" and start with "distance between distributions," more abstract.

Advanced connections

Jensen-Shannon divergence: A compromise between D_KL(p || q) and D_KL(q || p) is Jensen-Shannon divergence, which is symmetric and nearly a true metric: JS(p, q) = (D_KL(p || m) + D_KL(q || m)) / 2, where m = (p + q) / 2 is the average. Used in some machine learning contexts where symmetry is desirable.

Information bottleneck principle: In compression theory, the information bottleneck (Tishby et al. 2000) explains how to compress X while preserving information about Y. The solution minimizes I(X; T) - β I(Y; T), where T is the compressed representation. This unites compression and preservation of relevant information.

Bayesian inference and KL divergence: When updating prior belief p(x) to posterior q(x|data) using Bayes' theorem, KL divergence between posterior and prior quantifies how much information the data provides.

Pitfalls and common errors

  • Believing I(X;Y) is a metric. I is symmetric (good property), but doesn't satisfy triangle inequality by definition. KL divergence is not a metric (asymmetric).
  • Confusing D_KL(p || q) with D_KL(q || p). They're not equal. D_KL(p || q) penalizes model q when it assigns low probability to observed outcomes (frequentist). D_KL(q || p) penalizes when it assigns high probability to non-outcomes.
  • Using cross-entropy as "accuracy". Cross-entropy is a loss (continuous number), not accuracy (fraction 0-1). A cross-entropy of 0.5 doesn't mean "50% correct".
  • Forgetting KL divergence uses natural logarithm. In information theory, we often use log₂ for entropy and bits. In KL divergence, default is natural logarithm (ln, nats). To convert to bits, divide by ln(2).
  • Thinking D_KL = 0 means "distributions are equal". True: D_KL = 0 iff p = q. False: small D_KL doesn't mean "similar" - for very high-dimensional distributions, D_KL can be large even if visually similar.

Advanced exercises

  1. Manual I(X;Y) calculation: Create joint distribution p(X, Y) for small variables (e.g., X in {0,1}, Y in {0,1,2}). Compute H(X), H(X|Y), I(X;Y). Then compute D_KL(p(x,y) || p(x)p(y)) and verify they're equal.
  2. KL asymmetry: Take two distributions p, q. Compute D_KL(p || q) and D_KL(q || p). Explain which is larger.
  3. Cross-entropy vs KL: Train small classifier (e.g., logistic regression) on binary dataset. Extract final cross-entropy loss value. Manually compute D_KL between true distributions (one-hot) and predicted. Verify they're equal.
  4. Jensen-Shannon symmetric: Implement JS(p, q) for two distributions. Verify that JS(p, q) = JS(q, p) (symmetry).
  5. Information bottleneck: Create X in {0,1}, Y = X with noise (P(Y=X) = 0.8, P(Y≠X) = 0.2). Compute I(X; Y). Then create T = sign(X + noise) and compute I(X; T) and I(Y; T). Which is higher?
  6. KL convergence to zero: Create true distribution p. Generate sequence of approximations q_n converging to p (e.g., increasing sample size). Measure D_KL(p || q_n) as n grows. Does it converge to 0?

Essential bibliography

  • Thomas M. Cover, Joy A. Thomas. Elements of Information Theory, 2nd ed., Wiley, 2006. (Chapter 2 on mutual information, Chapter 3 on KL divergence.)
  • Kullback, S., Leibler, R.A. "On Information and Sufficiency," Annals of Mathematical Statistics, 22:1, pp. 79-86, 1951.
  • David J.C. MacKay. Information Theory, Inference, and Learning Algorithms, Cambridge University Press, 2003. (Chapters 1-2 on entropy and KL.)
  • Tishby, N., Pereira, F.C., Bialek, W. "The information bottleneck method," The 37th Annual Allerton Conference on Communication, Control, and Computing, 1999.
  • Stanford Encyclopedia of Philosophy - "Information" - https://plato.stanford.edu/entries/information/ (open access)

Technical prerequisites

  • Shannon entropy (previous article 1716): definition, formula, properties.
  • Conditional probability and Bayes' rule: p(x|y), p(x,y) = p(x)p(y|x).
  • Logarithms: ln, log₂, conversion between bases, logarithm properties.
  • Summation and expected value: E[X] = Σ p(x) x.
  • Set notation and operators Σ, ∀.

Related resources

Article published 2026-10-03, graduate/advanced level.

Commenti

Caricamento commenti...

Sign in to comment

Discuss this article in the community

Join the Telegram group to discuss with other developers, ask questions and share your experiences.

Did you like this article?

Explore other content on the blog or discover my projects