@numero-uno-jpn/rstats 0.15.3 → 0.17.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 +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- 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/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- 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/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- 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/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -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 +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- 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 +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- 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 +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -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 +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -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/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -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 +24 -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/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- 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-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/distributions/t.js
CHANGED
|
@@ -16,6 +16,12 @@ import { betaIncBothDD, betaIncDD } from "../special/incbet.js";
|
|
|
16
16
|
import { bd0, bd0dd, stirlerr } from "../special/loader.js";
|
|
17
17
|
import { dnorm, pnorm, qnorm } from "./normal.js";
|
|
18
18
|
import { HUGE_DF, dnt1, hugeDfRatio, hugeDfTail, hugeDfTailLog, hugeDfTailQuantile, pnt1, qnt1 } from "./nt.js";
|
|
19
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
20
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
21
|
+
const DT_KEYS = ["log", "ncp"];
|
|
22
|
+
const PT_KEYS = ["lowerTail", "logP", "ncp"];
|
|
23
|
+
const QT_KEYS = ["lowerTail", "logP", "ncp"];
|
|
24
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
19
25
|
const LN_SQRT_2PI = 0.918938533204672741780329736406;
|
|
20
26
|
/** -log(sqrt(2 pi)) in double-double (computed with Rmpfr) */
|
|
21
27
|
const LN_SQRT_2PI_DD_NEG = [-0.91893853320467278, 3.8782941580672414e-17];
|
|
@@ -288,6 +294,7 @@ export function qt1(p, df, lowerTail, logP) {
|
|
|
288
294
|
* @category Distributions
|
|
289
295
|
*/
|
|
290
296
|
export function dt(x, df, options = {}) {
|
|
297
|
+
checkOptions(options, DT_KEYS, "dt"); // generated: option-keys.mjs
|
|
291
298
|
const { log = false, ncp } = options;
|
|
292
299
|
if (ncp !== undefined && ncp !== 0)
|
|
293
300
|
return vectorize(x, (v) => dnt1(v, df, ncp, log));
|
|
@@ -312,6 +319,7 @@ export function dt(x, df, options = {}) {
|
|
|
312
319
|
* @category Distributions
|
|
313
320
|
*/
|
|
314
321
|
export function pt(q, df, options = {}) {
|
|
322
|
+
checkOptions(options, PT_KEYS, "pt"); // generated: option-keys.mjs
|
|
315
323
|
const { lowerTail = true, logP = false, ncp } = options;
|
|
316
324
|
if (ncp !== undefined && ncp !== 0)
|
|
317
325
|
return vectorize(q, (v) => pnt1(v, df, ncp, lowerTail, logP));
|
|
@@ -336,6 +344,7 @@ export function pt(q, df, options = {}) {
|
|
|
336
344
|
* @category Distributions
|
|
337
345
|
*/
|
|
338
346
|
export function qt(p, df, options = {}) {
|
|
347
|
+
checkOptions(options, QT_KEYS, "qt"); // generated: option-keys.mjs
|
|
339
348
|
const { lowerTail = true, logP = false, ncp } = options;
|
|
340
349
|
if (ncp !== undefined && ncp !== 0)
|
|
341
350
|
return vectorize(p, (v) => qnt1(v, df, ncp, lowerTail, logP));
|
|
@@ -7,6 +7,13 @@
|
|
|
7
7
|
// of lengths (each at most 1), so that wide and narrow intervals neither overflow nor underflow on the way.
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
10
|
+
import { midpoint } from "../internal/extremes.js";
|
|
11
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
12
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
13
|
+
const DTRIANG_KEYS = ["a", "b", "c", "log"];
|
|
14
|
+
const PTRIANG_KEYS = ["a", "b", "c", "lowerTail", "logP"];
|
|
15
|
+
const QTRIANG_KEYS = ["a", "b", "c", "lowerTail", "logP"];
|
|
16
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
10
17
|
const MIN_NORMAL = 2 ** -1022;
|
|
11
18
|
const valid = (a, b, c) => Number.isFinite(a) && Number.isFinite(b) && a < b && a <= c && c <= b;
|
|
12
19
|
/** 1/2 when b - a overflows (x, a, b and c are all halved then; the probabilities do not change), else 1 */
|
|
@@ -110,20 +117,24 @@ export function qtriang1(p, a, b, c, lowerTail, logP) {
|
|
|
110
117
|
// put x at the mode: qtriang(1.6e-8, { a: 0, b: 1.03e-316, c: 8.7e-317, lowerTail: false }))
|
|
111
118
|
const cl = (c - a) / w;
|
|
112
119
|
const cr = (b - c) / w;
|
|
120
|
+
// For a mode at an end the distance from it is w times the tail given (c = a: the lower one), which was 0 where that
|
|
121
|
+
// tail, exp(p), underflows: w exp(p / 2)^2 then, exp(p / 2) being normal (qtriang(-1000, { a: 0, b: 1e308, c: 0,
|
|
122
|
+
// logP: true }) was 0 for 2.5e-127; issue #121)
|
|
123
|
+
const tinyTail = logP && pv < 2 ** -1022;
|
|
113
124
|
let x;
|
|
114
125
|
if (cl > 0 && lower <= cl) {
|
|
115
126
|
const t = lower / cl;
|
|
116
127
|
x =
|
|
117
128
|
t <= 0.5
|
|
118
129
|
? a + (lowerTail ? sqrtP : Math.sqrt(lower)) * Math.sqrt(w) * Math.sqrt(c - a)
|
|
119
|
-
: c - ((c - a) * ((upper - cr) / cl)) / (1 + Math.sqrt(t));
|
|
130
|
+
: c - (cr === 0 && !lowerTail && tinyTail ? w * sqrtP * sqrtP : (c - a) * ((upper - cr) / cl)) / (1 + Math.sqrt(t));
|
|
120
131
|
}
|
|
121
132
|
else {
|
|
122
133
|
const t = upper / cr;
|
|
123
134
|
x =
|
|
124
135
|
t <= 0.5
|
|
125
136
|
? b - (lowerTail ? Math.sqrt(upper) : sqrtP) * Math.sqrt(w) * Math.sqrt(b - c)
|
|
126
|
-
: c + ((b - c) * ((lower - cl) / cr)) / (1 + Math.sqrt(t));
|
|
137
|
+
: c + (cl === 0 && lowerTail && tinyTail ? w * sqrtP * sqrtP : (b - c) * ((lower - cl) / cr)) / (1 + Math.sqrt(t));
|
|
127
138
|
}
|
|
128
139
|
// Rounding must not take x out of the support
|
|
129
140
|
return Math.min(Math.max(x, a), b);
|
|
@@ -143,8 +154,9 @@ export function qtriang1(p, a, b, c, lowerTail, logP) {
|
|
|
143
154
|
* @category Distributions
|
|
144
155
|
*/
|
|
145
156
|
export function dtriang(x, options = {}) {
|
|
157
|
+
checkOptions(options, DTRIANG_KEYS, "dtriang"); // generated: option-keys.mjs
|
|
146
158
|
const { a = -1, b = 1, log = false } = options;
|
|
147
|
-
const c = options.c ?? (a
|
|
159
|
+
const c = options.c ?? midpoint(a, b);
|
|
148
160
|
return vectorize(x, (v) => dtriang1(v, a, b, c, log));
|
|
149
161
|
}
|
|
150
162
|
/**
|
|
@@ -159,8 +171,9 @@ export function dtriang(x, options = {}) {
|
|
|
159
171
|
* @category Distributions
|
|
160
172
|
*/
|
|
161
173
|
export function ptriang(q, options = {}) {
|
|
174
|
+
checkOptions(options, PTRIANG_KEYS, "ptriang"); // generated: option-keys.mjs
|
|
162
175
|
const { a = -1, b = 1, lowerTail = true, logP = false } = options;
|
|
163
|
-
const c = options.c ?? (a
|
|
176
|
+
const c = options.c ?? midpoint(a, b);
|
|
164
177
|
return vectorize(q, (v) => ptriang1(v, a, b, c, lowerTail, logP));
|
|
165
178
|
}
|
|
166
179
|
/**
|
|
@@ -175,7 +188,8 @@ export function ptriang(q, options = {}) {
|
|
|
175
188
|
* @category Distributions
|
|
176
189
|
*/
|
|
177
190
|
export function qtriang(p, options = {}) {
|
|
191
|
+
checkOptions(options, QTRIANG_KEYS, "qtriang"); // generated: option-keys.mjs
|
|
178
192
|
const { a = -1, b = 1, lowerTail = true, logP = false } = options;
|
|
179
|
-
const c = options.c ?? (a
|
|
193
|
+
const c = options.c ?? midpoint(a, b);
|
|
180
194
|
return vectorize(p, (v) => qtriang1(v, a, b, c, lowerTail, logP));
|
|
181
195
|
}
|
|
@@ -23,6 +23,10 @@ import { vectorize } from "../internal/vectorize.js";
|
|
|
23
23
|
import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
24
24
|
import { stirlerr } from "../special/loader.js";
|
|
25
25
|
import { pnorm } from "./normal.js";
|
|
26
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
27
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
28
|
+
const PTUKEY_KEYS = ["nranges", "lowerTail", "logP"];
|
|
29
|
+
const QTUKEY_KEYS = ["nranges", "lowerTail", "logP"];
|
|
26
30
|
// ---- Clenshaw-Curtis rule (convergence judged from the Chebyshev coefficients) ----
|
|
27
31
|
//
|
|
28
32
|
// Evaluate f on [a, b] at the Chebyshev points x_j = cos(jπ/N) (j = 0..N), compute the Chebyshev
|
|
@@ -475,8 +479,11 @@ function logRangeCdf(w, k) {
|
|
|
475
479
|
* (the range exceeds w essentially only when a single pair differs by more than w; the correction is below e^-833)
|
|
476
480
|
*/
|
|
477
481
|
function logRangeCdfC(w, k) {
|
|
478
|
-
if (w > 100)
|
|
479
|
-
|
|
482
|
+
if (w > 100) {
|
|
483
|
+
// the number of pairs overflows above k ≈ 1.3e154: the sum of the logs only then (the same bits below)
|
|
484
|
+
const pairs = k * (k - 1);
|
|
485
|
+
return (Number.isFinite(pairs) ? Math.log(pairs) : Math.log(k) + Math.log(k - 1)) + logPhiC(w / Math.SQRT2);
|
|
486
|
+
}
|
|
480
487
|
const lf = (z) => {
|
|
481
488
|
const lP = logPhi(z);
|
|
482
489
|
const lt = logPhiShift(z, w) - lP; // log(a / P)
|
|
@@ -1027,6 +1034,7 @@ function brent(f, a, b, fa, fb) {
|
|
|
1027
1034
|
* @category Distributions
|
|
1028
1035
|
*/
|
|
1029
1036
|
export function ptukey(q, nmeans, df, options = {}) {
|
|
1037
|
+
checkOptions(options, PTUKEY_KEYS, "ptukey"); // generated: option-keys.mjs
|
|
1030
1038
|
const { nranges = 1, lowerTail = true, logP = false } = options;
|
|
1031
1039
|
return vectorize(q, (v) => ptukey1(v, nmeans, df, nranges, lowerTail, logP));
|
|
1032
1040
|
}
|
|
@@ -1050,6 +1058,7 @@ export function ptukey(q, nmeans, df, options = {}) {
|
|
|
1050
1058
|
* @category Distributions
|
|
1051
1059
|
*/
|
|
1052
1060
|
export function qtukey(p, nmeans, df, options = {}) {
|
|
1061
|
+
checkOptions(options, QTUKEY_KEYS, "qtukey"); // generated: option-keys.mjs
|
|
1053
1062
|
const { nranges = 1, lowerTail = true, logP = false } = options;
|
|
1054
1063
|
return vectorize(p, (v) => qtukey1(v, nmeans, df, nranges, lowerTail, logP));
|
|
1055
1064
|
}
|
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
// Formulas follow the definition. Argument handling (min = max, infinite ends, out of range) is matched to R's output.
|
|
4
4
|
import { vectorize } from "../internal/vectorize.js";
|
|
5
5
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
6
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
7
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
8
|
+
const DUNIF_KEYS = ["min", "max", "log"];
|
|
9
|
+
const PUNIF_KEYS = ["min", "max", "lowerTail", "logP"];
|
|
10
|
+
const QUNIF_KEYS = ["min", "max", "lowerTail", "logP"];
|
|
6
11
|
/** When max - min overflows (e.g. min = -1e308, max = 1e308), both ends are halved before computing. This is that factor */
|
|
7
12
|
function halfIfOverflow(min, max) {
|
|
8
13
|
return Number.isFinite(max - min) ? 1 : 0.5;
|
|
@@ -76,6 +81,7 @@ export function qunif1(p, min, max, lowerTail, logP) {
|
|
|
76
81
|
* @category Distributions
|
|
77
82
|
*/
|
|
78
83
|
export function dunif(x, options = {}) {
|
|
84
|
+
checkOptions(options, DUNIF_KEYS, "dunif"); // generated: option-keys.mjs
|
|
79
85
|
const { min = 0, max = 1, log = false } = options;
|
|
80
86
|
return vectorize(x, (v) => dunif1(v, min, max, log));
|
|
81
87
|
}
|
|
@@ -95,6 +101,7 @@ export function dunif(x, options = {}) {
|
|
|
95
101
|
* @category Distributions
|
|
96
102
|
*/
|
|
97
103
|
export function punif(q, options = {}) {
|
|
104
|
+
checkOptions(options, PUNIF_KEYS, "punif"); // generated: option-keys.mjs
|
|
98
105
|
const { min = 0, max = 1, lowerTail = true, logP = false } = options;
|
|
99
106
|
return vectorize(q, (v) => punif1(v, min, max, lowerTail, logP));
|
|
100
107
|
}
|
|
@@ -114,6 +121,7 @@ export function punif(q, options = {}) {
|
|
|
114
121
|
* @category Distributions
|
|
115
122
|
*/
|
|
116
123
|
export function qunif(p, options = {}) {
|
|
124
|
+
checkOptions(options, QUNIF_KEYS, "qunif"); // generated: option-keys.mjs
|
|
117
125
|
const { min = 0, max = 1, lowerTail = true, logP = false } = options;
|
|
118
126
|
return vectorize(p, (v) => qunif1(v, min, max, lowerTail, logP));
|
|
119
127
|
}
|
|
@@ -9,6 +9,11 @@ import { vectorize } from "../internal/vectorize.js";
|
|
|
9
9
|
import { d0, dt0, quantileBoundaries } from "../internal/dpq.js";
|
|
10
10
|
import { ddExp, expNeg, expNegOver, log1mExp, logOneMinusExpNeg, standardizeDD } from "../internal/ddexp.js";
|
|
11
11
|
import { ddDivNum, ddLog, ddMulNum, ddSub } from "../special/dd.js";
|
|
12
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
13
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
14
|
+
const DWEIBULL_KEYS = ["scale", "log"];
|
|
15
|
+
const PWEIBULL_KEYS = ["scale", "lowerTail", "logP"];
|
|
16
|
+
const QWEIBULL_KEYS = ["scale", "lowerTail", "logP"];
|
|
12
17
|
/** 1^y = 1, as with C's pow (JS Math.pow(1, Infinity) is NaN) */
|
|
13
18
|
function cpow(a, b) {
|
|
14
19
|
return a === 1 ? 1 : Math.pow(a, b);
|
|
@@ -34,6 +39,12 @@ function mulLarge(a, s) {
|
|
|
34
39
|
}
|
|
35
40
|
/** log(a / b) for positive finite a, b, also when a / b over- or underflows */
|
|
36
41
|
function logRatio(a, b) {
|
|
42
|
+
// a and b within 2^-20 of each other: log1p of their exact difference (log(a / b) took the rounding of the ratio,
|
|
43
|
+
// half an ulp of 1, for its value of the order of the difference: dweibull(2^52 - 0.5, 2^52, { scale: 2^52 - 0.5,
|
|
44
|
+
// log: true }) was an ulp off, as R; issue #111). Elsewhere as R
|
|
45
|
+
const d = a - b;
|
|
46
|
+
if (Math.abs(d) <= b * 2 ** -20)
|
|
47
|
+
return Math.log1p(d / b);
|
|
37
48
|
const r = a / b;
|
|
38
49
|
return r >= MIN_NORMAL && r < Infinity ? Math.log(r) : Math.log(a) - Math.log(b);
|
|
39
50
|
}
|
|
@@ -50,6 +61,10 @@ export function dweibull1(x, shape, scale, log) {
|
|
|
50
61
|
if (x === 0 && shape < 1)
|
|
51
62
|
return Infinity;
|
|
52
63
|
if (!allFinite(shape, scale)) {
|
|
64
|
+
// scale = Inf moves all the probability out to infinity: the density at a finite x > 0 is 0 in the limit (shape
|
|
65
|
+
// (x / scale)^(shape - 1) / scale -> 0), where the formula gives Inf * 0 = NaN (as R does)
|
|
66
|
+
if (scale === Infinity && x > 0)
|
|
67
|
+
return d0(log);
|
|
53
68
|
// The defining formula as is
|
|
54
69
|
const t1 = cpow(x / scale, shape - 1);
|
|
55
70
|
const t2 = t1 * (x / scale);
|
|
@@ -159,6 +174,7 @@ export function qweibull1(p, shape, scale, lowerTail, logP) {
|
|
|
159
174
|
* @category Distributions
|
|
160
175
|
*/
|
|
161
176
|
export function dweibull(x, shape, options = {}) {
|
|
177
|
+
checkOptions(options, DWEIBULL_KEYS, "dweibull"); // generated: option-keys.mjs
|
|
162
178
|
const { scale = 1, log = false } = options;
|
|
163
179
|
return vectorize(x, (v) => dweibull1(v, shape, scale, log));
|
|
164
180
|
}
|
|
@@ -179,6 +195,7 @@ export function dweibull(x, shape, options = {}) {
|
|
|
179
195
|
* @category Distributions
|
|
180
196
|
*/
|
|
181
197
|
export function pweibull(q, shape, options = {}) {
|
|
198
|
+
checkOptions(options, PWEIBULL_KEYS, "pweibull"); // generated: option-keys.mjs
|
|
182
199
|
const { scale = 1, lowerTail = true, logP = false } = options;
|
|
183
200
|
return vectorize(q, (v) => pweibull1(v, shape, scale, lowerTail, logP));
|
|
184
201
|
}
|
|
@@ -199,6 +216,7 @@ export function pweibull(q, shape, options = {}) {
|
|
|
199
216
|
* @category Distributions
|
|
200
217
|
*/
|
|
201
218
|
export function qweibull(p, shape, options = {}) {
|
|
219
|
+
checkOptions(options, QWEIBULL_KEYS, "qweibull"); // generated: option-keys.mjs
|
|
202
220
|
const { scale = 1, lowerTail = true, logP = false } = options;
|
|
203
221
|
return vectorize(p, (v) => qweibull1(v, shape, scale, lowerTail, logP));
|
|
204
222
|
}
|
|
@@ -8,7 +8,9 @@ export declare function qwilcox1(p: number, m: number, n: number, lowerTail: boo
|
|
|
8
8
|
*
|
|
9
9
|
* Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
|
|
10
10
|
*
|
|
11
|
-
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated
|
|
11
|
+
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
|
|
12
|
+
* 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
|
|
13
|
+
* center for m = n >= 550 or so; the tails stay available).
|
|
12
14
|
*
|
|
13
15
|
* @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
|
|
14
16
|
* @param m - Size of the first sample.
|
|
@@ -30,7 +32,9 @@ export declare function dwilcox<T extends Vectorizable>(x: T, m: number, n: numb
|
|
|
30
32
|
*
|
|
31
33
|
* Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
|
|
32
34
|
*
|
|
33
|
-
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated
|
|
35
|
+
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
|
|
36
|
+
* 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
|
|
37
|
+
* center for m = n >= 550 or so; the tails stay available).
|
|
34
38
|
*
|
|
35
39
|
* @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
|
|
36
40
|
* @param m - Size of the first sample.
|
|
@@ -52,7 +56,9 @@ export declare function pwilcox<T extends Vectorizable>(q: T, m: number, n: numb
|
|
|
52
56
|
*
|
|
53
57
|
* Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
|
|
54
58
|
*
|
|
55
|
-
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated
|
|
59
|
+
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
|
|
60
|
+
* 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
|
|
61
|
+
* center for m = n >= 550 or so; the tails stay available).
|
|
56
62
|
*
|
|
57
63
|
* @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
|
|
58
64
|
* @param m - Size of the first sample.
|
|
@@ -15,15 +15,43 @@
|
|
|
15
15
|
// m = n = 150. The coefficients are kept in double-double (within 2.2e-16 relative of exact BigInt values up to
|
|
16
16
|
// m = n = 300). Each step multiplies by i / (n + i) to stay on the probability scale (no division by
|
|
17
17
|
// C(m+n, m) needed), and everything is scaled by 2^960 (so tail probabilities below 1e-308 are less likely to underflow).
|
|
18
|
+
// Where the largest entry of a pass falls below 2^760 (tables of the lower tail only, for large m and n), the table is
|
|
19
|
+
// multiplied by 2^200 (exact) and its scale kept, as for signrank: P[W = 0] = 1 / C(2100, 1050), about 1e-630,
|
|
20
|
+
// underflowed at the fixed scale (dwilcox(0, 1050, 1050, log = TRUE) was -Inf).
|
|
21
|
+
//
|
|
22
|
+
// Above the center of each pass the entries drift far from their true values (the cumulative sums amplify the
|
|
23
|
+
// errors pass after pass); the drift cancels in the final values as long as the entries stay representable, but beyond
|
|
24
|
+
// about m = n = 510 they overflow near the center. The entries below the first that overflows keep their accuracy
|
|
25
|
+
// (each depends only on those below it), so the table ends there and reading beyond it is a RangeError. Copying the
|
|
26
|
+
// lower side onto the upper one (the passes are symmetric) breaks the cancellation and loses accuracy (3.5e-8 at the
|
|
27
|
+
// center for m = n = 400); a method stable for large samples near the center remains to be found.
|
|
18
28
|
import { vectorize } from "../internal/vectorize.js";
|
|
19
29
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
20
30
|
import { discreteQuantile } from "../internal/discrete.js";
|
|
21
|
-
import { nonInt, roundHalfEven } from "../internal/integer.js";
|
|
31
|
+
import { countAtMost, nonInt, roundHalfEven } from "../internal/integer.js";
|
|
22
32
|
import { qnorm } from "./normal.js";
|
|
33
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
34
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
35
|
+
const DWILCOX_KEYS = ["log"];
|
|
36
|
+
const PWILCOX_KEYS = ["lowerTail", "logP"];
|
|
37
|
+
const QWILCOX_KEYS = ["lowerTail", "logP"];
|
|
38
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
23
39
|
/** Scale 2^STORE_SCALE applied to the coefficients (small enough that the 2^27 factor of Veltkamp splitting does not overflow) */
|
|
24
40
|
const STORE_SCALE = 960;
|
|
41
|
+
/** Largest table entry the passes can take (2^27 + 1 times it, in Veltkamp splitting, must not overflow) */
|
|
42
|
+
const MAX_ENTRY = 2 ** 996;
|
|
25
43
|
/** Maximum table length (computations needing more throw RangeError; about 800 MB for the three arrays during construction) */
|
|
26
44
|
const MAX_LEN = 2 ** 25;
|
|
45
|
+
/**
|
|
46
|
+
* Maximum work of a table, its length times the m passes (m <= n): about 10 to 15 ns per entry and pass on the
|
|
47
|
+
* development machine, so about 6 to 8 s (pwilcox(2e5, 3000, 3000) took 5 s; m = n = 1e5 at q = 1e6 about 20 minutes)
|
|
48
|
+
*/
|
|
49
|
+
const MAX_WORK = 2 ** 29;
|
|
50
|
+
/** Where the largest entry of a pass falls below RESCALE_BELOW, the table is multiplied by 2^RESCALE_STEP */
|
|
51
|
+
const RESCALE_BELOW = 2 ** 760;
|
|
52
|
+
const RESCALE_STEP = 200;
|
|
53
|
+
/** An entry below this has lost digits (its low part, 2^-53 of it, is below the smallest normal number) */
|
|
54
|
+
const LOST_BELOW = 2 ** -969;
|
|
27
55
|
const SPLITTER = 134217729; // 2^27 + 1
|
|
28
56
|
/**
|
|
29
57
|
* Tables are cached per (m, n). When the total number of entries exceeds CACHE_BUDGET, the oldest are evicted
|
|
@@ -37,11 +65,19 @@ function buildTable(m, n, J) {
|
|
|
37
65
|
if (J + 1 > MAX_LEN) {
|
|
38
66
|
throw new RangeError(`wilcox: m = ${m}, n = ${n} needs too large a table (${J + 1} entries)`);
|
|
39
67
|
}
|
|
68
|
+
if (m * (J + 1) > MAX_WORK) {
|
|
69
|
+
throw new RangeError(`wilcox: m = ${m}, n = ${n} needs ${m * (J + 1)} steps for a table of ${J + 1} entries (at most 2^29)`);
|
|
70
|
+
}
|
|
40
71
|
const H = new Float64Array(J + 1);
|
|
41
72
|
const L = new Float64Array(J + 1);
|
|
42
73
|
H[0] = 2 ** STORE_SCALE;
|
|
74
|
+
let scale = STORE_SCALE;
|
|
75
|
+
// scale at the first pass where an entry lost digits
|
|
76
|
+
let lossScale = NaN;
|
|
77
|
+
// entries from end on broke down (see below) and are no longer computed
|
|
78
|
+
let end = J + 1;
|
|
43
79
|
for (let i = 1; i <= m; i++) {
|
|
44
|
-
|
|
80
|
+
let top = Math.min(end - 1, i * n);
|
|
45
81
|
// Multiply by i / (n + i) (double-double; i is an integer, so the product splits exactly)
|
|
46
82
|
const d = n + i;
|
|
47
83
|
let t = SPLITTER * i;
|
|
@@ -54,6 +90,16 @@ function buildTable(m, n, J) {
|
|
|
54
90
|
const a = H[j];
|
|
55
91
|
if (a === 0)
|
|
56
92
|
continue;
|
|
93
|
+
// Above the center of a pass the entries are far off the true (small) values: the errors of the cumulative sums
|
|
94
|
+
// grow pass after pass, and cancel only in the final values, as long as they stay representable (m = n = 500,
|
|
95
|
+
// the table to the center: up to 2^990.7, the results within 1e-15 of the exact ones). Beyond 2^996 the splitting
|
|
96
|
+
// below overflows and the values become NaN (m = n >= 550 near the center). Each entry depends only on those
|
|
97
|
+
// below it, so the entries below j keep their accuracy: the table ends there (reading beyond it is an error)
|
|
98
|
+
if (!(Math.abs(a) <= MAX_ENTRY)) {
|
|
99
|
+
end = j;
|
|
100
|
+
top = j - 1;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
57
103
|
// (a + L[j]) × i
|
|
58
104
|
const ph = a * i;
|
|
59
105
|
t = SPLITTER * a;
|
|
@@ -71,6 +117,8 @@ function buildTable(m, n, J) {
|
|
|
71
117
|
const s = q1 + q2;
|
|
72
118
|
L[j] = q2 - (s - q1);
|
|
73
119
|
H[j] = s;
|
|
120
|
+
if (Math.abs(s) < LOST_BELOW && Number.isNaN(lossScale))
|
|
121
|
+
lossScale = scale;
|
|
74
122
|
}
|
|
75
123
|
// Multiply by (1 - q^{n+i}): in descending order P[j] -= P[j - n - i] (the right side still holds old values)
|
|
76
124
|
const sh = n + i;
|
|
@@ -79,13 +127,28 @@ function buildTable(m, n, J) {
|
|
|
79
127
|
// Divide by (1 - q^i): in ascending order P[j] += P[j - i] (the right side already holds new values)
|
|
80
128
|
for (let j = i; j <= top; j++)
|
|
81
129
|
ddAddInto(H, L, j, j - i, 1);
|
|
130
|
+
// The coefficients of a pass are unimodal: keep the largest within range (exact powers of 2)
|
|
131
|
+
const center = Math.min(top, Math.floor((i * n) / 2));
|
|
132
|
+
while (center >= 0 && H[center] > 0 && H[center] < RESCALE_BELOW) {
|
|
133
|
+
const f = 2 ** RESCALE_STEP;
|
|
134
|
+
for (let j = 0; j <= top; j++) {
|
|
135
|
+
H[j] *= f;
|
|
136
|
+
L[j] *= f;
|
|
137
|
+
}
|
|
138
|
+
scale += RESCALE_STEP;
|
|
139
|
+
}
|
|
82
140
|
}
|
|
83
141
|
const cum = new Float64Array(J + 1);
|
|
84
142
|
let ch = 0;
|
|
85
143
|
let cl = 0;
|
|
86
|
-
for (let j = 0; j
|
|
144
|
+
for (let j = 0; j < end; j++) {
|
|
87
145
|
// (ch, cl) += (H[j], L[j])
|
|
88
146
|
const a = H[j];
|
|
147
|
+
// (the last pass, which no multiplication checks)
|
|
148
|
+
if (!(Math.abs(a) <= MAX_ENTRY)) {
|
|
149
|
+
end = j;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
89
152
|
const s = ch + a;
|
|
90
153
|
const bb = s - ch;
|
|
91
154
|
let e = ch - (s - bb) + (a - bb);
|
|
@@ -94,7 +157,12 @@ function buildTable(m, n, J) {
|
|
|
94
157
|
cl = e - (ch - s);
|
|
95
158
|
cum[j] = ch;
|
|
96
159
|
}
|
|
97
|
-
|
|
160
|
+
// The loss of an entry (below 2^-1022 absolute, at the scale of its pass) is propagated by the later passes, which
|
|
161
|
+
// amplify relative errors by up to about 2^53 (the accuracy of the double-double table); 2^60 more for a margin
|
|
162
|
+
const minTrusted = Number.isNaN(lossScale)
|
|
163
|
+
? 0
|
|
164
|
+
: 2 ** Math.min(1023, -1022 + 113 + Math.ceil(Math.log2(m * (J + 1))) + scale - lossScale);
|
|
165
|
+
return { m, n, len: J + 1, prob: H, cum, scale, minTrusted, valid: end };
|
|
98
166
|
}
|
|
99
167
|
/** (H[j], L[j]) += sign × (H[k], L[k]) */
|
|
100
168
|
function ddAddInto(H, L, j, k, sign) {
|
|
@@ -108,11 +176,9 @@ function ddAddInto(H, L, j, k, sign) {
|
|
|
108
176
|
L[j] = e - (r - s);
|
|
109
177
|
H[j] = r;
|
|
110
178
|
}
|
|
111
|
-
/** Table for (m, n) usable for s <= J */
|
|
112
|
-
function table(m, n, J) {
|
|
113
|
-
|
|
114
|
-
[m, n] = [n, m];
|
|
115
|
-
const key = `${m},${n}`;
|
|
179
|
+
/** Table for (m, n) (m <= n) usable for s <= J; with low, the low table (see tableValue) */
|
|
180
|
+
function table(m, n, J, low = false) {
|
|
181
|
+
const key = `${m},${n}${low ? ",low" : ""}`;
|
|
116
182
|
const old = cache.get(key);
|
|
117
183
|
if (old !== undefined && old.len > J) {
|
|
118
184
|
cache.delete(key);
|
|
@@ -120,9 +186,14 @@ function table(m, n, J) {
|
|
|
120
186
|
return old;
|
|
121
187
|
}
|
|
122
188
|
const half = Math.floor((m * n) / 2);
|
|
123
|
-
// Growing a little at a time would cause repeated rebuilds, so double the size
|
|
124
|
-
|
|
125
|
-
const
|
|
189
|
+
// Growing a little at a time would cause repeated rebuilds, so double the size (not beyond MAX_WORK, where s <= J
|
|
190
|
+
// alone is within it)
|
|
191
|
+
const grown = old === undefined ? 0 : Math.min(2 * old.len, Math.floor(MAX_WORK / m) - 1);
|
|
192
|
+
const want = Math.min(half, Math.max(J, grown));
|
|
193
|
+
return store(key, buildTable(m, n, want), old);
|
|
194
|
+
}
|
|
195
|
+
/** Put t in the cache under key (replacing old) and evict the oldest tables beyond the budget */
|
|
196
|
+
function store(key, t, old) {
|
|
126
197
|
if (old !== undefined) {
|
|
127
198
|
cache.delete(key);
|
|
128
199
|
cached -= old.len;
|
|
@@ -137,12 +208,60 @@ function table(m, n, J) {
|
|
|
137
208
|
}
|
|
138
209
|
return t;
|
|
139
210
|
}
|
|
140
|
-
|
|
141
|
-
|
|
211
|
+
/**
|
|
212
|
+
* The probability (cum false) or cumulative probability (cum true) at s for (m, n), as a table value with its table.
|
|
213
|
+
* A table built far beyond s may have lost the digits of its low entries (it was rescaled for its large ones), so a
|
|
214
|
+
* value outside the bound of buildTable is read from the low table of (m, n) instead, built only as far as needed,
|
|
215
|
+
* and failing that from a table rebuilt up to s: its last entry is the largest of the last pass.
|
|
216
|
+
*/
|
|
217
|
+
function tableValue(m, n, s, cum) {
|
|
218
|
+
if (m > n)
|
|
219
|
+
[m, n] = [n, m];
|
|
220
|
+
const read = (t) => {
|
|
221
|
+
if (s >= t.valid) {
|
|
222
|
+
throw new RangeError(`wilcox: the exact distribution for m = ${m}, n = ${n} cannot be computed accurately at ${s} (from ${t.valid} on, near the center of large samples)`);
|
|
223
|
+
}
|
|
224
|
+
return (cum ? t.cum : t.prob)[s];
|
|
225
|
+
};
|
|
226
|
+
const trusted = (t, v) => s === t.len - 1 || v >= (cum ? (s + 1) * t.minTrusted : t.minTrusted);
|
|
227
|
+
let t = table(m, n, s);
|
|
228
|
+
let v = read(t);
|
|
229
|
+
if (trusted(t, v))
|
|
230
|
+
return [v, t];
|
|
231
|
+
t = table(m, n, s, true);
|
|
232
|
+
v = read(t);
|
|
233
|
+
if (trusted(t, v))
|
|
234
|
+
return [v, t];
|
|
235
|
+
const key = `${m},${n},low`;
|
|
236
|
+
t = store(key, buildTable(m, n, s), cache.get(key));
|
|
237
|
+
return [read(t), t];
|
|
238
|
+
}
|
|
239
|
+
/** ln 2 in two parts, the first with 32 trailing zero bits (k ln 2 exact in the first part for abs(k) < 2^20) */
|
|
240
|
+
const LN2_HI = 6.93147180369123816490e-1;
|
|
241
|
+
const LN2_LO = 1.90821492927058770002e-10;
|
|
242
|
+
/** The probability v 2^-scale of the table value v (underflow only at the final step) */
|
|
243
|
+
function toProb(v, t, log) {
|
|
244
|
+
const e = -t.scale;
|
|
245
|
+
const e1 = Math.trunc(e / 2);
|
|
246
|
+
const p = v * 2 ** e1 * 2 ** (e - e1);
|
|
142
247
|
if (!log)
|
|
143
248
|
return p;
|
|
144
|
-
|
|
145
|
-
|
|
249
|
+
if (p >= 1e-300)
|
|
250
|
+
return Math.log(p);
|
|
251
|
+
// Otherwise from the significand m in [1, 2) and the exponent k of v: log m + (k + e) log 2, the last in two parts
|
|
252
|
+
// (log(v) - scale log 2 lost digits to the cancellation of large terms), the same bits whatever the table's scale
|
|
253
|
+
let k = Math.floor(Math.log2(v));
|
|
254
|
+
const k1 = Math.trunc(k / 2);
|
|
255
|
+
let mv = v * 2 ** -k1 * 2 ** -(k - k1);
|
|
256
|
+
if (mv >= 2) {
|
|
257
|
+
mv /= 2;
|
|
258
|
+
k++;
|
|
259
|
+
}
|
|
260
|
+
else if (mv < 1) {
|
|
261
|
+
mv *= 2;
|
|
262
|
+
k--;
|
|
263
|
+
}
|
|
264
|
+
return (k + e) * LN2_HI + (Math.log(mv) + (k + e) * LN2_LO);
|
|
146
265
|
}
|
|
147
266
|
/** Check and round m, n. undefined if invalid */
|
|
148
267
|
function wilcoxParams(m, n) {
|
|
@@ -150,7 +269,13 @@ function wilcoxParams(m, n) {
|
|
|
150
269
|
return undefined;
|
|
151
270
|
m = roundHalfEven(m);
|
|
152
271
|
n = roundHalfEven(n);
|
|
153
|
-
|
|
272
|
+
// The statistic ranges over 0..mn: beyond 2^53 its values are not all doubles (NaN, as other integer supports past
|
|
273
|
+
// 2^53). This also bounds the smaller of m and n by 2^26.5, the number of passes of the table (dwilcox(0, 1e12,
|
|
274
|
+
// 1e12) ran 1e12 of them for a table of one entry). An exact product of 2^53 + 1 rounds down to 2^53 (and would be
|
|
275
|
+
// taken as the end of the support), so a rounded 2^53 is checked exactly
|
|
276
|
+
if (m <= 0 || n <= 0 || m * n > 2 ** 53)
|
|
277
|
+
return undefined;
|
|
278
|
+
return m * n === 2 ** 53 && BigInt(m) * BigInt(n) !== 2n ** 53n ? undefined : [m, n];
|
|
154
279
|
}
|
|
155
280
|
export function dwilcox1(x, m, n, log) {
|
|
156
281
|
if (Number.isNaN(x) || Number.isNaN(m) || Number.isNaN(n))
|
|
@@ -166,7 +291,8 @@ export function dwilcox1(x, m, n, log) {
|
|
|
166
291
|
if (x < 0 || x > total)
|
|
167
292
|
return d0(log);
|
|
168
293
|
const s = Math.min(x, total - x);
|
|
169
|
-
|
|
294
|
+
const [v, t] = tableValue(m, n, s, false);
|
|
295
|
+
return toProb(v, t, log);
|
|
170
296
|
}
|
|
171
297
|
/** P[W <= x] (lower) / P[W > x] (0 <= x < mn, x an integer) */
|
|
172
298
|
function pwilcoxRaw(x, m, n, lowerTail, logP) {
|
|
@@ -174,10 +300,13 @@ function pwilcoxRaw(x, m, n, lowerTail, logP) {
|
|
|
174
300
|
// P[W > x] = P[W <= mn - x - 1] (symmetry). Read the smaller tail directly from the table
|
|
175
301
|
const direct = lowerTail ? x : total - x - 1;
|
|
176
302
|
const other = lowerTail ? total - x - 1 : x;
|
|
177
|
-
if (direct <= other)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
303
|
+
if (direct <= other) {
|
|
304
|
+
const [v, t] = tableValue(m, n, direct, true);
|
|
305
|
+
return toProb(v, t, logP);
|
|
306
|
+
}
|
|
307
|
+
const [v, t] = tableValue(m, n, other, true);
|
|
308
|
+
const p = toProb(v, t, false);
|
|
309
|
+
return logP ? Math.log1p(-p) : 1 - p;
|
|
181
310
|
}
|
|
182
311
|
export function pwilcox1(q, m, n, lowerTail, logP) {
|
|
183
312
|
if (Number.isNaN(q) || Number.isNaN(m) || Number.isNaN(n))
|
|
@@ -186,7 +315,7 @@ export function pwilcox1(q, m, n, lowerTail, logP) {
|
|
|
186
315
|
if (params === undefined)
|
|
187
316
|
return NaN;
|
|
188
317
|
[m, n] = params;
|
|
189
|
-
const x =
|
|
318
|
+
const x = countAtMost(q);
|
|
190
319
|
if (x < 0)
|
|
191
320
|
return dt0(lowerTail, logP);
|
|
192
321
|
if (x >= m * n)
|
|
@@ -222,7 +351,9 @@ export function qwilcox1(p, m, n, lowerTail, logP) {
|
|
|
222
351
|
*
|
|
223
352
|
* Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
|
|
224
353
|
*
|
|
225
|
-
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated
|
|
354
|
+
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
|
|
355
|
+
* 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
|
|
356
|
+
* center for m = n >= 550 or so; the tails stay available).
|
|
226
357
|
*
|
|
227
358
|
* @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
|
|
228
359
|
* @param m - Size of the first sample.
|
|
@@ -239,6 +370,7 @@ export function qwilcox1(p, m, n, lowerTail, logP) {
|
|
|
239
370
|
* @category Distributions
|
|
240
371
|
*/
|
|
241
372
|
export function dwilcox(x, m, n, options = {}) {
|
|
373
|
+
checkOptions(options, DWILCOX_KEYS, "dwilcox"); // generated: option-keys.mjs
|
|
242
374
|
const log = options.log ?? false;
|
|
243
375
|
return vectorize(x, (v) => dwilcox1(v, m, n, log));
|
|
244
376
|
}
|
|
@@ -247,7 +379,9 @@ export function dwilcox(x, m, n, options = {}) {
|
|
|
247
379
|
*
|
|
248
380
|
* Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
|
|
249
381
|
*
|
|
250
|
-
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated
|
|
382
|
+
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
|
|
383
|
+
* 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
|
|
384
|
+
* center for m = n >= 550 or so; the tails stay available).
|
|
251
385
|
*
|
|
252
386
|
* @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
|
|
253
387
|
* @param m - Size of the first sample.
|
|
@@ -264,6 +398,7 @@ export function dwilcox(x, m, n, options = {}) {
|
|
|
264
398
|
* @category Distributions
|
|
265
399
|
*/
|
|
266
400
|
export function pwilcox(q, m, n, options = {}) {
|
|
401
|
+
checkOptions(options, PWILCOX_KEYS, "pwilcox"); // generated: option-keys.mjs
|
|
267
402
|
const { lowerTail = true, logP = false } = options;
|
|
268
403
|
return vectorize(q, (v) => pwilcox1(v, m, n, lowerTail, logP));
|
|
269
404
|
}
|
|
@@ -272,7 +407,9 @@ export function pwilcox(q, m, n, options = {}) {
|
|
|
272
407
|
*
|
|
273
408
|
* Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
|
|
274
409
|
*
|
|
275
|
-
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated
|
|
410
|
+
* @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
|
|
411
|
+
* 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
|
|
412
|
+
* center for m = n >= 550 or so; the tails stay available).
|
|
276
413
|
*
|
|
277
414
|
* @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
|
|
278
415
|
* @param m - Size of the first sample.
|
|
@@ -289,6 +426,7 @@ export function pwilcox(q, m, n, options = {}) {
|
|
|
289
426
|
* @category Distributions
|
|
290
427
|
*/
|
|
291
428
|
export function qwilcox(p, m, n, options = {}) {
|
|
429
|
+
checkOptions(options, QWILCOX_KEYS, "qwilcox"); // generated: option-keys.mjs
|
|
292
430
|
const { lowerTail = true, logP = false } = options;
|
|
293
431
|
return vectorize(p, (v) => qwilcox1(v, m, n, lowerTail, logP));
|
|
294
432
|
}
|