SPB Git

Repository Public MIT

gen_special.py 6.1 KB 128 lines python

1#!/usr/bin/env python32# ORIX Calc3# File: Packages/ORIXStatistics/Reference/gen_special.py4# Description: Generates special-function reference values with mpmath (cross-checked against scipy) -> output/special.json.5#6# Author: Simon-Pierre Boucher7# Contact: contact@spboucher.ai8import math9import mpmath as mp10import scipy.special as sc11from reflib import *1213cases = []141516disagreements = []171819def add(fn, args, expected, check=None, rtol=1e-7, soft=False):20    if not representable(expected):21        return22    e = fl(expected)23    if check is not None and math.isfinite(check) and abs(e) > 1e-290:24        if abs(check - e) > rtol * abs(e) + 1e-300:25            # mpmath is the reference; scipy is only a sanity check and is known to be inexact in extreme tails.26            assert soft, (fn, args, e, check)27            disagreements.append({"fn": fn, "args": [float(a) for a in args], "mpmath": e, "scipy": float(check)})28    cases.append({"fn": fn, "args": [float(a) for a in args], "expected": e})293031for x in [1e-8, 0.001, 0.1, 0.5, 1, 1.5, 2, 3.7, 10, 25.5, 100, 171.5, 1000.25, 1e6, 1e10, -0.5, -1.5, -2.25, -10.1, -100.5]:32    add("logGamma", [x], mp.log(abs(mp.gamma(x))) if abs(x) < 1e3 else mp.loggamma(x), sc.gammaln(x), 1e-11)33for x in [1e-8, 0.001, 0.1, 0.5, 1, 1.5, 2, 3.7, 10, 25.5, 100, 171.5, -0.5, -1.5, -2.25, -10.1, -100.5]:34    add("gamma", [x], mp.gamma(x), sc.gamma(x), 1e-11)35bgrid = [1e-6, 0.01, 0.5, 1, 2.5, 10, 123.4, 1e4, 1e7]36for a in bgrid:37    for b in bgrid:38        add("logBeta", [a, b], log_beta(a, b), sc.betaln(a, b), 1e-8)39for a in [0.01, 0.5, 1, 2.5, 10, 50]:40    for b in [0.01, 0.5, 1, 2.5, 10, 50]:41        add("beta", [a, b], mp.beta(a, b), sc.beta(a, b), 1e-10)4243# Incomplete gamma44for a in [1e-5, 0.001, 0.01, 0.1, 0.5, 0.99, 1, 1.5, 2.5, 5, 10, 30, 100, 1000, 1e4, 1e5, 1e6]:45    xs = [a * f for f in [1e-3, 0.01, 0.1, 0.5, 0.9, 0.99, 1, 1.01, 1.1, 1.5, 2, 5, 10]]46    xs += [1e-10, 1e-3, 0.3, 0.9, 1.0, 1.1, 3.0, 20.0, 100.0] if a < 5 else []47    xs += [a + k * math.sqrt(a) for k in [-8, -5, -3, -1.5, -0.5, 0.5, 1.5, 3, 5, 8, 12, 20, 30] if a + k * math.sqrt(a) > 0]48    for x in xs:49        if a >= 1e5 and (x < 0.5 * a or x > 2 * a):50            continue51        p, q = gamma_pq(a, x)52        add("gammaP", [a, x], p, sc.gammainc(a, x), 1e-4)53        add("gammaQ", [a, x], q, sc.gammaincc(a, x), 1e-4)5455# Incomplete beta56ab = [0.01, 0.1, 0.5, 1, 2, 5, 20, 100, 1000, 1e5]57for a in ab:58    for b in ab:59        m = a / (a + b); sd = math.sqrt(a * b / ((a + b) ** 2 * (a + b + 1)))60        xs = [1e-6, 0.001, 0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 0.999]61        xs += [m + k * sd for k in [-8, -4, -2, -1, 0, 1, 2, 4, 8, 15, -15] if 0 < m + k * sd < 1]62        for x in xs:63            if max(a, b) >= 1e5 and abs(x - m) > 40 * sd:64                continue65            try:66                p, q = beta_pq(a, b, x)67            except RuntimeError as ex:68                print("skip betaI", a, b, x, ex); continue69            add("betaI", [x, a, b], p, sc.betainc(a, b, x), 1e-4)70            add("betaIc", [x, a, b], q, sc.betaincc(a, b, x), 1e-4)7172# Inverse error functions and the normal quantile73for y in [1e-300, 1e-20, 1e-8, 0.001, 0.1, 0.3, 0.49, 0.5, 0.7, 0.9, 0.99, 0.999999, 1 - 1e-12, 1 - 1e-16]:74    for s in (1, -1):75        v = solve(lambda t: mp.erf(t) - mp.mpf(s * y), sc.erfinv(s * y)) if y > 1e-200 else mp.sqrt(mp.pi) / 2 * mp.mpf(s * y)76        add("erfinv", [s * y], v, sc.erfinv(s * y), 1e-9)77for q in [1e-300, 1e-100, 1e-20, 1e-8, 0.001, 0.1, 0.5, 0.9, 1.0, 1.3, 1.5, 1.9, 1.999, 2 - 1e-10]:78    if q == 1.0:79        cases.append({"fn": "erfcinv", "args": [1.0], "expected": 0.0}); continue80    v = solve(lambda t: mp.log(mp.erfc(t) / mp.mpf(q)), sc.erfcinv(q))81    add("erfcinv", [q], v, sc.erfcinv(q), 1e-9)82ps = [1e-300, 1e-200, 1e-100, 1e-50, 1e-20, 1e-12, 1e-8, 1e-5, 0.001, 0.01, 0.02425, 0.024, 0.025, 0.05, 0.1, 0.25, 0.4, 0.49, 0.499999,83      0.500001, 0.6, 0.75, 0.9, 0.95, 0.975, 0.99, 0.999, 1 - 1e-6, 1 - 1e-10, 1 - 1e-15]84for p in ps:85    v = solve(lambda t: mp.log(ncdf(t) / mp.mpf(p)), sc.ndtri(p))86    add("normQ", [p], v, sc.ndtri(p), 1e-9)87for q in [1e-300, 1e-100, 1e-20, 1e-10, 1e-5, 0.001, 0.025, 0.05, 0.3]:88    v = solve(lambda t: mp.log(nsf(t) / mp.mpf(q)), -sc.ndtri(q))89    add("normQupper", [q], v, -sc.ndtri(q), 1e-9)90for z in [-38, -30, -10, -5, -2, -1, -0.5, -1e-5, 0, 0.3, 1, 1.96, 3, 6, 8.2, 10, 20, 37]:91    add("normCDF", [z], ncdf(z), sc.ndtr(z), 1e-10)92    add("normSF", [z], nsf(z), sc.ndtr(-z), 1e-10)9394# Inverse incomplete gamma: exact inverse of the double p (or q)95probs = [1e-300, 1e-100, 1e-30, 1e-10, 1e-5, 0.001, 0.01, 0.1, 0.3, 0.5, 0.7, 0.9, 0.99, 0.999]96for a in [0.001, 0.05, 0.5, 1, 1.5, 3, 10, 100, 1000, 1e5]:97    for p in probs:98        for fn, scf in (("gammaPinv", sc.gammaincinv), ("gammaQinv", sc.gammainccinv)):99            x0 = float(scf(a, p))100            if not (1e-290 < x0 < 1e290):101                continue102            idx = 0 if fn == "gammaPinv" else 1103            try:104                v = solve(lambda t: mp.log(gamma_pq(a, t)[idx] / mp.mpf(p)), x0)105            except Exception as ex:106                print("skip", fn, a, p, ex); continue107            add(fn, [a, p], v, x0, 1e-4, soft=p < 1e-20)108109# Inverse incomplete beta110for a in [0.05, 0.5, 1, 2, 5, 30, 500]:111    for b in [0.05, 0.5, 1, 2, 5, 30, 500]:112        for p in probs:113            for fn, scf in (("betaIinv", sc.betaincinv), ("betaIcinv", sc.betainccinv)):114                x0 = float(scf(a, b, p))115                if not (1e-290 < x0 < 1 - 1e-9):116                    continue117                idx = 0 if fn == "betaIinv" else 1118                try:119                    v = solve(lambda t: mp.log(beta_pq(a, b, t)[idx] / mp.mpf(p)), x0)120                except Exception as ex:121                    print("skip", fn, a, b, p, ex); continue122                if not (0 < v < 1):123                    continue124                add(fn, [p, a, b], v, x0, 1e-4, soft=p < 1e-20)125126write("special", {"source": "mpmath %s at %d digits, cross-checked against scipy.special" % (mp.__version__, BASE_DPS), "scipy_disagreements": disagreements, "cases": cases})127print("scipy disagreements (extreme tails):", len(disagreements))128
Files main
Menu

Theme