@numero-uno-jpn/rstats 0.15.3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +166 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-VLUL2HFW.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +24 -4
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +12 -4
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +5 -0
- package/dist/distributions/f.js +32 -17
- package/dist/distributions/gamma.js +27 -3
- package/dist/distributions/geometric.js +49 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +2 -1
- package/dist/distributions/laplace.js +27 -6
- package/dist/distributions/logistic.js +4 -1
- package/dist/distributions/lognormal.js +8 -2
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +94 -17
- package/dist/distributions/normal.js +9 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +4 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/triangular.js +10 -5
- package/dist/distributions/tukey.js +5 -2
- package/dist/distributions/weibull.js +10 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/random/extra.js
CHANGED
|
@@ -11,6 +11,7 @@ import { qpareto1 } from "../distributions/pareto.js";
|
|
|
11
11
|
import { generate } from "./common.js";
|
|
12
12
|
import { rgamma1 } from "./gamma.js";
|
|
13
13
|
import { expRand, unifRand } from "./rng.js";
|
|
14
|
+
import { midpoint } from "../internal/extremes.js";
|
|
14
15
|
/**
|
|
15
16
|
* Random numbers from the inverse gamma distribution. Equivalent to `extraDistr::rinvgamma(n, alpha, beta = 1)` in R.
|
|
16
17
|
*
|
|
@@ -28,7 +29,13 @@ import { expRand, unifRand } from "./rng.js";
|
|
|
28
29
|
*/
|
|
29
30
|
export function rinvgamma(n, alpha, options = {}) {
|
|
30
31
|
const { beta = 1 } = options;
|
|
31
|
-
|
|
32
|
+
// alpha = Inf is the limit 0, as qinvgamma (extraDistr's 1/rgamma: no uniform drawn); beta = Inf is NaN, as the
|
|
33
|
+
// density, distribution and quantile functions, also without a draw (extraDistr: Inf; issue #92)
|
|
34
|
+
return generate(n, () => {
|
|
35
|
+
if (!(alpha > 0 && beta > 0) || beta === Infinity)
|
|
36
|
+
return NaN;
|
|
37
|
+
return alpha === Infinity ? 0 : 1 / rgamma1(alpha, 1 / beta);
|
|
38
|
+
});
|
|
32
39
|
}
|
|
33
40
|
/**
|
|
34
41
|
* Random numbers from the Kumaraswamy distribution. Equivalent to `extraDistr::rkumar(n, a, b)` in R.
|
|
@@ -42,7 +49,14 @@ export function rinvgamma(n, alpha, options = {}) {
|
|
|
42
49
|
* @category Random numbers
|
|
43
50
|
*/
|
|
44
51
|
export function rkumar(n, a, b) {
|
|
45
|
-
|
|
52
|
+
// a = Inf is the limit 1, as qkumar; b = Inf is NaN, as dkumar, pkumar and qkumar, after the uniform extraDistr draws
|
|
53
|
+
// (it gives 0; issue #92)
|
|
54
|
+
return generate(n, () => {
|
|
55
|
+
if (!(a > 0 && b > 0))
|
|
56
|
+
return NaN;
|
|
57
|
+
const u = unifRand();
|
|
58
|
+
return b === Infinity ? NaN : qkumar1(1 - u, a, b, true, false);
|
|
59
|
+
});
|
|
46
60
|
}
|
|
47
61
|
/**
|
|
48
62
|
* Random numbers from the Pareto distribution. Equivalent to `extraDistr::rpareto(n, a = 1, b = 1)` in R.
|
|
@@ -57,7 +71,14 @@ export function rkumar(n, a, b) {
|
|
|
57
71
|
*/
|
|
58
72
|
export function rpareto(n, options = {}) {
|
|
59
73
|
const { a = 1, b = 1 } = options;
|
|
60
|
-
|
|
74
|
+
// a = Inf is the limit b, as qpareto; b = Inf is NaN, as dpareto, ppareto and qpareto, after the uniform extraDistr
|
|
75
|
+
// draws (it gives Inf; issue #92)
|
|
76
|
+
return generate(n, () => {
|
|
77
|
+
if (!(a > 0 && b > 0))
|
|
78
|
+
return NaN;
|
|
79
|
+
const u = unifRand();
|
|
80
|
+
return b === Infinity ? NaN : qpareto1(1 - u, a, b, true, false);
|
|
81
|
+
});
|
|
61
82
|
}
|
|
62
83
|
/**
|
|
63
84
|
* Random numbers from the triangular distribution.
|
|
@@ -73,14 +94,23 @@ export function rpareto(n, options = {}) {
|
|
|
73
94
|
*/
|
|
74
95
|
export function rtriang(n, options = {}) {
|
|
75
96
|
const { a = -1, b = 1 } = options;
|
|
76
|
-
const c = options.c ?? (a
|
|
97
|
+
const c = options.c ?? midpoint(a, b);
|
|
77
98
|
return generate(n, () => {
|
|
78
|
-
if (!(
|
|
99
|
+
if (!(a < b && a <= c && c <= b))
|
|
79
100
|
return NaN;
|
|
80
101
|
const u = unifRand();
|
|
81
102
|
const v = unifRand();
|
|
82
|
-
|
|
83
|
-
|
|
103
|
+
// an infinite endpoint is NaN, as dtriang, ptriang and qtriang, after the two uniforms extraDistr draws (it gives
|
|
104
|
+
// Inf for b = Inf with a finite c, NaN otherwise; issue #428)
|
|
105
|
+
if (!(Number.isFinite(a) && Number.isFinite(b)))
|
|
106
|
+
return NaN;
|
|
107
|
+
// b - a overflows for a range wider than the largest double: then from the halves of a, b and c (exact), the
|
|
108
|
+
// result doubled back (it lies in [a, b], so it is finite)
|
|
109
|
+
const wide = !Number.isFinite(b - a);
|
|
110
|
+
const [ah, bh, ch] = wide ? [a / 2, b / 2, c / 2] : [a, b, c];
|
|
111
|
+
const cc = (ch - ah) / (bh - ah);
|
|
112
|
+
const t = ah + (bh - ah) * ((1 - cc) * Math.min(u, v) + cc * Math.max(u, v));
|
|
113
|
+
return wide ? t * 2 : t;
|
|
84
114
|
});
|
|
85
115
|
}
|
|
86
116
|
/**
|
|
@@ -96,12 +126,15 @@ export function rtriang(n, options = {}) {
|
|
|
96
126
|
*/
|
|
97
127
|
export function rlaplace(n, options = {}) {
|
|
98
128
|
const { mu = 0, sigma = 1 } = options;
|
|
129
|
+
// an infinite mu is the limit mu, as qlaplace; sigma = Inf with a finite mu is -Inf or Inf by the sign, the limit of
|
|
130
|
+
// qlaplace, as extraDistr (review 24 revision); both infinite is NaN, as qlaplace, after the draws extraDistr makes
|
|
131
|
+
// (issue #425)
|
|
99
132
|
return generate(n, () => {
|
|
100
|
-
if (
|
|
133
|
+
if (Number.isNaN(mu) || !(sigma > 0))
|
|
101
134
|
return NaN;
|
|
102
135
|
const e = expRand();
|
|
103
136
|
const s = unifRand() > 0.5 ? 1 : -1;
|
|
104
|
-
return mu + sigma * s * e;
|
|
137
|
+
return sigma === Infinity && !Number.isFinite(mu) ? NaN : mu + sigma * s * e;
|
|
105
138
|
});
|
|
106
139
|
}
|
|
107
140
|
/**
|
package/dist/random/gamma.d.ts
CHANGED
|
@@ -22,7 +22,10 @@ export declare function rgamma(n: number, shape: number, options?: {
|
|
|
22
22
|
scale?: number;
|
|
23
23
|
}): number[];
|
|
24
24
|
export declare function rchisq1(df: number): number;
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* One noncentral χ² variate: Poisson mixture (χ² with df + 2 × Poisson(ncp/2) degrees of freedom). df = Inf gives Inf
|
|
27
|
+
* after the Poisson (and χ²) variates, as R's rnchisq (which rejects only a NaN df); the central rchisq1 gives NaN there
|
|
28
|
+
*/
|
|
26
29
|
export declare function rnchisq1(df: number, ncp: number): number;
|
|
27
30
|
/**
|
|
28
31
|
* Random numbers from the chi-squared distribution. Equivalent to R's `rchisq()`.
|
package/dist/random/gamma.js
CHANGED
|
@@ -10,6 +10,8 @@ import { gammaScale } from "../distributions/gamma.js";
|
|
|
10
10
|
import { generate } from "./common.js";
|
|
11
11
|
import { rpois1 } from "./poisson.js";
|
|
12
12
|
import { rnorm1 } from "./continuous.js";
|
|
13
|
+
import { ddExp } from "../internal/ddexp.js";
|
|
14
|
+
import { ddAdd, ddDivNum, ddLog } from "../special/dd.js";
|
|
13
15
|
const SQRT32 = 5.656854;
|
|
14
16
|
const EXP_M1 = 0.36787944117144233; // exp(-1)
|
|
15
17
|
// q_k and a_k of algorithm GD (tables in the paper)
|
|
@@ -33,16 +35,21 @@ function qOf(t) {
|
|
|
33
35
|
export function rgamma1(a, scale) {
|
|
34
36
|
if (Number.isNaN(a) || Number.isNaN(scale))
|
|
35
37
|
return NaN;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
// a negative shape or scale defines no distribution, also when the other is 0 (R returns 0 there; no variate is
|
|
39
|
+
// drawn either way, so the stream is R's); shape 0 or scale 0 is the point mass at 0
|
|
40
|
+
if (a < 0 || scale < 0)
|
|
41
|
+
return NaN;
|
|
42
|
+
if (a === 0 || scale === 0)
|
|
43
|
+
return 0;
|
|
38
44
|
if (!Number.isFinite(a) || !Number.isFinite(scale))
|
|
39
45
|
return Infinity;
|
|
40
46
|
if (a < 1) {
|
|
41
47
|
// Algorithm GS
|
|
42
48
|
const e = 1 + EXP_M1 * a;
|
|
43
49
|
let x;
|
|
50
|
+
let p;
|
|
44
51
|
for (;;) {
|
|
45
|
-
|
|
52
|
+
p = e * unifRand();
|
|
46
53
|
if (p >= 1) {
|
|
47
54
|
x = -Math.log((e - p) / a);
|
|
48
55
|
if (expRand() >= (1 - a) * Math.log(x))
|
|
@@ -54,6 +61,10 @@ export function rgamma1(a, scale) {
|
|
|
54
61
|
break;
|
|
55
62
|
}
|
|
56
63
|
}
|
|
64
|
+
// x = p^(1/a) (p < 1) can fall below the normal range, or to 0, although a large scale brings the variate back
|
|
65
|
+
// into it (R gives 0): scale p^(1/a) from the logs in double-double then (the acceptance above is unchanged)
|
|
66
|
+
if (x < 2 ** -1022 && scale > 1)
|
|
67
|
+
return ddExp(ddAdd(ddLog([scale, 0]), ddDivNum(ddLog([p, 0]), a)))[0];
|
|
57
68
|
return scale * x;
|
|
58
69
|
}
|
|
59
70
|
// Algorithm GD. Step 1: rebuild s and d if the parameters changed
|
|
@@ -144,9 +155,12 @@ export function rchisq1(df) {
|
|
|
144
155
|
return NaN;
|
|
145
156
|
return rgamma1(df / 2, 2);
|
|
146
157
|
}
|
|
147
|
-
/**
|
|
158
|
+
/**
|
|
159
|
+
* One noncentral χ² variate: Poisson mixture (χ² with df + 2 × Poisson(ncp/2) degrees of freedom). df = Inf gives Inf
|
|
160
|
+
* after the Poisson (and χ²) variates, as R's rnchisq (which rejects only a NaN df); the central rchisq1 gives NaN there
|
|
161
|
+
*/
|
|
148
162
|
export function rnchisq1(df, ncp) {
|
|
149
|
-
if (
|
|
163
|
+
if (Number.isNaN(df) || !Number.isFinite(ncp) || df < 0 || ncp < 0)
|
|
150
164
|
return NaN;
|
|
151
165
|
if (ncp === 0)
|
|
152
166
|
return df === 0 ? 0 : rgamma1(df / 2, 2);
|
|
@@ -212,6 +226,10 @@ export function rt(n, df, options = {}) {
|
|
|
212
226
|
if (Number.isNaN(ncp))
|
|
213
227
|
return generate(n, () => NaN);
|
|
214
228
|
const z = generate(n, () => rnorm1(ncp, 1));
|
|
229
|
+
// df = Inf: V / df is 1 in the limit, so T = Z + ncp ~ N(ncp, 1) (R gives NaN, as its rchisq(n, Inf) does). No χ²
|
|
230
|
+
// variates are drawn then, as R's rchisq(n, Inf) draws none, so the stream stays R's
|
|
231
|
+
if (df === Infinity)
|
|
232
|
+
return z;
|
|
215
233
|
const v = rchisq(n, df);
|
|
216
234
|
return z.map((zi, i) => zi / Math.sqrt(v[i] / df));
|
|
217
235
|
}
|
|
@@ -244,7 +262,17 @@ export function rf(n, df1, df2, options = {}) {
|
|
|
244
262
|
});
|
|
245
263
|
}
|
|
246
264
|
// Noncentral F: as in R, generate n noncentral χ² variates first, then n χ² variates
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
265
|
+
// An infinite df makes that χ² over its df 1 in the limit (for a finite ncp >= 0: mean (df + ncp) / df, variance
|
|
266
|
+
// (2 df + 4 ncp) / df^2): F is then X / df1, df2 / Y or 1 (R gives NaN). The variates R draws are drawn, so the
|
|
267
|
+
// stream stays R's: the noncentral χ² with df1 = Inf draws its Poisson mixture (R's value Inf, used here only for
|
|
268
|
+
// its draws and NaN), the central one with df2 = Inf draws none
|
|
269
|
+
const x = df1 === Infinity
|
|
270
|
+
? rchisq(n, df1, { ncp }).map((v) => (Number.isNaN(v) ? NaN : 1))
|
|
271
|
+
: rchisq(n, df1, { ncp }).map((v) => v / df1);
|
|
272
|
+
const y = df2 === Infinity ? generate(n, () => 1) : rchisq(n, df2).map((v) => v / df2);
|
|
273
|
+
// F needs df1 > 0 and df2 > 0 (the central branch gives NaN otherwise). R divides by df1 = 0 here and returns Inf or
|
|
274
|
+
// NaN depending on the draw; NaN throughout, with the same variates drawn (so the stream stays R's)
|
|
275
|
+
if (!(df1 > 0 && df2 > 0))
|
|
276
|
+
return x.map(() => NaN);
|
|
277
|
+
return x.map((xi, i) => xi / y[i]);
|
|
250
278
|
}
|
package/dist/random/hyper.js
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
// The order in which uniforms are consumed and the parameter handling follow observed R output.
|
|
7
7
|
import { unifRand } from "./rng.js";
|
|
8
8
|
import { generate } from "./common.js";
|
|
9
|
+
import { roundHalfEven } from "../internal/integer.js";
|
|
9
10
|
import { rbinom1 } from "./binomial.js";
|
|
10
|
-
import { qhyper1 } from "../distributions/hypergeometric.js";
|
|
11
|
+
import { qhyper1, whiteFraction } from "../distributions/hypergeometric.js";
|
|
11
12
|
const INT_MAX = 2147483647;
|
|
12
13
|
const LN_SQRT_2PI = 0.918938533204672741780329736406;
|
|
13
14
|
const DELTAL = 0.0078;
|
|
@@ -41,16 +42,18 @@ let a = 0, d = 0, xl = 0, xr = 0, kl = 0, kr = 0, lamdl = 0, lamdr = 0, p1 = 0,
|
|
|
41
42
|
export function rhyper1(nn1in, nn2in, kkin) {
|
|
42
43
|
if (!Number.isFinite(nn1in) || !Number.isFinite(nn2in) || !Number.isFinite(kkin))
|
|
43
44
|
return NaN;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
// as R's R_forceint (nearbyint): halves go to the even integer (2.5 is 2, as in R; Math.round made it 3)
|
|
46
|
+
nn1in = roundHalfEven(nn1in);
|
|
47
|
+
nn2in = roundHalfEven(nn2in);
|
|
48
|
+
kkin = roundHalfEven(kkin);
|
|
47
49
|
if (nn1in < 0 || nn2in < 0 || kkin < 0 || kkin > nn1in + nn2in)
|
|
48
50
|
return NaN;
|
|
49
51
|
if (nn1in >= INT_MAX || nn2in >= INT_MAX || kkin >= INT_MAX) {
|
|
50
52
|
// Parameters beyond the integer range: Bernoulli for a single draw, otherwise invert via the quantile function
|
|
53
|
+
// (the white fraction without forming nn1 + nn2, which overflows near the largest double: it was 0)
|
|
51
54
|
if (kkin === 1)
|
|
52
|
-
return rbinom1(kkin,
|
|
53
|
-
return qhyper1(unifRand(), nn1in, nn2in, kkin, false, false);
|
|
55
|
+
return rbinom1(kkin, whiteFraction(nn1in, nn2in));
|
|
56
|
+
return qhyper1(unifRand(), nn1in, nn2in, kkin, false, false, false);
|
|
54
57
|
}
|
|
55
58
|
const nn1 = nn1in;
|
|
56
59
|
const nn2 = nn2in;
|
package/dist/random/mt.js
CHANGED
|
@@ -44,7 +44,8 @@ export class MersenneTwister {
|
|
|
44
44
|
/** One 32-bit unsigned integer */
|
|
45
45
|
nextUint32() {
|
|
46
46
|
const mt = this.mt;
|
|
47
|
-
|
|
47
|
+
// (mti is 0 here only when a state with position 0 was set: R regenerates for it, as for 624; issue #90)
|
|
48
|
+
if (this.mti >= N || this.mti === 0) {
|
|
48
49
|
if (this.mti === N + 1)
|
|
49
50
|
this.sgenrand(4357);
|
|
50
51
|
let kk = 0;
|
|
@@ -89,7 +90,21 @@ export class MersenneTwister {
|
|
|
89
90
|
setRandomSeed(state) {
|
|
90
91
|
if (state.length !== N + 2)
|
|
91
92
|
throw new RangeError(`the random seed state must have ${N + 2} integers`);
|
|
92
|
-
|
|
93
|
+
// the position must be an integer from 0 to 2^31 - 1, as R's integer .Random.seed holds it (getRandomSeed returns it
|
|
94
|
+
// in an Int32Array). 0, and a position past N, make the next draw regenerate, as in R. R takes a negative one as
|
|
95
|
+
// given and its first draw then reads that slot itself as a state word; a corrupted state is refused here instead
|
|
96
|
+
// of producing numbers from it
|
|
97
|
+
const mti = state[1];
|
|
98
|
+
if (!Number.isInteger(mti) || mti < 0 || mti > 2 ** 31 - 1) {
|
|
99
|
+
throw new RangeError("the position in the random seed state must be a non-negative 32-bit integer");
|
|
100
|
+
}
|
|
101
|
+
for (let i = 0; i < N; i++) {
|
|
102
|
+
const v = state[i + 2];
|
|
103
|
+
// 32-bit words, as R's integer .Random.seed holds them (signed or unsigned); others were reduced modulo 2^32
|
|
104
|
+
if (!Number.isInteger(v) || v < -(2 ** 31) || v >= 2 ** 32)
|
|
105
|
+
throw new RangeError("the random seed state must hold 32-bit integers");
|
|
106
|
+
}
|
|
107
|
+
this.mti = mti;
|
|
93
108
|
for (let i = 0; i < N; i++)
|
|
94
109
|
this.mt[i] = state[i + 2] >>> 0;
|
|
95
110
|
}
|
package/dist/random/poisson.js
CHANGED
|
@@ -185,9 +185,27 @@ export function rgeom(n, prob) {
|
|
|
185
185
|
return generate(n, () => {
|
|
186
186
|
if (!Number.isFinite(prob) || prob <= 0 || prob > 1)
|
|
187
187
|
return NaN;
|
|
188
|
-
|
|
188
|
+
// for prob below about 5.6e-309, (1 - prob) / prob overflows while E (1 - prob) / prob is finite unless E is large
|
|
189
|
+
// (with probability 0.16 at prob = 1e-309; R gives NaN): the exponential draw first. A mean that overflows
|
|
190
|
+
// itself gives Infinity
|
|
191
|
+
const odds = (1 - prob) / prob;
|
|
192
|
+
const e = expRand();
|
|
193
|
+
const mu = Number.isFinite(odds) ? e * odds : (e * (1 - prob)) / prob;
|
|
194
|
+
return mu === Infinity ? Infinity : rpois1(mu);
|
|
189
195
|
});
|
|
190
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* A Poisson variate whose mean is a gamma variate with shape sz and scale num / den. Where the scale overflows (prob
|
|
199
|
+
* below about 5.6e-309, or mu / size; R gives NaN) the unit gamma variate is multiplied by num and divided by den, which
|
|
200
|
+
* is finite unless the variate is large; a mean that overflows itself gives Infinity
|
|
201
|
+
*/
|
|
202
|
+
function poissonOfGamma(sz, num, den) {
|
|
203
|
+
const scale = num / den;
|
|
204
|
+
if (Number.isFinite(scale))
|
|
205
|
+
return rpois1(rgamma1(sz, scale));
|
|
206
|
+
const m = (rgamma1(sz, 1) * num) / den;
|
|
207
|
+
return m === Infinity ? Infinity : rpois1(m);
|
|
208
|
+
}
|
|
191
209
|
/**
|
|
192
210
|
* Random numbers from the negative binomial distribution. Equivalent to R's `rnbinom()`.
|
|
193
211
|
*
|
|
@@ -216,13 +234,18 @@ export function rnbinom(n, size, options) {
|
|
|
216
234
|
return generate(n, () => {
|
|
217
235
|
if (!Number.isFinite(mu) || Number.isNaN(size) || size <= 0 || mu < 0)
|
|
218
236
|
return NaN;
|
|
219
|
-
return mu === 0 ? 0 :
|
|
237
|
+
return mu === 0 ? 0 : poissonOfGamma(sz, mu, sz);
|
|
220
238
|
});
|
|
221
239
|
}
|
|
222
240
|
const p = prob;
|
|
223
241
|
return generate(n, () => {
|
|
224
242
|
if (!Number.isFinite(p) || Number.isNaN(size) || size <= 0 || p <= 0 || p > 1)
|
|
225
243
|
return NaN;
|
|
226
|
-
|
|
244
|
+
if (p === 1)
|
|
245
|
+
return 0;
|
|
246
|
+
const v = poissonOfGamma(sz, 1 - p, p);
|
|
247
|
+
// size = Inf with a fixed prob < 1 puts all the probability at infinity (dnbinom 0, qnbinom Inf): Infinity, where R
|
|
248
|
+
// returns the draw for size = MAX / 2 (about 9e307); the same variates are drawn, so the stream stays R's
|
|
249
|
+
return size === Infinity ? Infinity : v;
|
|
227
250
|
});
|
|
228
251
|
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The largest grand total for which the table of log factorials (total + 1 doubles, 128 MB at the limit) is built:
|
|
3
|
+
* r2dtable and the simulated p-values of chisqTest and fisherTest (#285, #286, #336); the same 2^24 as the exact
|
|
4
|
+
* supports of fisherTest and mantelhaenTest (#232)
|
|
5
|
+
*/
|
|
6
|
+
export declare const MAX_TABLE_TOTAL: number;
|
|
1
7
|
/** log k! for k = 0..n, accumulated as sums of logs (the table AS 159 uses) */
|
|
2
8
|
export declare function logFactorialTable(n: number): Float64Array;
|
|
3
9
|
/**
|
|
@@ -15,7 +21,8 @@ export declare function rcont2(r: readonly number[], c: readonly number[], fact:
|
|
|
15
21
|
* @param r Row totals (non-negative integers, at least two).
|
|
16
22
|
* @param c Column totals (non-negative integers, at least two), with the same sum as `r`.
|
|
17
23
|
* @returns An array of `n` tables, each `r.length` rows of `c.length` counts.
|
|
18
|
-
* @throws RangeError for invalid arguments or totals that differ
|
|
24
|
+
* @throws RangeError for invalid arguments or totals that differ, and (when n >= 1) for a grand total above 2^24
|
|
25
|
+
* (16777216; R allows up to 2147483647), as the table of log factorials has total + 1 entries.
|
|
19
26
|
*
|
|
20
27
|
* @example
|
|
21
28
|
* ```ts
|
package/dist/random/r2dtable.js
CHANGED
|
@@ -12,6 +12,12 @@
|
|
|
12
12
|
// whose remaining total ie is 0 (all later rows and columns empty) is 0 without a draw. This consumes uniforms as R's
|
|
13
13
|
// r2dtable does (checked against R's output).
|
|
14
14
|
import { unifRand } from "./rng.js";
|
|
15
|
+
/**
|
|
16
|
+
* The largest grand total for which the table of log factorials (total + 1 doubles, 128 MB at the limit) is built:
|
|
17
|
+
* r2dtable and the simulated p-values of chisqTest and fisherTest (#285, #286, #336); the same 2^24 as the exact
|
|
18
|
+
* supports of fisherTest and mantelhaenTest (#232)
|
|
19
|
+
*/
|
|
20
|
+
export const MAX_TABLE_TOTAL = 2 ** 24;
|
|
15
21
|
/** log k! for k = 0..n, accumulated as sums of logs (the table AS 159 uses) */
|
|
16
22
|
export function logFactorialTable(n) {
|
|
17
23
|
const f = new Float64Array(n + 1);
|
|
@@ -125,7 +131,8 @@ function margins(v, name) {
|
|
|
125
131
|
* @param r Row totals (non-negative integers, at least two).
|
|
126
132
|
* @param c Column totals (non-negative integers, at least two), with the same sum as `r`.
|
|
127
133
|
* @returns An array of `n` tables, each `r.length` rows of `c.length` counts.
|
|
128
|
-
* @throws RangeError for invalid arguments or totals that differ
|
|
134
|
+
* @throws RangeError for invalid arguments or totals that differ, and (when n >= 1) for a grand total above 2^24
|
|
135
|
+
* (16777216; R allows up to 2147483647), as the table of log factorials has total + 1 entries.
|
|
129
136
|
*
|
|
130
137
|
* @example
|
|
131
138
|
* ```ts
|
|
@@ -136,14 +143,28 @@ function margins(v, name) {
|
|
|
136
143
|
* @category Random numbers
|
|
137
144
|
*/
|
|
138
145
|
export function r2dtable(n, r, c) {
|
|
139
|
-
|
|
140
|
-
|
|
146
|
+
// as R's as.integer(n): infinite values and values beyond the integer range are NA there, an error (an infinite n
|
|
147
|
+
// ran the loop below forever); the truncation comes first, so -0.5 is 0 and 2147483647.5 is 2147483647 (#93)
|
|
148
|
+
if (!(Number.isFinite(n) && n > -1 && n < 2 ** 31))
|
|
141
149
|
throw new RangeError("invalid argument 'n'");
|
|
150
|
+
const count = Math.trunc(n);
|
|
142
151
|
const rows = margins(r, "r");
|
|
143
152
|
const cols = margins(c, "c");
|
|
144
153
|
const total = rows.reduce((a, b) => a + b, 0);
|
|
145
154
|
if (total !== cols.reduce((a, b) => a + b, 0))
|
|
146
155
|
throw new RangeError("arguments 'r' and 'c' must have the same sums");
|
|
156
|
+
// the table of log factorials has total + 1 entries: a total beyond the integer range (R's integer sum overflows and
|
|
157
|
+
// the allocation fails) would take tens of gigabytes
|
|
158
|
+
if (total > 2147483647)
|
|
159
|
+
throw new RangeError("the sum of 'r' must be at most 2147483647");
|
|
160
|
+
// no table asked for: the margins are checked (as R) but the table of log factorials, total + 1 entries, is not
|
|
161
|
+
// built (16 GB for a total of 2e9) (#219)
|
|
162
|
+
if (count === 0)
|
|
163
|
+
return [];
|
|
164
|
+
// the table of log factorials is limited to 2^24 + 1 entries (128 MB); R builds it for any total up to 2147483647
|
|
165
|
+
// (8 GB for r2dtable(1, c(5e8, 5e8), c(5e8, 5e8))) (#336)
|
|
166
|
+
if (total > MAX_TABLE_TOTAL)
|
|
167
|
+
throw new RangeError("the sum of 'r' must be at most 16777216 (2^24) to generate tables");
|
|
147
168
|
const fact = logFactorialTable(total);
|
|
148
169
|
const out = new Int32Array(rows.length * cols.length);
|
|
149
170
|
const tables = [];
|
package/dist/random/rank.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export declare function rsignrank(nn: number, n: number): number[];
|
|
|
23
23
|
* @param m Number of observations in the first sample.
|
|
24
24
|
* @param n Number of observations in the second sample.
|
|
25
25
|
* @returns An array of `nn` values; `NaN` for invalid parameters.
|
|
26
|
+
* @throws RangeError if `m + n` exceeds 2147483647 or `n` exceeds 2^24 (one uniform is drawn per value of the second
|
|
27
|
+
* sample), before drawing.
|
|
26
28
|
*
|
|
27
29
|
* @example
|
|
28
30
|
* ```ts
|
package/dist/random/rank.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// Uniform consumption follows observed R output.
|
|
4
4
|
import { unifIndex, unifRand } from "./rng.js";
|
|
5
5
|
import { generate } from "./common.js";
|
|
6
|
+
import { roundHalfEven } from "../internal/integer.js";
|
|
6
7
|
/**
|
|
7
8
|
* Random numbers from the distribution of the Wilcoxon signed rank statistic.
|
|
8
9
|
* Equivalent to R's `rsignrank()`.
|
|
@@ -21,13 +22,18 @@ import { generate } from "./common.js";
|
|
|
21
22
|
*/
|
|
22
23
|
export function rsignrank(nn, n) {
|
|
23
24
|
return generate(nn, () => {
|
|
24
|
-
|
|
25
|
+
// an infinite n would never end the loop below (R's (int) cast of it is undefined)
|
|
26
|
+
if (!Number.isFinite(n))
|
|
25
27
|
return NaN;
|
|
26
|
-
|
|
28
|
+
// as R's R_forceint (nearbyint): halves go to the even integer
|
|
29
|
+
const k = roundHalfEven(n);
|
|
27
30
|
if (k < 0)
|
|
28
31
|
return NaN;
|
|
29
32
|
if (k === 0)
|
|
30
33
|
return 0;
|
|
34
|
+
// one uniform per rank: bounded to the domain of dsignrank (about 4 s here; R runs to 2147483647)
|
|
35
|
+
if (k >= 2 ** 27)
|
|
36
|
+
throw new RangeError("n must be less than 2^27 (134217728)");
|
|
31
37
|
let r = 0;
|
|
32
38
|
// Choose the sign of rank i with a uniform
|
|
33
39
|
for (let i = 1; i <= k; i++)
|
|
@@ -35,6 +41,29 @@ export function rsignrank(nn, n) {
|
|
|
35
41
|
return r;
|
|
36
42
|
});
|
|
37
43
|
}
|
|
44
|
+
/** Bound on n of rwilcox, and on the m + n ranks held in an array (beyond, the swapped ones in a Map) */
|
|
45
|
+
const MAX_RWILCOX_N = 2 ** 24;
|
|
46
|
+
/**
|
|
47
|
+
* The sum of n of the ranks 0..k-1 drawn without replacement, as the array version of rwilcox (the same uniforms and
|
|
48
|
+
* the same value) with only the slots that hold another rank than their own kept, in a Map: at most n - 1 of them, as
|
|
49
|
+
* the last draw updates none (an Int32Array of m + n, up to 8 GB, was filled for each value)
|
|
50
|
+
*/
|
|
51
|
+
function sparseRankSum(k, n) {
|
|
52
|
+
const moved = new Map();
|
|
53
|
+
let r = 0;
|
|
54
|
+
for (let i = 0; i < n; i++) {
|
|
55
|
+
const j = unifIndex(k);
|
|
56
|
+
r += moved.get(j) ?? j;
|
|
57
|
+
k--;
|
|
58
|
+
if (i === n - 1)
|
|
59
|
+
break;
|
|
60
|
+
// x[j] = x[k]; the slot k is out of reach from now on
|
|
61
|
+
if (j !== k)
|
|
62
|
+
moved.set(j, moved.get(k) ?? k);
|
|
63
|
+
moved.delete(k);
|
|
64
|
+
}
|
|
65
|
+
return r;
|
|
66
|
+
}
|
|
38
67
|
/**
|
|
39
68
|
* Random numbers from the distribution of the Wilcoxon rank sum statistic.
|
|
40
69
|
* Equivalent to R's `rwilcox()`.
|
|
@@ -43,6 +72,8 @@ export function rsignrank(nn, n) {
|
|
|
43
72
|
* @param m Number of observations in the first sample.
|
|
44
73
|
* @param n Number of observations in the second sample.
|
|
45
74
|
* @returns An array of `nn` values; `NaN` for invalid parameters.
|
|
75
|
+
* @throws RangeError if `m + n` exceeds 2147483647 or `n` exceeds 2^24 (one uniform is drawn per value of the second
|
|
76
|
+
* sample), before drawing.
|
|
46
77
|
*
|
|
47
78
|
* @example
|
|
48
79
|
* ```ts
|
|
@@ -56,14 +87,23 @@ export function rwilcox(nn, m, n) {
|
|
|
56
87
|
return generate(nn, () => {
|
|
57
88
|
if (Number.isNaN(m) || Number.isNaN(n))
|
|
58
89
|
return NaN;
|
|
59
|
-
|
|
60
|
-
const
|
|
90
|
+
// as R's R_forceint (nearbyint): halves go to the even integer
|
|
91
|
+
const mm = roundHalfEven(m);
|
|
92
|
+
const ny = roundHalfEven(n);
|
|
61
93
|
if (mm < 0 || ny < 0)
|
|
62
94
|
return NaN;
|
|
63
95
|
if (mm === 0 || ny === 0)
|
|
64
96
|
return 0;
|
|
97
|
+
// R holds the m + n ranks in an array, whose allocation fails beyond its integer range
|
|
98
|
+
if (!(mm + ny <= 2147483647))
|
|
99
|
+
throw new RangeError("m + n must be at most 2147483647");
|
|
100
|
+
// one uniform per rank drawn (about 3.5 s at 2^24), before drawing
|
|
101
|
+
if (ny > MAX_RWILCOX_N)
|
|
102
|
+
throw new RangeError("n must be at most 2^24 (16777216)");
|
|
65
103
|
// Choose n of 0..m+n-1 without replacement and subtract the minimum n(n-1)/2 from their sum
|
|
66
104
|
let k = mm + ny;
|
|
105
|
+
if (k > MAX_RWILCOX_N)
|
|
106
|
+
return sparseRankSum(k, ny) - (ny * (ny - 1)) / 2;
|
|
67
107
|
const x = new Int32Array(k);
|
|
68
108
|
for (let i = 0; i < k; i++)
|
|
69
109
|
x[i] = i;
|
package/dist/random/sample.d.ts
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
* @category Random numbers
|
|
5
5
|
*/
|
|
6
6
|
export interface SampleOptions {
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Number of items to draw (truncated, as R; below 2^31 with `prob`). Default: the number of candidates (a random
|
|
9
|
+
* permutation).
|
|
10
|
+
*/
|
|
8
11
|
size?: number;
|
|
9
12
|
/** Sample with replacement. Default `false`. */
|
|
10
13
|
replace?: boolean;
|
|
@@ -17,7 +20,8 @@ export interface SampleOptions {
|
|
|
17
20
|
* The result is R's `sample.int(n, ...)` minus 1, so it can be used directly as array
|
|
18
21
|
* indices. It consumes the same random numbers as R (default `sample.kind = "Rejection"`).
|
|
19
22
|
*
|
|
20
|
-
* @param n Number of candidates
|
|
23
|
+
* @param n Number of candidates: a non-negative number up to 4.5e15 (2^52, as R); below 2^31 with `prob`, and for
|
|
24
|
+
* sampling without replacement more than n / 2 of them.
|
|
21
25
|
* @param options See {@link SampleOptions}.
|
|
22
26
|
* @returns The drawn integers, each in `0..n-1`.
|
|
23
27
|
*
|
|
@@ -30,6 +34,12 @@ export interface SampleOptions {
|
|
|
30
34
|
* @category Random numbers
|
|
31
35
|
*/
|
|
32
36
|
export declare function sampleInt(n: number, options?: SampleOptions): number[];
|
|
37
|
+
/**
|
|
38
|
+
* One draw at a time from 0..n-1 with replacement and weights `prob`: the same draws, from the same uniforms, as
|
|
39
|
+
* consecutive elements of `sampleInt(n, { size, replace: true, prob })` (the method depends only on n and prob), with
|
|
40
|
+
* the same checks of n and prob. For simulations that count draws without keeping them (internal; #287)
|
|
41
|
+
*/
|
|
42
|
+
export declare function sampleIntReplaceDrawer(n: number, prob: readonly number[]): () => number;
|
|
33
43
|
/**
|
|
34
44
|
* Random sample of the elements of an array. Equivalent to R's `sample()`.
|
|
35
45
|
*
|