Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incomplete gamma function in python?

Tags:

python

scipy

the scipy.special.gammainc can not take negative values for the first argument. Are there any other implementations that could in python? I can do a manual integration for sure but I'd like to know if there are good alternatives that already exist.

Correct result: 1 - Gamma[-1,1] = 0.85

Use Scipy: scipy.special.gammainc(-1, 1) = 0

Thanks.

like image 959
nye17 Avatar asked May 10 '12 22:05

nye17


People also ask

What is the incomplete beta function?

The incomplete beta function, a generalization of the beta function, is defined as. For x = 1, the incomplete beta function coincides with the complete beta function. The relationship between the two functions is like that between the gamma function and its generalization the incomplete gamma function.

How do you calculate incomplete gamma in R?

Incomplete gamma functions can be calculated in R with pgamma, or with gamma_inc_Q from library(gsl), or with gammainc from library(expint). However, all of these functions take only real input.


2 Answers

I typically reach for mpmath whenever I need special functions and I'm not too concerned about performance. (Although its performance in many cases is pretty good anyway.)

For example:

>>> import mpmath
>>> mpmath.gammainc(-1,1)
mpf('0.14849550677592205')
>>> 1-mpmath.gammainc(-1,1)
mpf('0.85150449322407795')
>>> mpmath.mp.dps = 50 # arbitrary precision!
>>> 1-mpmath.gammainc(-1,1)
mpf('0.85150449322407795208164000529866078158523616237514084')
like image 84
DSM Avatar answered Oct 12 '22 09:10

DSM


I just had the same issue and ended up using the recurrence relations for the function when a<0. http://en.wikipedia.org/wiki/Incomplete_gamma_function#Properties

Note also that the scipy functions gammainc and gammaincc give the regularized forms Gamma(a,x)/Gamma(a)

like image 42
JoeZuntz Avatar answered Oct 12 '22 10:10

JoeZuntz