@numero-uno-jpn/rstats 0.15.3 → 0.16.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 +166 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-VLUL2HFW.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +24 -4
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +12 -4
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +5 -0
- package/dist/distributions/f.js +32 -17
- package/dist/distributions/gamma.js +27 -3
- package/dist/distributions/geometric.js +49 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +2 -1
- package/dist/distributions/laplace.js +27 -6
- package/dist/distributions/logistic.js +4 -1
- package/dist/distributions/lognormal.js +8 -2
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +94 -17
- package/dist/distributions/normal.js +9 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +4 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/triangular.js +10 -5
- package/dist/distributions/tukey.js +5 -2
- package/dist/distributions/weibull.js +10 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
// The power-series prefix shared by the Bessel functions I and J (besselIK.ts, besselJY.ts).
|
|
2
|
-
import { gamma } from "./gamma.js";
|
|
2
|
+
import { gamma, lgamma } from "./gamma.js";
|
|
3
3
|
import { dpoisRaw } from "./loader.js";
|
|
4
4
|
const DBL_MIN = 2.2250738585072014e-308;
|
|
5
|
+
const EPS = Number.EPSILON;
|
|
6
|
+
/** Below this x, x / 2 is not exact (Number.MIN_VALUE / 2 is 0) */
|
|
7
|
+
const HALF_EXACT = 2 ** -1021;
|
|
8
|
+
/** log(x/2), also for subnormal x (log(Number.MIN_VALUE / 2) was -Infinity) */
|
|
9
|
+
export function logHalf(x) {
|
|
10
|
+
return x >= HALF_EXACT ? Math.log(x / 2) : Math.log(x) - Math.LN2;
|
|
11
|
+
}
|
|
12
|
+
/** (x/2)^v, also for subnormal x */
|
|
13
|
+
export function powHalf(x, v) {
|
|
14
|
+
return x >= HALF_EXACT ? Math.pow(x / 2, v) : Math.pow(x, v) * Math.pow(2, -v);
|
|
15
|
+
}
|
|
5
16
|
/**
|
|
6
17
|
* (x/2)^v / Gamma(v + 1) * sum, the leading factor of the power series of I_v and J_v times the series sum.
|
|
7
18
|
* For v >= 10 the factor is Loader's Poisson term dpois(v; x/2) e^(x/2), which is accurate where pow / gamma loses
|
|
@@ -11,7 +22,7 @@ const DBL_MIN = 2.2250738585072014e-308;
|
|
|
11
22
|
*/
|
|
12
23
|
export function seriesTimesPrefix(v, x, sum) {
|
|
13
24
|
if (v < 10)
|
|
14
|
-
return (
|
|
25
|
+
return (powHalf(x, v) / gamma(v + 1)) * sum;
|
|
15
26
|
const lambda = x / 2;
|
|
16
27
|
const d = dpoisRaw(v, lambda, false);
|
|
17
28
|
if (d >= DBL_MIN && lambda < 700) {
|
|
@@ -22,3 +33,35 @@ export function seriesTimesPrefix(v, x, sum) {
|
|
|
22
33
|
const ld = dpoisRaw(v, lambda, true) + lambda + Math.log(Math.abs(sum));
|
|
23
34
|
return Math.sign(sum) * Math.exp(ld);
|
|
24
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Large orders. The recurrences in besselIK.ts and besselJY.ts take about v steps, which does not end for v = 1e12
|
|
38
|
+
* (besselK(1, 1e12)), while the result is far beyond the range of doubles. Above this order, bounds on the log of the
|
|
39
|
+
* result decide such cases first
|
|
40
|
+
*/
|
|
41
|
+
export const LARGE_ORDER = 1000;
|
|
42
|
+
const LOG_MAX = Math.log(Number.MAX_VALUE);
|
|
43
|
+
/** Values below Number.MIN_VALUE / 2 round to 0 */
|
|
44
|
+
const LOG_MIN = Math.log(Number.MIN_VALUE) - Math.LN2;
|
|
45
|
+
/**
|
|
46
|
+
* Bounds on log I_v(x) (v >= 0) from its power series (x/2)^v / Gamma(v + 1) sum (x^2/4)^k / (k! (v+1)_k): the terms
|
|
47
|
+
* are positive, and the sum is at most exp(x^2 / (4 (v + 1))). The lower bound is also the upper bound of
|
|
48
|
+
* log abs(J_v(x)) (DLMF 10.14.4)
|
|
49
|
+
*/
|
|
50
|
+
export function logISeriesBounds(v, x) {
|
|
51
|
+
const a = v * logHalf(x);
|
|
52
|
+
const g = lgamma(v + 1);
|
|
53
|
+
const t = (x * x) / (4 * (v + 1));
|
|
54
|
+
return { lo: a - g, hi: a - g + t, size: Math.abs(a) + Math.abs(g) + t };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 1 if a value whose log lies in [lo, hi] certainly overflows, -1 if it certainly rounds to 0, 0 otherwise. The bounds
|
|
58
|
+
* must clear the limits by a margin above the rounding of their terms
|
|
59
|
+
*/
|
|
60
|
+
export function certainRange(b) {
|
|
61
|
+
const m = 1 + 64 * EPS * b.size;
|
|
62
|
+
if (b.lo - m > LOG_MAX)
|
|
63
|
+
return 1;
|
|
64
|
+
if (b.hi + m < LOG_MIN)
|
|
65
|
+
return -1;
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
package/dist/special/beta.js
CHANGED
|
@@ -156,7 +156,10 @@ const HUGE = 2 ** 52;
|
|
|
156
156
|
*/
|
|
157
157
|
function lbetaHuge(a, b) {
|
|
158
158
|
const lead = ddMulNum(ddSub(ddLog([b, 0]), ddLog([a, 0])), b);
|
|
159
|
-
const
|
|
159
|
+
const l1p = Math.log1p(b / a);
|
|
160
|
+
// a + b can overflow (lbeta(1e308, 1e308) was -Infinity), while a L and b L stay finite
|
|
161
|
+
const sumTerm = Number.isFinite(a + b) ? (a + b - 0.5) * l1p : a * l1p + (b - 0.5) * l1p;
|
|
162
|
+
const rest = -0.5 * Math.log(b) - sumTerm + (stirlerr(a) + stirlerr(b) - stirlerr(a + b));
|
|
160
163
|
return ddAdd(ddAdd(lead, LS2PI_DD), [rest, 0]);
|
|
161
164
|
}
|
|
162
165
|
/**
|
package/dist/special/choose.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// log B is taken from lbetaDD (double-double), since its value can be in the hundreds and exp would turn the
|
|
11
11
|
// rounding of a double log into a relative error of 1e-13.
|
|
12
12
|
import { nonInt, roundHalfEven } from "../internal/integer.js";
|
|
13
|
+
import { ldexp } from "../internal/summary.js";
|
|
13
14
|
import { lbeta, lbetaDD } from "./beta.js";
|
|
14
15
|
import { ddAdd, ddDiv, ddLog, ddMul, twoSum } from "./dd.js";
|
|
15
16
|
import { gamma, lgamma, lgamma1p, sinpi } from "./gamma.js";
|
|
@@ -40,6 +41,16 @@ function fallingRatio(n, k) {
|
|
|
40
41
|
return r;
|
|
41
42
|
}
|
|
42
43
|
/** log B(a, b) (a, b > 0): double-double where lbetaDD is accurate, else lbeta (whose value is then large) */
|
|
44
|
+
/** n (n - 1) ... (n - k + 1) / k! where the product overflows on the way, in the coordinates 2^-s with 2^s near abs(n) */
|
|
45
|
+
function fallingRatioScaled(n, k) {
|
|
46
|
+
const s = Math.floor(Math.log2(Math.abs(n)));
|
|
47
|
+
let r = [1, 0];
|
|
48
|
+
for (let j = 0; j < k; j++) {
|
|
49
|
+
const t = twoSum(n, -j);
|
|
50
|
+
r = ddDiv(ddMul(r, [ldexp(t[0], -s), ldexp(t[1], -s)]), [j + 1, 0]);
|
|
51
|
+
}
|
|
52
|
+
return ldexp(r[0] + r[1], s * k);
|
|
53
|
+
}
|
|
43
54
|
function logBeta(a, b) {
|
|
44
55
|
return Math.min(a, b) >= 1 && Math.max(a, b) < 2 ** 52 ? lbetaDD(a, b) : [lbeta(a, b), 0];
|
|
45
56
|
}
|
|
@@ -123,11 +134,9 @@ export function choose(n, k, options) {
|
|
|
123
134
|
const v = r[0] + r[1];
|
|
124
135
|
if (Number.isFinite(v))
|
|
125
136
|
return isInt(n) || snap ? Math.round(v) : v;
|
|
126
|
-
//
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
p = (p * (n - j)) / (j + 1);
|
|
130
|
-
return p;
|
|
137
|
+
// The product overflows on the way (n (n - 1) at n = 1.5e154 while choose(n, 2) = 1.125e308; the double-double steps
|
|
138
|
+
// then give NaN): the factors n - j divided by 2^s (exact), and the result scaled back by 2^(s k)
|
|
139
|
+
return fallingRatioScaled(n, k);
|
|
131
140
|
}
|
|
132
141
|
if (rc) {
|
|
133
142
|
n = rSnap(n, k);
|
|
@@ -186,9 +195,9 @@ export function lchoose(n, k, options) {
|
|
|
186
195
|
const l = ddLog(r[0] < 0 ? [-r[0], -r[1]] : r);
|
|
187
196
|
return l[0] + l[1];
|
|
188
197
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
//
|
|
198
|
+
// Out of range: sum of logs (the result is large in magnitude, so its rounding is relatively small). v = 0 also comes
|
|
199
|
+
// here: a coefficient below the doubles has a finite log (lchoose(MIN, 2) was -Infinity for -745.13; issue #107),
|
|
200
|
+
// and an exact zero has a factor n - j = 0, whose log makes the sum -Infinity
|
|
192
201
|
let s = -lgamma(k + 1);
|
|
193
202
|
for (let j = 0; j < k; j++)
|
|
194
203
|
s += Math.log(Math.abs(n - j));
|
package/dist/special/dd.js
CHANGED
|
@@ -21,22 +21,66 @@ export function twoSum(a, b) {
|
|
|
21
21
|
if (!ddEnabled)
|
|
22
22
|
return [a + b, 0];
|
|
23
23
|
const s = a + b;
|
|
24
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
25
|
+
if (!Number.isFinite(s))
|
|
26
|
+
return [s, 0];
|
|
24
27
|
const bb = s - a;
|
|
25
28
|
const e = a - (s - bb) + (b - bb);
|
|
26
29
|
return [s, e];
|
|
27
30
|
}
|
|
31
|
+
/** x 2^k, in two steps so that 2^k itself cannot overflow or underflow */
|
|
32
|
+
function scale2(x, k) {
|
|
33
|
+
const h = Math.trunc(k / 2);
|
|
34
|
+
return x * 2 ** h * 2 ** (k - h);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The power of 2 that brings a divisor below 1 to [1, 2) when the numerator is below 2^-900, 0 otherwise (or when the
|
|
38
|
+
* numerator would overflow). The remainders of the division, about |a| eps and |a| eps^2, fell below the subnormals
|
|
39
|
+
* there and the low part of the quotient was lost (ddDiv([MIN], [3 MIN]) gave [1/3, 0], ddDiv([1e-300], [3e-300]) a
|
|
40
|
+
* low part of 8.4e-20 for 1.85e-17; issue #105); the scaling by 2^k is exact and leaves the quotient unchanged
|
|
41
|
+
*/
|
|
42
|
+
function tinyDivisionShift(a, b) {
|
|
43
|
+
if (!(Math.abs(a) < 2 ** -900) || !(Math.abs(b) < 1) || b === 0)
|
|
44
|
+
return 0;
|
|
45
|
+
const k = -Math.floor(Math.log2(Math.abs(b)));
|
|
46
|
+
return Number.isFinite(scale2(a, k)) ? k : 0;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The rounding error a * b - fl(a * b) for operands so large that Veltkamp splitting overflows: each operand is
|
|
50
|
+
* scaled near 1 by a power of 2 (exact), and the error of the scaled product scaled back
|
|
51
|
+
*/
|
|
52
|
+
function prodErrScaled(a, b) {
|
|
53
|
+
const ka = a === 0 ? 0 : Math.floor(Math.log2(Math.abs(a)));
|
|
54
|
+
const kb = b === 0 ? 0 : Math.floor(Math.log2(Math.abs(b)));
|
|
55
|
+
const x = scale2(a, -ka);
|
|
56
|
+
const y = scale2(b, -kb);
|
|
57
|
+
const p = x * y;
|
|
58
|
+
let t = SPLITTER * x;
|
|
59
|
+
const xh = t - (t - x);
|
|
60
|
+
const xl = x - xh;
|
|
61
|
+
t = SPLITTER * y;
|
|
62
|
+
const yh = t - (t - y);
|
|
63
|
+
const yl = y - yh;
|
|
64
|
+
// scaled back by 2^(ka + kb) at once: 2^ka first underflowed the error of 1e-320 * 1e308 to 0 before 2^kb (#62)
|
|
65
|
+
return scale2(xh * yh - p + xh * yl + xl * yh + xl * yl, ka + kb);
|
|
66
|
+
}
|
|
28
67
|
/** [p, e] satisfying a * b = p + e exactly (Veltkamp splitting) */
|
|
29
68
|
export function twoProd(a, b) {
|
|
30
69
|
if (!ddEnabled)
|
|
31
70
|
return [a * b, 0];
|
|
32
71
|
const p = a * b;
|
|
72
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
73
|
+
if (!Number.isFinite(p))
|
|
74
|
+
return [p, 0];
|
|
33
75
|
let t = SPLITTER * a;
|
|
34
76
|
const ah = t - (t - a);
|
|
35
77
|
const al = a - ah;
|
|
36
78
|
t = SPLITTER * b;
|
|
37
79
|
const bh = t - (t - b);
|
|
38
80
|
const bl = b - bh;
|
|
39
|
-
|
|
81
|
+
let e = ah * bh - p + ah * bl + al * bh + al * bl;
|
|
82
|
+
if (!Number.isFinite(e) && Number.isFinite(p))
|
|
83
|
+
e = prodErrScaled(a, b);
|
|
40
84
|
return [p, e];
|
|
41
85
|
}
|
|
42
86
|
// The helpers below expand the form that calls twoSum, quickTwoSum and twoProd (Dekker / Knuth), keeping the same
|
|
@@ -48,6 +92,9 @@ export function ddAdd(a, b) {
|
|
|
48
92
|
if (!ddEnabled)
|
|
49
93
|
return [a[0] + b[0], 0];
|
|
50
94
|
const s1 = a[0] + b[0];
|
|
95
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
96
|
+
if (!Number.isFinite(s1))
|
|
97
|
+
return [s1, 0];
|
|
51
98
|
const bb2 = s1 - a[0];
|
|
52
99
|
const e3 = a[0] - (s1 - bb2) + (b[0] - bb2);
|
|
53
100
|
const s4 = a[1] + b[1];
|
|
@@ -70,6 +117,9 @@ export function ddSub(a, b) {
|
|
|
70
117
|
const n1 = -b[0];
|
|
71
118
|
const n2 = -b[1];
|
|
72
119
|
const s3 = a[0] + n1;
|
|
120
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
121
|
+
if (!Number.isFinite(s3))
|
|
122
|
+
return [s3, 0];
|
|
73
123
|
const bb4 = s3 - a[0];
|
|
74
124
|
const e5 = a[0] - (s3 - bb4) + (n1 - bb4);
|
|
75
125
|
const s6 = a[1] + n2;
|
|
@@ -87,13 +137,18 @@ export function ddMul(a, b) {
|
|
|
87
137
|
if (!ddEnabled)
|
|
88
138
|
return [a[0] * b[0], 0];
|
|
89
139
|
const p1 = a[0] * b[0];
|
|
140
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
141
|
+
if (!Number.isFinite(p1))
|
|
142
|
+
return [p1, 0];
|
|
90
143
|
const t2 = SPLITTER * a[0];
|
|
91
144
|
const ah3 = t2 - (t2 - a[0]);
|
|
92
145
|
const al4 = a[0] - ah3;
|
|
93
146
|
const t5 = SPLITTER * b[0];
|
|
94
147
|
const bh6 = t5 - (t5 - b[0]);
|
|
95
148
|
const bl7 = b[0] - bh6;
|
|
96
|
-
|
|
149
|
+
let e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
|
|
150
|
+
if (!Number.isFinite(e8) && Number.isFinite(p1))
|
|
151
|
+
e8 = prodErrScaled(a[0], b[0]);
|
|
97
152
|
const u9 = e8 + (a[0] * b[1] + a[1] * b[0]);
|
|
98
153
|
const s10 = p1 + u9;
|
|
99
154
|
const e11 = u9 - (s10 - p1);
|
|
@@ -103,13 +158,18 @@ export function ddMulNum(a, b) {
|
|
|
103
158
|
if (!ddEnabled)
|
|
104
159
|
return [a[0] * b, 0];
|
|
105
160
|
const p1 = a[0] * b;
|
|
161
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
162
|
+
if (!Number.isFinite(p1))
|
|
163
|
+
return [p1, 0];
|
|
106
164
|
const t2 = SPLITTER * a[0];
|
|
107
165
|
const ah3 = t2 - (t2 - a[0]);
|
|
108
166
|
const al4 = a[0] - ah3;
|
|
109
167
|
const t5 = SPLITTER * b;
|
|
110
168
|
const bh6 = t5 - (t5 - b);
|
|
111
169
|
const bl7 = b - bh6;
|
|
112
|
-
|
|
170
|
+
let e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
|
|
171
|
+
if (!Number.isFinite(e8) && Number.isFinite(p1))
|
|
172
|
+
e8 = prodErrScaled(a[0], b);
|
|
113
173
|
const u9 = e8 + a[1] * b;
|
|
114
174
|
const s10 = p1 + u9;
|
|
115
175
|
const e11 = u9 - (s10 - p1);
|
|
@@ -118,7 +178,14 @@ export function ddMulNum(a, b) {
|
|
|
118
178
|
export function ddDiv(a, b) {
|
|
119
179
|
if (!ddEnabled)
|
|
120
180
|
return [a[0] / b[0], 0];
|
|
181
|
+
// (written by hand, as the prodErrScaled lines: a tiny numerator over a divisor below 1, both times 2^k first)
|
|
182
|
+
const k = tinyDivisionShift(a[0], b[0]);
|
|
183
|
+
if (k !== 0)
|
|
184
|
+
return ddDiv([scale2(a[0], k), scale2(a[1], k)], [scale2(b[0], k), scale2(b[1], k)]);
|
|
121
185
|
const q1 = a[0] / b[0];
|
|
186
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
187
|
+
if (!Number.isFinite(q1))
|
|
188
|
+
return [q1, 0];
|
|
122
189
|
const p2 = b[0] * q1;
|
|
123
190
|
const t3 = SPLITTER * b[0];
|
|
124
191
|
const ah4 = t3 - (t3 - b[0]);
|
|
@@ -126,7 +193,9 @@ export function ddDiv(a, b) {
|
|
|
126
193
|
const t6 = SPLITTER * q1;
|
|
127
194
|
const bh7 = t6 - (t6 - q1);
|
|
128
195
|
const bl8 = q1 - bh7;
|
|
129
|
-
|
|
196
|
+
let e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
|
|
197
|
+
if (!Number.isFinite(e9) && Number.isFinite(p2))
|
|
198
|
+
e9 = prodErrScaled(b[0], q1);
|
|
130
199
|
const u10 = e9 + b[1] * q1;
|
|
131
200
|
const s11 = p2 + u10;
|
|
132
201
|
const e12 = u10 - (s11 - p2);
|
|
@@ -152,7 +221,9 @@ export function ddDiv(a, b) {
|
|
|
152
221
|
const t32 = SPLITTER * q27;
|
|
153
222
|
const bh33 = t32 - (t32 - q27);
|
|
154
223
|
const bl34 = q27 - bh33;
|
|
155
|
-
|
|
224
|
+
let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
|
|
225
|
+
if (!Number.isFinite(e35) && Number.isFinite(p28))
|
|
226
|
+
e35 = prodErrScaled(b[0], q27);
|
|
156
227
|
const u36 = e35 + b[1] * q27;
|
|
157
228
|
const s37 = p28 + u36;
|
|
158
229
|
const e38 = u36 - (s37 - p28);
|
|
@@ -190,7 +261,14 @@ export function ddDiv(a, b) {
|
|
|
190
261
|
export function ddDivNum(a, b) {
|
|
191
262
|
if (!ddEnabled)
|
|
192
263
|
return [a[0] / b, 0];
|
|
264
|
+
// (by hand, as in ddDiv)
|
|
265
|
+
const k = tinyDivisionShift(a[0], b);
|
|
266
|
+
if (k !== 0)
|
|
267
|
+
return ddDivNum([scale2(a[0], k), scale2(a[1], k)], scale2(b, k));
|
|
193
268
|
const q1 = a[0] / b;
|
|
269
|
+
// A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
|
|
270
|
+
if (!Number.isFinite(q1))
|
|
271
|
+
return [q1, 0];
|
|
194
272
|
const p2 = b * q1;
|
|
195
273
|
const t3 = SPLITTER * b;
|
|
196
274
|
const ah4 = t3 - (t3 - b);
|
|
@@ -198,7 +276,9 @@ export function ddDivNum(a, b) {
|
|
|
198
276
|
const t6 = SPLITTER * q1;
|
|
199
277
|
const bh7 = t6 - (t6 - q1);
|
|
200
278
|
const bl8 = q1 - bh7;
|
|
201
|
-
|
|
279
|
+
let e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
|
|
280
|
+
if (!Number.isFinite(e9) && Number.isFinite(p2))
|
|
281
|
+
e9 = prodErrScaled(b, q1);
|
|
202
282
|
const u10 = e9 + 0 * q1;
|
|
203
283
|
const s11 = p2 + u10;
|
|
204
284
|
const e12 = u10 - (s11 - p2);
|
|
@@ -224,7 +304,9 @@ export function ddDivNum(a, b) {
|
|
|
224
304
|
const t32 = SPLITTER * q27;
|
|
225
305
|
const bh33 = t32 - (t32 - q27);
|
|
226
306
|
const bl34 = q27 - bh33;
|
|
227
|
-
|
|
307
|
+
let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
|
|
308
|
+
if (!Number.isFinite(e35) && Number.isFinite(p28))
|
|
309
|
+
e35 = prodErrScaled(b, q27);
|
|
228
310
|
const u36 = e35 + 0 * q27;
|
|
229
311
|
const s37 = p28 + u36;
|
|
230
312
|
const e38 = u36 - (s37 - p28);
|
|
@@ -450,7 +532,9 @@ export function ddLog(x) {
|
|
|
450
532
|
const t32 = SPLITTER * q27;
|
|
451
533
|
const bh33 = t32 - (t32 - q27);
|
|
452
534
|
const bl34 = q27 - bh33;
|
|
453
|
-
|
|
535
|
+
let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
|
|
536
|
+
if (!Number.isFinite(e35) && Number.isFinite(p28))
|
|
537
|
+
e35 = prodErrScaled(s25, q27);
|
|
454
538
|
const u36 = e35 + e26 * q27;
|
|
455
539
|
const s37 = p28 + u36;
|
|
456
540
|
const e38 = u36 - (s37 - p28);
|
|
@@ -476,7 +560,9 @@ export function ddLog(x) {
|
|
|
476
560
|
const t58 = SPLITTER * q53;
|
|
477
561
|
const bh59 = t58 - (t58 - q53);
|
|
478
562
|
const bl60 = q53 - bh59;
|
|
479
|
-
|
|
563
|
+
let e61 = ah56 * bh59 - p54 + ah56 * bl60 + al57 * bh59 + al57 * bl60;
|
|
564
|
+
if (!Number.isFinite(e61) && Number.isFinite(p54))
|
|
565
|
+
e61 = prodErrScaled(s25, q53);
|
|
480
566
|
const u62 = e61 + e26 * q53;
|
|
481
567
|
const s63 = p54 + u62;
|
|
482
568
|
const e64 = u62 - (s63 - p54);
|
|
@@ -516,7 +602,9 @@ export function ddLog(x) {
|
|
|
516
602
|
const t98 = SPLITTER * s92;
|
|
517
603
|
const bh99 = t98 - (t98 - s92);
|
|
518
604
|
const bl100 = s92 - bh99;
|
|
519
|
-
|
|
605
|
+
let e101 = ah96 * bh99 - p94 + ah96 * bl100 + al97 * bh99 + al97 * bl100;
|
|
606
|
+
if (!Number.isFinite(e101) && Number.isFinite(p94))
|
|
607
|
+
e101 = prodErrScaled(s92, s92);
|
|
520
608
|
const u102 = e101 + (s92 * e93 + e93 * s92);
|
|
521
609
|
const s103 = p94 + u102;
|
|
522
610
|
const e104 = u102 - (s103 - p94);
|
|
@@ -527,7 +615,9 @@ export function ddLog(x) {
|
|
|
527
615
|
const t109 = SPLITTER * s103;
|
|
528
616
|
const bh110 = t109 - (t109 - s103);
|
|
529
617
|
const bl111 = s103 - bh110;
|
|
530
|
-
|
|
618
|
+
let e112 = ah107 * bh110 - p105 + ah107 * bl111 + al108 * bh110 + al108 * bl111;
|
|
619
|
+
if (!Number.isFinite(e112) && Number.isFinite(p105))
|
|
620
|
+
e112 = prodErrScaled(RECIP[5][0], s103);
|
|
531
621
|
const u113 = e112 + (RECIP[5][0] * e104 + RECIP[5][1] * s103);
|
|
532
622
|
const s114 = p105 + u113;
|
|
533
623
|
const e115 = u113 - (s114 - p105);
|
|
@@ -550,7 +640,9 @@ export function ddLog(x) {
|
|
|
550
640
|
const t132 = SPLITTER * s103;
|
|
551
641
|
const bh133 = t132 - (t132 - s103);
|
|
552
642
|
const bl134 = s103 - bh133;
|
|
553
|
-
|
|
643
|
+
let e135 = ah130 * bh133 - p128 + ah130 * bl134 + al131 * bh133 + al131 * bl134;
|
|
644
|
+
if (!Number.isFinite(e135) && Number.isFinite(p128))
|
|
645
|
+
e135 = prodErrScaled(s126, s103);
|
|
554
646
|
const u136 = e135 + (s126 * e104 + e127 * s103);
|
|
555
647
|
const s137 = p128 + u136;
|
|
556
648
|
const e138 = u136 - (s137 - p128);
|
|
@@ -573,7 +665,9 @@ export function ddLog(x) {
|
|
|
573
665
|
const t155 = SPLITTER * s103;
|
|
574
666
|
const bh156 = t155 - (t155 - s103);
|
|
575
667
|
const bl157 = s103 - bh156;
|
|
576
|
-
|
|
668
|
+
let e158 = ah153 * bh156 - p151 + ah153 * bl157 + al154 * bh156 + al154 * bl157;
|
|
669
|
+
if (!Number.isFinite(e158) && Number.isFinite(p151))
|
|
670
|
+
e158 = prodErrScaled(s149, s103);
|
|
577
671
|
const u159 = e158 + (s149 * e104 + e150 * s103);
|
|
578
672
|
const s160 = p151 + u159;
|
|
579
673
|
const e161 = u159 - (s160 - p151);
|
|
@@ -596,7 +690,9 @@ export function ddLog(x) {
|
|
|
596
690
|
const t178 = SPLITTER * s103;
|
|
597
691
|
const bh179 = t178 - (t178 - s103);
|
|
598
692
|
const bl180 = s103 - bh179;
|
|
599
|
-
|
|
693
|
+
let e181 = ah176 * bh179 - p174 + ah176 * bl180 + al177 * bh179 + al177 * bl180;
|
|
694
|
+
if (!Number.isFinite(e181) && Number.isFinite(p174))
|
|
695
|
+
e181 = prodErrScaled(s172, s103);
|
|
600
696
|
const u182 = e181 + (s172 * e104 + e173 * s103);
|
|
601
697
|
const s183 = p174 + u182;
|
|
602
698
|
const e184 = u182 - (s183 - p174);
|
|
@@ -619,7 +715,9 @@ export function ddLog(x) {
|
|
|
619
715
|
const t201 = SPLITTER * s103;
|
|
620
716
|
const bh202 = t201 - (t201 - s103);
|
|
621
717
|
const bl203 = s103 - bh202;
|
|
622
|
-
|
|
718
|
+
let e204 = ah199 * bh202 - p197 + ah199 * bl203 + al200 * bh202 + al200 * bl203;
|
|
719
|
+
if (!Number.isFinite(e204) && Number.isFinite(p197))
|
|
720
|
+
e204 = prodErrScaled(s195, s103);
|
|
623
721
|
const u205 = e204 + (s195 * e104 + e196 * s103);
|
|
624
722
|
const s206 = p197 + u205;
|
|
625
723
|
const e207 = u205 - (s206 - p197);
|
|
@@ -642,7 +740,9 @@ export function ddLog(x) {
|
|
|
642
740
|
const t224 = SPLITTER * s218;
|
|
643
741
|
const bh225 = t224 - (t224 - s218);
|
|
644
742
|
const bl226 = s218 - bh225;
|
|
645
|
-
|
|
743
|
+
let e227 = ah222 * bh225 - p220 + ah222 * bl226 + al223 * bh225 + al223 * bl226;
|
|
744
|
+
if (!Number.isFinite(e227) && Number.isFinite(p220))
|
|
745
|
+
e227 = prodErrScaled(s92, s218);
|
|
646
746
|
const u228 = e227 + (s92 * e219 + e93 * s218);
|
|
647
747
|
const s229 = p220 + u228;
|
|
648
748
|
const e230 = u228 - (s229 - p220);
|
|
@@ -653,7 +753,9 @@ export function ddLog(x) {
|
|
|
653
753
|
const t235 = SPLITTER * 2;
|
|
654
754
|
const bh236 = t235 - (t235 - 2);
|
|
655
755
|
const bl237 = 2 - bh236;
|
|
656
|
-
|
|
756
|
+
let e238 = ah233 * bh236 - p231 + ah233 * bl237 + al234 * bh236 + al234 * bl237;
|
|
757
|
+
if (!Number.isFinite(e238) && Number.isFinite(p231))
|
|
758
|
+
e238 = prodErrScaled(s229, 2);
|
|
657
759
|
const u239 = e238 + e230 * 2;
|
|
658
760
|
const s240 = p231 + u239;
|
|
659
761
|
const e241 = u239 - (s240 - p231);
|
|
@@ -676,7 +778,9 @@ export function ddLog(x) {
|
|
|
676
778
|
const t258 = SPLITTER * k;
|
|
677
779
|
const bh259 = t258 - (t258 - k);
|
|
678
780
|
const bl260 = k - bh259;
|
|
679
|
-
|
|
781
|
+
let e261 = ah256 * bh259 - p254 + ah256 * bl260 + al257 * bh259 + al257 * bl260;
|
|
782
|
+
if (!Number.isFinite(e261) && Number.isFinite(p254))
|
|
783
|
+
e261 = prodErrScaled(LN2[0], k);
|
|
680
784
|
const u262 = e261 + LN2[1] * k;
|
|
681
785
|
const s263 = p254 + u262;
|
|
682
786
|
const e264 = u262 - (s263 - p254);
|
package/dist/special/extra.js
CHANGED
|
@@ -38,6 +38,11 @@ export function gammainc(x, a) {
|
|
|
38
38
|
const lg = lgamma(a);
|
|
39
39
|
const g = gamma(a);
|
|
40
40
|
const tail = (lower) => {
|
|
41
|
+
const logP = gammaInc(a, x, lower, true);
|
|
42
|
+
// a tail that is exactly 0 (the upper at x = Infinity, the lower at x = 0): 0, also where Gamma(a) overflows
|
|
43
|
+
// (Infinity * 0 made gammainc(Infinity, 1e308)$upper NaN)
|
|
44
|
+
if (logP === -Infinity)
|
|
45
|
+
return [0, -Infinity];
|
|
41
46
|
const p = gammaInc(a, x, lower, false);
|
|
42
47
|
const v = g * p;
|
|
43
48
|
// the product where it is a normal number (then also its log: lgamma(a) + log P can cancel)
|
|
@@ -86,6 +91,9 @@ function smallTail(x, a, lower) {
|
|
|
86
91
|
export function erfcinv(z) {
|
|
87
92
|
if (Number.isNaN(z) || z < 0 || z > 2)
|
|
88
93
|
return NaN;
|
|
94
|
+
// Below 2^-1021, z / 2 is not exact (Number.MIN_VALUE / 2 is 0): the log probability instead
|
|
95
|
+
if (z < 2 ** -1021)
|
|
96
|
+
return -qnorm(Math.log(z) - Math.LN2, { logP: true }) * Math.SQRT1_2;
|
|
89
97
|
return -qnorm(z / 2) * Math.SQRT1_2;
|
|
90
98
|
}
|
|
91
99
|
/**
|
|
@@ -136,7 +144,11 @@ export function perm(n, k) {
|
|
|
136
144
|
const intK = k === Math.floor(k);
|
|
137
145
|
if (n === Math.floor(n) && intK && k > n)
|
|
138
146
|
return 0;
|
|
139
|
-
|
|
147
|
+
// the limit n -> Infinity of a product ~ n^k for a fixed finite k (lgamma would give Infinity - Infinity);
|
|
148
|
+
// both infinite is indeterminate
|
|
149
|
+
if (n === Infinity)
|
|
150
|
+
return k === Infinity ? NaN : k === 0 ? 1 : Infinity;
|
|
151
|
+
if (!Number.isFinite(k))
|
|
140
152
|
return permLgamma(n, k);
|
|
141
153
|
if (intK && k <= 1000)
|
|
142
154
|
return fallingFactorial(n, k);
|
|
@@ -228,7 +240,7 @@ function lgammaDD(z) {
|
|
|
228
240
|
const e = ddSub(ddMul(twoSum(z, -0.5), ddLog([z, 0])), [z, 0]);
|
|
229
241
|
return ddAdd(ddAdd(e, LS2PI_DD), [stirlerr(z), 0]);
|
|
230
242
|
}
|
|
231
|
-
/** Gamma(n + 1) / Gamma(n - k + 1) from lgamma (
|
|
243
|
+
/** Gamma(n + 1) / Gamma(n - k + 1) from lgamma (finite n, infinite k) */
|
|
232
244
|
function permLgamma(n, k) {
|
|
233
245
|
const num = lgammaSign(n + 1);
|
|
234
246
|
const den = lgammaSign(n - k + 1);
|
package/dist/special/gamma.js
CHANGED
|
@@ -41,7 +41,8 @@ export function sinpi(x) {
|
|
|
41
41
|
* Replaces xsf's Stirling approximation (polynomial STIR), which has a relative error of 7e-14 at x = 170.
|
|
42
42
|
*/
|
|
43
43
|
function stirf(x) {
|
|
44
|
-
|
|
44
|
+
// MAXGAM rounds to the largest double whose Gamma is finite, so only beyond it is the result Infinity
|
|
45
|
+
if (x > MAXGAM)
|
|
45
46
|
return Infinity;
|
|
46
47
|
const n = x - 1;
|
|
47
48
|
const e = ddAdd(ddMulNum(ddLog([n, 0]), n), [-n + stirlerrForGamma(n), 0]);
|
|
@@ -97,7 +98,15 @@ export function gamma(x) {
|
|
|
97
98
|
z = q * sinpi(z);
|
|
98
99
|
if (z === 0)
|
|
99
100
|
return sgngam * Infinity;
|
|
100
|
-
|
|
101
|
+
const denom = Math.abs(z) * stirf(q);
|
|
102
|
+
if (Number.isFinite(denom))
|
|
103
|
+
return (sgngam * Math.PI) / denom;
|
|
104
|
+
// The denominator overflows (q beyond 171.6, or near it) while the result is a subnormal down to q = 177.5:
|
|
105
|
+
// Gamma(q) = Gamma(q - 10) (q - 1) ... (q - 10), with q - 10 exact, and the result rounded once at the end
|
|
106
|
+
let prod = 1;
|
|
107
|
+
for (let i = 1; i <= 10; i++)
|
|
108
|
+
prod *= q - i;
|
|
109
|
+
return (sgngam * Math.PI) / (Math.abs(z) * stirf(q - 10)) / prod;
|
|
101
110
|
}
|
|
102
111
|
return stirf(x);
|
|
103
112
|
}
|
package/dist/special/incbet.js
CHANGED
|
@@ -18,7 +18,7 @@ import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, isDoubleDouble, SPLITTER,
|
|
|
18
18
|
import { lgamma1p } from "./gamma.js";
|
|
19
19
|
import { lgammaDiffDD } from "./lgammaDiff.js";
|
|
20
20
|
import { HUGE_SHAPE, HUGE_SHAPE_MAX_SMALL, lgammaRatioHuge, mulBig, prefactorParts, SMALL_SHAPE, smallShapeG, } from "./incbetPrefactor.js";
|
|
21
|
-
import { betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
|
|
21
|
+
import { betaIncOverflowSum, betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
|
|
22
22
|
import { betaIncLargeA, LARGE_A_MIN, SMALL_B_MAX } from "./incbetLargeA.js";
|
|
23
23
|
const BIG = 4.503599627370496e15;
|
|
24
24
|
const BIGINV = 2.22044604925031308085e-16;
|
|
@@ -749,9 +749,9 @@ function pseriesOk(a, b, x) {
|
|
|
749
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
750
|
*/
|
|
751
751
|
function cfOk(a, b, x, xc) {
|
|
752
|
+
// (a/(a + b) as 1/(1 + b/a) and b/(a + b) as 1/(1 + a/b) where a + b overflows: the ratio was 0, below every x; #491)
|
|
752
753
|
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)
|
|
754
|
+
return x[0] <= (a + b < Infinity ? a / (a + b) : 1 / (1 + b / a));
|
|
755
755
|
return xc[0] >= (a + b < Infinity ? b / (a + b) : 1 / (1 + a / b));
|
|
756
756
|
}
|
|
757
757
|
/** Compute I_x(a, b) by continued fraction */
|
|
@@ -869,7 +869,9 @@ export function betaIncTinyX(a, b, x, lx, lower, wantLog) {
|
|
|
869
869
|
* pbeta(0.615, 2, 2.7e-158, { lowerTail: false }) was 1.0000000000000002), so it is capped at 1 (log at 0)
|
|
870
870
|
*/
|
|
871
871
|
function betaIncCore(a, b, xd, xcd, lower, wantLog) {
|
|
872
|
-
|
|
872
|
+
let r = betaIncUncapped(a, b, xd, xcd, lower, wantLog);
|
|
873
|
+
if (Number.isNaN(r[1]) || (wantLog && Number.isNaN(r[0])))
|
|
874
|
+
r = betaIncOverflowSum(a, b, xd, xcd, lower, wantLog) ?? r;
|
|
873
875
|
return r[1] > 1 || r[0] > 0 ? [Math.min(r[0], 0), Math.min(r[1], 1)] : r;
|
|
874
876
|
}
|
|
875
877
|
/**
|
|
@@ -889,11 +891,35 @@ function subShapeTail(t, s, small) {
|
|
|
889
891
|
return [t[0] + Math.log(r), v];
|
|
890
892
|
return [Math.log1p(-v), 1 - v];
|
|
891
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* [log, linear] for both shapes below SUB_SHAPE: I_x(a, b) = b/(a + b) and 1 - I_x(a, b) = a/(a + b) to double precision
|
|
896
|
+
* for every x in (0, 1). From the series, I_x(a, b) = x^a b/(a + b) Γ(a+b+1)/(Γ(a+1) Γ(b+1)) (1 + a S) with abs(S) at most
|
|
897
|
+
* about -log(1 - x) (below 37 for a double x below 1) and abs(a log x) at most 745 a, so the relative error is below
|
|
898
|
+
* 1e-297; the upper tail likewise from I_{1-x}(b, a). The series overflowed in 1/a and the prefactor a b/(a + b) underflowed
|
|
899
|
+
* to 0 there (pbeta(0.5, 5e-324, 5e-324) was NaN, pbeta(0.5, 1e-322, 1e-315) -Infinity). R (toms708) takes the same limit
|
|
900
|
+
* below 1e-3 ε. The shapes are scaled exactly by 2^1000 into the normal range and a + b is kept in double-double (a = b
|
|
901
|
+
* gives 1/2 exactly)
|
|
902
|
+
*/
|
|
903
|
+
function bothSubShape(a, b, lower, wantLog) {
|
|
904
|
+
const sa = a * 2 ** 1000;
|
|
905
|
+
const sb = b * 2 ** 1000;
|
|
906
|
+
const sum = twoSum(sa, sb);
|
|
907
|
+
const ratio = (u) => {
|
|
908
|
+
const r = ddDiv([u, 0], sum);
|
|
909
|
+
return r[0] + r[1];
|
|
910
|
+
};
|
|
911
|
+
const v = ratio(lower ? sb : sa);
|
|
912
|
+
if (!wantLog)
|
|
913
|
+
return [NaN, v];
|
|
914
|
+
return [v > 0.5 ? Math.log1p(-ratio(lower ? sa : sb)) : Math.log(v), v];
|
|
915
|
+
}
|
|
892
916
|
function betaIncUncapped(a, b, xd, xcd, lower, wantLog) {
|
|
893
917
|
if (xd[0] <= 0)
|
|
894
918
|
return lower ? [-Infinity, 0] : [0, 1];
|
|
895
919
|
if (xcd[0] <= 0)
|
|
896
920
|
return lower ? [0, 1] : [-Infinity, 0];
|
|
921
|
+
if (a < SUB_SHAPE && b < SUB_SHAPE)
|
|
922
|
+
return bothSubShape(a, b, lower, wantLog);
|
|
897
923
|
// 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
924
|
// that at SUB_SHAPE scaled (see there), and the other tail as 1 minus it
|
|
899
925
|
if (a < SUB_SHAPE && b > SUB_OTHER)
|
|
@@ -17,3 +17,10 @@ export declare function halfErfc(y: number): number;
|
|
|
17
17
|
* used (m >= TEMME_MIN_M and |τ| <= TAU_MAX); the caller then uses the series or continued fractions
|
|
18
18
|
*/
|
|
19
19
|
export declare function betaIncTemme(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Fallback where a + b overflows and the other methods gave NaN (the continued fraction and the prefactor form
|
|
22
|
+
* a + b; pbeta(0.1, 1e308, 1e308) was NaN; #491). The smaller tail is e^-E times a factor whose log is of the order of
|
|
23
|
+
* the logs of the shapes (below about 2000 in size), so where E >= 1e22 its log is -E to double precision and its
|
|
24
|
+
* linear value 0. E in the halved shapes, doubled, as in betaIncTemme. Undefined where a + b is finite or E < 1e22
|
|
25
|
+
*/
|
|
26
|
+
export declare function betaIncOverflowSum(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;
|