Skip to content
class

Gamma

extendsExponentialFamily
Gamma(concentration: Tensor | float, rate: Tensor | float, validate_args: bool | None = None)

Gamma distribution on [0,∞)[0, \infty) — shape/rate parameterisation.

Two-parameter continuous distribution that generalises the lucid.distributions.Exponential (shape α=1\alpha = 1) and lucid.distributions.Chi2 (shape k/2k/2, rate 1/21/2). Widely used as the conjugate prior for the rate parameter of a Poisson likelihood and for positive-valued measurements such as waiting times, life-times, and rainfall amounts.

Parameters

concentrationTensor or float
Shape (concentration) parameter α>0\alpha > 0. Sometimes called kk. When α\alpha is a positive integer the Gamma equals the Erlang distribution (sum of α\alpha IID Exponentials).
rateTensor or float
Rate (inverse-scale) parameter β>0\beta > 0. Equivalent scale parameterisation: θ=1/β\theta = 1/\beta.
validate_argsbool= None
If True, validate parameter constraints at construction time.

Notes

Probability density on x>0x > 0:

p(x;α,β)=βαΓ(α)xα−1e−βxp(x; \alpha, \beta) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha - 1} e^{-\beta x}

Moments:

E[X]=αβ,Var[X]=αβ2,Mode=max⁡(α−1,0)β\mathbb{E}[X] = \frac{\alpha}{\beta}, \qquad \mathrm{Var}[X] = \frac{\alpha}{\beta^2}, \qquad \mathrm{Mode} = \frac{\max(\alpha - 1, 0)}{\beta}

Entropy:

H[X]=α−log⁡β+log⁡Γ(α)+(1−α)ψ(α)H[X] = \alpha - \log \beta + \log \Gamma(\alpha) + (1 - \alpha)\psi(\alpha)

where ψ\psi is the digamma function.

Special cases and relations:

  • Gamma(1,β)=Exponential(β)\mathrm{Gamma}(1, \beta) = \mathrm{Exponential}(\beta)
  • Gamma(k/2,1/2)=χ2(k)\mathrm{Gamma}(k/2, 1/2) = \chi^2(k)
  • Sum of independent Gammas with the same rate adds shapes: Gamma(α1,β)+Gamma(α2,β)=Gamma(α1+α2,β)\mathrm{Gamma}(\alpha_1, \beta) + \mathrm{Gamma}(\alpha_2, \beta) = \mathrm{Gamma}(\alpha_1 + \alpha_2, \beta).
  • The ratio Gα/(Gα+Gβ)G_\alpha/(G_\alpha + G_\beta) is Beta(α,β)\mathrm{Beta}(\alpha, \beta).

Sampling uses Marsaglia–Tsang acceptance-rejection — accept rate is consistently above 95 %, so eight retry rounds are more than enough. Because the kernel is rejection-based, has_rsample = False and samples are detached.

Examples

>>> import lucid
>>> from lucid.distributions import Gamma
>>> d = Gamma(concentration=2.0, rate=1.0)
>>> d.mean  # α/β
tensor(2.)
>>> d.sample((4,)).shape
(4,)
>>> d.log_prob(lucid.tensor(1.5))  # log(1.5) - 1.5
tensor(-1.095)

Used by 4

Constructors

1
dunder

__init__

→None
__init__(concentration: Tensor | float, rate: Tensor | float, validate_args: bool | None = None)

Construct a Gamma distribution.

Parameters

concentrationTensor | float
Shape (concentration) parameter α>0\alpha > 0. Also known as the shape parameter kk.
rateTensor | float
Rate (inverse-scale) parameter β>0\beta > 0. The scale is θ=1/β\theta = 1/\beta.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Notes

The Gamma distribution has PDF:

p(x;α,β)=βαΓ(α)xα−1e−βx,x>0p(x; \alpha, \beta) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha-1} e^{-\beta x}, \quad x > 0

Special cases include:

  • Gamma(1,β)=Exponential(β)\text{Gamma}(1, \beta) = \text{Exponential}(\beta)
  • Gamma(k/2,1/2)=χ2(k)\text{Gamma}(k/2, 1/2) = \chi^2(k)

Sampling uses the Marsaglia–Tsang acceptance-rejection algorithm (see module docstring); the accept rate is > 95 % so eight retry rounds are more than sufficient.

