# How to ensure the non-negativity of Kullback-Leibler Divergence KLD Metric (Relative Entropy)?

I’m having some problems in ensuring the non-negativity of KLD!

I know that KLD is always positive and I went over the proof. However, it doesn’t seem to work for me. In some cases I’m getting negative results. Here is how I’m using KLD:

$${\rm KLD}( P(x) || Q(x) ) = \sum P(x) \log \left( \frac{P(x)}{Q(x)} \right) \, ,$$ where the Log is in base 2, and $P(x)$ and $Q(x)$ are two different distributions for all $x \in X$.

For example, $P(x) = {\rm Frequency}(x)/{\text Total Size}$; just a normal PMF! The same thing for $Q(x)$. Note that the Total_Size of $P$ might be different from that of the $Q$ distribution.

Could you please let me know if I’m missing something? Are there any special conditions that I have to take into consideration to avoid having negative results?

"The K-L divergence is only defined if P and Q both sum to 1 and if Q(i) > 0 for any i such that P(i) > 0."

I suspect that the second condition is your problem. Say that you have x which appears in P but not Q -- in this case you're probably adding zero contribution to the sum in your code so that you don't have to divide by zero or take the logarithm of zero, but this is effectively throwing out mass from P and you get a negative number for the divergence.

http://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence

Here is a simple proof, which also shows non-negativity for several other related functions.

Let $\varphi : \mathbb{R}^n \to \mathbb{R}$ be a strictly convex and continuously differentiable function. Then we can use $\varphi$ to "generate" a divergence function $D$ defined as follows: $$D(x,y) = \phi(x) - \phi(y) - \langle \nabla\varphi(y), x - y\rangle$$

The above divergence function is known as a Bregman divergence.

The fact that $D(x,y) \ge 0$ (equalling $0$, iff $x=y$) is an immediate consequence of the strict convexity of $\varphi$.

Now, if you choose $\varphi(x) = \sum_i x_i \log x_i$, you obtain the KL-Divergence (upto normalization) --- Note here that usually one takes $0\log 0 = 0$ (by continuity), and that the arguments to KL should satisfy the conditions mentioned by Ohai.