I'm trying to find if two hidden neurons in RBF Network overlap with each other or not? It's an online classification problem, it means data come to our network one-by-one and then discard completely. so I already know the mean and covariance of each neuron. I want to measure the distance between every two neurons. as long as this network is RBF and each neuron show a distribution, I want to use KL divergence as a measure of distance. ( I know KL can't be a measure, because the KL(P||Q) is not equal with KL(Q||P), but assume that I fix this problem.)
I read this question about how to calculate multivariate Gaussians KL divergence
and I use the formula:
$$
\begin{aligned}
KL = \frac{1}{2}\left[\log\frac{|\Sigma_2|}{|\Sigma_1|} - d + \text{tr} \{ \Sigma_2^{-1}\Sigma_1 \} + (\mu_2 - \mu_1)^T \Sigma_2^{-1}(\mu_2 - \mu_1)\right].
\end{aligned}
$$
I also implemented the matlab code as below.
ans1=log(norm(CovarianceMatrix(:,:,NrSNumberindx(i)))/norm(CovarianceMatrix(:,:,NrS)))-(size(A,2))+trace(pinv(CovarianceMatrix(:,:,NrSNumberindx(i)))*CovarianceMatrix(:,:,NrS));
ans1=ans1+(CenterTem-CenterNeuron(NrS,:))*CovarianceMatrix(:,:,NrSNumberindx(i))*(CenterTem-CenterNeuron(NrS,:))';
ans1=ans1*0.5;
I can't find any implementation problem in my code. But it gives me a negative value for KL which we already know, KL is always positive.
I trace the code. At the first of training, covariance matrix of data is equal to zero so I add the epsilon to each element of the covariance matrix. But still trace of these values are really small and if the dimensions of data are huge then the value of (size(A,2))
is huge too.
So the trace is a small value, the log is equal to zero (because each value of the covariance matrix is equal to epsilon) and value of (size(A,2)) is large and negative, then the result of KL become negative which doesn't have any meaning!!