October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Python SciPy Gamma: Choose Between `special.gamma` and `stats.gamma`

SciPy has separate APIs for the gamma function and gamma probability distribution. Here’s how to choose the right one and handle shape, scale, rate, and tail probabilities.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from 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.

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:

  • gammaln returns the logarithm of the absolute value of the gamma function.
  • loggamma returns the principal branch of the complex logarithm of the gamma function.
  • gammasgn gives the sign of the gamma function for real inputs.
  • rgamma is 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

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.

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: gdtr and gdtrc take rate first and shape second.
  • Using a CDF for an upper tail: choose sf or gdtrc for 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.