Lesson 4 of 15
Conditional Entropy
Conditional Entropy
Conditional entropy measures the average uncertainty remaining in after you observe .
Chain Rule
The most useful formula uses the chain rule of entropy:
These hold because:
- Knowing perfectly predicts ⟹
- Independent and ⟹ (knowing tells you nothing about )
Key Inequalities
- Lower bound 0: is determined by (deterministic channel)
- Upper bound : and are independent
Example
If and are independent and uniform over 2 values each:
If deterministically:
import math
def shannon_entropy(probs):
return sum(-p * math.log2(p) for p in probs if p > 0)
def joint_entropy(joint_probs):
result = 0.0
for row in joint_probs:
for p in row:
if p > 0:
result += -p * math.log2(p)
return result
def conditional_entropy_yx(joint):
# H(Y|X) = H(X,Y) - H(X)
hxy = joint_entropy(joint)
hx = shannon_entropy([sum(row) for row in joint])
return hxy - hx
Your Task
Implement:
conditional_entropy_yx(joint)—conditional_entropy_xy(joint)—
Both take a 2D list of joint probabilities.
Python runtime loading...
Loading...
Click "Run" to execute your code.