AI Knowledge Tree

Mutual information

Define mutual information and read it as the KL divergence between the joint and the product of marginals.

⏱ 30 minPrerequisites:KL divergence and cross-entropy

Covariance measures linear co-movement and misses everything nonlinear. Mutual information measures dependence of any kind: it is zero exactly when two variables are independent, and it counts the bits one variable reveals about the other[1, 2]. It underlies feature selection, the information bottleneck, and representation-learning objectives.

Definition

The mutual information between XX and YY is the KL divergence between their joint distribution and the product of their marginals[3]:

I(X;Y)=DKL(pX,Y ∥ pX pY)=∑x,yp(x,y)log⁡p(x,y)p(x) p(y).I(X; Y) = D_{\mathrm{KL}}\big(p_{X,Y} \,\|\, p_X\, p_Y\big) = \sum_{x, y} p(x, y)\log\frac{p(x, y)}{p(x)\,p(y)}.

The product pXpYp_X p_Y is what the joint would be if XX and YY were independent. So I(X;Y)I(X; Y) measures how far the true joint sits from independence, in the KL sense.

Nonnegativity and the independence test

Because mutual information is a KL divergence, the previous lesson's Gibbs inequality applies directly:

I(X;Y)≥0,with I(X;Y)=0  ⟺  pX,Y=pX pY.I(X; Y) \ge 0, \quad \text{with } I(X; Y) = 0 \iff p_{X,Y} = p_X\,p_Y.

Zero mutual information is exactly independence. Unlike zero covariance, this detects every form of dependence, linear or not.

This is the confusion worth naming: zero covariance is not independence, and the two measures can disagree completely. Take XX uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2, so YY is fully determined by XX. Covariance sees nothing, Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0, because the symmetric ±1\pm 1 contributions cancel. Mutual information sees the whole dependence: YY takes value 00 with probability 1/31/3 and 11 with probability 2/32/3, so I(X;Y)=H(Y)=−13log⁡213−23log⁡223=0.918I(X; Y) = H(Y) = -\tfrac13\log_2\tfrac13 - \tfrac23\log_2\tfrac23 = 0.918 bits. A linear summary reports independence; the information measure reports near-complete dependence. Where the dependence is linear the gap closes: for a bivariate Gaussian with correlation ρ\rho, I=−12log⁡2(1−ρ2)I = -\tfrac12\log_2(1 - \rho^2), which is 00 at ρ=0\rho = 0, 0.210.21 bits at ρ=0.5\rho = 0.5, and 1.201.20 bits at ρ=0.9\rho = 0.9. Covariance and mutual information agree on independence only when the dependence has no nonlinear part to miss.

The entropy decomposition

Expanding the log of the ratio connects mutual information to entropy:

I(X;Y)=H(X)−H(X∣Y)=H(Y)−H(Y∣X)=H(X)+H(Y)−H(X,Y),I(X; Y) = H(X) - H(X \mid Y) = H(Y) - H(Y \mid X) = H(X) + H(Y) - H(X, Y),

where H(X∣Y)=−∑x,yp(x,y)log⁡p(x∣y)H(X \mid Y) = -\sum_{x,y} p(x,y)\log p(x \mid y) is the conditional entropy, the average remaining uncertainty in XX once YY is known.

Derivation of the first equality. Use p(x,y)=p(x∣y)p(y)p(x, y) = p(x \mid y)p(y) in the definition:

I(X;Y)=∑x,yp(x,y)log⁡p(x∣y)p(x)=−∑x,yp(x,y)log⁡p(x)+∑x,yp(x,y)log⁡p(x∣y).I(X; Y) = \sum_{x,y} p(x,y)\log\frac{p(x \mid y)}{p(x)} = -\sum_{x,y} p(x,y)\log p(x) + \sum_{x,y} p(x,y)\log p(x \mid y).

The first term is −∑xp(x)log⁡p(x)=H(X)-\sum_x p(x)\log p(x) = H(X) (after marginalizing out yy); the second is −H(X∣Y)-H(X \mid Y). So I(X;Y)=H(X)−H(X∣Y)I(X; Y) = H(X) - H(X \mid Y).

The reading is direct: mutual information is the reduction in uncertainty about XX from learning YY. It is symmetric, I(X;Y)=I(Y;X)I(X; Y) = I(Y; X), so the bits flow both ways equally. It is also bounded: I(X;Y)≤min⁡(H(X),H(Y))I(X; Y) \le \min(H(X), H(Y)), since conditional entropy is nonnegative.

Check it on arrays

python · numpy
import numpy as np

rng = np.random.default_rng(15)

def mutual_info(joint):
    """MI in bits from a joint PMF table."""
    joint = joint / joint.sum()
    px = joint.sum(axis=1, keepdims=True)
    py = joint.sum(axis=0, keepdims=True)
    mask = joint > 0
    return np.sum(joint[mask] * np.log2(joint[mask] / (px * py)[mask]))

# Independent variables: MI ~ 0
indep = np.outer([0.3, 0.7], [0.4, 0.6])
print("MI(independent):", round(mutual_info(indep), 6), " (~0)")

# Perfectly dependent: Y = X, MI = H(X)
perfect = np.array([[0.5, 0.0], [0.0, 0.5]])
print("MI(Y=X):", round(mutual_info(perfect), 4), " = H(X) = 1 bit")

# Nonlinear dependence with zero covariance: Y = X^2, X in {-1,0,1}
rng2 = np.random.default_rng(16)
X = rng2.choice([-1, 0, 1], size=500_000)
Y = X**2
cov = np.mean((X - X.mean()) * (Y - Y.mean()))
# Build empirical joint over the value grid
xs, ys = np.unique(X), np.unique(Y)
joint = np.zeros((len(xs), len(ys)))
for i, xv in enumerate(xs):
    for j, yv in enumerate(ys):
        joint[i, j] = np.mean((X == xv) & (Y == yv))
