Skip to main content

Module probability

Module probability 

Source

Functions§

beta_moment_matched_interval
Equal-tailed predictive interval for a (0, 1)-bounded response modelled as a Beta whose first two moments match a point prediction: mean mu ∈ (0, 1) and total predictive variance total_var (estimation + observation noise). Returns the pair of Beta quantiles at lower-tail probabilities p_lo < p_hi — the skew-correct replacement for a symmetric mu ± z·σ band, which for a skewed Beta lands both edges below the corresponding true quantile and so mis-covers each tail (#1194).
beta_quantile
Quantile (inverse CDF) of a Beta distribution with shape parameters a > 0 and b > 0 at probability p ∈ (0, 1): the value x ∈ [0, 1] with I_x(a, b) = p, where I is the regularized incomplete beta (the Beta CDF).
binomial_coefficient_f64
Numerically stable C(n,k) = n! / (k!·(n−k)!) as f64. The implementation now lives in the lowest crate (gam-math) so the terms/basis cluster can consume it without reaching up into inference; re-exported here to keep crate::probability::binomial_coefficient_f64 resolving for all existing callers. Numerically stable C(n,k) = n! / (k!·(n−k)!) as f64. Uses the symmetry C(n,k) = C(n, n−k) to keep the loop count min(k, n−k) and the multiplicative recurrence C(n,j+1) = C(n,j)·(n−j)/(j+1), avoiding the overflow of separate factorial evaluations. Returns 0.0 for k > n and exact integer results within 2^53.
erfcx_nonnegative
Scaled complementary error function erfcx(x) = exp(x²) · erfc(x), specialized to x ≥ 0. The implementation now lives in the lowest crate (gam-math) so the survival/probit cluster can consume it without reaching up into inference; re-exported here to keep crate::probability::erfcx_nonnegative resolving for all existing callers. Scaled complementary error function erfcx(x) = exp(x²) · erfc(x), specialized to x ≥ 0. Returns 1.0 for x ≤ 0 and 0.0 for x = +∞. For 0 < x < 26 uses the direct exp(x²)·erfc(x) form; beyond that the (otherwise overflowing) exp(x²) is replaced by a 4-term asymptotic expansion (1/(x√π))·(1 − 1/(2x²) + 3/(4x⁴) − …), keeping relative accuracy near machine epsilon. The non-negative restriction lets the caller skip the reflection identity.
gamma_moment_matched_interval
Equal-tailed predictive interval for a strictly-positive, right-skewed response modelled as a Gamma whose first two moments match a point prediction: mean mu and total predictive variance total_var (estimation + observation noise). Returns the pair of Gamma quantiles at lower-tail probabilities p_lo < p_hi — the skew-correct replacement for a symmetric mu ± z·σ band, which for a Gamma pins the lower edge near the support floor and mis-covers each tail (#817).
gamma_quantile
Quantile (inverse CDF) of a Gamma distribution parameterized by shape k > 0 and scale θ > 0 at probability p ∈ (0, 1): the value x with P(X ≤ x) = p for X ~ Gamma(shape = k, scale = θ) (mean , variance kθ²).
log1mexp_positive
Computes log(1 - exp(-a)) for a >= 0 without cancellation. Implementation lives in gam-math; re-exported to keep crate::probability::log1mexp_positive resolving for all existing callers. Computes log(1 - exp(-a)) for a >= 0 without cancellation.
negative_binomial_moment_matched_interval
Equal-tailed predictive interval for a Negative-Binomial count response whose conditional law has mean mu > 0 and dispersion theta > 0, widened for estimation uncertainty to a total predictive variance total_var (estimation + observation noise). Returns the pair of integer NB quantiles at lower-tail probabilities p_lo < p_hi — the skew-correct, zero-atom-aware replacement for a symmetric mu ± z·σ band, which on right-skewed counts sits below the true upper quantile and under-covers the upper tail (#1193).
negative_binomial_quantile
Quantile (inverse CDF) of a Negative-Binomial with mean μ ≥ 0 and dispersion θ > 0 at probability p ∈ (0, 1): the smallest integer count k ≥ 0 with P(Y ≤ k) ≥ p, returned as an f64.
normal_cdf
Standard normal CDF Φ(x) evaluated via the exact special-function identity
normal_logcdf
Numerically stable ln Φ(x) for the standard normal CDF. Implementation lives in gam-math; re-exported to keep crate::probability::normal_logcdf resolving for all existing callers. Numerically stable ln Φ(x) for the standard normal CDF. For x ≥ 0 computes ln(Φ(x)) directly with a small floor against underflow; for x < 0 rewrites ln Φ(x) = −u² + ln(½·erfcx(u)), u = −x/√2, which preserves digits all the way into the deep left tail (no ln(0)). Returns ±∞ and NaN at the corresponding inputs.
normal_logsf
Numerically stable ln(1 − Φ(x)) = ln Φ(−x) for the standard normal survival function. Implementation lives in gam-math; re-exported to keep crate::probability::normal_logsf resolving for all existing callers. Numerically stable ln(1 − Φ(x)) = ln Φ(−x) for the standard normal survival function. Delegates to normal_logcdf(-x) so the deep-right tail benefits from the same erfcx-based representation.
normal_pdf
Standard normal PDF φ(x).
poisson_moment_matched_interval
Equal-tailed predictive interval for a Poisson count response whose conditional law has mean mu > 0 (so Var(Y|μ) = μ), widened for estimation uncertainty to a total predictive variance total_var ≥ μ (estimation + observation noise). Returns the pair of integer quantiles at lower-tail probabilities p_lo < p_hi — the skew-correct, zero-atom-aware replacement for a symmetric mu ± z·σ band, which on low-rate counts sits below the true upper quantile and under-covers the upper tail (the #817 defect, Poisson sibling of #1193).
poisson_quantile
Quantile (inverse CDF) of a Poisson with mean mu ≥ 0 at probability p ∈ (0, 1): the smallest integer count k ≥ 0 with P(Y ≤ k) ≥ p, returned as an f64.
signed_log_sum_exp
Numerically stable signed log-sum-exp. Implementation lives in gam-math; re-exported to keep crate::probability::signed_log_sum_exp resolving for all existing callers. Numerically stable signed log-sum-exp. Given pairs (log|aⱼ|, sign(aⱼ)) (with signs[j] ∈ {−1, 0, +1}), returns (log|S|, sign(S)) for S = Σⱼ signs[j]·exp(log_mags[j]). Positive and negative magnitudes are reduced separately with the standard log-sum-exp trick (subtract the max, sum, log, add back); the two partial sums are then combined via log(|p − n|) = max(log p, log n) + log1mexp(|log p − log n|), preserving accuracy even when p ≈ n (catastrophic cancellation regime). When all signs are zero or all magnitudes are −∞, returns (NEG_INFINITY, 0.0).
signed_probit_logcdf_and_mills_ratio
Joint evaluation of ln Φ(x) and the Mills-ratio analogue φ(x) / Φ(x). Implementation lives in gam-math; re-exported to keep crate::probability::signed_probit_logcdf_and_mills_ratio resolving for all existing callers. Joint evaluation of ln Φ(x) and the Mills-ratio analogue φ(x) / Φ(x), signed for the symmetric branch. Used by the latent probit families where the inverse-link gradient needs the ratio and the likelihood needs the log-CDF on the same x; computing both in one call shares the erfcx evaluation that dominates the cost in the deep tail.
stable_polynomial_times_exp_neg
Evaluate (Σ_k coeffs[k]·x^k) · exp(−x) without overflow. The implementation lives in gam-math so terms/basis code can consume it without reaching up into inference. Evaluate (Σ_k coeffs[k]·x^k) · exp(−x) without overflow. For moderate x ≤ 600 uses Horner + exp(−x) directly; for very large x rewrites xᵈ · exp(−x) = exp(d·ln x − x) and runs Horner in 1/x, which keeps both the polynomial sum and its multiplier inside double range. Returns 0.0 for non-finite x or empty coeffs.
standard_normal_quantile
Standard normal quantile Φ⁻¹(p) using Acklam’s rational approximation. Implementation lives in gam-math; re-exported to keep crate::probability::standard_normal_quantile resolving for all existing callers. Standard normal quantile Φ⁻¹(p) using Acklam’s rational approximation.
try_inverse_link_array
Inverse-link transform per likelihood specification (response scale).
tweedie_moment_matched_interval
Equal-tailed predictive interval for a Tweedie compound Poisson–Gamma response (power 1 < p < 2) whose conditional law has mean mu > 0 and dispersion phi > 0, widened for estimation uncertainty to a total predictive variance total_var (estimation + observation noise). Returns the pair of Tweedie quantiles at lower-tail probabilities p_lo < p_hi — the skew-correct, zero-atom-aware replacement for a symmetric mu ± z·σ band, which on a right-skewed Tweedie sits below the true upper quantile and under-covers the upper tail (the #817 defect, Tweedie sibling of #1193).
tweedie_quantile
Quantile (inverse CDF) of a Tweedie compound Poisson–Gamma response (power 1 < p < 2) with mean mu > 0 and dispersion phi > 0 at probability q ∈ (0, 1): the value y ≥ 0 with P(Y ≤ y) = q.