Binomial
_CountTrialDistributionBinomial(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: exact for every , with the algorithm chosen per batch element — pmf-table inversion below and BTRS transformed rejection above it.
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()
tensor(0.2668)Used by 3
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
4Mean of the Binomial distribution: .
Returns
TensorMean values of shape batch_shape.
Mode of the Binomial distribution: .
Support of the Binomial distribution: .
Returns
Constraintinteger_interval(0, total_count) — the bound is the
total_count tensor, so each batch member is held to its own
.
Variance of the Binomial distribution: .
Returns
TensorVariance values of shape batch_shape.
Instance methods
7Shannon entropy of the Binomial distribution (in nats).
summed over the whole support, as the reference framework sums it.
Returns
TensorEntropy in nats, shape batch_shape.
Raises
NotImplementedErrortotal_count differs across the batch: the support is
enumerated once, for one , as in the reference.Examples
>>> Binomial(total_count=2, probs=0.5).entropy() # 1.5 ln 2
tensor(1.04)The counts along axis 0, when total_count
is homogeneous across the batch.
Return a new Binomial instance with parameters expanded to batch_shape.
Log-odds parameter — as given, or derived from clamped probs on access.
Probability parameter — as given, or sigmoid(logits) on access.
Draw samples from the Binomial distribution.
Exact for every and , and the algorithm is chosen per batch element from its own : inversion of the pmf table below , the BTRS transformed-rejection sampler (Hoermann 1993) above it, on as the reference does. Arithmetic is float64 on the CPU and float32 on Metal, where counts beyond lose accuracy.
Parameters
sample_ shapetuple[int, ...]= ()Returns
TensorNon-negative integer-valued samples in
, shape
(*sample_shape, *batch_shape), in the dtype of probs.