Discrete distributions (Poisson, Binomial)
Two discrete distribution families: Poisson and Binomial. Both provide
a probability mass function (PMF) and a cumulative distribution function
(CDF). The count parameter k is passed as f32 but must be
integer-valued; non-integer k returns 0.
Poisson
Section titled “Poisson”poisson_pmf(k: f32, lambda: f32) -> f32
Computes P(X = k) = exp(k*ln(lambda) - lambda - ln(Gamma(k+1))) in log-space to avoid factorial overflow. Returns 0 for non-integer or negative k. At lambda = 0, returns 1 if k = 0, else 0.
poisson_cdf(k: f32, lambda: f32) -> f32
Computes P(X <= k) via the complement of the regularized lower incomplete gamma function: 1 - gammaP(k+1, lambda). This avoids summing individual PMF values.
- Parameters: k >= 0 (integer-valued f32), lambda > 0
- lambda < 0: returns NaN
- lambda = 0: CDF returns 1.0 for all k >= 0
import Nautilus.Distributions (poisson_pmf, poisson_cdf)
pmf = poisson_pmf(cast(3.0, f32), cast(2.5, f32)) -- approximately 0.2138cdf = poisson_cdf(cast(3.0, f32), cast(2.5, f32)) -- approximately 0.7576Binomial
Section titled “Binomial”binomial_pmf(k: f32, n: f32, p: f32) -> f32
Computes P(X = k) = C(n,k) * p^k * (1-p)^(n-k) in log-space using
log_gamma for the binomial coefficient. Returns 0 for non-integer k
or k outside [0, n].
binomial_cdf(k: f32, n: f32, p: f32) -> f32
Computes P(X <= k) via the regularized incomplete beta function: betaI(n-k, k+1, 1-p). Returns 0 for k < 0, returns 1 for k >= n.
- Parameters: k in [0, n] (integer-valued f32), n positive integer, p in [0, 1]
- p = 0: returns 1 if k = 0, else 0
- p = 1: returns 1 if k = n, else 0
- p outside [0, 1]: returns NaN
import Nautilus.Distributions (binomial_pmf, binomial_cdf)
pmf = binomial_pmf(cast(3.0, f32), cast(10.0, f32), cast(0.3, f32))-- approximately 0.2668cdf = binomial_cdf(cast(3.0, f32), cast(10.0, f32), cast(0.3, f32))-- approximately 0.6496Edge cases
Section titled “Edge cases”| Condition | PMF | CDF |
|---|---|---|
| Non-integer k | 0.0 | uses floor(k) implicitly |
| k < 0 | 0.0 | 0.0 |
| lambda < 0 (Poisson) | NaN | NaN |
| p outside [0,1] (Binomial) | NaN | NaN |
| k > n (Binomial) | 0.0 | 1.0 |