1.3.18Probability & Statistics

Entropy and KL divergence

2,438 words11 min readdifficulty · medium6 backlinks

Overview

Entropy measures the average uncertainty or information content in a probability distribution. KL divergence measures how one probability distribution differs from another. Both are foundational to machine learning: entropy guides decision trees, loss functions, and compression; KL divergence powers variational inference, GANs, and policy optimization.

Figure — Entropy and KL divergence

Shannon Entropy

Derivation from First Principles

Goal: Design a measure of "average surprise" that satisfies:

  1. Additivity: Information from independent events adds up
  2. Monotonicity: Rarer events carry more surprise
  3. Continuity: Small probability changes → small entropy changes

Step 1: Surprise for a single outcome

  • If outcome xx has probability p(x)p(x), define its surprisal (information content): I(x)=log2p(x)I(x) = -\log_2 p(x)

Why this step? We need surprise to decrease as probability increases. A certain event (p=1p=1) should have zero surprise: log2(1)=0-\log_2(1) = 0. A rare event (p=0.01p=0.01) has high surprise: log2(0.01)6.64-\log_2(0.01) \approx 6.64 bits. The log makes independent events' information add: I(x,y)=I(x)+I(y)I(x,y) = I(x) + I(y) when x,yx,y independent.

Step 2: Average surprise over all outcomes H(X)=Exp[I(x)]=xp(x)(log2p(x))=xp(x)log2p(x)H(X) = \mathbb{E}_{x \sim p}[I(x)] = \sum_{x} p(x) \cdot (-\log_2 p(x)) = -\sum_{x} p(x) \log_2 p(x)

Why this step? Entropy is the expected information you gain when you observe XX. Weighted average by probability.

Proof of Maximum Entropy (uniform distribution achieves it): Let u(x)=1nu(x) = \frac{1}{n} be the uniform distribution over nn outcomes. Use Gibs' inequality: for any two distributions p,qp, q, xp(x)logp(x)xp(x)logq(x)-\sum_x p(x) \log p(x) \leq -\sum_x p(x) \log q(x) with equality iff p=qp = q.

Set q=uq = u: H(X)=xp(x)logp(x)xp(x)log1n=lognH(X) = -\sum_x p(x) \log p(x) \leq -\sum_x p(x) \log \frac{1}{n} = \log n Equality when p(x)=1np(x) = \frac{1}{n} for all xx.


Kullback-Leibler (KL) Divergence

Derivation: Why This Formula?

Context: You're designing an optimal code for distribution pp, but you accidentally use the code optimized for qq. How much extra information do you transmit?

Step 1: Optimal code length for pp Usingpscode,averagemessagelength='s code, average message length = H(p) = -\sum_x p(x) \log p(x)$

Step 2: Actual code length when using qq's code If you encode using qq's code (which assigns logq(x)-\log q(x) bits to outcome xx), but outcomes follow pp, average length = xp(x)logq(x)-\sum_x p(x) \log q(x) (called cross-entropy H(p,q)H(p,q)).

Step 3: Extra cost DKL(pq)=H(p,q)H(p)=xp(x)logq(x)(xp(x)logp(x))D_{KL}(p \| q) = H(p,q) - H(p) = -\sum_x p(x) \log q(x) - \left(-\sum_x p(x) \log p(x)\right) =xp(x)[logp(x)logq(x)]=xp(x)logp(x)q(x)= \sum_x p(x) [\log p(x) - \log q(x)] = \sum_x p(x) \log \frac{p(x)}{q(x)}

Why this step? Cross-entropy is the code length under qq, entropy is the optimal length under pp. The difference is the inefficiency.

