Before this lesson:lesson 01 (surprisal,
entropy, 0≤H≤logK, the lemma lnt≤t−1); joint, marginal and conditional
distributions and independence (Probability 04).
Time: about 55 minutes, plus the notebook.
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). 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) is the noise that no model can remove: the
expected log-loss of any classifier that predicts Y from X is at least H(Y∣X)
(lesson 04). Decision trees choose splits by the
drop H(Y)−H(Y∣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.
To find the pair, first find x. This takes H(X) questions on average. Then find y while
knowing x. This takes the entropy of Ygiven this x, which we write H(Y∣X=x). On
average over x, it takes H(Y∣X) questions. In total:
H(X,Y)=H(X)+H(Y∣X).
This is the chain rule. Independent coins: 2=1+1. A copy: 1=1+0, because x tells
you y.
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) and
H(Y∣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.
Write the joint distribution as a table: one row for each value x, one column for each value y.
The sky and rain example (all numbers are probabilities):
p(x,y)
dry
drizzle
rain
p(x)
clear
1/2
0
0
1/2
cloudy
1/8
1/8
1/4
1/2
p(y)
5/8
1/8
1/4
1
Follow four steps.
Row sums give the marginal p(x).Column sums give the marginal p(y).
Divide each row by its sump(x). This gives the conditional distribution p(y∣x): the
distribution of the rain when you know the sky. Clear: (1,0,0). Cloudy: (41,41,21).
Take the entropy of each row.H(Y∣X=clear)=0 bits and
H(Y∣X=cloudy)=1.5 bits.
Average the row entropies, with the weights p(x). This is the conditional entropy:
H(Y∣X)=21⋅0+21⋅1.5=0.75 bits.
Compare with H(Y)=1.299 bits, the entropy of the bottom row. The cloudy row has more entropy
than H(Y), but the average 0.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) and the row entropy H(Y∣X=x) (blue). The grey
bottom row is the marginal p(y) with H(Y), also drawn as the dashed line. The
yellow bar is H(Y∣X), the average of the row entropies weighted by
p(x). Notice: one row can reach past the dashed line, but the yellow bar never does.
Why the average cannot exceed H(Y). The bottom row is a mixture of the rows:
p(y)=∑xp(x)p(y∣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=(41,41) and x2=(21,0) (columns y1,y2). What is H(Y∣X) in bits?
Why.
p(x1)=p(x2)=21. Row x1 normalized is (21,21) with 1 bit; row x2 normalized is (1,0) with 0 bits. The weighted average is 21⋅1+21⋅0=0.5 bit. 0.811 bit is H(Y) for p(y)=(43,41), and 1.5 bits is H(X,Y).
X takes values in a finite set X, and Y in a finite set Y. The joint
distribution is p(x,y)=P(X=x,Y=y). The marginals are p(x)=∑yp(x,y) and
p(y)=∑xp(x,y). For p(x)>0, the conditional distribution is p(y∣x)=p(x,y)/p(x).
As in lesson 01, log is the natural log unless a base is written, and 0log0=0.
Take a pair (x,y) with p(x,y)>0. Then p(x)≥p(x,y)>0, and the product rule gives
p(x,y)=p(x)p(y∣x).
Take −log of both sides. The log of a product is a sum:
−logp(x,y)=−logp(x)−logp(y∣x). In words: the surprisal of the pair is the surprisal
of x plus the surprisal of y given x.
Take the expectation over (X,Y)∼p. Pairs with p(x,y)=0 have weight 0. The left side
becomes H(X,Y), and the last term becomes H(Y∣X).
The middle term depends only on x. Sum over y first:
∑x,yp(x,y)(−logp(x))=−∑x(∑yp(x,y))logp(x)=−∑xp(x)logp(x)=H(X).
Exchange the roles of X and Y for the second form. ■
Conditional entropy is not symmetric. Subtract the two forms of the chain rule:
H(Y∣X)−H(X∣Y)=H(Y)−H(X). So H(Y∣X)=H(X∣Y) whenever H(X)=H(Y).
Quick check
X is any discrete variable and Y=X (a copy). What is H(X,Y)?
Why.
The chain rule gives H(X,Y)=H(X)+H(Y∣X), and H(Y∣X)=0 because Y is a function of X. The pair has the same uncertainty as X alone. 2H(X) would be right only if Y were an independent copy with the same distribution.
Theorem 2 (chain rule for n variables).
H(X1,…,Xn)=i=1∑nH(Xi∣X1,…,Xi−1),
where the term for i=1 is H(X1), and H(Xi∣X1,…,Xi−1) conditions on the vector
(X1,…,Xi−1) seen as one variable.
Proof.
Apply the product rule n−1 times. Wherever the left side is positive,
p(x1,…,xn)=p(x1)p(x2∣x1)⋯p(xn∣x1,…,xn−1).
Take −log: the surprisal of the sequence is the sum of n conditional surprisals
−logp(xi∣x1,…,xi−1).
Take expectations. By linearity, term i becomes H(Xi∣X1,…,Xi−1).■
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.
because the products p(x)p(y) over all pairs sum to 1, so over S they sum to at most 1.
Equality. Step 3 is tight only if p(x)p(y)=p(x,y) on S, and p(x)p(y)=0 outside S
(where p(x,y)=0). Together: p(x,y)=p(x)p(y) for all pairs, which is independence.
Conversely, independence gives p(y∣x)=p(y), and the two entropies are equal. ■
The quantity ∑p(x,y)logp(x)p(y)p(x,y)=H(Y)−H(Y∣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) (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.5 bits
>H(Y)=1.299 bits: a cloudy sky makes the rain less predictable than it was before you
looked. The certain row "clear ⇒ dry" pulls the average down to 0.75 bits. MacKay puts it
this way: data do not increase uncertainty, on average.
Quick check
Can a single observation X=x make you more uncertain about Y, that is, H(Y∣X=x)>H(Y)?
Why.
In the weather table, H(Y∣X=cloudy)=1.5>1.299=H(Y). Theorem 3 is about the average over x, weighted by p(x):H(Y∣X)=0.75≤1.299. If X and Y are independent, every row equals p(y), so every row entropy equals H(Y) exactly.
The mixture view and the concavity of entropy
The proof above shows a more general fact. For distributions p1,…,pm on the same set and
weights λk≥0 with ∑kλk=1, entropy is concave:
H(∑kλkpk)≥∑kλkH(pk). Take pk = row k of the table and
λk=p(xk): the mixture is p(y), and the inequality is H(Y)≥H(Y∣X).Lesson 03 proves concavity with Jensen's inequality
for the concave function −tlogt. The law of total variance has the same shape:
E[Var(Y∣X)]≤Var(Y), while one
Var(Y∣X=x) can be larger than Var(Y).
Bounds on the joint entropy.max(H(X),H(Y))≤H(X,Y)≤H(X)+H(Y).
Left: the chain rule and H(Y∣X)≥0.H(X,Y)=H(X) exactly when Y is a function of X.
Right (subadditivity): the chain rule and Theorem 3. Equality exactly when X and Y are independent.
For n variables, Theorem 2 and H(Xi∣X1,…,Xi−1)≤H(Xi) give
H(X1,…,Xn)≤∑iH(Xi).
A function cannot add entropy. For any function g, write the chain rule in both orders for the
pair (X,g(X)):H(X)+H(g(X)∣X)=H(g(X))+H(X∣g(X)). Since H(g(X)∣X)=0, we get
H(g(X))=H(X)−H(X∣g(X))≤H(X), with equality exactly when g is one-to-one on the
values that X takes. Merging categories or binning data can only lower the entropy.
Quick check
Which statement is true for every pair of discrete random variables?
Why.
The chain rule gives H(X,Y)=H(X)+H(Y∣X)≥H(X), and in the other order ≥H(Y). The sum H(X)+H(Y) is only an upper bound (equality for independent variables). Conditional entropy is not symmetric, and one row can have more entropy than H(Y).
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). Rearranged:
H(Y)−H(Y∣X)=H(X)−H(X∣Y)=:I(X;Y).
This number is the mutual information: the uncertainty about Y that learning X removes, on
average, and the same amount the other way around. By Theorem 3 it is ≥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).
Interactive figure: loading…
Move the flip probability ε.X is a coin with P(X=1)=a, and Y is X with
probability 1−ε and flipped with probability ε. The second bar is H(X),
split into H(X∣Y) and the shared part
I(X;Y); the third bar is H(Y), split into
I(X;Y) and H(Y∣X). At ε=0 the
bars overlap completely; at ε=0.5 they do not overlap at all. Also try the weather table.
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), can be negative. Example
(MacKay, exercise 8.8): X and Y are independent fair bits and Z=X⊕Y (XOR). Then
I(X;Y)=0, but given Z, knowing X tells you Y exactly: I(X;Y∣Z)=1 bit. The centre
would have area 0−1=−1 bit. Use the chain rule, not the picture, for three or more variables.
Continuous variables, briefly
For densities, h(X,Y)=E[−logp(X,Y)] and h(Y∣X)=E[−logp(Y∣X)] are
differential entropies (lesson 06).
The chain rule (same proof) and h(Y∣X)≤h(Y) (same proof, with integrals) still hold.
Everything that used H≥0 fails: h(Y∣X) can be negative, and h(X,Y) can be smaller
than h(X). Example: (X,Y) standard bivariate Gaussian with correlation ρ. For every x,Y∣X=x∼N(ρx,1−ρ2), so h(Y∣X)=21ln(2πe(1−ρ2))
nats. For ρ=0.99 this is −0.540 nats, so h(X,Y)=h(X)+h(Y∣X)<h(X).
Answers to section 1. (1) (a) 2 bits; (b) 1 bit. (2) Yes for one value: in the weather table,
H(Y∣X=cloudy)=1.5 bits >H(Y)=1.299 bits. No on average:
H(Y∣X)≤H(Y) (Theorem 3). (3) No to both: H(X)≤H(X,Y)≤H(X)+H(Y).
Take the sky and rain table of section 2. All values are in bits.
Joint entropy. The nonzero entries 21,81,81,41 have surprisals 1, 3, 3,
2 bits. H(X,Y)=21⋅1+81⋅3+81⋅3+41⋅2=0.5+0.375+0.375+0.5=1.75 bits.
Marginal entropies.p(x)=(21,21), so H(X)=1 bit.
p(y)=(85,81,41), so
H(Y)=85log258+81⋅3+41⋅2=0.424+0.375+0.5=1.299 bits.
Rows. Clear: p(y∣clear)=(1,0,0), so H(Y∣X=clear)=0.
Cloudy: p(y∣cloudy)=(41,41,21), so
H(Y∣X=cloudy)=41⋅2+41⋅2+21⋅1=1.5 bits.
Conditional entropy.H(Y∣X)=21⋅0+21⋅1.5=0.75 bits.
Check with the chain rule: H(X)+H(Y∣X)=1+0.75=1.75=H(X,Y). ✓
Columns. Divide each column by its sum p(y). Dry: p(x∣dry)=(5/81/2,5/81/8)=(54,51),
so H(X∣Y=dry)=Hb(0.2)=0.722 bits. Drizzle and rain both mean "cloudy" for sure:
entropy 0. So H(X∣Y)=85⋅0.722+81⋅0+41⋅0=0.451 bits.
Chain rule: H(Y)+H(X∣Y)=1.299+0.451=1.75. ✓
One value against the average.H(Y∣X=cloudy)=1.5>H(Y)=1.299, but
H(Y∣X)=0.75<1.299.
Not symmetric.H(Y∣X)=0.75=H(X∣Y)=0.451. The difference is
0.299=H(Y)−H(X). ✓
What X tells about Y.H(Y)−H(Y∣X)=1.299−0.75=0.549 and
H(X)−H(X∣Y)=1−0.451=0.549. ✓ This is I(X;Y). A decision tree that predicts the
rain and splits on the sky has an information gain of 0.549 bits.
In nats, multiply by ln2=0.693:H(X,Y)=1.213 nats and H(Y∣X)=0.520 nats.
Quick check
In the weather table, what is H(X∣Y), the uncertainty about the sky when you know the rain?
Why.
Divide each column by p(y). Only the dry column is uncertain: (54,51) with 0.722 bits, and it has weight p(dry)=85. So H(X∣Y)=85⋅0.722=0.451 bits. 0.722 forgets the weight; 0.75 is H(Y∣X), and conditional entropy is not symmetric.
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)
directly.
importnumpyasnpfromscipy.statsimportentropyfromsklearn.metricsimportmutual_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 valuesH_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)forrinpxy])# scipy normalizes each rowH_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 columnscol_H=np.array([entropy(c,base=2)forcinpxy.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 countsI_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 isrng=np.random.default_rng(0)row_above=avg_above=0for_inrange(10_000):t=rng.dirichlet(np.ones(6)).reshape(2,3)HY=entropy(t.sum(axis=0))row_above+=max(entropy(r)forrint)>HYavg_above+=np.sum(t.sum(axis=1)*[entropy(r)forrint])>HY+1e-12print(row_above/10_000,avg_above)# 0.5062 0
In about half of the random 2×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)=0
instead of computing 0/0.
Autoregressive models. A language model writes
logp(x1,…,xT)=∑tlogp(xt∣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). 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),E[−logq(Y∣X)]≥H(Y∣X), with equality only for q(y∣x)=p(y∣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).
Decision trees. The information gain of a split S is H(Y)−H(Y∣S), where
H(Y∣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 θ and a measurement Y, the
expected entropy after the measurement is H(θ∣Y)≤H(θ). One measurement can
increase the uncertainty about θ; on average it cannot. The expected drop
H(θ)−H(θ∣Y)=I(θ;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 Y (for example, the strength of
ground shaking) from inputs X (magnitude, distance, site) has an irreducible spread: its
conditional entropy H(Y∣X). Adding inputs cannot raise it, H(Y∣X,Z)≤H(Y∣X)
(check yourself 5). A good generative model of Y given X should reproduce this spread, not
collapse to one value.
Reading Theorem 3 for each value.H(Y∣X=x)>H(Y) is possible. Only the average
H(Y∣X) is bounded by H(Y).
Equal weights for the rows.H(Y∣X) weights row x by p(x), not by 1/∣X∣.
Dividing by the wrong marginal.−∑p(x,y)logp(x)p(x,y) is H(Y∣X):
divide by the marginal of the variable you condition on. Dividing by p(y) gives H(X∣Y).
Assuming symmetry.H(Y∣X)=H(X∣Y) in general; they differ by H(Y)−H(X).
Mixing up the two extremes.H(Y∣X)=0 means Y is a function of X;H(Y∣X)=H(Y) means independence.
Trusting the Venn diagram for three variables. The centre region can be negative (XOR example).
X is a fair bit. Give H(X,Y) and H(Y∣X) in bits when (a) Y=X, and (b) Y is an
independent fair bit.Answer(a) H(X,Y)=1 and H(Y∣X)=0. (b) H(X,Y)=2 and H(Y∣X)=1=H(Y).
D is a fair die and X is its parity (even or odd). Give H(D),H(X),H(X∣D),H(D∣X) and H(D,X) in bits.AnswerH(D)=log26=2.585;H(X)=1;H(X∣D)=0(X is a function of D);H(D∣X)=log23=1.585 (three equally likely faces remain); H(D,X)=H(D)+0=H(X)+H(D∣X)=1+1.585=2.585.
In the weather table, is there a value y with H(X∣Y=y)>H(X)?AnswerNo. H(X∣Y=y) is 0.722,0 and 0. No column could exceed H(X) here: X is binary and uniform, so H(X)=1 bit is already the maximum log22.
X1,X2 are independent fair bits and X3=X1⊕X2 (XOR). Use the chain rule to compute
H(X1,X2,X3). Compare it with ∑iH(Xi) and with H(Xi,Xj) for each pair.AnswerH(X1)+H(X2∣X1)+H(X3∣X1,X2)=1+1+0=2 bits. ∑iH(Xi)=3 bits, so subadditivity is strict. Every pair is independent and uniform on 4 values: H(Xi,Xj)=2 bits. Any two of the three bits fix the third.
Prove H(Y∣X,Z)≤H(Y∣X). When is it an equality?AnswerH(Y∣X,Z)=∑xp(x)H(Y∣Z,X=x), where H(Y∣Z,X=x) is the conditional entropy under the distribution p(y,z∣x). For each x with p(x)>0, Theorem 3 applied to p(y,z∣x) gives H(Y∣Z,X=x)≤H(Y∣X=x). Average with the weights p(x). Equality holds exactly when Y and Z are independent given X=x, for every x with p(x)>0.
A classifier predicts a label Y from an input X. The labels are noisy: for every input, the
correct label is flipped to the other class with probability 0.1. What is the smallest
possible average log-loss in bits?AnswerH(Y∣X). For every x, the row p(y∣x) is (0.9,0.1) or (0.1,0.9), with entropy Hb(0.1)=0.469 bits. So H(Y∣X)=0.469 bits (0.325 nats), whatever the distribution of X. 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.
Define the conditional entropy H(Y∣X) as an average.
H(Y∣X)=∑xp(x)H(Y∣X=x): the entropies of the rows p(y∣x), weighted by p(x). Equivalently E[−logp(Y∣X)].
State the chain rule for two variables.
H(X,Y)=H(X)+H(Y∣X)=H(Y)+H(X∣Y).
State the chain rule for n variables.
H(X1,…,Xn)=∑i=1nH(Xi∣X1,…,Xi−1).
Why does the chain rule hold? (one line)
−logp(x,y)=−logp(x)−logp(y∣x) because p(x,y)=p(x)p(y∣x); take expectations.
What does H(Y∣X)=0 mean?
Y is a function of X: every row p(y∣x) with p(x)>0 is one-hot.
Does conditioning always reduce entropy?
Only on average: H(Y∣X)≤H(Y), with equality iff X and Y are independent. One value can give H(Y∣X=x)>H(Y).
Give the bounds on H(X,Y) in terms of H(X) and H(Y).
max(H(X),H(Y))≤H(X,Y)≤H(X)+H(Y); the right side is an equality iff X and Y are independent.
Write I(X;Y) as a difference of entropies.
I(X;Y)=H(Y)−H(Y∣X)=H(X)−H(X∣Y): the uncertainty about one variable removed by learning the other, on average.
X is a fair coin and Y=X. Compute H(X,Y) and H(Y∣X).
H(X,Y)=1 bit and H(Y∣X)=0. (With an independent fair coin: 2 bits and 1 bit.)
How is H(Y∣X) related to the loss of a classifier?
It is the lowest possible expected log-loss of any model q(y∣x), reached only by the true p(y∣x).
"Does more data always reduce uncertainty?" — On average, yes: H(Y∣X)≤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), reached by the
true p(y∣x). It is 0 only if the label is a function of the input.
"How does the chain rule relate to autoregressive models?" —
logp(x1:T)=∑tlogp(xt∣x<t); in expectation,
H(X1:T)=∑tH(Xt∣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)≥0: the parent distribution is a mixture of the children, and entropy is
concave.
"Is H(Y∣X)=H(X∣Y)?" — No. They differ by H(Y)−H(X). Mutual information is the
symmetric quantity.
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×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 n 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).
Code along in Colab
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.