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.
Goal: Design a measure of "average surprise" that satisfies:
Additivity: Information from independent events adds up
Monotonicity: Rarer events carry more surprise
Continuity: Small probability changes → small entropy changes
Step 1: Surprise for a single outcome
If outcome x has probability p(x), define its surprisal (information content):
I(x)=−log2p(x)
Why this step? We need surprise to decrease as probability increases. A certain event (p=1) should have zero surprise: −log2(1)=0. A rare event (p=0.01) has high surprise: −log2(0.01)≈6.64 bits. The log makes independent events' information add: I(x,y)=I(x)+I(y) when x,y independent.
Step 2: Average surprise over all outcomes
H(X)=Ex∼p[I(x)]=∑xp(x)⋅(−log2p(x))=−∑xp(x)log2p(x)
Why this step? Entropy is the expected information you gain when you observe X. Weighted average by probability.
Proof of Maximum Entropy (uniform distribution achieves it):
Let u(x)=n1 be the uniform distribution over n outcomes. Use Gibs' inequality: for any two distributions p,q,
−∑xp(x)logp(x)≤−∑xp(x)logq(x)
with equality iff p=q.
Set q=u:
H(X)=−∑xp(x)logp(x)≤−∑xp(x)logn1=logn
Equality when p(x)=n1 for all x.
Context: You're designing an optimal code for distribution p, but you accidentally use the code optimized for q. How much extra information do you transmit?
Step 1: Optimal code length for p
Usingp′scode,averagemessagelength=H(p) = -\sum_x p(x) \log p(x)$
Step 2: Actual code length when using q's code
If you encode using q's code (which assigns −logq(x) bits to outcome x), but outcomes follow p, average length = −∑xp(x)logq(x) (called cross-entropy H(p,q)).
Step 3: Extra cost
DKL(p∥q)=H(p,q)−H(p)=−∑xp(x)logq(x)−(−∑xp(x)logp(x))=∑xp(x)[logp(x)−logq(x)]=∑xp(x)logq(x)p(x)
Why this step? Cross-entropy is the code length under q, entropy is the optimal length under p. The difference is the inefficiency.
Proof of Non-negativity (Gibs' inequality):
DKL(p∥q)=−∑xp(x)logp(x)q(x)
Use Jensen's inequality on the concave function log:
−∑xp(x)logp(x)q(x)≥−log(∑xp(x)p(x)q(x))=−log(∑xq(x))=−log1=0
Equality iff p(x)q(x) is constant, i.e., p=q.
In machine learning, when p is the true label distribution (often one-hot) and q is the model's predicted probabilities, minimizing cross-entropy = minimizing KL divergence (since H(p) is constant).
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.
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). 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 p) yeh hai: 70% rain, 30% sunny. Lekin tumhari ML model galat seekhi hai aur predict karti hai 50-50 (q). 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(p∥q) exactly yeh cost measure karta hai: kitne "extra bits" of information waste ho rahe hain jab tum sahi distribution p ke jagah galat distribution q use karte ho. Important baat: KL symmetric nahi hai, matlab DKL(p∥q)=DKL(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.