Examples

>>> from lucid.distributions import Gamma
>>> d = Gamma(concentration=2.0, rate=1.0)
>>> d.mean  # α/β = 2.0
tensor(2.)

Properties

3
prop

mean

→Tensor
mean: Tensor

Expected value of the Gamma distribution.

E[X]=αβE[X] = \frac{\alpha}{\beta}

Returns

Tensor

Mean α/β\alpha/\beta, shape batch_shape.

Examples

>>> Gamma(concentration=3.0, rate=2.0).mean
tensor(1.5)
prop

mode

→Tensor
mode: Tensor

Mode of the Gamma distribution.

mode=max⁡(α−1,  0)β\text{mode} = \frac{\max(\alpha - 1,\; 0)}{\beta}

The mode is zero for α≤1\alpha \leq 1 (the density is monotonically decreasing from infinity) and positive for α>1\alpha > 1.

Returns

Tensor

Mode max⁡(α−1,0)/β\max(\alpha-1, 0)/\beta, shape batch_shape.

Examples

>>> Gamma(concentration=3.0, rate=1.0).mode
tensor(2.)
prop

variance

→Tensor
variance: Tensor

Variance of the Gamma distribution.

Var⁡[X]=αβ2\operatorname{Var}[X] = \frac{\alpha}{\beta^2}

Returns

Tensor

Variance α/β2\alpha/\beta^2, shape batch_shape.

Examples

>>> Gamma(concentration=4.0, rate=2.0).variance
tensor(1.)

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the Gamma distribution.

F(x;α,β)=P(α,βx),x≥0F(x; \alpha, \beta) = P(\alpha, \beta x), \quad x \geq 0

the regularized lower incomplete gamma (lucid.special.gammainc). Chi2 inherits it.

Returns probabilities in [0,1][0, 1]; 0 below 0.

Raises

NotImplementedError
From backward, when concentration requires a gradient — PP is differentiable in its upper limit only, as in the reference framework. rate and value are differentiable.

Examples

>>> Gamma(concentration=1.0, rate=1.0).cdf(lucid.tensor(1.0))  # 1 - 1/e
tensor(0.6321)
fn

entropy

→Tensor
entropy()

Shannon entropy of the Gamma distribution (in nats).

H(X)=α−log⁡β+log⁡Γ(α)+(1−α)ψ(α)H(X) = \alpha - \log \beta + \log \Gamma(\alpha) + (1 - \alpha) \psi(\alpha)

where ψ\psi is the digamma function.

Returns

Tensor

Entropy in nats, shape batch_shape.

Examples

>>> Gamma(concentration=1.0, rate=1.0).entropy()  # Exp(1): H = 1
tensor(1.)
fn

expand

→Self
expand(batch_shape: Sequence[int], _instance: Self | None = None)

Return a new Gamma with batch dimensions expanded to batch_shape.

fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-density of value under the Gamma distribution.

log⁡p(x;α,β)=αlog⁡β+(α−1)log⁡x−βx−log⁡Γ(α)\log p(x; \alpha, \beta) = \alpha \log \beta + (\alpha - 1) \log x - \beta x - \log \Gamma(\alpha)

(α−1)log⁡x(\alpha - 1)\log x is taken as 0 when α=1\alpha = 1, so the density at x=0x = 0 is the finite β\beta of the exponential rather than 0 · (-inf) = NaN.

Parameters

valueTensor
Non-negative real values x≥0x \geq 0.

Returns

Tensor

Element-wise log-densities, shape batch_shape.

Examples

>>> Gamma(concentration=1.0, rate=1.0).log_prob(lucid.tensor(1.0))
tensor(-1.)
fn

rsample

→Tensor
rsample(sample_shape: tuple[int, ...] = ())

Draw reparameterised samples from the Gamma distribution via implicit reparameterisation.

fn

sample

→Tensor
sample(sample_shape: tuple[int, ...] = ())

Draw non-reparameterised samples from the Gamma distribution via _sample_standard_gamma.

Examples

>>> _ = lucid.manual_seed(0)
>>> d = Gamma(concentration=2.0, rate=1.0)
>>> x = d.sample((500,))
>>> x.shape
(500,)
>>> bool((x.mean() - 2.0).abs() < 0.2)  # the sample mean approaches α/β
True