I'm trying to implement a function to calculate Kullback-Liebler Divergence but I am running into a problem with complex infinities in my result. Here is how I have implemented it thus far:
pmfA={1/6,1/6,1/6,1/6,1/6,1/6};
pmfB={1/4,1/4,1/8,1/8,1/8,1/8};
Function[{p, q}, p*Log[p] /Log[q]][N@pmfA,N@pmfB]
Total[%]
For some "nice" sample data (containing no zeros), we get the nice result:
1.00526
However, if we use some "not so nice" sample data (containing zeros for some components of the PMF), it won't work, because of a negative infinity for Log[0].
eg. Try it out with...
pmfA={1/6,1/6,1/6,1/6,1/6,1/6};
pmfB={1/4,1/4,1/2,0,0,0};
For the purpose of computing the total, I think it would be acceptable to treat the instances as "0" but I am unsure of how to implement that.