class

Beta

extendsExponentialFamily
Beta(concentration1: Tensor | float, concentration0: Tensor | float, validate_args: bool | None = None)
source

Beta distribution on the open interval (0,1)(0, 1).

Two-parameter continuous distribution that is the conjugate prior of the lucid.distributions.Bernoulli / Binomial likelihoods. When α=β=1\alpha = \beta = 1 it reduces to a Uniform on (0,1)(0, 1); for large α+β\alpha + \beta it concentrates around α/(α+β)\alpha/(\alpha + \beta).

Parameters

concentration1Tensor or float
First shape parameter α>0\alpha > 0 (sometimes called a).
concentration0Tensor or float
Second shape parameter β>0\beta > 0 (sometimes called b).
validate_argsbool= None
If True, validate parameter constraints at construction time.

Notes

Probability density on x(0,1)x \in (0, 1):

p(x;α,β)=xα1(1x)β1B(α,β)p(x; \alpha, \beta) = \frac{x^{\alpha - 1} (1 - x)^{\beta - 1}}{B(\alpha, \beta)}

where B(α,β)=Γ(α)Γ(β)/Γ(α+β)B(\alpha, \beta) = \Gamma(\alpha)\Gamma(\beta)/\Gamma(\alpha + \beta).

Moments:

E[X]=αα+β,Var[X]=αβ(α+β)2(α+β+1)\mathbb{E}[X] = \frac{\alpha}{\alpha + \beta}, \qquad \mathrm{Var}[X] = \frac{\alpha \beta} {(\alpha + \beta)^2 (\alpha + \beta + 1)}

Mode: (α1)/(α+β2)(\alpha - 1) / (\alpha + \beta - 2) for α,β>1\alpha, \beta > 1.

Special cases / shapes:

  • α=β=1\alpha = \beta = 1Uniform(0,1)\mathrm{Uniform}(0, 1).
  • α=β\alpha = \beta → symmetric around 0.50.5.
  • α,β\alpha, \beta \to \infty with fixed mean → concentrates as a Gaussian about the mean.
  • Bimodal (U-shaped) when both α,β<1\alpha, \beta < 1.

Conjugacy: observing kk successes out of nn Bernoulli trials updates Beta(α, β) → Beta(α + k, β + n - k).

Sampling uses the ratio-of-Gammas method: X=Gα/(Gα+Gβ)X = G_\alpha / (G_\alpha + G_\beta) with independent GαGamma(α,1)G_\alpha \sim \mathrm{Gamma}(\alpha, 1), GβGamma(β,1)G_\beta \sim \mathrm{Gamma}(\beta, 1). Since the underlying Gamma sampler is rejection-based, samples are detached.

Examples

>>> import lucid
>>> from lucid.distributions import Beta
>>> d = Beta(concentration1=2.0, concentration0=5.0)
>>> d.mean  # 2/(2+5) ≈ 0.2857
Tensor(0.2857)
>>> d.sample((4,))
Tensor([...])
>>> d.log_prob(lucid.tensor(0.3))
Tensor(...)

Methods (6)

dunder

__init__

None
__init__(concentration1: Tensor | float, concentration0: Tensor | float, validate_args: bool | None = None)
source

Construct a Beta distribution.

Parameters

concentration1Tensor | float
First shape parameter α>0\alpha > 0 (also called concentration1). Controls how much mass is near 1.
concentration0Tensor | float
Second shape parameter β>0\beta > 0 (also called concentration0). Controls how much mass is near 0.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Notes

The Beta distribution has PDF:

p(x;α,β)=xα1(1x)β1B(α,β),x[0,1]p(x; \alpha, \beta) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}, \quad x \in [0, 1]

where B(α,β)=Γ(α)Γ(β)/Γ(α+β)B(\alpha, \beta) = \Gamma(\alpha)\Gamma(\beta)/\Gamma(\alpha+\beta).

Sampling uses the ratio-of-Gammas method: X=Gα/(Gα+Gβ)X = G_\alpha / (G_\alpha + G_\beta) where GαGamma(α,1)G_\alpha \sim \text{Gamma}(\alpha, 1).

Examples

>>> from lucid.distributions import Beta
>>> d = Beta(concentration1=2.0, concentration0=5.0)
>>> d.mean  # α/(α+β) = 2/7 ≈ 0.286
Tensor(0.2857)
prop

mean

Tensor
mean: Tensor
source

Expected value of the Beta distribution.

E[X]=αα+βE[X] = \frac{\alpha}{\alpha + \beta}

Returns

Tensor

Mean α/(α+β)\alpha/(\alpha+\beta), shape batch_shape.

Examples

>>> Beta(concentration1=1.0, concentration0=1.0).mean
Tensor(0.5)
prop

variance

Tensor
variance: Tensor
source

Variance of the Beta distribution.

Var[X]=αβ(α+β)2(α+β+1)\operatorname{Var}[X] = \frac{\alpha \beta}{(\alpha+\beta)^2 (\alpha+\beta+1)}

Returns

Tensor

Variance, shape batch_shape.

Examples

>>> Beta(concentration1=2.0, concentration0=2.0).variance
Tensor(0.05)
fn

sample

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

Draw samples from the Beta distribution.

Uses the ratio-of-independent-Gamma-samples identity:

X=GαGα+Gβ,GαGamma(α,1),  GβGamma(β,1)X = \frac{G_\alpha}{G_\alpha + G_\beta}, \quad G_\alpha \sim \text{Gamma}(\alpha, 1),\; G_\beta \sim \text{Gamma}(\beta, 1)

The result is detached since the underlying Gamma sampler is rejection-based and does not support reparameterisation.

Parameters

sample_shapetuple[int, ...]= ()
Leading shape dimensions for the sample batch. Default is ().

Returns

Tensor

Samples in [0,1][0, 1] of shape sample_shape + batch_shape.

Examples

>>> d = Beta(concentration1=2.0, concentration0=5.0)
>>> x = d.sample((500,))
>>> x.mean()  # approximately 2/7 ≈ 0.286
fn

log_prob

Tensor
log_prob(value: Tensor)
source

Log-density of value under the Beta distribution.

logp(x;α,β)=(α1)logx+(β1)log(1x)logB(α,β)\log p(x; \alpha, \beta) = (\alpha - 1) \log x + (\beta - 1) \log(1-x) - \log B(\alpha, \beta)

Parameters

valueTensor
Values in (0,1)(0, 1).

Returns

Tensor

Element-wise log-densities, shape batch_shape.

fn

entropy

Tensor
entropy()
source

Shannon entropy of the Beta distribution (in nats).

H(X)=logB(α,β)(α1)ψ(α)(β1)ψ(β)+(α+β2)ψ(α+β)H(X) = \log B(\alpha,\beta) - (\alpha-1)\psi(\alpha) - (\beta-1)\psi(\beta) + (\alpha+\beta-2)\psi(\alpha+\beta)

where ψ\psi is the digamma function and B(α,β)=Γ(α)Γ(β)/Γ(α+β)B(\alpha,\beta) = \Gamma(\alpha)\Gamma(\beta)/\Gamma(\alpha+\beta).

Returns

Tensor

Entropy in nats, shape batch_shape.