@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/beta.js
CHANGED
|
@@ -55,6 +55,9 @@ export function beta(a, b) {
|
|
|
55
55
|
let sign = 1;
|
|
56
56
|
if (a === 0 || b === 0)
|
|
57
57
|
return Infinity;
|
|
58
|
+
// B(a, b) > 1/a overflows where 1/a does (the product below was NaN at beta(1e-310, 1e-310); R gives Inf)
|
|
59
|
+
if (1 / Math.min(a, b) === Infinity)
|
|
60
|
+
return Infinity;
|
|
58
61
|
if (Math.abs(a) < Math.abs(b))
|
|
59
62
|
[a, b] = [b, a];
|
|
60
63
|
if (Math.abs(a) > ASYMP_FACTOR * Math.abs(b) && a > ASYMP_FACTOR) {
|
|
@@ -120,6 +123,10 @@ export function lbeta(a, b) {
|
|
|
120
123
|
}
|
|
121
124
|
if (a === 0 || b === 0)
|
|
122
125
|
return Infinity;
|
|
126
|
+
// Γ of a shape below 1e-300 overflows (Γ(a) ≈ 1/a), and the product below was Infinity or NaN (lbeta(1e-310, 1e-310) was
|
|
127
|
+
// NaN, R gives 714.49452600871405): the logs instead, -log a each, which cancel by at most a few ulps
|
|
128
|
+
if (Math.min(a, b) < 1e-300)
|
|
129
|
+
return lgamma(a) + lgamma(b) - lgamma(a + b);
|
|
123
130
|
if (Math.abs(a) < Math.abs(b))
|
|
124
131
|
[a, b] = [b, a];
|
|
125
132
|
if (Math.abs(a) > ASYMP_FACTOR * Math.abs(b) && a > ASYMP_FACTOR)
|
package/dist/special/dd.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare function ddMulNum(a: DD, b: number): DD;
|
|
|
16
16
|
export declare function ddDiv(a: DD, b: DD): DD;
|
|
17
17
|
export declare function ddDivNum(a: DD, b: number): DD;
|
|
18
18
|
/**
|
|
19
|
-
* Natural log in double-double. x
|
|
19
|
+
* Natural log in double-double. 0 gives -Infinity, Infinity gives Infinity, a negative x or NaN gives NaN.
|
|
20
20
|
*
|
|
21
21
|
* After reducing x = m 2^k (m ∈ [1/√2, √2)), divide m by the nearest c = i/128 and use
|
|
22
22
|
* log m = log c + 2 atanh(s), s = (m - c)/(m + c) (|s| <= 0.0028)
|
package/dist/special/dd.js
CHANGED
|
@@ -373,7 +373,7 @@ const LOGTAB128 = [
|
|
|
373
373
|
[0.3574558889218038, -2.5136910072413547e-17],
|
|
374
374
|
];
|
|
375
375
|
/**
|
|
376
|
-
* Natural log in double-double. x
|
|
376
|
+
* Natural log in double-double. 0 gives -Infinity, Infinity gives Infinity, a negative x or NaN gives NaN.
|
|
377
377
|
*
|
|
378
378
|
* After reducing x = m 2^k (m ∈ [1/√2, √2)), divide m by the nearest c = i/128 and use
|
|
379
379
|
* log m = log c + 2 atanh(s), s = (m - c)/(m + c) (|s| <= 0.0028)
|
|
@@ -386,6 +386,9 @@ const LOGTAB128 = [
|
|
|
386
386
|
export function ddLog(x) {
|
|
387
387
|
if (!ddEnabled)
|
|
388
388
|
return [Math.log(x[0]), 0];
|
|
389
|
+
// Outside (0, Infinity) the table index below is out of range; answer as Math.log does
|
|
390
|
+
if (!(x[0] > 0 && x[0] < Infinity))
|
|
391
|
+
return [Math.log(x[0]), 0];
|
|
389
392
|
// x = m 2^k, m ∈ [1/√2, √2). Subnormals are first multiplied by 2^600 to make them normal (exact, being a power of 2)
|
|
390
393
|
let x0 = x[0];
|
|
391
394
|
let x1 = x[1];
|
package/dist/special/gamma.js
CHANGED
|
@@ -170,6 +170,10 @@ export function lgammaSign(x) {
|
|
|
170
170
|
return { value: Infinity, sign: 1 };
|
|
171
171
|
return { value: LOGPI - Math.log(z) - w, sign };
|
|
172
172
|
}
|
|
173
|
+
// log|Γ(x)| = -log|x| - γ x + O(x²), -log|x| to the double; the product below overflowed (1 / x) and lgamma was Infinity
|
|
174
|
+
// at a subnormal x (lgamma(5e-324) is 744.44007192138122, as R gives)
|
|
175
|
+
if (x !== 0 && Math.abs(x) < 1e-300)
|
|
176
|
+
return { value: -Math.log(Math.abs(x)), sign: x > 0 ? 1 : -1 };
|
|
173
177
|
if (x < 13) {
|
|
174
178
|
let z = 1;
|
|
175
179
|
let p = 0;
|
package/dist/special/igam.js
CHANGED
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
// series or continued fraction, never by taking the log of a value that over/underflowed in exp
|
|
9
9
|
// - The prefactor x^a e^-x / Gamma(a) is computed with Loader's dpoisRaw (= x^a e^-x / Gamma(a+1)),
|
|
10
10
|
// to avoid cancellation in the log for large a and x
|
|
11
|
-
import { erfc } from "./erf.js";
|
|
11
|
+
import { erfc, erfcxNonNegative } from "./erf.js";
|
|
12
12
|
import { SPLITTER } from "./dd.js";
|
|
13
13
|
import { MACHEP } from "./constants.js";
|
|
14
14
|
import { lgamma, lgamma1p } from "./gamma.js";
|
|
15
15
|
import { IGAM_ASYMP_COEFF_D, IGAM_ASYMP_COEFF_K, IGAM_ASYMP_COEFF_N } from "./igamAsympCoeff.js";
|
|
16
|
-
import { dpoisRaw, dpoisRawBoth } from "./loader.js";
|
|
16
|
+
import { bd0dd, dpoisRaw, dpoisRawBoth } from "./loader.js";
|
|
17
17
|
import { log1pmx } from "./log1pmx.js";
|
|
18
18
|
// xsf uses 2000. For a around 1e6 with x close to a, the series and continued fraction take thousands of iterations, so raise it
|
|
19
19
|
const MAXITER = 10_000_000;
|
|
@@ -23,17 +23,27 @@ const SMALLRATIO = 0.3;
|
|
|
23
23
|
const LARGERATIO = 4.5;
|
|
24
24
|
const BIG = 4.503599627370496e15;
|
|
25
25
|
const BIGINV = 2.22044604925031308085e-16;
|
|
26
|
+
/**
|
|
27
|
+
* Below this the factor x^a e^-x / Gamma(a + 1) (dpoisRaw) is rounded on the subnormal grid or 0, and the linear value
|
|
28
|
+
* of the tail is exp of its log instead: the product with the series or continued fraction had lost digits, or was 0
|
|
29
|
+
* where the tail is a few subnormal ulps (pchisq(150856.77, 172464.33) was 0, its log -743.7469247408216, so 1e-323)
|
|
30
|
+
*/
|
|
31
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
26
32
|
/** Series for P(a, x) (DLMF 8.11.4) */
|
|
27
33
|
function igamSeries(a, x, log) {
|
|
28
34
|
const ans = igamSeriesSum(a, x);
|
|
29
35
|
// x^a e^-x / Gamma(a) * ans / a = dpoisRaw(a, x) * ans
|
|
30
|
-
|
|
36
|
+
if (log)
|
|
37
|
+
return dpoisRaw(a, x, true) + Math.log(ans);
|
|
38
|
+
const d = dpoisRaw(a, x, false);
|
|
39
|
+
return d >= MIN_NORMAL ? d * ans : Math.exp(dpoisRaw(a, x, true) + Math.log(ans));
|
|
31
40
|
}
|
|
32
41
|
/** [log, linear] of igamSeries */
|
|
33
42
|
function igamSeriesBoth(a, x) {
|
|
34
43
|
const ans = igamSeriesSum(a, x);
|
|
35
44
|
const d = dpoisRawBoth(a, x);
|
|
36
|
-
|
|
45
|
+
const l = d[0] + Math.log(ans);
|
|
46
|
+
return [l, d[1] >= MIN_NORMAL ? d[1] * ans : Math.exp(l)];
|
|
37
47
|
}
|
|
38
48
|
/** sum_n x^n / ((a + 1) ... (a + n)): P(a, x) = x^a e^-x / Gamma(a + 1) times this */
|
|
39
49
|
export function igamSeriesSum(a, x) {
|
|
@@ -105,18 +115,22 @@ export function igamSeriesSumDD(a, x) {
|
|
|
105
115
|
function igamcContinuedFraction(a, x, log) {
|
|
106
116
|
const ans = igamcContinuedFractionValue(a, x);
|
|
107
117
|
// x^a e^-x / Gamma(a) * ans = a * dpoisRaw(a, x) * ans
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
118
|
+
if (log)
|
|
119
|
+
return Math.log(a) + dpoisRaw(a, x, true) + Math.log(ans);
|
|
120
|
+
const d = dpoisRaw(a, x, false);
|
|
121
|
+
return d >= MIN_NORMAL ? a * d * ans : Math.exp(Math.log(a) + dpoisRaw(a, x, true) + Math.log(ans));
|
|
111
122
|
}
|
|
112
123
|
/** [log, linear] of igamcContinuedFraction */
|
|
113
124
|
function igamcContinuedFractionBoth(a, x) {
|
|
114
125
|
const ans = igamcContinuedFractionValue(a, x);
|
|
115
126
|
const d = dpoisRawBoth(a, x);
|
|
116
|
-
|
|
127
|
+
const l = Math.log(a) + d[0] + Math.log(ans);
|
|
128
|
+
return [l, d[1] >= MIN_NORMAL ? a * d[1] * ans : Math.exp(l)];
|
|
117
129
|
}
|
|
118
130
|
/** The continued fraction of DLMF 8.9.2: Q(a, x) = x^a e^-x / Gamma(a) times this */
|
|
119
131
|
export function igamcContinuedFractionValue(a, x) {
|
|
132
|
+
if (x > 1e20)
|
|
133
|
+
return igamcContinuedFractionLentz(a, x);
|
|
120
134
|
let y = 1 - a;
|
|
121
135
|
let z = x + y + 1;
|
|
122
136
|
let c = 0;
|
|
@@ -156,6 +170,35 @@ export function igamcContinuedFractionValue(a, x) {
|
|
|
156
170
|
}
|
|
157
171
|
return ans;
|
|
158
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* The same continued fraction by the modified Lentz method (only ratios are kept, so nothing grows). For large x
|
|
175
|
+
* the convergents above overflow: the first denominator is already (x + 2 - a) x, and each step multiplies by about x
|
|
176
|
+
* while the rescaling divides by only 2^52 (pgamma(7.2e60, 6.1e55), ppois(3.66e124, 8.1e125) and ppois(3.4e240,
|
|
177
|
+
* 5.5e241) were NaN). Used above 1e20, where overflow takes about 70 steps or fewer
|
|
178
|
+
*/
|
|
179
|
+
function igamcContinuedFractionLentz(a, x) {
|
|
180
|
+
const tiny = 1e-300;
|
|
181
|
+
let b = x + 1 - a;
|
|
182
|
+
let c = 1 / tiny;
|
|
183
|
+
let d = 1 / b;
|
|
184
|
+
let h = d;
|
|
185
|
+
for (let i = 1; i < MAXITER; i++) {
|
|
186
|
+
const an = -i * (i - a);
|
|
187
|
+
b += 2;
|
|
188
|
+
d = an * d + b;
|
|
189
|
+
if (Math.abs(d) < tiny)
|
|
190
|
+
d = tiny;
|
|
191
|
+
c = b + an / c;
|
|
192
|
+
if (Math.abs(c) < tiny)
|
|
193
|
+
c = tiny;
|
|
194
|
+
d = 1 / d;
|
|
195
|
+
const del = d * c;
|
|
196
|
+
h *= del;
|
|
197
|
+
if (Math.abs(del - 1) <= MACHEP)
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
return h;
|
|
201
|
+
}
|
|
159
202
|
/** Q(a, x) by DLMF 8.7.3. For small x (the value does not approach 0) */
|
|
160
203
|
function igamcSeries(a, x) {
|
|
161
204
|
let fac = 1;
|
|
@@ -177,16 +220,23 @@ function igamcSeries(a, x) {
|
|
|
177
220
|
/** Temme's uniform asymptotic expansion for a close to x (DLMF 8.12.3/8.12.4) */
|
|
178
221
|
function asymptoticSeries(a, x, lower) {
|
|
179
222
|
const sgn = lower ? -1 : 1;
|
|
223
|
+
const eta = temmeEta(a, x);
|
|
224
|
+
let res = 0.5 * erfc(sgn * eta * Math.sqrt(a / 2));
|
|
225
|
+
res += (sgn * Math.exp(-0.5 * a * eta * eta) * temmeSum(a, eta)) / Math.sqrt(2 * Math.PI * a);
|
|
226
|
+
return res;
|
|
227
|
+
}
|
|
228
|
+
/** η of Temme's expansion: η^2 / 2 = λ - 1 - log λ (λ = x / a), with the sign of λ - 1 */
|
|
229
|
+
function temmeEta(a, x) {
|
|
180
230
|
const lambda = x / a;
|
|
181
231
|
const sigma = (x - a) / a;
|
|
182
|
-
let eta;
|
|
183
232
|
if (lambda > 1)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
233
|
+
return Math.sqrt(-2 * log1pmx(sigma));
|
|
234
|
+
if (lambda < 1)
|
|
235
|
+
return -Math.sqrt(-2 * log1pmx(sigma));
|
|
236
|
+
return 0;
|
|
237
|
+
}
|
|
238
|
+
/** Σ_k c_k(η) a^-k of Temme's expansion */
|
|
239
|
+
function temmeSum(a, eta) {
|
|
190
240
|
const etapow = new Array(IGAM_ASYMP_COEFF_N).fill(0);
|
|
191
241
|
etapow[0] = 1;
|
|
192
242
|
let maxpow = 0;
|
|
@@ -216,8 +266,46 @@ function asymptoticSeries(a, x, lower) {
|
|
|
216
266
|
absoldterm = absterm;
|
|
217
267
|
afac /= a;
|
|
218
268
|
}
|
|
219
|
-
|
|
220
|
-
|
|
269
|
+
return sum;
|
|
270
|
+
}
|
|
271
|
+
/** xsf's region for Temme's expansion: |x - a| / a below 0.3 for 20 < a < 200, within 4.5 standard deviations for larger a */
|
|
272
|
+
function xsfAsymptotic(a, absxmaA) {
|
|
273
|
+
return (a > SMALL && a < LARGE && absxmaA < SMALLRATIO) || (a > LARGE && absxmaA < LARGERATIO / Math.sqrt(a));
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Above this a, Temme's expansion is used at every x (below 1e15 a): the double-double series of P overflows in its
|
|
277
|
+
* products of terms around a (pgamma(0.99 * 2^1000, 2^1000) was NaN after 0.1 s), and only the leading term of the
|
|
278
|
+
* expansion is left
|
|
279
|
+
*/
|
|
280
|
+
const HUGE_A = 1e299;
|
|
281
|
+
/**
|
|
282
|
+
* Beyond xsf's region, Temme's expansion is also used for a > 200 with |x - a| / a below 1e-3. There the series and
|
|
283
|
+
* the continued fraction need about 40 a / |x - a| terms (beyond 10^7 for a above 1e14: ppois(3.28e19 + 1e11, 3.28e19)
|
|
284
|
+
* took 0.15 s per call and stopped short of convergence), while η is below 1e-3 and the expansion converges in a few
|
|
285
|
+
* terms; and at every x above HUGE_A. See temmeTail for the form used.
|
|
286
|
+
*/
|
|
287
|
+
const farAsymptotic = (a, absxmaA) => a > LARGE && (absxmaA < 1e-3 || a > HUGE_A);
|
|
288
|
+
/**
|
|
289
|
+
* [log, linear] of the smaller tail (the upper if x > a) by Temme's expansion, in the form
|
|
290
|
+
* e^(-a η^2 / 2) (erfcx(y) / 2 ± S / sqrt(2π a)), y = |η| sqrt(a / 2),
|
|
291
|
+
* with a η^2 / 2 = a log(a / x) + x - a computed as Loader's bd0 in double-double. Forming erfc(y) and
|
|
292
|
+
* e^(-a η^2 / 2) from η directly carries the rounding of η times a η^2 (1e-14 relative at 11 standard deviations)
|
|
293
|
+
*/
|
|
294
|
+
function temmeTail(a, x) {
|
|
295
|
+
const e = bd0dd(a, [x, 0]);
|
|
296
|
+
const sgn = x > a ? 1 : -1;
|
|
297
|
+
const eta = temmeEta(a, x);
|
|
298
|
+
// Beyond a = 1e299 the sum is c_0(η) alone (the next term is c_1 / a), used for any x there (see farAsymptotic):
|
|
299
|
+
// in closed form c_0 = 1/(λ - 1) - 1/η away from η = 0, where the power series in η no longer converges
|
|
300
|
+
const s = a > HUGE_A && Math.abs(eta) > 0.1 ? 1 / ((x - a) / a) - 1 / eta : temmeSum(a, eta);
|
|
301
|
+
const b = 0.5 * erfcxNonNegative(Math.sqrt(e[0] + e[1])) + (sgn * s) / Math.sqrt(2 * Math.PI * a);
|
|
302
|
+
// (far below the subnormals e[1], half an ulp of E, can exceed 1)
|
|
303
|
+
return [-e[0] - e[1] + Math.log(b), e[0] > 2000 ? 0 : Math.exp(-e[0]) * (1 - e[1]) * b];
|
|
304
|
+
}
|
|
305
|
+
/** [log, linear] of P(a, x) (Q if !lower) in the region of farAsymptotic */
|
|
306
|
+
function farTail(a, x, lower) {
|
|
307
|
+
const [log, lin] = temmeTail(a, x);
|
|
308
|
+
return (x > a) !== lower ? [log, lin] : [Math.log1p(-lin), 1 - lin];
|
|
221
309
|
}
|
|
222
310
|
/**
|
|
223
311
|
* Decide which tail to compute directly and return its value. Same case split as xsf's igamc.
|
|
@@ -249,6 +337,13 @@ function directTailBoth(a, x) {
|
|
|
249
337
|
const q = igamcSeries(a, x);
|
|
250
338
|
return { lower: false, log: Math.log(q), lin: q };
|
|
251
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Below this a, Q(a, x) is taken as (a / A_SUB) Q(A_SUB, x): Q(a, x) / a varies with a by a relative O(a log x) only
|
|
342
|
+
* (below 1e-297 here). Q is then of the order of a, so subnormal for a below 1e-308 or so, and the direct forms had
|
|
343
|
+
* lost digits before the log was taken (pgamma(1, 4.8e-316, scale = 2.06, lower = FALSE, log = TRUE) was 5e-9
|
|
344
|
+
* off)
|
|
345
|
+
*/
|
|
346
|
+
const A_SUB = 1e-300;
|
|
252
347
|
/**
|
|
253
348
|
* Return [log, linear] of gammaInc from a single series/continued-fraction computation.
|
|
254
349
|
* The values are bit-identical to gammaInc(.., true) and gammaInc(.., false) respectively
|
|
@@ -279,12 +374,17 @@ export function gammaIncBoth(a, x, lower) {
|
|
|
279
374
|
return [Math.log1p(-Math.exp(lq)), -Math.expm1(lq)];
|
|
280
375
|
return [lq, Math.exp(lq)];
|
|
281
376
|
}
|
|
377
|
+
if (!lower && a < A_SUB) {
|
|
378
|
+
const [l, v] = gammaIncBoth(A_SUB, x, false);
|
|
379
|
+
return [l + Math.log(a / A_SUB), v * (a / A_SUB)];
|
|
380
|
+
}
|
|
282
381
|
const absxmaA = Math.abs(x - a) / a;
|
|
283
|
-
if ((a
|
|
284
|
-
(a > LARGE && absxmaA < LARGERATIO / Math.sqrt(a))) {
|
|
382
|
+
if (xsfAsymptotic(a, absxmaA)) {
|
|
285
383
|
const v = asymptoticSeries(a, x, lower);
|
|
286
384
|
return [v > 0.5 ? Math.log1p(-asymptoticSeries(a, x, !lower)) : Math.log(v), v];
|
|
287
385
|
}
|
|
386
|
+
if (farAsymptotic(a, absxmaA))
|
|
387
|
+
return farTail(a, x, lower);
|
|
288
388
|
const d = directTailBoth(a, x);
|
|
289
389
|
if (d.lower === lower)
|
|
290
390
|
return [d.log, d.lin];
|
|
@@ -323,16 +423,21 @@ export function gammaInc(a, x, lower, log) {
|
|
|
323
423
|
return log ? Math.log1p(-Math.exp(lq)) : -Math.expm1(lq);
|
|
324
424
|
return log ? lq : Math.exp(lq);
|
|
325
425
|
}
|
|
426
|
+
if (!lower && a < A_SUB) {
|
|
427
|
+
const v = gammaInc(A_SUB, x, false, log);
|
|
428
|
+
return log ? v + Math.log(a / A_SUB) : v * (a / A_SUB);
|
|
429
|
+
}
|
|
326
430
|
/* Asymptotic regime where a ~ x: both tails can be computed directly */
|
|
327
431
|
const absxmaA = Math.abs(x - a) / a;
|
|
328
|
-
if ((a
|
|
329
|
-
(a > LARGE && absxmaA < LARGERATIO / Math.sqrt(a))) {
|
|
432
|
+
if (xsfAsymptotic(a, absxmaA)) {
|
|
330
433
|
const v = asymptoticSeries(a, x, lower);
|
|
331
434
|
if (!log)
|
|
332
435
|
return v;
|
|
333
436
|
// The log of a value near 1 is built with log1p from the small value of the other side
|
|
334
437
|
return v > 0.5 ? Math.log1p(-asymptoticSeries(a, x, !lower)) : Math.log(v);
|
|
335
438
|
}
|
|
439
|
+
if (farAsymptotic(a, absxmaA))
|
|
440
|
+
return farTail(a, x, lower)[log ? 0 : 1];
|
|
336
441
|
if (log) {
|
|
337
442
|
const d = directTail(a, x, true);
|
|
338
443
|
if (d.lower === lower)
|
package/dist/special/incbet.d.ts
CHANGED
|
@@ -23,3 +23,13 @@ export declare function betaIncDD(a: number, b: number, xd: DD, xcd: DD, lower:
|
|
|
23
23
|
export declare function betaIncBothDD(a: number, b: number, xd: DD, xcd: DD, lower: boolean): [number, number];
|
|
24
24
|
/** Version of betaInc returning both (takes x and 1 - x in double precision, like betaInc) */
|
|
25
25
|
export declare function betaIncBoth(a: number, b: number, x: number, xc: number, lower: boolean): [number, number];
|
|
26
|
+
/**
|
|
27
|
+
* 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
|
|
28
|
+
* underflowed to 0, as in the F distribution far out: only lx then carries it), as [log, linear] (log NaN unless
|
|
29
|
+
* 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
|
|
30
|
+
* (-b x)^n / n!, and b x is at most 4 here (b near the largest double), mostly negligible.
|
|
31
|
+
* log(1/(a B(a, b))) is lgamma(a+b) - lgamma(b) - lgamma(a+1) without subtraction, or for a small shape
|
|
32
|
+
* 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
|
|
33
|
+
* rounding of ε |log r| would become the relative error of the result)
|
|
34
|
+
*/
|
|
35
|
+
export declare function betaIncTinyX(a: number, b: number, x: number, lx: DD, lower: boolean, wantLog: boolean): [number, number];
|