Type at least 2 letters. Enter opens the first result, Esc closes.

    Information Theory · Lesson 7 · Continuous variables and mutual information

    Mutual information and the data processing inequality

    Intermediate 75 min 5 interactive figures 3 videos Colab notebook

    Before this lesson: lesson 02 (joint and conditional entropy, the chain rule, the two-bar diagram), lesson 03 (KL divergence, Gibbs' inequality) and lesson 06 (differential entropy, the Gaussian, invariance of KL). Joint and conditional distributions from probability lesson 04 and the multivariate Gaussian from probability lesson 05. Time: about 75 minutes, plus the notebook.


    Why this matters #

    • Feature selection and decision trees. mutual_info_classif in scikit-learn ranks features by their mutual information with the label. The "information gain" of a decision-tree split is the mutual information between the split and the label.
    • Representation learning. Contrastive methods (CPC, SimCLR, CLIP) maximize a lower bound on the mutual information between two views of the data (lesson 08). The data processing inequality limits what any encoder can keep.
    • Bayesian experimental design. The expected information gain of an experiment is the mutual information between the unknown parameters and the measurement. Choose the sensor location or the simulation that maximizes it.
    • Simulation-based inference. A summary statistic s(y)s(y) of simulator output can only lose information about the parameters. It loses nothing exactly when it is sufficient.
    • Communication. The capacity of a noisy channel, the largest rate of reliable communication, is a maximum of mutual information.

    In this track log⁡\log is the natural logarithm and gives nats; log⁡2\log_2 gives bits. Tables in this lesson are in bits; Gaussian formulas are in nats. Hb(p)=−plog⁡2p−(1−p)log⁡2(1−p)H_b(p) = -p\log_2 p - (1 - p)\log_2(1 - p) is the binary entropy in bits (lesson 01).

    1. Predict first #

    2. The picture #

    2.1 Two bits and a noisy channel #

    Let XX be a fair bit. Three cases:

    • Copy, Y=XY = X. Learning YY tells you XX exactly: 1 bit of information.
    • Independent, YY is a separate fair coin. Learning YY tells you nothing: 0 bits.
    • Noisy copy. YY equals XX with probability 1−ε1 - \varepsilon and 1−X1 - X with probability ε\varepsilon. This is the binary symmetric channel BSC(ε\varepsilon).

    For the noisy copy, count the uncertainty about YY in two steps.

    1. YY is a fair bit too (each value is reached with probability 12\frac12), so H(Y)=1H(Y) = 1 bit.
    2. If you know XX, the only uncertainty left is whether the channel flipped the bit: H(Y∣X)=Hb(ε)H(Y \mid X) = H_b(\varepsilon).
    3. The information is the uncertainty that knowing XX removes: I(X;Y)=H(Y)−H(Y∣X)=1−Hb(ε)I(X;Y) = H(Y) - H(Y \mid X) = 1 - H_b(\varepsilon).

    For ε=0.2\varepsilon = 0.2: Hb(0.2)=0.722H_b(0.2) = 0.722, so I=1−0.722=0.278I = 1 - 0.722 = 0.278 bits. A channel that is right 80% of the time carries much less than 0.8 bits per use.

    Interactive figure: loading…

    Move the flip probability ε\varepsilon. Left: the channel; the arrow widths are the transition probabilities. Middle: the bar H(X)H(X) is split into H(X∣Y)H(X \mid Y) and the shared part I(X;Y)I(X;Y); the bar H(Y)H(Y) is split into II and H(Y∣X)=Hb(ε)H(Y \mid X) = H_b(\varepsilon). Right: the violet curve 1−Hb(ε)1 - H_b(\varepsilon) and the current point. Notice that ε=0.9\varepsilon = 0.9 is as good as ε=0.1\varepsilon = 0.1. Then move the input bias a=P(X=1)a = P(X = 1): the information drops below the violet curve.

    Quick check

    XX is a fair bit and YY is XX sent through BSC(0.9): the bit is flipped 90% of the time. What is I(X;Y)I(X;Y)?

    2.2 How far the joint is from independence #

    The two-bar picture in the widget is the picture of lesson 02: the mutual information is the overlap of H(X)H(X) and H(Y)H(Y). A second picture compares two tables:

    • the real joint table p(x,y)p(x, y);
    • the table you would get if XX and YY were independent but had the same marginals: p(x) p(y)p(x)\,p(y).

    For each cell, the pointwise mutual information is

    pmi⁡(x,y)=log⁡p(x,y)p(x) p(y)=log⁡p(y∣x)p(y).\operatorname{pmi}(x, y) = \log\frac{p(x, y)}{p(x)\,p(y)} = \log\frac{p(y \mid x)}{p(y)} .

    It is positive when the pair (x,y)(x, y) occurs more often than under independence, and negative when it occurs less often. The mutual information is the average pmi under the joint:

    I(X;Y)=∑x,yp(x,y)log⁡p(x,y)p(x) p(y)=KL(p(x,y) ∥ p(x) p(y)).I(X;Y) = \sum_{x, y} p(x, y)\log\frac{p(x, y)}{p(x)\,p(y)} = \mathrm{KL}\big(p(x, y)\,\|\,p(x)\,p(y)\big).

    Some pmi values can be negative, but the average is never negative: it is a KL divergence (lesson 03).

    Interactive figure: loading…

    Edit the weights of the joint table, or try the presets. Left: the joint p(x,y)p(x, y) (blue). Middle: the product of its marginals p(x) p(y)p(x)\,p(y) (orange), the "independent version". Right: each cell's contribution p(x,y)pmi⁡(x,y)p(x, y)\operatorname{pmi}(x, y) in bits, violet when positive and grey when negative. Their sum is I(X;Y)I(X;Y). Make all rows proportional and watch II become 0. Try "Y=X2Y = X^2": the correlation of this pair is 0, but II is not.

    Quick check

    In a joint table, two cells have a negative pointwise mutual information. What can you say about I(X;Y)I(X;Y)?

    3. Definitions and main results #

    3.1 Mutual information #

    For discrete random variables X,YX, Y with joint distribution p(x,y)p(x, y) and marginals p(x)p(x), p(y)p(y), the mutual information is

    I(X;Y)=∑x,yp(x,y)log⁡p(x,y)p(x) p(y)=KL(p(x,y) ∥ p(x) p(y)).I(X;Y) = \sum_{x, y} p(x, y)\log\frac{p(x, y)}{p(x)\,p(y)} = \mathrm{KL}\big(p(x, y)\,\|\,p(x)\,p(y)\big).

    For densities, replace the sums by integrals. We write a semicolon, I(X;Y)I(X;Y), because the two arguments are two groups of variables: I(X;Y,Z)I(X; Y, Z) is the information between XX and the pair (Y,Z)(Y, Z).

    Theorem 1 (identities and bounds).

    • (a) 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).
    • (b) I(X;Y)≥0I(X;Y) \ge 0, with equality if and only if XX and YY are independent.
    • (c) I(X;Y)=I(Y;X)I(X;Y) = I(Y;X) and I(X;X)=H(X)I(X;X) = H(X). For discrete variables, I(X;Y)≤min⁡(H(X),H(Y))I(X;Y) \le \min\big(H(X), H(Y)\big).

    Proof.

    1. (a) Write the pmi as log⁡p(x∣y)p(x)=−log⁡p(x)−(−log⁡p(x∣y))\log\frac{p(x \mid y)}{p(x)} = -\log p(x) - \big(-\log p(x \mid y)\big). Take the mean under p(x,y)p(x, y): the first part gives H(X)H(X), the second H(X∣Y)H(X \mid Y). So I=H(X)−H(X∣Y)I = H(X) - H(X \mid Y). Exchange the roles of XX and YY for the second form. The chain rule H(X,Y)=H(Y)+H(X∣Y)H(X, Y) = H(Y) + H(X \mid Y) (lesson 02) gives the third.
    2. (b) II is a KL divergence, so Gibbs' inequality gives I≥0I \ge 0, with equality only if p(x,y)=p(x)p(y)p(x, y) = p(x)p(y) for every x,yx, y: independence.
    3. (c) Symmetry is clear from the KL form. With Y=XY = X, H(X∣X)=0H(X \mid X) = 0, so I(X;X)=H(X)I(X;X) = H(X). Discrete conditional entropies are ≥0\ge 0, so I=H(X)−H(X∣Y)≤H(X)I = H(X) - H(X \mid Y) \le H(X), and in the same way I≤H(Y)I \le H(Y). ■\blacksquare

    For continuous variables, (a) and (b) still hold, with differential entropies. The bound (c) fails: differential entropies can be negative, and I(X;X)=∞I(X;X) = \infty for a continuous XX (a real number carries infinitely many bits about itself).

    3.2 Conditional mutual information and the chain rule #

    The conditional mutual information is the mutual information inside each slice Z=zZ = z, averaged over zz:

    I(X;Y∣Z)=H(X∣Z)−H(X∣Y,Z)=Ez[KL(p(x,y∣z) ∥ p(x∣z) p(y∣z))]≥0.I(X;Y \mid Z) = H(X \mid Z) - H(X \mid Y, Z) = \mathbb{E}_z\Big[\mathrm{KL}\big(p(x, y \mid z)\,\|\,p(x \mid z)\,p(y \mid z)\big)\Big] \ge 0 .

    It is 0 if and only if XX and YY are independent given ZZ. Add and subtract H(X∣Z)H(X \mid Z) to get the chain rule for mutual information:

    I(X;Y,Z)=H(X)−H(X∣Y,Z)=H(X)−H(X∣Z)⏟I(X;Z)+H(X∣Z)−H(X∣Y,Z)⏟I(X;Y∣Z).I(X; Y, Z) = H(X) - H(X \mid Y, Z) = \underbrace{H(X) - H(X \mid Z)}_{I(X;Z)} + \underbrace{H(X \mid Z) - H(X \mid Y, Z)}_{I(X;Y \mid Z)} .

    Conditioning can increase mutual information. Let XX and YY be independent fair bits and Z=X⊕YZ = X \oplus Y (XOR). Then I(X;Y)=0I(X;Y) = 0. But once you know ZZ, knowing XX tells you YY exactly: I(X;Y∣Z)=1I(X;Y \mid Z) = 1 bit. This is why the three-circle Venn picture fails for three variables (lesson 02).

    3.3 Mutual information of Gaussians #

    Theorem 2 (Gaussian mutual information). Let (X,Y)(X, Y) be jointly Gaussian with positive definite covariance Σ=(ΣXXΣXYΣYXΣYY)\Sigma = \begin{pmatrix} \Sigma_{XX} & \Sigma_{XY} \\ \Sigma_{YX} & \Sigma_{YY} \end{pmatrix}. Then

    I(X;Y)=12log⁡det⁡ΣXX det⁡ΣYYdet⁡Σ.I(X;Y) = \frac12\log\frac{\det\Sigma_{XX}\,\det\Sigma_{YY}}{\det\Sigma} .

    For two scalars with correlation ρ\rho: I(X;Y)=−12log⁡(1−ρ2)I(X;Y) = -\frac12\log(1 - \rho^2).

    Proof.

    1. Use I=h(X)+h(Y)−h(X,Y)I = h(X) + h(Y) - h(X, Y) (Theorem 1a with differential entropies).
    2. A kk-dimensional Gaussian has h=12log⁡((2πe)kdet⁡Σ)h = \frac12\log((2\pi e)^k\det\Sigma) (lesson 06). The factors (2πe)dX(2πe)dY/(2πe)dX+dY=1(2\pi e)^{d_X}(2\pi e)^{d_Y}/(2\pi e)^{d_X + d_Y} = 1 cancel, and the log-dets give the formula.
    3. Scalars: det⁡Σ=σX2σY2−ρ2σX2σY2=σX2σY2(1−ρ2)\det\Sigma = \sigma_X^2\sigma_Y^2 - \rho^2\sigma_X^2\sigma_Y^2 = \sigma_X^2\sigma_Y^2(1 - \rho^2), so I=12log⁡σX2σY2σX2σY2(1−ρ2)=−12log⁡(1−ρ2)I = \frac12\log\frac{\sigma_X^2\sigma_Y^2}{\sigma_X^2\sigma_Y^2(1 - \rho^2)} = -\frac12\log(1 - \rho^2). ■\blacksquare

    A check with the other form. Given X=xX = x, YY is Gaussian with variance σY2(1−ρ2)\sigma_Y^2(1 - \rho^2), for every xx. So I=h(Y)−h(Y∣X)=12log⁡σY2σY2(1−ρ2)I = h(Y) - h(Y \mid X) = \frac12\log\frac{\sigma_Y^2}{\sigma_Y^2(1 - \rho^2)} ✓. The mutual information depends only on ρ\rho, not on the scales. At ρ=0.9\rho = 0.9 it is 0.8300.830 nats (1.1981.198 bits); as ρ→±1\rho \to \pm 1 it goes to infinity.

    Mutual information sees nonlinear dependence. Correlation measures only linear dependence. Let XX be uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2. Then Cov⁡(X,Y)=E[X3]−E[X] E[X2]=0−0=0\operatorname{Cov}(X, Y) = \mathbb{E}[X^3] - \mathbb{E}[X]\,\mathbb{E}[X^2] = 0 - 0 = 0: the correlation is 0. But YY is a function of XX, so H(Y∣X)=0H(Y \mid X) = 0 and I(X;Y)=H(Y)=H(13,23)=0.918I(X;Y) = H(Y) = H(\frac13, \frac23) = 0.918 bits. The formula −12log⁡(1−ρ2)-\frac12\log(1 - \rho^2) is the mutual information only for jointly Gaussian variables.

    3.4 Invariance under invertible maps #

    Theorem 3. Let ff and gg be invertible maps (for densities: smooth, with smooth inverses). Then I(f(X);g(Y))=I(X;Y)I(f(X); g(Y)) = I(X;Y).

    Proof (densities).

    1. Let U=f(X)U = f(X) and V=g(Y)V = g(Y). By the change of variables, at x=f−1(u)x = f^{-1}(u) and y=g−1(v)y = g^{-1}(v): pUV(u,v)=pXY(x,y)∣det⁡Jf(x)∣ ∣det⁡Jg(y)∣p_{UV}(u, v) = \frac{p_{XY}(x, y)}{|\det J_f(x)|\,|\det J_g(y)|}, pU(u)=pX(x)∣det⁡Jf(x)∣p_U(u) = \frac{p_X(x)}{|\det J_f(x)|} and pV(v)=pY(y)∣det⁡Jg(y)∣p_V(v) = \frac{p_Y(y)}{|\det J_g(y)|}.
    2. In the ratio pUVpU pV\frac{p_{UV}}{p_U\,p_V} the Jacobian factors cancel, so pmi⁡UV(u,v)=pmi⁡XY(x,y)\operatorname{pmi}_{UV}(u, v) = \operatorname{pmi}_{XY}(x, y) at every point.
    3. Take the mean: I(U;V)=I(X;Y)I(U;V) = I(X;Y). ■\blacksquare

    For discrete variables an invertible map is a relabelling, which changes nothing. In contrast, the differential entropy changes, h(f(X))=h(X)+E[log⁡∣det⁡Jf(X)∣]h(f(X)) = h(X) + \mathbb{E}[\log|\det J_f(X)|] (lesson 06), and the correlation changes under nonlinear maps. Mutual information describes only the dependence, not the coordinates. This makes it the right quantity for continuous data with arbitrary units.

    Interactive figure: loading…

    Apply x↦x3x \mapsto x^3 or y↦eyy \mapsto e^y to the samples. The true mutual information −12log⁡(1−ρ2)-\frac12\log(1 - \rho^2) does not change: the maps are invertible. The sample correlation rr drops, and the Gaussian formula with rr (orange) gives a wrong value, because the transformed data are not jointly Gaussian. The entropy of the horizontal coordinate changes too. Move ρ\rho to see how II grows as ∣ρ∣→1|\rho| \to 1.

    Quick check

    Two variables have correlation exactly 0. What can you conclude about their mutual information?

    3.5 The data processing inequality #

    X→Y→ZX \to Y \to Z is a Markov chain if p(x,y,z)=p(x) p(y∣x) p(z∣y)p(x, y, z) = p(x)\,p(y \mid x)\,p(z \mid y): ZZ is produced from YY alone, without looking at XX. Equivalently, XX and ZZ are independent given YY: I(X;Z∣Y)=0I(X;Z \mid Y) = 0. Every function Z=g(Y)Z = g(Y), and every random processing of YY with its own independent noise, gives a Markov chain.

    Theorem 4 (data processing inequality, DPI). If X→Y→ZX \to Y \to Z is a Markov chain, then

    I(X;Z)≤I(X;Y).I(X;Z) \le I(X;Y).

    Equality holds if and only if I(X;Y∣Z)=0I(X;Y \mid Z) = 0: once ZZ is known, YY tells nothing more about XX.

    Proof.

    1. Markov property: I(X;Z∣Y)=0I(X;Z \mid Y) = 0.
    2. Apply the chain rule for I(X;Y,Z)I(X; Y, Z) in two orders: I(X;Y,Z)=I(X;Z)+I(X;Y∣Z)=I(X;Y)+I(X;Z∣Y)I(X; Y, Z) = I(X;Z) + I(X;Y \mid Z) = I(X;Y) + I(X;Z \mid Y).
    3. With step 1, I(X;Y)=I(X;Z)+I(X;Y∣Z)I(X;Y) = I(X;Z) + I(X;Y \mid Z).
    4. Conditional mutual information is ≥0\ge 0, so I(X;Y)≥I(X;Z)I(X;Y) \ge I(X;Z), with equality exactly when I(X;Y∣Z)=0I(X;Y \mid Z) = 0. ■\blacksquare

    Two channels in a row. XX is a fair bit, YY = BSC(0.2) of XX, and ZZ = BSC(0.2) of YY. ZZ differs from XX when exactly one of the two channels flips: probability 0.2⋅0.8+0.8⋅0.2=0.320.2 \cdot 0.8 + 0.8 \cdot 0.2 = 0.32. So ZZ is BSC(0.32) of XX, and I(X;Z)=1−Hb(0.32)=1−0.904=0.096I(X;Z) = 1 - H_b(0.32) = 1 - 0.904 = 0.096 bits, much less than I(X;Y)=0.278I(X;Y) = 0.278 bits.

    Interactive figure: loading…

    Choose how ZZ is made from YY. The bars are I(X;Y)I(X;Y), I(X;Z)I(X;Z) and I(Y;Z)I(Y;Z) (grey); the aqua segment is the information about XX that the processing lost. Try every option and both sliders: the yellow bar is never longer than the violet one. The copy and the relabelling Z=1−YZ = 1 - Y (both invertible) lose nothing; a constant loses everything.

    Corollaries.

    • I(X;g(Y))≤I(X;Y)I(X; g(Y)) \le I(X;Y) for every function gg: computing a feature cannot add information.
    • A Markov chain also runs backwards, Z→Y→XZ \to Y \to X, so the DPI also gives I(X;Z)≤I(Y;Z)I(X;Z) \le I(Y;Z).
    • A second proof of Theorem 3: for an invertible ff, both Y→X→f(X)Y \to X \to f(X) and Y→f(X)→XY \to f(X) \to X are Markov chains, so the DPI gives ≤\le in both directions.

    Sufficient statistics. Let θ\theta be a parameter with a prior, YY the data and T=T(Y)T = T(Y) a statistic. θ→Y→T\theta \to Y \to T is a Markov chain, so I(θ;T)≤I(θ;Y)I(\theta; T) \le I(\theta; Y). The statistic TT is sufficient for θ\theta when p(y∣t,θ)p(y \mid t, \theta) does not depend on θ\theta (Fisher's definition). Then θ→T→Y\theta \to T \to Y is also a Markov chain, and I(θ;T)=I(θ;Y)I(\theta; T) = I(\theta; Y) for every prior. Example: nn coin flips with bias θ\theta, and TT = the number of heads. Given T=tT = t, every sequence with tt heads has probability 1/(nt)1/\binom{n}{t}, whatever θ\theta is. So the count keeps all the information about θ\theta, and the order of the flips adds nothing.

    Quick check

    X→Y→ZX \to Y \to Z with I(X;Y)=0.5I(X;Y) = 0.5 bits, and Z=g(Y)Z = g(Y) for a cleverly designed function gg. Which value of I(X;Z)I(X;Z) is possible?

    The DPI for KL divergence, and why deterministic encoders break I(X;Z)I(X;Z)

    DPI for KL. Send two input distributions PP and QQ through the same channel K(y∣x)K(y \mid x), giving output distributions PKPK and QKQK. Then KL(PK ∥ QK)≤KL(P ∥ Q)\mathrm{KL}(PK\,\|\,QK) \le \mathrm{KL}(P\,\|\,Q).

    1. Form the joints PXY(x,y)=P(x)K(y∣x)P_{XY}(x, y) = P(x)K(y \mid x) and QXY(x,y)=Q(x)K(y∣x)Q_{XY}(x, y) = Q(x)K(y \mid x). In their log-ratio the channel cancels, so KL(PXY ∥ QXY)=KL(P ∥ Q)\mathrm{KL}(P_{XY}\,\|\,Q_{XY}) = \mathrm{KL}(P\,\|\,Q).
    2. Factor the same joints through the output: KL(PXY ∥ QXY)=KL(PK ∥ QK)+Ey∼PK[KL(PX∣y ∥ QX∣y)]≥KL(PK ∥ QK)\mathrm{KL}(P_{XY}\,\|\,Q_{XY}) = \mathrm{KL}(PK\,\|\,QK) + \mathbb{E}_{y \sim PK}\big[\mathrm{KL}(P_{X \mid y}\,\|\,Q_{X \mid y})\big] \ge \mathrm{KL}(PK\,\|\,QK). ■\blacksquare

    The DPI for mutual information is the special case P=p(x,y)P = p(x, y) and Q=p(x)p(y)Q = p(x)p(y) with the channel acting on yy. The same holds for every f-divergence (lesson 08).

    Deterministic encoders. Let XX be continuous and Z=f(X)Z = f(X) with ff injective and smooth. Then I(X;Z)=∞I(X;Z) = \infty: knowing XX gives ZZ exactly, and a real number carries infinitely many bits. So "the encoder compresses the input", measured by I(X;Z)I(X;Z), means nothing for a deterministic network on continuous inputs. It needs noise in the encoder (Z=f(X)+σεZ = f(X) + \sigma\varepsilon) or a discrete ZZ. This point matters in the information-bottleneck debate about deep networks (lesson 10).

    3.6 Estimating mutual information from samples #

    In practice we have NN samples, not the joint distribution. The simplest estimator is the plug-in estimator: count the pairs, form the empirical table p^(x,y)\hat p(x, y), and compute I^=I(p^)\hat I = I(\hat p). For independent or weakly dependent variables it is biased upward:

    1. I^≥0\hat I \ge 0 always. If the true value is 0, every error is upward: a finite sample always shows some spurious dependence.
    2. More precisely, each plug-in entropy is biased down, E[H^]≈H−K−12N\mathbb{E}[\hat H] \approx H - \frac{K - 1}{2N} nats for KK cells with positive probability and N≫KN \gg K (the Miller–Madow correction). In I^=H^(X)+H^(Y)−H^(X,Y)\hat I = \hat H(X) + \hat H(Y) - \hat H(X, Y) the joint entropy has the most cells, so it has the most bias. For independent X,YX, Y with KXK_X and KYK_Y values, the bias of I^\hat I is about
    E[I^]≈KXKY−KX−KY+12N=(KX−1)(KY−1)2N nats.\mathbb{E}[\hat I] \approx \frac{K_X K_Y - K_X - K_Y + 1}{2N} = \frac{(K_X - 1)(K_Y - 1)}{2N} \text{ nats}.

    The bias is not always upward. For Y=XY = X, the joint table has only KXK_X positive cells, so I^=H^(X)\hat I = \hat H(X), which is biased down by KX−12N\frac{K_X - 1}{2N}.

    Test before you trust. A small positive I^\hat I is not evidence of dependence. Compare it with a permutation baseline: shuffle YY many times (this destroys any dependence but keeps both marginals), recompute I^\hat I each time, and see where the real value falls. 2NI^2N\hat I is the G-test statistic of classical statistics; under independence it is approximately χ2\chi^2 with (KX−1)(KY−1)(K_X - 1)(K_Y - 1) degrees of freedom, whose mean gives the bias formula above.

    Interactive figure: loading…

    Change KK and NN, then switch to "weak dependence". The magenta histogram shows 400 plug-in estimates; its mean is the magenta line. The blue line is the true II, and the aqua dashed line is the true II plus the predicted bias (K−1)2/(2N)(K - 1)^2/(2N). The yellow line is one data set, with its permutation p-value. Notice: with K=10K = 10 and N=100N = 100 the bias is large; with large NN the estimates concentrate at the truth.

    For continuous data you must also choose bins, and the answer depends on them. The kk-nearest-neighbour (KSG) estimator of Kraskov, Stögbauer and Grassberger avoids bins and is used by mutual_info_regression and mutual_info_classif in scikit-learn; it is still biased in high dimensions. For high-dimensional data such as images, tables and neighbours fail, and we need the variational bounds of lesson 08.

    Quick check

    XX and YY are independent, each with 10 values, and you have N=1000N = 1000 samples. What plug-in estimate do you expect on average?

    3.7 Channel capacity, in one paragraph #

    A channel is a conditional distribution p(y∣x)p(y \mid x). Its capacity is C=max⁡p(x)I(X;Y)C = \max_{p(x)} I(X;Y), the largest mutual information over all input distributions. Shannon's channel coding theorem says that reliable communication is possible at every rate below CC bits per channel use, and impossible above it. For the BSC, I=Hb(a(1−ε)+(1−a)ε)−Hb(ε)I = H_b\big(a(1 - \varepsilon) + (1 - a)\varepsilon\big) - H_b(\varepsilon) for input P(X=1)=aP(X = 1) = a; it is largest at a=12a = \frac12, so C=1−Hb(ε)C = 1 - H_b(\varepsilon). For ε=0.1\varepsilon = 0.1, C=0.531C = 0.531 bits per use (MacKay, chapters 9–10).

    Answers to section 1. (1) 1−Hb(0.2)=0.2781 - H_b(0.2) = 0.278 bits: much less than 0.8 bits. (2) No. X→Y→ZX \to Y \to Z and then any processing of ZZ is still a Markov chain from XX, so the DPI gives I(X;result)≤I(X;Z)≤I(X;Y)I(X; \text{result}) \le I(X;Z) \le I(X;Y). For two BSC(0.2) in a row, I(X;Z)=0.096I(X;Z) = 0.096 bits. (3) The correlation is 0, but I(X;Y)=H(Y)=0.918I(X;Y) = H(Y) = 0.918 bits: YY is a function of XX.

    4. Worked example #

    (a) A forecast table, three ways. XX: heavy rain observed (1) or not (0). YY: heavy rain forecast (1) or not (0).

    p(x,y)p(x, y) Y=0Y = 0 Y=1Y = 1 p(x)p(x)
    X=0X = 0 0.70 0.10 0.80
    X=1X = 1 0.05 0.15 0.20
    p(y)p(y) 0.75 0.25

    In bits:

    • From XX. H(X)=Hb(0.2)=0.722H(X) = H_b(0.2) = 0.722. The columns give p(x∣y)p(x \mid y): H(X∣Y=0)=Hb(0.050.75)=Hb(0.0667)=0.353H(X \mid Y = 0) = H_b(\frac{0.05}{0.75}) = H_b(0.0667) = 0.353 and H(X∣Y=1)=Hb(0.150.25)=Hb(0.6)=0.971H(X \mid Y = 1) = H_b(\frac{0.15}{0.25}) = H_b(0.6) = 0.971. So H(X∣Y)=0.75⋅0.353+0.25⋅0.971=0.508H(X \mid Y) = 0.75 \cdot 0.353 + 0.25 \cdot 0.971 = 0.508 and I=0.722−0.508=0.214I = 0.722 - 0.508 = 0.214.
    • From YY. H(Y)=Hb(0.25)=0.811H(Y) = H_b(0.25) = 0.811. The rows give H(Y∣X)=0.8 Hb(0.100.80)+0.2 Hb(0.150.20)=0.8⋅0.544+0.2⋅0.811=0.597H(Y \mid X) = 0.8\,H_b(\frac{0.10}{0.80}) + 0.2\,H_b(\frac{0.15}{0.20}) = 0.8 \cdot 0.544 + 0.2 \cdot 0.811 = 0.597, and I=0.811−0.597=0.214I = 0.811 - 0.597 = 0.214 ✓.
    • KL form. The pmi values log⁡2p(x,y)p(x)p(y)\log_2\frac{p(x, y)}{p(x)p(y)} are log⁡20.700.60=0.222\log_2\frac{0.70}{0.60} = 0.222, log⁡20.100.20=−1\log_2\frac{0.10}{0.20} = -1, log⁡20.050.15=−1.585\log_2\frac{0.05}{0.15} = -1.585 and log⁡20.150.05=1.585\log_2\frac{0.15}{0.05} = 1.585. Weighted by p(x,y)p(x, y): 0.1557−0.1000−0.0792+0.2377=0.21420.1557 - 0.1000 - 0.0792 + 0.2377 = 0.2142 ✓. Two pmi values are negative; the average is not.

    A forecast of rain makes you less sure about the outcome for that one value (H(X∣Y=1)=0.971>H(X)=0.722H(X \mid Y = 1) = 0.971 > H(X) = 0.722), but on average the forecast helps.

    (b) Gaussians. For ρ=0.8\rho = 0.8: I=−12log⁡(1−0.64)=−12log⁡0.36=0.511I = -\frac12\log(1 - 0.64) = -\frac12\log 0.36 = 0.511 nats =0.737= 0.737 bits. For ρ=0.9\rho = 0.9: −12log⁡0.19=0.830-\frac12\log 0.19 = 0.830 nats. Five independent pairs with ρ=0.8\rho = 0.8 (so X,Y∈R5X, Y \in \mathbb{R}^5) have I=5⋅0.511=2.554I = 5 \cdot 0.511 = 2.554 nats: mutual information adds over independent pairs, because the covariance is block diagonal and the determinants multiply.

    (c) Two channels in a row. XX fair, YY = BSC(0.2) of XX, ZZ = BSC(0.2) of YY:

    • I(X;Y)=1−Hb(0.2)=1−0.722=0.278I(X;Y) = 1 - H_b(0.2) = 1 - 0.722 = 0.278 bits;
    • ZZ = BSC(0.32) of XX, so I(X;Z)=1−Hb(0.32)=1−0.904=0.096I(X;Z) = 1 - H_b(0.32) = 1 - 0.904 = 0.096 bits ≤I(X;Y)\le I(X;Y) ✓;
    • I(Y;Z)=0.278I(Y;Z) = 0.278 bits, so I(X;Z)≤I(Y;Z)I(X;Z) \le I(Y;Z) holds too.

    (d) A sufficient statistic. θ\theta is 0.20.2 or 0.80.8 with probability 12\frac12 each, and YY is three flips of a coin with bias θ\theta. Exact computation over the 2×82 \times 8 joint table (notebook): I(θ;Y)=0.594I(\theta; Y) = 0.594 bits; with TT = the number of heads, I(θ;T)=0.594I(\theta; T) = 0.594 bits (nothing lost); with only the first flip, I(θ;Y1)=1−Hb(0.2)=0.278I(\theta; Y_1) = 1 - H_b(0.2) = 0.278 bits.

    (e) Plug-in bias. Independent X,YX, Y with 4 values each and N=500N = 500: E[I^]≈3⋅3/(2⋅500)=0.009\mathbb{E}[\hat I] \approx 3 \cdot 3/(2 \cdot 500) = 0.009 nats, although the true value is 0.

    5. In code #

    SciPy computes entropies and KL divergences; scikit-learn has the plug-in estimator (mutual_info_score, in nats) and the kNN estimator (mutual_info_regression).

    import numpy as np
    from scipy.stats import entropy, multivariate_normal
    from sklearn.metrics import mutual_info_score
    from sklearn.feature_selection import mutual_info_regression
    
    # (a) MI of the forecast table, three ways (bits)
    pxy = np.array([[0.70, 0.10],
                    [0.05, 0.15]])
    px, py = pxy.sum(axis=1), pxy.sum(axis=0)
    H = lambda t: entropy(np.ravel(t), base=2)
    print(H(px) + H(py) - H(pxy))                                            # 0.2142
    print(entropy(pxy.ravel(), np.outer(px, py).ravel(), base=2))           # 0.2142, the KL form
    print(mutual_info_score(None, None, contingency=100 * pxy) / np.log(2))  # 0.2142 (sklearn wants counts, gives nats)
    
    # (b) Gaussian MI from entropies: h(X) + h(Y) - h(X, Y)
    C = np.array([[1.0, 0.9], [0.9, 1.0]])
    h = lambda S: multivariate_normal(cov=S).entropy()
    print(h(C[:1, :1]) + h(C[1:, 1:]) - h(C), -0.5 * np.log(1 - 0.9**2))    # 0.8304 0.8304
    
    # (c) DPI: a joint table times a channel matrix p(z | y) gives p(x, z)
    K = np.array([[0.8, 0.2], [0.2, 0.8]])        # BSC(0.2); each row sums to 1
    pxy2 = np.diag([0.5, 0.5]) @ K                 # X fair, Y = BSC(X)
    pxz = pxy2 @ K                                 # Z = BSC(Y)
    for t in (pxy2, pxz):
        print(entropy(t.ravel(), np.outer(t.sum(1), t.sum(0)).ravel(), base=2))   # 0.2781, then 0.0956
    
    # (d) Plug-in bias for independent X, Y with 4 values each and N = 500 (nats)
    rng = np.random.default_rng(0)
    est = [mutual_info_score(rng.integers(0, 4, 500), rng.integers(0, 4, 500)) for _ in range(2000)]
    print(np.mean(est), 3 * 3 / (2 * 500))         # 0.0089 0.009
    
    # (e) Invariance: x -> x^3 and y -> exp(y) change the correlation, not the MI
    x, y = rng.multivariate_normal([0, 0], C, size=5000).T
    print(np.corrcoef(x, y)[0, 1], np.corrcoef(x**3, np.exp(y))[0, 1])     # 0.903 0.668
    ksg = lambda a, b: mutual_info_regression(a[:, None], b, random_state=0)[0]   # kNN (KSG) estimator, nats
    print(ksg(x, y), ksg(x**3, np.exp(y)))        # 0.860 0.823: both near the true 0.830
    

    The kNN estimate is not exactly invariant (distances change under the maps); the true mutual information is. mutual_info_score(labels_x, labels_y) builds the table of counts for you; pass contingency= when you already have one.

    6. Where it shows up #

    • Feature selection. SelectKBest(mutual_info_classif) keeps the features with the largest estimated mutual information with the label, nonlinear dependence included. Compare the scores with a permutation baseline; small scores of independent features are not zero.
    • Decision trees. The information gain of a split SS is H(Y)−H(Y∣S)=I(Y;S)H(Y) - H(Y \mid S) = I(Y;S), the mutual information between the split and the label (the decision-tree lesson of the ML fundamentals track, in preparation).
    • Representations and the DPI. For a label YY, an input XX and a representation Z=enc⁡(X)Z = \operatorname{enc}(X), the chain Y→X→ZY \to X \to Z is Markov, so I(Y;Z)≤I(Y;X)I(Y;Z) \le I(Y;X): an encoder can keep label information or lose it, never create it. It can make the information easier to use for a simple classifier: the DPI is about information, not usable information. Keeping I(Y;Z)I(Y;Z) while dropping the rest of XX is the information bottleneck (lesson 10).
    • Contrastive learning. InfoNCE (CPC, SimCLR, CLIP) is a lower bound on the mutual information between two views; lesson 08 derives it.
    • Bayesian experimental design. The expected information gain of a design dd is I(θ;Y∣d)I(\theta; Y \mid d). For a linear-Gaussian model it is 12log⁡det⁡Σpriordet⁡Σpost\frac12\log\frac{\det\Sigma_{\text{prior}}}{\det\Sigma_{\text{post}}}, the Gaussian mutual information of section 3.3.
    • Simulation-based inference. Neural summary statistics are often trained to maximize a bound on I(θ;s(Y))I(\theta; s(Y)). The DPI caps them at I(θ;Y)I(\theta; Y), and sufficiency is the equality case.
    • Dependence between scientific fields. The mutual information between a ground-motion amplitude and a site parameter is the same for the amplitude and its logarithm, unlike the correlation. A histogram estimate between two climate indices needs a permutation baseline.

    7. Common mistakes #

    • "Correlation 0 means independent." For XX uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2, the correlation is 0 but I=0.918I = 0.918 bits. Only for jointly Gaussian variables does ρ=0\rho = 0 imply independence.
    • Using −12log⁡(1−ρ2)-\frac12\log(1 - \rho^2) on non-Gaussian data. It is the mutual information only for jointly Gaussian variables; on other data it uses only the linear part of the dependence.
    • Applying the DPI without a Markov chain. ZZ must depend on XX only through YY. And conditioning on a third variable can increase mutual information (the XOR example).
    • Treating mutual information like entropy for continuous data. hh changes with the units; mutual information does not. Continuous mutual information can be infinite (I(X;X)=∞I(X;X) = \infty); discrete mutual information is at most min⁡(H(X),H(Y))\min(H(X), H(Y)).
    • Trusting a small plug-in estimate. For independent variables it is about (KX−1)(KY−1)/(2N)(K_X - 1)(K_Y - 1)/(2N) nats, not 0. Use a permutation test.
    • Confusing information with usable information. The DPI does not say that preprocessing is useless. It says it cannot add information; it can still make the information easier to extract.

    8. Check yourself #

    1. XX is uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2. Compute Corr⁡(X,Y)\operatorname{Corr}(X, Y) and I(X;Y)I(X;Y) in bits.
      AnswerCov⁡(X,X2)=E[X3]−E[X] E[X2]=0\operatorname{Cov}(X, X^2) = \mathbb{E}[X^3] - \mathbb{E}[X]\,\mathbb{E}[X^2] = 0, so the correlation is 0. YY is a function of XX, so H(Y∣X)=0H(Y \mid X) = 0 and I=H(Y)=H(13,23)=0.918I = H(Y) = H(\frac13, \frac23) = 0.918 bits.
    2. (X,Y)(X, Y) is jointly Gaussian with ρ=0.6\rho = 0.6. Give I(X;Y)I(X;Y). What happens to II and to h(X)h(X) if you replace XX by eXe^X?
      AnswerI=−12log⁡0.64=0.223I = -\frac12\log 0.64 = 0.223 nats. II does not change (exe^x is invertible). h(eX)=h(X)+E[X]h(e^X) = h(X) + \mathbb{E}[X], because the log-Jacobian of exe^x is xx.
    3. X→Y→ZX \to Y \to Z is a Markov chain. Prove I(X;Z)≤I(Y;Z)I(X;Z) \le I(Y;Z).
      AnswerThe chain also runs backwards, Z→Y→XZ \to Y \to X (XX and ZZ are independent given YY, which is symmetric). The DPI for this chain gives I(Z;X)≤I(Z;Y)I(Z;X) \le I(Z;Y), and mutual information is symmetric.
    4. XX and YY are independent with 10 values each, and N=1000N = 1000. You measure I^=0.045\hat I = 0.045 nats. Is that evidence of dependence?
      AnswerNo. The expected value under independence is 9⋅9/2000=0.04059 \cdot 9/2000 = 0.0405 nats. 2NI^=902N\hat I = 90, against a χ812\chi^2_{81} null with mean 81 and standard deviation 12.712.7, gives p≈0.23p \approx 0.23. A permutation test gives the same message.
    5. Give I(X;X)I(X;X) for a discrete XX and for a continuous XX.
      AnswerDiscrete: I(X;X)=H(X)−H(X∣X)=H(X)I(X;X) = H(X) - H(X \mid X) = H(X). Continuous: ∞\infty, because knowing XX removes an infinite amount of uncertainty about a real number (h(X∣X)=−∞h(X \mid X) = -\infty).
    6. What is the capacity of BSC(0.2), and which input distribution reaches it?
      AnswerC=1−Hb(0.2)=0.278C = 1 - H_b(0.2) = 0.278 bits per use, reached by the uniform input P(X=1)=12P(X = 1) = \frac12, which makes H(Y)H(Y) as large as possible (1 bit).
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Write the mutual information I(X;Y)I(X;Y) as a difference of entropies.

    2. Write the mutual information as a KL divergence.

    3. What is I(X;Y)I(X;Y) for a fair bit XX sent through BSC(ε\varepsilon)?

    4. What is the mutual information of two jointly Gaussian scalars with correlation ρ\rho?

    5. What is I(X;X)I(X;X) for a discrete XX?

    6. State the data processing inequality and its condition.

    7. Why does the data processing inequality hold?

    8. How does mutual information change under invertible maps f(X)f(X), g(Y)g(Y)?

    9. What is the expected plug-in mutual information of independent variables with KXK_X and KYK_Y values and NN samples?

    10. Give an example with zero correlation but positive mutual information.

    Open the review →

    9. Interview angle #

    • "What is mutual information, and why use it instead of correlation?" The KL from the joint to the product of the marginals. It is zero exactly for independent variables, it sees nonlinear dependence, and it does not change under invertible transforms of each variable.
    • "Prove the data processing inequality." Expand I(X;Y,Z)I(X; Y, Z) with the chain rule in two orders, use I(X;Z∣Y)=0I(X;Z \mid Y) = 0 and I(X;Y∣Z)≥0I(X;Y \mid Z) \ge 0.
    • "Can a neural network's hidden layer contain more information about the label than the input?" No: Y→X→ZY \to X \to Z is Markov, so I(Y;Z)≤I(Y;X)I(Y;Z) \le I(Y;X). It can contain more usable information for a linear probe.
    • "How would you estimate mutual information between two signals?" Discrete: plug-in with a permutation baseline (or a bias correction). Continuous, low dimension: the Gaussian formula if the data are Gaussian, else KSG. High dimension: a variational bound with a learned critic (InfoNCE). Always state the bias.
    • "What is the information gain of a decision-tree split?" H(Y)−H(Y∣S)=I(Y;S)H(Y) - H(Y \mid S) = I(Y;S).

    10. Practice #

    The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:

    1. mutual_information(pxy) from a joint table, checked three ways: entropies, the KL form, and sklearn.metrics.mutual_info_score on counts.
    2. gaussian_mutual_information(cov, dx) from log-determinants, checked against −12log⁡(1−ρ2)-\frac12\log(1 - \rho^2) and against invariance under invertible linear maps of each block.
    3. The DPI by simulation: multiply a joint table by random channel matrices and check I(X;Z)≤I(X;Y)I(X;Z) \le I(X;Y).
    4. The plug-in bias: simulate independent data, compare the mean estimate with (KX−1)(KY−1)/(2N)(K_X - 1)(K_Y - 1)/(2N), and write a permutation test.
    5. Rank the features of the breast-cancer data set (sklearn.datasets.load_breast_cancer) by mutual information and by correlation with the label, with one synthetic feature whose spread, not its mean, depends on the class: correlation misses it, mutual information does not.

    11. Watch and read #

    Mutual information from a joint table, step by step, and why it works where correlation fails.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    David MacKay's Cambridge lecture: the mutual information of the binary symmetric channel and its capacity.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The definitions, the chain rule and the data processing inequality, with proofs.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • David MacKay, Information Theory, Inference, and Learning Algorithms, free PDF: §8.1 (mutual information and the two-bar diagram), §8.3 and exercise 8.9 (the data-processing theorem, with solution), chapter 9 (channels and capacity).
    • Kevin Murphy, Probabilistic Machine Learning: An Introduction, free PDF: §6.3 (mutual information, conditional mutual information, the data processing inequality, sufficient statistics).
    • Thomas Cover and Joy Thomas, Elements of Information Theory, 2nd edition, §2.4–2.9 (mutual information, chain rules, the data-processing inequality, sufficient statistics) and §8.5 (mutual information of densities; example 8.5.1 is the Gaussian case).
    • Alexander Kraskov, Harald Stögbauer and Peter Grassberger, "Estimating mutual information" (2004): the kNN (KSG) estimator used by scikit-learn.
    • Liam Paninski, "Estimation of entropy and mutual information" (Neural Computation, 2003): the bias of plug-in estimators and how to correct it.