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 𝑝(𝑥)=𝑁(𝜇1,𝜎1) and 𝑞(𝑥)=𝑁(𝜇2,𝜎2). From Bishop's
PRML I know that
𝐾𝐿(𝑝,𝑞)=−∫𝑝(𝑥)log𝑞(𝑥)𝑑𝑥+∫𝑝(𝑥)log𝑝(𝑥)𝑑𝑥
where integration is done over all real line, and that
∫𝑝(𝑥)log𝑝(𝑥)𝑑𝑥=−12(1+log2𝜋𝜎21),
so I restrict myself to ∫𝑝(𝑥)log𝑞(𝑥)𝑑𝑥, which I can write out as
−∫𝑝(𝑥)log1(2𝜋𝜎22)(1/2)𝑒−(𝑥−𝜇2)22𝜎22𝑑𝑥,
which can be separated into
12log(2𝜋𝜎22)−∫𝑝(𝑥)log𝑒−(𝑥−𝜇2)22𝜎22𝑑𝑥.
Taking the log I get
12log(2𝜋𝜎22)−∫𝑝(𝑥)(−(𝑥−𝜇2)22𝜎22)𝑑𝑥,
where I separate the sums and get 𝜎22 out of the integral.
12log(2𝜋𝜎22)+∫𝑝(𝑥)𝑥2𝑑𝑥−∫𝑝(𝑥)2𝑥𝜇2𝑑𝑥+∫𝑝(𝑥)𝜇22𝑑𝑥2𝜎22
Letting ⟨⟩ denote the expectation operator under 𝑝, I can rewrite this as
12log(2𝜋𝜎22)+⟨𝑥2⟩−2⟨𝑥⟩𝜇2+𝜇222𝜎22.
We know that 𝑣𝑎𝑟(𝑥)=⟨𝑥2⟩−⟨𝑥⟩2. Thus
⟨𝑥2⟩=𝜎21+𝜇21
and therefore
12log(2𝜋𝜎2)+𝜎21+𝜇21−2𝜇1𝜇2+𝜇222𝜎22,
which I can put as
12log(2𝜋𝜎22)+𝜎21+(𝜇1−𝜇2)22𝜎22.
Putting everything together, I get to
𝐾𝐿(𝑝,𝑞)=−∫𝑝(𝑥)log𝑞(𝑥)𝑑𝑥+∫𝑝(𝑥)log𝑝(𝑥)𝑑𝑥=12log(2𝜋𝜎22)+𝜎21+(𝜇1−𝜇2)22𝜎22−12(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:
𝐾𝐿(𝑝,𝑞)=log𝜎2𝜎1+𝜎21+(𝜇1−𝜇2)22𝜎22−12