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 is just the entropy of the pair, computed from the joint table:
Conditional entropy
The conditional entropy is the average, over , of the entropy of the row :
It is the uncertainty about that remains, on average, after is revealed.
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 and together is the same as learning and then given :
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: . A language model uses exactly this decomposition, predicting each token given the previous ones (Perplexity).
Conditioning reduces entropy (on average)
with equality exactly when and are independent. The difference is the mutual information (Mutual Information). Careful: this holds on average. A particular observation can increase the uncertainty:
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": , 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 by the chain rule, which is simpler than averaging rows. - In machine learning, 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 for some function , what is ?
Answer
Solution
Every row of puts all its mass on , so each row has entropy 0.
Exercise 2: Independent variables
is a fair coin and an independent fair die. What are and ?
Answer
bits and bits.
Solution
Independence gives and .
Exercise 3: Chain rule shortcut (code)
For the table , compute as .
Answer
About 0.7145 bits.
Solution
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 equals…
Which statement is always true?
- for every
When is ?
- When X and Y are independent
- When Y is a function of X
- Always
- Never