This page explains the following SciPy functions for computing the gamma function and related functions.
The SciPy function names correspond easily to mathematical functions. However, as noted below, there are a couple things to be aware of.
- The functions with named ending in
lndo indeed take natural logarithms, but they take absolute values first.
- The functions containing
inccompute regularized incomplete functions and not simply incomplete functions.
These points will be made clear below.
Gamma and beta functions
The SciPy functions for gamma function Γ(z) and the beta function B(x, y) are unsurprisingly called
beta. The function
and the function
beta(x, y) computes
Often the gamma and beta functions are not as useful in practice as their logarithms. (See explanation here.) These are implemented in SciPy as
betaln. Note that
gammaln actually returns the natural logarithm of the absolute value of the gamma function, i.e.
gammaln(z) computes log| γ(z) |. Similarly
betaln(x, y) computes log | B(x, y) |.
Psi and polygamma functions
The derivative of log( Γ(z) ) is denoted ψ(z) and is implemented in the
psi function. The nth derivative of ψ(z) is implemented in
Incomplete and complementary functions
Both the gamma and the beta function have “incomplete” versions. However, SciPy’s incomplete gamma function
gammainc corresponds to the regularized gamma function. Similarly, SciPy’s incomplete beta function
betainc corresponds to the regularized beta function.
The (lower) incomplete gamma function is defined by
and the upper incomplete gamma function is defined by
These are called “incomplete” because they integrate over part of the region defining the gamma function. We will see below that the incomplete beta function follows the same pattern.
The regularized versions of the upper and lower incomplete gamma functions are
The SciPy function
gammainc(a, z) computes P(a, z) and the function
gammaincc computes Q(a, z). Note the extra ‘c’ in
gammaincc that stands for “complement.” It may not seem necessary to provide functions for both P(a, z) and its complement Q(a, z) since Q(a, z) = 1 – P(a, z). However, the complementary functions may be necessary for numerical precision. See the explanation here regarding the error function and its complement; the same reasoning applies to incomplete gamma functions.
The incomplete beta function is defined by
and the regularized incomplete beta function is defined by
The SciPy function
betainc(a, b, x) computes Bx(a, b) and the function
betaincc computes Ix(a, b).
SciPy functions ending in
inv compute the inverse of the corresponding function.
gammaincinv(a, y) returns
x such that
gammainc(a, x) equals
gammainccinv is the inverse of
betaincinv(a, b, y) returns
x such that
betaincinv(a, b, x) equals
There is an asymmetry in the SciPy implementations of the gamma and beta functions: there is no functions
betainccinv. These functions are unnecessary because of symmetries in the beta function. One could define
betaincc(a, b, x) as
betainc(b, a, 1-x) and
betainccinv(a, b, x) as
betaincinv(b, a, 1-x).