Skip to content
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

fix issue #56 #77

Closed
wants to merge 1 commit into from
Closed

fix issue #56 #77

wants to merge 1 commit into from

Conversation

dhicks
Copy link

@dhicks dhicks commented Oct 24, 2017

Implements fix to issue #56 proposed by @Maren-Eckhoff

@cpsievert
Copy link
Owner

Thanks, and sorry for the long wait...@kshirley merging #80 doesn't necessarily fix #56, so lets check for 0 as well?

@cpsievert
Copy link
Owner

perhaps a more robust solution would be:

jensenShannon <- function(x, y) {
    m <- 0.5*(x + y)
    val <- 0.5*sum(x*(log(x)-log(m))) + 0.5*sum(y*(log(y)-log(m)))
    if (is.finite(val)) val else 0
  }

@kshirley
Copy link
Collaborator

is.finite() doesn't quite work here, I don't think.

A good test case is

x <- c(0, 0, 0.2, 0.8)
y <- c(0, 0.2, 0, 0.8)

The Jensen Shannon Divergence should return 0 for the first element, and non-zero for the last 3 elements, before summing.

I gotta run right now but I can look at this tonight, should just be a couple line fix.

I think in theory we didn't worry about this because technically with the priors in LDA, you should never have exact zeroes in phi or theta, but we probably should have realized they would occur with rounding! :)

@kshirley
Copy link
Collaborator

Ah, the solution above from @dhicks based on @Maren-Eckhoff 's earlier comment works.

Let's just replace log(x/m) with (log(x) - log(m)) and replace log(y/m) with (log(y) - log(m)) in the PR from @dhicks to make it more robust, as suggested in #80

@cpsievert
Copy link
Owner

Done in c7234d7

@cpsievert cpsievert closed this Apr 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants