TExcelTAD
Home › Information Theory › Joint and Conditional Entropy

Joint and Conditional Entropy

With two random variables we can ask how much uncertainty they have together, and how much uncertainty is left in one after we learn the other. These are the joint and conditional entropy, and they are tied together by the chain rule.

Joint entropy

The joint entropy of (X,Y)(X, Y) is just the entropy of the pair, computed from the joint table:

H(X,Y)=−∑x,yP(x,y)log⁡2P(x,y)H(X, Y) = -\sum_{x, y} P(x, y)\log_2 P(x, y)

Conditional entropy

The conditional entropy H(Y∣X)H(Y \mid X) is the average, over xx, of the entropy of the row P(y∣x)P(y \mid x):

H(Y∣X)=∑xP(x) H(Y∣X=x)=−∑x,yP(x,y)log⁡2P(y∣x)H(Y \mid X) = \sum_x P(x)\,H(Y \mid X = x) = -\sum_{x, y} P(x, y)\log_2 P(y \mid x)

It is the uncertainty about YY that remains, on average, after XX is revealed.

python · try it yourself
import numpy as np

def H(p):
    p = np.asarray(p, float).ravel(); p = p[p > 0]
    return float(-np.sum(p * np.log2(p))) + 0.0

# X = weather (sun, rain), Y = umbrella (no, yes)
P = np.array([[0.56, 0.14],
              [0.06, 0.24]])
Px = P.sum(axis=1)
H_Y_given_X = sum(Px[i] * H(P[i] / Px[i]) for i in range(2))
print("H(X)   =", round(H(Px), 4))
print("H(Y)   =", round(H(P.sum(axis=0)), 4))
print("H(X,Y) =", round(H(P), 4))
print("H(Y|X) =", round(H_Y_given_X, 4))
H(X)   = 0.8813
H(Y)   = 0.958
H(X,Y) = 1.6032
H(Y|X) = 0.7219

The chain rule

Learning XX and YY together is the same as learning XX and then YY given XX:

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)
python · try it yourself
import numpy as np

def H(p):
    p = np.asarray(p, float).ravel(); p = p[p > 0]
    return float(-np.sum(p * np.log2(p))) + 0.0

rng = np.random.default_rng(0)
P = rng.dirichlet(np.ones(12)).reshape(3, 4)
Px, Py = P.sum(axis=1), P.sum(axis=0)
H_Y_X = sum(Px[i] * H(P[i] / Px[i]) for i in range(3))
H_X_Y = sum(Py[j] * H(P[:, j] / Py[j]) for j in range(4))
print("H(X,Y)          =", round(H(P), 6))
print("H(X) + H(Y|X)   =", round(H(Px) + H_Y_X, 6))
print("H(Y) + H(X|Y)   =", round(H(Py) + H_X_Y, 6))
H(X,Y)          = 2.69482
H(X) + H(Y|X)   = 2.69482
H(Y) + H(X|Y)   = 2.69482

The chain rule extends to many variables: H(X1,…,Xn)=∑iH(Xi∣X1,…,Xi−1)H(X_1, \dots, X_n) = \sum_i H(X_i \mid X_1, \dots, X_{i-1}). A language model uses exactly this decomposition, predicting each token given the previous ones (Perplexity).

Conditioning reduces entropy (on average)

H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y)

with equality exactly when XX and YY are independent. The difference is the mutual information (Mutual Information). Careful: this holds on average. A particular observation X=xX = x can increase the uncertainty:

python · try it yourself
import numpy as np

def H(p):
    p = np.asarray(p, float).ravel(); p = p[p > 0]
    return float(-np.sum(p * np.log2(p))) + 0.0

# Y is usually 0, but when X = 1 (rare) Y is a coin flip
P = np.array([[0.90, 0.00],
              [0.05, 0.05]])
Px = P.sum(axis=1)
print("H(Y)         =", round(H(P.sum(axis=0)), 4))
print("H(Y | X = 0) =", round(H(P[0] / Px[0]), 4))
print("H(Y | X = 1) =", round(H(P[1] / Px[1]), 4), "(larger than H(Y)!)")
print("H(Y | X)     =", round(sum(Px[i] * H(P[i] / Px[i]) for i in range(2)), 4))
H(Y)         = 0.2864
H(Y | X = 0) = 0.0
H(Y | X = 1) = 1.0 (larger than H(Y)!)
H(Y | X)     = 0.1

Subadditivity

From the chain rule and "conditioning reduces entropy": H(X,Y)≤H(X)+H(Y)H(X, Y) \le H(X) + H(Y), with equality for independent variables. Two correlated files compressed together need fewer bits than compressed separately.

In software

  • With a joint table P, H(P.ravel()) is the joint entropy; H(P) - H(P.sum(axis=1)) is H(Y∣X)H(Y \mid X) by the chain rule, which is simpler than averaging rows.
  • In machine learning, H(Y∣X)H(Y \mid X) is the irreducible part of the classification loss: no model can do better than this cross-entropy (Cross-Entropy as a Loss).

Exercises

Exercise 1: Deterministic relation

If Y=f(X)Y = f(X) for some function ff, what is H(Y∣X)H(Y \mid X)?

Answer
Solution

Every row of P(y∣x)P(y \mid x) puts all its mass on f(x)f(x), so each row has entropy 0.

Exercise 2: Independent variables

XX is a fair coin and YY an independent fair die. What are H(X,Y)H(X, Y) and H(Y∣X)H(Y \mid X)?

Answer

H(X,Y)=3.585H(X, Y) = 3.585 bits and H(Y∣X)=2.585H(Y \mid X) = 2.585 bits.

Solution

Independence gives H(Y∣X)=H(Y)=log⁡26H(Y \mid X) = H(Y) = \log_2 6 and H(X,Y)=1+log⁡26H(X, Y) = 1 + \log_2 6.

Exercise 3: Chain rule shortcut (code)

For the table [0.30.10.10.5]\begin{bmatrix} 0.3 & 0.1 \\ 0.1 & 0.5 \end{bmatrix}, compute H(Y∣X)H(Y \mid X) as H(X,Y)−H(X)H(X, Y) - H(X).

Answer

About 0.7145 bits.

Solution
python · try it yourself
import numpy as np

def H(p):
    p = np.asarray(p, float).ravel(); p = p[p > 0]
    return float(-np.sum(p * np.log2(p))) + 0.0

P = np.array([[0.3, 0.1], [0.1, 0.5]])
print(round(H(P) - H(P.sum(axis=1)), 4))
0.7145

Test yourself

The chain rule says H(X,Y)H(X, Y) equals…

  1. H(X)+H(Y∣X)H(X) + H(Y \mid X)
  2. H(X)⋅H(Y)H(X) \cdot H(Y)
  3. H(X)−H(Y)H(X) - H(Y)
  4. max⁡(H(X),H(Y))\max(H(X), H(Y))

Which statement is always true?

  1. H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y)
  2. H(Y∣X=x)≤H(Y)H(Y \mid X = x) \le H(Y) for every xx
  3. H(X,Y)≥H(X)+H(Y)H(X, Y) \ge H(X) + H(Y)
  4. H(Y∣X)=H(X∣Y)H(Y \mid X) = H(X \mid Y)

When is H(X,Y)=H(X)+H(Y)H(X, Y) = H(X) + H(Y)?

  1. When X and Y are independent
  2. When Y is a function of X
  3. Always
  4. Never