Mutual information
Define mutual information and read it as the KL divergence between the joint and the product of marginals.
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 and is the KL divergence between their joint distribution and the product of their marginals[3]:
The product is what the joint would be if and were independent. So 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:
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 uniform on and , so is fully determined by . Covariance sees nothing, , because the symmetric contributions cancel. Mutual information sees the whole dependence: takes value with probability and with probability , so 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 , , which is at , bits at , and bits at . 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:
where is the conditional entropy, the average remaining uncertainty in once is known.
Derivation of the first equality. Use in the definition:
The first term is (after marginalizing out ); the second is . So .
The reading is direct: mutual information is the reduction in uncertainty about from learning . It is symmetric, , so the bits flow both ways equally. It is also bounded: , since conditional entropy is nonnegative.
Check it on arrays
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, scores the full entropy, and the nonlinear has zero covariance yet clearly positive mutual information.
Exercises
Exercise 1. A joint distribution over with has and . Compute in bits by hand.
Solution
Step 1. The marginals are uniform: and likewise . So every product .
Step 2. Plug into . The two diagonal cells contribute each and the two off-diagonal cells each:
Step 3. Evaluate: and , so
Positive and below the -bit cap , as the bound requires.
Exercise 2. Prove that when and are independent, directly from the definition, without invoking the Gibbs inequality.
Solution
Step 1. Independence means for every pair, so the ratio inside the log is
Step 2. Then in every term, so the sum is
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 , the closed form is bits. Build a discretized joint table for by binning samples and confirm the plug-in estimate approaches the closed form.
Solution
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 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]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]MacKay, D. J. C. (2003). Information Theory, Inference, and Learning Algorithms. Cambridge University Press. §8.1, mutual information and dependence.↩
- [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↩