@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
|
@@ -31,9 +31,16 @@ const TOL = Number.EPSILON ** 0.25;
|
|
|
31
31
|
* @category Numerical methods
|
|
32
32
|
*/
|
|
33
33
|
export function integrate(f, lower, upper, options = {}) {
|
|
34
|
-
const
|
|
34
|
+
const subdivisions = options.subdivisions ?? 100;
|
|
35
|
+
const limit = Math.trunc(subdivisions);
|
|
35
36
|
const relTol = options.relTol ?? TOL;
|
|
36
37
|
const absTol = options.absTol ?? relTol;
|
|
38
|
+
// infinite or negative tolerances are errors (R accepts an infinite one and returns the first, coarse estimate as converged);
|
|
39
|
+
// subdivisions as R's as.integer: beyond the integer range it is NA and an error in R (2^32 reached the typed-array
|
|
40
|
+
// allocation, #369)
|
|
41
|
+
if (!(Math.abs(subdivisions) <= 2147483647) || !Number.isFinite(relTol) || !Number.isFinite(absTol) || relTol < 0 || absTol < 0) {
|
|
42
|
+
throw new RangeError("invalid parameter values");
|
|
43
|
+
}
|
|
37
44
|
const stopOnError = options.stopOnError ?? true;
|
|
38
45
|
if (Number.isNaN(lower) || Number.isNaN(upper))
|
|
39
46
|
throw new RangeError("a limit is NA or NaN");
|
|
@@ -45,19 +52,42 @@ export function integrate(f, lower, upper, options = {}) {
|
|
|
45
52
|
throw new RangeError("non-finite function value");
|
|
46
53
|
return v;
|
|
47
54
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
// An infinite limit is taken with its sign: from a to -Inf is minus the integral over (-Inf, a), from Inf to b minus
|
|
56
|
+
// that over (b, Inf), from Inf to -Inf minus the whole line (R integrates over (a, Inf) and (-Inf, Inf) there, so
|
|
57
|
+
// integrate(f, 0, -Inf) had the sign and the side wrong), and between equal infinite limits 0
|
|
58
|
+
if (lower === upper && !Number.isFinite(lower))
|
|
59
|
+
return { value: 0, absError: 0, subdivisions: 0, ier: 0, message: MESSAGES[0] };
|
|
60
|
+
let sign = 1;
|
|
61
|
+
if (lower === Infinity || upper === -Infinity) {
|
|
62
|
+
[lower, upper] = [upper, lower];
|
|
63
|
+
sign = -1;
|
|
64
|
+
}
|
|
65
|
+
let r;
|
|
66
|
+
if (Number.isFinite(lower) && Number.isFinite(upper)) {
|
|
67
|
+
// b - a or a midpoint of the subintervals (up to 2 max(|a|, |b|)) overflows (both finite): x = 2u, the integral of
|
|
68
|
+
// f(2u) over (a/2, b/2) doubled, with the same nodes (it ended in "roundoff error" for a density spread over
|
|
69
|
+
// (-1e308, 1e308), and 1/x over (1e308, 1.1e308) was 0 with every node at Inf)
|
|
70
|
+
if (Number.isFinite(2 * Math.max(Math.abs(lower), Math.abs(upper))))
|
|
71
|
+
r = dqagse(g, lower, upper, absTol, relTol, limit);
|
|
72
|
+
else {
|
|
73
|
+
// (a positive absTol kept positive: MIN halved to 0 made the input invalid; issue #122)
|
|
74
|
+
const halfTol = absTol > 0 ? Math.max(absTol * 0.5, Number.MIN_VALUE) : absTol * 0.5;
|
|
75
|
+
const h = dqagse((u) => g(2 * u), lower * 0.5, upper * 0.5, halfTol, relTol, limit);
|
|
76
|
+
r = { ...h, result: 2 * h.result, abserr: 2 * h.abserr };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (Number.isFinite(lower))
|
|
80
|
+
r = dqagie(g, lower, 1, absTol, relTol, limit);
|
|
81
|
+
else if (Number.isFinite(upper))
|
|
82
|
+
r = dqagie(g, upper, -1, absTol, relTol, limit);
|
|
83
|
+
else
|
|
84
|
+
r = dqagie(g, 0, 2, absTol, relTol, limit);
|
|
55
85
|
const message = MESSAGES[r.ier] ?? "unknown error";
|
|
56
86
|
if (r.ier === 6)
|
|
57
87
|
throw new RangeError("the input is invalid");
|
|
58
88
|
if (r.ier !== 0 && stopOnError)
|
|
59
89
|
throw new RangeError(message);
|
|
60
|
-
return { value: r.result, absError: r.abserr, subdivisions: r.last, ier: r.ier, message };
|
|
90
|
+
return { value: sign * r.result, absError: r.abserr, subdivisions: r.last, ier: r.ier, message };
|
|
61
91
|
}
|
|
62
92
|
/**
|
|
63
93
|
* Minimum (or maximum) of a function of one variable on an interval, by Brent's golden-section and parabolic
|
|
@@ -74,6 +104,9 @@ export function integrate(f, lower, upper, options = {}) {
|
|
|
74
104
|
export function optimize(f, lower, upper, options = {}) {
|
|
75
105
|
const maximum = options.maximum ?? false;
|
|
76
106
|
const tol = options.tol ?? TOL;
|
|
107
|
+
// (a positive tol, as in R: with tol = 0 the search stopped narrowing among the subnormals, see localMin)
|
|
108
|
+
if (!(tol > 0 && tol < Infinity))
|
|
109
|
+
throw new RangeError("'tol' must be a positive finite number");
|
|
77
110
|
// R checks the ends before calling f, lower first
|
|
78
111
|
if (!Number.isFinite(lower))
|
|
79
112
|
throw new RangeError("invalid 'xmin' value");
|
|
@@ -81,8 +114,36 @@ export function optimize(f, lower, upper, options = {}) {
|
|
|
81
114
|
throw new RangeError("invalid 'xmax' value");
|
|
82
115
|
if (!(lower < upper))
|
|
83
116
|
throw new RangeError("'xmin' not less than 'xmax'");
|
|
84
|
-
|
|
85
|
-
|
|
117
|
+
// as R, the search sees NaN and +Inf as the largest double and -Inf as the most negative (R warns, "NA/NaN replaced
|
|
118
|
+
// by maximum positive value"; the library does not report warnings); objective is f itself
|
|
119
|
+
const fn = (x) => {
|
|
120
|
+
const v = maximum ? -f(x) : f(x);
|
|
121
|
+
return Number.isNaN(v) || v === Infinity ? Number.MAX_VALUE : v === -Infinity ? -Number.MAX_VALUE : v;
|
|
122
|
+
};
|
|
123
|
+
// upper - lower or upper + lower overflows (both finite): search over u = x/2 on (lower/2, upper/2) with tol/2 (the
|
|
124
|
+
// search did not return: optimize((x / 1e308)^2, -1e308, 1e308), and with the midpoint 0.5 (a + b) = Infinity,
|
|
125
|
+
// optimize((x / 1e308 - 1.05)^2, 1e308, 1.1e308); R too); also where an end is beyond Number.MAX_VALUE / 2, as the
|
|
126
|
+
// midpoint of a bracket narrowed near it overflows (optimize((x / 1e308 + 1.6)^2, -1.7e308, 1e306), #212)
|
|
127
|
+
// (2 lower and 2 upper finite imply that upper - lower and upper + lower are)
|
|
128
|
+
const x = Number.isFinite(2 * lower) && Number.isFinite(2 * upper)
|
|
129
|
+
? localMin(fn, lower, upper, tol).x
|
|
130
|
+
: 2 * localMin((u) => fn(2 * u), lower * 0.5, upper * 0.5, tol * 0.5).x;
|
|
131
|
+
return { optimum: x, objective: f(x) };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* extendInt of uniroot, as R's match.arg(extendInt) against c("no", "yes", "downX", "upX"): the full name or a
|
|
135
|
+
* unique prefix; anything else is an error before f is evaluated (it was taken as "no", #247)
|
|
136
|
+
*/
|
|
137
|
+
function extendIntArg(e) {
|
|
138
|
+
if (e === undefined)
|
|
139
|
+
return "no";
|
|
140
|
+
const choices = ["no", "yes", "downX", "upX"];
|
|
141
|
+
if (typeof e === "string" && e.length > 0) {
|
|
142
|
+
const hit = choices.filter((v) => v.startsWith(e));
|
|
143
|
+
if (hit.length === 1)
|
|
144
|
+
return hit[0];
|
|
145
|
+
}
|
|
146
|
+
throw new RangeError(`'extendInt' should be one of "no", "yes", "downX", "upX"`);
|
|
86
147
|
}
|
|
87
148
|
/**
|
|
88
149
|
* A root of a function of one variable in an interval, by Brent's zeroin. Equivalent to R's `uniroot()`.
|
|
@@ -95,9 +156,27 @@ export function optimize(f, lower, upper, options = {}) {
|
|
|
95
156
|
* @category Numerical methods
|
|
96
157
|
*/
|
|
97
158
|
export function uniroot(f, lower, upper, options = {}) {
|
|
159
|
+
// R's "invalid 'tol' value" for tol <= 0 (tol = 0 was accepted, Brent's method then ran to maxiter or the doubles)
|
|
160
|
+
const tol = options.tol ?? TOL;
|
|
161
|
+
if (!(tol > 0 && tol < Infinity))
|
|
162
|
+
throw new RangeError("'tol' must be a positive finite number");
|
|
163
|
+
return unirootCore(f, lower, upper, options);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* {@link uniroot} without the check of tol > 0: the internal callers that run Brent's method until the bracket is as
|
|
167
|
+
* narrow as the doubles allow (tol = 0, the noncentrality roots of src/htest/effectSize.ts) or whose tolerance is
|
|
168
|
+
* scaled by a standard error (src/internal/uniroot.ts)
|
|
169
|
+
*
|
|
170
|
+
* @internal
|
|
171
|
+
*/
|
|
172
|
+
export function unirootCore(f, lower, upper, options = {}) {
|
|
98
173
|
const tol = options.tol ?? TOL;
|
|
99
|
-
const maxiter = options.maxiter ?? 1000;
|
|
100
|
-
|
|
174
|
+
const maxiter = Math.trunc(options.maxiter ?? 1000);
|
|
175
|
+
if (!(tol >= 0 && tol < Infinity))
|
|
176
|
+
throw new RangeError("'tol' must be a non-negative finite number");
|
|
177
|
+
if (!(maxiter >= 1 && maxiter < Infinity))
|
|
178
|
+
throw new RangeError("'maxiter' must be a positive integer");
|
|
179
|
+
const extendInt = extendIntArg(options.extendInt);
|
|
101
180
|
if (!(lower < upper))
|
|
102
181
|
throw new RangeError("lower < upper is not fulfilled");
|
|
103
182
|
let fLo = options.fLower ?? f(lower);
|
|
@@ -110,7 +189,9 @@ export function uniroot(f, lower, upper, options = {}) {
|
|
|
110
189
|
const bound = (v) => Math.max(-Number.MAX_VALUE, Math.min(Number.MAX_VALUE, v));
|
|
111
190
|
const step0 = (u) => 0.01 * Math.max(1e-4, Math.abs(u));
|
|
112
191
|
const sig = extendInt === "downX" ? -1 : extendInt === "upX" ? 1 : 0;
|
|
113
|
-
|
|
192
|
+
// same sign from the signs, not the product (1e-300 * 2e-300 underflows to 0, and the interval was not extended)
|
|
193
|
+
const sameSign = (a, b) => Math.sign(a) * Math.sign(b) > 0;
|
|
194
|
+
const extend = extendInt === "yes" ? sameSign(fLo, fUp) : sig !== 0 && (sig * bound(fLo) > 0 || sig * bound(fUp) < 0);
|
|
114
195
|
let extIter = 0;
|
|
115
196
|
const count = () => {
|
|
116
197
|
if (++extIter > maxiter)
|
|
@@ -119,7 +200,7 @@ export function uniroot(f, lower, upper, options = {}) {
|
|
|
119
200
|
if (extend && extendInt === "yes") {
|
|
120
201
|
let dLo = step0(lower);
|
|
121
202
|
let dUp = step0(upper);
|
|
122
|
-
while (fLo
|
|
203
|
+
while (sameSign(fLo, fUp) && (Number.isFinite(lower) || Number.isFinite(upper))) {
|
|
123
204
|
count();
|
|
124
205
|
if (Number.isFinite(lower)) {
|
|
125
206
|
const x = lower - dLo;
|
|
@@ -165,7 +246,13 @@ export function uniroot(f, lower, upper, options = {}) {
|
|
|
165
246
|
throw new RangeError("invalid 'xmin' value");
|
|
166
247
|
if (!Number.isFinite(upper))
|
|
167
248
|
throw new RangeError("invalid 'xmax' value");
|
|
168
|
-
|
|
249
|
+
// upper - lower overflows (both finite): the root of f(2u) on (lower/2, upper/2) with tol/2, doubled (uniroot(x,
|
|
250
|
+
// -1e308, 1e308) gave -Infinity)
|
|
251
|
+
const wide = !Number.isFinite(upper - lower);
|
|
252
|
+
const zh = wide
|
|
253
|
+
? zero((u) => finiteValue(f(2 * u)), lower * 0.5, upper * 0.5, finiteValue(fLo), finiteValue(fUp), tol * 0.5, maxiter)
|
|
254
|
+
: zero((x) => finiteValue(f(x)), lower, upper, finiteValue(fLo), finiteValue(fUp), tol, maxiter);
|
|
255
|
+
const z = wide ? { ...zh, root: 2 * zh.root, estimPrec: 2 * zh.estimPrec } : zh;
|
|
169
256
|
return {
|
|
170
257
|
root: z.root,
|
|
171
258
|
fRoot: f(z.root),
|
|
@@ -14,7 +14,10 @@ export type Ties = "mean" | "min" | "max" | ((v: number[]) => number);
|
|
|
14
14
|
export interface ApproxOptions {
|
|
15
15
|
/** Points to interpolate at. Default: `n` equally spaced points over the range of `x`. */
|
|
16
16
|
xout?: Sample;
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* `"linear"` (default) or `"constant"` (step function). A unique prefix is accepted and anything else is an
|
|
19
|
+
* error, as in R.
|
|
20
|
+
*/
|
|
18
21
|
method?: "linear" | "constant";
|
|
19
22
|
/** Number of points when `xout` is not given. Default `50`. */
|
|
20
23
|
n?: number;
|
|
@@ -22,7 +25,10 @@ export interface ApproxOptions {
|
|
|
22
25
|
yleft?: number;
|
|
23
26
|
/** Value right of the data. Default: NaN (rule 1) or the last y (rule 2). */
|
|
24
27
|
yright?: number;
|
|
25
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Extrapolation rule (1: NaN, 2: the end value), for both sides or `[left, right]`. Default `1`. Any other value is
|
|
30
|
+
* an error (R takes every value other than 1 as 2).
|
|
31
|
+
*/
|
|
26
32
|
rule?: number | readonly [number, number];
|
|
27
33
|
/** For `"constant"`: the step value is (1 - f) y_left + f y_right, with f in [0, 1] (else an error, as in R). Default `0`. */
|
|
28
34
|
f?: number;
|