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()
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
6Shannon 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.
Log-odds — as given, or derived on access.
Derived from probs clamped one epsilon inside , as
the reference framework derives it. log_prob does not use
this attribute: it keeps the exact, possibly infinite, log-odds that
its degenerate-probability guards are written around.
Success probability — as given, or sigmoid(logits) on access.
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).