Gamma
ExponentialFamilyGamma(concentration: Tensor | float, rate: Tensor | float, validate_args: bool | None = None)Gamma distribution on — shape/rate parameterisation.
Two-parameter continuous distribution that generalises the
lucid.distributions.Exponential (shape )
and lucid.distributions.Chi2 (shape , rate
). 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 floatrateTensor or floatvalidate_argsbool= NoneTrue, validate parameter constraints at construction time.Notes
Probability density on :
Moments:
Entropy:
where is the digamma function.
Special cases and relations:
- Sum of independent Gammas with the same rate adds shapes: .
- The ratio is .
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.0)
>>> d.sample((4,))
Tensor([...])
>>> d.log_prob(lucid.tensor(1.5))
Tensor(...)Methods (7)
__init__
→None__init__(concentration: Tensor | float, rate: Tensor | float, validate_args: bool | None = None)Construct a Gamma distribution.
Parameters
concentrationTensor | floatrateTensor | floatvalidate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Notes
The Gamma distribution has PDF:
Special cases include:
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.0)mean
→Tensormean: TensorExpected value of the Gamma distribution.
Returns
TensorMean , shape batch_shape.
Examples
>>> Gamma(concentration=3.0, rate=2.0).mean
Tensor(1.5)mode
→Tensormode: TensorMode of the Gamma distribution.
The mode is zero for (the density is monotonically decreasing from infinity) and positive for .
Returns
TensorMode , shape batch_shape.
Examples
>>> Gamma(concentration=3.0, rate=1.0).mode
Tensor(2.0)variance
→Tensorvariance: TensorVariance of the Gamma distribution.
Returns
TensorVariance , shape batch_shape.
Examples
>>> Gamma(concentration=4.0, rate=2.0).variance
Tensor(1.0)sample
→Tensorsample(sample_shape: tuple[int, ...] = ())Draw samples from the Gamma distribution.
Delegates to _sample_standard_gamma (Marsaglia–Tsang algorithm)
and scales by to obtain
samples. The result is detached since the rejection-based
sampler does not support reparameterisation.
Parameters
sample_shapetuple[int, ...]= ()().Returns
TensorNon-negative samples of shape sample_shape + batch_shape.
Examples
>>> d = Gamma(concentration=2.0, rate=1.0)
>>> x = d.sample((500,))
>>> x.mean() # approximately 2.0log_prob
→Tensorlog_prob(value: Tensor)Log-density of value under the Gamma distribution.
Parameters
valueTensorReturns
TensorElement-wise log-densities, shape batch_shape.
Examples
>>> Gamma(concentration=1.0, rate=1.0).log_prob(lucid.tensor(1.0))
Tensor(-1.0)entropy
→Tensorentropy()Shannon entropy of the Gamma distribution (in nats).
where is the digamma function.
Returns
TensorEntropy in nats, shape batch_shape.
Examples
>>> Gamma(concentration=1.0, rate=1.0).entropy() # Exp(1): H = 1
Tensor(1.0)