Entropy and KL Divergence
Entropy
Shannon entropy measures the uncertainty or information content of a random variable.
Discrete Entropy
For a discrete random variable $X$ with PMF $P(X)$:
Properties:
- $H(X) \geq 0$ (non-negative)
- $H(X) = 0$ iff $X$ is deterministic
- Maximized when $X$ is uniform: $H_{\text{max}} = \log \lvert\mathcal{X}\rvert$
- Base of logarithm: $\log_2$ gives bits, $\ln$ gives nats
Differential Entropy
For a continuous random variable $X$ with PDF $f(x)$:
Differences from discrete entropy:
- Can be negative (e.g., narrow Gaussian)
- Not invariant to coordinate transformations
- Requires Jacobian correction under change of variables
Entropy of Common Distributions
| Distribution | Entropy |
|---|---|
| Bernoulli($p$) | $-p \log p - (1-p) \log(1-p)$ |
| Uniform($a, b$) | $\log(b - a)$ |
| Normal($\mu, \sigma^2$) | $\frac{1}{2} \log(2\pi e \sigma^2)$ |
| Exponential($\lambda$) | $1 - \log \lambda$ |
Joint and Conditional Entropy
Joint Entropy
Uncertainty in multiple variables together:
Conditional Entropy
Uncertainty in $Y$ given knowledge of $X$:
Chain rule:
Property: $H(Y \mid X) \leq H(Y)$ (conditioning reduces entropy)
Equality holds iff $X$ and $Y$ are independent.
Mutual Information
Mutual information (MI) measures the amount of information shared between two variables:
Equivalent expressions:
Properties:
- $I(X; Y) \geq 0$ (non-negative)
- $I(X; Y) = 0$ iff $X$ and $Y$ are independent
- Symmetric: $I(X; Y) = I(Y; X)$
- $I(X; X) = H(X)$ (self-information equals entropy)
Conditional Mutual Information
Information shared between $X$ and $Y$ given knowledge of $Z$.
KL Divergence
Kullback-Leibler divergence measures how much one distribution diverges from another.
Definition
For discrete distributions $P$ and $Q$:
For continuous distributions:
Properties
- $D_{\text{KL}}(P \Vert Q) \geq 0$ (Gibbs’ inequality)
- $D_{\text{KL}}(P \Vert Q) = 0$ iff $P = Q$ (almost everywhere)
- Not symmetric: $D_{\text{KL}}(P \Vert Q) \neq D_{\text{KL}}(Q \Vert P)$ in general
- Not a metric: doesn’t satisfy triangle inequality
Relationship to Entropy and Cross-Entropy
Cross-entropy:
Relationship:
Minimizing cross-entropy is equivalent to minimizing KL divergence (since $H(P)$ is constant).
KL Divergence for Common Distributions
Bernoulli:
Normal:
Multivariate Normal:
Forward vs Reverse KL
Forward KL ($D_{\text{KL}}(P \Vert Q)$)
- Zero-avoiding: $Q$ must cover all regions where $P$ has mass
- Tends to overestimate the support of $P$
- Used in: maximum likelihood, variational inference (variational distribution approximates true posterior)
Reverse KL ($D_{\text{KL}}(Q \Vert P)$)
- Zero-forcing: $Q$ concentrates on high-probability regions of $P$
- Tends to underestimate the support of $P$
- Used in: EM algorithm, reinforcement learning (policy optimization)
Jensen-Shannon Divergence
Symmetrized, smoothed version of KL divergence:
where $M = \frac{1}{2}(P + Q)$ (mixture distribution).
Properties:
- Always finite (unlike KL)
- Symmetric: $\text{JSD}(P \Vert Q) = \text{JSD}(Q \Vert P)$
- Bounded: $0 \leq \text{JSD} \leq \log 2$ (for $\log_2$)
- $\sqrt{\text{JSD}}$ is a proper metric
Cross-Entropy in Machine Learning
Classification Loss
For true label $y$ (one-hot) and predicted probabilities $\hat{y}$:
Binary classification:
Multi-class classification:
Minimizing cross-entropy loss = maximizing log-likelihood = minimizing KL divergence between true and predicted distributions.
Applications in Information Theory
Source Coding Theorem
The minimum expected code length to encode samples from $P$ is $H(P)$ bits.
Using a code optimized for $Q$ when the true distribution is $P$ gives expected length $H(P, Q)$.
Rate-Distortion Theory
Minimum rate (bits) needed to represent a source within distortion $D$:
Applications in Machine Learning
Variational Inference
Approximate intractable posterior $P(\theta \mid D)$ with variational distribution $q(\theta)$:
ELBO (Evidence Lower Bound):
Variational Autoencoders (VAEs)
KL divergence regularizes the latent space:
Expectation-Maximization (EM)
E-step: Compute expected complete-data log-likelihood. M-step: Maximize w.r.t. parameters.
Equivalent to minimizing reverse KL divergence.
Information Bottleneck
Compress input $X$ while preserving information about target $Y$:
Trade-off between compression and prediction.
Contrastive Learning
InfoNCE loss estimates mutual information:
Lower bound on mutual information between representations.