I have two very large discrete frequency distributions (about 4 million items), and each contains many items with counts of 0. I want to calculate the KL divergence between them and use the empirical probability as the probability estimate. However, this seems to result in a negative value, which if I understand correctly, should not be possible with KL divergence, and I think it's because there are counts of 0.
For instance, if I run the following R code, I get a negative:
library(philentropy)
mat <- matrix(
rep(1:271, each = 2), # 271 or higher all yield negative values
nrow = 2
)
mat[2, 1] <- 0
KL(mat, est.prob = "empirical")
Result: -7.181671e-08
If I add a very small fixed number to every count, though, I get a very small but positive result:
mat <- mat + 0.01
KL(mat, est.prob = "empirical")
Result: 0.0001433061
Essentially, I'm using Laplace smoothing (I think) to get rid of 0 counts. Is this statistically valid, though?