@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/internal/integer.js
CHANGED
|
@@ -7,6 +7,18 @@
|
|
|
7
7
|
export function nonInt(x) {
|
|
8
8
|
return Math.abs(x - roundHalfEven(x)) > 1e-9 * Math.max(1, Math.abs(x));
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* The largest count k with k <= q, for the distribution functions of discrete distributions on the integers. A q within
|
|
12
|
+
* min(1e-7, 1e-9 max(1, |q|)) of an integer is that integer (a computed 2.9999999999999996 counts as 3); any other q
|
|
13
|
+
* gives floor(q). R adds 1e-7 before taking the floor (or, in psignrank, rounding to the nearest integer), which counts
|
|
14
|
+
* 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
|
|
15
|
+
* (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
|
|
16
|
+
* 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.
|
|
17
|
+
*/
|
|
18
|
+
export function countAtMost(q) {
|
|
19
|
+
const r = roundHalfEven(q);
|
|
20
|
+
return Math.abs(q - r) <= Math.min(1e-7, 1e-9 * Math.max(1, Math.abs(q))) ? r : Math.floor(q);
|
|
21
|
+
}
|
|
10
22
|
/** Round half to even (0.5 goes to the nearest even number) */
|
|
11
23
|
export function roundHalfEven(x) {
|
|
12
24
|
if (!Number.isFinite(x))
|
|
@@ -32,5 +32,10 @@ export interface InvertProblem {
|
|
|
32
32
|
*/
|
|
33
33
|
positive?: boolean;
|
|
34
34
|
maxIter?: number;
|
|
35
|
+
/**
|
|
36
|
+
* (Optional) The noise of log F (absolute): a point whose residual is within it is returned as it is (where the
|
|
37
|
+
* distribution function is an integral accurate to about 1e-13, the steps would otherwise wander over a few ulps of x)
|
|
38
|
+
*/
|
|
39
|
+
residualTolerance?: number;
|
|
35
40
|
}
|
|
36
41
|
export declare function invertContinuous(problem: InvertProblem): number;
|
package/dist/internal/invert.js
CHANGED
|
@@ -9,7 +9,7 @@ export function invertContinuous(problem) {
|
|
|
9
9
|
const residual = (x, lF) => {
|
|
10
10
|
if (linearTarget !== undefined && cdf !== undefined && lF > -700) {
|
|
11
11
|
const F = cdf(x, lower);
|
|
12
|
-
if (F > 0)
|
|
12
|
+
if (F > 0 && F / linearTarget < Infinity)
|
|
13
13
|
return Math.log(F / linearTarget);
|
|
14
14
|
}
|
|
15
15
|
return lF - logTarget;
|
|
@@ -21,7 +21,9 @@ export function invertContinuous(problem) {
|
|
|
21
21
|
return [lF, residual(x, lF)];
|
|
22
22
|
}
|
|
23
23
|
const [lF, F] = cdfBoth(x, lower);
|
|
24
|
-
|
|
24
|
+
// (F / target overflows for a subnormal target; the residual was then Infinity and the steps went astray:
|
|
25
|
+
// qgamma(7.2e-317, 3.3e126, { scale: 0.136, lowerTail: false }) was 8.2e146, 1e21 times the mean)
|
|
26
|
+
if (linearTarget !== undefined && lF > -700 && F > 0 && F / linearTarget < Infinity)
|
|
25
27
|
return [lF, Math.log(F / linearTarget)];
|
|
26
28
|
return [lF, lF - logTarget];
|
|
27
29
|
};
|
|
@@ -43,7 +45,7 @@ export function invertContinuous(problem) {
|
|
|
43
45
|
bestResid = Math.abs(resid);
|
|
44
46
|
best = u;
|
|
45
47
|
}
|
|
46
|
-
if (resid === 0)
|
|
48
|
+
if (resid === 0 || Math.abs(resid) <= (problem.residualTolerance ?? 0))
|
|
47
49
|
return x;
|
|
48
50
|
// The lower probability is increasing in x, the upper decreasing
|
|
49
51
|
if (resid < 0 === lower)
|
|
@@ -94,8 +96,11 @@ export function invertContinuous(problem) {
|
|
|
94
96
|
let x = x0;
|
|
95
97
|
let [lF, r] = evaluate(x);
|
|
96
98
|
for (let k = 0; k < 4 && r !== 0; k++) {
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
+
const lf = pdfLog(x) - lF;
|
|
100
|
+
const slope = (lower ? 1 : -1) * Math.exp(lf);
|
|
101
|
+
// (where f/F overflows, as at a subnormal x, the step relative to x: qbeta(3.8e-277, 27.9, 1.5e299) is 9.6e-309,
|
|
102
|
+
// and the step there was 0, which left the answer 5e-14 off)
|
|
103
|
+
const xn = Number.isFinite(slope) ? x - r / slope : x - (lower ? 1 : -1) * r * x * Math.exp(-(lf + Math.log(x)));
|
|
99
104
|
if (!(xn > 0) || !Number.isFinite(xn) || xn === x)
|
|
100
105
|
break;
|
|
101
106
|
const [lFn, rn] = evaluate(xn);
|
|
@@ -140,8 +145,11 @@ export function invertContinuous(problem) {
|
|
|
140
145
|
}
|
|
141
146
|
if (!(xl > 0 && xh > xl && Number.isFinite(xh)))
|
|
142
147
|
return x0;
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
// halved on the bit patterns of the doubles (positive x: ordered as integers), so that any bracket shrinks to
|
|
149
|
+
// adjacent doubles within 64 steps; halving the values took 64 steps per factor 2^64 of width, and a bracket 1e55
|
|
150
|
+
// times wider than the answer ended far from it (qgamma(0.75, 1e145) was 151 ulps below the right 1e145)
|
|
151
|
+
for (let k = 0; k < 70; k++) {
|
|
152
|
+
const mid = bitMidpoint(xl, xh);
|
|
145
153
|
if (mid <= xl || mid >= xh)
|
|
146
154
|
break;
|
|
147
155
|
const r = evaluate(mid)[1];
|
|
@@ -157,6 +165,16 @@ export function invertContinuous(problem) {
|
|
|
157
165
|
return Math.abs(evaluate(xl)[1]) < Math.abs(evaluate(xh)[1]) ? xl : xh;
|
|
158
166
|
}
|
|
159
167
|
}
|
|
168
|
+
const bitView = new DataView(new ArrayBuffer(8));
|
|
169
|
+
/** The double halfway between positive finite a < b in the order of the doubles (the mean of their bit patterns) */
|
|
170
|
+
function bitMidpoint(a, b) {
|
|
171
|
+
bitView.setFloat64(0, a);
|
|
172
|
+
const ia = bitView.getBigUint64(0);
|
|
173
|
+
bitView.setFloat64(0, b);
|
|
174
|
+
const ib = bitView.getBigUint64(0);
|
|
175
|
+
bitView.setBigUint64(0, (ia + ib) / 2n);
|
|
176
|
+
return bitView.getFloat64(0);
|
|
177
|
+
}
|
|
160
178
|
/** Midpoint of the bracketing interval. If an end is infinite, expand outward from the current value */
|
|
161
179
|
function midpoint(lo, hi, x) {
|
|
162
180
|
if (Number.isFinite(lo) && Number.isFinite(hi))
|
|
@@ -46,3 +46,10 @@ export declare function quadratureControl(tol?: number, maxBisections?: number):
|
|
|
46
46
|
* would otherwise be bisected to the full depth (2^40 intervals)
|
|
47
47
|
*/
|
|
48
48
|
export declare function kronrodAdaptive(f: (u: number) => number, a: number, b: number, total: number, depth: number, control: QuadratureControl): number;
|
|
49
|
+
/**
|
|
50
|
+
* Whether a noncentral density equals the central one to the double: it is e^-μ Σ_j μ^j/j! f_j, and f_1/f_0 is r
|
|
51
|
+
* (x (a + b)/a for the beta, x/k for χ² with k df), with the ratios of later terms smaller still, so the terms from
|
|
52
|
+
* j = 1 on and the factor e^-μ change it by less than 2 μ max(1, r) relative. Below 1e-20 that is the central density
|
|
53
|
+
* itself (at a tiny or subnormal λ the mixture's weights were NaN: dbeta(1.05e-231, 0.19, 141, { ncp: 1.4e-303 }))
|
|
54
|
+
*/
|
|
55
|
+
export declare function negligibleNcp(mu: number, r: number): boolean;
|
|
@@ -113,3 +113,12 @@ export function kronrodAdaptive(f, a, b, total, depth, control) {
|
|
|
113
113
|
return k;
|
|
114
114
|
return kronrodAdaptive(f, a, m, total, depth + 1, control) + kronrodAdaptive(f, m, b, total, depth + 1, control);
|
|
115
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Whether a noncentral density equals the central one to the double: it is e^-μ Σ_j μ^j/j! f_j, and f_1/f_0 is r
|
|
118
|
+
* (x (a + b)/a for the beta, x/k for χ² with k df), with the ratios of later terms smaller still, so the terms from
|
|
119
|
+
* j = 1 on and the factor e^-μ change it by less than 2 μ max(1, r) relative. Below 1e-20 that is the central density
|
|
120
|
+
* itself (at a tiny or subnormal λ the mixture's weights were NaN: dbeta(1.05e-231, 0.19, 141, { ncp: 1.4e-303 }))
|
|
121
|
+
*/
|
|
122
|
+
export function negligibleNcp(mu, r) {
|
|
123
|
+
return mu * Math.max(1, r) < 1e-20;
|
|
124
|
+
}
|
|
@@ -84,7 +84,12 @@ export function dpoisLogDD(x, lambda) {
|
|
|
84
84
|
if (x === 0)
|
|
85
85
|
return [-lambda, 0];
|
|
86
86
|
const DBL_MIN = 2.2250738585072014e-308;
|
|
87
|
-
|
|
87
|
+
// (λ < x DBL_MIN not for x in [1, 1e10], where lgamma1p(x) in double would put an absolute error of eps log Γ(x + 1)
|
|
88
|
+
// into a log that is not much larger: pchisq(1e-310, 1.2e-51, { ncp: 197 }) was 3.8e-15 off, and 1.4e-15 at x = 1e-307;
|
|
89
|
+
// bd0dd takes the logs of x and λ apart for such ratios. Below 1 log Γ(x + 1) is small, while stirlerr(x) and log x
|
|
90
|
+
// cancel in Loader's form; above 1e10 the error is below eps of x log λ)
|
|
91
|
+
const moderate = x >= 1 && x <= 1e10;
|
|
92
|
+
if (x <= lambda * DBL_MIN || (lambda < x * DBL_MIN && !moderate) || !Number.isFinite(x)) {
|
|
88
93
|
const l = ddAdd(ddMul(ddLog([lambda, 0]), [x, 0]), [-lambda - lgamma1p(x), 0]);
|
|
89
94
|
return l;
|
|
90
95
|
}
|
|
@@ -298,6 +303,21 @@ function logRatio(a, b) {
|
|
|
298
303
|
return Infinity;
|
|
299
304
|
return Math.log(a.h / b.h) + (a.l / a.h - b.l / b.h) + (a.k - b.k) * LN_UNIT;
|
|
300
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* w_j = w_(j-1) μ / j. Where μ is beyond 1e290 the splitting of the quotient in ddDivNum overflows (the noncentral χ²
|
|
308
|
+
* at λ = 1e302 and x = 1e-300 was NaN), so μ is multiplied in as a scaled number instead. (R 4.5.2 gives
|
|
309
|
+
* -1.0243631212370614e307 for dchisq(1e-308, 294.4, ncp = 2.9e304, log = TRUE), which is -λ/2 = -1.45e304 to the double)
|
|
310
|
+
*/
|
|
311
|
+
function weightUp(w, mu, j) {
|
|
312
|
+
return mu < 1e290 ? mulDD(w, ddDivNum([mu, 0], j)) : divDD(mulDD(w, [mu, 0]), [j, 0]);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* w_(j-1) = w_j j / μ, with μ split off as a scaled number where the splitting in ddDivNum would overflow: for a tiny
|
|
316
|
+
* μ (of j / μ; the densities at λ = 1.4e-303 were NaN) and for a huge one (of μ)
|
|
317
|
+
*/
|
|
318
|
+
function weightDown(w, j, mu) {
|
|
319
|
+
return mu > 1e-290 && mu < 1e290 ? mulDD(w, ddDivNum([j, 0], mu)) : divDD(mulDD(w, [j, 0]), [mu, 0]);
|
|
320
|
+
}
|
|
301
321
|
function weight(j, mu) {
|
|
302
322
|
return fromValue(dpoisRaw(j, mu, false), () => dpoisLogDD(j, mu));
|
|
303
323
|
}
|
|
@@ -389,7 +409,7 @@ function sumLower(mu, k) {
|
|
|
389
409
|
break;
|
|
390
410
|
// P_{j-1} = P_j + d_{j-1}, w_{j-1} = w_j j/μ, d_{j-2} = d_{j-1} / (d_{j-1}/d_{j-2})
|
|
391
411
|
p = add(p, d);
|
|
392
|
-
w = refresh(
|
|
412
|
+
w = refresh(weightDown(w, j, mu), () => weight(j - 1, mu));
|
|
393
413
|
if (j >= 2)
|
|
394
414
|
d = d.h === 0 ? increment(k, j - 2) : refresh(divRatio(d, k, j - 1), () => increment(k, j - 2));
|
|
395
415
|
}
|
|
@@ -430,7 +450,7 @@ function sumUpper(mu, k) {
|
|
|
430
450
|
prev = t;
|
|
431
451
|
// Q_{j+1} = Q_j + d_j, w_{j+1} = w_j μ/(j+1), d_{j+1} = d_j (d_{j+1}/d_j)
|
|
432
452
|
q = add(q, d);
|
|
433
|
-
w = refresh(
|
|
453
|
+
w = refresh(weightUp(w, mu, j + 1), () => weight(j + 1, mu));
|
|
434
454
|
d = d.h === 0 ? increment(k, j + 1) : refresh(mulRatio(d, k, j + 1), () => increment(k, j + 1));
|
|
435
455
|
}
|
|
436
456
|
if (restart < 0)
|
|
@@ -445,7 +465,10 @@ function sumUpper(mu, k) {
|
|
|
445
465
|
* Returns the sum divided by divisor (as a log if log).
|
|
446
466
|
*/
|
|
447
467
|
export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
|
|
448
|
-
|
|
468
|
+
// (μ = 0 where λ/2 underflows, at λ = 5e-324: the j = 0 term alone, as for the central distribution, whose weight is
|
|
469
|
+
// the only one not 0; the terms from j = 1 on are of relative size μ times the ratio of the densities of components 1
|
|
470
|
+
// and 0, negligible unless that ratio is above 1e300)
|
|
471
|
+
const j0 = Number.isFinite(jStar) && mu > 0 ? Math.max(0, Math.round(jStar)) : 0;
|
|
449
472
|
const sum = new ScaledSum();
|
|
450
473
|
const w0 = weight(j0, mu);
|
|
451
474
|
const d0 = increment(kernel, j0);
|
|
@@ -461,7 +484,7 @@ export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
|
|
|
461
484
|
if (t.h === 0 && j > mu + 50 && j > j0 + 50)
|
|
462
485
|
break; // all later terms are 0 too
|
|
463
486
|
prev = t;
|
|
464
|
-
w = refresh(
|
|
487
|
+
w = refresh(weightUp(w, mu, j + 1), () => weight(j + 1, mu));
|
|
465
488
|
d = d.h === 0 ? increment(kernel, j + 1) : refresh(mulRatio(d, kernel, j + 1), () => increment(kernel, j + 1));
|
|
466
489
|
}
|
|
467
490
|
// Downward
|
|
@@ -469,7 +492,7 @@ export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
|
|
|
469
492
|
d = d0;
|
|
470
493
|
prev = ZERO;
|
|
471
494
|
for (let j = j0 - 1; j >= 0; j--) {
|
|
472
|
-
w = refresh(
|
|
495
|
+
w = refresh(weightDown(w, j + 1, mu), () => weight(j, mu));
|
|
473
496
|
d = d.h === 0 ? increment(kernel, j) : refresh(divRatio(d, kernel, j + 1), () => increment(kernel, j));
|
|
474
497
|
const t = mulDD(mul(w, d), [coef(j), 0]);
|
|
475
498
|
sum.add(t);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (R'R)^-1 of an upper triangular R as inv_jk 2^(-e_j - e_k): the columns of R divided by 2^e_j first (exact). The
|
|
3
|
+
* inverse of R itself overflowed for columns of 1e-200 (and underflowed for 1e200), so standard errors were Infinity
|
|
4
|
+
* or 0 though they scale with the column (R's too). e_j is the exponent of the largest |R_ij| of a column outside
|
|
5
|
+
* [2^-400, 2^400], else 0, which leaves the computation as before
|
|
6
|
+
*/
|
|
7
|
+
export declare function chol2invColumns(R: readonly (readonly number[])[]): {
|
|
8
|
+
inv: number[][];
|
|
9
|
+
e: number[];
|
|
10
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// (R'R)^-1 of a triangular factor whose columns may be far from 1 (review 64).
|
|
2
|
+
import { chol2inv } from "../linalg/chol.js";
|
|
3
|
+
import { ldexp } from "./summary.js";
|
|
4
|
+
/**
|
|
5
|
+
* (R'R)^-1 of an upper triangular R as inv_jk 2^(-e_j - e_k): the columns of R divided by 2^e_j first (exact). The
|
|
6
|
+
* inverse of R itself overflowed for columns of 1e-200 (and underflowed for 1e200), so standard errors were Infinity
|
|
7
|
+
* or 0 though they scale with the column (R's too). e_j is the exponent of the largest |R_ij| of a column outside
|
|
8
|
+
* [2^-400, 2^400], else 0, which leaves the computation as before
|
|
9
|
+
*/
|
|
10
|
+
export function chol2invColumns(R) {
|
|
11
|
+
const p = R.length;
|
|
12
|
+
const e = new Array(p).fill(0);
|
|
13
|
+
for (let j = 0; j < p; j++) {
|
|
14
|
+
let top = 0;
|
|
15
|
+
for (let i = 0; i <= j; i++)
|
|
16
|
+
top = Math.max(top, Math.abs(R[i][j]));
|
|
17
|
+
if (top > 0 && Number.isFinite(top) && (top < 2 ** -400 || top > 2 ** 400))
|
|
18
|
+
e[j] = Math.floor(Math.log2(top));
|
|
19
|
+
}
|
|
20
|
+
const Rs = e.every((v) => v === 0) ? R.map((r) => r.slice()) : R.map((r) => r.map((v, j) => ldexp(v, -e[j])));
|
|
21
|
+
return { inv: chol2inv(Rs), e };
|
|
22
|
+
}
|
package/dist/internal/seq.js
CHANGED
|
@@ -4,10 +4,23 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export function seqLength(from, to, n) {
|
|
6
6
|
const len = Math.ceil(n);
|
|
7
|
+
// (a length that is not finite or rounds up to a negative number was an empty array; issue #98)
|
|
8
|
+
if (!Number.isFinite(n) || len < 0)
|
|
9
|
+
throw new RangeError("'length.out' must be a non-negative number");
|
|
7
10
|
if (len === 1)
|
|
8
11
|
return [from];
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
let out;
|
|
13
|
+
if (Number.isFinite(to - from) || !Number.isFinite(from) || !Number.isFinite(to)) {
|
|
14
|
+
const by = (to - from) / (len - 1);
|
|
15
|
+
out = Array.from({ length: len }, (_, i) => from + i * by);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
// finite ends whose difference overflows: in half coordinates, doubled (seqLength(-1e308, 1e308, 3) was NaN,
|
|
19
|
+
// Infinity, 1e308; R's seq gives -1e308, 0, 1e308)
|
|
20
|
+
const h = from / 2;
|
|
21
|
+
const by = (to / 2 - h) / (len - 1);
|
|
22
|
+
out = Array.from({ length: len }, (_, i) => 2 * (h + i * by));
|
|
23
|
+
}
|
|
11
24
|
if (len > 0)
|
|
12
25
|
out[len - 1] = to;
|
|
13
26
|
return out;
|
|
@@ -14,13 +14,77 @@ export declare function wideSumAdd(acc: WideSum, v: number, n: number): void;
|
|
|
14
14
|
export declare function wideSumValue(acc: WideSum): number;
|
|
15
15
|
/** v 2^k for an integer k, in two steps so that 2^k itself does not overflow or underflow */
|
|
16
16
|
export declare function ldexp(v: number, k: number): number;
|
|
17
|
+
/** v 2^k for any integer k (beyond the +-2046 of ldexp), in steps of at most 2^1000 */
|
|
18
|
+
export declare function ldexpWide(v: number, k: number): number;
|
|
17
19
|
/** Mean as double-double. x contains no NaN and has length >= 1 */
|
|
18
20
|
export declare function meanDD(x: readonly number[]): DD;
|
|
19
21
|
export declare function mean(x: readonly number[]): number;
|
|
20
22
|
/** Unbiased variance. NaN for length below 2 (R's var gives NA) */
|
|
21
23
|
export declare function variance(x: readonly number[]): number;
|
|
24
|
+
/**
|
|
25
|
+
* Sample standard deviation from the scaled deviations: sqrt(sum d^2 / (n - 1)) in the coordinates 2^-k, times 2^k.
|
|
26
|
+
* 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)),
|
|
27
|
+
* and the right value where it overflows or underflows (sd of c(-1e308, -1e308, -1e308, -1e308, 1.5e308) is 1.118e308
|
|
28
|
+
* while its variance is 1.25e616). x contains no NaN
|
|
29
|
+
*/
|
|
30
|
+
export declare function standardDeviation(x: readonly number[]): number;
|
|
22
31
|
/** Unbiased covariance. x and y have the same length and contain no NaN */
|
|
23
32
|
export declare function covariance(x: readonly number[], y: readonly number[]): number;
|
|
33
|
+
/**
|
|
34
|
+
* The deviations from the mean of x scaled by 2^-k (the scaling first, see the rules above), in double-double, with k
|
|
35
|
+
* and the scaled mean. undefined when x has an infinite value (the mean is not finite)
|
|
36
|
+
*/
|
|
37
|
+
export declare function centered(x: readonly number[]): {
|
|
38
|
+
d: DD[];
|
|
39
|
+
k: number;
|
|
40
|
+
mean: DD;
|
|
41
|
+
} | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* (x - mean) / sd, from the scaled deviations (no overflow or underflow at any scale). NaN values when the standard
|
|
44
|
+
* deviation is 0 or x has fewer than 2 values or an infinite one
|
|
45
|
+
*/
|
|
46
|
+
export declare function standardize(x: readonly number[]): number[];
|
|
47
|
+
/**
|
|
48
|
+
* Moments of groups on one common scale 2^-k (see the rules above): per group the size, mean, within sum of squares
|
|
49
|
+
* and variance (NaN below 2 values), the grand mean, the between and pooled within sums of squares (accumulated in
|
|
50
|
+
* double-double), and the scaled values. Means are scaled back by 2^k, sums of squares and variances by 2^2k
|
|
51
|
+
*/
|
|
52
|
+
export interface GroupMoments {
|
|
53
|
+
k: number;
|
|
54
|
+
groups: number[][];
|
|
55
|
+
n: number[];
|
|
56
|
+
mean: number[];
|
|
57
|
+
ss: number[];
|
|
58
|
+
variance: number[];
|
|
59
|
+
grandMean: number;
|
|
60
|
+
ssBetween: number;
|
|
61
|
+
ssWithin: number;
|
|
62
|
+
}
|
|
63
|
+
export declare function groupMoments(groups: readonly (readonly number[])[]): GroupMoments;
|
|
64
|
+
/** x - y for paired values, both scaled by one 2^-k first (so the differences cannot overflow), with k */
|
|
65
|
+
export declare function pairedDifferences(x: readonly number[], y: readonly number[]): {
|
|
66
|
+
d: number[];
|
|
67
|
+
k: number;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Satterthwaite's degrees of freedom (sum w)^2 / sum(w^2 / df) of a sum of independent variance estimates w with
|
|
71
|
+
* degrees of freedom df. The w are scaled by a power of 2 first (no rounding), so the squares neither overflow nor
|
|
72
|
+
* 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
|
|
73
|
+
* order of operations); tTest, cohensD and gamesHowellTest use R's forms below and this only where those are not finite
|
|
74
|
+
*/
|
|
75
|
+
export declare function satterthwaiteDf(w: readonly number[], df: readonly number[]): number;
|
|
76
|
+
/**
|
|
77
|
+
* 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)
|
|
78
|
+
* 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
|
|
79
|
+
* wherever R's terms are in range, and no overflow). {@link satterthwaiteDf} where that is not finite and positive
|
|
80
|
+
* (revision of review 2)
|
|
81
|
+
*/
|
|
82
|
+
export declare function satterthwaiteDfSe(w: readonly number[], df: readonly number[]): number;
|
|
83
|
+
/**
|
|
84
|
+
* 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
|
|
85
|
+
* A = sum s2_i / n_i, for the group variances s2 and sizes n, scaled as {@link satterthwaiteDfSe} (revision of review 2)
|
|
86
|
+
*/
|
|
87
|
+
export declare function satterthwaiteDfPM(s2: readonly number[], n: readonly number[]): number;
|
|
24
88
|
/**
|
|
25
89
|
* Sum (accumulated in double-double without overflow, then rounded; ±Infinity beyond the double range). With
|
|
26
90
|
* infinite or NaN values, as in double arithmetic
|
|
@@ -37,3 +101,29 @@ export declare function tieCounts(x: readonly number[]): number[];
|
|
|
37
101
|
export declare function dropNaN(x: readonly number[]): number[];
|
|
38
102
|
/** Keep only the pairs where neither x nor y is NaN (R's complete.cases) */
|
|
39
103
|
export declare function completePairs(x: readonly number[], y: readonly number[]): [number[], number[]];
|
|
104
|
+
/**
|
|
105
|
+
* d^2 / e, as for a Pearson chi-squared term. When d^2 overflows (or loses precision below the normal range) although
|
|
106
|
+
* the quotient is representable, it is formed as (d / sqrt(e))^2 instead
|
|
107
|
+
*/
|
|
108
|
+
export declare function squareOver(d: number, e: number): number;
|
|
109
|
+
/**
|
|
110
|
+
* a b / n, as for an expected count from two margins. When the product overflows, underflows or is subnormal (with a
|
|
111
|
+
* and b not 0), the three are scaled to [1, 2) by powers of 2 and the exponents added back at the end
|
|
112
|
+
*/
|
|
113
|
+
export declare function productOver(a: number, b: number, n: number): number;
|
|
114
|
+
/**
|
|
115
|
+
* 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
|
|
116
|
+
* none). The scaling adds no rounding error, so statistics that do not depend on the units (t, F, chi-squared,
|
|
117
|
+
* ratios of variances, degrees of freedom) come out as from the original data, but their squares and variances
|
|
118
|
+
* can no longer overflow or underflow. Quantities with units are multiplied back by 2^k (see ldexp)
|
|
119
|
+
*/
|
|
120
|
+
export declare function scaleToUnit(groups: readonly (readonly number[])[]): {
|
|
121
|
+
groups: number[][];
|
|
122
|
+
k: number;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Euclidean norm sqrt(sum x_i^2): the plain sum where it is in range (so the value is unchanged there), otherwise from
|
|
126
|
+
* the values divided by the largest |x_i| (as BLAS's dnrm2), so that squares of values around 1e200 or 1e-200 neither
|
|
127
|
+
* overflow nor underflow
|
|
128
|
+
*/
|
|
129
|
+
export declare function norm2(x: readonly number[]): number;
|