This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/stats/special

The special functions under lib/stats's distributions

Each function takes and returns one number. The methods, and where each is held to mpmath at 50 digits, are in lib/stats/accuracy.py (the grid) and lib/stats/accuracy.json (the worst errors found, beside SciPy's and R's).

  • log1p(x), expm1(x): log(1 + x) and exp(x) - 1 by Kahan's corrections, log1pmx(t): log(1 + t) - t, by the series in u = t / (2 + t) for |t| < 1/2.
  • lgamma(x): log |Gamma(x)|. Taylor series about 1 and 2 with the coefficients zeta(k) - 1 (lib/stats/tables.viv), the recurrence down to [3/2, 5/2) below 10, Stirling's series from 10, reflection below 0.
  • digamma(x): psi(x). The Taylor series about its positive zero within 1/4 of it, the series about 1 and 2, the recurrence, the asymptotic series from 10, reflection below 0.
  • stirlerr(x): log Gamma(x + 1) - (x + 1/2) log x + x - log sqrt(2 pi), Stirling's series from 10, and below it the recurrence stirlerr(x) = stirlerr(x + 1) + sum_k u^2k / (2k + 1), u = 1 / (2x + 1).
  • bd0(x, m): x log(x / m) + m - x, Loader's deviance term.
  • dpois_raw(k, m): m^k e^-m / Gamma(k + 1) for real k, Loader's saddle-point form exp(-stirlerr(k) - bd0(k, m)) / sqrt(2 pi k).
  • dbinom_raw(k, n, p, q): the binomial term for real k and n, Loader's form (R's dbinom_raw).
  • erfc(x), erfcx(x) = exp(x^2) erfc(x): Cody's rational function on [1/2, 4] (W. J. Cody, Math. Comp. 23 (1969), as in netlib's specfun CALERF), the continued fraction above 4, and exp(-x^2) split so that the square is exact. erf below 1/2 is its Taylor series (stats_erf).
  • ndtr(x), ndtri(p): the standard normal distribution function and its inverse (Acklam's approximation refined by Halley steps on ndtr).
  • erfinv(y), erfcinv(q): inverses by Halley steps on erf or erfc.
  • gammainc(a, x), gammaincc(a, x): the regularized incomplete gamma functions P and Q. The series for P below x = a + 1, the continued fraction for Q above it, and for a < 1, x <= 1.1 both from one alternating series (P = c (1 - a s), Q = -expm1(log c) + c a s, c = x^a / Gamma(1 + a)). The factor x^a e^-x / Gamma(a) is a dpois_raw.
  • gammaincinv(a, p), gammainccinv(a, q): their inverses in x, from Wilson-Hilferty's or the small-x start, by safeguarded Halley steps on the smaller tail.
  • betainc(a, b, x), betaincc(a, b, x): the regularized incomplete beta function and its complement by the continued fraction of DLMF 8.17.22 (modified Lentz, Thompson and Barnett 1986) on the side of the mean where it converges, with the factor x^a (1 - x)^b / B(a, b) in Stirling form.
  • betaincinv(a, b, p), betainccinv(a, b, q): inverses in x, from the starting value of algorithm AS 109 (Cran, Martin and Thomas 1977), by safeguarded Newton steps.
  • lbeta(a, b): log B(a, b).

Used with from stats.special import ....

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 54–1058