Guide To AI LogoGuide To AI
Unit 12

Information Theory for Machine Learning

measuring information, uncertainty, and statistical relationships within data

Core Concepts Covered

  • Entropy, joint entropy, and conditional entropy
  • Cross-entropy, Kullback-Leibler (KL) divergence, and mutual info
  • Information gain in tree-based algorithms

1. Entropy: Measuring Uncertainty and Surprise

Information theory, pioneered by Claude Shannon in 1948, is the mathematical study of quantifying, storing, and communicating data. In machine learning, it provides the fundamental tools to measure statistical uncertainty, quantify structural correlations, and design robust loss functions.

Shannon Entropy (H(X)H(X)): Measures the average amount of uncertainty or 'surprise' in a random variable XX. If an outcome is highly certain, its entropy is low; if it is completely random, its entropy is maximized. The formula for a discrete variable is:

H(X)=xXP(x)log2P(x)H(X) = -\sum_{x \in \mathcal{X}} P(x) \log_2 P(x),

Biased Coin Trace: Let's calculate and compare the entropy of a fair coin vs. a heavily biased coin:

- *Fair Coin:* P(H)=0.5,P(T)=0.5P(H) = 0.5, P(T) = 0.5. The entropy is: H(X)=(0.5log20.5+0.5log20.5)=(0.5(1)+0.5(1))=1.0 bitH(X) = -(0.5 \log_2 0.5 + 0.5 \log_2 0.5) = -(0.5(-1) + 0.5(-1)) = 1.0 \text{ bit} This represents maximum uncertainty—we have no prior bias about the toss.

- *Biased Coin:* P(H)=0.8,P(T)=0.2P(H) = 0.8, P(T) = 0.2. The entropy is: H(X)=(0.8log20.8+0.8log20.8)(0.8(0.32)+0.2(2.32))0.256+0.464=0.72 bitsH(X) = -(0.8 \log_2 0.8 + 0.8 \log_2 0.8) \approx -(0.8(-0.32) + 0.2(-2.32)) \approx 0.256 + 0.464 = 0.72 \text{ bits} Because the coin is heavily biased, we are less 'surprised' by the outcomes on average, reducing our statistical entropy from 1.01.0 down to 0.720.72 bits!

Joint Entropy (H(X,Y)H(X, Y)): Measures the combined total uncertainty in a pair of random variables XX and YY simultaneously: H(X,Y)=xyP(x,y)log2P(x,y)H(X, Y) = -\sum_{x} \sum_{y} P(x, y) \log_2 P(x, y)

Conditional Entropy (H(YX)H(Y | X)): Measures the remaining uncertainty of variable YY given that we already know the exact value of variable XX: H(YX)=xyP(x,y)log2P(yx)H(Y | X) = -\sum_{x} \sum_{y} P(x, y) \log_2 P(y | x)

2. KL Divergence and Cross-Entropy Loss

In machine learning, models output predicted probability distributions (QQ) trying to approximate the true target distribution (PP). We need to measure how much these two distributions differ.

Kullback-Leibler (KL) Divergence (DKL(PQ)D_{\text{KL}}(P \parallel Q)): Measures the extra information/bits required to represent data using distribution QQ instead of the true distribution PP. It acts as an asymmetric distance measure (DKL(PQ)DKL(QP)D_{\text{KL}}(P \parallel Q) \neq D_{\text{KL}}(Q \parallel P)): DKL(PQ)=xXP(x)logP(x)Q(x)D_{\text{KL}}(P \parallel Q) = \sum_{x \in \mathcal{X}} P(x) \log \frac{P(x)}{Q(x)}

Cross-Entropy (H(P,Q)H(P, Q)): Measures the average bits needed to encode symbols from true distribution PP using code model QQ. It is mathematically equal to the sum of the true distribution's entropy and the KL divergence: H(P,Q)=H(P)+DKL(PQ)=xXP(x)logQ(x)H(P, Q) = H(P) + D_{\text{KL}}(P \parallel Q) = -\sum_{x \in \mathcal{X}} P(x) \log Q(x)

Cross-Entropy Loss Calculation: Suppose we have a binary classification task. The true label of an image is P=[1.0,0.0]P = [1.0, 0.0] (it is 100%100\% a cat). Our neural network outputs a prediction Q=[0.8,0.2]Q = [0.8, 0.2] (the model is 80%80\% confident it is a cat). Let's calculate the Cross-Entropy Loss (using natural logarithms): H(P,Q)=(1.0ln(0.8)+0.0ln(0.2))=1.0×(0.223)+0=0.223 natsH(P, Q) = -\left( 1.0 \ln(0.8) + 0.0 \ln(0.2) \right) = -1.0 \times (-0.223) + 0 = 0.223 \text{ nats} If our model was less confident (e.g. Q=[0.5,0.5]Q = [0.5, 0.5]), the loss would rise to ln(0.5)=0.693-\ln(0.5) = 0.693 nats. In deep classification networks, minimizing the cross-entropy loss function is mathematically equivalent to minimizing the KL divergence, forcing our predictions QQ to match true targets PP!

3. Information Gain and Decision Tree Splitting

Information theory also guides how classical algorithms build models, such as Decision Trees.

When a Decision Tree decides which feature column to split on, it chooses the feature that decreases dataset entropy the most. This reduction in entropy is called Information Gain (IG). High Information Gain means the resulting child split partitions are highly homogeneous (pure) compared to the unsplit parent dataset: IG(T,a)=H(T)H(Ta)\text{IG}(T, a) = H(T) - H(T | a)

Interactive Practice Quiz

Test your understanding with instant feedback

QUESTION 01

Calculate the Shannon Entropy H(X)H(X) of a fair, binary coin flip (where success probability P(H)=0.50P(H) = 0.50 and P(T)=0.50P(T) = 0.50) using base-2 logarithms:

QUESTION 02

Which of the following describes why the Kullback-Leibler (KL) Divergence is NOT a true mathematical metric/distance?

QUESTION 03

In neural classification models, minimizing the Cross-Entropy loss H(P,Q)H(P, Q) between the true labels PP and predicted probabilities QQ is mathematically equivalent to minimizing which of the following?

QUESTION 04

In decision tree models, what does a high 'Information Gain' value for a specific feature split indicate?

QUESTION 05

What is the Shannon Entropy H(X)H(X) of a completely deterministic event (where success probability P(A)=1.0P(A) = 1.0)?

QUESTION 06

Calculate the Shannon Entropy H(X)H(X) of a biased coin where the probability of heads is P(H)=0.80P(H) = 0.80 and tails is P(T)=0.20P(T) = 0.20 (using natural logarithms, so the unit is 'nats'):

QUESTION 07

Which of the following formulas represents the Mutual Information I(X;Y)I(X; Y) between two random variables?

QUESTION 08

If we have a true label distribution PP and model prediction QQ, which expression represents the Cross-Entropy H(P,Q)H(P, Q)?

QUESTION 09

What is the relation between Joint Entropy H(X,Y)H(X, Y), individual Entropy H(X)H(X), and Conditional Entropy H(YX)H(Y | X)?

QUESTION 10

Which of the following is true about Kullback-Leibler (KL) Divergence DKL(PQ)D_{\text{KL}}(P \parallel Q)?