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

    Information Theory · Lesson 2 · Surprise and entropy

    Joint and conditional entropy, the chain rule

    Beginner 55 min 3 interactive figures 3 videos Colab notebook

    Before this lesson: lesson 01 (surprisal, entropy, 0≤H≤log⁡K0 \le H \le \log K, the lemma ln⁡t≤t−1\ln t \le t - 1); joint, marginal and conditional distributions and independence (Probability 04). Time: about 55 minutes, plus the notebook.


    Why this matters #

    An autoregressive model, such as a large language model, writes the probability of a sequence as a product of next-token probabilities: p(x1,…,xT)=∏tp(xt∣x<t)p(x_1, \dots, x_T) = \prod_t p(x_t \mid x_{<t}). Its loss adds one conditional surprisal per token. The chain rule of this lesson is the same statement for entropies. The conditional entropy H(Y∣X)H(Y \mid X) is the noise that no model can remove: the expected log-loss of any classifier that predicts YY from XX is at least H(Y∣X)H(Y \mid X) (lesson 04). Decision trees choose splits by the drop H(Y)−H(Y∣split)H(Y) - H(Y \mid \text{split}), and active learning and Bayesian experimental design choose measurements by the expected drop in entropy. That drop is the mutual information, previewed at the end of section 3 and treated in lesson 07.

    1. Predict first #

    2. The picture #

    A pair is one random variable #

    The pair (X,Y)(X, Y) is itself a random variable. Its values are pairs (x,y)(x, y), and its entropy is the joint entropy H(X,Y)H(X, Y). Take XX a fair coin.

    • Independent coins. If YY is a second fair coin, independent of XX, the four pairs (H,H),(H,T),(T,H),(T,T)(\text{H}, \text{H}), (\text{H}, \text{T}), (\text{T}, \text{H}), (\text{T}, \text{T}) are equally likely. So H(X,Y)=log⁡24=2H(X, Y) = \log_2 4 = 2 bits.
    • A copy. If Y=XY = X, only (H,H)(\text{H}, \text{H}) and (T,T)(\text{T}, \text{T}) can happen, each with probability 12\frac12. So H(X,Y)=1H(X, Y) = 1 bit.

    Dependence lowers the joint entropy: once you know XX, a copy YY adds no new uncertainty.

    The question game for a pair #

    To find the pair, first find xx. This takes H(X)H(X) questions on average. Then find yy while knowing xx. This takes the entropy of YY given this xx, which we write H(Y∣X=x)H(Y \mid X = x). On average over xx, it takes H(Y∣X)H(Y \mid X) questions. In total:

    H(X,Y)=H(X)+H(Y∣X).H(X, Y) = H(X) + H(Y \mid X).

    This is the chain rule. Independent coins: 2=1+12 = 1 + 1. A copy: 1=1+01 = 1 + 0, because xx tells you yy.

    Interactive figure: loading…

    Choose an example and press Play once. The blue questions find the first variable; the yellow questions then find the second variable given the first. The readout compares the average number of questions of each stage with H(X)H(X) and H(Y∣X)H(Y \mid X). Try a coin and its copy: stage 2 needs no question. Then try weather: rain first: the entropies still add up, but whole questions cannot match surprisals that are not whole numbers.

    Rows of the joint table #

    Write the joint distribution as a table: one row for each value xx, one column for each value yy. The sky and rain example (all numbers are probabilities):

    p(x,y)p(x, y) dry drizzle rain p(x)p(x)
    clear 1/21/2 00 00 1/21/2
    cloudy 1/81/8 1/81/8 1/41/4 1/21/2
    p(y)p(y) 5/85/8 1/81/8 1/41/4 11

    Follow four steps.

    1. Row sums give the marginal p(x)p(x). Column sums give the marginal p(y)p(y).
    2. Divide each row by its sum p(x)p(x). This gives the conditional distribution p(y∣x)p(y \mid x): the distribution of the rain when you know the sky. Clear: (1,0,0)(1, 0, 0). Cloudy: (14,14,12)(\frac14, \frac14, \frac12).
    3. Take the entropy of each row. H(Y∣X=clear)=0H(Y \mid X = \text{clear}) = 0 bits and H(Y∣X=cloudy)=1.5H(Y \mid X = \text{cloudy}) = 1.5 bits.
    4. Average the row entropies, with the weights p(x)p(x). This is the conditional entropy: H(Y∣X)=12⋅0+12⋅1.5=0.75H(Y \mid X) = \frac12 \cdot 0 + \frac12 \cdot 1.5 = 0.75 bits.

    Compare with H(Y)=1.299H(Y) = 1.299 bits, the entropy of the bottom row. The cloudy row has more entropy than H(Y)H(Y), but the average 0.750.75 is less.

    Interactive figure: loading…

    Edit the weights or choose an example. Each row of the table is divided by its sum to give the bars p(y∣x)p(y \mid x) and the row entropy H(Y∣X=x)H(Y \mid X = x) (blue). The grey bottom row is the marginal p(y)p(y) with H(Y)H(Y), also drawn as the dashed line. The yellow bar is H(Y∣X)H(Y \mid X), the average of the row entropies weighted by p(x)p(x). Notice: one row can reach past the dashed line, but the yellow bar never does.

    Why the average cannot exceed H(Y)H(Y). The bottom row is a mixture of the rows: p(y)=∑xp(x) p(y∣x)p(y) = \sum_x p(x)\, p(y \mid x). Mixing distributions blurs them, and a blurred distribution is harder to guess than its parts on average. Section 3 proves this.

    Quick check

    A joint table has rows x1=(14,14)x_1 = (\frac14, \frac14) and x2=(12,0)x_2 = (\frac12, 0) (columns y1,y2y_1, y_2). What is H(Y∣X)H(Y \mid X) in bits?

    3. Definitions and main results #

    XX takes values in a finite set X\mathcal{X}, and YY in a finite set Y\mathcal{Y}. The joint distribution is p(x,y)=P(X=x,Y=y)p(x, y) = P(X = x, Y = y). The marginals are p(x)=∑yp(x,y)p(x) = \sum_y p(x, y) and p(y)=∑xp(x,y)p(y) = \sum_x p(x, y). For p(x)>0p(x) > 0, the conditional distribution is p(y∣x)=p(x,y)/p(x)p(y \mid x) = p(x, y) / p(x). As in lesson 01, log⁡\log is the natural log unless a base is written, and 0log⁡0=00 \log 0 = 0.

    Definitions #

    • Joint entropy:
    H(X,Y)=−∑x,yp(x,y)log⁡p(x,y)=E[−log⁡p(X,Y)].H(X, Y) = -\sum_{x, y} p(x, y) \log p(x, y) = \mathbb{E}\big[-\log p(X, Y)\big].

    It is the entropy of the pair seen as one variable with ∣X∣⋅∣Y∣|\mathcal{X}| \cdot |\mathcal{Y}| values.

    • Entropy of YY given one value X=xX = x (for p(x)>0p(x) > 0): the entropy of row xx,
    H(Y∣X=x)=−∑yp(y∣x)log⁡p(y∣x).H(Y \mid X = x) = -\sum_y p(y \mid x) \log p(y \mid x).
    • Conditional entropy: the average of the row entropies, weighted by p(x)p(x),
    H(Y∣X)=∑xp(x) H(Y∣X=x)=−∑x,yp(x,y)log⁡p(y∣x)=E[−log⁡p(Y∣X)].H(Y \mid X) = \sum_x p(x)\, H(Y \mid X = x) = -\sum_{x, y} p(x, y) \log p(y \mid x) = \mathbb{E}\big[-\log p(Y \mid X)\big].

    The two sums are equal because p(x) p(y∣x)=p(x,y)p(x)\, p(y \mid x) = p(x, y). Rows with p(x)=0p(x) = 0 have weight 0 and are left out.

    Three facts follow directly.

    • H(Y∣X)H(Y \mid X) is one number, not a function of xx. It averages over xx.
    • H(Y∣X)≥0H(Y \mid X) \ge 0, because every row entropy is ≥0\ge 0.
    • H(Y∣X)=0H(Y \mid X) = 0 if and only if every row with p(x)>0p(x) > 0 is one-hot, that is, Y=g(X)Y = g(X) for some function gg: knowing XX removes all uncertainty about YY.

    The chain rule #

    Theorem 1 (chain rule).

    H(X,Y)=H(X)+H(Y∣X)=H(Y)+H(X∣Y).H(X, Y) = H(X) + H(Y \mid X) = H(Y) + H(X \mid Y).

    Proof.

    1. Take a pair (x,y)(x, y) with p(x,y)>0p(x, y) > 0. Then p(x)≥p(x,y)>0p(x) \ge p(x, y) > 0, and the product rule gives p(x,y)=p(x) p(y∣x)p(x, y) = p(x)\, p(y \mid x).
    2. Take −log⁡-\log of both sides. The log of a product is a sum: −log⁡p(x,y)=−log⁡p(x)−log⁡p(y∣x)-\log p(x, y) = -\log p(x) - \log p(y \mid x). In words: the surprisal of the pair is the surprisal of xx plus the surprisal of yy given xx.
    3. Take the expectation over (X,Y)∼p(X, Y) \sim p. Pairs with p(x,y)=0p(x, y) = 0 have weight 0. The left side becomes H(X,Y)H(X, Y), and the last term becomes H(Y∣X)H(Y \mid X).
    4. The middle term depends only on xx. Sum over yy first: ∑x,yp(x,y)(−log⁡p(x))=−∑x(∑yp(x,y))log⁡p(x)=−∑xp(x)log⁡p(x)=H(X)\sum_{x, y} p(x, y) (-\log p(x)) = -\sum_x \big(\sum_y p(x, y)\big) \log p(x) = -\sum_x p(x) \log p(x) = H(X).
    5. Exchange the roles of XX and YY for the second form. ■\blacksquare

    Conditional entropy is not symmetric. Subtract the two forms of the chain rule: H(Y∣X)−H(X∣Y)=H(Y)−H(X)H(Y \mid X) - H(X \mid Y) = H(Y) - H(X). So H(Y∣X)≠H(X∣Y)H(Y \mid X) \ne H(X \mid Y) whenever H(X)≠H(Y)H(X) \ne H(Y).

    Quick check

    XX is any discrete variable and Y=XY = X (a copy). What is H(X,Y)H(X, Y)?

    Theorem 2 (chain rule for nn variables).

    H(X1,…,Xn)=∑i=1nH(Xi∣X1,…,Xi−1),H(X_1, \dots, X_n) = \sum_{i=1}^n H(X_i \mid X_1, \dots, X_{i-1}),

    where the term for i=1i = 1 is H(X1)H(X_1), and H(Xi∣X1,…,Xi−1)H(X_i \mid X_1, \dots, X_{i-1}) conditions on the vector (X1,…,Xi−1)(X_1, \dots, X_{i-1}) seen as one variable.

    Proof.

    1. Apply the product rule n−1n - 1 times. Wherever the left side is positive, p(x1,…,xn)=p(x1) p(x2∣x1)⋯p(xn∣x1,…,xn−1)p(x_1, \dots, x_n) = p(x_1)\, p(x_2 \mid x_1) \cdots p(x_n \mid x_1, \dots, x_{n-1}).
    2. Take −log⁡-\log: the surprisal of the sequence is the sum of nn conditional surprisals −log⁡p(xi∣x1,…,xi−1)-\log p(x_i \mid x_1, \dots, x_{i-1}).
    3. Take expectations. By linearity, term ii becomes H(Xi∣X1,…,Xi−1)H(X_i \mid X_1, \dots, X_{i-1}). ■\blacksquare

    Every order of the variables gives the same total, but the single terms change. An autoregressive model uses the order of the text; the total is the same for any order.

    Conditioning reduces entropy, on average #

    Theorem 3. H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y), with equality if and only if XX and YY are independent.

    Proof. We use the lemma of lesson 01: ln⁡t≤t−1\ln t \le t - 1 for t>0t > 0, with equality only at t=1t = 1. Work in nats and let S={(x,y):p(x,y)>0}S = \{(x, y) : p(x, y) > 0\}.

    1. For (x,y)∈S(x, y) \in S, both p(x)>0p(x) > 0 and p(y)>0p(y) > 0. Write both entropies as sums over SS: H(Y)=−∑Sp(x,y)ln⁡p(y)H(Y) = -\sum_{S} p(x, y) \ln p(y) (sum over xx first) and H(Y∣X)=−∑Sp(x,y)ln⁡p(y∣x)H(Y \mid X) = -\sum_S p(x, y) \ln p(y \mid x).
    2. Subtract, and use p(y∣x)=p(x,y)/p(x)p(y \mid x) = p(x, y) / p(x):
    H(Y∣X)−H(Y)=∑Sp(x,y)ln⁡p(y)p(y∣x)=∑Sp(x,y)ln⁡p(x) p(y)p(x,y).H(Y \mid X) - H(Y) = \sum_S p(x, y) \ln \frac{p(y)}{p(y \mid x)} = \sum_S p(x, y) \ln \frac{p(x)\, p(y)}{p(x, y)}.
    1. Apply the lemma with t=p(x)p(y)p(x,y)t = \frac{p(x) p(y)}{p(x, y)} to every term:
    H(Y∣X)−H(Y)≤∑Sp(x,y)(p(x)p(y)p(x,y)−1)=∑Sp(x) p(y)−1≤0,H(Y \mid X) - H(Y) \le \sum_S p(x, y) \Big( \frac{p(x) p(y)}{p(x, y)} - 1 \Big) = \sum_S p(x)\, p(y) - 1 \le 0,

    because the products p(x)p(y)p(x) p(y) over all pairs sum to 1, so over SS they sum to at most 1.

    1. Equality. Step 3 is tight only if p(x)p(y)=p(x,y)p(x) p(y) = p(x, y) on SS, and p(x)p(y)=0p(x) p(y) = 0 outside SS (where p(x,y)=0p(x, y) = 0). Together: p(x,y)=p(x) p(y)p(x, y) = p(x)\, p(y) for all pairs, which is independence. Conversely, independence gives p(y∣x)=p(y)p(y \mid x) = p(y), and the two entropies are equal. ■\blacksquare

    The quantity ∑p(x,y)log⁡p(x,y)p(x)p(y)=H(Y)−H(Y∣X)\sum p(x, y) \log \frac{p(x, y)}{p(x) p(y)} = H(Y) - H(Y \mid X) appears again: it is the KL divergence between the joint distribution and the product of the marginals (lesson 03), and it is the mutual information I(X;Y)I(X;Y) (lesson 07).

    Only on average. Theorem 3 bounds the weighted average of the row entropies. One row can have more entropy than the marginal. In the weather table, H(Y∣X=cloudy)=1.5H(Y \mid X = \text{cloudy}) = 1.5 bits >H(Y)=1.299> H(Y) = 1.299 bits: a cloudy sky makes the rain less predictable than it was before you looked. The certain row "clear ⇒\Rightarrow dry" pulls the average down to 0.750.75 bits. MacKay puts it this way: data do not increase uncertainty, on average.

    Quick check

    Can a single observation X=xX = x make you more uncertain about YY, that is, H(Y∣X=x)>H(Y)H(Y \mid X = x) > H(Y)?

    The mixture view and the concavity of entropy

    The proof above shows a more general fact. For distributions p1,…,pmp_1, \dots, p_m on the same set and weights λk≥0\lambda_k \ge 0 with ∑kλk=1\sum_k \lambda_k = 1, entropy is concave: H(∑kλkpk)≥∑kλkH(pk)H\big(\sum_k \lambda_k p_k\big) \ge \sum_k \lambda_k H(p_k). Take pkp_k = row kk of the table and λk=p(xk)\lambda_k = p(x_k): the mixture is p(y)p(y), and the inequality is H(Y)≥H(Y∣X)H(Y) \ge H(Y \mid X). Lesson 03 proves concavity with Jensen's inequality for the concave function −tlog⁡t-t \log t. The law of total variance has the same shape: E[Var⁡(Y∣X)]≤Var⁡(Y)\mathbb{E}[\operatorname{Var}(Y \mid X)] \le \operatorname{Var}(Y), while one Var⁡(Y∣X=x)\operatorname{Var}(Y \mid X = x) can be larger than Var⁡(Y)\operatorname{Var}(Y).

    Consequences #

    • Bounds on the joint entropy. max⁡(H(X),H(Y))≤H(X,Y)≤H(X)+H(Y)\max(H(X), H(Y)) \le H(X, Y) \le H(X) + H(Y).
      • Left: the chain rule and H(Y∣X)≥0H(Y \mid X) \ge 0. H(X,Y)=H(X)H(X, Y) = H(X) exactly when YY is a function of XX.
      • Right (subadditivity): the chain rule and Theorem 3. Equality exactly when XX and YY are independent.
      • For nn variables, Theorem 2 and H(Xi∣X1,…,Xi−1)≤H(Xi)H(X_i \mid X_1, \dots, X_{i-1}) \le H(X_i) give H(X1,…,Xn)≤∑iH(Xi)H(X_1, \dots, X_n) \le \sum_i H(X_i).
    • A function cannot add entropy. For any function gg, write the chain rule in both orders for the pair (X,g(X))(X, g(X)): H(X)+H(g(X)∣X)=H(g(X))+H(X∣g(X))H(X) + H(g(X) \mid X) = H(g(X)) + H(X \mid g(X)). Since H(g(X)∣X)=0H(g(X) \mid X) = 0, we get H(g(X))=H(X)−H(X∣g(X))≤H(X)H(g(X)) = H(X) - H(X \mid g(X)) \le H(X), with equality exactly when gg is one-to-one on the values that XX takes. Merging categories or binning data can only lower the entropy.
    Quick check

    Which statement is true for every pair of discrete random variables?

    The two-bar diagram and a preview of mutual information #

    The chain rule in both orders gives H(X)+H(Y∣X)=H(Y)+H(X∣Y)H(X) + H(Y \mid X) = H(Y) + H(X \mid Y). Rearranged:

    H(Y)−H(Y∣X)=H(X)−H(X∣Y)=:I(X;Y).H(Y) - H(Y \mid X) = H(X) - H(X \mid Y) =: I(X; Y).

    This number is the mutual information: the uncertainty about YY that learning XX removes, on average, and the same amount the other way around. By Theorem 3 it is ≥0\ge 0, and it is 0 exactly for independent variables. The joint entropy splits into three pieces (MacKay, Figure 8.1):

    H(X,Y)=H(X∣Y)+I(X;Y)+H(Y∣X).H(X, Y) = H(X \mid Y) + I(X; Y) + H(Y \mid X).
    Interactive figure: loading…

    Move the flip probability ε\varepsilon. XX is a coin with P(X=1)=aP(X = 1) = a, and YY is XX with probability 1−ε1 - \varepsilon and flipped with probability ε\varepsilon. The second bar is H(X)H(X), split into H(X∣Y)H(X \mid Y) and the shared part I(X;Y)I(X;Y); the third bar is H(Y)H(Y), split into I(X;Y)I(X;Y) and H(Y∣X)H(Y \mid X). At ε=0\varepsilon = 0 the bars overlap completely; at ε=0.5\varepsilon = 0.5 they do not overlap at all. Also try the weather table.

    Lesson 07 studies I(X;Y)I(X;Y) in full.

    Three variables: the Venn picture fails

    For two variables the bar picture is exact. With three variables it is tempting to draw three overlapping circles, but the centre region, I(X;Y)−I(X;Y∣Z)I(X;Y) - I(X;Y \mid Z), can be negative. Example (MacKay, exercise 8.8): XX and YY are independent fair bits and Z=X⊕YZ = X \oplus Y (XOR). Then I(X;Y)=0I(X;Y) = 0, but given ZZ, knowing XX tells you YY exactly: I(X;Y∣Z)=1I(X;Y \mid Z) = 1 bit. The centre would have area 0−1=−10 - 1 = -1 bit. Use the chain rule, not the picture, for three or more variables.

    Continuous variables, briefly

    For densities, h(X,Y)=E[−log⁡p(X,Y)]h(X, Y) = \mathbb{E}[-\log p(X, Y)] and h(Y∣X)=E[−log⁡p(Y∣X)]h(Y \mid X) = \mathbb{E}[-\log p(Y \mid X)] are differential entropies (lesson 06). The chain rule (same proof) and h(Y∣X)≤h(Y)h(Y \mid X) \le h(Y) (same proof, with integrals) still hold. Everything that used H≥0H \ge 0 fails: h(Y∣X)h(Y \mid X) can be negative, and h(X,Y)h(X, Y) can be smaller than h(X)h(X). Example: (X,Y)(X, Y) standard bivariate Gaussian with correlation ρ\rho. For every xx, Y∣X=x∼N(ρx,1−ρ2)Y \mid X = x \sim \mathcal{N}(\rho x, 1 - \rho^2), so h(Y∣X)=12ln⁡(2πe(1−ρ2))h(Y \mid X) = \frac12 \ln\big(2\pi e (1 - \rho^2)\big) nats. For ρ=0.99\rho = 0.99 this is −0.540-0.540 nats, so h(X,Y)=h(X)+h(Y∣X)<h(X)h(X, Y) = h(X) + h(Y \mid X) < h(X).

    Answers to section 1. (1) (a) 22 bits; (b) 11 bit. (2) Yes for one value: in the weather table, H(Y∣X=cloudy)=1.5H(Y \mid X = \text{cloudy}) = 1.5 bits >H(Y)=1.299> H(Y) = 1.299 bits. No on average: H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y) (Theorem 3). (3) No to both: H(X)≤H(X,Y)≤H(X)+H(Y)H(X) \le H(X, Y) \le H(X) + H(Y).

    4. Worked example #

    Take the sky and rain table of section 2. All values are in bits.

    1. Joint entropy. The nonzero entries 12,18,18,14\frac12, \frac18, \frac18, \frac14 have surprisals 1, 3, 3, 2 bits. H(X,Y)=12⋅1+18⋅3+18⋅3+14⋅2=0.5+0.375+0.375+0.5=1.75H(X, Y) = \frac12 \cdot 1 + \frac18 \cdot 3 + \frac18 \cdot 3 + \frac14 \cdot 2 = 0.5 + 0.375 + 0.375 + 0.5 = 1.75 bits.
    2. Marginal entropies. p(x)=(12,12)p(x) = (\frac12, \frac12), so H(X)=1H(X) = 1 bit. p(y)=(58,18,14)p(y) = (\frac58, \frac18, \frac14), so H(Y)=58log⁡285+18⋅3+14⋅2=0.424+0.375+0.5=1.299H(Y) = \frac58 \log_2 \frac85 + \frac18 \cdot 3 + \frac14 \cdot 2 = 0.424 + 0.375 + 0.5 = 1.299 bits.
    3. Rows. Clear: p(y∣clear)=(1,0,0)p(y \mid \text{clear}) = (1, 0, 0), so H(Y∣X=clear)=0H(Y \mid X = \text{clear}) = 0. Cloudy: p(y∣cloudy)=(14,14,12)p(y \mid \text{cloudy}) = (\frac14, \frac14, \frac12), so H(Y∣X=cloudy)=14⋅2+14⋅2+12⋅1=1.5H(Y \mid X = \text{cloudy}) = \frac14 \cdot 2 + \frac14 \cdot 2 + \frac12 \cdot 1 = 1.5 bits.
    4. Conditional entropy. H(Y∣X)=12⋅0+12⋅1.5=0.75H(Y \mid X) = \frac12 \cdot 0 + \frac12 \cdot 1.5 = 0.75 bits. Check with the chain rule: H(X)+H(Y∣X)=1+0.75=1.75=H(X,Y)H(X) + H(Y \mid X) = 1 + 0.75 = 1.75 = H(X, Y). ✓
    5. Columns. Divide each column by its sum p(y)p(y). Dry: p(x∣dry)=(1/25/8,1/85/8)=(45,15)p(x \mid \text{dry}) = (\frac{1/2}{5/8}, \frac{1/8}{5/8}) = (\frac45, \frac15), so H(X∣Y=dry)=Hb(0.2)=0.722H(X \mid Y = \text{dry}) = H_b(0.2) = 0.722 bits. Drizzle and rain both mean "cloudy" for sure: entropy 0. So H(X∣Y)=58⋅0.722+18⋅0+14⋅0=0.451H(X \mid Y) = \frac58 \cdot 0.722 + \frac18 \cdot 0 + \frac14 \cdot 0 = 0.451 bits. Chain rule: H(Y)+H(X∣Y)=1.299+0.451=1.75H(Y) + H(X \mid Y) = 1.299 + 0.451 = 1.75. ✓
    6. One value against the average. H(Y∣X=cloudy)=1.5>H(Y)=1.299H(Y \mid X = \text{cloudy}) = 1.5 > H(Y) = 1.299, but H(Y∣X)=0.75<1.299H(Y \mid X) = 0.75 < 1.299.
    7. Not symmetric. H(Y∣X)=0.75≠H(X∣Y)=0.451H(Y \mid X) = 0.75 \ne H(X \mid Y) = 0.451. The difference is 0.299=H(Y)−H(X)0.299 = H(Y) - H(X). ✓
    8. What XX tells about YY. H(Y)−H(Y∣X)=1.299−0.75=0.549H(Y) - H(Y \mid X) = 1.299 - 0.75 = 0.549 and H(X)−H(X∣Y)=1−0.451=0.549H(X) - H(X \mid Y) = 1 - 0.451 = 0.549. ✓ This is I(X;Y)I(X;Y). A decision tree that predicts the rain and splits on the sky has an information gain of 0.5490.549 bits.

    In nats, multiply by ln⁡2=0.693\ln 2 = 0.693: H(X,Y)=1.213H(X, Y) = 1.213 nats and H(Y∣X)=0.520H(Y \mid X) = 0.520 nats.

    Quick check

    In the weather table, what is H(X∣Y)H(X \mid Y), the uncertainty about the sky when you know the rain?

    5. In code #

    NumPy and SciPy check the worked example; scikit-learn gives a second route through the mutual information. scipy.stats.entropy normalizes its input, so a row of the table gives H(Y∣X=x)H(Y \mid X = x) directly.

    import numpy as np
    from scipy.stats import entropy
    from sklearn.metrics import mutual_info_score
    
    # rows x = (clear, cloudy), columns y = (dry, drizzle, rain)
    pxy = np.array([[1/2, 0,   0  ],
                    [1/8, 1/8, 1/4]])
    px, py = pxy.sum(axis=1), pxy.sum(axis=0)
    H_XY = entropy(pxy.ravel(), base=2)          # the pair as one variable with 6 values
    H_X, H_Y = entropy(px, base=2), entropy(py, base=2)
    print(H_XY, H_X, round(H_Y, 4))              # 1.75 1.0 1.2988
    
    # H(Y|X): average of the row entropies, weighted by p(x)
    row_H = np.array([entropy(r, base=2) for r in pxy])   # scipy normalizes each row
    H_YgX = np.sum(px * row_H)
    print(row_H, H_YgX, H_XY - H_X)              # [0.  1.5] 0.75 0.75
    
    # H(X|Y): the same with columns
    col_H = np.array([entropy(c, base=2) for c in pxy.T])
    print(col_H.round(4), round(np.sum(py * col_H), 4), round(H_XY - H_Y, 4))
    # [0.7219 0.     0.    ] 0.4512 0.4512
    
    # I(X;Y) = H(Y) - H(Y|X); sklearn computes it in nats from counts
    I_bits = mutual_info_score(None, None, contingency=8 * pxy) / np.log(2)
    print(round(H_Y - H_YgX, 4), round(I_bits, 4))   # 0.5488 0.5488
    
    # random 2 x 3 tables: one row can be above H(Y), the average never is
    rng = np.random.default_rng(0)
    row_above = avg_above = 0
    for _ in range(10_000):
        t = rng.dirichlet(np.ones(6)).reshape(2, 3)
        HY = entropy(t.sum(axis=0))
        row_above += max(entropy(r) for r in t) > HY
        avg_above += np.sum(t.sum(axis=1) * [entropy(r) for r in t]) > HY + 1e-12
    print(row_above / 10_000, avg_above)         # 0.5062 0
    

    In about half of the random 2×32 \times 3 tables, one row has more entropy than the marginal; the average never does. When you write the row normalization yourself, skip rows with p(x)=0p(x) = 0 instead of computing 0/00/0.

    6. Where it shows up #

    • Autoregressive models. A language model writes log⁡p(x1,…,xT)=∑tlog⁡p(xt∣x<t)\log p(x_1, \dots, x_T) = \sum_t \log p(x_t \mid x_{<t}): Theorem 2 for single outcomes. In expectation, the entropy of a sequence is the sum of the per-token conditional entropies H(Xt∣X<t)H(X_t \mid X_{<t}). The training loss is the average conditional surprisal per token, and its exponential (in nats) is the perplexity (the LLMs track, in preparation). Probabilistic weather emulators that roll out one time step at a time use the same factorization over time.
    • The lowest possible loss of a classifier. For any model q(y∣x)q(y \mid x), E[−log⁡q(Y∣X)]≥H(Y∣X)\mathbb{E}[-\log q(Y \mid X)] \ge H(Y \mid X), with equality only for q(y∣x)=p(y∣x)q(y \mid x) = p(y \mid x) (lesson 04). The floor is the label noise that remains given the input (aleatoric uncertainty). It is not 0, and it is not H(Y)H(Y).
    • Decision trees. The information gain of a split SS is H(Y)−H(Y∣S)H(Y) - H(Y \mid S), where H(Y∣S)H(Y \mid S) is the entropy of the labels in each child, weighted by the fraction of samples in the child. Theorem 3 says it is never negative (the decision-tree lesson of the ML fundamentals track, in preparation).
    • Experimental design and active learning. For a parameter θ\theta and a measurement YY, the expected entropy after the measurement is H(θ∣Y)≤H(θ)H(\theta \mid Y) \le H(\theta). One measurement can increase the uncertainty about θ\theta; on average it cannot. The expected drop H(θ)−H(θ∣Y)=I(θ;Y)H(\theta) - H(\theta \mid Y) = I(\theta; Y) is the expected information gain, used by BALD in active learning and by Bayesian design of experiments and sensor networks.
    • ML for science. A model that predicts an observed quantity YY (for example, the strength of ground shaking) from inputs XX (magnitude, distance, site) has an irreducible spread: its conditional entropy H(Y∣X)H(Y \mid X). Adding inputs cannot raise it, H(Y∣X,Z)≤H(Y∣X)H(Y \mid X, Z) \le H(Y \mid X) (check yourself 5). A good generative model of YY given XX should reproduce this spread, not collapse to one value.

    7. Common mistakes #

    • Reading Theorem 3 for each value. H(Y∣X=x)>H(Y)H(Y \mid X = x) > H(Y) is possible. Only the average H(Y∣X)H(Y \mid X) is bounded by H(Y)H(Y).
    • Equal weights for the rows. H(Y∣X)H(Y \mid X) weights row xx by p(x)p(x), not by 1/∣X∣1/|\mathcal{X}|.
    • Dividing by the wrong marginal. −∑p(x,y)log⁡p(x,y)p(x)-\sum p(x, y) \log \frac{p(x, y)}{p(x)} is H(Y∣X)H(Y \mid X): divide by the marginal of the variable you condition on. Dividing by p(y)p(y) gives H(X∣Y)H(X \mid Y).
    • Assuming symmetry. H(Y∣X)≠H(X∣Y)H(Y \mid X) \ne H(X \mid Y) in general; they differ by H(Y)−H(X)H(Y) - H(X).
    • Mixing up the two extremes. H(Y∣X)=0H(Y \mid X) = 0 means YY is a function of XX; H(Y∣X)=H(Y)H(Y \mid X) = H(Y) means independence.
    • Trusting the Venn diagram for three variables. The centre region can be negative (XOR example).

    8. Check yourself #

    1. XX is a fair bit. Give H(X,Y)H(X, Y) and H(Y∣X)H(Y \mid X) in bits when (a) Y=XY = X, and (b) YY is an independent fair bit.
      Answer(a) H(X,Y)=1H(X, Y) = 1 and H(Y∣X)=0H(Y \mid X) = 0. (b) H(X,Y)=2H(X, Y) = 2 and H(Y∣X)=1=H(Y)H(Y \mid X) = 1 = H(Y).
    2. DD is a fair die and XX is its parity (even or odd). Give H(D)H(D), H(X)H(X), H(X∣D)H(X \mid D), H(D∣X)H(D \mid X) and H(D,X)H(D, X) in bits.
      AnswerH(D)=log⁡26=2.585H(D) = \log_2 6 = 2.585; H(X)=1H(X) = 1; H(X∣D)=0H(X \mid D) = 0 (XX is a function of DD); H(D∣X)=log⁡23=1.585H(D \mid X) = \log_2 3 = 1.585 (three equally likely faces remain); H(D,X)=H(D)+0=H(X)+H(D∣X)=1+1.585=2.585H(D, X) = H(D) + 0 = H(X) + H(D \mid X) = 1 + 1.585 = 2.585.
    3. In the weather table, is there a value yy with H(X∣Y=y)>H(X)H(X \mid Y = y) > H(X)?
      AnswerNo. H(X∣Y=y)H(X \mid Y = y) is 0.7220.722, 00 and 00. No column could exceed H(X)H(X) here: XX is binary and uniform, so H(X)=1H(X) = 1 bit is already the maximum log⁡22\log_2 2.
    4. X1,X2X_1, X_2 are independent fair bits and X3=X1⊕X2X_3 = X_1 \oplus X_2 (XOR). Use the chain rule to compute H(X1,X2,X3)H(X_1, X_2, X_3). Compare it with ∑iH(Xi)\sum_i H(X_i) and with H(Xi,Xj)H(X_i, X_j) for each pair.
      AnswerH(X1)+H(X2∣X1)+H(X3∣X1,X2)=1+1+0=2H(X_1) + H(X_2 \mid X_1) + H(X_3 \mid X_1, X_2) = 1 + 1 + 0 = 2 bits. ∑iH(Xi)=3\sum_i H(X_i) = 3 bits, so subadditivity is strict. Every pair is independent and uniform on 4 values: H(Xi,Xj)=2H(X_i, X_j) = 2 bits. Any two of the three bits fix the third.
    5. Prove H(Y∣X,Z)≤H(Y∣X)H(Y \mid X, Z) \le H(Y \mid X). When is it an equality?
      AnswerH(Y∣X,Z)=∑xp(x) H(Y∣Z,X=x)H(Y \mid X, Z) = \sum_x p(x)\, H(Y \mid Z, X = x), where H(Y∣Z,X=x)H(Y \mid Z, X = x) is the conditional entropy under the distribution p(y,z∣x)p(y, z \mid x). For each xx with p(x)>0p(x) > 0, Theorem 3 applied to p(y,z∣x)p(y, z \mid x) gives H(Y∣Z,X=x)≤H(Y∣X=x)H(Y \mid Z, X = x) \le H(Y \mid X = x). Average with the weights p(x)p(x). Equality holds exactly when YY and ZZ are independent given X=xX = x, for every xx with p(x)>0p(x) > 0.
    6. A classifier predicts a label YY from an input XX. The labels are noisy: for every input, the correct label is flipped to the other class with probability 0.10.1. What is the smallest possible average log-loss in bits?
      AnswerH(Y∣X)H(Y \mid X). For every xx, the row p(y∣x)p(y \mid x) is (0.9,0.1)(0.9, 0.1) or (0.1,0.9)(0.1, 0.9), with entropy Hb(0.1)=0.469H_b(0.1) = 0.469 bits. So H(Y∣X)=0.469H(Y \mid X) = 0.469 bits (0.3250.325 nats), whatever the distribution of XX. No model can reach a lower average loss.
    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. Define the conditional entropy H(Y∣X)H(Y \mid X) as an average.

    2. State the chain rule for two variables.

    3. State the chain rule for nn variables.

    4. Why does the chain rule hold? (one line)

    5. What does H(Y∣X)=0H(Y \mid X) = 0 mean?

    6. Does conditioning always reduce entropy?

    7. Give the bounds on H(X,Y)H(X, Y) in terms of H(X)H(X) and H(Y)H(Y).

    8. Write I(X;Y)I(X;Y) as a difference of entropies.

    9. XX is a fair coin and Y=XY = X. Compute H(X,Y)H(X, Y) and H(Y∣X)H(Y \mid X).

    10. How is H(Y∣X)H(Y \mid X) related to the loss of a classifier?

    Open the review →

    9. Interview angle #

    • "Does more data always reduce uncertainty?" — On average, yes: H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y). Not for every outcome: a surprising observation can raise the entropy.
    • "What is the lowest cross-entropy loss a classifier can reach?" — H(Y∣X)H(Y \mid X), reached by the true p(y∣x)p(y \mid x). It is 0 only if the label is a function of the input.
    • "How does the chain rule relate to autoregressive models?" — log⁡p(x1:T)=∑tlog⁡p(xt∣x<t)\log p(x_{1:T}) = \sum_t \log p(x_t \mid x_{<t}); in expectation, H(X1:T)=∑tH(Xt∣X<t)H(X_{1:T}) = \sum_t H(X_t \mid X_{<t}). Every order gives the same total; the single terms differ.
    • "Why is the information gain of a decision-tree split never negative?" — It is H(Y)−H(Y∣S)≥0H(Y) - H(Y \mid S) \ge 0: the parent distribution is a mixture of the children, and entropy is concave.
    • "Is H(Y∣X)=H(X∣Y)H(Y \mid X) = H(X \mid Y)?" — No. They differ by H(Y)−H(X)H(Y) - H(X). Mutual information is the symmetric quantity.

    10. Practice #

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

    1. Write marginals and conditional_table for a joint table (a row with p(x)=0p(x) = 0 stays zero).
    2. Write conditional_entropy(pxy) from the definition, and check it against the chain rule.
    3. Search random 2×32 \times 3 tables for one where a row entropy exceeds H(Y)H(Y), and check that the average never does.
    4. Check the chain rule for three variables on a random 2×2×22 \times 2 \times 2 table, in two orders.
    5. Compute the information gain of a split at the root of a decision tree on a small data set.

    11. Watch and read #

    Joint and conditional entropy, mutual information and the two-bar picture for a noisy channel.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A university lecture that defines entropy, joint and conditional entropy, with the proofs.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A slow, concrete preview of mutual information, the shared part of the two-bar diagram.
    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 (definitions, the chain rule, Figure 8.1) and exercises 8.2 (one value against the average), 8.6 (a 4×44 \times 4 table, with solution) and 8.8 (why the Venn diagram misleads).
    • Kevin Murphy, Probabilistic Machine Learning: An Introduction, free PDF: §6.1.3 (joint entropy) and §6.1.4 (conditional entropy, the "on average" remark and the chain rule for nn variables).
    • The dit Python library, Basic Shannon measures: joint and conditional entropy with code.
    • Cover and Thomas, Elements of Information Theory (2nd edition), §2.2, §2.5 and Theorem 2.6.5. The standard textbook (not free).