@numero-uno-jpn/rstats 0.15.3 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +24 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/numerics/ode.js
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
// Written for rStats.js from the textbook formulas (no ported code). The step is the spacing of `times` (one step
|
|
5
5
|
// per interval), as in deSolve's fixed-step methods. rk2 is Heun's method (deSolve's rkMethod("rk2")): the slopes at
|
|
6
6
|
// the start and at the Euler predictor, averaged; rk4 is the classical fourth-order Runge-Kutta method.
|
|
7
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
8
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
9
|
+
const ODE_KEYS = ["method"];
|
|
7
10
|
/**
|
|
8
11
|
* Solve the initial value problem y' = func(t, y, parms), y(times[0]) = y, with one fixed step per interval of
|
|
9
12
|
* `times`. Equivalent to `deSolve::ode(y, times, func, parms, method)` in R. That is for `"euler"` and `"rk4"`; for
|
|
@@ -22,9 +25,14 @@
|
|
|
22
25
|
* @category Numerical methods
|
|
23
26
|
*/
|
|
24
27
|
export function ode(y, times, func, parms = [], options = {}) {
|
|
28
|
+
checkOptions(options, ODE_KEYS, "ode"); // generated: option-keys.mjs
|
|
25
29
|
const method = options.method ?? "rk4";
|
|
26
30
|
if (method !== "euler" && method !== "rk2" && method !== "rk4")
|
|
27
31
|
throw new RangeError(`unknown method '${String(method)}'`);
|
|
32
|
+
// an infinite or NaN time gives an infinite or NaN step, and the state became NaN even with a zero derivative
|
|
33
|
+
// (Infinity * 0) (#360)
|
|
34
|
+
if (!times.every(Number.isFinite))
|
|
35
|
+
throw new RangeError("'times' must be finite values");
|
|
28
36
|
const p = [...parms];
|
|
29
37
|
const n = y.length;
|
|
30
38
|
const deriv = (t, s) => {
|
|
@@ -42,6 +50,11 @@ export function ode(y, times, func, parms = [], options = {}) {
|
|
|
42
50
|
for (let j = 1; j < times.length; j++) {
|
|
43
51
|
const t = times[j - 1];
|
|
44
52
|
const h = times[j] - t;
|
|
53
|
+
if (!Number.isFinite(h) && Number.isFinite(t) && Number.isFinite(times[j])) {
|
|
54
|
+
state = halvedStep(state, t, times[j] / 2 - t / 2, method, deriv);
|
|
55
|
+
out.push([times[j], ...state]);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
45
58
|
const k1 = deriv(t, state);
|
|
46
59
|
if (method === "euler")
|
|
47
60
|
state = axpy(state, h, k1);
|
|
@@ -53,9 +66,54 @@ export function ode(y, times, func, parms = [], options = {}) {
|
|
|
53
66
|
const k2 = deriv(t + h / 2, axpy(state, h / 2, k1));
|
|
54
67
|
const k3 = deriv(t + h / 2, axpy(state, h / 2, k2));
|
|
55
68
|
const k4 = deriv(t + h, axpy(state, h, k3));
|
|
56
|
-
|
|
69
|
+
// h/6 times the weighted sum, as deSolve. Where h/6 falls below the normal range or the sum overflows (2 k2 for a
|
|
70
|
+
// slope near the largest double), the sum divided by 4 (exact weights, no overflow), times h (a small h first
|
|
71
|
+
// brought into the normal range by 2^60, exactly), over 1.5: ode([0], [0, MIN], () => [1]) stayed at 0, and a
|
|
72
|
+
// slope of 1e308 over h = 1e-308 gave Infinity for 1 (issue #124)
|
|
73
|
+
const h6 = h / 6;
|
|
74
|
+
const plain = h === 0 || Math.abs(h6) >= 2 ** -1022;
|
|
75
|
+
const scale = Math.abs(h) < 1 ? 2 ** 60 : 1;
|
|
76
|
+
state = state.map((v, i) => {
|
|
77
|
+
const inc = h6 * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]);
|
|
78
|
+
if (plain && Number.isFinite(inc))
|
|
79
|
+
return v + inc;
|
|
80
|
+
const quarter = k1[i] / 4 + k2[i] / 2 + k3[i] / 2 + k4[i] / 4;
|
|
81
|
+
const scaled = h * scale * quarter;
|
|
82
|
+
// the quarter-weighted sum itself overflows for finite slopes (weights summing to 1.5: ode([0], [0, 0.5], () =>
|
|
83
|
+
// [1.7e308]) was Infinity for 8.5e307, #469): the sum divided by 8, 2 h eighth / 1.5
|
|
84
|
+
if (!Number.isFinite(quarter) && [k1[i], k2[i], k3[i], k4[i]].every(Number.isFinite)) {
|
|
85
|
+
const eighth = k1[i] / 8 + k2[i] / 4 + k3[i] / 4 + k4[i] / 8;
|
|
86
|
+
return v + 2 * ((h * eighth) / 1.5);
|
|
87
|
+
}
|
|
88
|
+
if (Number.isFinite(scaled) || !Number.isFinite(quarter))
|
|
89
|
+
return v + scaled / 1.5 / scale;
|
|
90
|
+
// the scaled product overflows for a large slope (ode([0], [0, 0.5], () => [1e308]) was Infinity for 5e307,
|
|
91
|
+
// #394): there h quarter is far above the normal range and needs no scale; where it overflows too, with h / 2
|
|
92
|
+
const p = h * quarter;
|
|
93
|
+
return v + (Number.isFinite(p) ? p / 1.5 : 2 * (((h / 2) * quarter) / 1.5));
|
|
94
|
+
});
|
|
57
95
|
}
|
|
58
96
|
out.push([times[j], ...state]);
|
|
59
97
|
}
|
|
60
98
|
return out;
|
|
61
99
|
}
|
|
100
|
+
// One step whose length h = 2 hh overflows although both times are finite (ode(y, c(-1e308, 1e308), ...)): the same
|
|
101
|
+
// formulas with h k formed as hh k added twice, so a zero slope keeps the state (1 + Inf * 0 was NaN, issue #211). The
|
|
102
|
+
// stage times are t + hh and (t + hh) + hh, the midpoint and the end.
|
|
103
|
+
function halvedStep(state, t, hh, method, deriv) {
|
|
104
|
+
const twice = (s, c, k) => s.map((v, i) => v + c * k[i] + c * k[i]);
|
|
105
|
+
const mid = t + hh;
|
|
106
|
+
const end = mid + hh;
|
|
107
|
+
const k1 = deriv(t, state);
|
|
108
|
+
if (method === "euler")
|
|
109
|
+
return twice(state, hh, k1);
|
|
110
|
+
if (method === "rk2") {
|
|
111
|
+
const k2 = deriv(end, twice(state, hh, k1));
|
|
112
|
+
return twice(state, hh, k1.map((v, i) => 0.5 * v + 0.5 * k2[i]));
|
|
113
|
+
}
|
|
114
|
+
const k2 = deriv(mid, state.map((v, i) => v + hh * k1[i]));
|
|
115
|
+
const k3 = deriv(mid, state.map((v, i) => v + hh * k2[i]));
|
|
116
|
+
const k4 = deriv(end, twice(state, hh, k3));
|
|
117
|
+
// h/6 (k1 + 2 k2 + 2 k3 + k4) = (hh / 1.5) times the quarter-weighted sum, added twice
|
|
118
|
+
return twice(state, hh / 1.5, k1.map((v, i) => v / 4 + k2[i] / 2 + k3[i] / 2 + k4[i] / 4));
|
|
119
|
+
}
|
package/dist/numerics.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.
|
|
2
|
-
import{a as $,b as T,c as D,d as F,e as O}from"./chunks/chunk-PMJI5QO6.js";import"./chunks/chunk-ZIX7NYQF.js";import{a as k,f as j}from"./chunks/chunk-MAUAOQG3.js";import"./chunks/chunk-IUULP3LE.js";import{f as S}from"./chunks/chunk-3WPU6LGK.js";import"./chunks/chunk-VLUL2HFW.js";import"./chunks/chunk-KT66SB7R.js";function M(o,l,t="mean"){if(o.length!==l.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(l[r])&&n.push(r);n.sort((r,s)=>o[r]-o[s]);let e=typeof t=="function"?t:t==="min"?r=>Math.min(...r):t==="max"?r=>Math.max(...r):S,a=[],f=[];for(let r=0;r<n.length;){let s=o[n[r]],c=r+1;for(;c<n.length&&o[n[c]]===s;)c++;a.push(s),f.push(c-r===1?l[n[r]]:e(n.slice(r,c).map(i=>l[i]))),r=c}return{x:a,y:f}}function X(o,l,t,n){let e=(n-o[t])/(o[t+1]-o[t]);return l[t]+(l[t+1]-l[t])*e}function J(o,l,t){let n=(e,a)=>e===0?0:e*a;return n(1-t,o[l])+n(t,o[l+1])}function P(o,l,t={}){let{method:n="linear",n:e=50,f:a=0,rule:f=1,ties:r="mean"}=t,s=M(o,l,r),c=s.x.length;if(c<=1&&n==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(c===0)throw new RangeError("zero non-NA points");if(n==="constant"&&!(a>=0&&a<=1))throw new RangeError("approx(): invalid f value");let[i,p]=typeof f=="number"?[f,f]:f,d=t.yleft??(i===1?NaN:s.y[0]),h=t.yright??(p===1?NaN:s.y[c-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(e<=0)throw new RangeError("'approx' requires n >= 1");m=k(s.x[0],s.x[c-1],e)}let g=s.x,u=s.y,w=j(m,g),y=m.map((b,A)=>{let R=w[A];if(Number.isNaN(R))return NaN;if(R===0)return d;let E=R-1;return b===g[E]?u[E]:R===c?h:n==="linear"?X(g,u,E,b):J(u,E,a)});return{x:m,y}}function v(o,l){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),a=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:a};if(t<3)return n[0]=(l[1]-l[0])/(o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:a};let f=t-1;a[0]=o[1]-o[0],e[1]=(l[1]-l[0])/a[0];for(let r=1;r<f;r++)a[r]=o[r+1]-o[r],n[r]=2*(a[r-1]+a[r]),e[r+1]=(l[r+1]-l[r])/a[r],e[r]=e[r+1]-e[r];n[0]=-a[0],n[f]=-a[t-2],e[0]=0,e[f]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[f]=e[t-2]/(o[f]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*a[0]*a[0]/(o[3]-o[0]),e[f]=-e[f]*a[t-2]*a[t-2]/(o[f]-o[t-4]));for(let r=1;r<t;r++){let s=a[r-1]/n[r-1];n[r]=n[r]-s*a[r-1],e[r]=e[r]-s*e[r-1]}e[f]=e[f]/n[f];for(let r=t-2;r>=0;r--)e[r]=(e[r]-a[r]*e[r+1])/n[r];n[f]=(l[f]-l[t-2])/a[t-2]+a[t-2]*(e[t-2]+2*e[f]);for(let r=0;r<f;r++)n[r]=(l[r+1]-l[r])/a[r]-a[r]*(e[r+1]+2*e[r]),a[r]=(e[r+1]-e[r])/a[r],e[r]=3*e[r];return e[f]=3*e[f],a[f]=a[t-2],{b:n,c:e,d:a}}function z(o,l,t){let n=o.length,e=new Array(n).fill(0),a=new Array(n).fill(0),f=new Array(n).fill(0);for(let r=0;r<n-1;r++){let s=o[r+1]-o[r];e[r]=(l[r+1]-l[r])/s-s*(t[r+1]+2*t[r]),a[r]=3*t[r],f[r]=(t[r+1]-t[r])/s}return{b:e,c:a,d:f}}function Y(o,l){let t=o.length;if(t<3)return v(o,l);let n=Array.from({length:t-1},(c,i)=>o[i+1]-o[i]),e=n.map((c,i)=>(l[i+1]-l[i])/c),a=new Array(t).fill(0),f=new Array(t).fill(0);for(let c=1;c<t-1;c++)a[c]=2*(n[c-1]+n[c]),f[c]=e[c]-e[c-1];for(let c=2;c<t-1;c++){let i=n[c-1]/a[c-1];a[c]=a[c]-i*n[c-1],f[c]=f[c]-i*f[c-1]}let r=new Array(t).fill(0);r[t-2]=f[t-2]/a[t-2];for(let c=t-3;c>=1;c--)r[c]=(f[c]-n[c]*r[c+1])/a[c];let s=z(o,l,r);return s.b[t-1]=e[t-2]+n[t-2]*r[t-2],s}function B(o,l){let t=o.length;if(l[0]!==l[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(u,w)=>o[w+1]-o[w]),a=e.map((u,w)=>(l[w+1]-l[w])/u),f=u=>u===0?n-1:u-1,r=Array.from({length:n},(u,w)=>2*(e[f(w)]+e[w])),s=Array.from({length:n},(u,w)=>a[w]-a[f(w)]),c=e.slice(),i=new Array(n).fill(0);i[0]=e[n-1],i[n-2]=i[n-2]+e[n-2];let p=i[0],d=r[n-1],h=s[n-1];for(let u=0;u<n-1;u++){if(u+1<n-1){let y=e[u]/r[u];r[u+1]=r[u+1]-y*c[u],i[u+1]=i[u+1]-y*i[u],s[u+1]=s[u+1]-y*s[u]}let w=p/r[u];d-=w*i[u],h-=w*s[u],u+1<n-1&&(p=(u+1===n-2?e[n-2]:0)-w*c[u])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let u=n-2;u>=0;u--){let w=s[u]-i[u]*m[n-1]-(u<n-2?c[u]*m[u+1]:0);m[u]=w/r[u]}m[t-1]=m[0];let g=z(o,l,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function G(o,l){let t=o.length,{b:n,c:e,d:a}=v(o,l);if(t<2)return{b:n,c:e,d:a};let f=Array.from({length:t-1},(s,c)=>(l[c+1]-l[c])/(o[c+1]-o[c]));for(let s=0;s<t;s++){let c=f[s===0?0:s-1],i=f[s===t-1?t-2:s],p=Math.min(Math.abs(c),Math.abs(i)),d=c*i>0?i>0:n[s]>=0;n[s]=d?Math.min(Math.max(0,n[s]),3*p):Math.max(Math.min(0,n[s]),-3*p)}for(let s=0;s<t-1;s++){let c=o[s+1]-o[s],i=(l[s+1]-l[s])/c;e[s]=(3*i-2*n[s]-n[s+1])/c,a[s]=(n[s]+n[s+1]-2*i)/(c*c)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(l[t-1]-l[t-2])/r)/r,a[t-1]=a[t-2],{b:n,c:e,d:a}}function q(o,l,t,n){let e=M(o,l,n),a=e.x.length;if(a===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[a-1]!==e.y[0]&&(e.y[a-1]=e.y[0]),t==="hyman"&&K(e.y);let f=t==="fmm"?v(e.x,e.y):t==="natural"?Y(e.x,e.y):t==="periodic"?B(e.x,e.y):t==="hyman"?G(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...f}}function K(o){let l=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(l=!1),e<=0||(t=!1)}if(!l&&!t)throw new RangeError("'y' must be increasing or decreasing")}function C(o){let l=Math.trunc(o);if(!(l>=0&&l<=3))throw new RangeError("'deriv' must be between 0 and 3");return l}function H(o,l,t){let n=C(t),{x:e,y:a,b:f,c:r,d:s,method:c}=o,i=e.length,p=c==="natural",d=e[i-1]-e[0],h=0;return Array.from(l,m=>{let g=m;if(c==="periodic"&&i>1){let y=(g-e[0])%d;y<0&&(y+=d),g=e[0]+y}if(p&&n>0&&g<=e[0])return n===1?f[0]:0;if(!(e[h]<=g&&(h===i-1||g<=e[h+1]))){let y=0,b=i;for(;b-y>1;){let A=y+b>>1;e[A]<=g?y=A:b=A}h=y}let u=g-e[h],w=p&&g<e[0]?0:s[h];switch(n){case 0:return a[h]+u*(f[h]+u*(r[h]+u*w));case 1:return f[h]+u*(2*r[h]+u*(3*w));case 2:return 2*r[h]+u*(6*w);default:return 6*w}})}function Q(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t,a=q(o,l,n,e),f;if(t.xout!==void 0)f=Array.from(t.xout);else{let r=a.x.length,s=t.n??3*r;if(s<=0)throw new RangeError("'spline' requires n >= 1");f=k(t.xmin??a.x[0],t.xmax??a.x[r-1],s)}return{x:f,y:H(a,f,0)}}function U(o,l,t={}){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let f=M(o,l,e);if(f.x.length===0)throw new RangeError("zero non-NA points");let r=V(f.x,f.y);return(s,c=0)=>I(f.x,f.y,r,s,c)}let a=q(o,l,n,e);return(f,r=0)=>H(a,f,r)}function V(o,l){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(a,f)=>(l[f+1]-l[f])/(o[f+1]-o[f])),e=Array.from({length:t},(a,f)=>f===0?n[0]:f===t-1?n[t-2]:(n[f-1]+n[f])/2);for(let a=0;a<t-1;a++){let f=n[a];if(f===0){e[a]=0,e[a+1]=0;continue}let r=e[a]/f,s=e[a+1]/f,c=2*r+s-3,i=r+2*s-3;if(c>0&&i>0&&r*(c+i)<c*c){let p=3*f/Math.sqrt(r*r+s*s);e[a]=p*r,e[a+1]=p*s}}return e}function I(o,l,t,n,e){let a=C(e),f=o.length;return Array.from(n,r=>{if(Number.isNaN(r))return NaN;let s=0,c=f;for(;s<c;){let A=s+c>>1;o[A]<=r?s=A+1:c=A}let i=s;if(i===0||i===f){let A=i===0?0:f-1;return a===0?l[A]+t[A]*(r-o[A]):a===1?t[A]:0}let p=o[i-1],d=o[i]-p,h=(r-p)/d,m=h-1,g=l[i-1],u=l[i],w=t[i-1],y=t[i],b=(u-g)/d;switch(a){case 0:{let A=h*h*(3-2*h);return g*(1-A)+d*w*(h*m*m)+u*A+d*y*(h*h*m)}case 1:return b*(-6*h*m)+w*(m*(3*h-1))+y*(h*(3*h-2));case 2:return(b*(6-12*h)+w*(6*h-4)+y*(6*h-2))/d;default:return(-12*b+6*w+6*y)/(d*d)}})}function W(o,l,t){let n=o.length;if(l.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(s,c)=>c).sort((s,c)=>o[s]-o[c]),a=e.map(s=>o[s]),f=e.map(s=>l[s]),r=e.map(s=>t[s]);for(let s=1;s<n;s++)if(!(a[s]-a[s-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");return(s,c=0)=>I(a,f,r,s,c)}function Z(o,l,t){let n=Array.from(o),e=Array.from(l),a=n.length;if(e.length!==a)throw new RangeError("'xi' and 'yi' must have the same length");let f=Array.from(t),r=Math.min(...n),s=Math.max(...n);if(f.some(i=>i<r||i>s))throw new RangeError("Some interpolation points outside the nodes.");let c=n.map((i,p)=>{let d=1;for(let h=0;h<a;h++)h!==p&&(d*=i-n[h]);return 1/d});return f.map(i=>{let p=0,d=0,h=n.lastIndexOf(i);if(h>=0)return e[h];for(let m=0;m<a;m++){let g=i-n[m],u=c[m]/g;p+=u*e[m],d+=u}return p/d})}function tt(o,l,t,n){let e=Array.from(o),a=Array.from(l),f=Array.from(t),r=e.length;if(a.length!==r||f.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let s=e.flatMap(h=>[h,h]),c=2*r,i=a.flatMap(h=>[h,h]),p=[i[0]],d=i.slice();for(let h=1;h<c;h++){let m=[];for(let g=0;g<c-h;g++)h===1&&s[g+1]===s[g]?m.push(f[g>>1]):m.push((d[g+1]-d[g])/(s[g+h]-s[g]));p.push(m[0]),d=m}return Array.from(n,h=>{let m=p[c-1];for(let g=c-2;g>=0;g--)m=m*(h-s[g])+p[g];return m})}function nt(o,l,t,n=[],e={}){let a=e.method??"rk4";if(a!=="euler"&&a!=="rk2"&&a!=="rk4")throw new RangeError(`unknown method '${String(a)}'`);let f=[...n],r=o.length,s=(d,h)=>{let m=Array.from(t(d,h,f));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},c=(d,h,m)=>d.map((g,u)=>g+h*m[u]),i=[...o],p=[];if(l.length===0)return p;p.push([l[0],...i]);for(let d=1;d<l.length;d++){let h=l[d-1],m=l[d]-h,g=s(h,i);if(a==="euler")i=c(i,m,g);else if(a==="rk2"){let u=s(h+m,c(i,m,g));i=i.map((w,y)=>w+m*(.5*g[y]+.5*u[y]))}else{let u=s(h+m/2,c(i,m/2,g)),w=s(h+m/2,c(i,m/2,u)),y=s(h+m,c(i,m,w));i=i.map((b,A)=>b+m/6*(g[A]+2*u[A]+2*w[A]+y[A]))}p.push([l[d],...i])}return p}function _(o,l,t){let n=l.eps??1e-4,e=l.d??t,a=l.zeroTol??Math.sqrt(Number.EPSILON/7e-7),f=l.r??4;if(!(Number.isInteger(f)&&f>=1))throw new RangeError("'r' must be a positive integer");return{eps:n,r:f,h0:o.map(r=>Math.abs(e*r)+(Math.abs(r)<a?n:0))}}function x(o){let l=[...o];for(let t=1;t<l.length;t++){let n=4**t;for(let e=0;e<l.length-t;e++)l[e]=(l[e+1]*n-l[e])/(n-1)}return l[0]}function N(o,l,t){let n=[...o];return n[l]=n[l]+t,n}function L(o){let l=o.method??"Richardson";if(l!=="Richardson"&&l!=="simple")throw new RangeError(`unknown method "${String(l)}" (expected "Richardson" or "simple")`);return l}function rt(o,l,t={}){let n=[...l],e=n.length;if(L(t)==="simple"){let s=t.eps??1e-4,c=o([...n]);return n.map((i,p)=>(o(N(n,p,s))-c)/s)}let{r:a,h0:f}=_(n,t,1e-4),r=[];for(let s=0;s<e;s++){let c=[],i=f[s];for(let p=0;p<a;p++){if(p>0&&Math.abs(c[p-1])<1e-20?c.push(0):c.push((o(N(n,s,i))-o(N(n,s,-i)))/(2*i)),Number.isNaN(c[p]))throw new RangeError(`func returned NaN at step ${i} from x`);i/=2}r.push(x(c))}return r}function et(o,l,t={}){let n=[...l],e=n.length,a=Array.from(o([...n])),f=a.map(()=>new Array(e).fill(NaN));if(L(t)==="simple"){let c=t.eps??1e-4;for(let i=0;i<e;i++){let p=o(N(n,i,c));a.forEach((d,h)=>f[h][i]=(p[h]-d)/c)}return f}let{r,h0:s}=_(n,t,1e-4);for(let c=0;c<e;c++){let i=a.map(()=>[]),p=s[c];for(let d=0;d<r;d++){let h=o(N(n,c,p)),m=o(N(n,c,-p));a.forEach((g,u)=>i[u].push((h[u]-m[u])/(2*p))),p/=2}a.forEach((d,h)=>f[h][c]=x(i[h]))}return f}function ot(o,l,t={}){if((t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...l],e=n.length,{r:a,h0:f}=_(n,t,.1),r=o([...n]),s=[];for(let i=0;i<e;i++){let p=[],d=f[i];for(let h=0;h<a;h++)p.push((o(N(n,i,d))-2*r+o(N(n,i,-d)))/d**2),d/=2;s.push(x(p))}let c=n.map(()=>new Array(e).fill(NaN));for(let i=0;i<e;i++){c[i][i]=s[i];for(let p=0;p<i;p++){let d=[],h=f[i],m=f[p];for(let g=0;g<a;g++){let u=N(N(n,i,h),p,m),w=N(N(n,i,-h),p,-m);d.push((o(u)-2*r+o(w)-s[i]*h**2-s[p]*m**2)/(2*h*m)),h/=2,m/=2}c[i][p]=c[p][i]=x(d)}}return c}export{P as approx,Z as barylag,rt as grad,tt as hermiteInterp,ot as hessian,$ as integrate,et as jacobian,nt as ode,O as optim,F as optimHess,T as optimize,Q as spline,U as splinefun,W as splinefunH,D as uniroot};
|
|
1
|
+
/*! rStats.js 0.17.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as st,b as it,c as ct,e as at,f as ft}from"./chunks/chunk-2C33MXSW.js";import"./chunks/chunk-Z37NELOS.js";import{a as D,f as Z}from"./chunks/chunk-ZFQWB222.js";import"./chunks/chunk-ONY5CISH.js";import{b as P}from"./chunks/chunk-RFE7MVP7.js";import"./chunks/chunk-DXI5N2ET.js";import"./chunks/chunk-C7V2FH6L.js";import{a as $,b as V,c as K}from"./chunks/chunk-SE7V2KLO.js";import{l as j,u as y,v as C,x as Q}from"./chunks/chunk-WM7VFXTV.js";var ut=["xout","method","n","yleft","yright","rule","f","ties"],lt=["n","method","xmin","xmax","xout","ties"],ht=["method","ties"];function H(o,i,t="mean"){if(o.length!==i.length)throw new RangeError("'x' and 'y' lengths differ");let n=[];for(let r=0;r<o.length;r++)!Number.isNaN(o[r])&&!Number.isNaN(i[r])&&n.push(r);n.sort((r,a)=>o[r]-o[a]);let e=typeof t=="function"?t:t==="min"?r=>K(r):t==="max"?r=>$(r):Q,s=[],u=[];for(let r=0;r<n.length;){let a=o[n[r]],f=r+1;for(;f<n.length&&o[n[f]]===a;)f++;s.push(a),u.push(f-r===1?i[n[r]]:e(n.slice(r,f).map(c=>i[c]))),r=f}return{x:s,y:u}}function mt(o,i,t,n){let e=(n-o[t])/(o[t+1]-o[t]);(!Number.isFinite(o[t+1]-o[t])||!Number.isFinite(n-o[t]))&&(e=(n/2-o[t]/2)/(o[t+1]/2-o[t]/2));let s=i[t+1]-i[t];return!Number.isFinite(s)&&Number.isFinite(i[t])&&Number.isFinite(i[t+1])?2*(i[t]/2+(i[t+1]/2-i[t]/2)*e):i[t]+s*e}function J(o,i,t){let n=i-o;return!Number.isFinite(n)&&Number.isFinite(o)&&Number.isFinite(i)?2*((i/2-o/2)/t):n/t}function O(o,i,t){let n=1,e=t;for(let s of o){if(s===0)return 0*n;let u=Math.floor(Math.log2(Math.abs(s)));n*=y(s,-u),e+=u}for(let s of i){let u=Math.floor(Math.log2(Math.abs(s)));n/=y(s,-u),e-=u}return C(n,e)}function pt(o,i,t){let n=(e,s)=>e===0?0:e*s;return n(1-t,o[i])+n(t,o[i+1])}function bt(o){if(o===void 0)return"linear";if(typeof o=="string"&&o.length>0){let i=["linear","constant"].filter(t=>t.startsWith(o));if(i.length===1)return i[0]}throw new RangeError("invalid interpolation method")}function dt(o){let i=typeof o=="number"?[o]:Array.isArray(o)?o:[],t=n=>n===1||n===2;if(i.length<1||i.length>2||!i.every(t))throw new RangeError("'rule' must be 1 or 2, or a pair of them");return[i[0],i[i.length-1]]}function gt(o,i,t={}){j(t,ut,"approx");let{n=50,f:e=0,rule:s=1,ties:u="mean"}=t,r=bt(t.method),[a,f]=dt(s),c=H(o,i,u),p=c.x.length;if(p<=1&&r==="linear")throw new RangeError("need at least two non-NA values to interpolate");if(p===0)throw new RangeError("zero non-NA points");if(r==="constant"&&!(e>=0&&e<=1))throw new RangeError("approx(): invalid f value");let d=t.yleft??(a===1?NaN:c.y[0]),h=t.yright??(f===1?NaN:c.y[p-1]),m;if(t.xout!==void 0)m=Array.from(t.xout);else{if(!Number.isFinite(n)&&!Number.isNaN(n))throw new RangeError("'length.out' must be a finite number");if(!(n>0))throw new RangeError("'approx' requires n >= 1");m=D(c.x[0],c.x[p-1],n)}let g=c.x,l=c.y,b=Z(m,g),N=m.map((M,A)=>{let E=b[A];if(Number.isNaN(E))return NaN;if(E===0)return d;let R=E-1;return M===g[R]?l[R]:E===p?h:r==="linear"?mt(g,l,R,M):pt(l,R,e)});return{x:m,y:N}}function W(o,i){let t=o.length,n=new Array(t).fill(0),e=new Array(t).fill(0),s=new Array(t).fill(0);if(t<2)return{b:n,c:e,d:s};if(t<3)return n[0]=J(i[0],i[1],o[1]-o[0]),n[1]=n[0],{b:n,c:e,d:s};let u=t-1;s[0]=o[1]-o[0],e[1]=(i[1]-i[0])/s[0];for(let r=1;r<u;r++)s[r]=o[r+1]-o[r],n[r]=2*(s[r-1]+s[r]),e[r+1]=(i[r+1]-i[r])/s[r],e[r]=e[r+1]-e[r];n[0]=-s[0],n[u]=-s[t-2],e[0]=0,e[u]=0,t>3&&(e[0]=e[2]/(o[3]-o[1])-e[1]/(o[2]-o[0]),e[u]=e[t-2]/(o[u]-o[t-3])-e[t-3]/(o[t-2]-o[t-4]),e[0]=e[0]*s[0]*s[0]/(o[3]-o[0]),e[u]=-e[u]*s[t-2]*s[t-2]/(o[u]-o[t-4]));for(let r=1;r<t;r++){let a=s[r-1]/n[r-1];n[r]=n[r]-a*s[r-1],e[r]=e[r]-a*e[r-1]}e[u]=e[u]/n[u];for(let r=t-2;r>=0;r--)e[r]=(e[r]-s[r]*e[r+1])/n[r];n[u]=(i[u]-i[t-2])/s[t-2]+s[t-2]*(e[t-2]+2*e[u]);for(let r=0;r<u;r++)n[r]=(i[r+1]-i[r])/s[r]-s[r]*(e[r+1]+2*e[r]),s[r]=(e[r+1]-e[r])/s[r],e[r]=3*e[r];return e[u]=3*e[u],s[u]=s[t-2],{b:n,c:e,d:s}}function tt(o,i,t){let n=o.length,e=new Array(n).fill(0),s=new Array(n).fill(0),u=new Array(n).fill(0);for(let r=0;r<n-1;r++){let a=o[r+1]-o[r];e[r]=(i[r+1]-i[r])/a-a*(t[r+1]+2*t[r]),s[r]=3*t[r],u[r]=(t[r+1]-t[r])/a}return{b:e,c:s,d:u}}function Nt(o,i){let t=o.length;if(t<3)return W(o,i);let n=Array.from({length:t-1},(f,c)=>o[c+1]-o[c]),e=n.map((f,c)=>(i[c+1]-i[c])/f),s=new Array(t).fill(0),u=new Array(t).fill(0);for(let f=1;f<t-1;f++)s[f]=2*(n[f-1]+n[f]),u[f]=e[f]-e[f-1];for(let f=2;f<t-1;f++){let c=n[f-1]/s[f-1];s[f]=s[f]-c*n[f-1],u[f]=u[f]-c*u[f-1]}let r=new Array(t).fill(0);r[t-2]=u[t-2]/s[t-2];for(let f=t-3;f>=1;f--)r[f]=(u[f]-n[f]*r[f+1])/s[f];let a=tt(o,i,r);return a.b[t-1]=e[t-2]+n[t-2]*r[t-2],a}function wt(o,i){let t=o.length;if(i[0]!==i[t-1])throw new RangeError("periodic spline needs y[1] == y[n]");if(t<3)return{b:new Array(t).fill(0),c:new Array(t).fill(0),d:new Array(t).fill(0)};let n=t-1,e=Array.from({length:n},(l,b)=>o[b+1]-o[b]),s=e.map((l,b)=>(i[b+1]-i[b])/l),u=l=>l===0?n-1:l-1,r=Array.from({length:n},(l,b)=>2*(e[u(b)]+e[b])),a=Array.from({length:n},(l,b)=>s[b]-s[u(b)]),f=e.slice(),c=new Array(n).fill(0);c[0]=e[n-1],c[n-2]=c[n-2]+e[n-2];let p=c[0],d=r[n-1],h=a[n-1];for(let l=0;l<n-1;l++){if(l+1<n-1){let N=e[l]/r[l];r[l+1]=r[l+1]-N*f[l],c[l+1]=c[l+1]-N*c[l],a[l+1]=a[l+1]-N*a[l]}let b=p/r[l];d-=b*c[l],h-=b*a[l],l+1<n-1&&(p=(l+1===n-2?e[n-2]:0)-b*f[l])}let m=new Array(t).fill(0);m[n-1]=h/d;for(let l=n-2;l>=0;l--){let b=a[l]-c[l]*m[n-1]-(l<n-2?f[l]*m[l+1]:0);m[l]=b/r[l]}m[t-1]=m[0];let g=tt(o,i,m);return g.b[t-1]=g.b[0],g.c[t-1]=g.c[0],g.d[t-1]=g.d[0],g}function Mt(o,i){let t=o.length,{b:n,c:e,d:s}=W(o,i);if(t<2)return{b:n,c:e,d:s};let u=Array.from({length:t-1},(a,f)=>(i[f+1]-i[f])/(o[f+1]-o[f]));for(let a=0;a<t;a++){let f=u[a===0?0:a-1],c=u[a===t-1?t-2:a],p=Math.min(Math.abs(f),Math.abs(c)),d=f>0&&c>0||f<0&&c<0?c>0:n[a]>=0;n[a]=d?Math.min(Math.max(0,n[a]),3*p):Math.max(Math.min(0,n[a]),-3*p)}for(let a=0;a<t-1;a++){let f=o[a+1]-o[a],c=(i[a+1]-i[a])/f;e[a]=(3*c-2*n[a]-n[a+1])/f,s[a]=(n[a]+n[a+1]-2*c)/(f*f)}let r=o[t-1]-o[t-2];return e[t-1]=(n[t-2]+2*n[t-1]-3*(i[t-1]-i[t-2])/r)/r,s[t-1]=s[t-2],{b:n,c:e,d:s}}function T(o){let i=o.length;return i<2||!o.every(Number.isFinite)||Number.isFinite(o[i-1]-o[0])?0:Math.floor(Math.log2($(o.map(Math.abs))))}function L(o,i,t,n){let e=H(o,i,n),s=e.x.length;if(s===0)throw new RangeError("zero non-NA points");t==="periodic"&&e.y[s-1]!==e.y[0]&&(e.y[s-1]=e.y[0]),t==="hyman"&&yt(e.y);let u=t==="fmm"?W(e.x,e.y):t==="natural"?Nt(e.x,e.y):t==="periodic"?wt(e.x,e.y):t==="hyman"?Mt(e.x,e.y):(()=>{throw new RangeError(`invalid spline method '${String(t)}'`)})();return{method:t,...e,...u}}function yt(o){let i=!0,t=!0;for(let n=1;n<o.length;n++){let e=o[n]-o[n-1];e>=0||(i=!1),e<=0||(t=!1)}if(!i&&!t)throw new RangeError("'y' must be increasing or decreasing")}function B(o){let i=Math.trunc(o);if(!(i>=0&&i<=3))throw new RangeError("'deriv' must be between 0 and 3");return i}function Y(o,i,t){let n=B(t),{x:e,y:s,b:u,c:r,d:a,method:f}=o,c=e.length,p=f==="natural",d=e[c-1]-e[0],h=0;return Array.from(i,m=>{let g=m;if(f==="periodic"&&c>1){let N=(g-e[0])%d;N<0&&(N+=d),g=e[0]+N}if(p&&n>0&&g<=e[0])return n===1?u[0]:0;if(!(e[h]<=g&&(h===c-1||g<=e[h+1]))){let N=0,M=c;for(;M-N>1;){let A=N+M>>1;e[A]<=g?N=A:M=A}h=N}let l=g-e[h],b=p&&g<e[0]?0:a[h];switch(n){case 0:return s[h]+l*(u[h]+l*(r[h]+l*b));case 1:return u[h]+l*(2*r[h]+l*(3*b));case 2:return 2*r[h]+l*(6*b);default:return 6*b}})}function Ft(o,i,t={}){j(t,lt,"spline");let{method:n="fmm",ties:e="mean"}=t,s=L(o,i,n,e),u=T(s.x);u!==0&&(s=L(Array.from(o,a=>y(a,-u)),i,n,e));let r;if(t.xout!==void 0)r=Array.from(t.xout);else{let a=s.x.length,f=t.n??3*a;if(!Number.isFinite(f))throw new RangeError("'length.out' must be a finite number");if(!(f>0))throw new RangeError("'spline' requires n >= 1");r=D(t.xmin??y(s.x[0],u),t.xmax??y(s.x[a-1],u),f)}return{x:r,y:Y(s,u===0?r:r.map(a=>y(a,-u)),0)}}function At(o,i,t={}){j(t,ht,"splinefun");let n=Et(o,i,t);return(e,...s)=>n(e,P(["deriv"],s,"the function from splinefun").deriv??0)}function Et(o,i,t){let{method:n="fmm",ties:e="mean"}=t;if(n==="monoH.FC"){let r=H(o,i,e);if(r.x.length===0)throw new RangeError("zero non-NA points");let a=T(r.x);a!==0&&(r=H(Array.from(o,c=>y(c,-a)),i,e));let f=Rt(r.x,r.y);return(c,p)=>nt(r.x,r.y,f,a,a,c,p)}let s=L(o,i,n,e),u=T(s.x);return u===0?(r,a)=>Y(s,r,a):(s=L(Array.from(o,r=>y(r,-u)),i,n,e),(r,a)=>{let f=B(a);return Y(s,Array.from(r,c=>y(c,-u)),f).map(c=>y(c,-f*u))})}function Rt(o,i){let t=o.length;if(t<2)throw new RangeError("length(m) must be at least two");let n=Array.from({length:t-1},(s,u)=>(i[u+1]-i[u])/(o[u+1]-o[u])),e=Array.from({length:t},(s,u)=>u===0?n[0]:u===t-1?n[t-2]:V(n[u-1],n[u]));for(let s=0;s<t-1;s++){let u=n[s];if(u===0){e[s]=0,e[s+1]=0;continue}let r=e[s]/u,a=e[s+1]/u,f=2*r+a-3,c=r+2*a-3;if(!(Number.isFinite(r*(f+c))&&Number.isFinite(f*f)&&Number.isFinite(r*r+a*a))&&Number.isFinite(e[s])&&Number.isFinite(e[s+1])&&Number.isFinite(u)){let d=M=>M===0?-1/0:Math.floor(Math.log2(Math.abs(M))),h=Math.max(d(e[s]),d(e[s+1]))-d(u),m=O([e[s]],[u],-h),g=O([e[s+1]],[u],-h),l=C(3,-h),b=2*m+g-l,N=m+2*g-l;if(b>0&&N>0&&m*(b+N)<b*b){let M=3*u/Math.sqrt(m*m+g*g);e[s]=M*m,e[s+1]=M*g}continue}if(f>0&&c>0&&r*(f+c)<f*f){let d=3*u/Math.sqrt(r*r+a*a);e[s]=d*r,e[s+1]=d*a}}return e}function nt(o,i,t,n,e,s,u){let r=B(u),a=o.length;return Array.from(s,f=>{if(Number.isNaN(f))return NaN;let c=n===0?f:y(f,-n),p=0,d=a;for(;p<d;){let w=p+d>>1;o[w]<=c?p=w+1:d=w}let h=p;if(h===0||h===a){let w=h===0?0:a-1;return r===0?i[w]+y(t[w]*(c-o[w]),n-e):r===1?y(t[w],-e):0}let m=o[h-1],g=o[h]-m,l=(c-m)/g,b=l-1,N=i[h-1],M=i[h],A=t[h-1],E=t[h],R=y(J(N,M,g),e-n);switch(r){case 0:{let w=l*l*(3-2*l),F=(k,S)=>{let _=y(g*k,n-e)*S;return Number.isFinite(_)||!Number.isFinite(k)||!Number.isFinite(S)?_:O([g,k,S],[],n-e)};return N*(1-w)+F(A,l*b*b)+M*w+F(E,l*l*b)}case 1:{if(Number.isFinite(R)||!Number.isFinite(N)||!Number.isFinite(M))return y(R*(-6*l*b)+A*(b*(3*l-1))+E*(l*(3*l-2)),-e);let w=M-N;return(Number.isFinite(w)?O([w,-6*l*b],[g],-n):O([M/2-N/2,-6*l*b],[g],1-n))+y(A*(b*(3*l-1))+E*(l*(3*l-2)),-e)}case 2:{if(Number.isFinite(R)||!Number.isFinite(N)||!Number.isFinite(M))return y((R*(6-12*l)+A*(6*l-4)+E*(6*l-2))/g,-e-n);let w=M-N;return(Number.isFinite(w)?O([w,6-12*l],[g,g],-2*n):O([M/2-N/2,6-12*l],[g,g],1-2*n))+y((A*(6*l-4)+E*(6*l-2))/g,-e-n)}default:{let w=-12*R+6*A+6*E,F=g*g;return F>=2**-1022&&F<1/0?y(w/F,-e-2*n):y(w/g/g,-e-2*n)}}})}function xt(o,i,t){let n=o.length;if(i.length!==n||t.length!==n)throw new RangeError("'x', 'y' and 'm' must have the same length");let e=Array.from({length:n},(c,p)=>p).sort((c,p)=>o[c]-o[p]),s=e.map(c=>o[c]),u=e.map(c=>i[c]),r=e.map(c=>t[c]);for(let c=1;c<n;c++)if(!(s[c]-s[c-1]>0))throw new RangeError("'x' must be *strictly* increasing (non - NA)");let a=T(s),f=a===0?s:s.map(c=>y(c,-a));return(c,...p)=>nt(f,u,r,a,0,c,P(["deriv"],p,"the function from splinefunH").deriv??0)}function kt(o,i,t){let n=Array.from(o),e=Array.from(i),s=n.length;if(e.length!==s)throw new RangeError("'xi' and 'yi' must have the same length");let u=Array.from(t),r=K(n),a=$(n);if(u.some(b=>b<r||b>a))throw new RangeError("Some interpolation points outside the nodes.");let f=Number.isFinite(r)&&Number.isFinite(a)&&!Number.isFinite(a-r),c=f?n.map(b=>b/2):n,p=c.map((b,N)=>{let M=1,A=0;for(let E=0;E<s;E++){if(E===N)continue;let R=b-c[E],w=M*R;if(M!==0&&R!==0&&Number.isFinite(R)&&!(Math.abs(w)>=2**-1022&&Math.abs(w)<1/0)){let k=Math.floor(Math.log2(Math.abs(M))),S=Math.floor(Math.log2(Math.abs(R)));M=y(M,-k)*y(R,-S),A+=k+S;continue}M=w;let F=Math.abs(M);if(F>2**500||F<2**-500&&F>0){let k=Math.floor(Math.log2(F));M=y(M,-k),A+=k}}return{p:M,e:A}}),d=p.reduce((b,N)=>Math.max(b,-N.e),-1/0),h=p.map(b=>y(1/b.p,-b.e-d)),m=n.every(Number.isFinite)&&e.every(Number.isFinite),g=m?e.reduce((b,N)=>N===0?b:Math.max(b,Math.floor(Math.log2(Math.abs(N)))),-1/0):0,l=b=>{if(g===-1/0)return 0;let N=1,M=0,A=[];for(let F=0;F<s;F++){let k=b-c[F];if(k===0)return;let S=Math.floor(Math.log2(Math.abs(k))),_=y(k,-S);N*=_;let x=Math.floor(Math.log2(Math.abs(N)));N=y(N,-x),M+=S+x;let v=1/p[F].p/_;if(!Number.isFinite(v)||v===0)return;let z=Math.floor(Math.log2(Math.abs(v)));A.push({r:y(v,-z),x:z-p[F].e-S})}let E=A.reduce((F,k)=>Math.max(F,k.x),-1/0),R=0;for(let F=0;F<s;F++)R+=y(A[F].r,A[F].x-E)*y(e[F],-g);if(R===0)return 0;let w=Math.floor(Math.log2(Math.abs(R)));return C(N*y(R,-w),M+E+g+w)};return u.map(b=>{let N=0,M=0,A=n.lastIndexOf(b);if(A>=0)return e[A];let E=0,R=0;for(let x=0;x<s;x++){let v=(f?b/2:b)-c[x],z=h[x]/v;N+=z*e[x],M+=z,E+=Math.abs(z),R+=Math.abs(z*e[x])}let w=N/M;if(m&&Number.isFinite(E)&&E>0&&Math.abs(M)<y(E,-100)&&Number.isFinite(R)&&Math.abs(N)>=y(R,-26)){let x=l(f?b/2:b);if(x!==void 0)return x}if(Number.isFinite(w)||!m)return w;let F=p.map((x,v)=>{let z=(f?b/2:b)-c[v],U=Math.floor(Math.log2(Math.abs(z)));return{r:1/x.p/y(z,-U),x:-x.e-U}});if(!F.every(x=>Number.isFinite(x.r)&&Number.isFinite(x.x)))return w;let k=F.reduce((x,v)=>Math.max(x,v.x),-1/0),S=0,_=0;for(let x=0;x<s;x++){let v=y(F[x].r,F[x].x-k);S+=v*e[x],_+=v}return S/_})}function St(o,i,t,n){let e=Array.from(o),s=Array.from(i),u=Array.from(t),r=e.length;if(s.length!==r||u.length!==r)throw new RangeError("'xi', 'yi' and 'di' must have the same length");let a=T([...e].sort((h,m)=>h-m)),f=a===0?X(e,s,u):void 0;if(f?.lost&&[e,s,u].every(h=>h.every(Number.isFinite))){let h=Math.floor(Math.log2($(e.map(Math.abs))));h!==0&&([a,f]=[h,void 0])}f===void 0&&(f=X(e.map(h=>y(h,-a)),s,u.map(h=>y(h,a))));let{z:c,coef:p}=f,d=2*r;return Array.from(n,h=>{let m=p[d-1],g=a===0?h:y(h,-a);for(let l=d-2;l>=0;l--)m=m*(g-c[l])+p[l];return m})}function X(o,i,t){let n=o.flatMap(f=>[f,f]),e=n.length,s=!1,u=i.flatMap(f=>[f,f]),r=[u[0]],a=u.slice();for(let f=1;f<e;f++){let c=[];for(let p=0;p<e-f;p++)if(f===1&&n[p+1]===n[p])c.push(t[p>>1]);else{let d=a[p+1]-a[p],h=n[p+f]-n[p],m=J(a[p],a[p+1],h);Number.isFinite(a[p+1])&&Number.isFinite(a[p])&&d!==0&&h!==0&&!(Math.abs(m)>=2**-1022&&Math.abs(m)<1/0)&&(s=!0),c.push(m)}r.push(c[0]),a=c}return{z:n,coef:r,lost:s}}var vt=["method"];function It(o,i,t,n=[],e={}){j(e,vt,"ode");let s=e.method??"rk4";if(s!=="euler"&&s!=="rk2"&&s!=="rk4")throw new RangeError(`unknown method '${String(s)}'`);if(!i.every(Number.isFinite))throw new RangeError("'times' must be finite values");let u=[...n],r=o.length,a=(d,h)=>{let m=Array.from(t(d,h,u));if(m.length!==r)throw new RangeError(`The number of derivatives returned by func() (${m.length}) must equal the length of the initial conditions vector (${r})`);return m},f=(d,h,m)=>d.map((g,l)=>g+h*m[l]),c=[...o],p=[];if(i.length===0)return p;p.push([i[0],...c]);for(let d=1;d<i.length;d++){let h=i[d-1],m=i[d]-h;if(!Number.isFinite(m)&&Number.isFinite(h)&&Number.isFinite(i[d])){c=_t(c,h,i[d]/2-h/2,s,a),p.push([i[d],...c]);continue}let g=a(h,c);if(s==="euler")c=f(c,m,g);else if(s==="rk2"){let l=a(h+m,f(c,m,g));c=c.map((b,N)=>b+m*(.5*g[N]+.5*l[N]))}else{let l=a(h+m/2,f(c,m/2,g)),b=a(h+m/2,f(c,m/2,l)),N=a(h+m,f(c,m,b)),M=m/6,A=m===0||Math.abs(M)>=2**-1022,E=Math.abs(m)<1?2**60:1;c=c.map((R,w)=>{let F=M*(g[w]+2*l[w]+2*b[w]+N[w]);if(A&&Number.isFinite(F))return R+F;let k=g[w]/4+l[w]/2+b[w]/2+N[w]/4,S=m*E*k;if(!Number.isFinite(k)&&[g[w],l[w],b[w],N[w]].every(Number.isFinite)){let x=g[w]/8+l[w]/4+b[w]/4+N[w]/8;return R+2*(m*x/1.5)}if(Number.isFinite(S)||!Number.isFinite(k))return R+S/1.5/E;let _=m*k;return R+(Number.isFinite(_)?_/1.5:2*(m/2*k/1.5))})}p.push([i[d],...c])}return p}function _t(o,i,t,n,e){let s=(d,h,m)=>d.map((g,l)=>g+h*m[l]+h*m[l]),u=i+t,r=u+t,a=e(i,o);if(n==="euler")return s(o,t,a);if(n==="rk2"){let d=e(r,s(o,t,a));return s(o,t,a.map((h,m)=>.5*h+.5*d[m]))}let f=e(u,o.map((d,h)=>d+t*a[h])),c=e(u,o.map((d,h)=>d+t*f[h])),p=e(r,s(o,t,c));return s(o,t/1.5,a.map((d,h)=>d/4+f[h]/2+c[h]/2+p[h]/4))}var zt=["method","eps","d","zeroTol","r"],jt=["method","eps","d","zeroTol","r"],Ot=["method","eps","d","zeroTol","r"];function et(o){if(!(Number.isFinite(o)&&o!==0))throw new RangeError("'eps' must be a finite nonzero number")}function rt(o){let i=o.eps??1e-4;return et(i),i}function G(o,i,t){let n=i.eps??1e-4,e=i.d??t,s=i.zeroTol??Math.sqrt(Number.EPSILON/7e-7);if(!(Number.isFinite(e)&&e!==0))throw new RangeError("'d' must be a finite nonzero number");if(et(n),!(Number.isFinite(s)&&s>=0))throw new RangeError("'zeroTol' must be a non-negative finite number");let u=i.r??4;if(!(Number.isInteger(u)&&u>=1))throw new RangeError("'r' must be a positive integer");if(u>512)throw new RangeError("'r' must be at most 512");let r=o.map(a=>Math.abs(e*a)+(Math.abs(a)<s?n:0));return r.forEach((a,f)=>{if(!(Math.abs(a)>0&&Math.abs(a)<1/0))throw new RangeError(`the step for x[${f}] is ${a}: not a nonzero finite number`)}),{eps:n,r:u,h0:r}}function q(o){let i=[...o];for(let t=1;t<i.length;t++){let n=4**t;for(let e=0;e<i.length-t;e++)i[e]=(i[e+1]*n-i[e])/(n-1)}return i[0]}function I(o,i,t){let n=[...o];return n[i]=n[i]+t,n}function ot(o){let i=o.method??"Richardson";if(i!=="Richardson"&&i!=="simple")throw new RangeError(`unknown method "${String(i)}" (expected "Richardson" or "simple")`);return i}function $t(o,i,t={}){j(t,zt,"grad");let n=[...i],e=n.length;if(ot(t)==="simple"){let a=rt(t),f=o([...n]);return n.map((c,p)=>(o(I(n,p,a))-f)/a)}let{r:s,h0:u}=G(n,t,1e-4),r=[];for(let a=0;a<e;a++){let f=[],c=u[a];for(let p=0;p<s;p++){if(p>0&&Math.abs(f[p-1])<1e-20?f.push(0):f.push((o(I(n,a,c))-o(I(n,a,-c)))/(2*c)),Number.isNaN(f[p]))throw new RangeError(`func returned NaN at step ${c} from x`);c/=2}r.push(q(f))}return r}function Tt(o,i,t={}){j(t,Ot,"jacobian");let n=[...i],e=n.length,s=Array.from(o([...n])),u=s.map(()=>new Array(e).fill(NaN)),r=c=>{let p=o(c);if(p.length!==s.length)throw new RangeError(`jacobian: func returned ${p.length} values at a shifted x, but ${s.length} at x`);return p};if(ot(t)==="simple"){let c=rt(t);for(let p=0;p<e;p++){let d=r(I(n,p,c));s.forEach((h,m)=>u[m][p]=(d[m]-h)/c)}return u}let{r:a,h0:f}=G(n,t,1e-4);for(let c=0;c<e;c++){let p=s.map(()=>[]),d=f[c];for(let h=0;h<a;h++){let m=r(I(n,c,d)),g=r(I(n,c,-d));s.forEach((l,b)=>p[b].push((m[b]-g[b])/(2*d))),d/=2}s.forEach((h,m)=>u[m][c]=q(p[m]))}return u}function Ct(o,i,t={}){if(j(t,jt,"hessian"),(t.method??"Richardson")!=="Richardson")throw new RangeError('hessian supports only method "Richardson"');let n=[...i],e=n.length,{r:s,h0:u}=G(n,t,.1),r=o([...n]),a=[];for(let c=0;c<e;c++){let p=[],d=u[c];for(let h=0;h<s;h++){let m=o(I(n,c,d)),g=o(I(n,c,-d)),l=m-2*r+g,b=d**2;!Number.isFinite(l)&&Number.isFinite(m)&&Number.isFinite(g)&&Number.isFinite(r)?p.push((m/d-2*(r/d)+g/d)/d):p.push(Number.isFinite(b)?l/b:l/d/d),d/=2}a.push(q(p))}let f=n.map(()=>new Array(e).fill(NaN));for(let c=0;c<e;c++){f[c][c]=a[c];for(let p=0;p<c;p++){let d=[],h=u[c],m=u[p];for(let g=0;g<s;g++){let l=I(I(n,c,h),p,m),b=I(I(n,c,-h),p,-m),N=o(l),M=o(b),A=N-2*r+M,E=2*h*m;!Number.isFinite(A)&&Number.isFinite(N)&&Number.isFinite(M)&&Number.isFinite(r)?d.push((N/h/m-2*(r/h/m)+M/h/m-a[c]*(h/m)-a[p]*(m/h))/2):Number.isFinite(E)&&Number.isFinite(h**2)&&Number.isFinite(m**2)?d.push((A-a[c]*h**2-a[p]*m**2)/E):d.push((A/h/m-a[c]*(h/m)-a[p]*(m/h))/2),h/=2,m/=2}f[c][p]=f[p][c]=q(d)}}return f}export{gt as approx,kt as barylag,$t as grad,St as hermiteInterp,Ct as hessian,st as integrate,Tt as jacobian,It as ode,ft as optim,at as optimHess,it as optimize,Ft as spline,At as splinefun,xt as splinefunH,ct as uniroot};
|
package/dist/optim/localmin.js
CHANGED
|
@@ -61,6 +61,10 @@ export function localMin(f, lower, upper, tol = LOCALMIN_TOL) {
|
|
|
61
61
|
}
|
|
62
62
|
// f must not be evaluated too close to x
|
|
63
63
|
const u = Math.abs(d) >= tol1 ? x + d : d >= 0 ? x + tol1 : x - tol1; // x + sign(tol1, d)
|
|
64
|
+
// a step that rounds to 0 (tol1 = 0 among the subnormals, for a tol whose third underflows: the bracket stayed
|
|
65
|
+
// [5e-324, 1e-323] for x² on [-1, 1] at tol 5e-324, where R does not return either) cannot narrow the bracket
|
|
66
|
+
if (u === x)
|
|
67
|
+
break;
|
|
64
68
|
const fu = f(u);
|
|
65
69
|
evaluations++;
|
|
66
70
|
// update a, b, v, w and x
|
package/dist/optim/optim.d.ts
CHANGED
|
@@ -95,7 +95,9 @@ export interface OptimResult {
|
|
|
95
95
|
export declare function optimHess(par: readonly number[], fn: (par: number[]) => number, gr?: (par: number[]) => number[], control?: OptimControl): number[][];
|
|
96
96
|
/**
|
|
97
97
|
* General-purpose minimisation of `fn` from the starting point `par`. Equivalent to R's `optim()`. The same methods
|
|
98
|
-
* with the same iterations, so the same results and counts
|
|
98
|
+
* with the same iterations, so the same results and counts, except CG: after an accepted step it tries the minimum of
|
|
99
|
+
* the parabola through the start and the accepted point, where R's cgmin always tries the midpoint (so CG's path,
|
|
100
|
+
* results and counts differ from R's).
|
|
99
101
|
*
|
|
100
102
|
* @example
|
|
101
103
|
* ```ts
|
package/dist/optim/optim.js
CHANGED
|
@@ -5,8 +5,21 @@
|
|
|
5
5
|
// theorems for a class of simulated annealing algorithms on R^d", J. Appl. Probab. 29 (1992) 885-895) with R's random
|
|
6
6
|
// numbers; Brent is optimize(). Written for rStats.js from the specification (no R code was read or used).
|
|
7
7
|
import { optimize } from "../numerics/integrate.js";
|
|
8
|
+
import { ldexp, ldexpWide } from "../internal/summary.js";
|
|
8
9
|
import { normRand, unifRand } from "../random/rng.js";
|
|
9
10
|
import { lbfgsb } from "./lbfgsb.js";
|
|
11
|
+
import { reported } from "../internal/warnings.js";
|
|
12
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
13
|
+
import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
|
|
14
|
+
const OPTIM_KEYS = ["gr", "method", "lower", "upper", "hessian", "control"];
|
|
15
|
+
const OPTIM_CONTROL_KEYS = /* @__PURE__ */ new Set([
|
|
16
|
+
"maxit", "reltol", "abstol", "parscale", "fnscale", "ndeps", "alpha", "beta", "gamma", "type", "lmm", "factr",
|
|
17
|
+
"pgtol", "temp", "tmax",
|
|
18
|
+
]);
|
|
19
|
+
const OPTIM_HESS_KEYS = /* @__PURE__ */ new Set([
|
|
20
|
+
"maxit", "reltol", "abstol", "parscale", "fnscale", "ndeps", "alpha", "beta", "gamma", "type", "lmm", "factr",
|
|
21
|
+
"pgtol", "temp", "tmax",
|
|
22
|
+
]);
|
|
10
23
|
// R's constants (oracle/optim-behaviour.md, rule 5)
|
|
11
24
|
/** Scale at which a step is taken to change nothing */
|
|
12
25
|
const SHIFT = 10;
|
|
@@ -28,6 +41,16 @@ function recycle(v, n, dflt) {
|
|
|
28
41
|
throw new RangeError("argument of length 0");
|
|
29
42
|
return Array.from({ length: n }, (_, i) => a[i % a.length]);
|
|
30
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* R's check in fminfn and (with an analytic gradient) fmingr: every point at which fn or gr is evaluated must be
|
|
46
|
+
* finite. An infinite start trapped Nelder-Mead's simplex set-up, and an infinite gradient BFGS's and CG's line
|
|
47
|
+
* search, in loops that never ended (#354)
|
|
48
|
+
*/
|
|
49
|
+
function finitePar(p) {
|
|
50
|
+
for (const v of p)
|
|
51
|
+
if (!Number.isFinite(v))
|
|
52
|
+
throw new Error("non-finite value supplied by optim");
|
|
53
|
+
}
|
|
31
54
|
/** The scaled problem: p = par / parscale, f(p) = fn(p * parscale) / fnscale */
|
|
32
55
|
class Scaled {
|
|
33
56
|
fn;
|
|
@@ -42,22 +65,39 @@ class Scaled {
|
|
|
42
65
|
this.parscale = parscale;
|
|
43
66
|
this.fnscale = fnscale;
|
|
44
67
|
this.ndeps = ndeps;
|
|
68
|
+
// fn / +-Infinity is 0 or NaN everywhere: R returns the start with value NaN and convergence 0 (#248); NaN and 0
|
|
69
|
+
// fail at the initial value, as in R
|
|
70
|
+
if (fnscale === Infinity || fnscale === -Infinity)
|
|
71
|
+
throw new RangeError("'fnscale' must not be infinite");
|
|
45
72
|
this.n = parscale.length;
|
|
46
73
|
}
|
|
47
74
|
unscale(p) {
|
|
48
75
|
return p.map((v, i) => v * this.parscale[i]);
|
|
49
76
|
}
|
|
50
77
|
f(p) {
|
|
78
|
+
finitePar(p);
|
|
51
79
|
return this.fn(this.unscale(p)) / this.fnscale;
|
|
52
80
|
}
|
|
53
81
|
/** Gradient in the scaled parameters; numerical steps are cut at the bounds when given (L-BFGS-B) */
|
|
54
82
|
g(p, lower, upper) {
|
|
55
83
|
const { n, parscale, fnscale } = this;
|
|
56
84
|
if (this.gr !== undefined) {
|
|
85
|
+
finitePar(p);
|
|
57
86
|
const gx = this.gr(this.unscale(p));
|
|
58
87
|
if (gx.length !== n)
|
|
59
88
|
throw new RangeError(`gradient in optim evaluated to length ${gx.length} not ${n}`);
|
|
60
|
-
|
|
89
|
+
// as the numerical gradient's check: R does not check gr, and a NaN component returned the start as converged
|
|
90
|
+
// (convergence 0) without a step (#355)
|
|
91
|
+
for (let i = 0; i < n; i++) {
|
|
92
|
+
if (!Number.isFinite(gx[i]))
|
|
93
|
+
throw new Error(`non-finite value of the gradient [${i + 1}] supplied by 'gr'`);
|
|
94
|
+
}
|
|
95
|
+
// v·parscale can overflow where the scaled gradient is finite (1e200 · 1e200 / 1e300): only there divide by
|
|
96
|
+
// fnscale first (R's fmingr overflows to NaN; #498)
|
|
97
|
+
return gx.map((v, i) => {
|
|
98
|
+
const w = v * parscale[i];
|
|
99
|
+
return Number.isFinite(w) ? w / fnscale : (v / fnscale) * parscale[i];
|
|
100
|
+
});
|
|
61
101
|
}
|
|
62
102
|
const x = this.unscale(p);
|
|
63
103
|
const slopes = new Array(n);
|
|
@@ -403,6 +443,7 @@ function conjugateGradients(s, start, type, abstol, reltol, maxit) {
|
|
|
403
443
|
dir[i] = dir[i] * beta - grad[i];
|
|
404
444
|
slope += dir[i] * grad[i];
|
|
405
445
|
}
|
|
446
|
+
const atStart = best;
|
|
406
447
|
const search = backtrack(s, from, dir, slope, best, firstStep, point, value);
|
|
407
448
|
fnEvals += search.evals;
|
|
408
449
|
value = search.value;
|
|
@@ -410,8 +451,10 @@ function conjugateGradients(s, start, type, abstol, reltol, maxit) {
|
|
|
410
451
|
const step = search.step;
|
|
411
452
|
if (moved) {
|
|
412
453
|
best = value;
|
|
413
|
-
// minimum of the parabola through the start (value and slope) and the accepted point
|
|
414
|
-
|
|
454
|
+
// minimum of the parabola through the start (value and slope) and the accepted point. (R's cgmin forms it after
|
|
455
|
+
// setting the start's value to the accepted one, so the curvature is -2 slope step and the trial point always
|
|
456
|
+
// the midpoint; issue #15)
|
|
457
|
+
let curvature = 2 * (value - atStart - slope * step);
|
|
415
458
|
if (curvature > 0) {
|
|
416
459
|
curvature = -((slope * step * step) / curvature);
|
|
417
460
|
for (let i = 0; i < n; i++)
|
|
@@ -474,6 +517,7 @@ function annealing(s, start, maxit, perTemperature, startTemperature) {
|
|
|
474
517
|
* @category Numerical methods
|
|
475
518
|
*/
|
|
476
519
|
export function optimHess(par, fn, gr, control = {}) {
|
|
520
|
+
checkOptions(control, OPTIM_HESS_KEYS, "optimHess", "control"); // generated: option-keys.mjs
|
|
477
521
|
const n = par.length;
|
|
478
522
|
const s = new Scaled(fn, gr, recycle(control.parscale, n, 1), control.fnscale ?? 1, recycle(control.ndeps, n, 1e-3));
|
|
479
523
|
return hessianOf(s, par);
|
|
@@ -490,8 +534,14 @@ function hessianOf(s, par) {
|
|
|
490
534
|
p[i] = centre - step;
|
|
491
535
|
const gradDown = s.g(p);
|
|
492
536
|
p[i] = centre;
|
|
493
|
-
for (let j = 0; j < n; j++)
|
|
494
|
-
|
|
537
|
+
for (let j = 0; j < n; j++) {
|
|
538
|
+
const num = fnscale * (gradUp[j] - gradDown[j]);
|
|
539
|
+
const den = 2 * step * parscale[i] * parscale[j];
|
|
540
|
+
const q = num / den;
|
|
541
|
+
// the difference or the product can overflow (or the product underflow) where the quotient is representable:
|
|
542
|
+
// only there form it from factors scaled by powers of 2 (R's optimhess overflows to 0 or Inf; #503)
|
|
543
|
+
hess[i][j] = Number.isFinite(num) && Number.isFinite(den) && Number.isFinite(q) ? q : scaledQuotient(fnscale, gradUp[j], gradDown[j], step, parscale[i], parscale[j], q);
|
|
544
|
+
}
|
|
495
545
|
}
|
|
496
546
|
// symmetrise
|
|
497
547
|
for (let i = 0; i < n; i++) {
|
|
@@ -503,10 +553,25 @@ function hessianOf(s, par) {
|
|
|
503
553
|
}
|
|
504
554
|
return hess;
|
|
505
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* fnscale (up - down) / (2 step ps_i ps_j) with each factor scaled to [1, 2) by a power of 2 and the exponents added
|
|
558
|
+
* back at the end, so that no intermediate overflows or underflows. `direct` (the quotient as written) is kept when a
|
|
559
|
+
* factor is 0 or not finite
|
|
560
|
+
*/
|
|
561
|
+
function scaledQuotient(fnscale, up, down, step, pi, pj, direct) {
|
|
562
|
+
const f = [up / 2 - down / 2, fnscale, step, pi, pj];
|
|
563
|
+
if (f.some((v) => v === 0 || !Number.isFinite(v)))
|
|
564
|
+
return direct;
|
|
565
|
+
const e = f.map((v) => Math.floor(Math.log2(Math.abs(v))));
|
|
566
|
+
const m = f.map((v, k) => ldexp(v, -e[k]));
|
|
567
|
+
return ldexpWide((m[0] * m[1]) / (m[2] * m[3] * m[4]), e[0] + e[1] - e[2] - e[3] - e[4]);
|
|
568
|
+
}
|
|
506
569
|
const METHODS = ["Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"];
|
|
507
570
|
/**
|
|
508
571
|
* General-purpose minimisation of `fn` from the starting point `par`. Equivalent to R's `optim()`. The same methods
|
|
509
|
-
* with the same iterations, so the same results and counts
|
|
572
|
+
* with the same iterations, so the same results and counts, except CG: after an accepted step it tries the minimum of
|
|
573
|
+
* the parabola through the start and the accepted point, where R's cgmin always tries the midpoint (so CG's path,
|
|
574
|
+
* results and counts differ from R's).
|
|
510
575
|
*
|
|
511
576
|
* @example
|
|
512
577
|
* ```ts
|
|
@@ -518,6 +583,8 @@ const METHODS = ["Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"];
|
|
|
518
583
|
* @category Numerical methods
|
|
519
584
|
*/
|
|
520
585
|
export function optim(par, fn, options = {}) {
|
|
586
|
+
checkOptions(options, OPTIM_KEYS, "optim"); // generated: option-keys.mjs
|
|
587
|
+
checkOptions(nestedOptions(options, "control"), OPTIM_CONTROL_KEYS, "optim", "control"); // generated: option-keys.mjs
|
|
521
588
|
const n = par.length;
|
|
522
589
|
let method = options.method ?? "Nelder-Mead";
|
|
523
590
|
if (!METHODS.includes(method))
|
|
@@ -533,9 +600,17 @@ export function optim(par, fn, options = {}) {
|
|
|
533
600
|
const parscale = recycle(control.parscale, n, 1);
|
|
534
601
|
const fnscale = control.fnscale ?? 1;
|
|
535
602
|
const ndeps = recycle(control.ndeps, n, 1e-3);
|
|
536
|
-
const
|
|
603
|
+
const maxit0 = control.maxit ?? (method === "Nelder-Mead" ? 500 : method === "SANN" ? 10000 : 100);
|
|
604
|
+
// R's as.integer: truncated, and an error beyond the integer range (Infinity kept SANN's loop running forever)
|
|
605
|
+
if (!(Math.abs(maxit0) <= 2147483647))
|
|
606
|
+
throw new RangeError("'maxit' is not an integer");
|
|
607
|
+
const maxit = Math.trunc(maxit0);
|
|
537
608
|
const reltol = control.reltol ?? Math.sqrt(2 ** -52);
|
|
538
609
|
const abstol = control.abstol ?? -Infinity;
|
|
610
|
+
if (!(reltol >= 0 && reltol < Infinity))
|
|
611
|
+
throw new RangeError("'reltol' must be a non-negative finite number");
|
|
612
|
+
if (Number.isNaN(abstol))
|
|
613
|
+
throw new RangeError("'abstol' must not be NaN");
|
|
539
614
|
if (method === "Nelder-Mead" && n === 1)
|
|
540
615
|
warnings.push('one-dimensional optimization by Nelder-Mead is unreliable:\nuse "Brent" or optimize() directly');
|
|
541
616
|
if (method === "L-BFGS-B" && (control.reltol !== undefined || control.abstol !== undefined))
|
|
@@ -558,6 +633,11 @@ export function optim(par, fn, options = {}) {
|
|
|
558
633
|
const perTemperature = control.tmax ?? 10;
|
|
559
634
|
if (!(perTemperature >= 1))
|
|
560
635
|
throw new RangeError("'tmax' is not a positive integer");
|
|
636
|
+
// (R fails on 0 with "non-finite value supplied by optim" and accepts a negative one, where the acceptance
|
|
637
|
+
// probability exp(-Δ / T) prefers uphill moves; here 0 returned the start silently)
|
|
638
|
+
if (!(control.temp === undefined || (control.temp > 0 && control.temp < Infinity))) {
|
|
639
|
+
throw new RangeError("'temp' must be a positive finite number");
|
|
640
|
+
}
|
|
561
641
|
run = annealing(s, start, maxit, Math.trunc(perTemperature), control.temp ?? 10);
|
|
562
642
|
break;
|
|
563
643
|
}
|
|
@@ -572,12 +652,19 @@ export function optim(par, fn, options = {}) {
|
|
|
572
652
|
const result = { par: run.par, value: run.value * fnscale, counts: [NaN, NaN], convergence: 0, message: null, warnings };
|
|
573
653
|
if (options.hessian)
|
|
574
654
|
result.hessian = hessianOf(s, result.par);
|
|
575
|
-
return result;
|
|
655
|
+
return reported("optim", result);
|
|
576
656
|
}
|
|
577
657
|
default: {
|
|
578
658
|
// L-BFGS-B
|
|
579
659
|
const lo = lower.map((v, i) => v / parscale[i]);
|
|
580
660
|
const up = upper.map((v, i) => v / parscale[i]);
|
|
661
|
+
// R's as.integer: truncated (1.5 indexed the workspace with fractions, 0.5 ran with m = 0.5); NaN and values
|
|
662
|
+
// beyond the integer range are NA (the latter with R's warning), which lbfgsb rejects as M .LE. 0, like any
|
|
663
|
+
// m <= 0 (passed as 0: a negative m gave negative workspace lengths) (#362)
|
|
664
|
+
const lmm0 = control.lmm ?? 5;
|
|
665
|
+
if (Math.abs(lmm0) > 2147483647)
|
|
666
|
+
warnings.push("NAs introduced by coercion to integer range");
|
|
667
|
+
const lmm = Math.abs(lmm0) <= 2147483647 ? Math.max(Math.trunc(lmm0), 0) : 0;
|
|
581
668
|
if (n === 0) {
|
|
582
669
|
run = { par: [], value: s.f([]), fnEvals: 0, gradEvals: 0, code: 0 };
|
|
583
670
|
message = "NOTHING TO DO";
|
|
@@ -589,7 +676,7 @@ export function optim(par, fn, options = {}) {
|
|
|
589
676
|
return fl && fu ? 2 : fl ? 1 : fu ? 3 : 0;
|
|
590
677
|
});
|
|
591
678
|
let iterations = 0;
|
|
592
|
-
const r = lbfgsb(n,
|
|
679
|
+
const r = lbfgsb(n, lmm, start, lo, up, nbd, control.factr ?? 1e7, control.pgtol ?? 0, (x) => {
|
|
593
680
|
const f = s.f(x);
|
|
594
681
|
if (!Number.isFinite(f))
|
|
595
682
|
throw new Error("L-BFGS-B needs finite values of 'fn'");
|
|
@@ -610,5 +697,5 @@ export function optim(par, fn, options = {}) {
|
|
|
610
697
|
};
|
|
611
698
|
if (options.hessian)
|
|
612
699
|
result.hessian = hessianOf(s, result.par);
|
|
613
|
-
return result;
|
|
700
|
+
return reported("optim", result);
|
|
614
701
|
}
|
package/dist/random/beta.js
CHANGED
|
@@ -6,7 +6,12 @@
|
|
|
6
6
|
// 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 { rchisq } from "./gamma.js";
|
|
9
|
+
import { rchisq, rgamma1 } from "./gamma.js";
|
|
10
|
+
import { rpois1 } from "./poisson.js";
|
|
11
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
12
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
13
|
+
const RBETA_KEYS = ["ncp"];
|
|
14
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
10
15
|
/** The value of log(DBL_MAX) (1024 log 2) */
|
|
11
16
|
const EXPMAX = 1024 * Math.LN2;
|
|
12
17
|
let beta = 0, gamma = 0, delta = 0, k1 = 0, k2 = 0;
|
|
@@ -81,6 +86,22 @@ export function rbeta1(aa, bb) {
|
|
|
81
86
|
beta = Math.sqrt((alpha - 2) / (2 * a * b - alpha));
|
|
82
87
|
gamma = a + 1 / beta;
|
|
83
88
|
}
|
|
89
|
+
if (!(2 * a * b < Infinity)) {
|
|
90
|
+
// 2ab overflows: the constants above are 0, Infinity or NaN. The larger shape B is then above 1e154 / min, and
|
|
91
|
+
// G_B / B = 1 + O(B^-1/2) to far below double precision, so X = G_m / (G_m + B) for the smaller shape m. With m up
|
|
92
|
+
// to 1e32 G_m is drawn (rbeta(5, 1.1, 1e308) gave 1.1e-308 five times, the mean, as R; issue #30: the draws differ
|
|
93
|
+
// from R's there). Beyond, both are so large that the mean is the draw to double precision (a relative standard
|
|
94
|
+
// deviation below 1e-16); the two uniforms of R's single pass are drawn, so the stream stays R's
|
|
95
|
+
const m = Math.min(aa, bb);
|
|
96
|
+
if (m <= 1e32) {
|
|
97
|
+
const big = Math.max(aa, bb);
|
|
98
|
+
const r = rgamma1(m, 1) / big;
|
|
99
|
+
return aa <= bb ? r / (1 + r) : 1 / (1 + r);
|
|
100
|
+
}
|
|
101
|
+
unifRand();
|
|
102
|
+
unifRand();
|
|
103
|
+
return aa >= bb ? 1 / (1 + bb / aa) : aa / bb / (1 + aa / bb);
|
|
104
|
+
}
|
|
84
105
|
let r, t;
|
|
85
106
|
do {
|
|
86
107
|
const u1 = unifRand();
|
|
@@ -97,6 +118,19 @@ export function rbeta1(aa, bb) {
|
|
|
97
118
|
} while (r + alpha * Math.log(alpha / (b + w)) < t);
|
|
98
119
|
return aa !== a ? b / (b + w) : w / (b + w);
|
|
99
120
|
}
|
|
121
|
+
/** Half a noncentral chi-squared variate with 2 shape degrees of freedom (the draws of rnchisq1(2 shape, ncp)) */
|
|
122
|
+
function halfNoncentralChisq(shape, ncp) {
|
|
123
|
+
if (!Number.isFinite(ncp) || ncp < 0)
|
|
124
|
+
return NaN;
|
|
125
|
+
if (ncp === 0)
|
|
126
|
+
return shape === 0 ? 0 : rgamma1(shape, 1);
|
|
127
|
+
let r = rpois1(ncp / 2);
|
|
128
|
+
if (r > 0)
|
|
129
|
+
r = rgamma1(r, 1);
|
|
130
|
+
if (shape > 0)
|
|
131
|
+
r += rgamma1(shape, 1);
|
|
132
|
+
return r;
|
|
133
|
+
}
|
|
100
134
|
/**
|
|
101
135
|
* Random numbers from the beta distribution. Equivalent to R's `rbeta()`.
|
|
102
136
|
*
|
|
@@ -115,11 +149,31 @@ export function rbeta1(aa, bb) {
|
|
|
115
149
|
* @category Random numbers
|
|
116
150
|
*/
|
|
117
151
|
export function rbeta(n, shape1, shape2, options = {}) {
|
|
152
|
+
checkOptions(options, RBETA_KEYS, "rbeta"); // generated: option-keys.mjs
|
|
118
153
|
const { ncp } = options;
|
|
119
154
|
if (ncp === undefined)
|
|
120
155
|
return generate(n, () => rbeta1(shape1, shape2));
|
|
121
156
|
// Noncentral beta: as in R, generate n draws X ~ noncentral χ²(2 shape1, ncp), then n draws Y ~ χ²(2 shape2), and return X / (X + Y)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
157
|
+
let x;
|
|
158
|
+
let y;
|
|
159
|
+
if (Number.isFinite(shape1) && Number.isFinite(shape2) && (!Number.isFinite(2 * shape1) || !Number.isFinite(2 * shape2))) {
|
|
160
|
+
// 2 shape overflows (rbeta(3, 1e308, 1e308, ncp = 0) was NaN, R too): X / 2 and Y / 2 as gamma variates, with the
|
|
161
|
+
// draws of R's rnchisq (rpois(ncp / 2) and a gamma for the Poisson count, then the gamma for the shape)
|
|
162
|
+
x = generate(n, () => halfNoncentralChisq(shape1, ncp));
|
|
163
|
+
y = generate(n, () => rgamma1(shape2, 1));
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
x = rchisq(n, 2 * shape1, { ncp });
|
|
167
|
+
y = rchisq(n, 2 * shape2);
|
|
168
|
+
}
|
|
169
|
+
// With both shapes 0, X = Y = 0 (always for ncp = 0, for a Poisson count 0 otherwise) is the Beta(0, 0) component of
|
|
170
|
+
// the mixture: 0 or 1 with probability 1/2 each, decided by a uniform as the central rbeta does (R returns 0 / 0 =
|
|
171
|
+
// NaN without a draw, so the stream differs from R's only there)
|
|
172
|
+
return x.map((xi, i) => {
|
|
173
|
+
if (xi === 0 && y[i] === 0 && shape1 === 0 && shape2 === 0)
|
|
174
|
+
return unifRand() < 0.5 ? 0 : 1;
|
|
175
|
+
const yi = y[i];
|
|
176
|
+
// (the sum of two variates near 1e308 overflows: halved first)
|
|
177
|
+
return Number.isFinite(xi + yi) || !Number.isFinite(xi) || !Number.isFinite(yi) ? xi / (xi + yi) : xi / 2 / (xi / 2 + yi / 2);
|
|
178
|
+
});
|
|
125
179
|
}
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* log(f(ix) / f(m)) for the binomial probabilities f with n trials and success probability p = 1 - q, m the integer
|
|
3
|
+
* mode, by Stirling's formula (the final test of BTPE). The corrections of log m! and log (n - m)! enter with a plus
|
|
4
|
+
* sign and those of log ix! and log (n - ix)! with a minus sign; R's rbinom.c adds all four (an error of up to about
|
|
5
|
+
* 0.02 in the log near n p = 30), which changes some acceptances and so the stream from R's
|
|
6
|
+
*/
|
|
7
|
+
export declare function btpeLogRatio(n: number, p: number, q: number, m: number, ix: number): number;
|
|
1
8
|
/** One binomial variate (nin trials, success probability pp) */
|
|
2
9
|
export declare function rbinom1(nin: number, pp: number): number;
|
|
3
10
|
/**
|
|
4
11
|
* Random numbers from the binomial distribution. Equivalent to R's `rbinom()`.
|
|
5
12
|
*
|
|
13
|
+
* For `size * min(prob, 1 - prob) >= 30` (algorithm BTPE) the stream can depart from R's: R's final acceptance test has
|
|
14
|
+
* two Stirling corrections with the wrong sign, and here the signs are right (`setSeed(439); rbinom(1, 10000, 0.5)` is
|
|
15
|
+
* 4879, R 5023). Below 30 (inversion) the stream is R's.
|
|
16
|
+
*
|
|
6
17
|
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
7
18
|
* @param size Number of trials.
|
|
8
19
|
* @param prob Probability of success in each trial.
|
|
@@ -24,10 +35,10 @@ export declare function rbinom(n: number, size: number, prob: number): number[];
|
|
|
24
35
|
* with one draw per row (the transpose of R's result). The random stream is the same as R's.
|
|
25
36
|
*
|
|
26
37
|
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
27
|
-
* @param size Number of objects distributed into the K categories in each draw.
|
|
38
|
+
* @param size Number of objects distributed into the K categories in each draw (fractions are truncated, as in R).
|
|
28
39
|
* @param prob Category weights of length K. They need not sum to 1 (they are divided by their sum).
|
|
29
|
-
* @returns An array of `n` arrays, each of length K and summing to `size`.
|
|
30
|
-
* @throws RangeError if `size` is
|
|
40
|
+
* @returns An array of `n` arrays, each of length K and summing to the truncated `size`.
|
|
41
|
+
* @throws RangeError if the truncated `size` is negative or 2^31 or more, or `prob` has a negative or
|
|
31
42
|
* non-finite value, or no positive value.
|
|
32
43
|
*
|
|
33
44
|
* @example
|