Special functions and distribution primitives behind Nous.Eval.Stats.
OTP's :math has erf/1 and erfc/1 but no log-gamma, so everything that
needs a Beta or binomial term is built here from a Lanczos lgamma/1.
These are numeric primitives: each function is total on the domain stated in
its @doc and returns a float. Arguments outside that domain do not match
any clause (FunctionClauseError); the data-facing functions in
Nous.Eval.Stats and its other submodules validate their inputs and return
{:error, reason} before calling in here.
Accuracy (checked against SciPy in test/nous/eval/stats/special_test.exs):
lgamma/1 about 1e-14 relative, betainc/3 and beta_quantile/3 about
1e-12, norm_quantile/1 about 1e-14 (Acklam's approximation plus one
Halley step).
Summary
Functions
Quantile of Beta(a, b): the x with betainc(a, b, x) == p, for
0 <= p <= 1 and a, b > 0. Found by bisection to full double precision.
Regularized incomplete beta function I_x(a, b) for a, b > 0 and
0 <= x <= 1; this is also the CDF of Beta(a, b) at x.
Natural log of the gamma function, ln Γ(x), for x > 0.
ln B(a, b) for a, b > 0.
ln C(n, k) for integers 0 <= k <= n.
Standard normal CDF Φ(x).
Standard normal quantile Φ⁻¹(p) for 0 < p < 1.
Sample quantile of a list at probability 0 <= p <= 1, using linear
interpolation between order statistics (Hyndman–Fan type 7, the default of
R's quantile and NumPy's quantile). The list must be non-empty and
already sorted ascending.
CDF of Student's t distribution with df > 0 degrees of freedom.
Quantile of Student's t distribution with df > 0 degrees of freedom, for
0 < p < 1.
Functions
Quantile of Beta(a, b): the x with betainc(a, b, x) == p, for
0 <= p <= 1 and a, b > 0. Found by bisection to full double precision.
Examples
iex> Nous.Eval.Stats.Special.beta_quantile(0.5, 2, 2) |> Float.round(12)
0.5
Regularized incomplete beta function I_x(a, b) for a, b > 0 and
0 <= x <= 1; this is also the CDF of Beta(a, b) at x.
Evaluated by the continued fraction of Numerical Recipes §6.4 with the
modified Lentz method, using the symmetry I_x(a, b) = 1 - I_{1-x}(b, a)
on the side where the fraction converges fast.
Examples
iex> Nous.Eval.Stats.Special.betainc(2, 3, 0.4) |> Float.round(12)
0.5248
Natural log of the gamma function, ln Γ(x), for x > 0.
Examples
iex> Nous.Eval.Stats.Special.lgamma(1.0) |> abs() < 1.0e-14
true
iex> Nous.Eval.Stats.Special.lgamma(10) |> Float.round(10)
12.8018274801
ln B(a, b) for a, b > 0.
When an argument is 10 or more the large lgamma terms are combined
analytically (Stirling remainder plus log1p, as in R's lbeta), so the
result keeps full relative accuracy instead of losing digits to
cancellation between lgamma(b) and lgamma(a + b).
@spec log_choose(non_neg_integer(), non_neg_integer()) :: float()
ln C(n, k) for integers 0 <= k <= n.
Examples
iex> Nous.Eval.Stats.Special.log_choose(10, 3) |> :math.exp() |> round()
120
Standard normal CDF Φ(x).
Examples
iex> Nous.Eval.Stats.Special.norm_cdf(0)
0.5
Standard normal quantile Φ⁻¹(p) for 0 < p < 1.
Acklam's rational approximation refined by one Halley step on the
erfc-based CDF. The upper half is computed as -Φ⁻¹(1 - p), which is
exact in floating point for p >= 0.5, so both tails keep full relative
accuracy.
Examples
iex> Nous.Eval.Stats.Special.norm_quantile(0.975) |> Float.round(10)
1.9599639845
Sample quantile of a list at probability 0 <= p <= 1, using linear
interpolation between order statistics (Hyndman–Fan type 7, the default of
R's quantile and NumPy's quantile). The list must be non-empty and
already sorted ascending.
CDF of Student's t distribution with df > 0 degrees of freedom.
Examples
iex> Nous.Eval.Stats.Special.t_cdf(0, 5)
0.5
Quantile of Student's t distribution with df > 0 degrees of freedom, for
0 < p < 1.
Examples
iex> Nous.Eval.Stats.Special.t_quantile(0.975, 10) |> Float.round(6)
2.228139