@numero-uno-jpn/rstats 0.6.1 → 0.8.0
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 +98 -0
- package/README.ja.md +18 -12
- package/README.md +18 -12
- package/THIRD_PARTY_NOTICES.md +30 -0
- package/dist/chunks/{chunk-DRNMIEYH.js → chunk-32PRRONF.js} +1 -1
- package/dist/chunks/chunk-3I6EKGB6.js +2 -0
- package/dist/chunks/chunk-EJRCDJ76.js +2 -0
- package/dist/chunks/{chunk-Y4IZV7XK.js → chunk-I3LAZE6C.js} +2 -2
- package/dist/chunks/chunk-JERGQKZM.js +2 -0
- package/dist/chunks/{chunk-ZWJ42QN6.js → chunk-JVYV4633.js} +1 -1
- package/dist/chunks/chunk-LXMMKO26.js +2 -0
- package/dist/chunks/{chunk-2JCIB5H2.js → chunk-MJQLKEO5.js} +2 -2
- package/dist/chunks/{chunk-UGUQYWRQ.js → chunk-OHPHNE3O.js} +1 -1
- package/dist/chunks/chunk-RZ7SBJEG.js +2 -0
- package/dist/chunks/{chunk-TPC6MTYC.js → chunk-SPQC445Q.js} +2 -2
- package/dist/chunks/{chunk-AJYQNFXK.js → chunk-U633VFH3.js} +2 -2
- package/dist/chunks/{chunk-7NK4SA6B.js → chunk-VIPSPG3Y.js} +2 -2
- package/dist/chunks/chunk-XU4BGFZ6.js +2 -0
- package/dist/chunks/{chunk-NOCEAM44.js → chunk-YGWPU67I.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +21 -5
- package/dist/distributions.js +2 -2
- package/dist/entries/distributions.d.ts +3 -1
- package/dist/entries/distributions.js +3 -1
- package/dist/entries/special.d.ts +1 -0
- package/dist/entries/special.js +1 -0
- package/dist/entries/tests.d.ts +2 -1
- package/dist/entries/tests.js +2 -1
- package/dist/htest/ansariTest.d.ts +18 -1
- package/dist/htest/ansariTest.js +181 -8
- package/dist/htest/brunnerMunzelTest.d.ts +28 -0
- package/dist/htest/brunnerMunzelTest.js +86 -0
- package/dist/htest/chisqTest.d.ts +34 -2
- package/dist/htest/chisqTest.js +140 -37
- package/dist/htest/core/ansari.d.ts +7 -0
- package/dist/htest/core/ansari.js +65 -0
- package/dist/htest/core/simulate.d.ts +2 -0
- package/dist/htest/core/simulate.js +16 -0
- package/dist/htest/fisherTest.d.ts +12 -1
- package/dist/htest/fisherTest.js +53 -7
- package/dist/linalg.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/random/lecuyer.d.ts +20 -0
- package/dist/random/lecuyer.js +71 -0
- package/dist/random/r2dtable.d.ts +26 -0
- package/dist/random/r2dtable.js +153 -0
- package/dist/random/rng.d.ts +78 -11
- package/dist/random/rng.js +128 -26
- package/dist/random/sample.js +24 -7
- package/dist/random/streams.d.ts +34 -0
- package/dist/random/streams.js +75 -0
- package/dist/rstats.js +3 -2
- package/dist/special/igam.d.ts +1 -0
- package/dist/special/igam.js +1 -1
- package/dist/special/incbet.js +487 -6
- package/dist/special/incbetLargeA.d.ts +15 -0
- package/dist/special/incbetLargeA.js +200 -0
- package/dist/special/incbetTemme.d.ts +19 -0
- package/dist/special/incbetTemme.js +216 -0
- package/dist/special/loader.js +151 -118
- package/dist/special/log1p.d.ts +53 -0
- package/dist/special/log1p.js +93 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +3 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-7JVSDGQQ.js +0 -2
- package/dist/chunks/chunk-NAYDS7CL.js +0 -2
- package/dist/chunks/chunk-QNAIK4PY.js +0 -2
- package/dist/chunks/chunk-XM2TVD63.js +0 -2
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// Asymptotic expansion of the incomplete beta function I_x(a, b) for large a and small b.
|
|
2
|
+
//
|
|
3
|
+
// Written from A. R. DiDonato and A. H. Morris, "Algorithm 708: Significant digit computation of the incomplete beta
|
|
4
|
+
// function ratios", ACM TOMS 18 (1992) 360-373, eq. 9 to 9.6 (the published formulas; no code was taken from TOMS 708
|
|
5
|
+
// or R), and N. M. Temme, "Special Functions" (Wiley, 1996), section 11.3.3.1 (DLMF 8.18.3-8.18.7). The organisation
|
|
6
|
+
// of the sum (the coefficients p_n and the upward recursion of J_n) follows Boost.Math's beta_small_b_large_a_series
|
|
7
|
+
// (boost/math/special_functions/beta.hpp, Copyright John Maddock 2006, BSL-1.0; see THIRD_PARTY_NOTICES.md).
|
|
8
|
+
//
|
|
9
|
+
// With T = a + (b - 1)/2, l = -log x and u = T l, the substitution t = e^-v in the integral of I_x(a, b) gives
|
|
10
|
+
// t^(a-1) (1-t)^(b-1) dt = v^(b-1) e^(-T v) f(v) dv, f(v) = (sinh(v/2) / (v/2))^(b-1) = Σ_n p_n (v/2)^(2n),
|
|
11
|
+
// and integrating term by term
|
|
12
|
+
// I_x(a, b) = M Σ_n p_n J_n, J_n = Γ(b + 2n, u) / (u^b e^-u (2T)^(2n)) (v from l to ∞; asymptotic in 1/T²)
|
|
13
|
+
// 1 - I_x(a, b) = M Σ_n p_n H_n, H_n = γ(b + 2n, u) / (u^b e^-u (2T)^(2n)) (v from 0 to l; convergent for l < 2π)
|
|
14
|
+
// with M = l^b x^T / B(a, b). The leading terms are Q(b, u) and P(b, u). The p_n are the coefficients of a power of
|
|
15
|
+
// the series sinh(w)/w = Σ w^(2k)/(2k+1)! (J. C. P. Miller's recurrence, DiDonato and Morris eq. 9.4):
|
|
16
|
+
// p_0 = 1, p_n = (1/n) Σ_(k=1..n) (k b - n) p_(n-k) / (2k+1)!.
|
|
17
|
+
// Both J_n and H_n follow from Γ(s + 2, u) = s (s + 1) Γ(s, u) + u^s e^-u (u + s + 1) (and the same with a minus
|
|
18
|
+
// sign for γ): J_n upwards from J_0 = e^u u^-b Γ(b, u), H_n downwards from H_N = (l/2)^(2N) S(b + 2N, u)/(b + 2N),
|
|
19
|
+
// S the series of the lower incomplete gamma function. Every term of both recursions is positive.
|
|
20
|
+
//
|
|
21
|
+
// Changes from Boost
|
|
22
|
+
// - Only the smaller tail is computed from the expansion (the J side when u is above the median of Gamma(b, 1), the
|
|
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
|
+
// - 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
|
+
// 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.
|
|
27
|
+
// - l = -log x and u are formed in double-double from x in double-double: the result is about as sensitive to the
|
|
28
|
+
// absolute error of u as to a relative one, and u is of order b.
|
|
29
|
+
// - J_0 comes from a continued fraction evaluated backwards (below), not from the incomplete gamma function of
|
|
30
|
+
// igam.ts, and H_N from the series in double-double: both to about an ulp.
|
|
31
|
+
import { dbinomExponent } from "./loader.js";
|
|
32
|
+
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, twoProd, twoSum } from "./dd.js";
|
|
33
|
+
import { erfcxNonNegative } from "./erf.js";
|
|
34
|
+
import { igamSeriesSumDD } from "./igam.js";
|
|
35
|
+
import { halfErfc } from "./incbetTemme.js";
|
|
36
|
+
/**
|
|
37
|
+
* Region where the expansion is used (the caller tries it where it would otherwise use the continued fraction):
|
|
38
|
+
* 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,
|
|
39
|
+
* J_0 would need more than 1000 terms of its continued fraction). Chosen by measurement against mpmath
|
|
40
|
+
* (scripts/mpmath/smallb.py) and against the time of the continued fraction (docs/performance.md)
|
|
41
|
+
*/
|
|
42
|
+
export const LARGE_A_MIN = 15;
|
|
43
|
+
export const SMALL_B_MAX = 40;
|
|
44
|
+
const Y_MAX = 0.3;
|
|
45
|
+
const U_MIN_J = 0.1;
|
|
46
|
+
const U_MAX = 500;
|
|
47
|
+
/** Largest number of terms (the series in 1/T² converges like ((b + 2n)/(2πT))^(2n) in the bulk) */
|
|
48
|
+
const NMAX = 30;
|
|
49
|
+
/** Relative size of the last term kept */
|
|
50
|
+
const EPS = 2 ** -56;
|
|
51
|
+
// 1/(2k+1)! for k = 0..NMAX, and the work array of p_n
|
|
52
|
+
const INV_ODD_FACT = new Float64Array(NMAX + 1);
|
|
53
|
+
{
|
|
54
|
+
let f = 1;
|
|
55
|
+
INV_ODD_FACT[0] = 1;
|
|
56
|
+
for (let k = 1; k <= NMAX; k++) {
|
|
57
|
+
f *= 2 * k * (2 * k + 1);
|
|
58
|
+
INV_ODD_FACT[k] = 1 / f;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const pn = new Float64Array(NMAX + 1);
|
|
62
|
+
const SQRT_PI = 1.7724538509055160273;
|
|
63
|
+
/** 1/(2 pi) in double-double */
|
|
64
|
+
const INV_2PI = [0.15915494309189535, -9.839338337591243e-18];
|
|
65
|
+
/**
|
|
66
|
+
* J_0 = e^u u^-b Γ(b, u) for u above the median of Gamma(b, 1), by the continued fraction (DLMF 8.9.2, even part)
|
|
67
|
+
* J_0 = 1/(u + 1 - b - 1 (1 - b)/(u + 3 - b - 2 (2 - b)/(u + 5 - b - ...)))
|
|
68
|
+
* evaluated backwards with a number of terms fixed in advance: its error after n terms is about exp(-4 sqrt(n u))
|
|
69
|
+
* for small u, so n = 100/u + b/2 + 10. It depends on u only through u - b, which is formed first, with the low part
|
|
70
|
+
* of u (u + 1 - b would round at the size of u before the cancellation). The last two steps and the final division
|
|
71
|
+
* are in double-double, which leaves J_0 about correctly rounded for u >= 0.3 and within 5e-16 for u >= 0.1
|
|
72
|
+
* (against mpmath, b from 0.1 to 45 and u up to 4 times the median + 10). The forward evaluation in igam.ts stops
|
|
73
|
+
* when two approximants agree, which is too early where they approach the value slowly (2e-14 at u = 0.3,
|
|
74
|
+
* 1e-10 at u = 1e-3, 4e-15 at u = 3).
|
|
75
|
+
* For b = 1/2, J_0 = sqrt(pi/u) erfcx(sqrt(u)) in closed form, corrected by uLow d(log J_0)/du with
|
|
76
|
+
* d(log J_0)/du = 1 - b/u - 1/(u J_0)
|
|
77
|
+
*/
|
|
78
|
+
function upperGammaScaled(b, u, uLow) {
|
|
79
|
+
if (b === 0.5) {
|
|
80
|
+
const r = Math.sqrt(u);
|
|
81
|
+
const j = (SQRT_PI / r) * (r >= 1 ? erfcxNonNegative(r) : 2 * Math.exp(u) * halfErfc(r));
|
|
82
|
+
return j * (1 + uLow * (1 - b / u - 1 / (u * j)));
|
|
83
|
+
}
|
|
84
|
+
const ubd = ddAdd(twoSum(u, -b), [uLow, 0]);
|
|
85
|
+
const ub = ubd[0];
|
|
86
|
+
let t = 0;
|
|
87
|
+
for (let k = Math.ceil(100 / u + b / 2) + 10; k > 2; k--)
|
|
88
|
+
t = (-k * (k - b)) / (ub + (2 * k + 1) + t);
|
|
89
|
+
let td = [t, 0];
|
|
90
|
+
for (let k = 2; k >= 1; k--)
|
|
91
|
+
td = ddDiv(ddMulNum(twoSum(k, -b), -k), ddAdd(ddAdd(ubd, [2 * k + 1, 0]), td));
|
|
92
|
+
const r = ddDiv([1, 0], ddAdd(ddAdd(ubd, [1, 0]), td));
|
|
93
|
+
return r[0] + r[1];
|
|
94
|
+
}
|
|
95
|
+
/** p_n from p_0..p_(n-1) (stored in pn) */
|
|
96
|
+
function nextP(b, n) {
|
|
97
|
+
let s = 0;
|
|
98
|
+
for (let k = 1; k <= n; k++)
|
|
99
|
+
s += (k * b - n) * pn[n - k] * INV_ODD_FACT[k];
|
|
100
|
+
return (pn[n] = s / n);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* I_x(a, b) (lower) or 1 - I_x(a, b) by the expansion for large a, as [log, linear] (log NaN unless wantLog).
|
|
104
|
+
* x and y = 1 - x are given in double-double. Returns undefined outside the region above (assumes a >= LARGE_A_MIN
|
|
105
|
+
* and b <= SMALL_B_MAX)
|
|
106
|
+
*/
|
|
107
|
+
export function betaIncLargeA(a, b, xd, yd, lower, wantLog) {
|
|
108
|
+
// Cheap filter on y and u (in double)
|
|
109
|
+
const y = yd[0];
|
|
110
|
+
if (!(y <= Y_MAX))
|
|
111
|
+
return undefined;
|
|
112
|
+
const u0 = -(a + (b - 1) / 2) * Math.log1p(-y);
|
|
113
|
+
// The median of Gamma(b, 1) is about b - 1/3 for b >= 1 and below 2^(-1/b) for small b
|
|
114
|
+
const med = b >= 1 ? b - 1 / 3 : Math.pow(0.5, 1 / b);
|
|
115
|
+
const jSide = u0 >= med;
|
|
116
|
+
if (u0 > U_MAX || (jSide && u0 < U_MIN_J))
|
|
117
|
+
return undefined;
|
|
118
|
+
// T = a + (b - 1)/2, l = -log x and u = T l in double-double
|
|
119
|
+
const bm1 = twoSum(b, -1);
|
|
120
|
+
const td = ddAdd([a, 0], [bm1[0] / 2, bm1[1] / 2]);
|
|
121
|
+
const lx = ddLog(xd);
|
|
122
|
+
const ld = [-lx[0], -lx[1]];
|
|
123
|
+
const ud = ddMul(td, ld);
|
|
124
|
+
const u = ud[0] + ud[1];
|
|
125
|
+
// (the low part of u, which the incomplete gamma functions below take into account)
|
|
126
|
+
const uLow = ud[1] - (u - ud[0]);
|
|
127
|
+
const l = ld[0];
|
|
128
|
+
const T = td[0];
|
|
129
|
+
// 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
|
+
// 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
|
+
// 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
|
|
133
|
+
const dl = ddSub(ld, yd);
|
|
134
|
+
const em = b * Math.log1p((dl[0] + dl[1]) / y) - ((b - 1) / 2) * l;
|
|
135
|
+
const { e } = dbinomExponent(a, b, xd, yd);
|
|
136
|
+
const md = ddMul(ddDiv(twoProd(a, b), twoSum(a, b)), INV_2PI);
|
|
137
|
+
const sh = Math.sqrt(md[0]);
|
|
138
|
+
const sq = twoProd(sh, sh);
|
|
139
|
+
const delta = Math.expm1(em - e[1]) + (md[0] - sq[0] - sq[1] + md[1]) / (2 * md[0]);
|
|
140
|
+
const l2 = (l / 2) * (l / 2);
|
|
141
|
+
const t4 = 4 * T * T;
|
|
142
|
+
let sum;
|
|
143
|
+
pn[0] = 1;
|
|
144
|
+
if (jSide) {
|
|
145
|
+
// J_0 = e^u u^-b Γ(b, u), then J_n = (s (s + 1) J_(n-1) + (l/2)^(2n-2) (u + s + 1)) / (4T²), s = b + 2n - 2
|
|
146
|
+
let j = upperGammaScaled(b, u, uLow);
|
|
147
|
+
sum = j;
|
|
148
|
+
let lp = 1;
|
|
149
|
+
let s = b;
|
|
150
|
+
let prev = Infinity;
|
|
151
|
+
for (let n = 1; n <= NMAX; n++) {
|
|
152
|
+
j = (s * (s + 1) * j + lp * (u + s + 1)) / t4;
|
|
153
|
+
lp *= l2;
|
|
154
|
+
s += 2;
|
|
155
|
+
const term = nextP(b, n) * j;
|
|
156
|
+
const at = Math.abs(term);
|
|
157
|
+
// (an asymptotic series: stop before the terms grow)
|
|
158
|
+
if (at > prev)
|
|
159
|
+
break;
|
|
160
|
+
sum += term;
|
|
161
|
+
if (at <= EPS * sum)
|
|
162
|
+
break;
|
|
163
|
+
prev = at;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
// Number of terms: H_n / H_0 <= (l/2)^(2n), so stop where |p_n| (l/2)^(2n) is negligible (lp = (l/2)^(2N))
|
|
168
|
+
let N = 0;
|
|
169
|
+
let lp = 1;
|
|
170
|
+
for (let n = 1; n <= NMAX; n++) {
|
|
171
|
+
if (Math.abs(nextP(b, n)) * lp * l2 <= EPS)
|
|
172
|
+
break;
|
|
173
|
+
lp *= l2;
|
|
174
|
+
N = n;
|
|
175
|
+
}
|
|
176
|
+
// H_N from the series, then H_(n-1) = (4T² H_n + (l/2)^(2n-2) (u + s + 1)) / (s (s + 1)), s = b + 2n - 2,
|
|
177
|
+
// summing the small terms first
|
|
178
|
+
let s = b + 2 * N;
|
|
179
|
+
let h = (lp * igamSeriesSumDD(s, u)) / s;
|
|
180
|
+
sum = pn[N] * h;
|
|
181
|
+
for (let n = N; n >= 1; n--) {
|
|
182
|
+
lp /= l2;
|
|
183
|
+
s -= 2;
|
|
184
|
+
h = (t4 * h + lp * (u + s + 1)) / (s * (s + 1));
|
|
185
|
+
sum += pn[n - 1] * h;
|
|
186
|
+
}
|
|
187
|
+
// The leading term H_0 has d(log H_0)/du = 1 - b/u + 1/(u H_0) (the rest is smaller by 1/T²); correct for the
|
|
188
|
+
// rounding of u to a double, which would cost up to ε u times that
|
|
189
|
+
sum *= 1 + uLow * (1 - b / u + 1 / (u * h));
|
|
190
|
+
}
|
|
191
|
+
const mSum = Math.exp(-e[0]) * (sh * sum);
|
|
192
|
+
const small = mSum + mSum * delta;
|
|
193
|
+
// (far in the lower tails e^-E underflows; the series of incbet.ts handles them)
|
|
194
|
+
if (!(small >= 1e-290))
|
|
195
|
+
return undefined;
|
|
196
|
+
// The J side is I_x(a, b)
|
|
197
|
+
if (lower === jSide)
|
|
198
|
+
return [wantLog ? Math.log(small) : NaN, small];
|
|
199
|
+
return [wantLog ? Math.log1p(-small) : NaN, 1 - small];
|
|
200
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type DD } from "./dd.js";
|
|
2
|
+
/**
|
|
3
|
+
* Smallest m = ab/(a+b) for which the expansion is used. Measured against mpmath with |τ| <= 1: within 9e-16 from
|
|
4
|
+
* m = 20 on (as accurate as the continued fraction), 7e-15 for m in 15-20 and 5e-14 for m in 10-15 (the series
|
|
5
|
+
* in 1/m no longer converges far enough). It is faster than the continued fraction from m = 10 on (2 µs against 8)
|
|
6
|
+
*/
|
|
7
|
+
export declare const TEMME_MIN_M = 25;
|
|
8
|
+
/**
|
|
9
|
+
* ½ erfc(y) to a few ulp. erfc in erf.ts forms 1 - erf(y) below 1, which loses a factor of up to 6 near y = 1, so
|
|
10
|
+
* for 0.5 <= y < 1 use erfc(y) = erfc(1) + (2/sqrt(pi)) ∫_y^1 e^(-t²) dt (both terms positive), with the integral
|
|
11
|
+
* by 8-point Gauss-Legendre quadrature (3e-16 relative on this interval)
|
|
12
|
+
*/
|
|
13
|
+
export declare function halfErfc(y: number): number;
|
|
14
|
+
/**
|
|
15
|
+
* I_x(a, b) (lower) or 1 - I_x(a, b) by the uniform asymptotic expansion, as [log, linear] (log NaN unless
|
|
16
|
+
* wantLog). x and 1 - x are given in double-double. Returns undefined outside the region where the expansion is
|
|
17
|
+
* used (m >= TEMME_MIN_M and |τ| <= TAU_MAX); the caller then uses the series or continued fractions
|
|
18
|
+
*/
|
|
19
|
+
export declare function betaIncTemme(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Uniform asymptotic expansion of the incomplete beta function I_x(a, b) for large a and b.
|
|
2
|
+
//
|
|
3
|
+
// Written from the method of N. M. Temme, "Incomplete Laplace integrals: uniform asymptotic expansion with
|
|
4
|
+
// application to the incomplete beta function", SIAM J. Math. Anal. 18 (1987) 1638-1663, and Temme, "Special
|
|
5
|
+
// Functions: An Introduction to the Classical Functions of Mathematical Physics" (Wiley, 1996), section 11.3.3.
|
|
6
|
+
// The coefficients are not taken from a table: their Taylor series are generated at run time from the
|
|
7
|
+
// differential equation of the change of variable (below). The derivation is summarised here.
|
|
8
|
+
//
|
|
9
|
+
// With r = a + b, p = a/r, q = b/r and m = a b / r = r p q,
|
|
10
|
+
// I_x(a, b) = sqrt(r/(2 pi)) e^-D ∫_{-∞}^{η} e^{-r ζ²/2} g(ζ) dζ, D = stirlerr(a) + stirlerr(b) - stirlerr(r),
|
|
11
|
+
// where ½ ζ² = p log(p/t) + q log(q/(1-t)) (sign of t - p), g = sqrt(pq) ζ / (t - p), and r η²/2 = E is the
|
|
12
|
+
// deviance bd0(a, r x) + bd0(b, r (1 - x)) of Loader's binomial density. Integrating by parts repeatedly
|
|
13
|
+
// (g_0 = g, h_k = (g_k - g_k(0))/ζ, g_{k+1} = h_k') gives
|
|
14
|
+
// I_x(a, b) = ½ erfc(-η sqrt(r/2)) - e^{-E} e^{-D} / sqrt(2 pi r) Σ_k h_k(η) r^-k.
|
|
15
|
+
// In the scaled variable τ = η / sqrt(pq) (= ± sqrt(2E/m)) the k-th term is of order m^-k, uniformly in p, and
|
|
16
|
+
// the Taylor coefficients of g in τ follow from w = (t - p)/(pq), which satisfies
|
|
17
|
+
// τ dτ/dw = w / ((1 + q w)(1 - p w)), i.e. ½ (w²)' = τ (1 + (q - p) w - pq w²),
|
|
18
|
+
// with g = τ / w. They are bounded (the series in τ converges for |τ| up to about 3.5), so for |τ| <= 1 thirty
|
|
19
|
+
// terms suffice.
|
|
20
|
+
//
|
|
21
|
+
// In the tails the two terms above nearly cancel (the result is smaller than ½ erfc by a factor of about 2E),
|
|
22
|
+
// so for E >= 2.25 the smaller tail is computed from the equivalent form
|
|
23
|
+
// e^{-E} [F(y) + e^{-D} Σ_k g_k(τ) m^-k / (|τ| sqrt(2 pi m))], y = sqrt(E), F(y) = ½ erfcx(y) - 1/(2 sqrt(pi) y),
|
|
24
|
+
// which uses e^{-D} Σ_k g_k(0) r^-k = 1 (both sides of the identity have the same asymptotic expansion), with
|
|
25
|
+
// g_0 = m τ / (r x - a) in closed form and F(y) = -e^z Γ(-½, z) / (4 sqrt(pi)), z = y², from Legendre's
|
|
26
|
+
// continued fraction. Its two terms have opposite signs but differ by a factor of about 1 + 1/y².
|
|
27
|
+
//
|
|
28
|
+
// Both forms were checked against mpmath (scripts/mpmath/largebeta.py): within 1e-15 for m >= 20 and |τ| <= 1.
|
|
29
|
+
// The cost is about 2 µs whatever the size of a and b.
|
|
30
|
+
import { erfc } from "./erf.js";
|
|
31
|
+
import { bd0dd, stirlerr } from "./loader.js";
|
|
32
|
+
import { ddAdd, ddMul, ddSub, twoSum } from "./dd.js";
|
|
33
|
+
/**
|
|
34
|
+
* Smallest m = ab/(a+b) for which the expansion is used. Measured against mpmath with |τ| <= 1: within 9e-16 from
|
|
35
|
+
* m = 20 on (as accurate as the continued fraction), 7e-15 for m in 15-20 and 5e-14 for m in 10-15 (the series
|
|
36
|
+
* in 1/m no longer converges far enough). It is faster than the continued fraction from m = 10 on (2 µs against 8)
|
|
37
|
+
*/
|
|
38
|
+
export const TEMME_MIN_M = 25;
|
|
39
|
+
/** Largest |τ| = sqrt(2E/m) (the Taylor series in τ, with at most NMAX terms, is accurate to 1e-16 up to here) */
|
|
40
|
+
const TAU_MAX = 1;
|
|
41
|
+
/** Number of Taylor coefficients of g (the series converges like (|τ|/3.5)^n) */
|
|
42
|
+
const NMAX = 30;
|
|
43
|
+
/** Largest number of terms of the expansion in 1/m */
|
|
44
|
+
const KMAX = 12;
|
|
45
|
+
/** Below this y = sqrt(E) the first form is used, above it the tail form */
|
|
46
|
+
const Y_SWITCH = 1.5;
|
|
47
|
+
const SQRT_PI = 1.7724538509055160273;
|
|
48
|
+
const TWO_PI = 6.283185307179586477;
|
|
49
|
+
// Work arrays: Taylor coefficients of w = Σ wc[n] τ^n, of w², and of g and its successors
|
|
50
|
+
const wc = new Float64Array(NMAX + 2);
|
|
51
|
+
const w2 = new Float64Array(NMAX + 3);
|
|
52
|
+
const gc = new Float64Array(NMAX);
|
|
53
|
+
/** Horner evaluation of Σ_{n<len} c[n] τ^n */
|
|
54
|
+
function poly(c, len, tau) {
|
|
55
|
+
let s = 0;
|
|
56
|
+
for (let n = len - 1; n >= 0; n--)
|
|
57
|
+
s = s * tau + c[n];
|
|
58
|
+
return s;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Fill gc[0..n) with the Taylor coefficients of g(τ) = τ / w(τ) for the given p, q (p + q = 1).
|
|
62
|
+
* w = Σ_{n>=1} wc[n] τ^n with wc[1] = 1 from ½ (w²)' = τ (1 + δ w - pq w²), δ = q - p:
|
|
63
|
+
* the coefficient of τ^(j-1) gives (w²)_j = (2/j) (δ wc[j-2] - pq (w²)_{j-2}) for j >= 3, and
|
|
64
|
+
* (w²)_j = 2 wc[j-1] + Σ_{i=2}^{j-2} wc[i] wc[j-i] then gives wc[j-1]
|
|
65
|
+
*/
|
|
66
|
+
function gCoefficients(p, q, n) {
|
|
67
|
+
const delta = q - p;
|
|
68
|
+
const pq = p * q;
|
|
69
|
+
wc[1] = 1;
|
|
70
|
+
w2[1] = 0;
|
|
71
|
+
w2[2] = 1;
|
|
72
|
+
for (let j = 3; j <= n + 1; j++) {
|
|
73
|
+
const v = (2 / j) * (delta * wc[j - 2] - pq * w2[j - 2]);
|
|
74
|
+
let s = 0;
|
|
75
|
+
for (let i = 2; i <= j - 2; i++)
|
|
76
|
+
s += wc[i] * wc[j - i];
|
|
77
|
+
wc[j - 1] = (v - s) / 2;
|
|
78
|
+
w2[j] = v;
|
|
79
|
+
}
|
|
80
|
+
// g = 1 / (w/τ), where w/τ = Σ_n wc[n + 1] τ^n
|
|
81
|
+
gc[0] = 1;
|
|
82
|
+
for (let k = 1; k < n; k++) {
|
|
83
|
+
let s = 0;
|
|
84
|
+
for (let i = 1; i <= k; i++)
|
|
85
|
+
s += wc[i + 1] * gc[k - i];
|
|
86
|
+
gc[k] = -s;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Sum the expansion in 1/m. With tail false: Σ_{k>=0} ĥ_k(τ) m^-k (the scaled h_k); with tail true:
|
|
91
|
+
* Σ_{k>=1} ĝ_k(τ) m^-k (the scaled g_k without g_0). In τ, ĥ_k = (ĝ_k - ĝ_k(0))/τ and ĝ_{k+1} = dĥ_k/dτ, so each
|
|
92
|
+
* step drops the constant coefficient, shifts, and differentiates (in place in gc)
|
|
93
|
+
*/
|
|
94
|
+
function expansionSum(tau, m, n, tail) {
|
|
95
|
+
let len = n;
|
|
96
|
+
let mk = 1;
|
|
97
|
+
let sum = 0;
|
|
98
|
+
for (let k = 0; k < KMAX && len > 1; k++) {
|
|
99
|
+
let term;
|
|
100
|
+
if (tail) {
|
|
101
|
+
term = k === 0 ? 0 : mk * poly(gc, len, tau);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
// ĥ_k: coefficients gc[1..len)
|
|
105
|
+
let s = 0;
|
|
106
|
+
for (let i = len - 1; i >= 1; i--)
|
|
107
|
+
s = s * tau + gc[i];
|
|
108
|
+
term = mk * s;
|
|
109
|
+
}
|
|
110
|
+
sum += term;
|
|
111
|
+
if (k > 1 && Math.abs(term) <= 1e-17 * Math.abs(sum))
|
|
112
|
+
break;
|
|
113
|
+
// ĝ_{k+1}[i] = (i + 1) ĥ_k[i + 1] = (i + 1) gc[i + 2]
|
|
114
|
+
for (let i = 0; i + 2 < len; i++)
|
|
115
|
+
gc[i] = (i + 1) * gc[i + 2];
|
|
116
|
+
len -= 2;
|
|
117
|
+
mk /= m;
|
|
118
|
+
}
|
|
119
|
+
return sum;
|
|
120
|
+
}
|
|
121
|
+
// Gauss-Legendre nodes and weights (8 points, symmetric pairs) and erfc(1), computed with mpmath
|
|
122
|
+
const GL_X = [0.9602898564975363, 0.7966664774136267, 0.525532409916329, 0.1834346424956498];
|
|
123
|
+
const GL_W = [0.10122853629037626, 0.22238103445337448, 0.31370664587788727, 0.362683783378362];
|
|
124
|
+
const ERFC1 = 0.15729920705028513;
|
|
125
|
+
/**
|
|
126
|
+
* ½ erfc(y) to a few ulp. erfc in erf.ts forms 1 - erf(y) below 1, which loses a factor of up to 6 near y = 1, so
|
|
127
|
+
* for 0.5 <= y < 1 use erfc(y) = erfc(1) + (2/sqrt(pi)) ∫_y^1 e^(-t²) dt (both terms positive), with the integral
|
|
128
|
+
* by 8-point Gauss-Legendre quadrature (3e-16 relative on this interval)
|
|
129
|
+
*/
|
|
130
|
+
export function halfErfc(y) {
|
|
131
|
+
if (y < 0.5 || y >= 1)
|
|
132
|
+
return 0.5 * erfc(y);
|
|
133
|
+
const c = (1 + y) / 2;
|
|
134
|
+
const h = (1 - y) / 2;
|
|
135
|
+
let s = 0;
|
|
136
|
+
for (let i = 0; i < 4; i++) {
|
|
137
|
+
const t1 = c + h * GL_X[i];
|
|
138
|
+
const t2 = c - h * GL_X[i];
|
|
139
|
+
s += GL_W[i] * (Math.exp(-t1 * t1) + Math.exp(-t2 * t2));
|
|
140
|
+
}
|
|
141
|
+
return 0.5 * ERFC1 + (h * s) / SQRT_PI;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* F(y) = ½ erfcx(y) - 1/(2 sqrt(pi) y) for y >= 1.5, without cancellation:
|
|
145
|
+
* F = -e^z Γ(-½, z) / (4 sqrt(pi)) with e^z Γ(-½, z) = z^-½ / (z + 3/2 - 1·(3/2)/(z + 7/2 - 2·(5/2)/(z + 11/2 - ...))),
|
|
146
|
+
* evaluated backwards with a fixed number of terms (enough for 1e-16 from y = 1.5 on)
|
|
147
|
+
*/
|
|
148
|
+
function erfcxMinusLeading(y) {
|
|
149
|
+
const z = y * y;
|
|
150
|
+
const n = Math.ceil(4 + 120 / z);
|
|
151
|
+
let t = 0;
|
|
152
|
+
for (let k = n; k >= 1; k--)
|
|
153
|
+
t = (-k * (k + 0.5)) / (z + 2 * k + 1.5 + t);
|
|
154
|
+
return -1 / (4 * SQRT_PI * y * (z + 1.5 + t));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* I_x(a, b) (lower) or 1 - I_x(a, b) by the uniform asymptotic expansion, as [log, linear] (log NaN unless
|
|
158
|
+
* wantLog). x and 1 - x are given in double-double. Returns undefined outside the region where the expansion is
|
|
159
|
+
* used (m >= TEMME_MIN_M and |τ| <= TAU_MAX); the caller then uses the series or continued fractions
|
|
160
|
+
*/
|
|
161
|
+
export function betaIncTemme(a, b, xd, xcd, lower, wantLog) {
|
|
162
|
+
const rr = twoSum(a, b);
|
|
163
|
+
const r = rr[0];
|
|
164
|
+
const m = (a * b) / r;
|
|
165
|
+
if (!(m >= TEMME_MIN_M) || !Number.isFinite(r))
|
|
166
|
+
return undefined;
|
|
167
|
+
const p = a / r;
|
|
168
|
+
const q = b / r;
|
|
169
|
+
const pq = m / r;
|
|
170
|
+
// Cheap filter: |τ| <= 1 needs |w| = |x - p| / pq below about 1.6
|
|
171
|
+
const x = xd[0];
|
|
172
|
+
if (Math.abs(x - p) > 2 * pq)
|
|
173
|
+
return undefined;
|
|
174
|
+
// r x - a exactly enough (sign of x - p, and g_0 in the tail form)
|
|
175
|
+
const rx = ddMul(rr, xd);
|
|
176
|
+
const dx = ddSub(rx, [a, 0]);
|
|
177
|
+
const d = dx[0] + dx[1];
|
|
178
|
+
// E = bd0(a, r x) + bd0(b, r (1 - x)) in double-double
|
|
179
|
+
const e = ddAdd(bd0dd(a, rx), bd0dd(b, ddMul(rr, xcd)));
|
|
180
|
+
const E = e[0] + e[1];
|
|
181
|
+
const tau2 = (2 * E) / m;
|
|
182
|
+
if (!(tau2 <= TAU_MAX * TAU_MAX))
|
|
183
|
+
return undefined;
|
|
184
|
+
const absTau = Math.sqrt(tau2);
|
|
185
|
+
const tau = d < 0 ? -absTau : absTau;
|
|
186
|
+
const n = absTau <= 0.5 ? 20 : absTau <= 0.75 ? 24 : NMAX;
|
|
187
|
+
gCoefficients(p, q, n);
|
|
188
|
+
const eD = Math.exp(-(stirlerr(a) + stirlerr(b) - stirlerr(r)));
|
|
189
|
+
const y = Math.sqrt(E);
|
|
190
|
+
// The smaller tail is the lower one when x < p
|
|
191
|
+
const smallIsLower = d < 0;
|
|
192
|
+
if (y < Y_SWITCH) {
|
|
193
|
+
// ½ erfc(∓y) ∓ R with R = e^-E e^-D Σ ĥ_k m^-k / sqrt(2 pi m)
|
|
194
|
+
const R = (Math.exp(-e[0]) * (1 - e[1]) * eD * expansionSum(tau, m, n, false)) / Math.sqrt(TWO_PI * m);
|
|
195
|
+
const sy = d < 0 ? y : -y; // -η sqrt(r/2)
|
|
196
|
+
const lowerV = (sy >= 0 ? halfErfc(sy) : 1 - halfErfc(-sy)) - R;
|
|
197
|
+
const upperV = (sy <= 0 ? halfErfc(-sy) : 1 - halfErfc(sy)) + R;
|
|
198
|
+
const v = lower ? lowerV : upperV;
|
|
199
|
+
if (!wantLog)
|
|
200
|
+
return [NaN, v];
|
|
201
|
+
const small = smallIsLower ? lowerV : upperV;
|
|
202
|
+
return [lower === smallIsLower ? Math.log(v) : Math.log1p(-small), v];
|
|
203
|
+
}
|
|
204
|
+
// Tail form: smaller tail = e^-E B
|
|
205
|
+
// The k = 0 term g_0 / (|τ| sqrt(2 pi m)) with g_0 = m τ / (r x - a) is sqrt(m / (2 pi)) / |r x - a| (τ cancels)
|
|
206
|
+
const B = erfcxMinusLeading(y) +
|
|
207
|
+
eD * (Math.sqrt(m / TWO_PI) / Math.abs(d) + expansionSum(tau, m, n, true) / (absTau * Math.sqrt(TWO_PI * m)));
|
|
208
|
+
let small;
|
|
209
|
+
if (e[0] < 700)
|
|
210
|
+
small = Math.exp(-e[0]) * (1 - e[1]) * B;
|
|
211
|
+
else
|
|
212
|
+
small = Math.exp(700 - e[0]) * (1 - e[1]) * B * Math.exp(-700);
|
|
213
|
+
if (lower === smallIsLower)
|
|
214
|
+
return [wantLog ? -e[0] - e[1] + Math.log(B) : NaN, small];
|
|
215
|
+
return [wantLog ? Math.log1p(-small) : NaN, 1 - small];
|
|
216
|
+
}
|