In SciPy, scipy.special.gamma(z) evaluates the mathematical gamma function Γ(z), while scipy.stats.gamma represents a gamma probability distribution. Use the first to calculate a gamma-function value; use the second for distribution tasks such as density, probability, quantiles, and random sampling. Their connection is that the gamma function appears in the distribution’s density.
Choose the API for your task
| What you need | Use | Example result |
|---|---|---|
| Evaluate Γ(z), the generalized factorial function | scipy.special.gamma |
A gamma-function value |
| Work with a gamma-distributed random variable | scipy.stats.gamma |
A density, probability, quantile, or random variate |
| Calculate a gamma-distribution CDF or survival probability directly | scipy.special.gdtr or scipy.special.gdtrc |
A CDF or upper-tail probability |
The names are similar, but the inputs and outputs have different meanings. In particular, the shape and rate arguments used by the direct CDF functions are ordered differently from the shape and scale convention used by stats.gamma.
As an Amazon Associate I earn from qualifying purchases.
Calculate the gamma function with scipy.special.gamma
The gamma function extends the factorial to non-integer and complex arguments. For positive real part, it is defined by Γ(z) = ∫₀∞ tz−1e−tdt and is extended elsewhere by analytic continuation. Its recurrence is Γ(z + 1) = zΓ(z), so Γ(n + 1) = n! for natural numbers n. See the SciPy special.gamma reference for its definition, examples, and behavior.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutefrom scipy.special import gamma
values = gamma([0, 0.5, 1, 5])
gamma accepts array-like input, as in this example. Choose it when your formula explicitly calls for Γ(z), not when you want the probability of an event under a gamma distribution.
#1 Best Overall
Related special functions are not interchangeable
If you need a logarithm or a reciprocal, select the corresponding function rather than applying gamma and assuming it is numerically or mathematically equivalent for every purpose:
gammalnreturns the logarithm of the absolute value of the gamma function.loggammareturns the principal branch of the complex logarithm of the gamma function.gammasgngives the sign of the gamma function for real inputs.rgammais the reciprocal gamma function.
SciPy also lists regularized incomplete gamma functions and their inverses; see its special-functions index to match the function to the quantity in your formula.
Rank #2
Poles and signed zero in SciPy 1.15 and later
The current SciPy gamma reference documents poles at nonnegative integers, with NaN at negative integer poles. At zero, the sign bit affects the result: gamma(-0.0) is negative infinity and gamma(+0.0) is positive infinity. SciPy says this behavior was fixed in version 1.15; before that, the function returned positive infinity at each pole.
This matters when gamma appears in a denominator: a pole may now propagate NaN where older behavior could have produced zero. For reciprocal-gamma expressions, SciPy recommends rewriting the factor with rgamma. Check the documentation for the SciPy version you have installed before relying on this behavior in version-sensitive code.
Use scipy.stats.gamma for a probability distribution
The standard gamma distribution has shape a > 0 and support x ≥ 0. Its standardized density is xa−1e−x/Γ(a). SciPy’s distribution object applies location and scale parameters; its scale argument is a scale, not a rate. See the SciPy gamma-distribution tutorial and probability-distribution tutorial.
from scipy.stats import gamma
shape = 2.0
rate = 3.0
distribution = gamma(a=shape, scale=1 / rate)
probability = distribution.cdf(1.0)
This creates a gamma distribution with shape 2 and rate 3, then calculates the probability that a value is at most 1. The conversion is scale = 1 / rate. If a paper or another library specifies a rate λ, pass scale=1/λ; do not pass λ itself as SciPy’s scale.
Use the distribution methods that match the question
The continuous-distribution interface supplies common operations, including density, cumulative probability, quantiles, and random variates. For example, distribution.pdf(x) gives the density at x, distribution.cdf(x) gives the probability up to x, and distribution.rvs() draws random variates. For an upper-tail probability, use distribution.sf(x), the survival function.
Calculate a gamma-distribution CDF or upper tail directly
SciPy provides gdtr for the CDF and gdtrc for the survival probability. Unlike stats.gamma, these functions take rate first, then shape:
Best Value
from scipy.special import gdtr, gdtrc
cdf_value = gdtr(rate, shape, x)
tail_probability = gdtrc(rate, shape, x)
| Direct function | Argument order | Equivalent distribution operation |
|---|---|---|
gdtr(rate, shape, x) |
rate, shape, value | gamma(shape, scale=1/rate).cdf(x) |
gdtrc(rate, shape, x) |
rate, shape, value | gamma(shape, scale=1/rate).sf(x) |
Use gdtrc or the distribution’s sf method when you want an upper-tail probability directly, rather than calculating 1 minus a CDF. SciPy’s gdtr reference and gdtrc reference document the equivalences. SciPy also notes that these direct functions can often be faster for small arrays or individual values; this is a qualified documentation note, not a quantified performance guarantee.
Quick Recap
Common parameter and API mistakes
- Calling the wrong gamma:
special.gamma(z)evaluates Γ(z); it does not construct a probability distribution. - Passing a rate as a scale: for rate λ, use
stats.gamma(a=shape, scale=1/λ). - Reversing direct-function arguments:
gdtrandgdtrctake rate first and shape second. - Using a CDF for an upper tail: choose
sforgdtrcfor the survival probability. - Assuming older pole behavior: signed-zero and negative-integer pole handling changed in SciPy 1.15, so verify installed-version behavior when it affects results.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




