@numero-uno-jpn/rstats 0.15.2 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +280 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-PWVQVJTP.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +35 -5
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +87 -8
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +11 -1
- package/dist/distributions/f.js +73 -22
- package/dist/distributions/gamma.js +61 -6
- package/dist/distributions/geometric.js +53 -14
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +6 -1
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +31 -7
- package/dist/distributions/logistic.js +11 -2
- package/dist/distributions/lognormal.js +20 -3
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +128 -35
- package/dist/distributions/nchisq.js +85 -14
- package/dist/distributions/negbinomial.js +100 -18
- package/dist/distributions/normal.js +35 -5
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +536 -35
- package/dist/distributions/pareto.js +9 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/t.js +55 -3
- package/dist/distributions/triangular.js +45 -13
- package/dist/distributions/tukey.js +21 -2
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +33 -3
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +23 -1
- package/dist/internal/ddexp.js +86 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +25 -7
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +11 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +15 -2
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +100 -10
- package/dist/special/incbetLargeA.js +7 -2
- package/dist/special/incbetPrefactor.d.ts +5 -0
- package/dist/special/incbetPrefactor.js +14 -3
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +179 -33
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-3WQCKGKP.js +0 -2
- package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
- package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-HBMHR5SS.js +0 -2
- package/dist/chunks/chunk-IBPWRQIF.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-LG74QY5U.js +0 -2
- package/dist/chunks/chunk-PEYGUK32.js +0 -2
- package/dist/chunks/chunk-Q4PGYESP.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-SREPSVTT.js +0 -2
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
- package/dist/chunks/chunk-VWJ74UHC.js +0 -2
- package/dist/chunks/chunk-XNPU54NH.js +0 -2
- package/dist/chunks/chunk-XPLDEK6D.js +0 -2
- package/dist/chunks/chunk-YGT4P7EU.js +0 -2
- package/dist/chunks/chunk-YVREYVI2.js +0 -2
package/dist/htest/wilcoxTest.js
CHANGED
|
@@ -25,6 +25,7 @@ import { completePairs, dropNaN, mean, rank, tieCounts, } from "../internal/summ
|
|
|
25
25
|
import { uniroot } from "../internal/uniroot.js";
|
|
26
26
|
import { twoSampleData } from "./testFormula.js";
|
|
27
27
|
import { matchAlternative } from "./types.js";
|
|
28
|
+
import { maxOf, midpoint, minOf } from "../internal/extremes.js";
|
|
28
29
|
export function wilcoxTest(x, y, options = {}) {
|
|
29
30
|
if (typeof x === "string") {
|
|
30
31
|
if (options.paired !== undefined)
|
|
@@ -138,10 +139,19 @@ function signedRankStat(x) {
|
|
|
138
139
|
function signedRank(x0, exactOpt, paired, ctx) {
|
|
139
140
|
const { alternative, mu, correct } = ctx;
|
|
140
141
|
let x = x0.map((v) => v - mu);
|
|
142
|
+
// A finite observation whose difference from mu overflows: the differences in half units (x / 2 - mu / 2). Then
|
|
143
|
+
// abs(mu) >= 2^970, so each half difference is exactly half the rounded difference (a subnormal x / 2 rounds far
|
|
144
|
+
// below the spacing of mu / 2) and the ranks, signs and zeros are those of the differences
|
|
145
|
+
const halved = x.some((v, i) => !Number.isFinite(v) && Number.isFinite(x0[i]));
|
|
146
|
+
if (halved)
|
|
147
|
+
x = x0.map((v) => v / 2 - mu / 2);
|
|
141
148
|
const zeroes = x.some((v) => v === 0);
|
|
142
149
|
if (zeroes)
|
|
143
150
|
x = x.filter((v) => v !== 0);
|
|
144
151
|
const n = x.length;
|
|
152
|
+
// only zero differences: nothing to rank (R returns a p-value of NaN)
|
|
153
|
+
if (n === 0)
|
|
154
|
+
throw new RangeError("no non-zero differences for the signed rank test");
|
|
145
155
|
const exact = exactOpt ?? n < 50;
|
|
146
156
|
const r = rank(x.map(Math.abs));
|
|
147
157
|
const ties = tieCounts(r).length > 0;
|
|
@@ -179,7 +189,20 @@ function signedRank(x0, exactOpt, paired, ctx) {
|
|
|
179
189
|
if (!ctx.confInt)
|
|
180
190
|
return result;
|
|
181
191
|
// The interval is on the original scale (add mu back to the data with zero differences removed)
|
|
182
|
-
|
|
192
|
+
// (with half units, the observations other than mu themselves)
|
|
193
|
+
// R also adds mu back. Where mu dwarfs the data, (x - mu) + mu loses the observations (rnorm(n, 0, 1e-200) with
|
|
194
|
+
// mu = 0.5 gave R's interval NaN NaN and estimate 0): then the observations themselves, which are the same values
|
|
195
|
+
// (x - mu = 0 exactly where x = mu). The bound 2^-26 of the largest observation leaves the rounding of ordinary
|
|
196
|
+
// data, an ulp or two, to R's values (#502)
|
|
197
|
+
let xo = halved ? x0.filter((v) => v !== mu) : x.map((v) => v + mu);
|
|
198
|
+
if (!halved) {
|
|
199
|
+
const direct = x0.filter((v) => v !== mu);
|
|
200
|
+
let big = 0;
|
|
201
|
+
for (const v of direct)
|
|
202
|
+
big = Math.max(big, Math.abs(v));
|
|
203
|
+
if (xo.some((v, i) => Math.abs(v - direct[i]) > 2 ** -26 * big))
|
|
204
|
+
xo = direct;
|
|
205
|
+
}
|
|
183
206
|
const ci = useExact
|
|
184
207
|
? signedRankExactCI(xo, ctx)
|
|
185
208
|
: signedRankNormalCI(xo, ctx);
|
|
@@ -206,8 +229,8 @@ function exactRank(quantile, alternative, a) {
|
|
|
206
229
|
return q === 0 ? 1 : q;
|
|
207
230
|
}
|
|
208
231
|
function checkTolRoot(tol) {
|
|
209
|
-
if (!(tol > 0))
|
|
210
|
-
throw new RangeError("tolRoot must be positive");
|
|
232
|
+
if (!(tol > 0 && tol < Infinity))
|
|
233
|
+
throw new RangeError("tolRoot must be a positive finite number");
|
|
211
234
|
}
|
|
212
235
|
/**
|
|
213
236
|
* Exact interval from the order statistics of the Walsh averages (Bauer 1972) and the Hodges-Lehmann estimate, their
|
|
@@ -219,7 +242,7 @@ function signedRankExactCI(x, ctx) {
|
|
|
219
242
|
const walsh = [];
|
|
220
243
|
for (let i = 0; i < n; i++)
|
|
221
244
|
for (let j = i; j < n; j++)
|
|
222
|
-
walsh.push((x[i]
|
|
245
|
+
walsh.push(midpoint(x[i], x[j]));
|
|
223
246
|
walsh.sort(ascending);
|
|
224
247
|
const N = walsh.length;
|
|
225
248
|
const a = 1 - confLevel;
|
|
@@ -242,10 +265,10 @@ function signedRankNormalCI(x, ctx) {
|
|
|
242
265
|
const n = x.length;
|
|
243
266
|
if (n === 0)
|
|
244
267
|
return noInterval(alternative, NaN);
|
|
245
|
-
const lo =
|
|
246
|
-
const hi =
|
|
268
|
+
const lo = minOf(x);
|
|
269
|
+
const hi = maxOf(x);
|
|
247
270
|
if (!Number.isFinite(lo) || !Number.isFinite(hi) || n === 1 || lo === hi) {
|
|
248
|
-
return noInterval(alternative, (lo
|
|
271
|
+
return noInterval(alternative, midpoint(lo, hi), "midrange");
|
|
249
272
|
}
|
|
250
273
|
const stat = (d, corr) => {
|
|
251
274
|
const [, centre, sigma] = signedRankStat(x.map((v) => v - d).filter((v) => v !== 0));
|
|
@@ -309,33 +332,89 @@ function rankSumStat(x, y) {
|
|
|
309
332
|
const sigma = Math.sqrt(((nx * ny) / 12) * (N + 1 - ties / (N * (N - 1))));
|
|
310
333
|
return [W, W - (nx * ny) / 2, sigma];
|
|
311
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* rankSumStat for x_i - d given in half units h_i = x_i / 2 - d / 2 (used where a finite x_i - d overflows, so
|
|
337
|
+
* abs(d) >= 2^970), with whether there are ties. y stays in its own units: h_i is compared with y_j / 2, which is exact
|
|
338
|
+
* except for a subnormal y_j, and there h_i is 0 (compared with y_j itself) or of magnitude above 2^900, so the order
|
|
339
|
+
* is that of the differences. Halving y would merge neighbouring subnormals
|
|
340
|
+
*/
|
|
341
|
+
function rankSumHalf(h, y) {
|
|
342
|
+
const nx = h.length;
|
|
343
|
+
const ny = y.length;
|
|
344
|
+
const N = nx + ny;
|
|
345
|
+
// sign of 2 h - v (h in half units, v in units)
|
|
346
|
+
const cmpHalf = (hv, v) => {
|
|
347
|
+
const w = hv === 0 || !Number.isFinite(hv) || !Number.isFinite(v) ? v : v / 2;
|
|
348
|
+
return hv < w ? -1 : hv > w ? 1 : 0;
|
|
349
|
+
};
|
|
350
|
+
const cmp = (a, b) => {
|
|
351
|
+
if (a < nx && b < nx)
|
|
352
|
+
return h[a] < h[b] ? -1 : h[a] > h[b] ? 1 : 0;
|
|
353
|
+
if (a >= nx && b >= nx)
|
|
354
|
+
return y[a - nx] < y[b - nx] ? -1 : y[a - nx] > y[b - nx] ? 1 : 0;
|
|
355
|
+
return a < nx ? cmpHalf(h[a], y[b - nx]) : -cmpHalf(h[b], y[a - nx]);
|
|
356
|
+
};
|
|
357
|
+
const idx = Array.from({ length: N }, (_, i) => i);
|
|
358
|
+
idx.sort((a, b) => cmp(a, b) || a - b);
|
|
359
|
+
let s = 0;
|
|
360
|
+
let ties = 0;
|
|
361
|
+
let i = 0;
|
|
362
|
+
while (i < N) {
|
|
363
|
+
let j = i;
|
|
364
|
+
while (j + 1 < N && cmp(idx[j + 1], idx[i]) === 0)
|
|
365
|
+
j++;
|
|
366
|
+
const avg = (i + j + 2) / 2;
|
|
367
|
+
for (let k = i; k <= j; k++)
|
|
368
|
+
if (idx[k] < nx)
|
|
369
|
+
s += avg;
|
|
370
|
+
const t = j - i + 1;
|
|
371
|
+
ties += t * t * t - t;
|
|
372
|
+
i = j + 1;
|
|
373
|
+
}
|
|
374
|
+
const W = s - (nx * (nx + 1)) / 2;
|
|
375
|
+
const sigma = Math.sqrt(((nx * ny) / 12) * (N + 1 - ties / (N * (N - 1))));
|
|
376
|
+
return [W, W - (nx * ny) / 2, sigma, ties > 0];
|
|
377
|
+
}
|
|
312
378
|
function rankSum(x, y, exactOpt, ctx) {
|
|
313
379
|
const { alternative, mu, correct } = ctx;
|
|
314
380
|
const nx = x.length;
|
|
315
381
|
const ny = y.length;
|
|
316
|
-
|
|
382
|
+
let xs = x.map((v) => v - mu);
|
|
317
383
|
const exact = exactOpt ?? (nx < 50 && ny < 50);
|
|
318
|
-
|
|
319
|
-
const
|
|
384
|
+
// A finite x_i - mu that overflows: ranked from x / 2 - mu / 2 against y (see rankSumHalf)
|
|
385
|
+
const halved = xs.some((v, i) => !Number.isFinite(v) && Number.isFinite(x[i]));
|
|
386
|
+
let ties;
|
|
387
|
+
let W, dz, sigma;
|
|
388
|
+
if (halved) {
|
|
389
|
+
xs = x.map((v) => v / 2 - mu / 2);
|
|
390
|
+
[W, dz, sigma, ties] = rankSumHalf(xs, y);
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
ties = tieCounts([...xs, ...y]).length > 0;
|
|
394
|
+
[W, dz, sigma] = rankSumStat(xs, y);
|
|
395
|
+
}
|
|
320
396
|
let pValue;
|
|
321
397
|
let method;
|
|
322
398
|
const useExact = exact && !ties;
|
|
323
399
|
if (useExact) {
|
|
324
400
|
method = "Wilcoxon rank sum exact test";
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
401
|
+
pValue = rankSumExact(() => {
|
|
402
|
+
if (alternative === "less")
|
|
403
|
+
return pwilcox1(W, nx, ny, true, false);
|
|
404
|
+
if (alternative === "greater")
|
|
405
|
+
return pwilcox1(W - 1, nx, ny, false, false);
|
|
330
406
|
const p = W > (nx * ny) / 2
|
|
331
407
|
? pwilcox1(W - 1, nx, ny, false, false)
|
|
332
408
|
: pwilcox1(W, nx, ny, true, false);
|
|
333
|
-
|
|
334
|
-
}
|
|
409
|
+
return Math.min(1, 2 * p);
|
|
410
|
+
});
|
|
335
411
|
}
|
|
336
412
|
else {
|
|
337
413
|
method = `Wilcoxon rank sum test${correct ? " with continuity correction" : ""}`;
|
|
338
|
-
|
|
414
|
+
// all values tied: W always equals its expectation, so p = 1 (R 4.5.2: 0 / 0 = NaN two-sided; R 4.6 returns 1)
|
|
415
|
+
// (never with half units: an overflowing difference equals no y)
|
|
416
|
+
const allTied = !halved && xs.every((v) => v === y[0]) && y.every((v) => v === y[0]);
|
|
417
|
+
pValue = allTied ? 1 : normalP((dz - correction(dz, alternative, correct)) / sigma, alternative);
|
|
339
418
|
}
|
|
340
419
|
const result = {
|
|
341
420
|
method,
|
|
@@ -348,9 +427,23 @@ function rankSum(x, y, exactOpt, ctx) {
|
|
|
348
427
|
};
|
|
349
428
|
if (!ctx.confInt)
|
|
350
429
|
return result;
|
|
351
|
-
const ci = useExact ? rankSumExactCI(x, y, ctx) : rankSumNormalCI(x, y, ctx);
|
|
430
|
+
const ci = useExact ? rankSumExact(() => rankSumExactCI(x, y, ctx)) : rankSumNormalCI(x, y, ctx);
|
|
352
431
|
return { ...result, ...ci };
|
|
353
432
|
}
|
|
433
|
+
/**
|
|
434
|
+
* f, with the RangeErrors of the exact distribution (a table too large or too long to build, or inaccurate near the
|
|
435
|
+
* center of large samples) pointing to exact: false
|
|
436
|
+
*/
|
|
437
|
+
function rankSumExact(f) {
|
|
438
|
+
try {
|
|
439
|
+
return f();
|
|
440
|
+
}
|
|
441
|
+
catch (e) {
|
|
442
|
+
if (e instanceof RangeError && e.message.startsWith("wilcox:"))
|
|
443
|
+
throw new RangeError(`${e.message}: use exact: false`);
|
|
444
|
+
throw e;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
354
447
|
/** Exact interval from the order statistics of the differences x_i - y_j and their median as the estimate */
|
|
355
448
|
function rankSumExactCI(x, y, ctx) {
|
|
356
449
|
const { alternative, confLevel } = ctx;
|
|
@@ -379,8 +472,8 @@ function rankSumExactCI(x, y, ctx) {
|
|
|
379
472
|
*/
|
|
380
473
|
function rankSumNormalCI(x, y, ctx) {
|
|
381
474
|
const { alternative, correct, confLevel, tolRoot } = ctx;
|
|
382
|
-
const lo =
|
|
383
|
-
const hi =
|
|
475
|
+
const lo = minOf(x) - maxOf(y);
|
|
476
|
+
const hi = maxOf(x) - minOf(y);
|
|
384
477
|
if (!Number.isFinite(lo) || !Number.isFinite(hi)) {
|
|
385
478
|
throw new Error("cannot compute the confidence interval because the differences are not finite");
|
|
386
479
|
}
|
|
@@ -275,11 +275,31 @@ function dqpsrt(limit, last, maxerr, elist, iord, nrmax) {
|
|
|
275
275
|
* transformed) range [a0, b0]; `neval` gives the number of evaluations from the number of subintervals
|
|
276
276
|
*/
|
|
277
277
|
function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
278
|
+
// The work arrays (length limit + 1 in QUADPACK) start small and are doubled as subintervals are added, up to
|
|
279
|
+
// limit + 1: iteration L reads and writes indices up to L + 1 at most (the final sum reads rlist[L]), and the unused
|
|
280
|
+
// entries are 0 as in a full-length array, so the result is the same; a large subdivisions no longer allocates
|
|
281
|
+
// 36 (limit + 1) bytes up front (#369)
|
|
282
|
+
let cap = Math.min(limit + 1, 128);
|
|
283
|
+
let alist = new Float64Array(cap);
|
|
284
|
+
let blist = new Float64Array(cap);
|
|
285
|
+
let rlist = new Float64Array(cap);
|
|
286
|
+
let elist = new Float64Array(cap);
|
|
287
|
+
let iord = new Int32Array(cap);
|
|
288
|
+
const grow = (need) => {
|
|
289
|
+
cap = Math.min(limit + 1, Math.max(need, 2 * cap));
|
|
290
|
+
const f64 = (old) => {
|
|
291
|
+
const a = new Float64Array(cap);
|
|
292
|
+
a.set(old);
|
|
293
|
+
return a;
|
|
294
|
+
};
|
|
295
|
+
alist = f64(alist);
|
|
296
|
+
blist = f64(blist);
|
|
297
|
+
rlist = f64(rlist);
|
|
298
|
+
elist = f64(elist);
|
|
299
|
+
const io = new Int32Array(cap);
|
|
300
|
+
io.set(iord);
|
|
301
|
+
iord = io;
|
|
302
|
+
};
|
|
283
303
|
let ier = 0;
|
|
284
304
|
let last = 0;
|
|
285
305
|
let result = 0;
|
|
@@ -332,6 +352,8 @@ function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
|
|
|
332
352
|
let L = 1;
|
|
333
353
|
for (L = 1; L < limit; L++) {
|
|
334
354
|
last = L + 1;
|
|
355
|
+
if (cap < L + 2)
|
|
356
|
+
grow(L + 2);
|
|
335
357
|
// bisect the subinterval with the nrmax-th largest error estimate
|
|
336
358
|
const a1 = alist[maxerr];
|
|
337
359
|
const b1 = 0.5 * (alist[maxerr] + blist[maxerr]);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws unless the matrix is square, finite, symmetric (as R's isSymmetric: relative difference from its transpose at
|
|
3
|
+
* most 100 ε) and has a positive diagonal. R's factanal takes any matrix: an asymmetric one gave a result
|
|
4
|
+
*/
|
|
5
|
+
export declare function checkCovmat(S: readonly (readonly number[])[]): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The check of a covariance (or correlation) matrix given as `covmat` (factanal, omega).
|
|
2
|
+
import { isSymmetric } from "../linalg/eigen.js";
|
|
3
|
+
import { toMat } from "../linalg/matrix.js";
|
|
4
|
+
/**
|
|
5
|
+
* Throws unless the matrix is square, finite, symmetric (as R's isSymmetric: relative difference from its transpose at
|
|
6
|
+
* most 100 ε) and has a positive diagonal. R's factanal takes any matrix: an asymmetric one gave a result
|
|
7
|
+
*/
|
|
8
|
+
export function checkCovmat(S) {
|
|
9
|
+
const p = S.length;
|
|
10
|
+
if (p === 0 || S.some((r) => r.length !== p))
|
|
11
|
+
throw new RangeError("'covmat' must be a square matrix");
|
|
12
|
+
if (S.some((r) => r.some((v) => !Number.isFinite(v))))
|
|
13
|
+
throw new RangeError("'covmat' must be finite");
|
|
14
|
+
if (S.some((r, i) => !(r[i] > 0)))
|
|
15
|
+
throw new RangeError("the diagonal of 'covmat' must be positive");
|
|
16
|
+
// in the matrix's own units, as R's isSymmetric (isSymmetric handles sums that overflow; the matrix scaled to its
|
|
17
|
+
// largest entry first took [[1e308, 1e-10], [0, 1]] for a symmetric one, issue #99)
|
|
18
|
+
if (!isSymmetric(toMat(S)))
|
|
19
|
+
throw new RangeError("'covmat' must be symmetric");
|
|
20
|
+
}
|
package/dist/internal/ddexp.d.ts
CHANGED
|
@@ -12,13 +12,35 @@ export declare function standardizeDD(x: number, location: number, scale: number
|
|
|
12
12
|
* subnormal, 0 or overflows (the log of a subnormal ratio has lost them in the division)
|
|
13
13
|
*/
|
|
14
14
|
export declare function logRatioDD(a: number, b: number): DD;
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* a b as double-double: twoProd, which keeps the error term of a finite product also where Veltkamp splitting would
|
|
17
|
+
* overflow (it splits the operands scaled by powers of 2); only the double product when that is not finite
|
|
18
|
+
*/
|
|
16
19
|
export declare function mulDD(a: number, b: number): DD;
|
|
17
20
|
/** exp(-t) (t is double-double). The low-order part of t enters to first order */
|
|
18
21
|
export declare function expNeg(t: DD): number;
|
|
22
|
+
/**
|
|
23
|
+
* exp(-t) / s (t double-double, s > 0) where exp(-t) is below the normal range and s small enough to bring the quotient
|
|
24
|
+
* back into it: exp(-t) itself would be rounded on the subnormal grid (a density formed so had lost digits:
|
|
25
|
+
* dlaplace(9.4863e-158, { mu: 9.4863e-158 + 4.5e-167, sigma: 6.04e-171 }) was 6e-4 off). With k = round(t / log 2) and
|
|
26
|
+
* s = f 2^j (f in [1, 2)), it is exp(-(t - k log 2)) / f times 2^(-k - j), the power of two applied last (in two
|
|
27
|
+
* steps, the first within the normal range), so that the only rounding into the subnormal range is the final one
|
|
28
|
+
*/
|
|
29
|
+
export declare function expNegOver(t: DD, s: number): number;
|
|
19
30
|
/** log(1 - exp(x)) (x <= 0). Switches method by the size of x to avoid cancellation */
|
|
20
31
|
export declare function log1mExp(x: number): number;
|
|
32
|
+
/** log(e^a + e^b) */
|
|
33
|
+
export declare function logAddExp(a: number, b: number): number;
|
|
34
|
+
/** log(e^a - e^b) for a >= b */
|
|
35
|
+
export declare function logSubExp(a: number, b: number): number;
|
|
21
36
|
/** Log of the lower probability 1 - exp(-t) (t double-double, t > 0). For large t, exp(-t) includes the low-order part of t */
|
|
22
37
|
export declare function logOneMinusExpNeg(t: DD): number;
|
|
23
38
|
/** location + scale t with a single rounding (the rounding of scale t would show up in the subtraction with location) */
|
|
24
39
|
export declare function locationScale(location: number, scale: number, t: number): number;
|
|
40
|
+
/**
|
|
41
|
+
* (x - location) / scale in double, as the plain formula gives it, but from the halves of x and location when their
|
|
42
|
+
* difference overflows although both are finite (z itself may be in range)
|
|
43
|
+
*/
|
|
44
|
+
export declare function standardizeValue(x: number, location: number, scale: number): number;
|
|
45
|
+
/** location + scale t in double, as the plain formula gives it, unless scale t overflows (then as locationScale) */
|
|
46
|
+
export declare function affine(location: number, scale: number, t: number): number;
|
package/dist/internal/ddexp.js
CHANGED
|
@@ -48,8 +48,14 @@ export function ddExp(x) {
|
|
|
48
48
|
*/
|
|
49
49
|
export function standardizeDD(x, location, scale) {
|
|
50
50
|
const diff = x - location;
|
|
51
|
-
if (!Number.isFinite(diff))
|
|
51
|
+
if (!Number.isFinite(diff)) {
|
|
52
|
+
// x - location overflows although both are finite: from the halves (exact), then times 2
|
|
53
|
+
if (Number.isFinite(x) && Number.isFinite(location)) {
|
|
54
|
+
const h = standardizeDD(x * 0.5, location * 0.5, scale);
|
|
55
|
+
return [h[0] * 2, h[1] * 2];
|
|
56
|
+
}
|
|
52
57
|
return [diff / scale, 0];
|
|
58
|
+
}
|
|
53
59
|
const d = location === 0 ? [x, 0] : twoSum(x, -location);
|
|
54
60
|
if (scale === 1)
|
|
55
61
|
return d;
|
|
@@ -86,11 +92,11 @@ function divideTail(d, scale, q1) {
|
|
|
86
92
|
export function logRatioDD(a, b) {
|
|
87
93
|
return ddSub(ddLog([a, 0]), ddLog([b, 0]));
|
|
88
94
|
}
|
|
89
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* a b as double-double: twoProd, which keeps the error term of a finite product also where Veltkamp splitting would
|
|
97
|
+
* overflow (it splits the operands scaled by powers of 2); only the double product when that is not finite
|
|
98
|
+
*/
|
|
90
99
|
export function mulDD(a, b) {
|
|
91
|
-
const p = a * b;
|
|
92
|
-
if (!Number.isFinite(p) || Math.abs(a) > 1e300 || Math.abs(b) > 1e300)
|
|
93
|
-
return [p, 0];
|
|
94
100
|
return twoProd(a, b);
|
|
95
101
|
}
|
|
96
102
|
/** exp(-t) (t is double-double). The low-order part of t enters to first order */
|
|
@@ -101,19 +107,93 @@ export function expNeg(t) {
|
|
|
101
107
|
}
|
|
102
108
|
return Math.exp(-t[0]) * (1 - t[1]);
|
|
103
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* exp(-t) / s (t double-double, s > 0) where exp(-t) is below the normal range and s small enough to bring the quotient
|
|
112
|
+
* back into it: exp(-t) itself would be rounded on the subnormal grid (a density formed so had lost digits:
|
|
113
|
+
* dlaplace(9.4863e-158, { mu: 9.4863e-158 + 4.5e-167, sigma: 6.04e-171 }) was 6e-4 off). With k = round(t / log 2) and
|
|
114
|
+
* s = f 2^j (f in [1, 2)), it is exp(-(t - k log 2)) / f times 2^(-k - j), the power of two applied last (in two
|
|
115
|
+
* steps, the first within the normal range), so that the only rounding into the subnormal range is the final one
|
|
116
|
+
*/
|
|
117
|
+
export function expNegOver(t, s) {
|
|
118
|
+
// (beyond t = 2000 the quotient is below e^-1255 even for the smallest s)
|
|
119
|
+
if (!(t[0] < 2000) || s === Infinity)
|
|
120
|
+
return 0;
|
|
121
|
+
let j = 0;
|
|
122
|
+
let f = s;
|
|
123
|
+
if (f < 2 ** -1000) {
|
|
124
|
+
f *= 2 ** 600;
|
|
125
|
+
j = -600;
|
|
126
|
+
}
|
|
127
|
+
const e = Math.floor(Math.log2(f));
|
|
128
|
+
f *= 2 ** -e;
|
|
129
|
+
j += e;
|
|
130
|
+
const k = Math.round(t[0] / LN2[0]);
|
|
131
|
+
const r = ddSub(t, ddMulNum(LN2, k));
|
|
132
|
+
const v = (Math.exp(-r[0]) * (1 - r[1])) / f;
|
|
133
|
+
const n = -k - j;
|
|
134
|
+
const h = Math.trunc(n / 2);
|
|
135
|
+
return v * 2 ** h * 2 ** (n - h);
|
|
136
|
+
}
|
|
104
137
|
/** log(1 - exp(x)) (x <= 0). Switches method by the size of x to avoid cancellation */
|
|
105
138
|
export function log1mExp(x) {
|
|
106
139
|
return x > -Math.LN2 ? Math.log(-Math.expm1(x)) : Math.log1p(-Math.exp(x));
|
|
107
140
|
}
|
|
141
|
+
/** log(e^a + e^b) */
|
|
142
|
+
export function logAddExp(a, b) {
|
|
143
|
+
const m = Math.max(a, b);
|
|
144
|
+
// both ends: -Inf + -Inf and Inf + Inf (Inf - Inf in the shift would be NaN)
|
|
145
|
+
if (m === -Infinity || m === Infinity)
|
|
146
|
+
return m;
|
|
147
|
+
return m + Math.log1p(Math.exp(Math.min(a, b) - m));
|
|
148
|
+
}
|
|
149
|
+
/** log(e^a - e^b) for a >= b */
|
|
150
|
+
export function logSubExp(a, b) {
|
|
151
|
+
return b === -Infinity ? a : a + log1mExp(b - a);
|
|
152
|
+
}
|
|
108
153
|
/** Log of the lower probability 1 - exp(-t) (t double-double, t > 0). For large t, exp(-t) includes the low-order part of t */
|
|
109
154
|
export function logOneMinusExpNeg(t) {
|
|
110
155
|
return t[0] > Math.LN2 ? Math.log1p(-expNeg(t)) : Math.log(-Math.expm1(-(t[0] + t[1])));
|
|
111
156
|
}
|
|
112
157
|
/** location + scale t with a single rounding (the rounding of scale t would show up in the subtraction with location) */
|
|
113
158
|
export function locationScale(location, scale, t) {
|
|
159
|
+
// scale = Inf at the median (t = 0): location, the limit (Inf times 0 would be NaN; other t give ±Inf)
|
|
160
|
+
if (t === 0 && scale === Infinity)
|
|
161
|
+
return location;
|
|
114
162
|
const st = mulDD(scale, t);
|
|
115
|
-
if (!Number.isFinite(st[0]) || !Number.isFinite(location))
|
|
163
|
+
if (!Number.isFinite(st[0]) || !Number.isFinite(location)) {
|
|
164
|
+
// scale t overflows although the three are finite: location and scale divided by a power of 2 (exact) first, the
|
|
165
|
+
// result multiplied back (finite whenever it is representable)
|
|
166
|
+
if (Number.isFinite(location) && Number.isFinite(scale) && Number.isFinite(t)) {
|
|
167
|
+
const k = Math.ceil(Math.log2(Math.abs(scale)));
|
|
168
|
+
const r = ddAdd(mulDD(ldexpSafe(scale, -k), t), [ldexpSafe(location, -k), 0]);
|
|
169
|
+
return ldexpSafe(r[0] + r[1], k);
|
|
170
|
+
}
|
|
116
171
|
return location + st[0];
|
|
172
|
+
}
|
|
117
173
|
const r = ddAdd(st, [location, 0]);
|
|
118
174
|
return r[0] + r[1];
|
|
119
175
|
}
|
|
176
|
+
/** v 2^k in two steps (2^k alone may overflow or underflow) */
|
|
177
|
+
function ldexpSafe(v, k) {
|
|
178
|
+
const a = Math.trunc(k / 2);
|
|
179
|
+
return v * 2 ** a * 2 ** (k - a);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* (x - location) / scale in double, as the plain formula gives it, but from the halves of x and location when their
|
|
183
|
+
* difference overflows although both are finite (z itself may be in range)
|
|
184
|
+
*/
|
|
185
|
+
export function standardizeValue(x, location, scale) {
|
|
186
|
+
const diff = x - location;
|
|
187
|
+
if (Number.isFinite(diff) || !Number.isFinite(x) || !Number.isFinite(location))
|
|
188
|
+
return diff / scale;
|
|
189
|
+
return ((x * 0.5 - location * 0.5) / scale) * 2;
|
|
190
|
+
}
|
|
191
|
+
/** location + scale t in double, as the plain formula gives it, unless scale t overflows (then as locationScale) */
|
|
192
|
+
export function affine(location, scale, t) {
|
|
193
|
+
if (t === 0 && scale === Infinity)
|
|
194
|
+
return location; // the limit at the median, as in locationScale
|
|
195
|
+
const st = scale * t;
|
|
196
|
+
if (Number.isFinite(st) || !Number.isFinite(scale) || !Number.isFinite(t) || !Number.isFinite(location))
|
|
197
|
+
return location + st;
|
|
198
|
+
return locationScale(location, scale, t);
|
|
199
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export declare const FUZZ_LINEAR: number;
|
|
2
|
+
export declare const FUZZ_LOG: number;
|
|
1
3
|
export interface DiscreteQuantileProblem {
|
|
2
4
|
/** Probability supplied by the user (0 < p < 1; log if logP) */
|
|
3
5
|
p: number;
|
|
@@ -12,3 +14,15 @@ export interface DiscreteQuantileProblem {
|
|
|
12
14
|
hi: number;
|
|
13
15
|
}
|
|
14
16
|
export declare function discreteQuantile(problem: DiscreteQuantileProblem): number;
|
|
17
|
+
/**
|
|
18
|
+
* For a predicate on the integers from lo on that is false up to some integer and true from there on: the first
|
|
19
|
+
* integer >= lo where it holds, at most hi + 1 (hi + 1 when none up to hi; hi may be Infinity). Found by doubling the
|
|
20
|
+
* step from lo and then bisecting, so in O(log) evaluations: the two-sided exact tests use it on the tail beyond the
|
|
21
|
+
* mode of a unimodal distribution, where "probability at most that of the observation" switches once
|
|
22
|
+
*/
|
|
23
|
+
export declare function firstTrue(pred: (i: number) => boolean, lo: number, hi: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* For a predicate on lo..hi that is true up to some integer and false from there on: the last integer <= hi where it
|
|
26
|
+
* holds (lo - 1 when none). By bisection (the mirror image of firstTrue, on a finite range)
|
|
27
|
+
*/
|
|
28
|
+
export declare function lastTrue(pred: (i: number) => boolean, lo: number, hi: number): number;
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
// The slack is applied to p on the scale the user supplied (lowerTail, logP). Its size is 8 ε on the linear scale
|
|
7
7
|
// and 2 ε on the log scale, both chosen by comparison with R's answers (for p ≈ 1, converting to the smaller tail
|
|
8
8
|
// before testing gives answers off by 1; the slack must be larger than 1.8 ε and smaller than 49 ε).
|
|
9
|
-
const FUZZ_LINEAR = 8 * Number.EPSILON;
|
|
10
|
-
const FUZZ_LOG = 2 * Number.EPSILON;
|
|
9
|
+
export const FUZZ_LINEAR = 8 * Number.EPSILON;
|
|
10
|
+
export const FUZZ_LOG = 2 * Number.EPSILON;
|
|
11
11
|
/** Probabilities below this are near subnormal with few significant digits, so compare on the log scale */
|
|
12
12
|
const TINY = 1e-290;
|
|
13
13
|
export function discreteQuantile(problem) {
|
|
@@ -83,9 +83,19 @@ export function discreteQuantile(problem) {
|
|
|
83
83
|
y = good;
|
|
84
84
|
step *= 2;
|
|
85
85
|
good = y + step;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
if (good === Infinity && hi === Infinity) {
|
|
87
|
+
// The step overflowed: the quantile may still lie below the largest double (a threshold at 1.7e308 searched from
|
|
88
|
+
// 1 gave Infinity), so test that before concluding it lies beyond the doubles. (With a finite hi, the loop ends
|
|
89
|
+
// and the search below runs up to hi)
|
|
90
|
+
const top = Number.MAX_VALUE;
|
|
91
|
+
const atTop = ok(top);
|
|
92
|
+
if (nan)
|
|
93
|
+
return NaN;
|
|
94
|
+
if (!atTop)
|
|
95
|
+
return hi;
|
|
96
|
+
good = top;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
89
99
|
}
|
|
90
100
|
if (good > hi)
|
|
91
101
|
good = hi;
|
|
@@ -106,3 +116,66 @@ function bisect(bad, good, ok, stop) {
|
|
|
106
116
|
}
|
|
107
117
|
return good;
|
|
108
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* For a predicate on the integers from lo on that is false up to some integer and true from there on: the first
|
|
121
|
+
* integer >= lo where it holds, at most hi + 1 (hi + 1 when none up to hi; hi may be Infinity). Found by doubling the
|
|
122
|
+
* step from lo and then bisecting, so in O(log) evaluations: the two-sided exact tests use it on the tail beyond the
|
|
123
|
+
* mode of a unimodal distribution, where "probability at most that of the observation" switches once
|
|
124
|
+
*/
|
|
125
|
+
export function firstTrue(pred, lo, hi) {
|
|
126
|
+
if (lo > hi || pred(lo))
|
|
127
|
+
return lo;
|
|
128
|
+
let fails = lo; // largest integer known not to hold
|
|
129
|
+
let step = 1;
|
|
130
|
+
let j = lo + 1; // an integer known to hold, or hi + 1 (taken as holding, never evaluated)
|
|
131
|
+
for (;;) {
|
|
132
|
+
if (j > hi) {
|
|
133
|
+
j = hi + 1;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
if (pred(j))
|
|
137
|
+
break;
|
|
138
|
+
fails = j;
|
|
139
|
+
if (j === Number.MAX_VALUE)
|
|
140
|
+
return hi + 1;
|
|
141
|
+
step *= 2;
|
|
142
|
+
j = lo + step;
|
|
143
|
+
// past the largest double (hi = Infinity): try it once; Infinity was taken as the bound (a finite threshold gave
|
|
144
|
+
// Infinity, and a predicate false everywhere never stopped)
|
|
145
|
+
if (j === Infinity)
|
|
146
|
+
j = Number.MAX_VALUE;
|
|
147
|
+
}
|
|
148
|
+
while (j - fails > 1) {
|
|
149
|
+
const mid = fails + Math.floor((j - fails) / 2);
|
|
150
|
+
// beyond 2^53 the midpoint can round to an end, which then never moved (binomTest(0, 1e20) did not return)
|
|
151
|
+
if (mid === fails || mid === j)
|
|
152
|
+
break;
|
|
153
|
+
if (pred(mid))
|
|
154
|
+
j = mid;
|
|
155
|
+
else
|
|
156
|
+
fails = mid;
|
|
157
|
+
}
|
|
158
|
+
return j;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* For a predicate on lo..hi that is true up to some integer and false from there on: the last integer <= hi where it
|
|
162
|
+
* holds (lo - 1 when none). By bisection (the mirror image of firstTrue, on a finite range)
|
|
163
|
+
*/
|
|
164
|
+
export function lastTrue(pred, lo, hi) {
|
|
165
|
+
if (lo > hi || !pred(lo))
|
|
166
|
+
return lo - 1;
|
|
167
|
+
if (pred(hi))
|
|
168
|
+
return hi;
|
|
169
|
+
let j = lo;
|
|
170
|
+
let fails = hi; // smallest integer known not to hold
|
|
171
|
+
while (fails - j > 1) {
|
|
172
|
+
const mid = j + Math.floor((fails - j) / 2);
|
|
173
|
+
if (mid === fails || mid === j)
|
|
174
|
+
break; // (as in firstTrue)
|
|
175
|
+
if (pred(mid))
|
|
176
|
+
j = mid;
|
|
177
|
+
else
|
|
178
|
+
fails = mid;
|
|
179
|
+
}
|
|
180
|
+
return j;
|
|
181
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Math.max(...v) without spreading (-Infinity for an empty array) */
|
|
2
|
+
export declare function maxOf(v: ArrayLike<number>): number;
|
|
3
|
+
/**
|
|
4
|
+
* (a + b) / 2, falling back to a / 2 + b / 2 only when a and b are finite and their sum overflows (so that the
|
|
5
|
+
* value is the usual one otherwise)
|
|
6
|
+
*/
|
|
7
|
+
export declare function midpoint(a: number, b: number): number;
|
|
8
|
+
/** Math.min(...v) without spreading (Infinity for an empty array) */
|
|
9
|
+
export declare function minOf(v: ArrayLike<number>): number;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Largest and smallest values of an array, as Math.max(...v) and Math.min(...v) (NaN if any value is NaN) but
|
|
2
|
+
// without spreading the array into arguments, which overflows the call stack for large arrays.
|
|
3
|
+
/** Math.max(...v) without spreading (-Infinity for an empty array) */
|
|
4
|
+
export function maxOf(v) {
|
|
5
|
+
let m = -Infinity;
|
|
6
|
+
for (let i = 0; i < v.length; i++)
|
|
7
|
+
m = Math.max(m, v[i]);
|
|
8
|
+
return m;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* (a + b) / 2, falling back to a / 2 + b / 2 only when a and b are finite and their sum overflows (so that the
|
|
12
|
+
* value is the usual one otherwise)
|
|
13
|
+
*/
|
|
14
|
+
export function midpoint(a, b) {
|
|
15
|
+
const m = 0.5 * (a + b);
|
|
16
|
+
return Number.isFinite(m) || !Number.isFinite(a) || !Number.isFinite(b) ? m : 0.5 * a + 0.5 * b;
|
|
17
|
+
}
|
|
18
|
+
/** Math.min(...v) without spreading (Infinity for an empty array) */
|
|
19
|
+
export function minOf(v) {
|
|
20
|
+
let m = Infinity;
|
|
21
|
+
for (let i = 0; i < v.length; i++)
|
|
22
|
+
m = Math.min(m, v[i]);
|
|
23
|
+
return m;
|
|
24
|
+
}
|
package/dist/internal/fround.js
CHANGED
|
@@ -17,12 +17,21 @@ function binaryExponent(x) {
|
|
|
17
17
|
const bits = hi !== 0 ? 32 + (31 - Math.clz32(hi)) : 31 - Math.clz32(lo);
|
|
18
18
|
return bits - 1074;
|
|
19
19
|
}
|
|
20
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* 10^k for an integer k >= 0 by squaring, as R's round forms it (k multiplications by 10 rounded differently from 10^23
|
|
22
|
+
* on and moved the result: round(0x1.0b7fa925827cbp-287, 100) and round(0x1.ec75dfa8b17cbp+84, -25); issue #101)
|
|
23
|
+
*/
|
|
21
24
|
function powerOf10(k) {
|
|
22
25
|
let p = 1;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
let b = 10;
|
|
27
|
+
for (let n = k;;) {
|
|
28
|
+
if (n & 1)
|
|
29
|
+
p *= b;
|
|
30
|
+
n >>= 1;
|
|
31
|
+
if (n === 0)
|
|
32
|
+
return p;
|
|
33
|
+
b *= b;
|
|
34
|
+
}
|
|
26
35
|
}
|
|
27
36
|
/** The nearest integer, ties to even (C's nearbyint in the default rounding mode); the sign of x is kept */
|
|
28
37
|
function roundHalfEven(x) {
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
/** Whether x is not an integer (relative slack 1e-9) */
|
|
2
2
|
export declare function nonInt(x: number): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* The largest count k with k <= q, for the distribution functions of discrete distributions on the integers. A q within
|
|
5
|
+
* min(1e-7, 1e-9 max(1, |q|)) of an integer is that integer (a computed 2.9999999999999996 counts as 3); any other q
|
|
6
|
+
* gives floor(q). R adds 1e-7 before taking the floor (or, in psignrank, rounding to the nearest integer), which counts
|
|
7
|
+
* q in [k - 1e-7, k) as k although P[X <= q] is P[X <= k - 1] there, and does not agree with its densities
|
|
8
|
+
* (dpois(1 - 1e-8, 1) is 0 while ppois(1 - 1e-8, 1) is P[X <= 1]) (#145, #146). The slack of {@link nonInt} alone
|
|
9
|
+
* would be wider than R's above q = 100 (ppois(1000 - 5e-7, 1000) would be P[X <= 1000]), so it is capped at 1e-7.
|
|
10
|
+
*/
|
|
11
|
+
export declare function countAtMost(q: number): number;
|
|
3
12
|
/** Round half to even (0.5 goes to the nearest even number) */
|
|
4
13
|
export declare function roundHalfEven(x: number): number;
|