Proof of Non-negativity (Gibs' inequality): DKL(pq)=xp(x)logq(x)p(x)D_{KL}(p \| q) = -\sum_x p(x) \log \frac{q(x)}{p(x)} Use Jensen's inequality on the concave function log\log: xp(x)logq(x)p(x)log(xp(x)q(x)p(x))=log(xq(x))=log1=0-\sum_x p(x) \log \frac{q(x)}{p(x)} \geq -\log \left(\sum_x p(x) \frac{q(x)}{p(x)}\right) = -\log \left(\sum_x q(x)\right) = -\log 1 = 0 Equality iff q(x)p(x)\frac{q(x)}{p(x)} is constant, i.e., p=qp = q.


Cross-Entropy

In machine learning, when pp is the true label distribution (often one-hot) and qq is the model's predicted probabilities, minimizing cross-entropy = minimizing KL divergence (since H(p)H(p) is constant).


Applications in Machine Learning

  1. Decision Trees: Split criterion uses information gain = entropy reduction Gain=H(parent)childrenchildparentH(child)\text{Gain} = H(\text{parent}) - \sum_{\text{children}} \frac{|child|}{|parent|} H(\text{child})

  2. Classification: Cross-entropy loss for neural networks L=iyilogy^i\mathcal{L} = -\sum_{i} y_i \log \hat{y}_i where yy is true distribution (one-hot), y^\hat{y} is predicted probabilities.

  3. Variational Inference: Minimize DKL(qϕ(z)p(zx))D_{KL}(q\phi(z) \| p(z|x)) to approximate posterior

  4. GANs: Discriminator maximizes cross-entropy; generator minimizes JS divergence (related to KL)

  5. Reinforcement Learning: Policy gradient methods use KL penalties to prevent policy collapse


Recall Explain to a 12-year-old

Entropy: Imagine you have a bag of marbles. If all marbles are red, reaching in blindfolded gives you no surprise—you always know what you'll get. That's zero entropy, like a boring predictable story. But if the bag has equal red, blue, and green marbles, you're maximally surprised each time—high entropy, like an exciting mystery novel. Entropy measures how surprising or unpredictable something is. KL Divergence: Now imagine you made a guidebook for the marble game, but you got the marble counts wrong. If your guidebook says "mostly red" but the bag is actually "mostly blue," people following your guide will be confused and waste effort. KL divergence measures how wrong your guidebook is—the extra confusion caused by using bad information. It's always zero or positive (you can't be "negatively wrong"), and it's zero only when your guidebook is perfect.


Connections

  • Mutual Information - Uses entropy: I(X;Y)=H(X)+H(Y)H(X,Y)I(X;Y) = H(X) + H(Y) - H(X,Y)
  • Maximum Entropy Principle - Justifies probability distributions
  • Cross-Entropy Loss - Direct application in neural networks
  • Variational Autoencoders - ELBO = reconstruction - KL divergence
  • Jensen-Shannon Divergence - Symmetric version of KL
  • Information Gain - Entropy reduction in decision trees
  • Evidence Lower Bound (ELBO) - Uses KL divergence for Bayesian inference
  • F-divergences - Family of divergences including KL

#flashcards/ai-ml

What is Shannon entropy? :: The average amount of information (in bits) gained when observing a random variable; H(X)=xp(x)log2p(x)H(X) = -\sum_x p(x) \log_2 p(x)

What does high entropy indicate?
High uncertainty or unpredictability in the distribution; outcomes are more "surprising" on average
What is the maximum possible entropy for a discrete variable with nn outcomes?
log2n\log_2 n bits, achieved by the uniform distribution where each outcome has probability 1n\frac{1}{n}
What is KL divergence DKL(pq)D_{KL}(p \| q)?
A measure of how distribution pp differs from distribution qq; the extra bits needed when using qq's code for data drawn from pp
Is KL divergence symmetric?
No, DKL(pq)DKL(qp)D_{KL}(p \| q) \neq D_{KL}(q \| p) in general; it measures directional difference
What is the minimum value of KL divergence and when is it achieved?
Minimum is 0, achieved if and only if p=qp = q almost everywhere
How does cross-entropy relate to KL divergence?
DKL(pq)=H(p,q)H(p)D_{KL}(p \| q) = H(p, q) - H(p) where H(p,q)=xp(x)logq(x)H(p,q) = -\sum_x p(x) \log q(x) is cross-entropy
Why do we minimize cross-entropy in classification?
Minimizing cross-entropy = minimizing KL divergence from true labels to predictions (since H(p)H(p) is constant)
What does entropy of0 mean?
The variable is deterministic (no uncertainty); one outcome has probability 1
What is the entropy of a fair coin flip?
Exactly 1 bit, since H(X)=[0.5log20.5+0.5log20.5]=1H(X) = -[0.5 \log_2 0.5 + 0.5 \log_2 0.5] = 1

Concept Map

averaged over outcomes

motivates log

rarer = more surprise

non-negative, max at uniform

proves

special case

measures avg uncertainty

measures difference between

underlies

guides

powers

Surprisal I x = -log p x

Shannon Entropy H X

Additivity axiom

Monotonicity axiom

Key Properties

Gibbs inequality

Max Entropy = log n

Uncertainty & Info Content

KL Divergence

Two Distributions

Decision Trees & Loss

Variational Inference & GANs

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Entropy aur KL Divergence: Information Theory ki Nenv

Socho tumhare pas ek dice hai. Agar dice ka har face barabar probability ke sath ata hai (1/6 each), toh tumhe maximum surprise milta hai har roll pe—yeh high entropy hai. Lekin agar dice biased hai aur 6 hamesha ata hai, toh koi surprise nahi—zero entropy. Shannon entropy basically yeh measure karta hai ki ek probability distribution kitna "uncertain" ya "surprising" hai. Formula simple hai: H(X)=p(x)logp(x)H(X) = -\sum p(x) \log p(x). Jitna zyada spread out distribution, utna zyada entropy, utna zyada information gain jab tum actual outcome dekhte ho.

Ab KL divergence ko samjho ek example se. Maan lo real weather pattern (true distribution pp) yeh hai: 70% rain, 30% sunny. Lekin tumhari ML model galat seekhi hai aur predict karti hai 50-50 (qq). Jab tum is galat model se decisions lo (jaise umbrella leke jana ya nahi), tum extra cost pay karte ho—zyada galat predictions, zyada wrong moves. KL divergence DKL(pq)D_{KL}(p \| q) exactly yeh cost measure karta hai: kitne "extra bits" of information waste ho rahe hain jab tum sahi distribution pp ke jagah galat distribution qq use karte ho. Important baat: KL symmetric nahi hai, matlab DKL(pq)DKL(qp)D_{KL}(p \| q) \neq D_{KL}(q \| p). Direction matter karta hai—"using q when truth is p" alag hai "using p when truth is q" se.

Machine learning mein yeh dono concepts har jagah dikhte hain. Classification loss (cross-entropy) actually KL divergence minimize kar raha hota hai between true labels aur predicted probabilities. Decision trees information gain use karte hain jo entropy decrease measure karta hai. Variational Autoencoders (VAE) apne latent distribution ko true posterior ke pas lane ke liye KL penalty add karte hain. Jab bhi tumhe yeh samajhna ho ki "kitna uncertain hai system" ya "kitna galat hai mera model," entropy aur KL divergence tumhare go-to tools hain. Intuition pakad lo: entropy = surprise level, KL = wrongness cost.

Go deeper — visual, from zero

Test yourself — Probability & Statistics

Connections