print("Cov(X, X^2):", round(cov, 4), " but MI:", round(mutual_info(joint), 4), "bits")

Independent variables score zero, Y=XY = X scores the full entropy, and the nonlinear Y=X2Y = X^2 has zero covariance yet clearly positive mutual information.

Exercises

Exercise 1. A joint distribution over (X,Y)(X, Y) with X,Y∈{0,1}X, Y \in \{0, 1\} has p(0,0)=p(1,1)=0.4p(0,0) = p(1,1) = 0.4 and p(0,1)=p(1,0)=0.1p(0,1) = p(1,0) = 0.1. Compute I(X;Y)I(X; Y) in bits by hand.

Solution

Step 1. The marginals are uniform: pX(0)=0.4+0.1=0.5p_X(0) = 0.4 + 0.1 = 0.5 and likewise pX(1)=pY(0)=pY(1)=0.5p_X(1) = p_Y(0) = p_Y(1) = 0.5. So every product p(x)p(y)=0.25p(x)p(y) = 0.25.

Step 2. Plug into I(X;Y)=∑x,yp(x,y)log⁡2p(x,y)p(x)p(y)I(X;Y) = \sum_{x,y} p(x,y)\log_2\frac{p(x,y)}{p(x)p(y)}. The two diagonal cells contribute 0.4log⁡20.40.250.4\log_2\frac{0.4}{0.25} each and the two off-diagonal cells 0.1log⁡20.10.250.1\log_2\frac{0.1}{0.25} each:

I=2⋅0.4log⁡21.6+2⋅0.1log⁡20.4.I = 2 \cdot 0.4\log_2 1.6 + 2 \cdot 0.1\log_2 0.4.

Step 3. Evaluate: log⁡21.6=0.6781\log_2 1.6 = 0.6781 and log⁡20.4=−1.3219\log_2 0.4 = -1.3219, so

I=0.8⋅0.6781+0.2⋅(−1.3219)=0.5425−0.2644=0.278 bits.I = 0.8 \cdot 0.6781 + 0.2 \cdot (-1.3219) = 0.5425 - 0.2644 = 0.278 \text{ bits}.

Positive and below the 11-bit cap min⁡(H(X),H(Y))=1\min(H(X), H(Y)) = 1, as the bound requires.

Exercise 2. Prove that I(X;Y)=0I(X; Y) = 0 when XX and YY are independent, directly from the definition, without invoking the Gibbs inequality.

Solution

Step 1. Independence means p(x,y)=p(x) p(y)p(x, y) = p(x)\,p(y) for every pair, so the ratio inside the log is

p(x,y)p(x) p(y)=p(x) p(y)p(x) p(y)=1.\frac{p(x, y)}{p(x)\,p(y)} = \frac{p(x)\,p(y)}{p(x)\,p(y)} = 1.

Step 2. Then log⁡p(x,y)p(x)p(y)=log⁡1=0\log\frac{p(x,y)}{p(x)p(y)} = \log 1 = 0 in every term, so the sum is

I(X;Y)=∑x,yp(x,y)⋅0=0.I(X; Y) = \sum_{x, y} p(x, y) \cdot 0 = 0.

Independence makes the joint equal to the product of marginals, the exact reference point mutual information measures distance from.

Exercise 3 (code). For a bivariate Gaussian with correlation ρ\rho, the closed form is I=−12log⁡2(1−ρ2)I = -\tfrac12\log_2(1 - \rho^2) bits. Build a discretized joint table for ρ=0.5\rho = 0.5 by binning samples and confirm the plug-in estimate approaches the closed form.

Solution
python · numpy
import numpy as np

def mutual_info(joint):
    joint = joint / joint.sum()
    px = joint.sum(axis=1, keepdims=True)
    py = joint.sum(axis=0, keepdims=True)
    mask = joint > 0
    return np.sum(joint[mask] * np.log2(joint[mask] / (px * py)[mask]))

rng = np.random.default_rng(21)
rho = 0.5
N = 2_000_000
z1 = rng.normal(size=N)
z2 = rng.normal(size=N)
x = z1
y = rho * z1 + np.sqrt(1 - rho**2) * z2          # corr(x, y) = rho

# Bin into a joint histogram; finer bins approach the continuous MI
edges = np.linspace(-4, 4, 41)
joint, _, _ = np.histogram2d(x, y, bins=[edges, edges])
mi_est = mutual_info(joint)
closed = -0.5 * np.log2(1 - rho**2)

# Binning loses some information, so the estimate sits a bit below the truth
assert abs(mi_est - closed) < 0.05
print("MI estimate:", round(mi_est, 4), " closed form:", round(closed, 4))

The binned plug-in estimate lands within a few hundredths of the closed-form −12log⁡2(1−0.25)=0.2075-\tfrac12\log_2(1 - 0.25) = 0.2075 bits; the small gap is the discretization discarding sub-bin structure.

The next lesson moves from static dependence to dependence over time: Markov chains.


References

  1. [1]Shannon, C. E. (1948). "A mathematical theory of communication". Bell System Technical Journal, 27, 379-423. The origin of mutual information as the rate of information transmission. doi:10.1002/j.1538-7305.1948.tb01338.x↩
  2. [2]MacKay, D. J. C. (2003). Information Theory, Inference, and Learning Algorithms. Cambridge University Press. §8.1, mutual information and dependence.↩
  3. [3]Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory (2nd ed.). Wiley. §2.4, mutual information as relative entropy between joint and product marginals. doi:10.1002/047174882X↩