NegativeBinomial
DistributionNegativeBinomial(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
before achieving successes in a sequence of independent
Bernoulli trials each with success probability .
Equivalently, this is a Gamma-Poisson compound: draw
, then
. This interpretation
extends the distribution to real-valued total_count (the
generalised Negative Binomial) and forms the basis of the sampler.
Parameters
total_countTensor | floatprobsTensor | float | None= Nonelogits.logitsTensor | float | None= Noneprobs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Attributes
total_countTensorprobsTensorprobs).logitsTensorlogits).Notes
PMF:
Log-PMF (via lgamma for numerical stability):
Moments:
- Mean:
- Variance:
Sampler: uses the Gamma-Poisson representation. A standard-Gamma variate with concentration is drawn via rejection sampling, scaled by , and passed to the Poisson sampler. This produces exact (non-approximate) samples for all real .
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.meanMethods (5)
__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 | floatprobsTensor | float | None= Nonelogits.logitsTensor | float | None= Noneprobs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorprobs and logits are provided.mean
→Tensormean: TensorMean of the Negative Binomial: .
Returns
TensorMean values of shape batch_shape.
variance
→Tensorvariance: TensorVariance of the Negative Binomial: .
Returns
TensorVariance values of shape batch_shape.
sample
→Tensorsample(sample_shape: tuple[int, ...] = ())Draw samples via the Gamma-Poisson compound representation.
Draws then . This produces exact (non-approximate) samples for all real .
Parameters
sample_shapetuple[int, ...]= ()Returns
TensorNon-negative integer samples of shape
(*sample_shape, *batch_shape).
log_prob
→Tensorlog_prob(value: Tensor)Log-probability of counts under the Negative Binomial distribution.
Parameters
valueTensorReturns
TensorLog-probability values of the same shape as value.