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

Question about Gamma and Inverse Gamma logpdf, values outside support #1761

Open
bratslavia opened this issue Aug 18, 2023 · 4 comments
Open

Comments

@bratslavia
Copy link

bratslavia commented Aug 18, 2023

I was doing some testing with MixtureModels and logsumexp functions, and I happened to notice a couple of things about the logpdf/loglikelihood functions for Gamma and Inverse Gamma that had me scratching my head.

Specifically, they seem to handle variables outside the support differently. E.g.,

using Distributions

logpdf(Gamma(), 0) # 0.0, but shouldn't this be -Inf?
logpdf(InverseGamma(), 0) # NaN, but again, shouldn't it be -Inf?

logpdf(Gamma(), -1) # -Inf (seems correct)
logpdf(InverseGamma(), -1) # Throws a domain error for "log", but shouldn't it just return -Inf?

Support for both of those distributions is supposed to be the positive reals, right? I would have expected that anything outside the support would just return -Inf for the logpdf.

[NOTE: I realize the Gamma logpdf may be correct -- I discovered that some implementations (e.g., Boost) allow a random variate of zero in the function. But in that case, perhaps the documentation might need to be updated to indicate that the range is [0, Inf), not (0, Inf)? Which is what the wikipedia link in the documentation says, and the way many (most?) implementations work, afaik.]

The same issues seem to affect the overall loglikelihood computation. E.g.,

loglikelihood(Gamma(), [1.0, 0.0]) # Gives -1.0, when I would expect -Inf, given the stated support
loglikelihood(InverseGamma(), [1.0, 0.0]) # Gives NaN, when I would expect -Inf

loglikelihood(Gamma(), [1.0, -1.0]) # Gives -Inf, which seems correct
loglikelihood(InverseGamma(), [1.0, -1.0]) # Domain error, in "log" function
@3f6a
Copy link

3f6a commented Nov 19, 2023

Another example:

julia> logpdf(Gamma(0.8, 18), 0.0)
Inf

This causes problems for me with inference of Hidden Markov Models.

@andreasnoack
Copy link
Member

I hadn't noticed that Wikipedia defines the distribution for x > 0. That surprises me and it also differs from R and Wolfram. IMO, the current behavior for Gamma is fine. Why is it a problem for the linked package?

The situation is different for InverseGamma. Here something needs to be adjusted to avoid the NaN and the exception.

@3f6a
Copy link

3f6a commented Nov 20, 2023

Note that Wolfram Mathematica gives zero:

In[17]:= PDF[GammaDistribution[0.8, 18], 0]
Out[17]= 0.

@andreasnoack
Copy link
Member

Looks like Wolfram is inconsistent here. I don't think it changes the point, though.

For the record then R does use the right limit

> dgamma(0.0, 0.8, 5.0)
[1] Inf

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

No branches or pull requests

3 participants