Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.gamma

scipy.stats.gamma is SciPy’s continuous gamma random-variable object. Provide a positive shape a, optionally shift with loc and scale with scale, then evaluate probabilities or generate samples.

How it works

gamma is SciPy’s continuous gamma-distribution object. Supply the positive shape a, then use inherited distribution methods such as pdf, cdf, or rvs; loc shifts the distribution and scale stretches it, while calling gamma(a, ...) creates a frozen object with parameters retained.

Syntax

scipy.stats.gamma(a, loc=0, scale=1)

Parameters

NameTypeDescription
aarray_likeShape parameter; the documented gamma distribution requires a > 0.
locarray_like, optionalLocation parameter used to shift the distribution; default is 0.
scalearray_like, optionalScale parameter used to scale the distribution; default is 1.

Returns

The distribution object provides continuous-random-variable methods; calling it with parameters returns a frozen continuous random-variable object.

Key points

  • scipy.stats.gamma represents a continuous gamma random variable.
  • It inherits the standard continuous-distribution API, including probability, quantile, summary-statistic, and sampling methods.
  • Pass a as the distribution’s shape parameter.
  • Its standard density is proportional to x^(a-1) exp(-x) / Gamma(a) on x >= 0, with a > 0.
  • Use loc to translate the distribution and scale to change its spread.
  • Calling the distribution with parameters creates a frozen object that retains those parameters for later method calls.
  • The documentation identifies erlang and expon as related distributions.

Common mistakes

  • Use scale for the scale convention documented by SciPy; a rate parameter beta corresponds to scale = 1 / beta, not scale = beta.
  • The exponential and Erlang cases are special cases of this distribution, not separate parameter conventions.

Example: Evaluate and sample a gamma distribution

from scipy.stats import gamma

shape = 2.0
x = 3.0
pdf = gamma.pdf(x, a=shape)
sample = gamma.rvs(a=shape, size=5, random_state=0)
print(pdf)
print(sample)

Evaluates the gamma density at one point and generates reproducible random variates.

When to use it

Probability-density evaluation

Compute likelihoods or densities for nonnegative continuous measurements.

Random sampling

Generate simulated observations from a gamma model.

Positive continuous modeling

Model waiting times, durations, or aggregate positive quantities with adjustable shape and scale.

Interview questions

QWhat parameters define scipy.stats.gamma?

a is the shape parameter and must be positive. The standard density is proportional to x**(a-1) * exp(-x) divided by the gamma function, for nonnegative x.

Follow-up: How would you use a rate parameter β? Use SciPy’s scale=1 / β, because its documented alternative parameterization uses rate as the reciprocal of scale.

QWhat do loc and scale do?

loc shifts the distribution and scale scales it. For the standard gamma form, the density is defined for x >= 0; applying loc changes the location through the usual continuous-distribution transformation.

Follow-up: How can repeated evaluations avoid passing parameters each time? Call gamma(a, loc=..., scale=...) to obtain a frozen continuous random-variable object.

QHow is the gamma object typically used?

The object provides generic continuous-distribution methods, including rvs for random variates and pdf for the probability density function. For example, gamma.pdf(x, a) evaluates the density and gamma.rvs(a, size=...) samples values.

Follow-up: Which related distributions should you check for special cases? The documentation lists erlang and expon; integer a gives Erlang, while a=1 gives exponential.

  • scipy.stats.erlang
  • scipy.stats.expon

Source: scipy.stats.gamma. Checked 2026-10-15.