Mutual Information
How much does knowing tell you about ? The mutual information answers in bits: it is the reduction in uncertainty about when is revealed. It is symmetric, never negative, and zero exactly when the variables are independent, which makes it a general measure of dependence that also catches nonlinear relations.
Definition
Equivalently, it is the KL divergence (Relative Entropy (KL Divergence)) between the joint distribution and the product of the marginals:
import numpy as np
def H(p):
p = np.asarray(p, float).ravel(); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
def mutual_information(P):
Px, Py = P.sum(axis=1), P.sum(axis=0)
Q = np.outer(Px, Py)
nz = P > 0
return float(np.sum(P[nz] * np.log2(P[nz] / Q[nz])))
P = np.array([[0.56, 0.14], # weather x umbrella
[0.06, 0.24]])
print("I from KL formula: ", round(mutual_information(P), 4))
print("H(X) + H(Y) - H(X,Y): ", round(H(P.sum(axis=1)) + H(P.sum(axis=0)) - H(P), 4))I from KL formula: 0.2361
H(X) + H(Y) - H(X,Y): 0.2361
The information diagram
The relations between the quantities fit a picture with two overlapping circles: and are the circles, the overlap is , the parts outside the overlap are and , and the union is .
| Region | Quantity |
|---|---|
| left circle only | |
| overlap | |
| right circle only | |
| union |
import numpy as np
def H(p):
p = np.asarray(p, float).ravel(); p = p[p > 0]
return float(-np.sum(p * np.log2(p))) + 0.0
rng = np.random.default_rng(0)
P = rng.dirichlet(np.ones(12)).reshape(3, 4)
HX, HY, HXY = H(P.sum(axis=1)), H(P.sum(axis=0)), H(P)
I = HX + HY - HXY
print(f"H(X|Y) = {HXY - HY:.4f} I(X;Y) = {I:.4f} H(Y|X) = {HXY - HX:.4f}")
print(f"sum of the three regions = {HXY - HY + I + HXY - HX:.4f} = H(X,Y) = {HXY:.4f}")H(X|Y) = 1.0229 I(X;Y) = 0.1407 H(Y|X) = 1.5312
sum of the three regions = 2.6948 = H(X,Y) = 2.6948
Properties
- Symmetry: .
- Non-negativity: , with equality exactly for independent variables (from Gibbs' inequality).
- Bounded: .
- Self-information: , which is why entropy is sometimes called self-information.
Mutual information versus correlation
Correlation only measures linear dependence. Mutual information catches any dependence. Here for symmetric : correlation is zero, but tells us a lot about :
import numpy as np
x_vals = np.array([-2, -1, 0, 1, 2]); px = np.ones(5) / 5
y_vals = np.unique(x_vals**2) # 0, 1, 4
P = np.zeros((5, 3))
for i, x in enumerate(x_vals):
P[i, np.searchsorted(y_vals, x**2)] = px[i]
Py = P.sum(axis=0)
Ey = Py @ y_vals; Ex = px @ x_vals
cov = sum(P[i, j] * (x_vals[i] - Ex) * (y_vals[j] - Ey) for i in range(5) for j in range(3))
Q = np.outer(px, Py); nz = P > 0
print("covariance:", round(float(cov), 6))
print("mutual information:", round(float(np.sum(P[nz] * np.log2(P[nz] / Q[nz]))), 4), "bits")
print("H(Y):", round(float(-np.sum(Py * np.log2(Py))), 4), "bits (all of it is explained by X)")covariance: 0.0
mutual information: 1.5219 bits
H(Y): 1.5219 bits (all of it is explained by X)
Estimating mutual information from samples
From paired samples, build a contingency table, normalize it and apply the formula. scikit-learn's mutual_info_score does this (in nats):
import numpy as np
from sklearn.metrics import mutual_info_score
rng = np.random.default_rng(1)
x = rng.integers(0, 4, 20_000)
y = np.where(rng.random(20_000) < 0.7, x, rng.integers(0, 4, 20_000)) # copy x 70% of the time
table = np.zeros((4, 4))
np.add.at(table, (x, y), 1)
P = table / table.sum()
Q = np.outer(P.sum(axis=1), P.sum(axis=0))
print("our estimate:", round(float(np.sum(P * np.log2(P / Q))), 4), "bits")
print("sklearn: ", round(mutual_info_score(x, y) / np.log(2), 4), "bits")our estimate: 0.8801 bits
sklearn: 0.8801 bits
In software
sklearn.metrics.mutual_info_score(labels_a, labels_b)returns nats; divide bynp.log(2)for bits.sklearn.feature_selection.mutual_info_classifestimates MI between continuous features and a class, using nearest neighbours (Mutual Information for Feature Selection).- Normalized mutual information (
normalized_mutual_info_score) compares clusterings.
Exercises
Exercise 1: Copy channel
is uniform on 8 values and . What is ?
Answer
3 bits.
Solution
.
Exercise 2: Independent coins
and are independent fair coins and (XOR). What are and ?
Answer
, but bit.
Solution
alone is a fair coin independent of . But once is known, determines exactly. Conditioning can increase mutual information.
Exercise 3: A noisy copy (code)
is a fair coin and equals with probability 0.9 and is flipped otherwise. Compute .
Answer
About 0.531 bits.
Solution
import numpy as np
P = 0.5 * np.array([[0.9, 0.1], [0.1, 0.9]])
Q = np.outer(P.sum(axis=1), P.sum(axis=0))
print(round(float(np.sum(P * np.log2(P / Q))), 4))0.531
This is , the capacity of the binary symmetric channel (Channel Capacity).
Test yourself
Mutual information is zero exactly when…
- X and Y are independent
- X and Y are uncorrelated
- X equals Y
- H(X) = H(Y)
Which identity is correct?
Mutual information is the KL divergence between…
- the joint and the product of the marginals
- the two marginals
- the joint and the uniform distribution
- the conditionals and