@numero-uno-jpn/rstats 0.15.2 → 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 +280 -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-PWVQVJTP.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 +35 -5
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +87 -8
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +11 -1
- package/dist/distributions/f.js +73 -22
- package/dist/distributions/gamma.js +61 -6
- package/dist/distributions/geometric.js +53 -14
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +6 -1
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +31 -7
- package/dist/distributions/logistic.js +11 -2
- package/dist/distributions/lognormal.js +20 -3
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +128 -35
- package/dist/distributions/nchisq.js +85 -14
- package/dist/distributions/negbinomial.js +100 -18
- package/dist/distributions/normal.js +35 -5
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +536 -35
- package/dist/distributions/pareto.js +9 -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/t.js +55 -3
- package/dist/distributions/triangular.js +45 -13
- package/dist/distributions/tukey.js +21 -2
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +33 -3
- 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 +23 -1
- package/dist/internal/ddexp.js +86 -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.d.ts +5 -0
- package/dist/internal/invert.js +25 -7
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- 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 +11 -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 +15 -2
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +100 -10
- package/dist/special/incbetLargeA.js +7 -2
- package/dist/special/incbetPrefactor.d.ts +5 -0
- package/dist/special/incbetPrefactor.js +14 -3
- 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 +179 -33
- 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-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-3WQCKGKP.js +0 -2
- package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
- package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-HBMHR5SS.js +0 -2
- package/dist/chunks/chunk-IBPWRQIF.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-LG74QY5U.js +0 -2
- package/dist/chunks/chunk-PEYGUK32.js +0 -2
- package/dist/chunks/chunk-Q4PGYESP.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-SREPSVTT.js +0 -2
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
- package/dist/chunks/chunk-VWJ74UHC.js +0 -2
- package/dist/chunks/chunk-XNPU54NH.js +0 -2
- package/dist/chunks/chunk-XPLDEK6D.js +0 -2
- package/dist/chunks/chunk-YGT4P7EU.js +0 -2
- package/dist/chunks/chunk-YVREYVI2.js +0 -2
package/dist/distributions/f.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
//
|
|
3
3
|
// The density uses the relation to the binomial probability. With p = m x/(n + m x) and q = n/(n + m x),
|
|
4
4
|
// m >= 2: f(x) = m q / 2 * dbinom((m-2)/2; (m+n-2)/2, p)
|
|
5
|
-
//
|
|
6
|
-
//
|
|
5
|
+
// (derived by rewriting the F density in terms of Gamma functions); m < 2 (or n < 2) is computed from the log of the
|
|
6
|
+
// density with the small-shape prefactor (fLogDensity).
|
|
7
7
|
// The distribution function is P[F <= x] = I_p(m/2, n/2); quantiles use Newton's method in u = log x.
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
@@ -16,7 +16,8 @@ import { ddAdd, ddDiv, ddLog, ddMul, ddNeg, ddSub, twoProd, twoSum } from "../sp
|
|
|
16
16
|
import { dgamma1, pgamma1, qgamma1 } from "./gamma.js";
|
|
17
17
|
import { dnf1, pnf1, qnf1 } from "./nbeta.js";
|
|
18
18
|
import { betaQuantileGuess } from "./beta.js";
|
|
19
|
-
const
|
|
19
|
+
const DBL_MIN = 2.2250738585072014e-308;
|
|
20
|
+
const LOG_DBL_MIN = Math.log(DBL_MIN);
|
|
20
21
|
/**
|
|
21
22
|
* Log density in double-double from log p and log q (p = m x/(n + m x), q = 1 - p = n/(n + m x)), with a = m/2, b = n/2:
|
|
22
23
|
* f(x) = (m/n) p^(a-1) q^(b+1) / B(a, b) (the beta density of p times dp/dx = (m/n) q^2)
|
|
@@ -42,12 +43,24 @@ function fromLog(e, log) {
|
|
|
42
43
|
* DBL_MIN and the callers work from its log)
|
|
43
44
|
*/
|
|
44
45
|
function fRatios(x, m, n) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// A factor above 1e290 is scaled down by 2^600 and the other up (exactly), as its splitting in twoProd overflows: at a
|
|
47
|
+
// subnormal df1 and x from 1e301, m x is moderate but was taken for huge, and n/m overflowed in the callers' q ≈ n/(m x)
|
|
48
|
+
// (pf(1e301, 1.4e-312, 6.72, { lowerTail: false }) was 1, 1.77e-311 expected; qf there did not return)
|
|
49
|
+
const S = 2 ** 600;
|
|
50
|
+
let mx = x > 1e290 ? twoProd(m * S, x / S) : m > 1e290 ? twoProd(m / S, x * S) : twoProd(m, x);
|
|
51
|
+
if (!(Number.isFinite(mx[0]) && Number.isFinite(mx[1]) && Math.abs(mx[0]) < 1e290)) {
|
|
52
|
+
// m x and n both divided by 2^600 (exactly): a huge m x is not always far above n, and the callers' p ≈ 1 gave
|
|
53
|
+
// pf(1e150, 1e150, 1e308) the wrong tail (lower -Inf for 0 in logs; issue #116). Where m x is still huge or n falls
|
|
54
|
+
// below 1e-290 there, m x is far above n and the callers' p ≈ 1, q ≈ n/(m x) holds
|
|
55
|
+
const mxS = twoProd(Math.max(m, x) / S, Math.min(m, x));
|
|
56
|
+
const nS = n / S;
|
|
57
|
+
if (!(Number.isFinite(mxS[0]) && Number.isFinite(mxS[1]) && Math.abs(mxS[0]) < 1e290) || !(nS >= 1e-290))
|
|
58
|
+
return undefined;
|
|
59
|
+
const dS = ddAdd(mxS, [nS, 0]);
|
|
60
|
+
return [ddDiv(mxS, dS), ddDiv([nS, 0], dS)];
|
|
61
|
+
}
|
|
48
62
|
let nn = n;
|
|
49
63
|
if (x > 0 && mx[0] < 1e-290 && n < 1e127) {
|
|
50
|
-
const S = 2 ** 600;
|
|
51
64
|
mx = m < x ? twoProd(m * S, x) : twoProd(m, x * S);
|
|
52
65
|
nn = n * S;
|
|
53
66
|
}
|
|
@@ -80,15 +93,11 @@ function fDensity(x, m, n, log) {
|
|
|
80
93
|
// below underflowed (df(6.6e-112, 3.2e-173, 1868) was -Infinity on the log scale, NaN at x = 4.6e-157)
|
|
81
94
|
if (Math.min(m, n) / 2 < SMALL_SHAPE)
|
|
82
95
|
return fromLog(fLogDensity(m, n, ddLog(pd), ddLog(qd)), log);
|
|
83
|
-
|
|
96
|
+
// (m >= 2 here: SMALL_SHAPE is 1, so df1 < 2 took the branch above; R's form for df1 < 2, whose 2 (m + n) overflowed
|
|
97
|
+
// for df2 near 1e308, was never reached and is gone; issue #32)
|
|
84
98
|
const q = qd[0];
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const v = dbinomRawDD((m - 2) / 2, n / 2, pd, qd, log);
|
|
88
|
-
return log ? Math.log(c) + v : c * v;
|
|
89
|
-
}
|
|
90
|
-
const c = ((m / (n + m * x)) * m * n) / (2 * (m + n) * p);
|
|
91
|
-
const v = dbinomRawDD(m / 2, n / 2, pd, qd, log);
|
|
99
|
+
const c = (m * q) / 2;
|
|
100
|
+
const v = dbinomRawDD((m - 2) / 2, n / 2, pd, qd, log);
|
|
92
101
|
return log ? Math.log(c) + v : c * v;
|
|
93
102
|
}
|
|
94
103
|
export function dfDensity1(x, m, n, log) {
|
|
@@ -107,16 +116,25 @@ export function dfDensity1(x, m, n, log) {
|
|
|
107
116
|
}
|
|
108
117
|
if (!Number.isFinite(m) && !Number.isFinite(n))
|
|
109
118
|
return x === 1 ? Infinity : d0(log);
|
|
119
|
+
// The density tends to 0 as x -> Inf for every df1 and df2 (x^(-df2/2 - 1)). R gives NaN for df1 < 2 and for
|
|
120
|
+
// df1 = Inf with df2 < 2 (Inf / Inf in its forms; #493)
|
|
121
|
+
if (x === Infinity)
|
|
122
|
+
return d0(log);
|
|
110
123
|
if (!Number.isFinite(n))
|
|
111
124
|
return dgamma1(x, m / 2, 2 / m, log); // χ²_m / m
|
|
112
125
|
if (!Number.isFinite(m)) {
|
|
113
126
|
// n / χ²_n: 1/x follows a gamma distribution with shape n/2, scale 2/n
|
|
114
|
-
// At tiny x this is 0 / 0 = NaN (x^2 underflows), as R gives (df(1e-300, Inf, 5) is NaN); the log is finite
|
|
115
127
|
const dens = dgamma1(1 / x, n / 2, 2 / n, log);
|
|
116
|
-
|
|
128
|
+
if (log)
|
|
129
|
+
return dens - 2 * Math.log(x);
|
|
130
|
+
// Where the gamma density or x^2 leaves the normal range, the quotient is 0 / 0 = NaN (df(1e-300, Inf, 5); R the
|
|
131
|
+
// same), or 0 where the density underflows or x^2 overflows (df(1e160, Inf, 1) is 3.99e-241; R 0): from the log
|
|
132
|
+
// (#493). Elsewhere the quotient and its bits are kept
|
|
133
|
+
const x2 = x * x;
|
|
134
|
+
if (dens >= DBL_MIN && dens < Infinity && x2 >= DBL_MIN && x2 < Infinity)
|
|
135
|
+
return dens / x2;
|
|
136
|
+
return Math.exp(dgamma1(1 / x, n / 2, 2 / n, true) - 2 * Math.log(x));
|
|
117
137
|
}
|
|
118
|
-
if (!Number.isFinite(x))
|
|
119
|
-
return m < 2 ? NaN : d0(log); // R gives NaN at x = Inf when df1 < 2
|
|
120
138
|
return fDensity(x, m, n, log);
|
|
121
139
|
}
|
|
122
140
|
/**
|
|
@@ -138,8 +156,10 @@ function pfTiny(x, m, n, lower, wantLog) {
|
|
|
138
156
|
return betaIncTinyX(n / 2, m / 2, Math.exp(-lr[0]), ddNeg(lr), !lower, wantLog);
|
|
139
157
|
return undefined;
|
|
140
158
|
}
|
|
141
|
-
/** P[F <= x] (lower) / P[F > x] (!lower). x
|
|
159
|
+
/** P[F <= x] (lower) / P[F > x] (!lower). x positive, m, n finite */
|
|
142
160
|
function pfRaw(x, m, n, lower, log) {
|
|
161
|
+
if (x === Infinity)
|
|
162
|
+
return lower === log ? 0 : lower ? 1 : -Infinity;
|
|
143
163
|
const t = pfTiny(x, m, n, lower, log);
|
|
144
164
|
if (t !== undefined)
|
|
145
165
|
return log ? t[0] : t[1];
|
|
@@ -161,6 +181,10 @@ function fQuantileGuess(m, n, lower, logTarget) {
|
|
|
161
181
|
}
|
|
162
182
|
/** [log, linear] of pfRaw (each value identical to pfRaw) */
|
|
163
183
|
function pfRawBoth(x, m, n, lower) {
|
|
184
|
+
// (x = Infinity, which the quantile search reaches from a far step, was NaN, and the search spent its iterations on it
|
|
185
|
+
// at 25 ms each: qf(-735.2, 1.25e-305, 3.3e5, { lowerTail: false, logP: true }) returned 1 after 5 s)
|
|
186
|
+
if (x === Infinity)
|
|
187
|
+
return lower ? [0, 1] : [-Infinity, 0];
|
|
164
188
|
const t = pfTiny(x, m, n, lower, true);
|
|
165
189
|
if (t !== undefined)
|
|
166
190
|
return t;
|
|
@@ -170,6 +194,26 @@ function pfRawBoth(x, m, n, lower) {
|
|
|
170
194
|
const zc = n / m / x;
|
|
171
195
|
return betaIncBoth(m / 2, n / 2, 1 - zc, zc, lower);
|
|
172
196
|
}
|
|
197
|
+
const MIN_NORMAL = 2.2250738585072014e-308;
|
|
198
|
+
/**
|
|
199
|
+
* P[χ²_m / m <= q] (lower), the F distribution with df2 = Inf: pgamma at q m, or with scale 2/m where q m is below the
|
|
200
|
+
* normal range (the product had lost its digits or underflowed to 0: pf(2e-323, 0.012, Inf) was 0, as R 4.5.2 gives,
|
|
201
|
+
* where it is 0.011; so the quantile 0 of qf(7.8e-37, 0.012, Inf) did not round-trip). Not for a subnormal m, where 2/m
|
|
202
|
+
* overflows
|
|
203
|
+
*/
|
|
204
|
+
function pChisqLimit(q, m, lowerTail, logP) {
|
|
205
|
+
const t = q * m;
|
|
206
|
+
if (t < MIN_NORMAL && m > MIN_NORMAL)
|
|
207
|
+
return pgamma1(q, m / 2, 2 / m, lowerTail, logP);
|
|
208
|
+
return pgamma1(t, m / 2, 2, lowerTail, logP);
|
|
209
|
+
}
|
|
210
|
+
/** The quantile of χ²_m / m, likewise with scale 2/m where the quantile or that of χ²_m is below the normal range */
|
|
211
|
+
function qChisqLimit(p, m, lowerTail, logP) {
|
|
212
|
+
const t = qgamma1(p, m / 2, 2, lowerTail, logP);
|
|
213
|
+
if ((t < MIN_NORMAL || t / m < MIN_NORMAL) && m > MIN_NORMAL)
|
|
214
|
+
return qgamma1(p, m / 2, 2 / m, lowerTail, logP);
|
|
215
|
+
return t / m;
|
|
216
|
+
}
|
|
173
217
|
export function pf1(q, m, n, lowerTail, logP) {
|
|
174
218
|
if (Number.isNaN(q) || Number.isNaN(m) || Number.isNaN(n))
|
|
175
219
|
return NaN;
|
|
@@ -186,7 +230,7 @@ export function pf1(q, m, n, lowerTail, logP) {
|
|
|
186
230
|
return q < 1 ? dt0(lowerTail, logP) : dt1(lowerTail, logP);
|
|
187
231
|
}
|
|
188
232
|
if (!Number.isFinite(n))
|
|
189
|
-
return
|
|
233
|
+
return pChisqLimit(q, m, lowerTail, logP); // χ²_m
|
|
190
234
|
if (!Number.isFinite(m))
|
|
191
235
|
return pgamma1(n / q, n / 2, 2, !lowerTail, logP); // n / χ²_n
|
|
192
236
|
return pfRaw(q, m, n, lowerTail, logP);
|
|
@@ -202,10 +246,17 @@ export function qf1(p, m, n, lowerTail, logP) {
|
|
|
202
246
|
if (!Number.isFinite(m) && !Number.isFinite(n))
|
|
203
247
|
return 1;
|
|
204
248
|
if (!Number.isFinite(n))
|
|
205
|
-
return
|
|
249
|
+
return qChisqLimit(p, m, lowerTail, logP);
|
|
206
250
|
if (!Number.isFinite(m))
|
|
207
251
|
return n / qgamma1(p, n / 2, 2, !lowerTail, logP);
|
|
208
252
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
253
|
+
// A df2 so large that χ²_n/n is 1 on the scale of the quantile: the χ² limit, as for n = Inf. With W = χ²_n/n and G
|
|
254
|
+
// the distribution function of χ²_m, P[F <= x] = E G(m x W) moves from G(m x) by Var W (m x)² G''/2 = (m x)² G''/n,
|
|
255
|
+
// and (m x)² G''/G is of the order of (d log G/d log t)², at most (m + 2 |log p| + 1e3)² (p the smaller tail): the
|
|
256
|
+
// quantile moves by less than 1e-20 relative. (The incomplete beta function, out of its range at such a df (see the
|
|
257
|
+
// known violations), was NaN in the bulk, and the search took seconds: qf(1.9e-13, 0.0885, 6e302) did not return)
|
|
258
|
+
if (n > 1e20 * (m - 2 * logTarget + 1e3) ** 2)
|
|
259
|
+
return qChisqLimit(p, m, lowerTail, logP);
|
|
209
260
|
// Near 0, P[F <= x] ≈ (m x / n)^(m/2) / ((m/2) B(m/2, n/2)). The answer is 0 if it underflows
|
|
210
261
|
if (lower && (logTarget + Math.log(m / 2) + lbeta(m / 2, n / 2)) / (m / 2) + Math.log(n / m) < -746)
|
|
211
262
|
return 0;
|
|
@@ -22,18 +22,23 @@ import { qnorm } from "./normal.js";
|
|
|
22
22
|
export function gammaScale(params) {
|
|
23
23
|
const { rate, scale } = params;
|
|
24
24
|
if (rate !== undefined && scale !== undefined) {
|
|
25
|
-
|
|
25
|
+
// (as R: a product that is NaN, from rate = NaN or Inf * 0, is an error too; it passed with >=)
|
|
26
|
+
if (!(Math.abs(rate * scale - 1) < 1e-15)) {
|
|
26
27
|
throw new RangeError("rate and scale can both be given only when scale = 1 / rate");
|
|
27
28
|
}
|
|
28
29
|
return scale;
|
|
29
30
|
}
|
|
30
31
|
if (scale !== undefined)
|
|
31
32
|
return scale;
|
|
33
|
+
// For a rate below about 5.6e-309 (subnormal), 1 / rate overflows to Infinity and the whole distribution is treated as
|
|
34
|
+
// lying at +Infinity (pgamma 0 and dgamma 0 at every finite x, qgamma Infinity), as R does with its scale = 1 / rate;
|
|
35
|
+
// some quantiles of the exact distribution would be finite there
|
|
32
36
|
if (rate !== undefined)
|
|
33
37
|
return 1 / rate;
|
|
34
38
|
return 1;
|
|
35
39
|
}
|
|
36
40
|
const MIN_NORMAL = 2 ** -1022;
|
|
41
|
+
const EULER = 0.5772156649015329;
|
|
37
42
|
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
38
43
|
/** c t in double-double; only the double product where c is beyond the range of Veltkamp splitting */
|
|
39
44
|
const mulBig = (t, c) => (c < 1e300 ? ddMulNum(t, c) : [t[0] * c, 0]);
|
|
@@ -44,7 +49,13 @@ const expOf = (l) => (l[0] < -746 ? 0 : ddExp(l)[0]);
|
|
|
44
49
|
* lgamma(shape + 1), with log y from log x - log scale (y itself has lost digits in the division, or is 0)
|
|
45
50
|
*/
|
|
46
51
|
function logLowerTiny(x, shape, scale) {
|
|
47
|
-
|
|
52
|
+
const m = mulBig(logRatioDD(x, scale), shape);
|
|
53
|
+
const lg = lgamma1p(shape);
|
|
54
|
+
// (both terms are negative; where one overflows the sum is -Infinity, which a double-double sum made NaN:
|
|
55
|
+
// pchisq(1e-320, 1e306) was NaN)
|
|
56
|
+
if (!Number.isFinite(m[0]) || !Number.isFinite(lg))
|
|
57
|
+
return [m[0] - lg, 0];
|
|
58
|
+
return ddAdd(m, [-lg, 0]);
|
|
48
59
|
}
|
|
49
60
|
/**
|
|
50
61
|
* Whether the log density l = lp + (log of a divisor) has cancelled to below half of |lp|: the roundings of the two
|
|
@@ -86,7 +97,11 @@ export function dgamma1(x, shape, scale, log) {
|
|
|
86
97
|
}
|
|
87
98
|
if (x / scale < MIN_NORMAL && scale < Infinity && shape < Infinity) {
|
|
88
99
|
// log f = shape log y + log(shape) - lgamma(shape + 1) - log x (e^-y is 1)
|
|
89
|
-
const
|
|
100
|
+
const lp = logLowerTiny(x, shape, scale);
|
|
101
|
+
// (-Infinity where shape log y or lgamma(shape + 1) overflows: dgamma(3.1e-272, 4.4e307, scale 4.1e138) was NaN)
|
|
102
|
+
if (lp[0] === -Infinity)
|
|
103
|
+
return d0(log);
|
|
104
|
+
const l = ddAdd(ddSub(lp, ddLog([x, 0])), ddLog([shape, 0]));
|
|
90
105
|
return log ? l[0] + l[1] : expOf(l);
|
|
91
106
|
}
|
|
92
107
|
if (shape < 1) {
|
|
@@ -116,6 +131,10 @@ export function pgamma1(q, shape, scale, lowerTail, logP) {
|
|
|
116
131
|
return NaN;
|
|
117
132
|
if (shape < 0 || scale <= 0)
|
|
118
133
|
return NaN;
|
|
134
|
+
// P[X <= Inf] = 1 also at scale = Inf (from a subnormal rate, 1/rate overflows), where q / scale is NaN. R 4.5.2
|
|
135
|
+
// gives NaN there (pgamma(Inf, 892.4, rate = 2.8e-316))
|
|
136
|
+
if (q === Infinity)
|
|
137
|
+
return dt1(lowerTail, logP);
|
|
119
138
|
const x = q / scale;
|
|
120
139
|
if (Number.isNaN(x))
|
|
121
140
|
return x;
|
|
@@ -133,6 +152,13 @@ export function pgamma1(q, shape, scale, lowerTail, logP) {
|
|
|
133
152
|
if (lowerTail)
|
|
134
153
|
return logP ? lp : expOf(l);
|
|
135
154
|
// 1 - P with P from the double-double log where P < 1/2 (exp of the rounded log loses eps |log P| relative)
|
|
155
|
+
if (lp > -1e-10 && logP) {
|
|
156
|
+
// log Q = log(-lp) + log1p(lp / 2) with -lp = shape (-m), m = log y - lgamma1p(shape) / shape, from log shape: Q is
|
|
157
|
+
// of the order of shape and lost digits where it is subnormal (pgamma(1e-310, 5e-324, upper, log) was 3e-4 off).
|
|
158
|
+
// lgamma1p(shape) / shape is -γ (1 + O(shape)) where lgamma1p(shape) would be subnormal
|
|
159
|
+
const m = logRatioDD(q, scale)[0] - (shape < 1e-300 ? -EULER : lgamma1p(shape) / shape);
|
|
160
|
+
return Math.log(shape) + Math.log(-m) + Math.log1p(lp / 2);
|
|
161
|
+
}
|
|
136
162
|
if (lp > -Math.LN2)
|
|
137
163
|
return logP ? Math.log(-Math.expm1(lp)) : -Math.expm1(lp);
|
|
138
164
|
const e = expOf(l);
|
|
@@ -140,8 +166,22 @@ export function pgamma1(q, shape, scale, lowerTail, logP) {
|
|
|
140
166
|
}
|
|
141
167
|
if (x <= 0)
|
|
142
168
|
return dt0(lowerTail, logP);
|
|
169
|
+
if (x === Infinity && !lowerTail && logP && scale < Infinity && overflowLogUpperFinite(q, shape, scale))
|
|
170
|
+
return NaN;
|
|
143
171
|
return gammaInc(shape, x, lowerTail, logP);
|
|
144
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Whether log Q(shape, q / scale) may be finite although q / scale overflows (#368). The other tails are right there
|
|
175
|
+
* (P = 1, Q = 0 and log P = 0 to double precision: q / scale exceeds even the largest shape by far more than its
|
|
176
|
+
* standard deviation), and so is log Q = -Inf where its leading term shape (r - 1 - log r), r = q / (shape scale),
|
|
177
|
+
* overflows. Where that term is finite (a shape of about 1e290 and above) log Q is finite, but gammaInc sees only
|
|
178
|
+
* x = Inf and returns -Inf: such values are NaN, not computed (user decision, 2026-11-03)
|
|
179
|
+
*/
|
|
180
|
+
function overflowLogUpperFinite(q, shape, scale) {
|
|
181
|
+
const ps = shape * scale;
|
|
182
|
+
const r = ps >= MIN_NORMAL ? q / ps : Math.exp(Math.log(q) - Math.log(scale) - Math.log(shape));
|
|
183
|
+
return shape * (r - 1 - Math.log(r)) < Infinity;
|
|
184
|
+
}
|
|
145
185
|
/**
|
|
146
186
|
* log of the standardized gamma quantile when it is below the smallest normal double (else undefined): there
|
|
147
187
|
* P(shape, x) = x^shape / Gamma(shape + 1) to double precision, so log x = (log P + lgamma(shape + 1)) / shape.
|
|
@@ -165,8 +205,10 @@ export function logTinyGammaQuantile(logTarget, lower, shape, linearTarget) {
|
|
|
165
205
|
export function qgammaStd(p, shape, lowerTail, logP, scale = 1) {
|
|
166
206
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
167
207
|
const tiny = logTinyGammaQuantile(logTarget, lower, shape, linearTarget);
|
|
208
|
+
// (the log is -Infinity where log P / shape overflows at a subnormal shape; added to log scale in double-double it was
|
|
209
|
+
// NaN: qchisq(0.5, 1e-310) was NaN)
|
|
168
210
|
if (tiny !== undefined)
|
|
169
|
-
return expOf(scale === 1 ? tiny : ddAdd(tiny, ddLog([scale, 0])));
|
|
211
|
+
return expOf(scale === 1 || tiny[0] === -Infinity ? tiny : ddAdd(tiny, ddLog([scale, 0])));
|
|
170
212
|
let x0;
|
|
171
213
|
if (shape > 1 && logTarget <= -700) {
|
|
172
214
|
// Deep tail: Wilson-Hilferty approximation x = a (1 - 1/(9a) + z / sqrt(9a))^3
|
|
@@ -203,21 +245,34 @@ export function qgammaStd(p, shape, lowerTail, logP, scale = 1) {
|
|
|
203
245
|
hi: Infinity,
|
|
204
246
|
positive: true,
|
|
205
247
|
});
|
|
248
|
+
// The standardized quantile beyond the largest double (#368): the search stops at Number.MAX_VALUE, and x * scale
|
|
249
|
+
// was MAX_VALUE times scale (qgamma(-1e308, 1e308, scale 1e-308, upper, log) 1.797, the true value about 3.146). With
|
|
250
|
+
// scale >= 1 the quantile is Inf; below 1 it may be finite but is not computed here: NaN (user decision, 2026-11-03)
|
|
251
|
+
if (x >= 2 ** 1000) {
|
|
252
|
+
const atMax = gammaInc(shape, Number.MAX_VALUE, lower, true);
|
|
253
|
+
if (lower ? atMax < logTarget : atMax > logTarget)
|
|
254
|
+
return scale >= 1 ? Infinity : NaN;
|
|
255
|
+
}
|
|
206
256
|
return x * scale;
|
|
207
257
|
}
|
|
208
258
|
export function qgamma1(p, shape, scale, lowerTail, logP) {
|
|
209
259
|
if (Number.isNaN(p) || Number.isNaN(shape) || Number.isNaN(scale))
|
|
210
260
|
return NaN;
|
|
261
|
+
// parameters that define no distribution before the ends, as dgamma and pgamma (R gives 0 and Inf at p = 0 and 1)
|
|
262
|
+
if (shape < 0 || scale <= 0)
|
|
263
|
+
return NaN;
|
|
211
264
|
const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
|
|
212
265
|
if (b !== undefined)
|
|
213
266
|
return b;
|
|
214
|
-
if (shape < 0 || scale <= 0)
|
|
215
|
-
return NaN;
|
|
216
267
|
if (shape === 0)
|
|
217
268
|
return 0;
|
|
218
269
|
// Shape Inf: all the mass at +Inf, so every probability strictly between 0 and 1 maps to Inf
|
|
219
270
|
if (shape === Infinity)
|
|
220
271
|
return Infinity;
|
|
272
|
+
// Scale Inf (1/rate overflows for a rate below 5.6e-309): pgamma is 0 at every finite q there, so Inf likewise (the
|
|
273
|
+
// scaling of a quantile below the normal range made it NaN: qgamma(3.3e-160, 0.5, rate = 4.2e-319))
|
|
274
|
+
if (scale === Infinity)
|
|
275
|
+
return Infinity;
|
|
221
276
|
return qgammaStd(p, shape, lowerTail, logP, scale);
|
|
222
277
|
}
|
|
223
278
|
/**
|
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
// P[X = x] = p q^x, P[X > x] = q^(x+1) (q = 1 - p). q^n is computed with Loader's (2000) building blocks
|
|
4
4
|
// (dbinomRawDD in src/special/loader.ts, as the binomial probability of 0 successes and n failures)
|
|
5
5
|
// (the exponent is kept in double-double so that the rounding of n log q is not amplified n times).
|
|
6
|
-
// Quantiles use a closed form.
|
|
6
|
+
// Quantiles use a closed form, with the ratio of logs in double-double.
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
9
9
|
import { log1mExp } from "../internal/ddexp.js";
|
|
10
|
-
import { nonInt } from "../internal/integer.js";
|
|
10
|
+
import { countAtMost, nonInt } from "../internal/integer.js";
|
|
11
|
+
import { FUZZ_LINEAR, FUZZ_LOG } from "../internal/discrete.js";
|
|
11
12
|
import { dbinomRawDD } from "../special/loader.js";
|
|
12
|
-
import { ddLog, ddMul, ddMulNum, twoSum } from "../special/dd.js";
|
|
13
|
+
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, twoSum } from "../special/dd.js";
|
|
13
14
|
/** p and q = 1 - p in double-double (the smaller one is the exact base) */
|
|
14
15
|
export function probPair(prob) {
|
|
15
16
|
return prob <= 0.5
|
|
@@ -24,7 +25,10 @@ export function probPair(prob) {
|
|
|
24
25
|
function qPow(n, pd, qd, log) {
|
|
25
26
|
if (n < 1e290)
|
|
26
27
|
return dbinomRawDD(0, n, pd, qd, log);
|
|
27
|
-
|
|
28
|
+
// log q = -p - p²/2 - ... for a tiny p (ddLog of 1 - p lost a subnormal p in its low part, and pgeom(1e290, 5e-324)
|
|
29
|
+
// was 0, 4.9e-34 expected)
|
|
30
|
+
const lq = qd[0] === 1 ? [-pd[0], -(pd[0] * pd[0]) / 2] : ddLog(qd);
|
|
31
|
+
const l = ddMul([n / 2 ** 64, 0], ddMulNum(lq, 2 ** 64));
|
|
28
32
|
if (!(l[0] > -Infinity))
|
|
29
33
|
return log ? -Infinity : 0;
|
|
30
34
|
return log ? l[0] + l[1] : Math.exp(l[0]) * (1 + l[1]);
|
|
@@ -45,10 +49,19 @@ export function dgeom1(x, prob, log) {
|
|
|
45
49
|
const qn = qPow(n, pd, qd, log);
|
|
46
50
|
return log ? Math.log(prob) + qn : prob * qn;
|
|
47
51
|
}
|
|
48
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* P[X > x] = q^(x+1) (x a nonnegative integer). Where x + 1 is not exact (beyond 2^53), q^x q: x + 1 rounded to x or
|
|
54
|
+
* x + 2 and the tail was off by a factor q (relative 1e-14 for prob = 1e-14 at x = 2^54; issue #327)
|
|
55
|
+
*/
|
|
49
56
|
function upper(x, prob, log) {
|
|
50
57
|
const { pd, qd } = probPair(prob);
|
|
51
|
-
|
|
58
|
+
if (x + 1 - x === 1)
|
|
59
|
+
return qPow(x + 1, pd, qd, log);
|
|
60
|
+
const qx = qPow(x, pd, qd, log);
|
|
61
|
+
if (!log)
|
|
62
|
+
return qx * (qd[0] + qd[1]);
|
|
63
|
+
const lq = qd[0] === 1 ? [-pd[0], -(pd[0] * pd[0]) / 2] : ddLog(qd);
|
|
64
|
+
return qx + (lq[0] + lq[1]);
|
|
52
65
|
}
|
|
53
66
|
function pgeomRaw(x, prob, lowerTail, logP) {
|
|
54
67
|
if (!lowerTail)
|
|
@@ -68,7 +81,7 @@ export function pgeom1(q, prob, lowerTail, logP) {
|
|
|
68
81
|
return dt0(lowerTail, logP);
|
|
69
82
|
if (!Number.isFinite(q) || prob === 1)
|
|
70
83
|
return dt1(lowerTail, logP);
|
|
71
|
-
return pgeomRaw(
|
|
84
|
+
return pgeomRaw(countAtMost(q), prob, lowerTail, logP);
|
|
72
85
|
}
|
|
73
86
|
export function qgeom1(p, prob, lowerTail, logP) {
|
|
74
87
|
if (Number.isNaN(p) || Number.isNaN(prob))
|
|
@@ -82,14 +95,40 @@ export function qgeom1(p, prob, lowerTail, logP) {
|
|
|
82
95
|
return 0;
|
|
83
96
|
if (b !== undefined)
|
|
84
97
|
return b;
|
|
85
|
-
// Smallest integer y with P[X > y] = q^(y+1) <= (upper probability),
|
|
86
|
-
// (
|
|
98
|
+
// Smallest integer y with P[X > y] = q^(y+1) <= (upper probability) u, in closed form: y + 1 = ceil(log u / log q)
|
|
99
|
+
// (the distribution function is flat, so in an integer search the probability slack would move the answer a lot).
|
|
100
|
+
// R's form in double with a slack of 1e-12 on the ratio took y - 1 for a p up to 1e-12 relative above a jump
|
|
101
|
+
// (qgeom(0.7500000000000866, 0.5) was 1, where P[X <= 1] = 0.75 < p). Here the ratio r is formed in double-double
|
|
102
|
+
// (log u exact from p, 1 - p by twoSum, except from a log lower probability, whose -expm1 is rounded once), and the
|
|
103
|
+
// slack of discreteQuantile on p (8 ε relative, 2 ε on the log scale, so that qgeom(pgeom(y)) = y) is carried over
|
|
104
|
+
// to r only where it is below half a step. Where the rounding of p spans whole steps (p near 1 with a tiny prob:
|
|
105
|
+
// 8 ε there is 1.8e5 steps for qgeom(0.9999999999, 1e-10)), the answer for p as given, as R's
|
|
87
106
|
let lu;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
107
|
+
let dlu; // change of log u for the slack on p
|
|
108
|
+
if (logP) {
|
|
109
|
+
lu = lowerTail ? (p > -Math.LN2 ? ddLog([-Math.expm1(p), 0]) : [log1mExp(p), 0]) : [p, 0];
|
|
110
|
+
// (u = 1 - e^p: d log u / dp = -1 / expm1(-p))
|
|
111
|
+
dlu = FUZZ_LOG * -p * (lowerTail ? 1 / Math.expm1(-p) : 1);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
lu = lowerTail ? ddLog(twoSum(1, -p)) : ddLog([p, 0]);
|
|
115
|
+
dlu = FUZZ_LINEAR * (lowerTail ? p / (1 - p) : 1);
|
|
116
|
+
}
|
|
117
|
+
const { pd, qd } = probPair(prob);
|
|
118
|
+
const lq = qd[0] === 1 ? [-pd[0], -(pd[0] * pd[0]) / 2] : ddLog(qd);
|
|
119
|
+
const r = ddDiv(lu, lq);
|
|
120
|
+
// beyond the doubles (a subnormal prob): Infinity
|
|
121
|
+
if (!Number.isFinite(r[0]))
|
|
122
|
+
return r[0];
|
|
123
|
+
const dr = dlu / -lq[0];
|
|
124
|
+
// an r within 2^-90 relative above an integer is that integer too (a p equal to the distribution function at a jump,
|
|
125
|
+
// such as 0.75 for prob = 1/2, is not moved up by the rounding of the logs)
|
|
126
|
+
const tie = Math.abs(r[0]) * 2 ** -90;
|
|
127
|
+
const s = ddAdd(r, [-(dr < 0.5 ? dr + tie : tie), 0]);
|
|
128
|
+
let n = Math.ceil(s[0]);
|
|
129
|
+
if (s[0] === n && s[1] > 0)
|
|
130
|
+
n += 1;
|
|
131
|
+
return Math.max(0, n - 1);
|
|
93
132
|
}
|
|
94
133
|
/**
|
|
95
134
|
* Probability mass function of the geometric distribution (number of failures before the first success). Equivalent to R's `dgeom(x, prob, log = FALSE)`.
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
|
|
2
2
|
import { type DensityOptions, type TailOptions } from "../internal/dpq.js";
|
|
3
3
|
export declare function dhyper1(x: number, m: number, n: number, k: number, log: boolean): number;
|
|
4
|
+
/** Check and round the parameters of phyper / qhyper. undefined if invalid */
|
|
5
|
+
/** m / (m + n) without forming m + n, which may overflow */
|
|
6
|
+
export declare function whiteFraction(m: number, n: number): number;
|
|
4
7
|
export declare function phyper1(q: number, m: number, n: number, k: number, lowerTail: boolean, logP: boolean): number;
|
|
5
|
-
|
|
8
|
+
/** (limitTime false: without the bound on sigma, for rhyper's inversion, which is unchanged) */
|
|
9
|
+
export declare function qhyper1(p: number, m: number, n: number, k: number, lowerTail: boolean, logP: boolean, limitTime?: boolean): number;
|
|
6
10
|
/**
|
|
7
11
|
* Probability mass function of the hypergeometric distribution (number of white balls among `k` drawn without replacement from an urn of `m` white and `n` black balls). Equivalent to R's `dhyper(x, m, n, k, log = FALSE)`.
|
|
8
12
|
*
|
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
10
10
|
import { discreteQuantile } from "../internal/discrete.js";
|
|
11
|
-
import { nonInt, roundHalfEven } from "../internal/integer.js";
|
|
11
|
+
import { countAtMost, nonInt, roundHalfEven } from "../internal/integer.js";
|
|
12
12
|
import { bd0dd, dbinomExponent } from "../special/loader.js";
|
|
13
|
-
import { ddAdd, ddDiv, ddLog, ddMul, ddNeg, ddSub, twoProd } from "../special/dd.js";
|
|
13
|
+
import { ddAdd, ddDiv, ddLog, ddMul, ddNeg, ddSub, twoProd, twoSum } from "../special/dd.js";
|
|
14
14
|
import { qnorm } from "./normal.js";
|
|
15
|
+
import { dbinom1, pbinom1, qbinom1 } from "./binomial.js";
|
|
15
16
|
/**
|
|
16
17
|
* Binomial probability b(x; x + nmx, p) as an ExpForm (0 <= x, 0 <= nmx, 0 < p < 1).
|
|
17
18
|
* At the ends (x = 0 or nmx = 0), q^n / p^n is represented by the exponent alone.
|
|
@@ -53,6 +54,14 @@ function evalForm(f, s) {
|
|
|
53
54
|
function logForm(f) {
|
|
54
55
|
return -f.e[0] - f.e[1] - 0.5 * f.logDenom;
|
|
55
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Whether m + n is exact in double precision. Beyond 2^53 it may round (1 + 1e16 to 1e16: k = 1e16 was taken for the
|
|
59
|
+
* whole population and dhyper(0, 1, 1e16, 1e16) was 1 for 1e-16; issue #118), and the probabilities inside the support
|
|
60
|
+
* are then NaN, as for the other integers beyond 2^53 (the ends, 0 and 1, and the binomial limit still answer)
|
|
61
|
+
*/
|
|
62
|
+
function exactTotal(m, n) {
|
|
63
|
+
return twoSum(m, n)[1] === 0;
|
|
64
|
+
}
|
|
56
65
|
/** Support [lo, hi] */
|
|
57
66
|
function support(m, n, k) {
|
|
58
67
|
return [Math.max(0, k - n), Math.min(k, m)];
|
|
@@ -67,13 +76,19 @@ export function dhyper1(x, m, n, k, log) {
|
|
|
67
76
|
k = roundHalfEven(k);
|
|
68
77
|
if (m < 0 || n < 0 || k < 0 || k > m + n)
|
|
69
78
|
return NaN;
|
|
79
|
+
if (binomialLimit(m, n, k))
|
|
80
|
+
return dbinom1(x, k, whiteFraction(m, n), log);
|
|
70
81
|
if (nonInt(x))
|
|
71
82
|
return d0(log);
|
|
72
83
|
x = roundHalfEven(x);
|
|
73
84
|
const [lo, hi] = support(m, n, k);
|
|
74
85
|
if (x < lo || x > hi)
|
|
75
86
|
return d0(log);
|
|
76
|
-
if (k === 0
|
|
87
|
+
if (k === 0)
|
|
88
|
+
return log ? 0 : 1;
|
|
89
|
+
if (!exactTotal(m, n))
|
|
90
|
+
return NaN;
|
|
91
|
+
if (k === m + n)
|
|
77
92
|
return log ? 0 : 1;
|
|
78
93
|
const f = hyperForm(x, m, n, k);
|
|
79
94
|
return log ? logForm(f) : evalForm(f, 1);
|
|
@@ -137,6 +152,18 @@ function directTail(x, m, n, k, lowerSide, lo, hi) {
|
|
|
137
152
|
return { form, sum, value: evalForm(form, sum) };
|
|
138
153
|
}
|
|
139
154
|
/** Check and round the parameters of phyper / qhyper. undefined if invalid */
|
|
155
|
+
/** m / (m + n) without forming m + n, which may overflow */
|
|
156
|
+
export function whiteFraction(m, n) {
|
|
157
|
+
return m >= n ? 1 / (1 + n / m) : n / m === Infinity ? 0 : m / n / (1 + m / n);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Whether k draws from m + n differ from k independent draws by less than double precision: k / (m + n) < 2^-60 (the
|
|
161
|
+
* hypergeometric and Bin(k, m / (m + n)) probabilities differ by a relative k / (m + n) or so). Always so when m + n
|
|
162
|
+
* overflows with k below 2^290, where the exact computation (which forms m + n) gave NaN
|
|
163
|
+
*/
|
|
164
|
+
function binomialLimit(m, n, k) {
|
|
165
|
+
return k > 0 && k < m * 2 ** -60 + n * 2 ** -60;
|
|
166
|
+
}
|
|
140
167
|
function hyperParams(m, n, k) {
|
|
141
168
|
if (![m, n, k].every(Number.isFinite))
|
|
142
169
|
return undefined;
|
|
@@ -154,30 +181,90 @@ export function phyper1(q, m, n, k, lowerTail, logP) {
|
|
|
154
181
|
if (params === undefined)
|
|
155
182
|
return NaN;
|
|
156
183
|
[m, n, k] = params;
|
|
157
|
-
|
|
184
|
+
if (binomialLimit(m, n, k))
|
|
185
|
+
return pbinom1(q, k, whiteFraction(m, n), lowerTail, logP);
|
|
186
|
+
const x = countAtMost(q);
|
|
158
187
|
const [lo, hi] = support(m, n, k);
|
|
159
188
|
if (x < lo)
|
|
160
189
|
return dt0(lowerTail, logP);
|
|
161
190
|
if (x >= hi)
|
|
162
191
|
return dt1(lowerTail, logP);
|
|
192
|
+
if (hi > MAX_EXACT || !exactTotal(m, n))
|
|
193
|
+
return NaN;
|
|
194
|
+
const terms = phyperTerms(x, m, n, k, lo, hi);
|
|
195
|
+
if (terms > MAX_TERMS) {
|
|
196
|
+
throw new RangeError(`phyper: m = ${m}, n = ${n}, k = ${k} needs about ${Math.round(terms)} terms at q = ${q} (at most 2^26)`);
|
|
197
|
+
}
|
|
163
198
|
return phyperRaw(x, m, n, k, lowerTail, logP);
|
|
164
199
|
}
|
|
165
|
-
|
|
200
|
+
/** Variance of the hypergeometric distribution (N = m + n) */
|
|
201
|
+
function hyperVariance(m, n, k, N) {
|
|
202
|
+
return (((k * m) / N) * (n / N) * (N - k)) / Math.max(N - 1, 1);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Estimated number of terms of tailSum from start (lower: downwards), from the normal approximation: the terms fall
|
|
206
|
+
* below 2^-60 of the sum about where (v^2 - u^2) / (2 sigma^2) = 60 ln 2 (u, v: signed distances of start and of the
|
|
207
|
+
* last term past the mean in the direction of the sum), so about sqrt(u^2 + 83 sigma^2) - u terms, and at most the
|
|
208
|
+
* distance to the end of the support. (Counted with a copy of the loop for sigma up to 4e4: at most 1.02 times this.)
|
|
209
|
+
*/
|
|
210
|
+
function tailTerms(start, lower, mu, variance, lo, hi) {
|
|
211
|
+
const u = lower ? mu - start : start - mu;
|
|
212
|
+
const w = Math.sqrt(u * u + 83 * variance);
|
|
213
|
+
return Math.min(u >= 0 ? (83 * variance) / (w + u) : w - u, lower ? start - lo : hi - start);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Estimated number of terms phyperRaw sums at x: the tail away from the mean, and the other one as well where the
|
|
217
|
+
* first may exceed 1/2, which needs x within 1 of the mean (the median of the distribution is that close to it)
|
|
218
|
+
*/
|
|
219
|
+
function phyperTerms(x, m, n, k, lo, hi) {
|
|
220
|
+
const N = m + n;
|
|
221
|
+
const mu = (k * m) / N;
|
|
222
|
+
const variance = hyperVariance(m, n, k, N);
|
|
223
|
+
const lowerSide = x * N < k * m;
|
|
224
|
+
const start = lowerSide ? x : x + 1;
|
|
225
|
+
const far = tailTerms(start, lowerSide, mu, variance, lo, hi);
|
|
226
|
+
if (Math.abs(start - mu) >= 1)
|
|
227
|
+
return far;
|
|
228
|
+
return far + tailTerms(lowerSide ? x + 1 : x, !lowerSide, mu, variance, lo, hi);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Bounds on the time of phyper and qhyper (the tail sums take about 80 to 170 ns per term on the development machine):
|
|
232
|
+
* phyper's estimated number of terms at most 2^26 (about 10 s; sigma about 7e6 at the center), qhyper's standard
|
|
233
|
+
* deviation at most 2^21 (its search calls the distribution function a few times, about 2.5 times 9.1 sigma terms)
|
|
234
|
+
*/
|
|
235
|
+
const MAX_TERMS = 2 ** 26;
|
|
236
|
+
const MAX_QHYPER_SIGMA = 2 ** 21;
|
|
237
|
+
/**
|
|
238
|
+
* Largest upper end of the support for phyper and qhyper: the tail sums step through the integers one by one, which
|
|
239
|
+
* beyond 2^53 no longer move (phyper(5e16, 1e17, 1e17, 1e17) did not return), so the answer is NaN there, as for
|
|
240
|
+
* other integers beyond 2^53. Below it the time grows as the standard deviation, bounded by MAX_TERMS and
|
|
241
|
+
* MAX_QHYPER_SIGMA
|
|
242
|
+
*/
|
|
243
|
+
const MAX_EXACT = 2 ** 53;
|
|
244
|
+
/** (limitTime false: without the bound on sigma, for rhyper's inversion, which is unchanged) */
|
|
245
|
+
export function qhyper1(p, m, n, k, lowerTail, logP, limitTime = true) {
|
|
166
246
|
if ([p, m, n, k].some(Number.isNaN))
|
|
167
247
|
return NaN;
|
|
168
248
|
const params = hyperParams(m, n, k);
|
|
169
249
|
if (params === undefined)
|
|
170
250
|
return NaN;
|
|
171
251
|
[m, n, k] = params;
|
|
252
|
+
if (binomialLimit(m, n, k))
|
|
253
|
+
return qbinom1(p, k, whiteFraction(m, n), lowerTail, logP);
|
|
172
254
|
const [lo, hi] = support(m, n, k);
|
|
173
255
|
const b = quantileBoundaries(p, lo, hi, lowerTail, logP);
|
|
174
256
|
if (b !== undefined)
|
|
175
257
|
return b;
|
|
176
258
|
if (lo === hi)
|
|
177
259
|
return lo;
|
|
260
|
+
if (hi > MAX_EXACT || !exactTotal(m, n))
|
|
261
|
+
return NaN;
|
|
178
262
|
const N = m + n;
|
|
179
263
|
const mu = (k * m) / N;
|
|
180
|
-
const sigma = Math.sqrt((
|
|
264
|
+
const sigma = Math.sqrt(hyperVariance(m, n, k, N));
|
|
265
|
+
if (limitTime && sigma > MAX_QHYPER_SIGMA) {
|
|
266
|
+
throw new RangeError(`qhyper: m = ${m}, n = ${n}, k = ${k} has a standard deviation of about ${sigma.toPrecision(3)} (at most 2^21)`);
|
|
267
|
+
}
|
|
181
268
|
const z = qnorm(p, { lowerTail, logP });
|
|
182
269
|
const y0 = mu + sigma * z;
|
|
183
270
|
return discreteQuantile({
|
|
@@ -23,8 +23,9 @@ export function dinvgamma1(x, alpha, beta, log) {
|
|
|
23
23
|
return NaN;
|
|
24
24
|
if (x <= 0 || x === Infinity)
|
|
25
25
|
return d0(log);
|
|
26
|
+
// alpha = Inf: all the mass at 0 (the limit pinvgamma and qinvgamma take), so the density at x > 0 is 0 (it was NaN)
|
|
26
27
|
if (alpha === Infinity)
|
|
27
|
-
return
|
|
28
|
+
return d0(log);
|
|
28
29
|
const [z, zl] = standardizeDD(beta, 0, x);
|
|
29
30
|
if (z === Infinity)
|
|
30
31
|
return d0(log);
|
|
@@ -32,6 +33,10 @@ export function dinvgamma1(x, alpha, beta, log) {
|
|
|
32
33
|
// beta / x subnormal or 0: in logs, log f = alpha log(beta / x) - lgamma(alpha) - log x (e^-z is 1), with
|
|
33
34
|
// log(beta / x) from log beta - log x
|
|
34
35
|
const lr = logRatioDD(beta, x);
|
|
36
|
+
// (where alpha log(beta / x) or lgamma(alpha) overflows the log is -Infinity, which the double-double sums made NaN:
|
|
37
|
+
// dinvgamma(4.9e270, 1.3e306, beta 1.8e-118))
|
|
38
|
+
if (!(Math.abs(lr[0] * alpha) < Infinity) || lgamma(alpha) === Infinity)
|
|
39
|
+
return d0(log);
|
|
35
40
|
const l = ddAdd(ddSub(alpha < 1e300 ? ddMulNum(lr, alpha) : [lr[0] * alpha, 0], ddLog([x, 0])), [-lgamma(alpha), 0]);
|
|
36
41
|
return log ? l[0] + l[1] : l[0] < -746 ? 0 : ddExp(l)[0];
|
|
37
42
|
}
|