skip to content

How does the chain rule H(X,Y) = H(X) + H(Y|X) price storing both columns of a routing log?

level: middleimportance: must knowfreq 48%

answer

  1. pay full price once, then only the surprise
  2. joint splits into marginal plus conditional
  3. sum of marginals is the upper bound
  4. larger marginal is the lower bound
  5. either ordering, same total

basics

~20 s

The chain rule prices a column pair as the first column's entropy plus whatever surprise the second still holds: H(X,Y) = H(X) + H(Y|X). Storing both together therefore never costs more than storing each separately, and usually costs less.

solid answer

~40 s

The chain rule decomposes the **joint entropy** of a pair into one column's entropy plus the *conditional* entropy of the other given it. In a log with four equally common regions and a data centre that is the region's home one 7 rows in 8, that is 2 + 0.544 = 2.544 bits a row. Compare the alternatives: two fixed-width 2-bit fields cost 4 bits, and coding the columns as if independent also costs 2 + 2 = 4 bits, because independent coding cannot use the region to predict the data centre. So the pair's floor is about 36% below the naive cost. The decomposition is symmetric, H(X) + H(Y|X) = H(Y) + H(X|Y), so ordering changes which conditional you must model, not the total.

go deeper

for a junior

Remember the shape: the cost of a pair of fields is one field's cost plus whatever the second still adds on top. Adding the two fields' costs outright overstates it whenever they are related.

for a middle

Write the identity, put real numbers in it from a small joint table, and place the result between its two bounds: at least the larger marginal, at most the sum of the marginals.

for a senior

Use it to argue about a real record layout: what the floor is for keeping both fields, why that is not the same as dropping one, and why the average hides per-row variation.

for a principal

Weigh the saving the decomposition promises against the cost of carrying a conditional model that must stay correct as the relationship between the fields drifts.

## What the rule says The **chain rule for entropy** states that the joint entropy of two columns equals one column's entropy plus the conditional entropy of the other given it: `H(X, Y) = H(X) + H(Y | X) = H(Y) + H(X | Y)` Read it as a bill in two lines. You pay in full for whichever column you describe first, and then you pay only for what the second column still surprises you with once the first is on the table. That is also exactly how a two-pass encoder would emit the pair: region first, then the data centre against a model chosen by the region. ## The worked log Each row holds a **region** and the **data centre** it routed to, four values each. The regions are equally common; each routes 7 rows in 8 to its home data centre and 1 in 8 to a failover, cyclically, so the data-centre column is also uniform over four values. - entropy of the region column: 2 bits - entropy of the data-centre column: 2 bits - data centre given region: H(7/8, 1/8) = 0.544 bits - joint entropy of the pair: 2 + 0.544 = **2.544 bits a row** ## Reading it as a bill | scheme | bits a row | what it assumes | |---|---|---| | two fixed-width fields | 4 | nothing; four values need 2 bits each | | each column coded separately at its own entropy | 4 | the columns carry no information about each other | | the pair coded at its joint entropy | 2.544 | the region predicts the data centre, and the coder uses it | | region column only, data centre dropped | 2 | that the second column is recoverable, which here it is not | The third row is the chain rule's number and the honest floor for keeping both fields. The fourth is not a cheaper encoding of the same data; it is a different, lossier dataset, because 1 row in 8 disagrees with its region's home mapping. ## The bounds that fall out Two inequalities follow immediately, and interviewers ask for both: - **Upper bound:** `H(X, Y) <= H(X) + H(Y)`, because conditioning never raises entropy on average. Equality holds exactly when the columns are independent. - **Lower bound:** `H(X, Y) >= max(H(X), H(Y))`, because knowing the pair reveals each column, and conditional entropies are never negative. Equality holds when one column is a function of the other. In the worked log both bounds are informative: 2 <= 2.544 <= 4. A candidate who cannot place the joint between those two numbers has not internalised the rule. ## Why the order does not matter Both decompositions give the same total, so the pair's cost is a property of the pair, not of the order you chose. Here 2 + 0.544 and 2 + 0.544 agree trivially because the marginals match; in general `H(X) + H(Y | X) = H(Y) + H(X | Y)` is what forces the difference of the conditionals to equal the difference of the marginals. Ordering matters for a different reason: it decides which conditional model you have to build and keep, and describing first the column with fewer distinct values usually means fewer per-slice models to carry. ## More than two columns The rule telescopes: `H(X, Y, Z) = H(X) + H(Y | X) + H(Z | X, Y)`. Each new column costs only what it still surprises you with given everything already described. This is why wide records with several near-duplicate fields are so much cheaper jointly than field by field, and also why the conditioning tables grow fast: conditioning on two columns means one model per observed combination, and combinations get sparse quickly. ## Where engineers go wrong - Adding the two marginal entropies and calling the sum the joint entropy. That is an upper bound, and it is tight only under independence. - Believing the joint can fall below the larger marginal, so that storing a pair costs less than storing one of its columns. - Treating the 2.544-bit figure as a per-row cost. It is an average; a conforming row carries about 2.19 bits of surprisal and a departing one about 5 bits, and only the mean hits 2.544. - Thinking the chain rule needs independence. It is an identity that always holds; independence is only the special case where the conditional term collapses to a marginal.

  • Does it matter which column the chain rule takes first?
    Not for the total: H(X) + H(Y|X) and H(Y) + H(X|Y) are equal by the identity, so the pair's floor is order-independent. Ordering decides which conditional model you must build and keep, so in practice you describe first the column with fewer distinct values, which needs fewer per-slice models.
  • How does the rule extend to three columns?
    It telescopes: H(X,Y,Z) = H(X) + H(Y|X) + H(Z|X,Y). Each column costs only what it adds given everything already described. The catch is practical rather than theoretical: conditioning on two columns needs a model per observed combination, and those combinations go sparse long before the arithmetic does.
  • Why can the joint entropy never fall below either column's own entropy?
    Because knowing the pair tells you each column, and the extra term in the chain rule is a conditional entropy, which is never negative. So H(X,Y) = H(X) + H(Y|X) is at least H(X), and by symmetry at least H(Y). Storing a pair can never be cheaper than storing one of its halves.

Describing a pair of fields is like giving directions twice: you spell out the street in full, then for the house number you only have to say how it differs from the one the street already implies.

saying these in an interview costs you the question

  • Adds the two marginal entropies and calls that the joint entropy
  • Believes the joint entropy can be lower than either column's entropy
  • Thinks the chain rule only holds when the columns are independent
  • Treats the joint entropy figure as an exact per-row cost
  • Assumes the decomposition order changes the joint entropy value