@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/internal/summary.js
CHANGED
|
@@ -9,14 +9,29 @@
|
|
|
9
9
|
// sum runs unscaled while it stays finite (so its results are unchanged), and once it would overflow it continues on
|
|
10
10
|
// the values scaled by a power of 2 (exact) and is scaled back at the end. A mean whose sum overflows a double is the
|
|
11
11
|
// mean of the scaled values, scaled back (R averages x[i] / n in that case, which gives the mean without overflow).
|
|
12
|
+
//
|
|
13
|
+
// Moments (deviations, variances, covariances, sums of squares) follow these rules, so that no statistic depends on
|
|
14
|
+
// the units of the data:
|
|
15
|
+
// 1. Scale first, centre afterwards: the data are divided by a power of 2, 2^-k, that brings the largest absolute
|
|
16
|
+
// value into [1, 2) (exact), and the mean, deviations and cross-products are computed on the scaled values. A
|
|
17
|
+
// deviation x - mean can then neither overflow (values near both ends of the range) nor underflow.
|
|
18
|
+
// 2. Quantities without units (t, F, chi-squared, correlations, effect sizes, ratios, degrees of freedom, p-values)
|
|
19
|
+
// are computed in the scaled coordinates.
|
|
20
|
+
// 3. Quantities with units are scaled back only for output: means, differences, standard errors and intervals by
|
|
21
|
+
// 2^k, sums of squares and variances by 2^2k (ldexp). Nothing without units is computed from scaled-back values.
|
|
22
|
+
// 4. In the ordinary range the results are those of the unscaled formulas (scaling by a power of 2 changes no bit).
|
|
23
|
+
// See centered, groupMoments, pairedDifferences, standardize and scaleToUnit.
|
|
12
24
|
import { ddAdd, ddDivNum, ddMul, ddSub } from "../special/dd.js";
|
|
25
|
+
import { isDoubleDouble } from "../special/dd.js";
|
|
26
|
+
import { roundScaled, splitDouble } from "../linalg/dot.js";
|
|
13
27
|
export function wideSum() {
|
|
14
28
|
return { s: [0, 0], shift: 0 };
|
|
15
29
|
}
|
|
16
30
|
/**
|
|
17
31
|
* Power of 2 by which the sums of n finite values are scaled once they overflow: the scaled sum stays below 2^992,
|
|
18
32
|
* so that it can also be divided in double-double (whose splitting overflows above about 2^996). Values below
|
|
19
|
-
* 2^(shift - 1022) lose bits when scaled, which
|
|
33
|
+
* 2^(shift - 1022) lose bits when scaled, which matters only where the large values later cancel (sum() and
|
|
34
|
+
* meanDD() bound such losses and then sum exactly, #374 and #473)
|
|
20
35
|
*/
|
|
21
36
|
function overflowShift(n) {
|
|
22
37
|
return 32 + Math.ceil(Math.log2(Math.max(n, 1)));
|
|
@@ -44,6 +59,20 @@ export function ldexp(v, k) {
|
|
|
44
59
|
const a = Math.trunc(k / 2);
|
|
45
60
|
return v * 2 ** a * 2 ** (k - a);
|
|
46
61
|
}
|
|
62
|
+
/** v 2^k for any integer k (beyond the +-2046 of ldexp), in steps of at most 2^1000 */
|
|
63
|
+
export function ldexpWide(v, k) {
|
|
64
|
+
let x = v;
|
|
65
|
+
let r = k;
|
|
66
|
+
while (r > 1000) {
|
|
67
|
+
x *= 2 ** 1000;
|
|
68
|
+
r -= 1000;
|
|
69
|
+
}
|
|
70
|
+
while (r < -1000) {
|
|
71
|
+
x *= 2 ** -1000;
|
|
72
|
+
r += 1000;
|
|
73
|
+
}
|
|
74
|
+
return x * 2 ** r;
|
|
75
|
+
}
|
|
47
76
|
/** Sum of values that are not all finite, as in double arithmetic (NaN propagates, Inf and -Inf give NaN) */
|
|
48
77
|
function nonFiniteSum(x) {
|
|
49
78
|
let s = 0;
|
|
@@ -58,23 +87,93 @@ export function meanDD(x) {
|
|
|
58
87
|
// With Inf present the double-double sum becomes NaN; the mean is the (infinite or NaN) sum divided by n
|
|
59
88
|
if (!x.every(Number.isFinite))
|
|
60
89
|
return [nonFiniteSum(x) / n, 0];
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
90
|
+
if (!isDoubleDouble()) {
|
|
91
|
+
const acc = wideSum();
|
|
92
|
+
for (const v of x)
|
|
93
|
+
wideSumAdd(acc, v, n);
|
|
94
|
+
return acc.shift !== 0 ? scaledMean(x) : meanFromSum(x, acc.s);
|
|
95
|
+
}
|
|
96
|
+
// The sum loses a third band of magnitudes as sum() did (#374): mean([1e308, 1e100, 1, -1e308, -1e100]) was 0 and
|
|
97
|
+
// mean([1e20, 1e3, 1e-10, -1e20, -1e3]) 2.0009e-11 (#473). With the bound on what the sum lost, the mean is formed
|
|
98
|
+
// exactly where that could move it across a rounding boundary; otherwise it has the bits of the double-double steps
|
|
99
|
+
const b = boundedSum(x);
|
|
100
|
+
const m = b.shift !== 0 ? scaledMean(x) : meanFromSum(x, [b.hi, b.lo]);
|
|
101
|
+
if (b.lost > 0 && mayMissMean(b, m, n))
|
|
102
|
+
return exactMean(x);
|
|
103
|
+
return m;
|
|
104
|
+
}
|
|
105
|
+
/** The mean of values whose sum overflows a double: the mean of the values scaled by a power of 2, scaled back */
|
|
106
|
+
function scaledMean(x) {
|
|
107
|
+
const k = overflowShift(x.length);
|
|
108
|
+
const f = 2 ** -k;
|
|
109
|
+
const m = meanDD(x.map((v) => v * f));
|
|
110
|
+
return [ldexp(m[0], k), ldexp(m[1], k)];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Whether the true mean may round to another double than m[0]: it lies within lost (1 + n 2^-52) / n of s / n for the
|
|
114
|
+
* double-double sum s = (hi + lo) 2^shift (plus the error of the division), and m lies within abs(m - s / n) of it.
|
|
115
|
+
* Computed in units of 2^shift; with shift != 0 also where m is near the subnormals there
|
|
116
|
+
*/
|
|
117
|
+
function mayMissMean(b, m, n) {
|
|
118
|
+
const r = m[0];
|
|
119
|
+
if (!Number.isFinite(r))
|
|
120
|
+
return true;
|
|
121
|
+
const rs = ldexp(r, -b.shift);
|
|
122
|
+
if (b.shift !== 0 && Math.abs(rs) < 2 ** -1000)
|
|
123
|
+
return true;
|
|
124
|
+
const q = ddDivNum([b.hi, b.lo], n);
|
|
125
|
+
const d = ddSub([rs, ldexp(m[1], -b.shift)], q);
|
|
126
|
+
const bound = ((b.lost * (1 + n * 2 ** -52)) / n + Math.abs(d[0] + d[1]) + Math.abs(q[0]) * 2 ** -100) * (1 + 2 ** -50);
|
|
127
|
+
return mayRoundOtherwise(rs, ldexp(m[1], -b.shift), rs, bound, 0);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The mean of finite values from their exact sum (integers times 2^-1074 in BigInt): the quotient by n to 128 bits
|
|
131
|
+
* beyond its leading bit, with a sticky bit for a remainder, rounded once for the high part and once more for the low
|
|
132
|
+
*/
|
|
133
|
+
function exactMean(x) {
|
|
134
|
+
let low = Infinity;
|
|
135
|
+
const parts = [];
|
|
136
|
+
for (const v of x) {
|
|
137
|
+
if (v === 0)
|
|
138
|
+
continue;
|
|
139
|
+
const p = splitDouble(v);
|
|
140
|
+
parts.push(p);
|
|
141
|
+
if (p[1] < low)
|
|
142
|
+
low = p[1];
|
|
143
|
+
}
|
|
144
|
+
let acc = 0n;
|
|
145
|
+
for (const [m, e] of parts)
|
|
146
|
+
acc += m << BigInt(e - low);
|
|
147
|
+
if (acc === 0n)
|
|
148
|
+
return [0, 0];
|
|
149
|
+
const nb = BigInt(x.length);
|
|
150
|
+
const extra = 128 + x.length.toString(2).length;
|
|
151
|
+
const num = acc << BigInt(extra);
|
|
152
|
+
let q = num / nb;
|
|
153
|
+
let e = low - extra;
|
|
154
|
+
if (num % nb !== 0n) {
|
|
155
|
+
q = 2n * q + (acc < 0n ? -1n : 1n);
|
|
156
|
+
e -= 1;
|
|
157
|
+
}
|
|
158
|
+
const hi = roundScaled(q, e);
|
|
159
|
+
if (!Number.isFinite(hi) || hi === 0)
|
|
160
|
+
return [hi, 0];
|
|
161
|
+
const [mh, eh] = splitDouble(hi);
|
|
162
|
+
return [hi, roundScaled(q - (mh << BigInt(eh - e)), e)];
|
|
163
|
+
}
|
|
164
|
+
/** The double-double mean from the double-double sum s of x (finite, not overflowing): s / n corrected by the deviations */
|
|
165
|
+
function meanFromSum(x, s) {
|
|
166
|
+
const n = x.length;
|
|
72
167
|
const m = ddDivNum(s, n);
|
|
73
168
|
if (!Number.isFinite(m[0]))
|
|
74
169
|
return [s[0] / n, 0];
|
|
75
170
|
let t = [0, 0];
|
|
76
171
|
for (const v of x)
|
|
77
172
|
t = ddAdd(t, ddSub([v, 0], m));
|
|
173
|
+
// A deviation v - m beyond the double range (values near both ends of it) leaves the correction infinite: m, the
|
|
174
|
+
// double-double quotient of the exact sum, is kept
|
|
175
|
+
if (!Number.isFinite(t[0]))
|
|
176
|
+
return m;
|
|
78
177
|
return ddAdd(m, ddDivNum(t, n));
|
|
79
178
|
}
|
|
80
179
|
export function mean(x) {
|
|
@@ -84,39 +183,186 @@ export function mean(x) {
|
|
|
84
183
|
export function variance(x) {
|
|
85
184
|
return covariance(x, x);
|
|
86
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Sample standard deviation from the scaled deviations: sqrt(sum d^2 / (n - 1)) in the coordinates 2^-k, times 2^k.
|
|
188
|
+
* The same bits as sqrt(variance(x)) where the variance is a normal number (the square root of 4^k y is 2^k sqrt(y)),
|
|
189
|
+
* and the right value where it overflows or underflows (sd of c(-1e308, -1e308, -1e308, -1e308, 1.5e308) is 1.118e308
|
|
190
|
+
* while its variance is 1.25e616). x contains no NaN
|
|
191
|
+
*/
|
|
192
|
+
export function standardDeviation(x) {
|
|
193
|
+
const n = x.length;
|
|
194
|
+
if (n < 2)
|
|
195
|
+
return NaN;
|
|
196
|
+
const c = centered(x);
|
|
197
|
+
if (c === undefined)
|
|
198
|
+
return NaN;
|
|
199
|
+
return ldexp(Math.sqrt(ddDivNum(crossSum(c.d, c.d), n - 1)[0]), c.k);
|
|
200
|
+
}
|
|
87
201
|
/** Unbiased covariance. x and y have the same length and contain no NaN */
|
|
88
202
|
export function covariance(x, y) {
|
|
89
203
|
const n = x.length;
|
|
90
204
|
if (n < 2)
|
|
91
205
|
return NaN;
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
if (
|
|
206
|
+
const cx = centered(x);
|
|
207
|
+
const cy = x === y ? cx : centered(y);
|
|
208
|
+
if (cx === undefined || cy === undefined)
|
|
95
209
|
return NaN;
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
210
|
+
const s = crossSum(cx.d, cy.d);
|
|
211
|
+
// Scaled back by 2^(kx + ky) at once: by 2^kx first could overflow (or underflow) where 2^ky would bring it back,
|
|
212
|
+
// and cov(x, y) would then differ from cov(y, x)
|
|
213
|
+
return ldexp(ddDivNum(s, n - 1)[0], cx.k + cy.k);
|
|
214
|
+
}
|
|
215
|
+
/** Binary exponent k that brings the largest finite absolute value into [1, 2) (0 when there is none) */
|
|
216
|
+
function unitExponent(groups) {
|
|
217
|
+
let m = 0;
|
|
218
|
+
for (const g of groups)
|
|
219
|
+
for (const v of g)
|
|
220
|
+
if (Number.isFinite(v))
|
|
221
|
+
m = Math.max(m, Math.abs(v));
|
|
222
|
+
return m === 0 ? 0 : Math.floor(Math.log2(m));
|
|
223
|
+
}
|
|
224
|
+
/** sum a[i] b[i] in double-double */
|
|
225
|
+
function crossSum(a, b) {
|
|
104
226
|
let s = [0, 0];
|
|
105
|
-
for (let i = 0; i <
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
227
|
+
for (let i = 0; i < a.length; i++)
|
|
228
|
+
s = ddAdd(s, ddMul(a[i], b[i]));
|
|
229
|
+
return s;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The deviations from the mean of x scaled by 2^-k (the scaling first, see the rules above), in double-double, with k
|
|
233
|
+
* and the scaled mean. undefined when x has an infinite value (the mean is not finite)
|
|
234
|
+
*/
|
|
235
|
+
export function centered(x) {
|
|
236
|
+
if (!x.every(Number.isFinite))
|
|
237
|
+
return undefined;
|
|
238
|
+
const k = unitExponent([x]);
|
|
239
|
+
const xs = k === 0 ? x : x.map((v) => ldexp(v, -k));
|
|
240
|
+
const mean = meanDD(xs);
|
|
241
|
+
return { d: xs.map((v) => ddSub([v, 0], mean)), k, mean };
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* (x - mean) / sd, from the scaled deviations (no overflow or underflow at any scale). NaN values when the standard
|
|
245
|
+
* deviation is 0 or x has fewer than 2 values or an infinite one
|
|
246
|
+
*/
|
|
247
|
+
export function standardize(x) {
|
|
248
|
+
const c = x.length < 2 ? undefined : centered(x);
|
|
249
|
+
if (c === undefined)
|
|
250
|
+
return x.map(() => NaN);
|
|
251
|
+
const sd = Math.sqrt(ddDivNum(crossSum(c.d, c.d), x.length - 1)[0]);
|
|
252
|
+
return c.d.map((v) => (sd > 0 ? v[0] / sd : NaN));
|
|
253
|
+
}
|
|
254
|
+
export function groupMoments(groups) {
|
|
255
|
+
const { groups: scaled, k } = scaleToUnit(groups);
|
|
256
|
+
const n = scaled.map((g) => g.length);
|
|
257
|
+
const all = scaled.flat();
|
|
258
|
+
const grand = all.length === 0 ? [NaN, 0] : meanDD(all);
|
|
259
|
+
const mean = [];
|
|
260
|
+
const ss = [];
|
|
261
|
+
const variance = [];
|
|
262
|
+
let ssb = [0, 0];
|
|
263
|
+
let ssw = [0, 0];
|
|
264
|
+
for (const g of scaled) {
|
|
265
|
+
if (g.length === 0) {
|
|
266
|
+
mean.push(NaN);
|
|
267
|
+
ss.push(NaN);
|
|
268
|
+
variance.push(NaN);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const m = meanDD(g);
|
|
272
|
+
let s = [0, 0];
|
|
273
|
+
for (const v of g) {
|
|
274
|
+
const e = ddSub([v, 0], m);
|
|
275
|
+
s = ddAdd(s, ddMul(e, e));
|
|
276
|
+
}
|
|
277
|
+
const d = ddSub(m, grand);
|
|
278
|
+
ssb = ddAdd(ssb, ddMul(ddMul(d, d), [g.length, 0]));
|
|
279
|
+
ssw = ddAdd(ssw, s);
|
|
280
|
+
mean.push(m[0]);
|
|
281
|
+
ss.push(s[0]);
|
|
282
|
+
// divided in double-double, as variance() does
|
|
283
|
+
variance.push(g.length > 1 ? ddDivNum(s, g.length - 1)[0] : NaN);
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
k,
|
|
287
|
+
groups: scaled,
|
|
288
|
+
n,
|
|
289
|
+
mean,
|
|
290
|
+
ss,
|
|
291
|
+
variance,
|
|
292
|
+
grandMean: grand[0],
|
|
293
|
+
ssBetween: ssb[0],
|
|
294
|
+
ssWithin: ssw[0],
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** x - y for paired values, both scaled by one 2^-k first (so the differences cannot overflow), with k */
|
|
298
|
+
export function pairedDifferences(x, y) {
|
|
299
|
+
const { groups, k } = scaleToUnit([x, y]);
|
|
300
|
+
const [a, b] = groups;
|
|
301
|
+
return { d: a.map((v, i) => v - b[i]), k };
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Satterthwaite's degrees of freedom (sum w)^2 / sum(w^2 / df) of a sum of independent variance estimates w with
|
|
305
|
+
* degrees of freedom df. The w are scaled by a power of 2 first (no rounding), so the squares neither overflow nor
|
|
306
|
+
* underflow when the w do not; the ratio does not depend on the units of the data. The form of irr's icc (the same
|
|
307
|
+
* order of operations); tTest, cohensD and gamesHowellTest use R's forms below and this only where those are not finite
|
|
308
|
+
*/
|
|
309
|
+
export function satterthwaiteDf(w, df) {
|
|
310
|
+
let m = 0;
|
|
311
|
+
for (const v of w)
|
|
312
|
+
m = Math.max(m, Math.abs(v));
|
|
313
|
+
const k = m === 0 || !Number.isFinite(m) ? 0 : Math.floor(Math.log2(m));
|
|
314
|
+
let num = 0;
|
|
315
|
+
let den = 0;
|
|
316
|
+
for (let i = 0; i < w.length; i++) {
|
|
317
|
+
const a = ldexp(w[i], -k);
|
|
318
|
+
num += a;
|
|
319
|
+
den += (a * a) / df[i];
|
|
109
320
|
}
|
|
110
|
-
|
|
111
|
-
// Multiply by 2^(kx + ky) in two steps (a single step can overflow or underflow to 0)
|
|
112
|
-
return v * 2 ** kx * 2 ** ky;
|
|
321
|
+
return (num * num) / den;
|
|
113
322
|
}
|
|
114
|
-
/**
|
|
115
|
-
function
|
|
323
|
+
/** An even exponent 2j with max abs(w) / 2^2j in [1, 4) (0 for all w 0 or a non-finite one): sqrt then scales exactly */
|
|
324
|
+
function evenExponent(w) {
|
|
116
325
|
let m = 0;
|
|
117
|
-
for (const v of
|
|
118
|
-
m = Math.max(m, Math.abs(v
|
|
119
|
-
|
|
326
|
+
for (const v of w)
|
|
327
|
+
m = Math.max(m, Math.abs(v));
|
|
328
|
+
if (m === 0 || !Number.isFinite(m))
|
|
329
|
+
return 0;
|
|
330
|
+
const e = Math.floor(Math.log2(m));
|
|
331
|
+
return e - (((e % 2) + 2) % 2);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Welch's degrees of freedom in the form of R's t.test and effectsize: se^4 / sum(se_i^4 / df_i) with se_i = sqrt(w_i)
|
|
335
|
+
* and se = sqrt(sum se_i^2), on the w scaled by an even power of 2 (exact, also through sqrt, so the same bits as R
|
|
336
|
+
* wherever R's terms are in range, and no overflow). {@link satterthwaiteDf} where that is not finite and positive
|
|
337
|
+
* (revision of review 2)
|
|
338
|
+
*/
|
|
339
|
+
export function satterthwaiteDfSe(w, df) {
|
|
340
|
+
const k = evenExponent(w);
|
|
341
|
+
let s2 = 0;
|
|
342
|
+
let den = 0;
|
|
343
|
+
for (let i = 0; i < w.length; i++) {
|
|
344
|
+
const se = Math.sqrt(ldexp(w[i], -k));
|
|
345
|
+
s2 += se ** 2;
|
|
346
|
+
den += se ** 4 / df[i];
|
|
347
|
+
}
|
|
348
|
+
const r = Math.sqrt(s2) ** 4 / den;
|
|
349
|
+
return Number.isFinite(r) && r > 0 ? r : satterthwaiteDf(w, df);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Welch's degrees of freedom in the form of PMCMRplus's gamesHowellTest: A^2 / sum(s2_i^2 / (n_i^2 (n_i - 1))) with
|
|
353
|
+
* A = sum s2_i / n_i, for the group variances s2 and sizes n, scaled as {@link satterthwaiteDfSe} (revision of review 2)
|
|
354
|
+
*/
|
|
355
|
+
export function satterthwaiteDfPM(s2, n) {
|
|
356
|
+
const k = evenExponent(s2);
|
|
357
|
+
let a = 0;
|
|
358
|
+
let den = 0;
|
|
359
|
+
for (let i = 0; i < s2.length; i++) {
|
|
360
|
+
const v = ldexp(s2[i], -k);
|
|
361
|
+
a += v / n[i];
|
|
362
|
+
den += v ** 2 / (n[i] ** 2 * (n[i] - 1));
|
|
363
|
+
}
|
|
364
|
+
const r = a ** 2 / den;
|
|
365
|
+
return Number.isFinite(r) && r > 0 ? r : satterthwaiteDf(s2.map((v, i) => v / n[i]), n.map((m) => m - 1));
|
|
120
366
|
}
|
|
121
367
|
/**
|
|
122
368
|
* Sum (accumulated in double-double without overflow, then rounded; ±Infinity beyond the double range). With
|
|
@@ -125,10 +371,124 @@ function scaleExponent(d) {
|
|
|
125
371
|
export function sum(x) {
|
|
126
372
|
if (!x.every(Number.isFinite))
|
|
127
373
|
return nonFiniteSum(x);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
374
|
+
if (!isDoubleDouble()) {
|
|
375
|
+
const acc = wideSum();
|
|
376
|
+
for (const v of x)
|
|
377
|
+
wideSumAdd(acc, v, x.length);
|
|
378
|
+
return wideSumValue(acc);
|
|
379
|
+
}
|
|
380
|
+
// The double-double keeps two components, so a third band of magnitudes is rounded away when it is added to the
|
|
381
|
+
// low one: with the bands later cancelling, sum([1e308, 1e100, 1, -1e308, -1e100]) was 0 and
|
|
382
|
+
// sum([1e20, 1e3, 1e-10, -1e20, -1e3]) 1.0004e-10 (#374). The value is that of wideSumAdd; only where the lost amount
|
|
383
|
+
// could move it across a rounding boundary is the sum formed exactly instead (rare: the losses are zero or about
|
|
384
|
+
// 2^-106 of the partial sums)
|
|
385
|
+
const { hi, lo, shift, lost } = boundedSum(x);
|
|
386
|
+
const r = hi + lo;
|
|
387
|
+
if (lost > 0 && mayRoundOtherwise(hi, lo, r, lost * (1 + x.length * 2 ** -52), shift))
|
|
388
|
+
return exactSum(x);
|
|
389
|
+
return ldexp(r, shift);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* The double-double sum of finite values with the steps of wideSumAdd (ddAdd of [v, 0]) written out, so that its
|
|
393
|
+
* value (hi + lo) 2^shift has the same bits, and an upper bound `lost` (in units of 2^shift) on what those steps
|
|
394
|
+
* rounded away (#374)
|
|
395
|
+
*/
|
|
396
|
+
function boundedSum(x) {
|
|
397
|
+
const n = x.length;
|
|
398
|
+
let hi = 0;
|
|
399
|
+
let lo = 0;
|
|
400
|
+
let shift = 0;
|
|
401
|
+
let f = 1;
|
|
402
|
+
let lost = 0;
|
|
403
|
+
for (const x0 of x) {
|
|
404
|
+
let v = x0;
|
|
405
|
+
if (shift !== 0) {
|
|
406
|
+
v = x0 * f;
|
|
407
|
+
// (a value below 2^(shift - 1022) is rounded onto the subnormals when scaled)
|
|
408
|
+
if (v / f !== x0)
|
|
409
|
+
lost += 2 ** -1074;
|
|
410
|
+
}
|
|
411
|
+
let s1 = hi + v;
|
|
412
|
+
let bb2 = s1 - hi;
|
|
413
|
+
let e3 = hi - (s1 - bb2) + (v - bb2);
|
|
414
|
+
let u7 = e3 + (lo + 0);
|
|
415
|
+
let s8 = s1 + u7;
|
|
416
|
+
let e9 = u7 - (s8 - s1);
|
|
417
|
+
let s11 = s8 + (e9 + 0);
|
|
418
|
+
let e12 = e9 + 0 - (s11 - s8);
|
|
419
|
+
if (shift === 0 && !(Number.isFinite(s1) && Number.isFinite(s11) && Number.isFinite(e12))) {
|
|
420
|
+
// the sum would overflow: from here on, the values scaled by 2^-shift (as wideSumAdd)
|
|
421
|
+
shift = overflowShift(n);
|
|
422
|
+
f = 2 ** -shift;
|
|
423
|
+
const h = hi * f;
|
|
424
|
+
const l = lo * f;
|
|
425
|
+
if (h / f !== hi)
|
|
426
|
+
lost += 2 ** -1074;
|
|
427
|
+
if (l / f !== lo)
|
|
428
|
+
lost += 2 ** -1074;
|
|
429
|
+
hi = h;
|
|
430
|
+
lo = l;
|
|
431
|
+
v = x0 * f;
|
|
432
|
+
if (v / f !== x0)
|
|
433
|
+
lost += 2 ** -1074;
|
|
434
|
+
s1 = hi + v;
|
|
435
|
+
bb2 = s1 - hi;
|
|
436
|
+
e3 = hi - (s1 - bb2) + (v - bb2);
|
|
437
|
+
u7 = e3 + (lo + 0);
|
|
438
|
+
s8 = s1 + u7;
|
|
439
|
+
e9 = u7 - (s8 - s1);
|
|
440
|
+
s11 = s8 + (e9 + 0);
|
|
441
|
+
e12 = e9 + 0 - (s11 - s8);
|
|
442
|
+
}
|
|
443
|
+
// the rounding of u7 = e3 + lo is the step that loses; the next two are exact where |s1| >= |u7| or s1 = 0
|
|
444
|
+
const bb = u7 - e3;
|
|
445
|
+
lost += Math.abs(e3 - (u7 - bb) + (lo - bb));
|
|
446
|
+
if (Math.abs(u7) > Math.abs(s1) && s1 !== 0)
|
|
447
|
+
lost += (Math.abs(u7) + Math.abs(s1)) * 2 ** -51;
|
|
448
|
+
hi = s11;
|
|
449
|
+
lo = e12;
|
|
450
|
+
}
|
|
451
|
+
return { hi, lo, shift, lost };
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Whether a true sum within lost of hi + lo (in units of 2^shift) may round to another double than r = fl(hi + lo):
|
|
455
|
+
* lost reaches the distance from hi + lo to the nearest rounding boundary of r (half the gap below a power of 2, as a
|
|
456
|
+
* bound). With shift != 0 also where scaling back may round again (a result near the subnormals)
|
|
457
|
+
*/
|
|
458
|
+
function mayRoundOtherwise(hi, lo, r, lost, shift) {
|
|
459
|
+
if (!Number.isFinite(r))
|
|
460
|
+
return true;
|
|
461
|
+
const a = Math.abs(r);
|
|
462
|
+
if (shift !== 0 && a < 2 ** -1000)
|
|
463
|
+
return true;
|
|
464
|
+
const offset = Math.abs(hi - r + lo); // exact: r = fl(hi + lo) with |lo| at most half an ulp of hi
|
|
465
|
+
let gap;
|
|
466
|
+
if (a < 2 ** -1022)
|
|
467
|
+
gap = 2 ** -1074;
|
|
468
|
+
else {
|
|
469
|
+
const [m, e] = splitDouble(a);
|
|
470
|
+
gap = 2 ** e;
|
|
471
|
+
if (m === 1n << 52n)
|
|
472
|
+
gap /= 2;
|
|
473
|
+
}
|
|
474
|
+
return lost >= gap / 2 - offset;
|
|
475
|
+
}
|
|
476
|
+
/** The sum of finite values formed exactly (integers times 2^-1074 in BigInt) and rounded once */
|
|
477
|
+
function exactSum(x) {
|
|
478
|
+
let low = Infinity;
|
|
479
|
+
const parts = [];
|
|
480
|
+
for (const v of x) {
|
|
481
|
+
if (v === 0)
|
|
482
|
+
continue;
|
|
483
|
+
const p = splitDouble(v);
|
|
484
|
+
parts.push(p);
|
|
485
|
+
if (p[1] < low)
|
|
486
|
+
low = p[1];
|
|
487
|
+
}
|
|
488
|
+
let acc = 0n;
|
|
489
|
+
for (const [m, e] of parts)
|
|
490
|
+
acc += m << BigInt(e - low);
|
|
491
|
+
return roundScaled(acc, low);
|
|
132
492
|
}
|
|
133
493
|
/**
|
|
134
494
|
* Ranks (ties get the average rank, R's rank(ties.method = "average")). x contains no NaN.
|
|
@@ -184,3 +544,62 @@ export function completePairs(x, y) {
|
|
|
184
544
|
}
|
|
185
545
|
return [a, b];
|
|
186
546
|
}
|
|
547
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
548
|
+
/**
|
|
549
|
+
* d^2 / e, as for a Pearson chi-squared term. When d^2 overflows (or loses precision below the normal range) although
|
|
550
|
+
* the quotient is representable, it is formed as (d / sqrt(e))^2 instead
|
|
551
|
+
*/
|
|
552
|
+
export function squareOver(d, e) {
|
|
553
|
+
const d2 = d * d;
|
|
554
|
+
if (d2 < Infinity && (d2 >= MIN_NORMAL || d === 0))
|
|
555
|
+
return d2 / e;
|
|
556
|
+
const z = d / Math.sqrt(e);
|
|
557
|
+
return z * z;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* a b / n, as for an expected count from two margins. When the product overflows, underflows or is subnormal (with a
|
|
561
|
+
* and b not 0), the three are scaled to [1, 2) by powers of 2 and the exponents added back at the end
|
|
562
|
+
*/
|
|
563
|
+
export function productOver(a, b, n) {
|
|
564
|
+
const ab = a * b;
|
|
565
|
+
if (a === 0 || b === 0 || !Number.isFinite(a) || !Number.isFinite(b) || !Number.isFinite(n))
|
|
566
|
+
return ab / n;
|
|
567
|
+
if (Math.abs(ab) >= MIN_NORMAL && Math.abs(ab) < Infinity)
|
|
568
|
+
return ab / n;
|
|
569
|
+
const ka = unitExponent([[a]]);
|
|
570
|
+
const kb = unitExponent([[b]]);
|
|
571
|
+
const kn = unitExponent([[n]]);
|
|
572
|
+
return ldexp((ldexp(a, -ka) * ldexp(b, -kb)) / ldexp(n, -kn), ka + kb - kn);
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Data scaled by a power of 2, 2^-k, so that the largest finite absolute value is in [1, 2) (k = 0 when there is
|
|
576
|
+
* none). The scaling adds no rounding error, so statistics that do not depend on the units (t, F, chi-squared,
|
|
577
|
+
* ratios of variances, degrees of freedom) come out as from the original data, but their squares and variances
|
|
578
|
+
* can no longer overflow or underflow. Quantities with units are multiplied back by 2^k (see ldexp)
|
|
579
|
+
*/
|
|
580
|
+
export function scaleToUnit(groups) {
|
|
581
|
+
const k = unitExponent(groups);
|
|
582
|
+
return { groups: groups.map((g) => (k === 0 ? Array.from(g) : g.map((v) => ldexp(v, -k)))), k };
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Euclidean norm sqrt(sum x_i^2): the plain sum where it is in range (so the value is unchanged there), otherwise from
|
|
586
|
+
* the values divided by the largest |x_i| (as BLAS's dnrm2), so that squares of values around 1e200 or 1e-200 neither
|
|
587
|
+
* overflow nor underflow
|
|
588
|
+
*/
|
|
589
|
+
export function norm2(x) {
|
|
590
|
+
let s = 0;
|
|
591
|
+
for (const v of x)
|
|
592
|
+
s += v * v;
|
|
593
|
+
if (s < Infinity && s >= MIN_NORMAL)
|
|
594
|
+
return Math.sqrt(s);
|
|
595
|
+
let m = 0;
|
|
596
|
+
for (const v of x)
|
|
597
|
+
m = Math.max(m, Math.abs(v));
|
|
598
|
+
// all 0, or an infinite value (NaN if there is also a NaN)
|
|
599
|
+
if (!(m > 0 && m < Infinity))
|
|
600
|
+
return Number.isNaN(s) ? NaN : m;
|
|
601
|
+
let t = 0;
|
|
602
|
+
for (const v of x)
|
|
603
|
+
t += (v / m) ** 2;
|
|
604
|
+
return m * Math.sqrt(t);
|
|
605
|
+
}
|
|
@@ -9,3 +9,10 @@ export interface UnirootOptions {
|
|
|
9
9
|
export declare const UNIROOT_TOL: number;
|
|
10
10
|
/** f(lower) and f(upper) must differ in sign. Returns one root */
|
|
11
11
|
export declare function uniroot(f: (x: number) => number, lower: number, upper: number, opts?: UnirootOptions): number;
|
|
12
|
+
/**
|
|
13
|
+
* Root in psi > 0 of a function monotone in psi (an odds ratio), given whether it lies below 1: solved in u = log psi
|
|
14
|
+
* to the precision of the doubles, from a bracket widened from u = 0 by doubling. 0 or Infinity when f does not change
|
|
15
|
+
* sign within the doubles. (R's fisher.test solves on psi in [0, 1] or 1/psi in [eps, 1] with the absolute tolerance
|
|
16
|
+
* eps^0.25, which leaves its estimates 1e-6 off and its limits up to several percent, or at the end 1/eps)
|
|
17
|
+
*/
|
|
18
|
+
export declare function logScaleRoot(f: (psi: number) => number, belowOne: boolean): number;
|
package/dist/internal/uniroot.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Univariate root finding for internal callers (the confidence limits and estimates of fisherTest, wilcoxTest and
|
|
2
|
-
// mantelhaenTest): a thin wrapper around the public uniroot (src/numerics/integrate.ts, Brent's method with R's
|
|
3
|
-
// conventions), returning just the root.
|
|
4
|
-
import {
|
|
2
|
+
// mantelhaenTest): a thin wrapper around the public uniroot's body unirootCore (src/numerics/integrate.ts, Brent's method with R's
|
|
3
|
+
// conventions), returning just the root, and the root of an odds ratio on the log scale (logScaleRoot).
|
|
4
|
+
import { unirootCore as unirootFull } from "../numerics/integrate.js";
|
|
5
5
|
export const UNIROOT_TOL = Math.pow(Number.EPSILON, 0.25);
|
|
6
6
|
/** f(lower) and f(upper) must differ in sign. Returns one root */
|
|
7
7
|
export function uniroot(f, lower, upper, opts = {}) {
|
|
@@ -12,3 +12,28 @@ export function uniroot(f, lower, upper, opts = {}) {
|
|
|
12
12
|
options.fUpper = opts.fUpper;
|
|
13
13
|
return unirootFull(f, lower, upper, options).root;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Root in psi > 0 of a function monotone in psi (an odds ratio), given whether it lies below 1: solved in u = log psi
|
|
17
|
+
* to the precision of the doubles, from a bracket widened from u = 0 by doubling. 0 or Infinity when f does not change
|
|
18
|
+
* sign within the doubles. (R's fisher.test solves on psi in [0, 1] or 1/psi in [eps, 1] with the absolute tolerance
|
|
19
|
+
* eps^0.25, which leaves its estimates 1e-6 off and its limits up to several percent, or at the end 1/eps)
|
|
20
|
+
*/
|
|
21
|
+
export function logScaleRoot(f, belowOne) {
|
|
22
|
+
const g = (u) => f(Math.exp(u));
|
|
23
|
+
let a = 0;
|
|
24
|
+
let ga = g(0);
|
|
25
|
+
let b = belowOne ? -1 : 1;
|
|
26
|
+
let gb = g(b);
|
|
27
|
+
while (gb !== 0 && Math.sign(gb) === Math.sign(ga)) {
|
|
28
|
+
if (Math.abs(b) > 745)
|
|
29
|
+
return belowOne ? 0 : Infinity;
|
|
30
|
+
a = b;
|
|
31
|
+
ga = gb;
|
|
32
|
+
b *= 2;
|
|
33
|
+
gb = g(b);
|
|
34
|
+
}
|
|
35
|
+
if (gb === 0)
|
|
36
|
+
return Math.exp(b);
|
|
37
|
+
const root = a < b ? uniroot(g, a, b, { tol: 2 ** -50, fLower: ga, fUpper: gb }) : uniroot(g, b, a, { tol: 2 ** -50, fLower: gb, fUpper: ga });
|
|
38
|
+
return Math.exp(root);
|
|
39
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that receives R's warnings: the warning's text and the name of the function that gave it, once per
|
|
3
|
+
* distinct warning of a call, after the call has computed its result.
|
|
4
|
+
*
|
|
5
|
+
* @category Hypothesis tests
|
|
6
|
+
*/
|
|
7
|
+
export type WarningHandler = (message: string, source: string) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Sets the function that receives R's warnings, and returns the one it replaces (`null` when there was none). Not an
|
|
10
|
+
* R function. It plays the part of R's printing of warnings (and, by throwing, of `options(warn = 2)`).
|
|
11
|
+
*
|
|
12
|
+
* Every test, model and fit returns R's warnings in its result's `warnings` whatever the handler. With a handler
|
|
13
|
+
* set, each call of such a function (and `pivotLonger` without `onWarning`) also passes its warnings to the handler
|
|
14
|
+
* once it has computed its result: each distinct warning once, in order, with the name of the function called (a
|
|
15
|
+
* function that calls another, such as `pairwiseWilcoxTest`, passes the warnings once, under its own name). A fit's
|
|
16
|
+
* warnings are passed once, when it is fitted (`lm`, `glm`, `aov`, `lmer`, ...): a function given a fit or a table
|
|
17
|
+
* (`anova`, `aovSummary`, `Anova`, `glmSummary`, `etaSquared`, `lmerSummary`, ...) keeps them in its result's
|
|
18
|
+
* `warnings` but passes only the warnings it adds. An error thrown by the handler propagates to the caller. By default
|
|
19
|
+
* there is no handler and nothing is printed.
|
|
20
|
+
*
|
|
21
|
+
* To print the warnings as R does, set `(message, source) => console.warn(source + ": " + message)`. A handler that
|
|
22
|
+
* throws (`(message) => { throw new Error(message); }`) turns each warning into an error, as R's `options(warn = 2)`.
|
|
23
|
+
*
|
|
24
|
+
* The handler is kept on `globalThis`, so it is one setting for every copy of the library loaded on a page.
|
|
25
|
+
*
|
|
26
|
+
* @param handler The function that receives the warnings, or `null` to stop passing them.
|
|
27
|
+
* @returns The handler set before, or `null`.
|
|
28
|
+
* @throws TypeError if `handler` is neither a function nor `null`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const seen: string[] = [];
|
|
33
|
+
* const previous = setWarningHandler((message, source) => { seen.push(`${source}: ${message}`); });
|
|
34
|
+
* optim([1, 1], (p) => (p[0] - 2) ** 2 + (p[1] - 3) ** 2, { lower: [0, 0] }).warnings;
|
|
35
|
+
* // ["bounds can only be used with method L-BFGS-B (or Brent)"]
|
|
36
|
+
* setWarningHandler(previous);
|
|
37
|
+
* seen; // ["optim: bounds can only be used with method L-BFGS-B (or Brent)"]
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @category Hypothesis tests
|
|
41
|
+
*/
|
|
42
|
+
export declare function setWarningHandler(handler: WarningHandler | null): WarningHandler | null;
|
|
43
|
+
/**
|
|
44
|
+
* Passes a call's warnings to the handler (each distinct one once, in order), unless the call is inside another
|
|
45
|
+
* public function (`unreported`) or no handler is set. An error of the handler propagates.
|
|
46
|
+
*/
|
|
47
|
+
export declare function reportWarnings(source: string, warnings: readonly string[]): void;
|
|
48
|
+
/**
|
|
49
|
+
* Passes the warnings of a result to the handler (reportWarnings) and returns the result. The warnings of `given` (the
|
|
50
|
+
* fits or tables the function was given, whose warnings the result keeps) are left out: they were passed by the call
|
|
51
|
+
* that made them.
|
|
52
|
+
*/
|
|
53
|
+
export declare function reported<T extends {
|
|
54
|
+
readonly warnings?: readonly string[];
|
|
55
|
+
}>(source: string, result: T, ...given: readonly unknown[]): T;
|
|
56
|
+
/** Runs a call of a public function inside another one: its warnings are not passed to the handler */
|
|
57
|
+
export declare function unreported<T>(f: () => T): T;
|
|
58
|
+
/** Collects warnings in order of first appearance, each once (as the fixtures list R's warnings) */
|
|
59
|
+
export declare class Warnings {
|
|
60
|
+
readonly list: string[];
|
|
61
|
+
add(message: string): void;
|
|
62
|
+
}
|
|
63
|
+
/** Runs f with `sink` as the sink of `warn` */
|
|
64
|
+
export declare function withWarnings<T>(sink: Warnings, f: () => T): T;
|
|
65
|
+
/** Adds a warning to the sink set by withWarnings (none outside it) */
|
|
66
|
+
export declare function warn(message: string): void;
|