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
1 (reduces to
Bernoulli).logits.probs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Attributes
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()Used by 1
Constructors
1__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
1 (reduces to
Bernoulli).logits.probs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorprobs and logits are provided.Properties
3Mean of the Binomial distribution: .
Returns
TensorMean values of shape batch_shape.
Support 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.
Variance of the Binomial distribution: .
Returns
TensorVariance values of shape batch_shape.
Instance methods
2Draw 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).