@numero-uno-jpn/rstats 0.15.1 → 0.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +147 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/chunks/{chunk-735NNFAS.js → chunk-3THFTB36.js} +2 -2
- package/dist/chunks/chunk-3TSWYE5L.js +2 -0
- package/dist/chunks/{chunk-7J4HW6VF.js → chunk-3WPU6LGK.js} +2 -2
- package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-5ITR67MB.js} +2 -2
- package/dist/chunks/chunk-5NRFU6F6.js +2 -0
- package/dist/chunks/{chunk-SAZAEPZC.js → chunk-E2LCSVQV.js} +1 -1
- package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
- package/dist/chunks/chunk-FKAKALOZ.js +2 -0
- package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HD73UKPT.js} +2 -2
- package/dist/chunks/{chunk-4ILI2FSB.js → chunk-IUULP3LE.js} +1 -1
- package/dist/chunks/{chunk-LY2T6DA6.js → chunk-JQG3YQOC.js} +2 -2
- package/dist/chunks/chunk-KT66SB7R.js +2 -0
- package/dist/chunks/{chunk-IQOVOHZG.js → chunk-MAUAOQG3.js} +2 -2
- package/dist/chunks/{chunk-GEJCBCMP.js → chunk-MDYE7QP2.js} +2 -2
- package/dist/chunks/{chunk-5SQFDLNG.js → chunk-NM3Z2UBQ.js} +2 -2
- package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-PMJI5QO6.js} +2 -2
- package/dist/chunks/chunk-UF7KOTHF.js +3 -0
- package/dist/chunks/chunk-UOE57JGT.js +2 -0
- package/dist/chunks/{chunk-3QTVXDUM.js → chunk-USPEBXSR.js} +2 -2
- package/dist/chunks/{chunk-H4CHJMFU.js → chunk-VLUL2HFW.js} +1 -1
- package/dist/chunks/{chunk-VOIJKNL2.js → chunk-ZIX7NYQF.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +48 -4
- package/dist/distributions/beta.d.ts +1 -1
- package/dist/distributions/beta.js +75 -2
- package/dist/distributions/cauchy.js +76 -5
- package/dist/distributions/exponential.js +16 -2
- package/dist/distributions/f.js +143 -24
- package/dist/distributions/gamma.d.ts +13 -2
- package/dist/distributions/gamma.js +122 -12
- package/dist/distributions/geometric.js +19 -3
- package/dist/distributions/invgamma.js +60 -10
- package/dist/distributions/kumaraswamy.js +116 -17
- package/dist/distributions/laplace.js +5 -2
- package/dist/distributions/logistic.js +8 -2
- package/dist/distributions/lognormal.js +40 -5
- package/dist/distributions/nbeta.js +597 -52
- package/dist/distributions/nchisq.d.ts +25 -0
- package/dist/distributions/nchisq.js +381 -96
- package/dist/distributions/negbinomial.js +26 -6
- package/dist/distributions/normal.js +29 -1
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +401 -63
- package/dist/distributions/pareto.js +73 -19
- package/dist/distributions/t.js +102 -12
- package/dist/distributions/triangular.js +93 -27
- package/dist/distributions/tukey.js +110 -50
- package/dist/distributions/uniform.js +14 -9
- package/dist/distributions/weibull.js +65 -10
- package/dist/distributions.js +2 -2
- package/dist/internal/ddexp.d.ts +13 -0
- package/dist/internal/ddexp.js +55 -3
- package/dist/internal/discrete.js +30 -7
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +59 -5
- package/dist/internal/noncentral.d.ts +41 -0
- package/dist/internal/noncentral.js +124 -2
- package/dist/internal/poissonMixture.d.ts +7 -2
- package/dist/internal/poissonMixture.js +142 -38
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +7 -0
- package/dist/special/dd.d.ts +1 -1
- package/dist/special/dd.js +4 -1
- package/dist/special/gamma.js +4 -0
- package/dist/special/igam.js +126 -21
- package/dist/special/incbet.d.ts +10 -0
- package/dist/special/incbet.js +218 -48
- package/dist/special/incbetLargeA.js +53 -14
- package/dist/special/incbetPrefactor.d.ts +57 -0
- package/dist/special/incbetPrefactor.js +103 -0
- package/dist/special/incbetTemme.js +15 -3
- package/dist/special/loader.js +97 -22
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
- package/dist/chunks/chunk-BDJPSEA7.js +0 -2
- package/dist/chunks/chunk-KO6QQPC6.js +0 -2
- package/dist/chunks/chunk-OLPTFKIZ.js +0 -3
- package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
- package/dist/chunks/chunk-UWBYDIOH.js +0 -2
- package/dist/chunks/chunk-UZHY5DWN.js +0 -2
package/dist/special/incbet.js
CHANGED
|
@@ -9,13 +9,15 @@
|
|
|
9
9
|
// - For each tail, choose the frame in which it can be computed directly (I_x(a,b) or I_{1-x}(b,a)),
|
|
10
10
|
// because forming it as 1 - (the other) loses digits in the smaller tail
|
|
11
11
|
// - The factor x^a (1-x)^b / B(a, b) in front of the series and continued fractions is computed with
|
|
12
|
-
// Loader's dbinomRaw (avoids cancellation in the log for large a, b)
|
|
12
|
+
// Loader's dbinomRaw (avoids cancellation in the log for large a, b), or for a small shape, or a huge one with the
|
|
13
|
+
// other moderate, from the forms of incbetPrefactor.ts
|
|
13
14
|
// - The log is formed directly from the result of each computation
|
|
14
15
|
import { MACHEP } from "./constants.js";
|
|
15
16
|
import { dbinomExponent, dbinomRawDD } from "./loader.js";
|
|
16
17
|
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, isDoubleDouble, SPLITTER, twoProd, twoSum } from "./dd.js";
|
|
17
18
|
import { lgamma1p } from "./gamma.js";
|
|
18
|
-
import {
|
|
19
|
+
import { lgammaDiffDD } from "./lgammaDiff.js";
|
|
20
|
+
import { HUGE_SHAPE, HUGE_SHAPE_MAX_SMALL, lgammaRatioHuge, mulBig, prefactorParts, SMALL_SHAPE, smallShapeG, } from "./incbetPrefactor.js";
|
|
19
21
|
import { betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
|
|
20
22
|
import { betaIncLargeA, LARGE_A_MIN, SMALL_B_MAX } from "./incbetLargeA.js";
|
|
21
23
|
const BIG = 4.503599627370496e15;
|
|
@@ -24,13 +26,20 @@ const BIGINV = 2.22044604925031308085e-16;
|
|
|
24
26
|
* Log of x^a (1-x)^b / B(a, b).
|
|
25
27
|
* Since dbinom(a; a + b, x) = Gamma(a+b+1)/(Gamma(a+1) Gamma(b+1)) x^a (1-x)^b
|
|
26
28
|
* (a and b are passed separately; recovering them from a + b loses digits of the smaller one),
|
|
27
|
-
* x^a (1-x)^b / B(a, b) = dbinom(a; a+b, x) * a b / (a + b).
|
|
29
|
+
* x^a (1-x)^b / B(a, b) = dbinom(a; a+b, x) * a b / (a + b). For a small shape, or a huge one with the other moderate,
|
|
30
|
+
* from prefactorParts (incbetPrefactor.ts) instead, as in prefactor and smallPrefactorTimes
|
|
28
31
|
*/
|
|
29
32
|
function logPrefactor(a, b, x, xc) {
|
|
33
|
+
const parts = prefactorParts(a, b, x, xc);
|
|
34
|
+
if (parts !== undefined)
|
|
35
|
+
return Math.log(parts[0]) + parts[1][0] + parts[1][1];
|
|
30
36
|
return dbinomRawDD(a, b, x, xc, true) + Math.log(a) + Math.log(b) - Math.log(a + b);
|
|
31
37
|
}
|
|
32
38
|
/** Linear value of x^a (1-x)^b / B(a, b) (0 on underflow) */
|
|
33
39
|
function prefactor(a, b, x, xc) {
|
|
40
|
+
const parts = prefactorParts(a, b, x, xc);
|
|
41
|
+
if (parts !== undefined)
|
|
42
|
+
return parts[0] * (Math.exp(parts[1][0]) * (1 + parts[1][1]));
|
|
34
43
|
return dbinomRawDD(a, b, x, xc, false) * ((a * b) / (a + b));
|
|
35
44
|
}
|
|
36
45
|
/** Lower limit of normal numbers. If the prefactor is larger, multiply on the linear scale */
|
|
@@ -43,6 +52,20 @@ const NORMAL_MIN = 2.2250738585072014e-308 * 1e3;
|
|
|
43
52
|
* Returns undefined when the result underflows to 0 (the caller then works on the log scale)
|
|
44
53
|
*/
|
|
45
54
|
function smallPrefactorTimes(a, b, x, xc, m) {
|
|
55
|
+
const parts = prefactorParts(a, b, x, xc);
|
|
56
|
+
if (parts !== undefined) {
|
|
57
|
+
const [ms, e] = parts;
|
|
58
|
+
const c = m * ms;
|
|
59
|
+
if (!(e[0] > -1400) || !Number.isFinite(c) || c === 0)
|
|
60
|
+
return undefined;
|
|
61
|
+
// (exp(e) directly above -700, where the prefactor is below the normal range through c alone, a shape below 1e-305:
|
|
62
|
+
// e + 700 would round e to the ulp of 700, 1e-13, and pbeta(1 - 6e-14, 0.97, 4.7e-307, { lowerTail: false }) was
|
|
63
|
+
// 1 - 5.5e-14, not 1)
|
|
64
|
+
if (e[0] > -700)
|
|
65
|
+
return Math.exp(e[0]) * (1 + e[1]) * c || undefined;
|
|
66
|
+
const v = Math.exp(e[0] + 700) * (1 + e[1]) * c * Math.exp(-700);
|
|
67
|
+
return v > 0 ? v : undefined;
|
|
68
|
+
}
|
|
46
69
|
const { e } = dbinomExponent(a, b, x, xc);
|
|
47
70
|
const c = (m * ((a * b) / (a + b))) / Math.sqrt(2 * Math.PI * a * (b / (a + b)));
|
|
48
71
|
if (!(e[0] < 1400) || !Number.isFinite(c) || c === 0)
|
|
@@ -79,12 +102,16 @@ function pseriesSum(a, b, x) {
|
|
|
79
102
|
* 1 - I = -expm1(L) - E a S, L = a log x + [lgamma(b+a) - lgamma(b)] - lgamma1p(a)
|
|
80
103
|
* For small a, I is close to 1 and forming 1 - I by subtraction loses digits (1e-7 relative at a = 1e-10).
|
|
81
104
|
* (Derived from the definition of the series)
|
|
105
|
+
* a log x and lgamma(b+a) - lgamma(b) (about a log b for a huge b) are added in double-double: for x near 1/b they
|
|
106
|
+
* cancel, and their rounding was 5e-14 of the result at pbeta(1.37e-272, 0.054, 3.9e270, { lowerTail: false }) (the
|
|
107
|
+
* latter from lgammaRatioHuge for a huge b, see there)
|
|
82
108
|
*/
|
|
83
109
|
function pseriesComplement(a, b, xd, log) {
|
|
84
110
|
const x = xd[0];
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
const
|
|
111
|
+
const lg = b >= HUGE_SHAPE && a <= HUGE_SHAPE_MAX_SMALL ? lgammaRatioHuge(a, b) : lgammaDiffDD(a, b);
|
|
112
|
+
const Ld = ddAdd(ddAdd(ddMulNum(ddLog(xd), a), lg), [-lgamma1p(a), 0]);
|
|
113
|
+
const E = Math.exp(Ld[0]) * (1 + Ld[1]);
|
|
114
|
+
const v = -(Math.expm1(Ld[0]) + Ld[1] * Math.exp(Ld[0])) - E * a * pseriesSum(a, b, x);
|
|
88
115
|
return log ? Math.log(v) : v;
|
|
89
116
|
}
|
|
90
117
|
/** Series (when b x is small and x is not close to 1). Returns I_x(a, b) */
|
|
@@ -133,7 +160,10 @@ function pseries(a, b, xd, xcd, log) {
|
|
|
133
160
|
*
|
|
134
161
|
* For the same reason a + b and b - 1 are kept in double-double: with a ≫ b, rounding a + b loses digits of b
|
|
135
162
|
* (5e-11 relative at a = 1e8, b = 0.01, x = 1 - 1e-6; checked against mpmath). From 2^52 on, the steps k += 1 of
|
|
136
|
-
* the other coefficients would be lost as well, so there every coefficient is a double-double (huge)
|
|
163
|
+
* the other coefficients would be lost as well, so there every coefficient is a double-double (huge). Below 2^52 the
|
|
164
|
+
* same holds for a non-integer a just below a power of two: a + 1, a + 2, ... cross into the next binade, where a's
|
|
165
|
+
* last bit no longer fits, and every coefficient after that is rounded the same way. The error grows with a (log P
|
|
166
|
+
* off by 3e-8 at a = 2^40 - 2^-13, 1.3e-4 at a = 2^52 - 0.5); see stepsInexact
|
|
137
167
|
*/
|
|
138
168
|
// ddDiv for the loop of the continued fraction without allocating: the same operations as in dd.ts, with the
|
|
139
169
|
// result left in R (a typed array, so that storing it allocates nothing). Generated with python3 scripts/ddgen.py scalar
|
|
@@ -209,9 +239,22 @@ function sDiv(ah, al, bh, bl) {
|
|
|
209
239
|
R[0] = s66;
|
|
210
240
|
R[1] = e67;
|
|
211
241
|
}
|
|
242
|
+
/** The continued fractions stop after 100000 steps, where the largest coefficient is a + 2 * 100000 + 2 */
|
|
243
|
+
const CF_SPAN = 200002;
|
|
244
|
+
/**
|
|
245
|
+
* Whether the steps a + 1, a + 2, ... of the continued fraction are not all exact in double (a < 2^52). They are all
|
|
246
|
+
* exact when a + CF_SPAN is (the ulp only grows along the way), which for a > CF_SPAN is (a + CF_SPAN) - a === CF_SPAN.
|
|
247
|
+
* Only from 2^20 on: below, the rounding of the steps (relative ε/2) moves the result by about an ulp (against the
|
|
248
|
+
* double-double steps: 2e-16 on log P at a = 2^20, 7e-16 at 2^25), and the slower double-double steps would be taken
|
|
249
|
+
* by most non-integer a (a + CF_SPAN crosses a binade for every a below 2^17)
|
|
250
|
+
*/
|
|
251
|
+
function stepsInexact(a) {
|
|
252
|
+
return a >= 2 ** 20 && a < 2 ** 52 && a + CF_SPAN - a !== CF_SPAN;
|
|
253
|
+
}
|
|
212
254
|
function continuedFractionCore(a, b, xd, xcd, second) {
|
|
213
|
-
if (!isDoubleDouble() || Math.max(a, b) >= 2 ** 52)
|
|
255
|
+
if (!isDoubleDouble() || Math.max(a, b) >= 2 ** 52 || stepsInexact(a)) {
|
|
214
256
|
return continuedFractionCoreOps(a, b, xd, xcd, second);
|
|
257
|
+
}
|
|
215
258
|
// The loop of continuedFractionCoreOps below on local variables, without allocating [hi, lo] arrays: the same
|
|
216
259
|
// operations in the same order, so the result is bit-identical. Generated with python3 scripts/ddgen.py cf
|
|
217
260
|
let zh = xd[0];
|
|
@@ -590,7 +633,26 @@ function continuedFractionCore(a, b, xd, xcd, second) {
|
|
|
590
633
|
function continuedFractionCoreOps(a, b, xd, xcd, second) {
|
|
591
634
|
// #1: coefficients use x; #2: coefficients use z = x/(1-x)
|
|
592
635
|
const z = second ? ddDiv(xd, xcd) : xd;
|
|
593
|
-
const huge = Math.max(a, b) >= 2 ** 52;
|
|
636
|
+
const huge = Math.max(a, b) >= 2 ** 52 || stepsInexact(a);
|
|
637
|
+
// Above 1e150 the products of two of the parameters (h3 h4 = a (a + 1), ...) overflow: divide before multiplying
|
|
638
|
+
const vast = Math.max(a, b) > 1e150;
|
|
639
|
+
// The splitting of an operand, or of a quotient, above about 1.3e300 overflows in ddDiv and ddMul: where one would be
|
|
640
|
+
// that large, the operands are scaled by 2^±64 first (exactly; the other operand is then small, or the result
|
|
641
|
+
// overflows anyway), and a quotient back. pbeta(1.5e-236, 9220.5, 2.6e301) and pbeta(1e-307, 55.1, 6.3e307) were NaN
|
|
642
|
+
const div = (p, q) => {
|
|
643
|
+
if (q[0] > 1e299)
|
|
644
|
+
return ddDiv(scaled(p, 2 ** -64), scaled(q, 2 ** -64));
|
|
645
|
+
if (Math.abs(p[0]) > 1e299 * q[0])
|
|
646
|
+
return scaled(ddDiv(scaled(p, 2 ** -64), q), 2 ** 64);
|
|
647
|
+
return ddDiv(p, q);
|
|
648
|
+
};
|
|
649
|
+
const mul = (p, q) => {
|
|
650
|
+
if (Math.abs(q[0]) > 1e299)
|
|
651
|
+
return ddMul(scaled(p, 2 ** 64), scaled(q, 2 ** -64));
|
|
652
|
+
if (Math.abs(p[0]) > 1e299)
|
|
653
|
+
return ddMul(scaled(p, 2 ** -64), scaled(q, 2 ** 64));
|
|
654
|
+
return ddMul(p, q);
|
|
655
|
+
};
|
|
594
656
|
// Partial numerators k1 x k2 / (k3 k4) and k5 x k6 / (k7 k8). k1, k3, k4, k7, k8 are doubles, with
|
|
595
657
|
// double-double copies (h1, ...) that are used and advanced only when huge
|
|
596
658
|
let k1 = a;
|
|
@@ -613,16 +675,20 @@ function continuedFractionCoreOps(a, b, xd, xcd, second) {
|
|
|
613
675
|
let qkm1 = [1, 0];
|
|
614
676
|
let ans = [1, 0];
|
|
615
677
|
for (let n = 0; n < 100000; n++) {
|
|
616
|
-
let xk =
|
|
617
|
-
?
|
|
618
|
-
:
|
|
678
|
+
let xk = vast
|
|
679
|
+
? mul(mul(z, div(h1, h3)), div(k2, h4))
|
|
680
|
+
: huge
|
|
681
|
+
? ddDiv(ddMul(ddMul(z, h1), k2), ddMul(h3, h4))
|
|
682
|
+
: ddDiv(ddMul(ddMulNum(z, k1), k2), twoProd(k3, k4));
|
|
619
683
|
let pk = ddSub(pkm1, ddMul(pkm2, xk));
|
|
620
684
|
let qk = ddSub(qkm1, ddMul(qkm2, xk));
|
|
621
685
|
pkm2 = pkm1;
|
|
622
686
|
pkm1 = pk;
|
|
623
687
|
qkm2 = qkm1;
|
|
624
688
|
qkm1 = qk;
|
|
625
|
-
xk =
|
|
689
|
+
xk = vast
|
|
690
|
+
? mul(mul(z, [k5, 0]), div(div(k6, h7), h8))
|
|
691
|
+
: ddDiv(ddMul(ddMulNum(z, k5), k6), huge ? ddMul(h7, h8) : twoProd(k7, k8));
|
|
626
692
|
pk = ddAdd(pkm1, ddMul(pkm2, xk));
|
|
627
693
|
qk = ddAdd(qkm1, ddMul(qkm2, xk));
|
|
628
694
|
pkm2 = pkm1;
|
|
@@ -669,13 +735,24 @@ function continuedFractionCoreOps(a, b, xd, xcd, second) {
|
|
|
669
735
|
}
|
|
670
736
|
return ans[0] + ans[1];
|
|
671
737
|
}
|
|
738
|
+
/** p s for a power of two s */
|
|
739
|
+
function scaled(p, s) {
|
|
740
|
+
return [p[0] * s, p[1] * s];
|
|
741
|
+
}
|
|
672
742
|
/** Whether the series can be used in frame (a, b, x) */
|
|
673
743
|
function pseriesOk(a, b, x) {
|
|
674
744
|
return b * x <= 1 && x <= 0.95;
|
|
675
745
|
}
|
|
676
|
-
/**
|
|
677
|
-
|
|
678
|
-
|
|
746
|
+
/**
|
|
747
|
+
* Whether to compute I_x(a, b) by continued fraction in frame (a, b, x) (below the mean, as in xsf). Above x = 1/2 the
|
|
748
|
+
* test is 1 - x >= b/(a + b), on the smaller sides: x and the mean can both round to 1 (pnbinom(2.6e200, 3.1e287,
|
|
749
|
+
* { mu: 195.6 }) has 1 - x = 6.3e-286 against b/(a + b) = 8.5e-88, and the continued fraction above the mean was NaN)
|
|
750
|
+
*/
|
|
751
|
+
function cfOk(a, b, x, xc) {
|
|
752
|
+
if (x[0] <= 0.5)
|
|
753
|
+
return x[0] <= a / (a + b);
|
|
754
|
+
// (b/(a + b) as 1/(1 + a/b) where a + b overflows)
|
|
755
|
+
return xc[0] >= (a + b < Infinity ? b / (a + b) : 1 / (1 + a / b));
|
|
679
756
|
}
|
|
680
757
|
/** Compute I_x(a, b) by continued fraction */
|
|
681
758
|
function continuedFraction(a, b, xd, xcd, log) {
|
|
@@ -744,33 +821,91 @@ export function betaIncBoth(a, b, x, xc, lower) {
|
|
|
744
821
|
const xcd = x <= xc ? twoSum(1, -x) : [xc, 0];
|
|
745
822
|
return betaIncCore(a, b, xd, xcd, lower, true);
|
|
746
823
|
}
|
|
824
|
+
const DBL_MIN = 2.2250738585072014e-308;
|
|
825
|
+
/**
|
|
826
|
+
* I_x(a, b) (lower) or 1 - I_x(a, b) for x below DBL_MIN, from x and lx = log x in double-double (x itself may have
|
|
827
|
+
* underflowed to 0, as in the F distribution far out: only lx then carries it), as [log, linear] (log NaN unless
|
|
828
|
+
* wantLog). From the series I_x(a, b) = x^a / (a B(a, b)) (1 + a S), S = pseriesSum(a, b, x): the terms of S are about
|
|
829
|
+
* (-b x)^n / n!, and b x is at most 4 here (b near the largest double), mostly negligible.
|
|
830
|
+
* log(1/(a B(a, b))) is lgamma(a+b) - lgamma(b) - lgamma(a+1) without subtraction, or for a small shape
|
|
831
|
+
* log(b/(a + b)) + log Γ(1+a+b)/(Γ(1+a) Γ(1+b)) with the ratio r = b/(a + b) kept on the linear scale (as a log, its
|
|
832
|
+
* rounding of ε |log r| would become the relative error of the result)
|
|
833
|
+
*/
|
|
834
|
+
export function betaIncTinyX(a, b, x, lx, lower, wantLog) {
|
|
835
|
+
const c = Math.log1p(a * pseriesSum(a, b, x));
|
|
836
|
+
let r = 1;
|
|
837
|
+
let lr = 0;
|
|
838
|
+
let e;
|
|
839
|
+
// a log x by mulBig (the splitting of a above 1e300 in ddMulNum overflowed: pbeta(1.5e-323, 1.4e304, 0.5) was NaN).
|
|
840
|
+
// Where it overflows to -Infinity, the terms of the order of a log a below are Infinity too, and the sum was NaN
|
|
841
|
+
// (pbeta(4.2e-310, 4.1e307, 1113))
|
|
842
|
+
const ax = mulBig(lx, a);
|
|
843
|
+
if (ax[0] === -Infinity)
|
|
844
|
+
return lower ? [-Infinity, 0] : [0, 1];
|
|
845
|
+
if (Math.min(a, b) < SMALL_SHAPE) {
|
|
846
|
+
const q = Math.min(a, b) / Math.max(a, b);
|
|
847
|
+
[r, lr] = a <= b ? [1 / (1 + q), -Math.log1p(q)] : [q / (1 + q), Math.log(q) - Math.log1p(q)];
|
|
848
|
+
e = ddAdd(ddAdd(ax, smallShapeG(a, b)), [c, 0]);
|
|
849
|
+
}
|
|
850
|
+
else {
|
|
851
|
+
const lg = b >= HUGE_SHAPE && a <= HUGE_SHAPE_MAX_SMALL ? lgammaRatioHuge(a, b) : lgammaDiffDD(a, b);
|
|
852
|
+
e = ddAdd(ddAdd(ax, lg), [c - lgamma1p(a), 0]);
|
|
853
|
+
}
|
|
854
|
+
const lt = lr + e[0] + e[1];
|
|
855
|
+
// (0 below the subnormal range, where the low part of e can be far beyond 1 and made it -0)
|
|
856
|
+
const v = e[0] < -746 ? 0 : r * (Math.exp(e[0]) * (1 + e[1]));
|
|
857
|
+
if (lower)
|
|
858
|
+
return [lt, v];
|
|
859
|
+
// The other tail: -expm1(lt), and its log from v where v is small (lt rounded to a double is off by up to ε |lt|,
|
|
860
|
+
// which would become the relative error of the result for |lt| > 700) and from -expm1(lt) where v is close to 1
|
|
861
|
+
// (log1p(-v) would lose the digits of 1 - v: 1 - I is -Infinity for x = 5e-324 at a = 5.7e-37)
|
|
862
|
+
return [wantLog ? (lt < -Math.LN2 ? Math.log1p(-v) : Math.log(-Math.expm1(lt))) : NaN, -Math.expm1(lt)];
|
|
863
|
+
}
|
|
747
864
|
/**
|
|
748
865
|
* Core of betaIncDD. Returns [log, linear]. The linear value is always computed; the log only when wantLog
|
|
749
866
|
* (otherwise the log slot is NaN). The log is log(v) of the linear value v; if v exceeds 1/2 the other side is
|
|
750
|
-
* computed directly and log1p is used, and if v is close to subnormal it is recomputed on the spot
|
|
867
|
+
* computed directly and log1p is used, and if v is close to subnormal it is recomputed on the spot.
|
|
868
|
+
* A value within rounding of 1 can come out an ulp above it (from the product of a prefactor and a series near 1:
|
|
869
|
+
* pbeta(0.615, 2, 2.7e-158, { lowerTail: false }) was 1.0000000000000002), so it is capped at 1 (log at 0)
|
|
751
870
|
*/
|
|
752
871
|
function betaIncCore(a, b, xd, xcd, lower, wantLog) {
|
|
872
|
+
const r = betaIncUncapped(a, b, xd, xcd, lower, wantLog);
|
|
873
|
+
return r[1] > 1 || r[0] > 0 ? [Math.min(r[0], 0), Math.min(r[1], 1)] : r;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Below this shape s, the tail of the order of s (1 - I_x(s, b), or I_x(a, s)) is taken as (s / SUB_SHAPE) times its
|
|
877
|
+
* value at SUB_SHAPE, where the other shape is above SUB_OTHER: 1 - I_x(s, b) = s g(x, b) (1 + O(s / min(1, b)) times
|
|
878
|
+
* logs of x and b), so the relative error of the scaling is below 1e-19. The tail is subnormal for s below 1e-308 or
|
|
879
|
+
* so, and its log, taken of the subnormal value, had lost digits (dbeta(0.97, 0.13, 8.15e-322) did not match the
|
|
880
|
+
* differences of pbeta(x, 0.13, 8.15e-322, { logP: true }))
|
|
881
|
+
*/
|
|
882
|
+
const SUB_SHAPE = 1e-300;
|
|
883
|
+
const SUB_OTHER = 1e-280;
|
|
884
|
+
/** [log, linear] of the tail t at SUB_SHAPE scaled to the shape s (small), or of the other tail (!small) */
|
|
885
|
+
function subShapeTail(t, s, small) {
|
|
886
|
+
const r = s / SUB_SHAPE;
|
|
887
|
+
const v = t[1] * r;
|
|
888
|
+
if (small)
|
|
889
|
+
return [t[0] + Math.log(r), v];
|
|
890
|
+
return [Math.log1p(-v), 1 - v];
|
|
891
|
+
}
|
|
892
|
+
function betaIncUncapped(a, b, xd, xcd, lower, wantLog) {
|
|
753
893
|
if (xd[0] <= 0)
|
|
754
894
|
return lower ? [-Infinity, 0] : [0, 1];
|
|
755
895
|
if (xcd[0] <= 0)
|
|
756
896
|
return lower ? [0, 1] : [-Infinity, 0];
|
|
757
|
-
//
|
|
758
|
-
//
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
return [lt, v];
|
|
770
|
-
// The log of the other tail from v, not from exp(lt): lt rounded to a double is off by up to ε |lt| (|lt| > 700),
|
|
771
|
-
// which would become the relative error of the result
|
|
772
|
-
return [wantLog ? Math.log1p(-v) : NaN, -Math.expm1(lt)];
|
|
773
|
-
}
|
|
897
|
+
// The tail of the order of a shape below SUB_SHAPE (1 - I_x(a, b) for a tiny a, I_x(a, b) for a tiny b), from
|
|
898
|
+
// that at SUB_SHAPE scaled (see there), and the other tail as 1 minus it
|
|
899
|
+
if (a < SUB_SHAPE && b > SUB_OTHER)
|
|
900
|
+
return subShapeTail(betaIncUncapped(SUB_SHAPE, b, xd, xcd, false, wantLog), a, !lower);
|
|
901
|
+
if (b < SUB_SHAPE && a > SUB_OTHER)
|
|
902
|
+
return subShapeTail(betaIncUncapped(a, SUB_SHAPE, xd, xcd, true, wantLog), b, lower);
|
|
903
|
+
// x (or 1 - x) is subnormal: the series with its prefactor in logs (betaIncTinyX). The binomial-probability helpers
|
|
904
|
+
// break down on products of subnormals
|
|
905
|
+
if (xd[0] < DBL_MIN)
|
|
906
|
+
return betaIncTinyX(a, b, xd[0], ddLog(xd), lower, wantLog);
|
|
907
|
+
if (xcd[0] < DBL_MIN)
|
|
908
|
+
return betaIncTinyX(b, a, xcd[0], ddLog(xcd), !lower, wantLog);
|
|
774
909
|
// Both parameters large and x near the mean: the uniform asymptotic expansion, whose cost does not grow with
|
|
775
910
|
// the parameters (the continued fraction needs about sqrt(a + b) steps there)
|
|
776
911
|
if (a >= TEMME_MIN_M && b >= TEMME_MIN_M) {
|
|
@@ -784,7 +919,12 @@ function betaIncCore(a, b, xd, xcd, lower, wantLog) {
|
|
|
784
919
|
// large a (incbetLargeA.ts) instead, in the frame of the large parameter. The length of the continued fraction
|
|
785
920
|
// stops growing with the large parameter, but in the bulk it takes 30 to 170 double-double steps; the expansion
|
|
786
921
|
// takes a few terms
|
|
787
|
-
|
|
922
|
+
// Also for a larger small parameter (up to 1e4, as the expansion takes about b/2 steps) where x rounds to 1 in the
|
|
923
|
+
// frame of the large one (then l = -log x is below ε and the terms after the leading Q(b, u) or P(b, u) vanish):
|
|
924
|
+
// the continued fraction is NaN there (pnbinom(1.9e153, 44.4, { prob: 1.6e-149, lowerTail: false }))
|
|
925
|
+
const largeA = Math.max(a, b) >= LARGE_A_MIN &&
|
|
926
|
+
(Math.min(a, b) <= SMALL_B_MAX ||
|
|
927
|
+
((a >= b ? xd[0] : xcd[0]) === 1 && Math.min(a, b) <= 1e4 && Math.min(a, b) < 1e-8 * Math.max(a, b)));
|
|
788
928
|
const viaLargeA = (low, log) => {
|
|
789
929
|
if (!largeA)
|
|
790
930
|
return undefined;
|
|
@@ -808,12 +948,16 @@ function betaIncCore(a, b, xd, xcd, lower, wantLog) {
|
|
|
808
948
|
if (pseriesOk(pa, pb, px[0])) {
|
|
809
949
|
const v = pseriesComplement(pa, pb, px, false);
|
|
810
950
|
// Cancellation check as for the requested tail (see below)
|
|
811
|
-
if (pa * Math.abs(Math.log(px[0])) > 32 * v
|
|
812
|
-
|
|
951
|
+
if (pa * Math.abs(Math.log(px[0])) > 32 * v) {
|
|
952
|
+
const e = viaLargeA(!lower, false)?.[1];
|
|
953
|
+
if (e !== undefined)
|
|
954
|
+
return e;
|
|
955
|
+
if (ox[0] < 1 && cfOk(oa, ob, ox, oxc))
|
|
956
|
+
return continuedFraction(oa, ob, ox, oxc, false);
|
|
813
957
|
}
|
|
814
958
|
return v;
|
|
815
959
|
}
|
|
816
|
-
if (cfOk(oa, ob, ox
|
|
960
|
+
if (cfOk(oa, ob, ox, oxc))
|
|
817
961
|
return viaLargeA(!lower, false)?.[1] ?? continuedFraction(oa, ob, ox, oxc, false);
|
|
818
962
|
return undefined;
|
|
819
963
|
};
|
|
@@ -823,30 +967,56 @@ function betaIncCore(a, b, xd, xcd, lower, wantLog) {
|
|
|
823
967
|
const v = pseries(pa, pb, px, pxc, false);
|
|
824
968
|
if (wantLog && v < TINY)
|
|
825
969
|
return [pseries(pa, pb, px, pxc, true), v];
|
|
970
|
+
// Above 1/2 with the series applying in the other frame too: 1 minus the other tail, which is accurate to about an
|
|
971
|
+
// ulp, while this series near x = 0.95 sums hundreds of terms whose products round (pbeta(0.9398, 0.0209, 0.723)
|
|
972
|
+
// and its upper tail added up to 1 - 2e-15)
|
|
973
|
+
if (v > 0.5 && pseriesOk(oa, ob, ox[0])) {
|
|
974
|
+
const o = pseries(oa, ob, ox, oxc, false);
|
|
975
|
+
return [wantLog ? Math.log1p(-o) : NaN, 1 - o];
|
|
976
|
+
}
|
|
977
|
+
// Likewise from the complement of the series where it passes the cancellation check (see below), as the log already
|
|
978
|
+
// is: for a tiny shape the series value is 1 - O(shape) and its rounding of a few ulps was all of its distance from 1
|
|
979
|
+
// (pbeta(1 - 6e-14, 0.97, 4.7e-307, { lowerTail: false }) was 1 - 1.1e-16 beside a lower tail of 1.4e-305)
|
|
980
|
+
if (v > 0.5) {
|
|
981
|
+
const o = pseriesComplement(pa, pb, px, false);
|
|
982
|
+
if (pa * Math.abs(Math.log(px[0])) <= 32 * o)
|
|
983
|
+
return [wantLog ? Math.log1p(-o) : NaN, 1 - o];
|
|
984
|
+
}
|
|
826
985
|
return finish(v, otherDirect);
|
|
827
986
|
}
|
|
828
987
|
if (pseriesOk(oa, ob, ox[0])) {
|
|
988
|
+
// Where the other tail is below 1/2, 1 minus it is accurate to about an ulp, while the exponent of
|
|
989
|
+
// pseriesComplement rounds at ε (|a log x| + |lgamma(b + a) - lgamma(b)| + |lgamma(a + 1)|), terms that cancel
|
|
990
|
+
// (pbinom(20, 21, 0.9265) was 0.7985148925910792, 2e-15 above 1 - prob^21)
|
|
991
|
+
const o = pseries(oa, ob, ox, oxc, false);
|
|
992
|
+
if (o < 0.5)
|
|
993
|
+
return [wantLog ? Math.log1p(-o) : NaN, 1 - o];
|
|
829
994
|
const v = pseriesComplement(oa, ob, ox, false);
|
|
830
995
|
// The exponent L of pseriesComplement is a sum of terms of size a |log x| that can cancel, and their rounding
|
|
831
996
|
// (absolute, about ε a |log x|) is then large relative to a small result (1e-14 at a = 0.01, b = 1e4, x = 1e-4).
|
|
832
|
-
// When a |log x| exceeds the result 32 times
|
|
833
|
-
//
|
|
834
|
-
// slower continued fraction)
|
|
835
|
-
|
|
997
|
+
// When a |log x| exceeds the result 32 times, use the expansion for large a where it applies, or the continued
|
|
998
|
+
// fraction where it applies in this tail's frame (a lower threshold would also send cases where the series is
|
|
999
|
+
// accurate, e.g. most of pt near 0, to the slower continued fraction). The expansion does not need the continued
|
|
1000
|
+
// fraction's side of the mean (the upper tail of pbeta(5.6e-161, 0.82, 1.5e160) was off by 8e-14 from the series)
|
|
1001
|
+
// (The continued fraction not when x rounds to 1 in this frame, with 1 - x below ε, e.g. pnbinom with size 6e178:
|
|
1002
|
+
// it is then NaN, and the exponent is so negative that the series value is 1 to double precision)
|
|
1003
|
+
if (oa * Math.abs(Math.log(ox[0])) > 32 * v) {
|
|
836
1004
|
const e = viaLargeA(lower, wantLog);
|
|
837
1005
|
if (e !== undefined)
|
|
838
1006
|
return e;
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
1007
|
+
if (px[0] < 1 && cfOk(pa, pb, px, pxc)) {
|
|
1008
|
+
const w = continuedFraction(pa, pb, px, pxc, false);
|
|
1009
|
+
if (wantLog && w < TINY)
|
|
1010
|
+
return [continuedFraction(pa, pb, px, pxc, true), w];
|
|
1011
|
+
return finish(w, otherDirect);
|
|
1012
|
+
}
|
|
843
1013
|
}
|
|
844
|
-
return finish(v, () =>
|
|
1014
|
+
return finish(v, () => o);
|
|
845
1015
|
}
|
|
846
1016
|
const e = viaLargeA(lower, wantLog);
|
|
847
1017
|
if (e !== undefined)
|
|
848
1018
|
return e;
|
|
849
|
-
if (cfOk(pa, pb, px
|
|
1019
|
+
if (cfOk(pa, pb, px, pxc)) {
|
|
850
1020
|
const v = continuedFraction(pa, pb, px, pxc, false);
|
|
851
1021
|
if (wantLog && v < TINY)
|
|
852
1022
|
return [continuedFraction(pa, pb, px, pxc, true), v];
|
|
@@ -23,16 +23,18 @@
|
|
|
23
23
|
// H side below it); the other tail is 1 - (that one), which is at least 1/2. Boost computes only the J side.
|
|
24
24
|
// - M is formed as [x^a (1-x)^b / B(a, b)] x^((b-1)/2) (l/y)^b, y = 1 - x, with the first factor from Loader's
|
|
25
25
|
// binomial density (dbinomExponent, the deviances in double-double), instead of Γ(a + b) / (Γ(a) T^b) times
|
|
26
|
-
// u^b e^-u / Γ(b), whose parts cancel for large a
|
|
26
|
+
// u^b e^-u / Γ(b), whose parts cancel for large a (for a small b or a huge a, the first factor from
|
|
27
|
+
// incbetPrefactor.ts instead).
|
|
27
28
|
// - l = -log x and u are formed in double-double from x in double-double: the result is about as sensitive to the
|
|
28
29
|
// absolute error of u as to a relative one, and u is of order b.
|
|
29
30
|
// - J_0 comes from a continued fraction evaluated backwards (below), not from the incomplete gamma function of
|
|
30
31
|
// igam.ts, and H_N from the series in double-double: both to about an ulp.
|
|
31
32
|
import { dbinomExponent } from "./loader.js";
|
|
32
|
-
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, twoProd, twoSum } from "./dd.js";
|
|
33
|
+
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddNeg, ddSub, twoProd, twoSum } from "./dd.js";
|
|
33
34
|
import { erfcxNonNegative } from "./erf.js";
|
|
34
35
|
import { igamSeriesSumDD } from "./igam.js";
|
|
35
36
|
import { halfErfc } from "./incbetTemme.js";
|
|
37
|
+
import { prefactorParts } from "./incbetPrefactor.js";
|
|
36
38
|
/**
|
|
37
39
|
* Region where the expansion is used (the caller tries it where it would otherwise use the continued fraction):
|
|
38
40
|
* a >= LARGE_A_MIN, b <= SMALL_B_MAX, y = 1 - x <= Y_MAX, u <= U_MAX, and on the J side u >= U_MIN_J (below,
|
|
@@ -113,15 +115,23 @@ export function betaIncLargeA(a, b, xd, yd, lower, wantLog) {
|
|
|
113
115
|
// The median of Gamma(b, 1) is about b - 1/3 for b >= 1 and below 2^(-1/b) for small b
|
|
114
116
|
const med = b >= 1 ? b - 1 / 3 : Math.pow(0.5, 1 / b);
|
|
115
117
|
const jSide = u0 >= med;
|
|
116
|
-
|
|
118
|
+
// (Where x rounds to 1 nothing else in incbet.ts applies, so U_MAX is not imposed there)
|
|
119
|
+
if ((u0 > U_MAX && xd[0] < 1) || (jSide && u0 < U_MIN_J))
|
|
117
120
|
return undefined;
|
|
118
121
|
// T = a + (b - 1)/2, l = -log x and u = T l in double-double
|
|
119
122
|
const bm1 = twoSum(b, -1);
|
|
120
123
|
const td = ddAdd([a, 0], [bm1[0] / 2, bm1[1] / 2]);
|
|
121
|
-
|
|
124
|
+
// For y below 1e-15, l = y + y^2/2 (+ y^3/3, below 1e-30 relative) from y: the logarithm of x is accurate to about
|
|
125
|
+
// 1e-32 absolute, which is all of l when y is tiny (l came out 0 for y = 6e-79 and the continued fraction for J_0,
|
|
126
|
+
// with 100/u steps, did not end)
|
|
127
|
+
const lx = y < 1e-15 ? ddNeg(ddAdd(yd, [(y * y) / 2, 0])) : ddLog(xd);
|
|
122
128
|
const ld = [-lx[0], -lx[1]];
|
|
123
|
-
|
|
129
|
+
// (td scaled by 2^-64 and ld by 2^64 above 1e299, where the splitting of td overflowed: pbeta(1e-305, 55.1, 6.3e307)
|
|
130
|
+
// was NaN)
|
|
131
|
+
const ud = td[0] > 1e299 ? ddMul([td[0] * 2 ** -64, td[1] * 2 ** -64], [ld[0] * 2 ** 64, ld[1] * 2 ** 64]) : ddMul(td, ld);
|
|
124
132
|
const u = ud[0] + ud[1];
|
|
133
|
+
if (!(u > 0 && u < Infinity))
|
|
134
|
+
return undefined;
|
|
125
135
|
// (the low part of u, which the incomplete gamma functions below take into account)
|
|
126
136
|
const uLow = ud[1] - (u - ud[0]);
|
|
127
137
|
const l = ld[0];
|
|
@@ -129,14 +139,32 @@ export function betaIncLargeA(a, b, xd, yd, lower, wantLog) {
|
|
|
129
139
|
// M = [x^a y^b / B(a, b)] exp(b log(l/y) - (b - 1) l/2), with l - y in double-double (l/y = 1 + O(y)), and
|
|
130
140
|
// x^a y^b / B(a, b) = e^-E sqrt(m/(2 pi)), m = ab/(a + b), from the exponent E of Loader's binomial density.
|
|
131
141
|
// M = e^-E_hi sh (1 + δ): sqrt(m/(2 pi)) = sh + sl from m in double-double, and the small factors in δ, applied
|
|
132
|
-
// last, so that each rounds once
|
|
142
|
+
// last, so that each rounds once. For a small b or a huge a, x^a y^b / B(a, b) = c e^(eHi + eLo) from prefactorParts
|
|
143
|
+
// instead (Loader's exponent loses digits there: 2e-14 at pbeta(0.0066, 1.4e-166, 317.19, { lowerTail: false }) and
|
|
144
|
+
// 8e-14 at a = 1e300). In both cases M = e^eHi fac (1 + δ), with facCorr the relative correction of fac
|
|
133
145
|
const dl = ddSub(ld, yd);
|
|
134
146
|
const em = b * Math.log1p((dl[0] + dl[1]) / y) - ((b - 1) / 2) * l;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
147
|
+
let eHi;
|
|
148
|
+
let eLo;
|
|
149
|
+
let fac;
|
|
150
|
+
let facCorr = 0;
|
|
151
|
+
const parts = prefactorParts(a, b, xd, yd);
|
|
152
|
+
if (parts !== undefined) {
|
|
153
|
+
const [c, es] = parts;
|
|
154
|
+
[eHi, eLo, fac] = [es[0], es[1], c];
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const { e } = dbinomExponent(a, b, xd, yd);
|
|
158
|
+
// (with a and a + b scaled by 2^-64 above 1e299, where the splitting of a in the products would overflow)
|
|
159
|
+
const as = a > 1e299 ? a * 2 ** -64 : a;
|
|
160
|
+
const bs = a > 1e299 ? b * 2 ** -64 : b;
|
|
161
|
+
const md = ddMul(ddDiv(twoProd(as, b), twoSum(as, bs)), INV_2PI);
|
|
162
|
+
const sh = Math.sqrt(md[0]);
|
|
163
|
+
const sq = twoProd(sh, sh);
|
|
164
|
+
[eHi, eLo, fac] = [-e[0], -e[1], sh];
|
|
165
|
+
facCorr = (md[0] - sq[0] - sq[1] + md[1]) / (2 * md[0]);
|
|
166
|
+
}
|
|
167
|
+
const delta = Math.expm1(em + eLo) + facCorr;
|
|
140
168
|
const l2 = (l / 2) * (l / 2);
|
|
141
169
|
const t4 = 4 * T * T;
|
|
142
170
|
let sum;
|
|
@@ -188,11 +216,22 @@ export function betaIncLargeA(a, b, xd, yd, lower, wantLog) {
|
|
|
188
216
|
// rounding of u to a double, which would cost up to ε u times that
|
|
189
217
|
sum *= 1 + uLow * (1 - b / u + 1 / (u * h));
|
|
190
218
|
}
|
|
191
|
-
const mSum = Math.exp(
|
|
219
|
+
const mSum = Math.exp(eHi) * (fac * sum);
|
|
192
220
|
const small = mSum + mSum * delta;
|
|
193
221
|
// (far in the lower tails e^-E underflows; the series of incbet.ts handles them)
|
|
194
|
-
if (!(small >= 1e-290))
|
|
195
|
-
|
|
222
|
+
if (!(small >= 1e-290)) {
|
|
223
|
+
// ... except where x rounds to 1 (y below ε), where neither the series nor the continued fraction of incbet.ts
|
|
224
|
+
// applies (pnbinom(1.6e258, 2.8e-255, { mu: 53.6, lowerTail: false }) was NaN): the log from the factors
|
|
225
|
+
// (δ is not formed: the low part of the exponent, half an ulp of its high part, is no longer small when that is huge)
|
|
226
|
+
if (xd[0] < 1 || !(sum > 0))
|
|
227
|
+
return undefined;
|
|
228
|
+
// (fac sum can underflow where fac is a tiny shape: pbeta(4.2e-24, 2.5e-209, 2e213, { lowerTail: false, logP: true })
|
|
229
|
+
// was -Infinity)
|
|
230
|
+
const lfs = fac * sum > 0 ? Math.log(fac * sum) : Math.log(fac) + Math.log(sum);
|
|
231
|
+
const ls = eHi + (em + eLo) + lfs + Math.log1p(facCorr);
|
|
232
|
+
const v = Math.exp(ls);
|
|
233
|
+
return lower === jSide ? [wantLog ? ls : NaN, v] : [wantLog ? -v : NaN, 1 - v];
|
|
234
|
+
}
|
|
196
235
|
// The J side is I_x(a, b)
|
|
197
236
|
if (lower === jSide)
|
|
198
237
|
return [wantLog ? Math.log(small) : NaN, small];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type DD } from "./dd.js";
|
|
2
|
+
/**
|
|
3
|
+
* Below this smaller shape the prefactor is formed by the small-shape form of prefactorParts. Loader's stirlerr of a
|
|
4
|
+
* shape s < 1 is lgamma1p(s) - (s + 1/2) log s + ..., whose rounding (absolute, about ε |log s| / 2) cancels against the
|
|
5
|
+
* log of the denominator: the prefactor was off by 2e-15 to 1e-14 relative for shapes from 1e-8 to 1e-30 (2e-14 at
|
|
6
|
+
* 1e-130: pbeta(1.08e-285, 1.56e-130, 2) was 1.0000000000000195). Against mpmath, the small-shape form was within 4e-16
|
|
7
|
+
* for smaller shapes from 1e-30 to 0.3 (Loader's 1.5e-15), and from 0.3 to 1 within 1.3e-15 with an rms error of
|
|
8
|
+
* 2.6e-16 (Loader's 1.1e-15 and 3.8e-16); from 1 on Loader's form is the better one where both shapes grow. Up to 1
|
|
9
|
+
* rather than 0.3: tails of a moderate shape from both forms disagreed by 1.6e-15 (pnbinom(0, 0.818, { prob: 0.0821 })
|
|
10
|
+
* and its upper tail), enough to move qnbinom of that upper tail to 1
|
|
11
|
+
*/
|
|
12
|
+
export declare const SMALL_SHAPE = 1;
|
|
13
|
+
/**
|
|
14
|
+
* From this larger shape on (with the smaller one at most HUGE_SHAPE_MAX_SMALL), the huge-shape form of
|
|
15
|
+
* prefactorParts. Loader's form was off by up to 2e-14 relative from 1e15 on and 8e-14 near 1e300 (against mpmath, the
|
|
16
|
+
* smaller shape from 1e-3 to 40 and 1 - x around it divided by the larger: pbeta(5.6e-161, 0.82, 1.5e160) and its
|
|
17
|
+
* upper tail added up to 1 + 3e-14); the huge-shape form stayed within 3.4e-15 from 1e15 on (the rounding of Γ(s)),
|
|
18
|
+
* but neglects terms of order s³/l² (see lgammaRatioHuge), which matter below about 1e10
|
|
19
|
+
*/
|
|
20
|
+
export declare const HUGE_SHAPE: number;
|
|
21
|
+
/** Largest smaller shape for the huge-shape form (Γ(s) is finite and accurate to a few ulps up to there) */
|
|
22
|
+
export declare const HUGE_SHAPE_MAX_SMALL = 100;
|
|
23
|
+
/**
|
|
24
|
+
* x^a (1-x)^b / B(a, b) = c exp(e) as [c, e] (e in double-double) where Loader's form loses digits: for
|
|
25
|
+
* min(a, b) < SMALL_SHAPE, or max(a, b) >= HUGE_SHAPE with min(a, b) <= HUGE_SHAPE_MAX_SMALL; undefined elsewhere.
|
|
26
|
+
* With s = min(a, b), l = max(a, b):
|
|
27
|
+
* - small shape: 1/B(a, b) = ab/(a + b) Γ(1+a+b)/(Γ(1+a) Γ(1+b)), c = ab/(a + b) = s/(1 + s/l) (no underflow of the
|
|
28
|
+
* product ab), and the log of the ratio of gamma functions from smallShapeG, of the size of s log l or less
|
|
29
|
+
* - huge shape: 1/B(a, b) = Γ(l + s)/(Γ(l) Γ(s)), c = 1/Γ(s), and log Γ(l + s) - log Γ(l) from lgammaRatioHuge
|
|
30
|
+
* a log x and b log(1-x) are formed in double-double (b log(1-x) can be large), each logarithm from the smaller of x and
|
|
31
|
+
* 1 - x: x may have rounded to 1 even in double-double (pf(2.5e-97, 1.7e80, 2.7e-95) passes 1 - x = 6.3e-79 with
|
|
32
|
+
* x = [1, 0]), and a log x, about -a (1 - x), then still matters for a huge a
|
|
33
|
+
*/
|
|
34
|
+
export declare function prefactorParts(a: number, b: number, x: DD, xc: DD): [number, DD] | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* log Γ(l + s) - log Γ(l) in double-double for l >= HUGE_SHAPE and s <= HUGE_SHAPE_MAX_SMALL. From Stirling's series,
|
|
37
|
+
* log Γ(l + s) - log Γ(l) = s log l + [(l + s - 1/2) log1p(s/l) - s] + [stirlerr(l + s) - stirlerr(l)]
|
|
38
|
+
* = s log l + s (s - 1) / (2 l) + O(s³/l²)
|
|
39
|
+
* (the stirlerr terms differ by about -s/(12 l²)); the neglected terms are below 1e-25 here. s log l is formed in
|
|
40
|
+
* double-double. lgammaDiff loses s itself where s/l underflows (it forms (l - 1/2) log1p(s/l) - s, which is then -s, not
|
|
41
|
+
* about 0): pbeta(2.2e-292, 2.4e-69, 4.5e290, { lowerTail: false }) was 6.87e-69, 4.44e-69 expected
|
|
42
|
+
*/
|
|
43
|
+
export declare function lgammaRatioHuge(s: number, l: number): DD;
|
|
44
|
+
/**
|
|
45
|
+
* log Γ(1+a+b)/(Γ(1+a) Γ(1+b)) for min(a, b) < SMALL_SHAPE (see prefactorParts), with s = min(a, b), l = max(a, b), as
|
|
46
|
+
* [lgamma(1+l+s) - lgamma(1+l)] - lgamma1p(s), or for l >= 1 with Γ(1+s+l)/Γ(1+l) = (s + l)/l Γ(s+l)/Γ(l). Each term
|
|
47
|
+
* is of the size of s (log l) and accurate relative to that: the result is about s (ψ(1+l) + γ), and where it decides
|
|
48
|
+
* 1 - I_x(a, b) = -expm1(...) it must be (with lgamma1p(a + b) for the first term, a + b dropped s at s = 5.9e-87,
|
|
49
|
+
* b = 0.0845, and pf(1e300, 0.169, 1.18e-86) came out 1.5e-4 too large). For l >= HUGE_SHAPE, lgamma(s+l) - lgamma(l)
|
|
50
|
+
* from lgammaRatioHuge (lgammaDiff loses s where s/l underflows)
|
|
51
|
+
*/
|
|
52
|
+
export declare function smallShapeG(a: number, b: number): DD;
|
|
53
|
+
/**
|
|
54
|
+
* x a in double-double, also for a above 1e290 (where the splitting of a in ddMulNum would overflow); [±Infinity, 0]
|
|
55
|
+
* where the product overflows (the scaling back made it NaN)
|
|
56
|
+
*/
|
|
57
|
+
export declare function mulBig(x: DD, a: number): DD;
|