Skip to content

Instantly share code, notes, and snippets.

@sergeyf
Last active December 20, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergeyf/6117803 to your computer and use it in GitHub Desktop.
Save sergeyf/6117803 to your computer and use it in GitHub Desktop.
An alternative parameterization for scipy's invgamma which uses the parameters 'alpha' and 'beta'. See https://en.wikipedia.org/wiki/Inverse_gamma for more info. The original is here: http://en.it-usenet.org/thread/16070/1596/#post1596
from scipy.stats import rv_continuous
from scipy.special import gammaln, gammaincinv, gammainc
from numpy import log,exp
class igamma_gen(rv_continuous):
def _pdf(self, x, a, b):
return exp(self._logpdf(x,a,b))
def _logpdf(self, x, a, b):
return a*log(b) - gammaln(a) -(a+1)*log(x) - b/x
def _cdf(self, x, a, b):
return 1.0-gammainc(a,b/x) # why is this different than the wiki?
def _ppf(self, q, a, b):
return b/gammaincinv(a,1-q)
def _munp(self, n, a, b):
args = (a,b)
super(igamma_gen, self)._munp(self, n, *args)
igamma = igamma_gen(a=0.0, name='invgamma', longname="An inverted gamma",
shapes = 'a,b', extradoc="""
Inverted gamma distribution
igamma.pdf(x,a,b) = b**a*x**(-a-1)/gamma(a) * exp(-b/x)
for x > 0, a > 0, b>0.
"""
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment