-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KL returns negative values? #26
Comments
…set their own epsilon value when dealing with division by zero and log(0) cases #26
Hi Josh, Many thanks for your fantastic question and I am glad to hear that you find You are absolutely correct, the KL distance or divergence is always analytically defined as
Analytically, these cases would simply be set to Your initial instinct to add a However, the more elegant solution (from my humble perspective) is not to transform the entire input data, but choosing an appropriate epsilon value that takes the similarity of the compared density functions and the number of input values into account. When comparing huge vectors which are fairly similar (especially at the 0 borders), then even smaller epsilon values will be required to get non-negative KL values near 0. I now introduced an epsilon argument to You can see that if you now run your example code mat <- matrix(
rep(1:271, each = 2),
nrow = 2
)
mat[2, 1] <- 0
KL(mat, est.prob = "empirical", epsilon = 0.0000001)
Result:
kullback-leibler
0.0001801934 I hope this helps to make better analysis choices and please let me know if this works for you. Many thanks, |
I assume the issue has been solved? |
First, thank you so much for this package. Second, I'm getting negative results for some reason when using the KL function in cases where the two probability distributions that I'm comparing are expected to be nearly identical. My distributions are very large (over 4 million column matrices) and contain many zeros, and this seems to be the problem. I'm not very familiar with the math behind KL divergence, so I'm not sure if this is expected or not. I've found that I can get negative results once my matrix gets to 271 or more columns by changing the very first value to 0, like so:
Additionally, I thought of smoothing the counts by adding something like 0.01 to every count and this changes the result of KL to be a positive value:
Is this all expected or is there an issue somewhere?
The text was updated successfully, but these errors were encountered: