Skip to content
class

NegativeBinomial

extends_CountTrialDistribution
NegativeBinomial(total_count: Tensor | float, probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)

Negative Binomial distribution over the number of failures before r successes.

NegativeBinomial(total_count=r, probs=p) models the count of failures kk before achieving rr successes in a sequence of independent Bernoulli trials each with success probability 1−p1 - p.

Equivalently, this is a Gamma-Poisson compound: draw λ∼Gamma⁡(r,(1−p)/p)\lambda \sim \operatorname{Gamma}(r, (1-p)/p), then X∼Poisson⁡(λ)X \sim \operatorname{Poisson}(\lambda). This interpretation extends the distribution to real-valued total_count (the generalised Negative Binomial) and forms the basis of the sampler.

Parameters

total_countTensor | float
Dispersion / number of successes r≥0r \geq 0. May be non-integer for the generalised version.
probsTensor | float | None= None
Probability of failure p∈[0,1)p \in [0, 1). Mutually exclusive with logits.
logitsTensor | float | None= None
Log-odds of failure l=log⁡(p/(1−p))∈Rl = \log(p / (1-p)) \in \mathbb{R}. Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Attributes

total_countTensor
Dispersion parameter rr, broadcast against the probability.
probsTensor
Failure probability — stored when given, otherwise derived from logits on access.
logitsTensor
Log-odds of failure — stored when given, otherwise derived from probs on access.

Notes

PMF:

P(X=k)=(k+r−1k)(1−p)rpk,k∈{0,1,2,…}P(X = k) = \binom{k + r - 1}{k} (1-p)^r p^k, \quad k \in \{0, 1, 2, \ldots\}

Log-PMF (via lgamma for numerical stability):

log⁡P(X=k)=log⁡Γ(k+r)−log⁡Γ(r)−log⁡Γ(k+1)+rlog⁡(1−p)+klog⁡p\log P(X = k) = \log\Gamma(k+r) - \log\Gamma(r) - \log\Gamma(k+1) + r \log(1-p) + k \log p

Moments:

  • Mean: E[X]=rp/(1−p)E[X] = r p / (1-p)
  • Variance: Var⁡[X]=rp/(1−p)2\operatorname{Var}[X] = r p / (1-p)^2

Sampler: uses the Gamma-Poisson representation. A standard-Gamma variate with concentration rr is drawn via rejection sampling, scaled by p/(1−p)p/(1-p), and passed to the Poisson sampler. This produces exact (non-approximate) samples for all real r>0r > 0.

Examples

>>> import lucid
>>> from lucid.distributions import NegativeBinomial
>>> dist = NegativeBinomial(total_count=5.0, probs=0.4)
>>> samples = dist.sample((100,))
>>> samples.shape
(100,)
>>> dist.mean  # r p / (1 - p) = 5 * 0.4 / 0.6
tensor(3.333)
>>> samples.dtype  # floating counts, like the parameters
lucid.float32
>>> NegativeBinomial(lucid.tensor([2.0, 5.0, 9.0]), probs=0.4).batch_shape
(3,)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(total_count: Tensor | float, probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)

Initialise a Negative Binomial distribution.

Parameters

total_countTensor | float
Dispersion / number of successes r≥0r \geq 0. May be non-integer for the generalised version.
probsTensor | float | None= None
Probability of failure p∈[0,1)p \in [0, 1). Mutually exclusive with logits.
logitsTensor | float | None= None
Log-odds of failure l=log⁡(p/(1−p))l = \log(p / (1-p)). Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Raises

ValueError
If both or neither of probs and logits are provided.

Properties

3
prop

mean

→Tensor
mean: Tensor

Mean of the Negative Binomial: E[X]=rp/(1−p)E[X] = r p / (1-p).

Returns

Tensor

Mean values of shape batch_shape.

prop

mode

→Tensor
mode: Tensor

Mode of the Negative Binomial distribution.

max⁡(⌊(r−1) p1−p⌋,0)\max\left(\left\lfloor \frac{(r - 1)\, p}{1 - p} \right\rfloor, 0\right)
prop

variance

→Tensor
variance: Tensor

Variance of the Negative Binomial: Var⁡[X]=rp/(1−p)2\operatorname{Var}[X] = r p / (1-p)^2.

Returns

Tensor

Variance values of shape batch_shape.

Instance methods

5
fn

expand

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

Return a new NegativeBinomial instance with parameters expanded to batch_shape.

fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-probability of counts under the Negative Binomial distribution.

log⁡P(X=k)=log⁡Γ(k+r)−log⁡Γ(r)−log⁡Γ(k+1)+rlog⁡(1−p)+klog⁡p\log P(X = k) = \log\Gamma(k+r) - \log\Gamma(r) - \log\Gamma(k+1) + r \log(1-p) + k \log p

The two logarithms are evaluated from the logit ℓ\ell as log⁡p=−softplus⁡(−ℓ)\log p = -\operatorname{softplus}(-\ell) and log⁡(1−p)=−softplus⁡(ℓ)\log(1-p) = -\operatorname{softplus}(\ell). Through probs they gave -inf once ∣ℓ∣|\ell| was large enough for pp to round to 0 or 1 (∣ℓ∣≳17|\ell| \gtrsim 17 in float32), where the answer is a finite −∣ℓ∣-|\ell|-sized value; and 0 · log 0 = NaN at p=0p = 0. A probs of exactly 0 or 1 is held one epsilon inside [0,1][0, 1] first, as the reference framework does.

At r=0r = 0 the distribution is the point mass at 0: the coefficient log⁡Γ(k+r)−log⁡Γ(r)\log\Gamma(k + r) - \log\Gamma(r) is ∞−∞\infty - \infty at k=0k = 0 and is taken as its limit 0, so log⁡P(X=0)=0\log P(X = 0) = 0 and every k>0k > 0 scores -inf, as in the reference framework.

Parameters

valueTensor
Non-negative integer counts k≥0k \geq 0.

Returns

Tensor

Log-probability values of the same shape as value.

fn

logits

→Tensorfrom _CountTrialDistribution
logits()

Log-odds parameter — as given, or derived from clamped probs on access.

fn

probs

→Tensorfrom _CountTrialDistribution
probs()

Probability parameter — as given, or sigmoid(logits) on access.

fn

sample

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

Draw samples via the Gamma-Poisson compound representation.

Draws λ∼Gamma⁡(r,(1−p)/p)\lambda \sim \operatorname{Gamma}(r, (1-p)/p) then X∼Poisson⁡(λ)X \sim \operatorname{Poisson}(\lambda). This produces exact (non-approximate) samples for all real r>0r > 0.

Parameters

sample_shapetuple[int, ...]= ()
Leading shape of the output sample batch.

Returns

Tensor

Non-negative integer-valued samples of shape (*sample_shape, *batch_shape), in the parameters' floating dtype.