Skip to content
class

Beta

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

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(1−x)β−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 = 1 → Uniform(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,)).shape
(4,)
>>> d.log_prob(lucid.tensor(0.3))  # log(30 * 0.3 * 0.7**4)
tensor(0.7705)

Used by 4

Constructors

1
dunder

__init__

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

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(1−x)β−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)

Properties

3
prop

mean

→Tensor
mean: Tensor

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

mode

→Tensor
mode: Tensor

Mode of the Beta distribution.

α−1α+β−2(α,β≥1)\frac{\alpha - 1}{\alpha + \beta - 2} \quad (\alpha, \beta \geq 1)

with each excess α−1\alpha - 1, β−1\beta - 1 floored at 0, so one concentration below 1 puts the mode at that end. With both below 1 the density is unbounded at both ends and 1 is returned; at α=β=1\alpha = \beta = 1 (uniform) the answer is nan — both as the reference framework answers.

prop

variance

→Tensor
variance: Tensor

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)

Instance methods

5
fn

entropy

→Tensor
entropy()

Shannon entropy of the Beta distribution (in nats).

H(X)=log⁡B(α,β)−(α−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.

fn

expand

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

Return a new Beta with batch dimensions expanded to batch_shape.

fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-density of value under the Beta distribution.

log⁡p(x;α,β)=(α−1)log⁡x+(β−1)log⁡(1−x)−log⁡B(α,β)\log p(x; \alpha, \beta) = (\alpha - 1) \log x + (\beta - 1) \log(1-x) - \log B(\alpha, \beta)

Each power term is taken as 0 when its exponent is, so Beta(1, 1) scores both endpoints as log⁡1=0\log 1 = 0 rather than 0 · (-inf) = NaN.

Parameters

valueTensor
Values in [0,1][0, 1].

Returns

Tensor

Element-wise log-densities, shape batch_shape.

fn

rsample

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

Draw reparameterised samples from the Beta distribution.

Uses X=Gα/(Gα+Gβ)X = G_\alpha / (G_\alpha + G_\beta) where Gα∼Gamma(α,1)G_\alpha \sim \text{Gamma}(\alpha, 1) and Gβ∼Gamma(β,1)G_\beta \sim \text{Gamma}(\beta, 1) carry implicit-reparameterisation gradients. The quotient is the sigmoid of log⁡Gα−log⁡Gβ\log G_\alpha - \log G_\beta, so it stays ordered at any concentration, where the gammas themselves would underflow.

fn

sample

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

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)

Parameters

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

Returns

Tensor

Samples of shape sample_shape + batch_shape, held in [tiny, 1 - eps/2] — off both ends of the unit interval, so log_prob of a draw is finite — where tiny and eps are the dtype's smallest normal number and machine epsilon.

Examples

>>> _ = lucid.manual_seed(0)
>>> d = Beta(concentration1=2.0, concentration0=5.0)
>>> x = d.sample((500,))
>>> x.shape
(500,)
>>> bool((x.mean() - 2 / 7).abs() < 0.03)  # the sample mean approaches 2/7
True