Loading content...
In probability theory and information theory, the Kullback-Leibler (KL) divergence is a fundamental measure that quantifies how one probability distribution differs from a reference probability distribution. It originated from information theory and answers the question: "How much information is lost when we use distribution Q to approximate distribution P?"
For univariate Gaussian (normal) distributions, the KL divergence has a elegant closed-form solution. Given two Gaussian distributions:
The KL divergence from P to Q is defined as:
$$D_{KL}(P | Q) = \log\left(\frac{\sigma_Q}{\sigma_P}\right) + \frac{\sigma_P^2 + (\mu_P - \mu_Q)^2}{2\sigma_Q^2} - \frac{1}{2}$$
Key Properties of KL Divergence:
Intuitive Understanding:
Think of KL divergence as measuring the "surprise" or "extra bits" needed when you expect data from distribution Q but it actually comes from distribution P. If Q is a poor approximation of P, you'll be "surprised" more often, resulting in a higher divergence value.
Your Task:
Implement a function that computes the KL divergence between two univariate Gaussian distributions. The function should accept the mean and standard deviation of both distributions and return the KL divergence value, rounded to 4 decimal places.
mu_p = 0.0
sigma_p = 1.0
mu_q = 1.0
sigma_q = 1.00.5Both distributions have the same standard deviation (σ = 1.0), but their means differ by 1.0 unit. When substituting into the KL divergence formula:
• log(σ_Q / σ_P) = log(1.0 / 1.0) = log(1) = 0 • (σ_P² + (μ_P - μ_Q)²) / (2σ_Q²) = (1 + 1) / (2 × 1) = 2/2 = 1 • Subtracting 0.5: 0 + 1 - 0.5 = 0.5
The KL divergence of 0.5 indicates moderate divergence due solely to the difference in means.
mu_p = 0.0
sigma_p = 1.0
mu_q = 0.0
sigma_q = 1.00.0When both distributions have identical parameters (same mean and same standard deviation), they are the same distribution. The KL divergence between identical distributions is always 0, representing no information loss.
• log(σ_Q / σ_P) = log(1/1) = 0 • (σ_P² + (μ_P - μ_Q)²) / (2σ_Q²) = (1 + 0) / 2 = 0.5 • Result: 0 + 0.5 - 0.5 = 0.0
This confirms the fundamental property that D_KL(P || P) = 0.
mu_p = 2.0
sigma_p = 1.0
mu_q = 2.0
sigma_q = 2.00.3181Here, both distributions share the same mean (μ = 2.0), but their standard deviations differ. Distribution Q has twice the spread of distribution P.
• log(σ_Q / σ_P) = log(2.0 / 1.0) = log(2) ≈ 0.6931 • (σ_P² + (μ_P - μ_Q)²) / (2σ_Q²) = (1 + 0) / (2 × 4) = 1/8 = 0.125 • Result: 0.6931 + 0.125 - 0.5 ≈ 0.3181
The divergence arises entirely from the difference in variance, not the means.
Constraints