viva_math/distributions
Probability distributions.
Closed-form PDF / CDF / log-PDF + samplers backed by viva_math/random.
All samplers are pure: they take a Seed and return a new seed.
Scope
Continuous: gaussian, uniform, exponential, cauchy, laplace.
Discrete: bernoulli, categorical.
For heavier statistics (gamma, beta, chi-square) prefer gleam_stats
or gleam_community_maths. Here we ship only what viva_emotion and
viva_tensor consume directly.
Types
pub type Categorical {
Categorical(probs: List(Float))
}
Constructors
-
Categorical(probs: List(Float))
pub type Cauchy {
Cauchy(location: Float, scale: Float)
}
Constructors
-
Cauchy(location: Float, scale: Float)
pub type Exponential {
Exponential(rate: Float)
}
Constructors
-
Exponential(rate: Float)
Gaussian parameters.
pub type Gaussian {
Gaussian(mean: Float, stddev: Float)
}
Constructors
-
Gaussian(mean: Float, stddev: Float)
pub type Laplace {
Laplace(location: Float, scale: Float)
}
Constructors
-
Laplace(location: Float, scale: Float)
Values
pub fn bernoulli_pmf(b: Bernoulli, k: Int) -> Float
pub fn bernoulli_sample(
b: Bernoulli,
seed: random.Seed,
) -> #(Bool, random.Seed)
pub fn categorical_entropy(c: Categorical) -> Float
Entropy H(X) = -Σ pᵢ log pᵢ.
pub fn categorical_pmf(c: Categorical, k: Int) -> Float
PMF for a category index (zero-based).
pub fn categorical_sample(
c: Categorical,
seed: random.Seed,
) -> Result(#(Int, random.Seed), Nil)
pub fn cauchy_pdf(c: Cauchy, x: Float) -> Float
pub fn cauchy_sample(
c: Cauchy,
seed: random.Seed,
) -> #(Float, random.Seed)
pub fn exponential_pdf(e: Exponential, x: Float) -> Float
PDF f(x; λ) = λ · e^(-λx) for x ≥ 0.
pub fn exponential_sample(
e: Exponential,
seed: random.Seed,
) -> #(Float, random.Seed)
Inverse-CDF sampling.
Uses x = -log1p(-U) / λ, which is numerically equivalent to
-ln(1 - U) / λ but avoids cancellation when U is near zero. Since
random.uniform returns U ∈ [0, 1), 1 - U ∈ (0, 1] never hits zero.
pub fn gaussian_cdf(g: Gaussian, x: Float) -> Float
Cumulative distribution F(x; μ, σ) = ½ · (1 + erf((x - μ)/(σ√2))).
pub fn gaussian_log_pdf(g: Gaussian, x: Float) -> Float
Log-density. More numerically stable than ln(gaussian_pdf).
pub fn gaussian_pdf(g: Gaussian, x: Float) -> Float
Probability density f(x; μ, σ) = 1/(σ√(2π)) · exp(-(x-μ)²/(2σ²)).
pub fn gaussian_sample(
g: Gaussian,
seed: random.Seed,
) -> #(Float, random.Seed)
Sample from a Gaussian.
pub fn laplace_pdf(l: Laplace, x: Float) -> Float
pub fn laplace_sample(
l: Laplace,
seed: random.Seed,
) -> #(Float, random.Seed)
pub fn uniform_pdf(u: Uniform, x: Float) -> Float
PDF on [low, high]; zero elsewhere.
pub fn uniform_sample(
u: Uniform,
seed: random.Seed,
) -> #(Float, random.Seed)