@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
|
@@ -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
|
|
@@ -48,10 +49,19 @@ export function dgeom1(x, prob, log) {
|
|
|
48
49
|
const qn = qPow(n, pd, qd, log);
|
|
49
50
|
return log ? Math.log(prob) + qn : prob * qn;
|
|
50
51
|
}
|
|
51
|
-
/**
|
|
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
|
+
*/
|
|
52
56
|
function upper(x, prob, log) {
|
|
53
57
|
const { pd, qd } = probPair(prob);
|
|
54
|
-
|
|
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]);
|
|
55
65
|
}
|
|
56
66
|
function pgeomRaw(x, prob, lowerTail, logP) {
|
|
57
67
|
if (!lowerTail)
|
|
@@ -71,7 +81,7 @@ export function pgeom1(q, prob, lowerTail, logP) {
|
|
|
71
81
|
return dt0(lowerTail, logP);
|
|
72
82
|
if (!Number.isFinite(q) || prob === 1)
|
|
73
83
|
return dt1(lowerTail, logP);
|
|
74
|
-
return pgeomRaw(
|
|
84
|
+
return pgeomRaw(countAtMost(q), prob, lowerTail, logP);
|
|
75
85
|
}
|
|
76
86
|
export function qgeom1(p, prob, lowerTail, logP) {
|
|
77
87
|
if (Number.isNaN(p) || Number.isNaN(prob))
|
|
@@ -85,14 +95,40 @@ export function qgeom1(p, prob, lowerTail, logP) {
|
|
|
85
95
|
return 0;
|
|
86
96
|
if (b !== undefined)
|
|
87
97
|
return b;
|
|
88
|
-
// Smallest integer y with P[X > y] = q^(y+1) <= (upper probability),
|
|
89
|
-
// (
|
|
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
|
|
90
106
|
let lu;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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);
|
|
96
132
|
}
|
|
97
133
|
/**
|
|
98
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);
|
|
@@ -7,25 +7,45 @@ import { vectorize } from "../internal/vectorize.js";
|
|
|
7
7
|
import { quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
8
8
|
import { expNeg, expNegOver, locationScale, standardizeDD } from "../internal/ddexp.js";
|
|
9
9
|
const MIN_NORMAL = 2 ** -1022;
|
|
10
|
-
const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0
|
|
11
|
-
// an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does
|
|
10
|
+
const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0;
|
|
11
|
+
// an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does. sigma = Inf is
|
|
12
|
+
// the limit too (review 24: density 0, probability 1/2 at every finite x, as extraDistr; the quantiles -Inf, mu and Inf),
|
|
13
|
+
// except at an infinite x or mu, where it is not defined (NaN, as extraDistr)
|
|
12
14
|
export function dlaplace1(x, mu, sigma, log) {
|
|
13
15
|
if (Number.isNaN(x) || !valid(mu, sigma))
|
|
14
16
|
return NaN;
|
|
15
|
-
if (
|
|
17
|
+
if (sigma === Infinity)
|
|
18
|
+
return Number.isFinite(x) && Number.isFinite(mu) ? (log ? -Infinity : 0) : NaN;
|
|
19
|
+
if (!Number.isFinite(mu)) {
|
|
20
|
+
// x and mu the same infinity: x - mu is Inf - Inf and the limit depends on the path (NaN, as in extraDistr; #338)
|
|
21
|
+
if (x === mu)
|
|
22
|
+
return NaN;
|
|
16
23
|
return log ? -Infinity : 0;
|
|
24
|
+
}
|
|
17
25
|
const z = standardizeDD(x, mu, sigma);
|
|
18
26
|
const a = z[0] < 0 ? [-z[0], -z[1]] : [z[0], z[1]];
|
|
27
|
+
// 2 sigma overflows for sigma above half the largest double: then the factor 1/2 is taken apart
|
|
28
|
+
const twoSigma = 2 * sigma;
|
|
19
29
|
if (log)
|
|
20
|
-
return -a[0] - a[1] - Math.log(
|
|
30
|
+
return -a[0] - a[1] - (twoSigma < Infinity ? Math.log(twoSigma) : Math.LN2 + Math.log(sigma));
|
|
21
31
|
// (from exp(-|z|) below the normal range the quotient is formed without rounding it there: see expNegOver)
|
|
22
32
|
const e = expNeg(a);
|
|
23
|
-
|
|
33
|
+
// (the factor 1/2 on e first, exactly: (e / sigma) / 2 rounded twice onto the subnormals, an ulp off at x = sigma / 2
|
|
34
|
+
// for sigma = 1.447e308; issue #110. Below 2 MIN_NORMAL the quotient is below e^-708 / 1.8e308, 0 either way)
|
|
35
|
+
if (twoSigma === Infinity)
|
|
36
|
+
return e >= 2 * MIN_NORMAL ? e / 2 / sigma : expNegOver(a, sigma) / 2;
|
|
37
|
+
return e >= MIN_NORMAL ? e / twoSigma : expNegOver(a, twoSigma);
|
|
24
38
|
}
|
|
25
39
|
export function plaplace1(q, mu, sigma, lowerTail, logP) {
|
|
26
40
|
if (Number.isNaN(q) || !valid(mu, sigma))
|
|
27
41
|
return NaN;
|
|
42
|
+
if (sigma === Infinity)
|
|
43
|
+
return Number.isFinite(q) && Number.isFinite(mu) ? (logP ? -Math.LN2 : 0.5) : NaN;
|
|
28
44
|
if (!Number.isFinite(mu)) {
|
|
45
|
+
// q and mu the same infinity: the limit is 0 or 1 depending on which goes first (NaN, as in extraDistr, pnorm,
|
|
46
|
+
// plogis and pcauchy; #338)
|
|
47
|
+
if (q === mu)
|
|
48
|
+
return NaN;
|
|
29
49
|
const lower = mu === Infinity ? 0 : 1;
|
|
30
50
|
const v = lowerTail ? lower : 1 - lower;
|
|
31
51
|
return logP ? Math.log(v) : v;
|
|
@@ -38,8 +58,9 @@ export function plaplace1(q, mu, sigma, lowerTail, logP) {
|
|
|
38
58
|
return logP ? Math.log1p(-half) : 1 - half;
|
|
39
59
|
}
|
|
40
60
|
export function qlaplace1(p, mu, sigma, lowerTail, logP) {
|
|
41
|
-
if (Number.isNaN(p) || !valid(mu, sigma))
|
|
61
|
+
if (Number.isNaN(p) || !valid(mu, sigma) || (sigma === Infinity && !Number.isFinite(mu)))
|
|
42
62
|
return NaN;
|
|
63
|
+
// (sigma = Inf: locationScale gives -Inf below p = 1/2, mu at 1/2 and Inf above)
|
|
43
64
|
const bd = quantileBoundaries(p, -Infinity, Infinity, lowerTail, logP);
|
|
44
65
|
if (bd !== undefined)
|
|
45
66
|
return bd;
|
|
@@ -25,7 +25,10 @@ export function dlogis1(x, location, scale, log) {
|
|
|
25
25
|
// 4.81926e-319 }) was 1.3e-6 off)
|
|
26
26
|
if (e < MIN_NORMAL)
|
|
27
27
|
return expNegOver(a, scale);
|
|
28
|
-
|
|
28
|
+
// (the division by scale last also where the denominator overflows: dlogis(0, { scale: 1.28e308 }) was 0 for
|
|
29
|
+
// 1.95e-309; issue #109)
|
|
30
|
+
const d = scale * (1 + e) * (1 + e);
|
|
31
|
+
return scale >= MIN_NORMAL && d < Infinity ? e / d : e / ((1 + e) * (1 + e)) / scale;
|
|
29
32
|
}
|
|
30
33
|
export function plogis1(q, location, scale, lowerTail, logP) {
|
|
31
34
|
if (Number.isNaN(q) || Number.isNaN(location) || Number.isNaN(scale))
|
|
@@ -32,8 +32,14 @@ function zOf(x, meanlog, sdlog) {
|
|
|
32
32
|
}
|
|
33
33
|
/** z^2 / 2 in double-double */
|
|
34
34
|
function halfSquare(z) {
|
|
35
|
-
if (Math.abs(z[0]) > 1e150)
|
|
36
|
-
|
|
35
|
+
if (Math.abs(z[0]) > 1e150) {
|
|
36
|
+
// z times 2^-512 (exact) so that the splitting in ddMul does not overflow, and the square back by 2^1024 / 2 (the
|
|
37
|
+
// low part of z was dropped here: an ulp off where z^2 / 2 is finite; issue #112)
|
|
38
|
+
const t = [z[0] * 2 ** -512, z[1] * 2 ** -512];
|
|
39
|
+
const s = ddMul(t, t);
|
|
40
|
+
const hi = s[0] * 2 ** 1023;
|
|
41
|
+
return Number.isFinite(hi) ? [hi, s[1] * 2 ** 1023] : [hi, 0];
|
|
42
|
+
}
|
|
37
43
|
const s = ddMul(z, z);
|
|
38
44
|
return [s[0] / 2, s[1] / 2];
|
|
39
45
|
}
|
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
// The sums of the weights are kept in double-double: q_k is raised to the power x_k, so a rounded sum would give an
|
|
7
7
|
// error of x_k ε (1e-9 at x_k = 1e7).
|
|
8
8
|
// Summing lgamma terms instead loses digits to cancellation for large counts.
|
|
9
|
+
import { lchoose } from "../special/choose.js";
|
|
9
10
|
import { dbinomRawDD } from "../special/loader.js";
|
|
10
11
|
import { ddAdd, ddDiv } from "../special/dd.js";
|
|
12
|
+
import { ldexp } from "../internal/summary.js";
|
|
11
13
|
/**
|
|
12
14
|
* Probability function of the multinomial distribution. Equivalent to R's
|
|
13
15
|
* `dmultinom(x, size = NULL, prob, log = FALSE)`.
|
|
@@ -40,29 +42,63 @@ export function dmultinom(x, prob, options = {}) {
|
|
|
40
42
|
const N = x.reduce((a, b) => a + b, 0);
|
|
41
43
|
if (options.size !== undefined && options.size !== N)
|
|
42
44
|
throw new RangeError("size must equal the sum of x");
|
|
45
|
+
// Suffix sums of the weights (double-double) and the counts
|
|
46
|
+
let w = prob;
|
|
47
|
+
let ps = suffixSums(w);
|
|
48
|
+
// the weights are divided by 2^scale (0 unless their sum overflows)
|
|
49
|
+
let scale = 0;
|
|
50
|
+
if (ps[0][0] === Infinity) {
|
|
51
|
+
// Finite weights whose sum overflows: divided by 2^s >= K, which only the ratios matter for (exact unless a
|
|
52
|
+
// weight becomes subnormal; one that becomes 0 is below 1e-600 of the largest); weights with a sum in range are
|
|
53
|
+
// used as given
|
|
54
|
+
scale = Math.ceil(Math.log2(K));
|
|
55
|
+
w = prob.map((p) => ldexp(p, -scale));
|
|
56
|
+
ps = suffixSums(w);
|
|
57
|
+
}
|
|
58
|
+
const ns = new Array(K + 1).fill(0);
|
|
59
|
+
for (let k = K - 1; k >= 0; k--)
|
|
60
|
+
ns[k] = ns[k + 1] + x[k];
|
|
43
61
|
// A category with probability 0 and a positive count makes the probability 0
|
|
44
62
|
for (let k = 0; k < K; k++)
|
|
45
63
|
if (prob[k] === 0 && x[k] > 0)
|
|
46
64
|
return log ? -Infinity : 0;
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
65
|
+
// A total of the counts that overflows (each count finite) is NaN on both scales, as dnbinom where size + x overflows
|
|
66
|
+
// (reviews 22 and 55) and dhyper's m + n: the integer range beyond 2^53. (The linear value was 0, the log NaN; the
|
|
67
|
+
// true value of dmultinom([1e308, 1e308], [0.5, 0.5]) is 5.64e-155; issue #152)
|
|
68
|
+
if (N === Infinity)
|
|
69
|
+
return NaN;
|
|
70
|
+
// The scaled weights are used only where the sum of the weights from k on overflows: a weight that the scaling takes
|
|
71
|
+
// to 0 (or into the subnormals) is not lost from the later ratios. Where nothing overflows this is ps itself
|
|
72
|
+
const psOrig = scale === 0 ? ps : suffixSums(prob);
|
|
73
|
+
const logDD = (d) => Math.log(d[0]) + d[1] / d[0];
|
|
54
74
|
let logSum = 0;
|
|
55
75
|
let prodLin = 1;
|
|
56
76
|
for (let k = 0; k < K; k++) {
|
|
57
77
|
if (prob[k] === 0)
|
|
58
78
|
continue;
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
79
|
+
const restOrig = psOrig[k + 1];
|
|
80
|
+
if (restOrig[0] === 0)
|
|
61
81
|
break; // the remaining counts all fall here: probability 1
|
|
62
|
-
const
|
|
63
|
-
const
|
|
82
|
+
const scaled = !(psOrig[k][0] < Infinity);
|
|
83
|
+
const sum = scaled ? ps[k] : psOrig[k];
|
|
84
|
+
const sc = scaled ? scale : 0;
|
|
85
|
+
const q = ddDiv([scaled ? w[k] : prob[k], 0], sum);
|
|
86
|
+
const qc = ddDiv(scaled ? ps[k + 1] : restOrig, sum);
|
|
64
87
|
const xk = x[k];
|
|
65
88
|
const nmx = ns[k + 1];
|
|
89
|
+
if (q[0] < 2 ** -1022 || qc[0] < 2 ** -1022) {
|
|
90
|
+
// q or 1 - q below the normal range (dmultinom([1, 0, 0], [5e-324, 1, 1], { log: true }) was -Infinity,
|
|
91
|
+
// -745.13 to the double; R gives -Infinity too): the term from the log of the small one, log(1 - r) = -r (to r²)
|
|
92
|
+
const small = q[0] < 2 ** -1022;
|
|
93
|
+
const lr = (small ? Math.log(prob[k]) : logDD(restOrig)) - sc * Math.LN2 - logDD(sum);
|
|
94
|
+
const [xr, xo] = small ? [xk, nmx] : [nmx, xk];
|
|
95
|
+
const term = lchoose(xk + nmx, xk, { rCompatible: false }) + (xr === 0 ? 0 : xr * lr) - xo * Math.exp(lr);
|
|
96
|
+
if (log)
|
|
97
|
+
logSum += term;
|
|
98
|
+
else
|
|
99
|
+
prodLin *= Math.exp(term);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
66
102
|
if (log)
|
|
67
103
|
logSum += dbinomRawDD(xk, nmx, q, qc, true);
|
|
68
104
|
else
|
|
@@ -70,3 +106,10 @@ export function dmultinom(x, prob, options = {}) {
|
|
|
70
106
|
}
|
|
71
107
|
return log ? logSum : prodLin;
|
|
72
108
|
}
|
|
109
|
+
/** Suffix sums w_k + ... + w_(K-1) in double-double, for k = 0..K (the last is 0) */
|
|
110
|
+
function suffixSums(w) {
|
|
111
|
+
const ps = new Array(w.length + 1).fill([0, 0]);
|
|
112
|
+
for (let k = w.length - 1; k >= 0; k--)
|
|
113
|
+
ps[k] = ddAdd(ps[k + 1], [w[k], 0]);
|
|
114
|
+
return ps;
|
|
115
|
+
}
|
|
@@ -96,8 +96,13 @@ function betaKernelIncLog(s, b, xd, xcd) {
|
|
|
96
96
|
if (!(xd[0] > 0 && xcd[0] > 0))
|
|
97
97
|
return -Infinity;
|
|
98
98
|
const l = ddAdd(ddMulNum(ddLog(xd), s), ddMulNum(ddLog(xcd), b));
|
|
99
|
-
if (s < 1 && b < 1)
|
|
100
|
-
|
|
99
|
+
if (s < 1 && b < 1) {
|
|
100
|
+
// log(1 + s / b), also where s / b overflows for a subnormal b (it was Infinity and the term 0: the j = 0 term of
|
|
101
|
+
// dbeta(0.5, 0.75, 5e-324, ncp = 0.1) was dropped, 3.7 off in the log; issue #114)
|
|
102
|
+
const q = s / b;
|
|
103
|
+
const lq = q < Infinity ? Math.log1p(q) : Math.log(s) - Math.log(b) + Math.log1p(b / s);
|
|
104
|
+
return ddAdd(l, [-(lgamma1p(s) + lgamma1p(b) - lgamma1p(s + b) + lq), 0]);
|
|
105
|
+
}
|
|
101
106
|
return ddAdd(ddAdd(l, ddNeg(lbetaDD(s, b))), [-Math.log(s), 0]);
|
|
102
107
|
}
|
|
103
108
|
/** Form x and 1 - x as a double-double pair, based on the smaller of the two */
|
|
@@ -137,6 +142,9 @@ function leftTailLog(a, b, ncp, xd, xcd) {
|
|
|
137
142
|
return undefined;
|
|
138
143
|
if (mu * rho > 100)
|
|
139
144
|
return rho <= 1e-16 ? -mu + betaIncDD(a, b, xd, xcd, true, true) : undefined;
|
|
145
|
+
// (λ/2 underflows to 0 for a subnormal λ: only the j = 0 term, I_x(a, b), is left)
|
|
146
|
+
if (mu === 0)
|
|
147
|
+
return betaIncDD(a, b, xd, xcd, true, true);
|
|
140
148
|
const lmu = Math.log(mu);
|
|
141
149
|
let max = -Infinity;
|
|
142
150
|
const terms = [];
|
|
@@ -584,7 +592,11 @@ export function dnbeta1(x, a, b, ncp, log) {
|
|
|
584
592
|
return log ? -Infinity : 0;
|
|
585
593
|
if (b < 1)
|
|
586
594
|
return Infinity;
|
|
587
|
-
|
|
595
|
+
const s = a + mu;
|
|
596
|
+
if (!log)
|
|
597
|
+
return s;
|
|
598
|
+
// the sum may overflow where its log does not (dnbeta(1, 1.5e308, 1, 1.5e308, log) was Infinity for 710.007; #96)
|
|
599
|
+
return Number.isFinite(s) ? Math.log(s) : Math.log(a / 2 + mu / 2) + Math.LN2;
|
|
588
600
|
}
|
|
589
601
|
if (negligibleNcp(mu, x * ((a + b) / a)))
|
|
590
602
|
return dbeta1(x, a, b, log);
|
|
@@ -593,6 +605,11 @@ export function dnbeta1(x, a, b, ncp, log) {
|
|
|
593
605
|
export function pnbeta1(q, a, b, ncp, lowerTail, logP) {
|
|
594
606
|
if (Number.isNaN(q) || Number.isNaN(a) || Number.isNaN(b) || Number.isNaN(ncp))
|
|
595
607
|
return NaN;
|
|
608
|
+
// parameters that define no distribution are checked before the ends of the support, as in the central pbeta (R
|
|
609
|
+
// gives 0 or 1 at q <= 0 or q >= 1 even for a = -1 or ncp = Inf). Shapes of 0 (point masses, the limits R accepts
|
|
610
|
+
// there) still get the ends
|
|
611
|
+
if (a < 0 || b < 0 || ncp < 0 || ncp === Infinity)
|
|
612
|
+
return NaN;
|
|
596
613
|
if (q <= 0)
|
|
597
614
|
return dt0(lowerTail, logP);
|
|
598
615
|
if (q >= 1)
|
|
@@ -690,8 +707,16 @@ function fPair(q, m, n) {
|
|
|
690
707
|
const d = ddAdd(mq, [n, 0]);
|
|
691
708
|
return [ddDiv(mq, d), ddDiv([n, 0], d)];
|
|
692
709
|
}
|
|
710
|
+
// m q is 1e290 or more (or overflows): 1 - x = zc where zc = n/(m q) is below the double-double precision
|
|
693
711
|
const zc = n / m / q;
|
|
694
|
-
|
|
712
|
+
if (zc < 2 ** -106)
|
|
713
|
+
return [twoSum(1, -zc), [zc, 0]];
|
|
714
|
+
// otherwise (n comparable to m q: zc = 1 gave x = 0 for 1/2; issue #158) x = 1/(1 + r), 1 - x = r/(1 + r),
|
|
715
|
+
// r = n/(m q) divided by the larger of m and q first (n/max(m, q) is then within the double range)
|
|
716
|
+
const [big, small] = m >= q ? [m, q] : [q, m];
|
|
717
|
+
const r = ddDiv(ddDiv([n, 0], [big, 0]), [small, 0]);
|
|
718
|
+
const d = ddAdd(r, [1, 0]);
|
|
719
|
+
return [ddDiv([1, 0], d), ddDiv(r, d)];
|
|
695
720
|
}
|
|
696
721
|
/** The Odds of x = m q/(n + m q): k = n/(m q), from its logs where it is out of the double range */
|
|
697
722
|
function fOdds(q, m, n, xd, xcd) {
|
|
@@ -702,11 +727,23 @@ function fOdds(q, m, n, xd, xcd) {
|
|
|
702
727
|
const lk = Math.log(n) - Math.log(m) - Math.log(q);
|
|
703
728
|
return { k, lk, l1mx: lk - Math.log1p(k) };
|
|
704
729
|
}
|
|
730
|
+
/** log(m/n), from the logs where the ratio is below the normal range (m/n underflows for finite positive m, n) */
|
|
731
|
+
function logRatio(m, n) {
|
|
732
|
+
const r = m / n;
|
|
733
|
+
return r >= 2 ** -1022 ? Math.log(r) : Math.log(m) - Math.log(n);
|
|
734
|
+
}
|
|
735
|
+
/** Whether x = m q/(n + m q) is below 1e-306 (the tiny-q series), with m q/n from its log where m/n underflows */
|
|
736
|
+
function tinyFx(q, m, n) {
|
|
737
|
+
const r = m / n;
|
|
738
|
+
if (r >= 2 ** -1022)
|
|
739
|
+
return r * q < 1e-306;
|
|
740
|
+
return logRatio(m, n) + Math.log(q) < Math.log(1e-306);
|
|
741
|
+
}
|
|
705
742
|
function dnfCore(q, m, n, ncp, log) {
|
|
706
|
-
if ((m
|
|
743
|
+
if (tinyFx(q, m, n)) {
|
|
707
744
|
// First term of the series: f(q) ≈ exp(-λ/2) (m q / n)^a / (B(a, b) q)
|
|
708
745
|
const a = m / 2;
|
|
709
|
-
const l = -ncp / 2 + a * (
|
|
746
|
+
const l = -ncp / 2 + a * (logRatio(m, n) + Math.log(q)) - lbeta(a, n / 2) - Math.log(q);
|
|
710
747
|
return log ? l : Math.exp(l);
|
|
711
748
|
}
|
|
712
749
|
const [xd, xcd] = fPair(q, m, n);
|
|
@@ -718,13 +755,13 @@ function dnfCore(q, m, n, ncp, log) {
|
|
|
718
755
|
*/
|
|
719
756
|
function pnfTiny(q, m, n, ncp, lower, log) {
|
|
720
757
|
const a = m / 2;
|
|
721
|
-
const l = -ncp / 2 + a * (
|
|
758
|
+
const l = -ncp / 2 + a * (logRatio(m, n) + Math.log(q)) - Math.log(a) - lbeta(a, n / 2);
|
|
722
759
|
if (lower)
|
|
723
760
|
return log ? l : Math.exp(l);
|
|
724
761
|
return log ? Math.log1p(-Math.exp(l)) : -Math.expm1(l);
|
|
725
762
|
}
|
|
726
763
|
export function pnfRaw(q, m, n, ncp, lower, log) {
|
|
727
|
-
if ((m
|
|
764
|
+
if (tinyFx(q, m, n))
|
|
728
765
|
return pnfTiny(q, m, n, ncp, lower, log);
|
|
729
766
|
const [xd, xcd] = fPair(q, m, n);
|
|
730
767
|
return pnbetaRaw(m / 2, n / 2, ncp, xd, xcd, lower, log, fOdds(q, m, n, xd, xcd));
|
|
@@ -744,13 +781,15 @@ export function dnf1(x, m, n, ncp, log) {
|
|
|
744
781
|
return x === 1 ? Infinity : log ? -Infinity : 0;
|
|
745
782
|
return x === 0 ? NaN : dfDensity1(x, m, n, log);
|
|
746
783
|
}
|
|
747
|
-
|
|
784
|
+
// (df2 so large that χ²_n/n is 1 on the scale of the result, by pnf1's criterion: the noncentral χ² limit, as for
|
|
785
|
+
// df2 = Inf; the tiny-q first term in dnfCore gave df(3, 2, 1e308, ncp = 1) 0.6065 for 0.0956)
|
|
786
|
+
if (n === Infinity || (n > 1e40 && hugeDf2(x, m, n, ncp))) {
|
|
748
787
|
// F = χ²_m(ncp) / m
|
|
749
788
|
const v = dnchisq1(x * m, m, ncp, log);
|
|
750
789
|
return log ? v + Math.log(m) : v * m;
|
|
751
790
|
}
|
|
752
791
|
if (x === Infinity)
|
|
753
|
-
return
|
|
792
|
+
return log ? -Infinity : 0; // the density tends to 0 (R gives NaN at x = Inf when df2 is finite; #493)
|
|
754
793
|
if (x === 0) {
|
|
755
794
|
if (m < 2)
|
|
756
795
|
return Infinity;
|
|
@@ -778,7 +817,9 @@ export function pnf1(q, m, n, ncp, lowerTail, logP) {
|
|
|
778
817
|
return NaN;
|
|
779
818
|
if (n === Infinity)
|
|
780
819
|
return pnchisq1(q * m, m, ncp, lowerTail, logP);
|
|
781
|
-
if (
|
|
820
|
+
if (n > 1e40 && hugeDf2(q, m, n, ncp))
|
|
821
|
+
return pnchisq1(q * m, m, ncp, lowerTail, logP);
|
|
822
|
+
if (tinyFx(q, m, n))
|
|
782
823
|
return pnfTiny(q, m, n, ncp, lowerTail, logP);
|
|
783
824
|
if (ncp === 0)
|
|
784
825
|
return pf1(q, m, n, lowerTail, logP);
|
|
@@ -787,6 +828,16 @@ export function pnf1(q, m, n, ncp, lowerTail, logP) {
|
|
|
787
828
|
return neg;
|
|
788
829
|
return pnfRaw(q, m, n, ncp, lowerTail, logP);
|
|
789
830
|
}
|
|
831
|
+
/**
|
|
832
|
+
* Whether df2 = n is so large that W = χ²_n/n is 1 on the scale of P[F <= q] (both tails), so that the noncentral χ²
|
|
833
|
+
* limit (df2 = Inf) is the distribution function to the double (also taken for the density in dnf1): the criterion
|
|
834
|
+
* of qnfRaw, with |log p| that of the smaller tail of the limit at q. (The series in x = m q/(n + m q) gave
|
|
835
|
+
* pf(2.9957, 2, 1e308, ncp = 1, upper) -0.817 for 0.13271; R's pnf takes the limit for every df2 > 1e8.)
|
|
836
|
+
*/
|
|
837
|
+
function hugeDf2(q, m, n, ncp) {
|
|
838
|
+
const lp = -Math.min(pnchisq1(q * m, m, ncp, true, true), pnchisq1(q * m, m, ncp, false, true));
|
|
839
|
+
return n > 1e40 * (lp + 1) && n > 1e20 * (m + 2 * lp + 1e3 + 4 * Math.sqrt((m + ncp) * (lp + 1)));
|
|
840
|
+
}
|
|
790
841
|
/** Below this log a probability rounds to 0 in double precision (as in src/distributions/nchisq.ts) */
|
|
791
842
|
const LOG_NEGLIGIBLE = -746;
|
|
792
843
|
/**
|