Skip to content

GenGamma _munp function fails due to overflow #4724

@ldamewood

Description

@ldamewood

There's a part of the gengamma distribution code that divides two gamma functions. As a result, The mean of the distribution is nan for values of a > ~170.

from scipy import special
import numpy as np
n = -2
a = 200
c = 1.
# Calcuate the mean (gives nan)
print(scipy.stats.distributions.gengamma(a, n, scale=c).mean())
# Function used in _munp (gives nan)
print(special.gamma(a+n*1.0/c) / special.gamma(a))
# Possible fix for large argument values? (gives correct answer)
print(np.exp(special.gammaln(a+n*1.0/c) - special.gammaln(a)))

Perhaps this in scipy/stats/_continuous_distns.py:2041?

    def _munp(self, n, a, c):
        if a > 170 or a+n*1.0/c > 170:
            return np.exp(gamln(a+n*1.0/c) - gamln(a))
        return special.gamma(a+n*1.0/c) / special.gamma(a)

But I am unsure if this catches all edge cases including positive/negative large/small values of n and c.

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.stats

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions