Skip to content
class

Binomial

extends_CountTrialDistribution
Binomial(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 nn i.i.d. Bernoulli trials has success probability pp. Parameterisation is via either probs (in [0,1][0, 1]) or logits (the log-odds log⁡(p/(1−p))∈R\log(p/(1-p)) \in \mathbb{R}); exactly one must be supplied.

Parameters

total_countTensor | int= 1
Number of trials n≥0n \geq 0. Default is 1 (reduces to Bernoulli).
probsTensor | float | None= None
Success probability p∈[0,1]p \in [0, 1]. Mutually exclusive with logits.
logitsTensor | float | None= None
Log-odds l=log⁡(p/(1−p))∈Rl = \log(p / (1-p)) \in \mathbb{R}. Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Attributes

total_countTensor
Number of trials nn, broadcast against the probability.
probsTensor
Success probability — stored when given, otherwise derived from logits on access.
logitsTensor
Log-odds — stored when given, otherwise derived from probs on access.

Notes

PMF:

P(X=k)=(nk)pk(1−p)n−k,k∈{0,1,…,n}P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}, \quad k \in \{0, 1, \ldots, n\}

Log-PMF via logits (numerically stable form):

log⁡P(X=k)=log⁡(nk)+k l−nlog⁡(1+el)\log P(X = k) = \log\binom{n}{k} + k \, l - n \log(1 + e^l)

where l=logit⁡(p)l = \operatorname{logit}(p) and the binomial coefficient is evaluated via log⁡Γ(n+1)−log⁡Γ(k+1)−log⁡Γ(n−k+1)\log \Gamma(n+1) - \log\Gamma(k+1) - \log\Gamma(n-k+1).

Moments:

  • Mean: E[X]=npE[X] = np
  • Variance: Var⁡[X]=np(1−p)\operatorname{Var}[X] = np(1-p)

Sampling strategy: exact for every nn, with the algorithm chosen per batch element — pmf-table inversion below np=10np = 10 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
dunder

__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= 1
Number of trials n≥0n \geq 0. Default is 1 (reduces to Bernoulli).
probsTensor | float | None= None
Success probability p∈[0,1]p \in [0, 1]. Mutually exclusive with logits.
logitsTensor | float | None= None
Log-odds l=log⁡(p/(1−p))∈Rl = \log(p / (1-p)) \in \mathbb{R}. Mutually exclusive with probs.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Raises

ValueError
If both or neither of probs and logits are provided.

Properties

4
prop

mean

→Tensor
mean: Tensor

Mean of the Binomial distribution: E[X]=npE[X] = np.

Returns

Tensor

Mean values of shape batch_shape.

prop

mode

→Tensor
mode: Tensor

Mode of the Binomial distribution: min⁡(⌊(n+1)p⌋,n)\min(\lfloor (n+1) p \rfloor, n).

prop

support

→Constraint
support: Constraint

Support of the Binomial distribution: {0,1,…,n}\{0, 1, \ldots, n\}.

Returns

Constraint

integer_interval(0, total_count) — the bound is the total_count tensor, so each batch member is held to its own nn.

prop

variance

→Tensor
variance: Tensor

Variance of the Binomial distribution: Var⁡[X]=np(1−p)\operatorname{Var}[X] = np(1-p).

Returns

Tensor

Variance values of shape batch_shape.

Instance methods

7
fn

entropy

→Tensor
entropy()

Shannon entropy of the Binomial distribution (in nats).

H=−∑k=0nP(X=k)log⁡P(X=k)H = -\sum_{k=0}^{n} P(X = k) \log P(X = k)

summed over the whole support, as the reference framework sums it.

Returns

Tensor

Entropy in nats, shape batch_shape.

Raises

NotImplementedError
If total_count differs across the batch: the support is enumerated once, for one nn, as in the reference.

Examples

>>> Binomial(total_count=2, probs=0.5).entropy()  # 1.5 ln 2
tensor(1.04)
fn

enumerate_support

→Tensor
enumerate_support(expand: bool = True)

The counts 0,…,n0, \ldots, n along axis 0, when total_count is homogeneous across the batch.

fn

expand

→Self
expand(batch_shape: Sequence[int], _instance: Self | None = None)

Return a new Binomial instance with parameters expanded to batch_shape.

fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-probability of the given counts under the Binomial distribution.

Computed via logits for numerical stability:

log⁡P(X=k)=log⁡(nk)+k l−nlog⁡(1+el)\log P(X = k) = \log\binom{n}{k} + k \, l - n \log(1 + e^l)

where l=logit⁡(p)l = \operatorname{logit}(p) and the binomial coefficient is evaluated via log⁡Γ\log\Gamma.

Parameters

valueTensor
Count values k∈{0,1,…,n}k \in \{0, 1, \ldots, n\}.

Returns

Tensor

Log-probability values of the same shape as value.

fn

logits

→Tensorfrom _CountTrialDistribution
logits()

Log-odds parameter — as given, or derived from clamped probs on access.

fn

probs

→Tensorfrom _CountTrialDistribution
probs()

Probability parameter — as given, or sigmoid(logits) on access.

fn

sample

→Tensor
sample(sample_shape: tuple[int, ...] = ())

Draw samples from the Binomial distribution.

Exact for every nn and pp, and the algorithm is chosen per batch element from its own npnp: inversion of the pmf table below np=10np = 10, the BTRS transformed-rejection sampler (Hoermann 1993) above it, on min⁡(p,1−p)\min(p, 1-p) as the reference does. Arithmetic is float64 on the CPU and float32 on Metal, where counts beyond n≈106n \approx 10^6 lose accuracy.

Parameters

sample_shapetuple[int, ...]= ()
Leading shape of the output sample batch.

Returns

Tensor

Non-negative integer-valued samples in {0,1,…,n}\{0, 1, \ldots, n\}, shape (*sample_shape, *batch_shape), in the dtype of probs.