TExcelTAD
Home › Information Theory › Mutual Information

Mutual Information

How much does knowing XX tell you about YY? The mutual information I(X;Y)I(X; Y) answers in bits: it is the reduction in uncertainty about YY when XX 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

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

Equivalently, it is the KL divergence (Relative Entropy (KL Divergence)) between the joint distribution and the product of the marginals:

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)}
python · try it yourself
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: H(X)H(X) and H(Y)H(Y) are the circles, the overlap is I(X;Y)I(X; Y), the parts outside the overlap are H(X∣Y)H(X \mid Y) and H(Y∣X)H(Y \mid X), and the union is H(X,Y)H(X, Y).

Region Quantity
left circle only H(X∣Y)H(X \mid Y)
overlap I(X;Y)I(X; Y)
right circle only H(Y∣X)H(Y \mid X)
union H(X,Y)H(X, Y)
python · try it yourself
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: I(X;Y)=I(Y;X)I(X; Y) = I(Y; X).
  • Non-negativity: I(X;Y)≥0I(X; Y) \ge 0, with equality exactly for independent variables (from Gibbs' inequality).
  • Bounded: I(X;Y)≤min⁡(H(X),H(Y))I(X; Y) \le \min(H(X), H(Y)).
  • Self-information: I(X;X)=H(X)I(X; X) = H(X), which is why entropy is sometimes called self-information.

Mutual information versus correlation

Correlation only measures linear dependence. Mutual information catches any dependence. Here Y=X2Y = X^2 for symmetric XX: correlation is zero, but XX tells us a lot about YY:

python · try it yourself
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):

python · try it yourself
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 by np.log(2) for bits.
  • sklearn.feature_selection.mutual_info_classif estimates 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

XX is uniform on 8 values and Y=XY = X. What is I(X;Y)I(X; Y)?

Answer

3 bits.

Solution

I(X;X)=H(X)=log⁡28=3I(X; X) = H(X) = \log_2 8 = 3.

Exercise 2: Independent coins

XX and YY are independent fair coins and Z=X⊕YZ = X \oplus Y (XOR). What are I(X;Z)I(X; Z) and I(X;Z∣Y)I(X; Z \mid Y)?

Answer

I(X;Z)=0I(X; Z) = 0, but I(X;Z∣Y)=1I(X; Z \mid Y) = 1 bit.

Solution

ZZ alone is a fair coin independent of XX. But once YY is known, ZZ determines XX exactly. Conditioning can increase mutual information.

Exercise 3: A noisy copy (code)

XX is a fair coin and YY equals XX with probability 0.9 and is flipped otherwise. Compute I(X;Y)I(X; Y).

Answer

About 0.531 bits.

Solution
python · try it yourself
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 1−H2(0.1)1 - H_2(0.1), the capacity of the binary symmetric channel (Channel Capacity).

Test yourself

Mutual information is zero exactly when…

  1. X and Y are independent
  2. X and Y are uncorrelated
  3. X equals Y
  4. H(X) = H(Y)

Which identity is correct?

  1. I(X;Y)=H(X)+H(Y)−H(X,Y)I(X; Y) = H(X) + H(Y) - H(X, Y)
  2. I(X;Y)=H(X,Y)−H(X)I(X; Y) = H(X, Y) - H(X)
  3. I(X;Y)=H(X)H(Y)I(X; Y) = H(X)H(Y)
  4. I(X;Y)=H(X∣Y)+H(Y∣X)I(X; Y) = H(X \mid Y) + H(Y \mid X)

Mutual information is the KL divergence between…

  1. the joint and the product of the marginals
  2. the two marginals
  3. the joint and the uniform distribution
  4. the conditionals P(y∣x)P(y \mid x) and P(x∣y)P(x \mid y)