KL divergence between two univariate Gaussians

link:  http://stats.stackexchange.com/questions/7440/kl-divergence-between-two-univariate-gaussians


Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

KL divergence between two univariate Gaussians

up vote 6 down vote favorite
4

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(μ1,σ1)  and  q(x)=N(μ2,σ2) . From Bishop's PRML I know that

KL(p,q)=p(x)logq(x)dx+p(x)logp(x)dx

where integration is done over all real line, and that

p(x)logp(x)dx=12(1+log2πσ21),

so I restrict myself to  p(x)logq(x)dx , which I can write out as

p(x)log1(2πσ22)(1/2)e(xμ2)22σ22dx,

which I can separate into

12log(2πσ22)p(x)loge(xμ2)22σ22dx.

Taking the log I get

12log(2πσ22)p(x)(xμ2)22σ22dx,

where I separate the sums and get  σ22  out of the integral.

12log(2πσ22)+p(x)x2dxp(x)2xμdx+p(x)μ2dx2σ22

Letting   denote the expectation operator under  p , I can rewrite this as

12log(2πσ22)+x22xμ2+μ222σ22.

We know that  var(x)=x2x2 . Thus

x2=σ21+μ21

and therefore

12log(2πσ2)+σ21+μ212μ1μ2+μ222σ22,

which I can put as

12log(2πσ22)+σ21+(μ1μ2)22σ22.

Putting everything together, I get to

KL(p,q)=p(x)logq(x)dx+p(x)logp(x)dx=12log(2πσ22)+σ21+(μ1μ2)22σ2212(1+log2πσ21)=logσ2σ1+σ21+(μ1μ2)22σ22.
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σ2σ1+σ21+(μ1μ2)22σ2212

share improve this question
 
 
please fix your latex, use  log  instead of  log . –   mpiktas  Feb 21 '11 at 10:51
 
sorry for posting the incorrect answer in the first place. I just looked at  xμ1  and immediately thought that the integral is zero. The point that it was squared completely missed my mind :) –   mpiktas  Feb 21 '11 at 12:02
 
what about the multi variate case? –   user7001  Oct 23 '11 at 0:49
 
I have just seen in a research paper that kld should be $KL(p, q) = ½ * ((μ₁-μ₂)² + σ₁²+σ₂²) * ( (1/σ₁²) + (1/σ₂²) ) - 2 –   skyde  Aug 1 '13 at 14:26 
add comment

2 Answers

active oldest votes
up vote 6 down vote accepted

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

KL(p,q)=p(x)logq(x)dx+p(x)logp(x)dx=12log(2πσ22)+σ21+(μ1μ2)22σ2212(1+log2πσ21)=logσ2σ1+σ21+(μ1μ2)22σ22

12  is missing in the last line. With it the last line becomes zero when  μ1=μ2  and  σ1=σ2 .

share improve this answer
 
 
The first line is trivially 0 because the two terms are equal with opposite sign. The "x" at the bottom of the integral signs has no meaning, either. Copy-and-paste errors? –   whuber   Feb 21 '11 at 17:39 
 
@whuber, yes copy-paste. The same error is in the OP, will fix it. –   mpiktas  Feb 21 '11 at 17:58
 
It's much clearer now, thanks. –   whuber   Feb 21 '11 at 18:05
add comment
up vote 2 down vote

I did not have a look at your calculation but here is mine with a lot of details. Suppose  p  is the density of a normal random variable with mean  μ1  and variance  σ21 , and that  q  is the density of a normal random variable with mean  μ2  and variance  σ22 . The Kullback-Leibler distance from  q  to  p  is:

[log(p(x))log(q(x))]p(x)dx

=[12log(2π)log(σ1)12(xμ1σ1)2+12log(2π)+log(σ2)+12(xμ2σ2)2]   ×12πσ1exp[12(xμ1σ1)2]dx

={log(σ2σ1)+12[(xμ2σ2)2+(xμ1σ1)2]}   ×12πσ1exp[12(xμ1σ1)2]dx

=E1{log(σ2σ1)+12[(xμ2σ2)2+(xμ1σ1)2]}

=log(σ2σ1)+12σ22E1{(Xμ2)2}12σ21E1{(Xμ1)2}

=12log(σ21σ22)+12σ22E1{(Xμ2)2}12

(Now note that  (Xμ2)2=(Xμ1+μ1μ2)2=(Xμ1)2+2(Xμ1)(μ1μ2)+(μ1μ2)2 )

=12log(σ21σ22)+12σ22[E1{(Xμ1)2}+2(μ1μ2)E1{Xμ1}+(μ1μ2)2]12

=12log(σ21σ22)+12σ21σ22+12σ22(μ1μ2)212

=(μ1μ2)22σ22+12(σ21σ221logσ21σ22)

share improve this answer

你可能感兴趣的:(KL divergence between two univariate Gaussians)