KL Divergence – Calculating Between Two Univariate Gaussian Distributions

kullback-leiblernormal distribution

I need to determine the KL-divergence between two Gaussians. I am comparing my results to these, but I can't reproduce their result. My result is obviously wrong, because the KL is not 0 for KL(p, p).

I wonder where I am doing a mistake and ask if anyone can spot it.

Let $p(x) = N(\mu_1, \sigma_1)$ and $q(x) = N(\mu_2, \sigma_2)$. From Bishop's
PRML I know that

$$KL(p, q) = – \int p(x) \log q(x) dx + \int p(x) \log p(x) dx$$

where integration is done over all real line, and that

$$\int p(x) \log p(x) dx = -\frac{1}{2} (1 + \log 2 \pi \sigma_1^2),$$

so I restrict myself to $\int p(x) \log q(x) dx$, which I can write out as

$$-\int p(x) \log \frac{1}{(2 \pi \sigma_2^2)^{(1/2)}} e^{-\frac{(x-\mu_2)^2}{2 \sigma_2^2}} dx,$$

which can be separated into

$$\frac{1}{2} \log (2 \pi \sigma_2^2) – \int p(x) \log e^{-\frac{(x-\mu_2)^2}{2 \sigma_2^2}} dx.$$

Taking the log I get

$$\frac{1}{2} \log (2 \pi \sigma_2^2) – \int p(x) \bigg(-\frac{(x-\mu_2)^2}{2 \sigma_2^2} \bigg) dx,$$

where I separate the sums and get $\sigma_2^2$ out of the integral.

$$\frac{1}{2} \log (2 \pi \sigma^2_2) + \frac{\int p(x) x^2 dx – \int p(x) 2x\mu_2 dx + \int p(x) \mu_2^2 dx}{2 \sigma_2^2}$$

Letting $\langle \rangle$ denote the expectation operator under $p$, I can rewrite this as

$$\frac{1}{2} \log (2 \pi \sigma_2^2) + \frac{\langle x^2 \rangle – 2 \langle x \rangle \mu_2 + \mu_2^2}{2 \sigma_2^2}.$$

We know that $var(x) = \langle x^2 \rangle – \langle x \rangle ^2$. Thus

$$\langle x^2 \rangle = \sigma_1^2 + \mu_1^2$$

and therefore

$$\frac{1}{2} \log (2 \pi \sigma_2^2) + \frac{\sigma_1^2 + \mu_1^2 – 2 \mu_1 \mu_2 + \mu_2^2}{2 \sigma_2^2},$$

which I can put as

$$\frac{1}{2} \log (2 \pi \sigma_2^2) + \frac{\sigma_1^2 + (\mu_1 – \mu_2)^2}{2 \sigma_2^2}.$$

Putting everything together, I get to

\begin{align*}
KL(p, q) &= – \int p(x) \log q(x) dx + \int p(x) \log p(x) dx\\\\
&= \frac{1}{2} \log (2 \pi \sigma_2^2) + \frac{\sigma_1^2 + (\mu_1 – \mu_2)^2}{2 \sigma_2^2} – \frac{1}{2} (1 + \log 2 \pi \sigma_1^2)\\\\
&= \log \frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 – \mu_2)^2}{2 \sigma_2^2}.
\end{align*}

Which is wrong since it equals $1$ for two identical Gaussians.

Can anyone spot my error?

Update

Thanks to mpiktas for clearing things up. The correct answer is:

$KL(p, q) = \log \frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 – \mu_2)^2}{2 \sigma_2^2} – \frac{1}{2}$

Best Answer

OK, my bad. The error is in the last equation:

\begin{align} KL(p, q) &= - \int p(x) \log q(x) dx + \int p(x) \log p(x) dx\\\\ &=\frac{1}{2} \log (2 \pi \sigma_2^2) + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2 \sigma_2^2} - \frac{1}{2} (1 + \log 2 \pi \sigma_1^2)\\\\ &= \log \frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2 \sigma_2^2} - \frac{1}{2} \end{align}

Note the missing $-\frac{1}{2}$. The last line becomes zero when $\mu_1=\mu_2$ and $\sigma_1=\sigma_2$.