Binomial
DistributionBinomial(total_count: Tensor | int = 1, probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)Binomial distribution over the number of successes in n independent trials.
Binomial(total_count=n, probs=p) models the count of successes when
each of i.i.d. Bernoulli trials has success probability
. Parameterisation is via either probs (in )
or logits (the log-odds );
exactly one must be supplied.
Parameters
total_countTensor | int= 11 (reduces to
Bernoulli).probsTensor | 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 logits (numerically stable form):
where and the binomial coefficient is evaluated via .
Moments:
- Mean:
- Variance:
Sampling strategy: for Bernoulli draws are summed exactly along a dedicated axis. For larger a Normal approximation is used with the result rounded and clamped to .
Examples
>>> import lucid
>>> from lucid.distributions import Binomial
>>> dist = Binomial(total_count=10, probs=0.3)
>>> samples = dist.sample((50,))
>>> samples.shape
(50,)
>>> # PMF at k=3
>>> dist.log_prob(lucid.tensor(3.0)).exp()Methods (6)
__init__
→None__init__(total_count: Tensor | int = 1, probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)Initialise a Binomial distribution.
Parameters
total_countTensor | int= 11 (reduces to
Bernoulli).probsTensor | float | None= Nonelogits.logitsTensor | float | None= Noneprobs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorprobs and logits are provided.support
→Constraintsupport: ConstraintSupport of the Binomial distribution: non-negative integers.
Although the strict support is per element,
this property returns nonnegative_integer because total_count
may differ across the batch.
Returns
ConstraintThe nonnegative_integer constraint.
mean
→Tensormean: TensorMean of the Binomial distribution: .
Returns
TensorMean values of shape batch_shape.
variance
→Tensorvariance: TensorVariance of the Binomial distribution: .
Returns
TensorVariance values of shape batch_shape.
sample
→Tensorsample(sample_shape: tuple[int, ...] = ())Draw samples from the Binomial distribution.
Uses an exact strategy for small (sum of Bernoulli draws) and a Normal approximation with rounding for large .
Parameters
sample_shapetuple[int, ...]= ()Returns
TensorNon-negative integer samples in ,
shape (*sample_shape, *batch_shape).
log_prob
→Tensorlog_prob(value: Tensor)Log-probability of the given counts under the Binomial distribution.
Computed via logits for numerical stability:
where and the binomial coefficient is evaluated via .
Parameters
valueTensorReturns
TensorLog-probability values of the same shape as value.