Bernoulli
ExponentialFamilyBernoulli(probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)Bernoulli distribution over .
The Bernoulli is the simplest discrete distribution: a single coin
flip with success probability . It models a binary
outcome and is the building block of the Binomial (sum of n IID
Bernoullis), the Categorical (its multinomial generalisation), and most
classification likelihoods in supervised learning.
Specify exactly one of probs or logits; the other is derived
lazily via the sigmoid / logit transform so there is no redundant
storage and the parameterisation chosen at construction remains the
canonical one for autograd.
Parameters
probsTensor or float= Nonelogits.logitsTensor or float= Noneprobs.validate_argsbool= NoneTrue, validate parameter constraints at construction time.Notes
Probability mass function on :
Moments:
The variance is maximised at (maximum uncertainty) and vanishes at the degenerate endpoints .
Relation to other distributions:
- is the sum of independent draws.
- counts Bernoulli failures before the first success.
- generalises Bernoulli to categories.
Conjugate prior: lucid.distributions.Beta — observing
successes out of updates
Beta(α, β) → Beta(α + k, β + n - k).
Examples
>>> import lucid
>>> from lucid.distributions import Bernoulli
>>> d = Bernoulli(probs=0.7)
>>> d.mean
Tensor(0.7)
>>> d.sample((4,))
Tensor([...])
>>> d.log_prob(lucid.tensor(1.0))
Tensor(-0.3567)Methods (7)
__init__
→None__init__(probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)Construct a Bernoulli distribution.
Exactly one of probs or logits must be supplied. The other
is derived on demand via the sigmoid / logit transform, so there is no
redundant storage.
Parameters
probsTensor | float | None= Nonelogits.logitsTensor | float | None= Noneprobs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorprobs / logits are provided.Examples
>>> from lucid.distributions import Bernoulli
>>> d = Bernoulli(probs=0.7)
>>> d.mean
Tensor(0.7)
>>> d2 = Bernoulli(logits=0.0) # p = 0.5
>>> d2.probs # derived lazily
Tensor(0.5)param
→Tensorparam: TensorThe canonical parameter used at construction time.
Returns the logits tensor when the distribution was constructed with
logits, or the probs tensor when constructed with probs.
This is used by ExponentialFamily machinery to access the
sufficient statistic parameter without forcing a conversion.
Returns
TensorEither self.logits or self.probs, depending on which was
provided at construction.
Examples
>>> d = Bernoulli(probs=0.3)
>>> d.param # returns self.probs
Tensor(0.3)mean
→Tensormean: TensorExpected value of the distribution.
For a Bernoulli random variable with success probability :
Returns
TensorSuccess probability , shape batch_shape.
Examples
>>> Bernoulli(probs=0.3).mean
Tensor(0.3)variance
→Tensorvariance: TensorVariance of the distribution.
For a Bernoulli random variable with success probability :
The variance is maximised at and equals zero at the degenerate extremes .
Returns
TensorVariance , shape batch_shape.
Examples
>>> Bernoulli(probs=0.5).variance
Tensor(0.25)sample
→Tensorsample(sample_shape: tuple[int, ...] = ())Draw samples from the Bernoulli distribution.
Samples by comparing uniform noise against the success probability :
The returned tensor has integer-valued entries in stored as floating-point and is detached from the autograd graph.
Parameters
sample_shapetuple[int, ...]= ()sample_shape + batch_shape. Default is ().Returns
TensorBinary samples of shape sample_shape + batch_shape.
Examples
>>> d = Bernoulli(probs=0.6)
>>> x = d.sample((1000,))
>>> x.mean() # approximately 0.6log_prob
→Tensorlog_prob(value: Tensor)Log-probability of value under the Bernoulli distribution.
Uses the numerically stable logits form to avoid :
where is the log-odds. This is equivalent to the cross-entropy form but avoids numerical issues at the boundaries .
Parameters
valueTensorReturns
TensorElement-wise log-probabilities, shape batch_shape.
Examples
>>> d = Bernoulli(probs=0.7)
>>> d.log_prob(lucid.tensor(1.0)) # log(0.7)
Tensor(-0.3567)entropy
→Tensorentropy()Shannon entropy of the Bernoulli distribution (in nats).
Computed in the numerically stable softplus form:
where is the log-odds. The entropy is maximised at (maximum uncertainty) and is zero at the degenerate cases .
Returns
TensorEntropy in nats, shape batch_shape.
Examples
>>> Bernoulli(probs=0.5).entropy() # log(2) ≈ 0.693
Tensor(0.6931)