Geometric
DistributionGeometric(probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)Geometric distribution: number of failures before the first success.
Models the number of failed Bernoulli trials preceding the first success in an unbounded sequence of independent trials with success probability . This is the shifted convention (counting failures); the alternative convention counts trials including the success and starts at .
Parameters
probsTensor or float= Nonelogits.logitsTensor or float= Noneprobs via
the sigmoid at construction. Mutually exclusive with probs.validate_argsbool= NoneTrue, validate parameter constraints at construction time.Notes
Probability mass function:
Moments:
Memoryless property — the Geometric is the only discrete distribution with the memoryless property:
Continuous analogue: lucid.distributions.Exponential
(the only continuous memoryless distribution).
The expected number of trials (including the success) is ; entropy grows without bound as and is zero at .
Examples
>>> import lucid
>>> from lucid.distributions import Geometric
>>> d = Geometric(probs=0.25)
>>> d.mean # (1 - p)/p = 3.0
Tensor(3.0)
>>> d.sample((4,))
Tensor([...])
>>> d.log_prob(lucid.tensor(2.0))
Tensor(...)Methods (6)
__init__
→None__init__(probs: Tensor | float | None = None, logits: Tensor | float | None = None, validate_args: bool | None = None)Construct a Geometric distribution.
Exactly one of probs or logits must be provided. If
logits is given, it is converted to probs via the sigmoid
transform and stored as self.probs.
Parameters
probsTensor | float | None= Nonelogits.logitsTensor | float | None= Noneprobs at construction time. Mutually exclusive with
probs.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorprobs / logits are provided.Notes
The support is representing the number of failures before the first success. The PMF is:
Examples
>>> from lucid.distributions import Geometric
>>> d = Geometric(probs=0.25)
>>> d.mean # E[X] = (1-p)/p = 3
Tensor(3.0)mean
→Tensormean: TensorExpected number of failures before the first success.
Returns
TensorMean , shape batch_shape.
Examples
>>> Geometric(probs=0.5).mean
Tensor(1.0)variance
→Tensorvariance: TensorVariance of the number of failures before the first success.
Returns
TensorVariance , shape batch_shape.
Examples
>>> Geometric(probs=0.5).variance
Tensor(2.0)sample
→Tensorsample(sample_shape: tuple[int, ...] = ())Draw samples from the Geometric distribution.
Uses the inverse-CDF trick: if ,
A small epsilon clamp guards against which would produce . The returned tensor is detached since the Geometric is discrete.
Parameters
sample_shapetuple[int, ...]= ()sample_shape + batch_shape. Default is ().Returns
TensorNon-negative integer samples of shape sample_shape + batch_shape.
Examples
>>> d = Geometric(probs=0.5)
>>> x = d.sample((1000,))
>>> x.mean() # approximately 1.0log_prob
→Tensorlog_prob(value: Tensor)Log-probability of value under the Geometric distribution.
Parameters
valueTensorReturns
TensorElement-wise log-probabilities, shape batch_shape.
Examples
>>> d = Geometric(probs=0.5)
>>> d.log_prob(lucid.tensor(0.0)) # log(0.5) ≈ -0.693
Tensor(-0.6931)entropy
→Tensorentropy()Shannon entropy of the Geometric distribution (in nats).
The entropy grows without bound as (more uncertainty over many possible outcomes) and is zero at (certain success on first trial).
Returns
TensorEntropy in nats, shape batch_shape.
Examples
>>> Geometric(probs=0.5).entropy()
Tensor(1.3863)