tf.compat.v1.distributions.Gamma

Gamma distribution.

Inherits From: Distribution

The Gamma distribution is defined over positive real numbers using parameters concentration (aka "alpha") and rate (aka "beta").

Mathematical Details

The probability density function (pdf) is,

pdf(x; alpha, beta, x > 0) = x**(alpha - 1) exp(-x beta) / Z
Z = Gamma(alpha) beta**(-alpha)

where:

  • concentration = alpha, alpha > 0,
  • rate = beta, beta > 0,
  • Z is the normalizing constant, and,
  • Gamma is the gamma function.

The cumulative density function (cdf) is,

cdf(x; alpha, beta, x > 0) = GammaInc(alpha, beta x) / Gamma(alpha)

where GammaInc is the lower incomplete Gamma function.

The parameters can be intuited via their relationship to mean and stddev,

concentration = alpha = (mean / stddev)**2
rate = beta = mean / stddev**2 = concentration / mean

Distribution parameters are automatically broadcast in all functions; see examples for details.

Samples of this distribution are reparameterized (pathwise differentiable). The derivatives are computed using the approach described in (Figurnov et al., 2018).

Examples

import tensorflow_probability as tfp
tfd = tfp.distributions

dist = tfd.Gamma(concentration=3.0, rate=2.0)
dist2 = tfd.Gamma(concentration=[3.0, 4.0], rate=[2.0, 3.0])

Compute the gradients of samples w.r.t. the parameters:

concentration = tf.constant(3.0)
rate = tf.constant(2.0)
dist = tfd.Gamma(concentration, rate)
samples = dist.sample(5)  # Shape [5]
loss = tf.reduce_mean(tf.square(samples))  # Arbitrary loss function
# Unbiased stochastic gradients of the loss function
grads = tf.gradients(loss, [concentration, rate])

References:

Implicit Reparameterization Gradients: Figurnov et al., 2018 (pdf)

concentration Floating point tensor, the concentration params of the distribution(s). Must contain only positive values.
rate Floating point tensor, the inverse scale params of the distribution(s). Must contain only positive values.
validate_args Python bool, default False. When True distribution parameters are checked for validity despite possibly degrading runtime performance. When False invalid inputs may silently render incorrect outputs.
allow_nan_stats Python bool, default True. When True, statistics (e.g., mean, mode, variance) use the value "NaN" to indicate the result is undefined. When False, an exception is raised if one or more of the statistic's batch members are undefined.
name Python str name prefixed to Ops created by this class.

TypeError if concentration and rate are different dtypes.

allow_nan_stats Python bool describing behavior when a stat is undefined.

Stats return +/- infinity when it makes sense. E.g., the variance of a Cauchy distribution is infinity. However, sometimes the statistic is undefined, e.g., if a distribution's pdf does not achieve a maximum within the support of the distribution, the mode is undefined. If the mean is undefined, then by definition the variance is undefined. E.g. the mean for Student's T for df = 1 is undefined (no clear way to say it is either + or - infinity), so the variance = E[(X - mean)**2] is also undefined.

batch_shape Shape of a single sample from a single event index as a TensorShape.

May be partially defined or unknown.

The batch dimensions are indexes into independent, non-identical parameterizations of this distribution.

concentration Concentration parameter.
dtype The DType of Tensors handled by this Distribution.
event_shape Shape of a single sample from a single batch as a TensorShape.

May be partially defined or unknown.

name Name prepended to all ops created by this Distribution.
parameters Dictionary of parameters used to instantiate this Distribution.
rate Rate parameter.
reparameterization_type Describes how samples from the distr