Skip to content

Commit

Permalink
distribution moment method compatible with scipy version >= 1.11.3
Browse files Browse the repository at this point in the history
  • Loading branch information
EdoLu committed Nov 11, 2023
1 parent b8b577e commit 59a13db
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion gemact/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ def moment(self, n):
"""
hf.assert_type_value(n, 'n', logger, (float, int), lower_bound=1, lower_close=True)
n = int(n)
return self._dist.moment(n=n)
try:
output = self._dist.moment(n=n)
except:
output = self._dist.moment(order=n)
return output

def skewness(self):
"""
Expand Down Expand Up @@ -3341,6 +3345,31 @@ def lev(self, v):
out = (1 - np.exp(-self.theta * v)) / self.theta
out[v < 0] = v[v < 0]
return out

def partial_moment(self, n, low, up):
"""
Partial moment of order n.
:param n: moment order.
:type n: ``int``
:param low: lower limit of the partial moment.
:type low: ``int``, ``float``
:param up: upper limit of the partial moment.
:type up: ``int``, ``float``
:return: raw partial moment of order n.
:rtype: ``float``
"""

hf.assert_type_value(low, 'low', logger, type=(int, float),
lower_bound=0, upper_bound=float('inf'), upper_close=False)
hf.assert_type_value(up, 'up', logger, type=(int, float), lower_bound=low, lower_close=False)
hf.assert_type_value(n, 'n', logger, type=(int, float), lower_bound=0, lower_close=True)
n = int(n)

scale = 1/self.theta
output = scale**n * np.exp(special.loggamma(n+1) - special.loggamma(1))
output *= stats.gamma(n+1, scale=scale).cdf(up) - stats.gamma(n+1, scale=scale).cdf(low)
return output


# Gamma
Expand Down Expand Up @@ -3431,6 +3460,32 @@ def lev(self, v):
out[flt] = (alpha / beta) * special.gammainc(alpha + 1, beta * v[flt]) + v[flt] * (1 - special.gammainc(alpha, beta * v[flt]))
return out

def partial_moment(self, n, low, up):
"""
Partial moment of order n.
:param n: moment order.
:type n: ``int``
:param low: lower limit of the partial moment.
:type low: ``int``, ``float``
:param up: upper limit of the partial moment.
:type up: ``int``, ``float``
:return: raw partial moment of order n.
:rtype: ``float``
"""

hf.assert_type_value(low, 'low', logger, type=(int, float),
lower_bound=0, upper_bound=float('inf'), upper_close=False)
hf.assert_type_value(up, 'up', logger, type=(int, float), lower_bound=low, lower_close=False)
hf.assert_type_value(n, 'n', logger, type=(int, float), lower_bound=0, lower_close=True)
n = int(n)

scale = self.scale
shape = self.a
output = scale**n * np.exp(special.loggamma(shape + n) - special.loggamma(shape))
output *= stats.gamma(shape + n, scale=scale).cdf(up) - stats.gamma(shape + n, scale=scale).cdf(low)
return output


# Inverse Gamma
class InvGamma(_ContinuousDistribution):
Expand Down

0 comments on commit 59a13db

Please sign in to comment.