Skip to main content
Open In Colab You already compare distributions with the KL divergence. Maximum likelihood minimizes it, and the ELBO of a variational autoencoder contains it. This section shows a case where KL gives a poor answer to the question of how far apart two densities are. It then derives the Wasserstein distance from transport cost. Everything here uses two Gaussians on the real line, where each quantity has a closed form. Transport maps and couplings carries the same ideas to a mixture target, where no closed form exists.

Maximum likelihood and the KL divergence

Maximum likelihood fits a model qθq_\theta to data drawn from a density pp by maximizing the expected log-likelihood Ex∼p[log⁡qθ(x)]\mathbb{E}_{x \sim p}[\log q_\theta(x)]. The negative of this quantity is the cross-entropy H(p,qθ)H(p, q_\theta), and Entropy and Maximum likelihood estimation of a marginal model show that it splits into two terms: H(p,qθ)=H(p)+KL(p ∥ qθ).H(p, q_\theta) = H(p) + \mathrm{KL}(p \,\|\, q_\theta). Since the entropy H(p)H(p) does not depend on θ\theta, maximizing the likelihood minimizes KL(p ∥ qθ)\mathrm{KL}(p \,\|\, q_\theta).

The KL divergence between two Gaussians

For two Gaussians p=N(μp,σp2)p = \mathcal{N}(\mu_p, \sigma_p^2) and q=N(μq,σq2)q = \mathcal{N}(\mu_q, \sigma_q^2) the KL divergence has a closed form, KL(p ∥ q)=log⁡σqσp+σp2+(μp−μq)22σq2−12.\mathrm{KL}(p \,\|\, q) = \log \frac{\sigma_q}{\sigma_p} + \frac{\sigma_p^2 + (\mu_p - \mu_q)^2}{2 \sigma_q^2} - \frac{1}{2}. It is not symmetric. For N(0,1)\mathcal{N}(0, 1) and N(0,4)\mathcal{N}(0, 4) the two orders give 0.32 and 0.81, as the check below prints. When the two widths are equal, σp=σq=σ\sigma_p = \sigma_q = \sigma, the logarithm vanishes, the variance term cancels the constant, and KL(p ∥ q)=(μp−μq)22σ2.\mathrm{KL}(p \,\|\, q) = \frac{(\mu_p - \mu_q)^2}{2 \sigma^2}. The code below defines the closed forms used in this section and checks the KL values.

Where KL falls short

Hold the gap between the means at Δμ=μq−μp=2\Delta\mu = \mu_q - \mu_p = 2 and shrink the common width σ\sigma. The two densities stay the same distance apart, and only their width changes. The KL divergence grows as 1/σ21/\sigma^2 all the same: it is 0.5 at σ=2\sigma = 2, 2 at σ=1\sigma = 1, 8 at σ=0.5\sigma = 0.5 and 200 at σ=0.1\sigma = 0.1. KL compares the two densities point by point through the ratio p(x)/q(x)p(x)/q(x). Narrow Gaussians overlap less, so the ratio becomes extreme where pp has its mass. In the limit of two point masses at 0 and 2 the KL divergence is infinite. It is also infinite for point masses at 0 and 0.001. KL depends only on the two density values at each point, so the distance the mass would have to move to turn one density into the other does not enter it. Four panels in a two by two grid, each with two Gaussian densities centered at 0 and 2 with a common standard deviation of 2, 1, 0.5 and 0.1 read row by row. The bumps narrow and separate while their centers stay 2 apart. Panel titles give the KL divergence as 0.5, 2, 8 and 200, and W2 as 2 in every panel.

Couplings of two Gaussians

To measure how far mass moves, you have to say where each piece of mass goes. From here on the source is p0p_0 and the target is p1p_1, the names the rest of the chapter uses. Take p0=N(0,1)p_0 = \mathcal{N}(0, 1) and p1=N(2,1)p_1 = \mathcal{N}(2, 1). A coupling π\pi of p0p_0 and p1p_1 is a joint distribution of a pair (X,Y)(X, Y) with X∼p0X \sim p_0 and Y∼p1Y \sim p_1. It pairs source points with target points. Its cost is the expected squared distance the mass travels, Eπ[(X−Y)2]\mathbb{E}_\pi[(X - Y)^2]. Many couplings share the same two marginals. For any of them, with ρ\rho the correlation between XX and YY, expanding the square gives E[(X−Y)2]=(μ0−μ1)2+σ02+σ12−2ρ σ0σ1=6−2ρfor these two Gaussians.\begin{aligned} \mathbb{E}[(X - Y)^2] &= (\mu_0 - \mu_1)^2 + \sigma_0^2 + \sigma_1^2 - 2 \rho \, \sigma_0 \sigma_1 \\ &= 6 - 2\rho \quad \text{for these two Gaussians.} \end{aligned} The independent coupling, ρ=0\rho = 0, costs 6. A positive correlation sends low source points to low target points and costs less, 4.4 at ρ=0.8\rho = 0.8. A negative correlation crosses the pairs and costs more, 7.6 at ρ=−0.8\rho = -0.8. The figure shows samples from four bivariate Gaussian couplings, and the check after it compares each formula with a sample average. Four scatter plots in a two by two grid of sample pairs with source x on the horizontal axis and target y on the vertical axis. With correlation 0 the cloud is round, cost 6. With 0.8 it tilts upward, cost 4.4. With minus 0.8 it tilts downward, cost 7.6. With correlation 1 every point lies on the line y = x + 2, cost 4.

The Wasserstein distance between Gaussians

The cost decreases as ρ\rho grows, and by the Cauchy-Schwarz inequality a correlation never exceeds 1. So over all couplings the cost is smallest at ρ=1\rho = 1, where YY is an increasing affine function of XX. There the cost is (μ0−μ1)2+(σ0−σ1)2(\mu_0 - \mu_1)^2 + (\sigma_0 - \sigma_1)^2. This smallest cost defines the squared Wasserstein distance, W22(p0,p1)=min⁡π  Eπ[(X−Y)2].W_2^2(p_0, p_1) = \min_{\pi} \; \mathbb{E}_\pi\bigl[(X - Y)^2\bigr]. For two Gaussians on the line the minimum is the cost of the ρ=1\rho = 1 coupling, so W22(p0,p1)=(μ0−μ1)2+(σ0−σ1)2.W_2^2(p_0, p_1) = (\mu_0 - \mu_1)^2 + (\sigma_0 - \sigma_1)^2 . W2W_2 is the Euclidean distance between the points (μ0,σ0)(\mu_0, \sigma_0) and (μ1,σ1)(\mu_1, \sigma_1) in the plane of means and standard deviations (Remark 2.31 in (Peyré & Cuturi, 2018)). Transport maps and couplings shows that the increasing pairing is optimal in 1D for any two densities. The optimal coupling has no density. All of its mass lies on a line in the (x,y)(x, y) plane, as the last panel of the figure shows. A coupling only has to have the right two marginals, so nothing stops it from concentrating on a line.

The transport map

At ρ=1\rho = 1 each source point goes to exactly one target point. The coupling is the graph of the map T(x)=μ1+σ1σ0(x−μ0),T(x) = \mu_1 + \frac{\sigma_1}{\sigma_0} (x - \mu_0), which shifts and rescales the source. If X∼p0X \sim p_0, then T(X)T(X) has mean μ1\mu_1 and standard deviation σ1\sigma_1, so TT carries p0p_0 onto p1p_1. The map is increasing, and it sends the point at quantile level uu of p0p_0 to the point at the same level of p1p_1. It is the quantile map T=F1−1∘F0T = F_1^{-1} \circ F_0, which solves Monge’s problem in 1D. Transport maps and couplings computes that map numerically for the mixture target, where no formula exists. Moving each point at constant speed along the straight line from xx to T(x)T(x) gives the path of densities studied in Dynamic optimal transport. The check below confirms the W2W_2 values and the map. For N(0,1)\mathcal{N}(0, 1) and N(2,32)\mathcal{N}(2, 3^2) the formula gives W22=4+4=8W_2^2 = 4 + 4 = 8, and the squared distance between the two quantile functions, averaged over levels uu, gives the same number.

KL and Wasserstein compared

Repeat the sweep from the KL section with both measures. W2W_2 stays at 2 for every width, because the mass moves the same distance. KL grows from 0.5 to 200. Log-log plot of KL divergence and W2 against the common standard deviation from 0.1 to 2, for two Gaussians with means 2 apart. KL falls along a straight line from 200 to 0.5. W2 is a flat line at 2. Maximum likelihood fits a model through KL, which compares values of densities. The Wasserstein distance depends on how far apart points are on the line, so it carries the geometry of the space. Next, Transport maps and couplings solves the problem for the mixture target. Dynamic optimal transport then moves the mass along a path in time.

References

  • Peyré, G., Cuturi, M. (2018). Computational Optimal Transport. arXiv [stat.ML]. DOI

Found an error on this page? Report an issue.