@numero-uno-jpn/rstats 0.10.0 → 0.11.1
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 +37 -0
- package/README.ja.md +13 -9
- package/README.md +13 -8
- package/dist/chunks/{chunk-QJJWGNJP.js → chunk-2X7JYQBI.js} +2 -2
- package/dist/chunks/{chunk-O6PT7IVU.js → chunk-36GVER3O.js} +2 -2
- package/dist/chunks/{chunk-EWC6I75B.js → chunk-3WUXRSM6.js} +1 -1
- package/dist/chunks/{chunk-4NM37MGA.js → chunk-6I6QKTFK.js} +2 -2
- package/dist/chunks/{chunk-2XKG3DX4.js → chunk-ETOEZTGL.js} +1 -1
- package/dist/chunks/{chunk-IT354SHV.js → chunk-FVPDVRJQ.js} +2 -2
- package/dist/chunks/{chunk-QULPY6YM.js → chunk-GG4BJMJW.js} +1 -1
- package/dist/chunks/{chunk-7HPVUVZB.js → chunk-HITD7CX6.js} +1 -1
- package/dist/chunks/{chunk-LIMZKONM.js → chunk-KK4GE65D.js} +2 -2
- package/dist/chunks/chunk-LAUYSGV4.js +3 -0
- package/dist/chunks/chunk-M6GDXKKS.js +2 -0
- package/dist/chunks/{chunk-QFULZNHL.js → chunk-PHHUHR7T.js} +1 -1
- package/dist/chunks/{chunk-ENL43FNO.js → chunk-WQPFDDIY.js} +2 -2
- package/dist/chunks/chunk-X2AWQSVS.js +2 -0
- package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-ZRJQD7VS.js} +2 -2
- package/dist/data/grouping.d.ts +100 -0
- package/dist/data/grouping.js +264 -0
- package/dist/descriptive/basic.d.ts +130 -17
- package/dist/descriptive/basic.js +115 -14
- package/dist/descriptive/cov.d.ts +22 -12
- package/dist/descriptive/dataFrames.d.ts +138 -0
- package/dist/descriptive/dataFrames.js +400 -0
- package/dist/descriptive/factanal.d.ts +26 -6
- package/dist/descriptive/factanal.js +7 -3
- package/dist/descriptive/hist.d.ts +27 -6
- package/dist/descriptive/hist.js +9 -3
- package/dist/descriptive/missing.d.ts +42 -0
- package/dist/descriptive/missing.js +52 -0
- package/dist/descriptive/moments.d.ts +91 -13
- package/dist/descriptive/moments.js +66 -9
- package/dist/descriptive/omega.d.ts +28 -6
- package/dist/descriptive/omega.js +16 -4
- package/dist/descriptive/pivot.d.ts +105 -0
- package/dist/descriptive/pivot.js +347 -0
- package/dist/descriptive/quantile.d.ts +33 -5
- package/dist/descriptive/quantile.js +20 -2
- package/dist/descriptive/rank.d.ts +12 -2
- package/dist/descriptive/rank.js +2 -0
- package/dist/descriptive/table.d.ts +55 -7
- package/dist/descriptive/table.js +38 -4
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.d.ts +40 -5
- package/dist/distributions/arcsine.js +35 -4
- package/dist/distributions/beta.d.ts +12 -0
- package/dist/distributions/beta.js +12 -0
- package/dist/distributions/binomial.d.ts +18 -0
- package/dist/distributions/binomial.js +18 -0
- package/dist/distributions/bvnorm.d.ts +4 -1
- package/dist/distributions/bvnorm.js +4 -1
- package/dist/distributions/cauchy.d.ts +24 -0
- package/dist/distributions/cauchy.js +24 -0
- package/dist/distributions/chisq.d.ts +18 -0
- package/dist/distributions/chisq.js +18 -0
- package/dist/distributions/exponential.d.ts +29 -1
- package/dist/distributions/exponential.js +24 -0
- package/dist/distributions/f.d.ts +18 -0
- package/dist/distributions/f.js +18 -0
- package/dist/distributions/gamma.d.ts +20 -0
- package/dist/distributions/gamma.js +18 -0
- package/dist/distributions/geometric.d.ts +24 -0
- package/dist/distributions/geometric.js +24 -0
- package/dist/distributions/hypergeometric.d.ts +24 -0
- package/dist/distributions/hypergeometric.js +24 -0
- package/dist/distributions/invgamma.d.ts +32 -4
- package/dist/distributions/invgamma.js +27 -3
- package/dist/distributions/kumaraswamy.d.ts +31 -3
- package/dist/distributions/kumaraswamy.js +31 -3
- package/dist/distributions/laplace.d.ts +36 -4
- package/dist/distributions/laplace.js +31 -3
- package/dist/distributions/logistic.d.ts +29 -1
- package/dist/distributions/logistic.js +24 -0
- package/dist/distributions/lognormal.d.ts +29 -1
- package/dist/distributions/lognormal.js +24 -0
- package/dist/distributions/multinomial.d.ts +7 -1
- package/dist/distributions/multinomial.js +2 -0
- package/dist/distributions/negbinomial.d.ts +29 -1
- package/dist/distributions/negbinomial.js +24 -0
- package/dist/distributions/normal.d.ts +11 -1
- package/dist/distributions/normal.js +6 -0
- package/dist/distributions/pareto.d.ts +36 -4
- package/dist/distributions/pareto.js +31 -3
- package/dist/distributions/poisson.d.ts +18 -0
- package/dist/distributions/poisson.js +18 -0
- package/dist/distributions/signrank.d.ts +24 -0
- package/dist/distributions/signrank.js +24 -0
- package/dist/distributions/summary.d.ts +20 -2
- package/dist/distributions/summary.js +4 -0
- package/dist/distributions/t.d.ts +18 -0
- package/dist/distributions/t.js +18 -0
- package/dist/distributions/triangular.d.ts +36 -4
- package/dist/distributions/triangular.js +31 -3
- package/dist/distributions/tukey.d.ts +15 -1
- package/dist/distributions/tukey.js +10 -0
- package/dist/distributions/uniform.d.ts +29 -1
- package/dist/distributions/uniform.js +24 -0
- package/dist/distributions/weibull.d.ts +29 -1
- package/dist/distributions/weibull.js +24 -0
- package/dist/distributions/wilcox.d.ts +24 -0
- package/dist/distributions/wilcox.js +24 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +5 -1
- package/dist/entries/descriptive.js +4 -0
- package/dist/entries/numerics.d.ts +1 -1
- package/dist/entries/tests.js +1 -1
- package/dist/htest/agreement.d.ts +61 -23
- package/dist/htest/agreement.js +19 -9
- package/dist/htest/ansariTest.d.ts +19 -1
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/aov.d.ts +43 -10
- package/dist/htest/aov.js +17 -3
- package/dist/htest/binomTest.d.ts +12 -2
- package/dist/htest/binomTest.js +2 -0
- package/dist/htest/brunnerMunzelTest.d.ts +9 -3
- package/dist/htest/brunnerMunzelTest.js +4 -2
- package/dist/htest/chisqTest.d.ts +19 -3
- package/dist/htest/cor.d.ts +31 -3
- package/dist/htest/cor.js +9 -27
- package/dist/htest/effectSize.d.ts +70 -12
- package/dist/htest/effectSize.js +40 -7
- package/dist/htest/fisherTest.d.ts +9 -1
- package/dist/htest/fisherTest.js +2 -0
- package/dist/htest/groups.d.ts +5 -1
- package/dist/htest/ksTest.d.ts +32 -2
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/leveneTest.d.ts +15 -4
- package/dist/htest/leveneTest.js +5 -2
- package/dist/htest/mantelhaenTest.d.ts +25 -3
- package/dist/htest/mantelhaenTest.js +15 -1
- package/dist/htest/mcnemarTest.d.ts +14 -4
- package/dist/htest/mcnemarTest.js +4 -2
- package/dist/htest/multipleComparisons.d.ts +173 -29
- package/dist/htest/multipleComparisons.js +123 -19
- package/dist/htest/normalityTests.d.ts +56 -9
- package/dist/htest/normalityTests.js +40 -6
- package/dist/htest/onewayTest.d.ts +45 -2
- package/dist/htest/onewayTest.js +15 -7
- package/dist/htest/pAdjust.d.ts +9 -1
- package/dist/htest/pAdjust.js +2 -0
- package/dist/htest/pairwise.d.ts +50 -5
- package/dist/htest/pairwise.js +25 -0
- package/dist/htest/poissonTest.d.ts +12 -2
- package/dist/htest/poissonTest.js +2 -0
- package/dist/htest/polycor.d.ts +29 -10
- package/dist/htest/power.d.ts +60 -6
- package/dist/htest/power.js +6 -0
- package/dist/htest/propTest.d.ts +13 -2
- package/dist/htest/propTest.js +2 -0
- package/dist/htest/pwr.d.ts +286 -36
- package/dist/htest/pwr.js +116 -15
- package/dist/htest/rankTests.d.ts +132 -7
- package/dist/htest/rankTests.js +30 -33
- package/dist/htest/tTest.d.ts +32 -2
- package/dist/htest/tTest.js +19 -24
- package/dist/htest/testFormula.d.ts +57 -0
- package/dist/htest/testFormula.js +171 -0
- package/dist/htest/trendTests.d.ts +29 -8
- package/dist/htest/trendTests.js +8 -4
- package/dist/htest/types.d.ts +14 -2
- package/dist/htest/varTest.d.ts +46 -6
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +23 -1
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/internal/dpq.d.ts +4 -0
- package/dist/internal/noncentral.d.ts +2 -0
- package/dist/internal/vectorize.d.ts +4 -0
- package/dist/linalg/basic.d.ts +39 -5
- package/dist/linalg/basic.js +15 -0
- package/dist/linalg/chol.d.ts +26 -5
- package/dist/linalg/chol.js +7 -0
- package/dist/linalg/eigen.d.ts +16 -7
- package/dist/linalg/eigen.js +6 -5
- package/dist/linalg/expm.d.ts +15 -5
- package/dist/linalg/expm.js +5 -3
- package/dist/linalg/kappa.d.ts +21 -1
- package/dist/linalg/kappa.js +16 -0
- package/dist/linalg/lu.d.ts +30 -6
- package/dist/linalg/lu.js +15 -3
- package/dist/linalg/matrix.d.ts +4 -0
- package/dist/linalg/norm.d.ts +16 -1
- package/dist/linalg/norm.js +9 -0
- package/dist/linalg/ops.d.ts +168 -36
- package/dist/linalg/ops.js +116 -20
- package/dist/linalg/qr.d.ts +87 -12
- package/dist/linalg/qr.js +31 -0
- package/dist/linalg/schur.d.ts +8 -2
- package/dist/linalg/schur.js +3 -1
- package/dist/linalg/solve.d.ts +42 -6
- package/dist/linalg/solve.js +22 -0
- package/dist/linalg/svd.d.ts +12 -2
- package/dist/linalg/svd.js +2 -0
- package/dist/linalg.js +2 -2
- package/dist/models/anovaSequential.d.ts +26 -4
- package/dist/models/anovaSequential.js +3 -1
- package/dist/models/aovEz.d.ts +29 -6
- package/dist/models/aovEz.js +5 -2
- package/dist/models/aovStrata.d.ts +12 -2
- package/dist/models/carAnova.d.ts +9 -3
- package/dist/models/carAnova.js +4 -2
- package/dist/models/coxph.d.ts +33 -7
- package/dist/models/coxph.js +8 -3
- package/dist/models/glm.d.ts +128 -21
- package/dist/models/glm.js +71 -14
- package/dist/models/glmAnova.d.ts +30 -5
- package/dist/models/glmAnova.js +15 -2
- package/dist/models/glmConfint.d.ts +16 -2
- package/dist/models/glmConfint.js +9 -1
- package/dist/models/glmFamily.d.ts +27 -5
- package/dist/models/lm.d.ts +75 -17
- package/dist/models/lm.js +22 -4
- package/dist/models/mauchly.d.ts +20 -5
- package/dist/models/mauchly.js +7 -3
- package/dist/models/modelFrame.d.ts +5 -1
- package/dist/models/modelMatrix.d.ts +18 -4
- package/dist/models/modelMatrix.js +3 -1
- package/dist/models/survival.d.ts +41 -9
- package/dist/models/survival.js +10 -4
- package/dist/numerics/deriv.d.ts +21 -6
- package/dist/numerics/deriv.js +16 -5
- package/dist/numerics/integrate.d.ts +37 -7
- package/dist/numerics/integrate.js +8 -2
- package/dist/numerics/interp.d.ts +63 -12
- package/dist/numerics/interp.js +30 -6
- package/dist/numerics/ode.d.ts +15 -5
- package/dist/numerics/ode.js +5 -3
- package/dist/numerics.js +2 -2
- package/dist/optim/optim.d.ts +32 -8
- package/dist/optim/optim.js +8 -4
- package/dist/random/beta.d.ts +8 -0
- package/dist/random/beta.js +8 -0
- package/dist/random/binomial.d.ts +16 -0
- package/dist/random/binomial.js +16 -0
- package/dist/random/continuous.d.ts +51 -0
- package/dist/random/continuous.js +51 -0
- package/dist/random/extra.d.ts +66 -6
- package/dist/random/extra.js +66 -6
- package/dist/random/gamma.d.ts +33 -0
- package/dist/random/gamma.js +33 -0
- package/dist/random/hyper.d.ts +8 -0
- package/dist/random/hyper.js +8 -0
- package/dist/random/poisson.d.ts +25 -0
- package/dist/random/poisson.js +25 -0
- package/dist/random/r2dtable.d.ts +2 -0
- package/dist/random/r2dtable.js +2 -0
- package/dist/random/rank.d.ts +16 -0
- package/dist/random/rank.js +16 -0
- package/dist/random/rng.d.ts +49 -6
- package/dist/random/rng.js +29 -2
- package/dist/random/sample.d.ts +10 -2
- package/dist/random/sample.js +5 -1
- package/dist/random/streams.d.ts +13 -0
- package/dist/random/streams.js +13 -0
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +11 -3
- package/dist/special/besselIK.js +6 -2
- package/dist/special/besselJY.d.ts +6 -2
- package/dist/special/besselJY.js +6 -2
- package/dist/special/beta.d.ts +16 -2
- package/dist/special/beta.js +16 -2
- package/dist/special/choose.d.ts +13 -5
- package/dist/special/choose.js +13 -5
- package/dist/special/erf.d.ts +8 -3
- package/dist/special/erf.js +8 -3
- package/dist/special/extra.d.ts +30 -10
- package/dist/special/extra.js +25 -9
- package/dist/special/gamma.d.ts +18 -2
- package/dist/special/gamma.js +18 -2
- package/dist/special/log1p.d.ts +16 -7
- package/dist/special/log1p.js +16 -7
- package/dist/special/psigamma.d.ts +9 -3
- package/dist/special/psigamma.js +9 -3
- package/dist/special/rCompatible.d.ts +5 -1
- package/dist/special.js +2 -2
- package/dist/tests.js +12 -13
- package/llms.txt +14 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-AECGWZHD.js +0 -2
- package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
package/dist/numerics/interp.js
CHANGED
|
@@ -62,6 +62,8 @@ function stepAt(Y, i, f) {
|
|
|
62
62
|
* ```ts
|
|
63
63
|
* approx([1, 2, 3], [10, 20, 40], { xout: [1.5, 2.5] }).y; // [15, 30]
|
|
64
64
|
* ```
|
|
65
|
+
*
|
|
66
|
+
* @category Numerical methods
|
|
65
67
|
*/
|
|
66
68
|
export function approx(x, y, options = {}) {
|
|
67
69
|
const { method = "linear", n = 50, f = 0, rule = 1, ties = "mean" } = options;
|
|
@@ -370,6 +372,8 @@ function evalCubic(sp, xq, deriv0) {
|
|
|
370
372
|
* ```ts
|
|
371
373
|
* spline([1, 2, 3, 4], [1, 4, 9, 16], { xout: [2.5] }).y; // [6.25]
|
|
372
374
|
* ```
|
|
375
|
+
*
|
|
376
|
+
* @category Numerical methods
|
|
373
377
|
*/
|
|
374
378
|
export function spline(x, y, options = {}) {
|
|
375
379
|
const { method = "fmm", ties = "mean" } = options;
|
|
@@ -389,7 +393,7 @@ export function spline(x, y, options = {}) {
|
|
|
389
393
|
}
|
|
390
394
|
/**
|
|
391
395
|
* The cubic spline through the points, as a function of `x` and the order of derivative (0 to 3; a fraction is
|
|
392
|
-
* truncated, as R does). Equivalent to R's `splinefun()
|
|
396
|
+
* truncated, as R does). Equivalent to R's `splinefun()`. Except that the third derivative of `"monoH.FC"` is the
|
|
393
397
|
* exact one (see {@link splinefunH}).
|
|
394
398
|
*
|
|
395
399
|
* @example
|
|
@@ -398,6 +402,8 @@ export function spline(x, y, options = {}) {
|
|
|
398
402
|
* s([2.5]); // [6.25]
|
|
399
403
|
* s([2.5], 1); // [5]
|
|
400
404
|
* ```
|
|
405
|
+
*
|
|
406
|
+
* @category Numerical methods
|
|
401
407
|
*/
|
|
402
408
|
export function splinefun(x, y, options = {}) {
|
|
403
409
|
const { method = "fmm", ties = "mean" } = options;
|
|
@@ -484,14 +490,16 @@ function evalHermite(x, y, m, xq, deriv0) {
|
|
|
484
490
|
}
|
|
485
491
|
/**
|
|
486
492
|
* The cubic Hermite spline with values `y` and slopes `m` at the points `x`, as a function of `x` and the order of
|
|
487
|
-
* derivative (0 to 3; a fraction is truncated, as R does); linear outside the data. Equivalent to R's `splinefunH()
|
|
488
|
-
*
|
|
493
|
+
* derivative (0 to 3; a fraction is truncated, as R does); linear outside the data. Equivalent to R's `splinefunH()`.
|
|
494
|
+
* Except for the third derivative: on [x_i, x_{i+1}] it is the exact (6 m_i + 6 m_{i+1} - 12 delta) / h^2, with
|
|
489
495
|
* h = x_{i+1} - x_i and delta = (y_{i+1} - y_i) / h, where R divides by h only once (the two agree when h = 1).
|
|
490
496
|
*
|
|
491
497
|
* @example
|
|
492
498
|
* ```ts
|
|
493
499
|
* splinefunH([0, 2], [0, 0], [1, 1])([1], 3); // [3] (R: 6)
|
|
494
500
|
* ```
|
|
501
|
+
*
|
|
502
|
+
* @category Numerical methods
|
|
495
503
|
*/
|
|
496
504
|
export function splinefunH(x, y, m) {
|
|
497
505
|
const n = x.length;
|
|
@@ -507,8 +515,15 @@ export function splinefunH(x, y, m) {
|
|
|
507
515
|
return (xq, deriv = 0) => evalHermite(X, Y, M, xq, deriv);
|
|
508
516
|
}
|
|
509
517
|
/**
|
|
510
|
-
* Lagrange interpolation in barycentric form through the points (xi, yi). Equivalent to `pracma::barylag()
|
|
511
|
-
* points must lie within the range of the nodes.
|
|
518
|
+
* Lagrange interpolation in barycentric form through the points (xi, yi). Equivalent to `pracma::barylag()` in R.
|
|
519
|
+
* The points must lie within the range of the nodes.
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```ts
|
|
523
|
+
* barylag([0, 1, 2], [1, 3, 7], [0.5, 1.5]); // [1.75, 4.75]
|
|
524
|
+
* ```
|
|
525
|
+
*
|
|
526
|
+
* @category Numerical methods
|
|
512
527
|
*/
|
|
513
528
|
export function barylag(xi, yi, x) {
|
|
514
529
|
const X = Array.from(xi);
|
|
@@ -546,7 +561,16 @@ export function barylag(xi, yi, x) {
|
|
|
546
561
|
}
|
|
547
562
|
/**
|
|
548
563
|
* The polynomial of degree 2n - 1 through n points with given values and derivatives (global Hermite
|
|
549
|
-
* interpolation, jStat's hermite), evaluated at `x`.
|
|
564
|
+
* interpolation, jStat's hermite), evaluated at `x`. Not an R function. Neither base R nor the recommended packages
|
|
565
|
+
* have it (for piecewise cubic Hermite interpolation, see {@link splinefunH}). Computed by divided differences on
|
|
566
|
+
* the doubled nodes.
|
|
567
|
+
*
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* hermiteInterp([0, 1], [0, 1], [0, 0], [0.25, 0.5]); // [0.15625, 0.5]
|
|
571
|
+
* ```
|
|
572
|
+
*
|
|
573
|
+
* @category Numerical methods
|
|
550
574
|
*/
|
|
551
575
|
export function hermiteInterp(xi, yi, di, x) {
|
|
552
576
|
const X = Array.from(xi);
|
package/dist/numerics/ode.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Derivative of the state: `func(t, y, parms)` returns dy/dt (one value per state variable).
|
|
3
|
+
*
|
|
4
|
+
* @category Numerical methods
|
|
5
|
+
*/
|
|
2
6
|
export type OdeFunc = (t: number, y: number[], parms: number[]) => readonly number[];
|
|
3
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Options for {@link ode}.
|
|
9
|
+
*
|
|
10
|
+
* @category Numerical methods
|
|
11
|
+
*/
|
|
4
12
|
export interface OdeOptions {
|
|
5
13
|
/** `"euler"`, `"rk2"` (Heun) or `"rk4"` (classical Runge-Kutta, default). */
|
|
6
14
|
method?: "euler" | "rk2" | "rk4";
|
|
7
15
|
}
|
|
8
16
|
/**
|
|
9
17
|
* Solve the initial value problem y' = func(t, y, parms), y(times[0]) = y, with one fixed step per interval of
|
|
10
|
-
* `times`. Equivalent to `deSolve::ode(y, times, func, parms, method)` for `"euler"` and `"rk4"`; for
|
|
11
|
-
* `deSolve::ode(..., method = rkMethod("rk2"))` (or `deSolve::rk(..., method = "rk2")`), since deSolve's `ode()`
|
|
18
|
+
* `times`. Equivalent to `deSolve::ode(y, times, func, parms, method)` in R. That is for `"euler"` and `"rk4"`; for
|
|
19
|
+
* `"rk2"`, it is `deSolve::ode(..., method = rkMethod("rk2"))` (or `deSolve::rk(..., method = "rk2")`), since deSolve's `ode()`
|
|
12
20
|
* does not accept `method = "rk2"`.
|
|
13
21
|
*
|
|
14
22
|
* @returns One row per time: `[t, y1, y2, ...]`.
|
|
@@ -17,7 +25,9 @@ export interface OdeOptions {
|
|
|
17
25
|
* ```ts
|
|
18
26
|
* // exponential decay y' = -0.7 y
|
|
19
27
|
* const out = ode([1], [0, 0.5, 1], (t, y, p) => [-p[0]! * y[0]!], [0.7], { method: "rk4" });
|
|
20
|
-
* out[2]; // [1, 0.
|
|
28
|
+
* out[2]; // [1, 0.49664] (exp(-0.7) = 0.49659)
|
|
21
29
|
* ```
|
|
30
|
+
*
|
|
31
|
+
* @category Numerical methods
|
|
22
32
|
*/
|
|
23
33
|
export declare function ode(y: readonly number[], times: readonly number[], func: OdeFunc, parms?: readonly number[], options?: OdeOptions): number[][];
|
package/dist/numerics/ode.js
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
// the start and at the Euler predictor, averaged; rk4 is the classical fourth-order Runge-Kutta method.
|
|
7
7
|
/**
|
|
8
8
|
* Solve the initial value problem y' = func(t, y, parms), y(times[0]) = y, with one fixed step per interval of
|
|
9
|
-
* `times`. Equivalent to `deSolve::ode(y, times, func, parms, method)` for `"euler"` and `"rk4"`; for
|
|
10
|
-
* `deSolve::ode(..., method = rkMethod("rk2"))` (or `deSolve::rk(..., method = "rk2")`), since deSolve's `ode()`
|
|
9
|
+
* `times`. Equivalent to `deSolve::ode(y, times, func, parms, method)` in R. That is for `"euler"` and `"rk4"`; for
|
|
10
|
+
* `"rk2"`, it is `deSolve::ode(..., method = rkMethod("rk2"))` (or `deSolve::rk(..., method = "rk2")`), since deSolve's `ode()`
|
|
11
11
|
* does not accept `method = "rk2"`.
|
|
12
12
|
*
|
|
13
13
|
* @returns One row per time: `[t, y1, y2, ...]`.
|
|
@@ -16,8 +16,10 @@
|
|
|
16
16
|
* ```ts
|
|
17
17
|
* // exponential decay y' = -0.7 y
|
|
18
18
|
* const out = ode([1], [0, 0.5, 1], (t, y, p) => [-p[0]! * y[0]!], [0.7], { method: "rk4" });
|
|
19
|
-
* out[2]; // [1, 0.
|
|
19
|
+
* out[2]; // [1, 0.49664] (exp(-0.7) = 0.49659)
|
|
20
20
|
* ```
|
|
21
|
+
*
|
|
22
|
+
* @category Numerical methods
|
|
21
23
|
*/
|
|
22
24
|
export function ode(y, times, func, parms = [], options = {}) {
|
|
23
25
|
const method = options.method ?? "rk4";
|
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-IT354SHV.js";import"./chunks/chunk-QULPY6YM.js";import{a as x,f as j}from"./chunks/chunk-QFULZNHL.js";import"./chunks/chunk-7HPVUVZB.js";import{b as S}from"./chunks/chunk-LIMZKONM.js";import"./chunks/chunk-2XKG3DX4.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");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=x(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=x(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 k(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(k(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]=k(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(k(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]=k(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.11.1 | 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 $,b as T,c as D,d as F,e as O}from"./chunks/chunk-FVPDVRJQ.js";import"./chunks/chunk-GG4BJMJW.js";import{a as x,f as j}from"./chunks/chunk-PHHUHR7T.js";import"./chunks/chunk-HITD7CX6.js";import{b as S}from"./chunks/chunk-KK4GE65D.js";import"./chunks/chunk-ETOEZTGL.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");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=x(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=x(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 k(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(k(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]=k(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(k(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]=k(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};
|
package/dist/optim/optim.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Methods of {@link optim}
|
|
3
|
+
*
|
|
4
|
+
* @category Numerical methods
|
|
5
|
+
*/
|
|
2
6
|
export type OptimMethod = "Nelder-Mead" | "BFGS" | "CG" | "L-BFGS-B" | "SANN" | "Brent";
|
|
3
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Control parameters of {@link optim} (R's `control`, in camelCase)
|
|
9
|
+
*
|
|
10
|
+
* @category Numerical methods
|
|
11
|
+
*/
|
|
4
12
|
export interface OptimControl {
|
|
5
13
|
/** Maximum number of iterations. Default 500 for Nelder-Mead, 10000 for SANN, 100 otherwise. */
|
|
6
14
|
maxit?: number;
|
|
@@ -33,7 +41,11 @@ export interface OptimControl {
|
|
|
33
41
|
/** SANN: evaluations per temperature. Default 10. */
|
|
34
42
|
tmax?: number;
|
|
35
43
|
}
|
|
36
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* Options of {@link optim}
|
|
46
|
+
*
|
|
47
|
+
* @category Numerical methods
|
|
48
|
+
*/
|
|
37
49
|
export interface OptimOptions {
|
|
38
50
|
/** Gradient of fn (numerical by central differences when omitted) */
|
|
39
51
|
gr?: (par: number[]) => number[];
|
|
@@ -45,11 +57,18 @@ export interface OptimOptions {
|
|
|
45
57
|
upper?: number | readonly number[];
|
|
46
58
|
/** Also return the numerically differentiated Hessian at the solution. Default `false`. */
|
|
47
59
|
hessian?: boolean;
|
|
60
|
+
/** Control parameters (R's `control`). See {@link OptimControl}. */
|
|
48
61
|
control?: OptimControl;
|
|
49
62
|
}
|
|
50
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* Result of {@link optim}
|
|
65
|
+
*
|
|
66
|
+
* @category Numerical methods
|
|
67
|
+
*/
|
|
51
68
|
export interface OptimResult {
|
|
69
|
+
/** The best parameters found. */
|
|
52
70
|
par: number[];
|
|
71
|
+
/** fn at `par`. */
|
|
53
72
|
value: number;
|
|
54
73
|
/** Evaluations of fn and gr (NaN where R has NA) */
|
|
55
74
|
counts: [number, number];
|
|
@@ -57,29 +76,34 @@ export interface OptimResult {
|
|
|
57
76
|
convergence: number;
|
|
58
77
|
/** L-BFGS-B's final message; null for the other methods */
|
|
59
78
|
message: string | null;
|
|
79
|
+
/** The numerically differentiated Hessian at `par`, when `hessian` is true (array of rows). */
|
|
60
80
|
hessian?: number[][];
|
|
61
81
|
/** R's warnings */
|
|
62
82
|
warnings: string[];
|
|
63
83
|
}
|
|
64
84
|
/**
|
|
65
85
|
* Numerically differentiated Hessian of fn at par, from central differences of the gradient (gr, or the numerical
|
|
66
|
-
* gradient of fn). Equivalent to R's `optimHess()
|
|
86
|
+
* gradient of fn). Equivalent to R's `optimHess()`. `control` uses `fnscale`, `parscale` and `ndeps`.
|
|
67
87
|
*
|
|
68
88
|
* @example
|
|
69
89
|
* ```ts
|
|
70
|
-
* optimHess([1, 2], (x) => x[0] ** 2 + 3 * x[0] * x[1] + x[1] ** 2); //
|
|
90
|
+
* optimHess([1, 2], (x) => x[0] ** 2 + 3 * x[0] * x[1] + x[1] ** 2); // [[2, 3], [3, 2]]
|
|
71
91
|
* ```
|
|
92
|
+
*
|
|
93
|
+
* @category Numerical methods
|
|
72
94
|
*/
|
|
73
95
|
export declare function optimHess(par: readonly number[], fn: (par: number[]) => number, gr?: (par: number[]) => number[], control?: OptimControl): number[][];
|
|
74
96
|
/**
|
|
75
|
-
* General-purpose minimisation of `fn` from the starting point `par`. Equivalent to R's `optim()
|
|
97
|
+
* General-purpose minimisation of `fn` from the starting point `par`. Equivalent to R's `optim()`. The same methods
|
|
76
98
|
* with the same iterations, so the same results and counts.
|
|
77
99
|
*
|
|
78
100
|
* @example
|
|
79
101
|
* ```ts
|
|
80
102
|
* const rosen = (x: number[]) => 100 * (x[1] - x[0] ** 2) ** 2 + (1 - x[0]) ** 2;
|
|
81
103
|
* optim([-1.2, 1], rosen).par; // [1.0002601387256695, 1.000505999303765]
|
|
82
|
-
* optim([-1.2, 1], rosen, { method: "BFGS" }).counts; // [
|
|
104
|
+
* optim([-1.2, 1], rosen, { method: "BFGS" }).counts; // [118, 38]
|
|
83
105
|
* ```
|
|
106
|
+
*
|
|
107
|
+
* @category Numerical methods
|
|
84
108
|
*/
|
|
85
109
|
export declare function optim(par: readonly number[], fn: (par: number[]) => number, options?: OptimOptions): OptimResult;
|
package/dist/optim/optim.js
CHANGED
|
@@ -464,12 +464,14 @@ function annealing(s, start, maxit, perTemperature, startTemperature) {
|
|
|
464
464
|
}
|
|
465
465
|
/**
|
|
466
466
|
* Numerically differentiated Hessian of fn at par, from central differences of the gradient (gr, or the numerical
|
|
467
|
-
* gradient of fn). Equivalent to R's `optimHess()
|
|
467
|
+
* gradient of fn). Equivalent to R's `optimHess()`. `control` uses `fnscale`, `parscale` and `ndeps`.
|
|
468
468
|
*
|
|
469
469
|
* @example
|
|
470
470
|
* ```ts
|
|
471
|
-
* optimHess([1, 2], (x) => x[0] ** 2 + 3 * x[0] * x[1] + x[1] ** 2); //
|
|
471
|
+
* optimHess([1, 2], (x) => x[0] ** 2 + 3 * x[0] * x[1] + x[1] ** 2); // [[2, 3], [3, 2]]
|
|
472
472
|
* ```
|
|
473
|
+
*
|
|
474
|
+
* @category Numerical methods
|
|
473
475
|
*/
|
|
474
476
|
export function optimHess(par, fn, gr, control = {}) {
|
|
475
477
|
const n = par.length;
|
|
@@ -503,15 +505,17 @@ function hessianOf(s, par) {
|
|
|
503
505
|
}
|
|
504
506
|
const METHODS = ["Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"];
|
|
505
507
|
/**
|
|
506
|
-
* General-purpose minimisation of `fn` from the starting point `par`. Equivalent to R's `optim()
|
|
508
|
+
* General-purpose minimisation of `fn` from the starting point `par`. Equivalent to R's `optim()`. The same methods
|
|
507
509
|
* with the same iterations, so the same results and counts.
|
|
508
510
|
*
|
|
509
511
|
* @example
|
|
510
512
|
* ```ts
|
|
511
513
|
* const rosen = (x: number[]) => 100 * (x[1] - x[0] ** 2) ** 2 + (1 - x[0]) ** 2;
|
|
512
514
|
* optim([-1.2, 1], rosen).par; // [1.0002601387256695, 1.000505999303765]
|
|
513
|
-
* optim([-1.2, 1], rosen, { method: "BFGS" }).counts; // [
|
|
515
|
+
* optim([-1.2, 1], rosen, { method: "BFGS" }).counts; // [118, 38]
|
|
514
516
|
* ```
|
|
517
|
+
*
|
|
518
|
+
* @category Numerical methods
|
|
515
519
|
*/
|
|
516
520
|
export function optim(par, fn, options = {}) {
|
|
517
521
|
const n = par.length;
|
package/dist/random/beta.d.ts
CHANGED
|
@@ -8,6 +8,14 @@ export declare function rbeta1(aa: number, bb: number): number;
|
|
|
8
8
|
* @param shape2 Second shape parameter.
|
|
9
9
|
* @param options.ncp Non-centrality parameter. If omitted, the central distribution is used.
|
|
10
10
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* setSeed(1);
|
|
15
|
+
* rbeta(2, 2, 3); // [0.2550998, 0.4468707]
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @category Random numbers
|
|
11
19
|
*/
|
|
12
20
|
export declare function rbeta(n: number, shape1: number, shape2: number, options?: {
|
|
13
21
|
ncp?: number;
|
package/dist/random/beta.js
CHANGED
|
@@ -105,6 +105,14 @@ export function rbeta1(aa, bb) {
|
|
|
105
105
|
* @param shape2 Second shape parameter.
|
|
106
106
|
* @param options.ncp Non-centrality parameter. If omitted, the central distribution is used.
|
|
107
107
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* setSeed(1);
|
|
112
|
+
* rbeta(2, 2, 3); // [0.2550998, 0.4468707]
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @category Random numbers
|
|
108
116
|
*/
|
|
109
117
|
export function rbeta(n, shape1, shape2, options = {}) {
|
|
110
118
|
const { ncp } = options;
|
|
@@ -7,6 +7,14 @@ export declare function rbinom1(nin: number, pp: number): number;
|
|
|
7
7
|
* @param size Number of trials.
|
|
8
8
|
* @param prob Probability of success in each trial.
|
|
9
9
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* setSeed(1);
|
|
14
|
+
* rbinom(5, 10, 0.5); // [4, 4, 5, 7, 4]
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @category Random numbers
|
|
10
18
|
*/
|
|
11
19
|
export declare function rbinom(n: number, size: number, prob: number): number[];
|
|
12
20
|
/**
|
|
@@ -21,5 +29,13 @@ export declare function rbinom(n: number, size: number, prob: number): number[];
|
|
|
21
29
|
* @returns An array of `n` arrays, each of length K and summing to `size`.
|
|
22
30
|
* @throws RangeError if `size` is not a non-negative integer, or `prob` has a negative or
|
|
23
31
|
* non-finite value, or no positive value.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* setSeed(1);
|
|
36
|
+
* rmultinom(2, 5, [0.2, 0.3, 0.5]); // [[0, 1, 4], [1, 3, 1]]
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @category Random numbers
|
|
24
40
|
*/
|
|
25
41
|
export declare function rmultinom(n: number, size: number, prob: readonly number[]): number[][];
|
package/dist/random/binomial.js
CHANGED
|
@@ -171,6 +171,14 @@ export function rbinom1(nin, pp) {
|
|
|
171
171
|
* @param size Number of trials.
|
|
172
172
|
* @param prob Probability of success in each trial.
|
|
173
173
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* setSeed(1);
|
|
178
|
+
* rbinom(5, 10, 0.5); // [4, 4, 5, 7, 4]
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @category Random numbers
|
|
174
182
|
*/
|
|
175
183
|
export function rbinom(n, size, prob) {
|
|
176
184
|
return generate(n, () => rbinom1(size, prob));
|
|
@@ -187,6 +195,14 @@ export function rbinom(n, size, prob) {
|
|
|
187
195
|
* @returns An array of `n` arrays, each of length K and summing to `size`.
|
|
188
196
|
* @throws RangeError if `size` is not a non-negative integer, or `prob` has a negative or
|
|
189
197
|
* non-finite value, or no positive value.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* setSeed(1);
|
|
202
|
+
* rmultinom(2, 5, [0.2, 0.3, 0.5]); // [[0, 1, 4], [1, 3, 1]]
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @category Random numbers
|
|
190
206
|
*/
|
|
191
207
|
export function rmultinom(n, size, prob) {
|
|
192
208
|
if (!Number.isFinite(size) || size < 0 || Math.round(size) !== size)
|
|
@@ -5,6 +5,15 @@
|
|
|
5
5
|
* @param options.min Lower limit. Default `0`.
|
|
6
6
|
* @param options.max Upper limit. Default `1`.
|
|
7
7
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* setSeed(1);
|
|
12
|
+
* runif(3); // [0.2655087, 0.3721239, 0.5728534]
|
|
13
|
+
* runif(2, { min: -1, max: 1 });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @category Random numbers
|
|
8
17
|
*/
|
|
9
18
|
export declare function runif(n: number, options?: {
|
|
10
19
|
min?: number;
|
|
@@ -27,6 +36,8 @@ export declare function rnorm1(mean: number, sd: number): number;
|
|
|
27
36
|
* rnorm(3); // [-0.6264538107423324, 0.18364332422208224, -0.8356286124100472]
|
|
28
37
|
* rnorm(2, { mean: 10, sd: 2 });
|
|
29
38
|
* ```
|
|
39
|
+
*
|
|
40
|
+
* @category Random numbers
|
|
30
41
|
*/
|
|
31
42
|
export declare function rnorm(n: number, options?: {
|
|
32
43
|
mean?: number;
|
|
@@ -38,6 +49,14 @@ export declare function rnorm(n: number, options?: {
|
|
|
38
49
|
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
39
50
|
* @param options.rate Rate. Default `1`.
|
|
40
51
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* setSeed(1);
|
|
56
|
+
* rexp(2, { rate: 2 }); // [0.3775909, 0.5908214]
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @category Random numbers
|
|
41
60
|
*/
|
|
42
61
|
export declare function rexp(n: number, options?: {
|
|
43
62
|
rate?: number;
|
|
@@ -49,6 +68,14 @@ export declare function rexp(n: number, options?: {
|
|
|
49
68
|
* @param options.meanlog Mean on the log scale. Default `0`.
|
|
50
69
|
* @param options.sdlog Standard deviation on the log scale. Default `1`.
|
|
51
70
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* setSeed(1);
|
|
75
|
+
* rlnorm(2, { meanlog: 0, sdlog: 1 }); // [0.5344838, 1.201587]
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @category Random numbers
|
|
52
79
|
*/
|
|
53
80
|
export declare function rlnorm(n: number, options?: {
|
|
54
81
|
meanlog?: number;
|
|
@@ -61,6 +88,14 @@ export declare function rlnorm(n: number, options?: {
|
|
|
61
88
|
* @param options.location Location. Default `0`.
|
|
62
89
|
* @param options.scale Scale. Default `1`.
|
|
63
90
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* setSeed(1);
|
|
95
|
+
* rlogis(2, { location: 0, scale: 1 }); // [-1.017531, -0.523116]
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @category Random numbers
|
|
64
99
|
*/
|
|
65
100
|
export declare function rlogis(n: number, options?: {
|
|
66
101
|
location?: number;
|
|
@@ -73,6 +108,14 @@ export declare function rlogis(n: number, options?: {
|
|
|
73
108
|
* @param options.location Location. Default `0`.
|
|
74
109
|
* @param options.scale Scale. Default `1`.
|
|
75
110
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* setSeed(1);
|
|
115
|
+
* rcauchy(2, { location: 0, scale: 1 }); // [1.10252, 2.353831]
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @category Random numbers
|
|
76
119
|
*/
|
|
77
120
|
export declare function rcauchy(n: number, options?: {
|
|
78
121
|
location?: number;
|
|
@@ -85,6 +128,14 @@ export declare function rcauchy(n: number, options?: {
|
|
|
85
128
|
* @param shape Shape parameter (required, as in R).
|
|
86
129
|
* @param options.scale Scale. Default `1`.
|
|
87
130
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* setSeed(1);
|
|
135
|
+
* rweibull(2, 2, { scale: 1 }); // [1.151568, 0.9942477]
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* @category Random numbers
|
|
88
139
|
*/
|
|
89
140
|
export declare function rweibull(n: number, shape: number, options?: {
|
|
90
141
|
scale?: number;
|
|
@@ -11,6 +11,15 @@ import { generate } from "./common.js";
|
|
|
11
11
|
* @param options.min Lower limit. Default `0`.
|
|
12
12
|
* @param options.max Upper limit. Default `1`.
|
|
13
13
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* setSeed(1);
|
|
18
|
+
* runif(3); // [0.2655087, 0.3721239, 0.5728534]
|
|
19
|
+
* runif(2, { min: -1, max: 1 });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @category Random numbers
|
|
14
23
|
*/
|
|
15
24
|
export function runif(n, options = {}) {
|
|
16
25
|
const { min = 0, max = 1 } = options;
|
|
@@ -46,6 +55,8 @@ export function rnorm1(mean, sd) {
|
|
|
46
55
|
* rnorm(3); // [-0.6264538107423324, 0.18364332422208224, -0.8356286124100472]
|
|
47
56
|
* rnorm(2, { mean: 10, sd: 2 });
|
|
48
57
|
* ```
|
|
58
|
+
*
|
|
59
|
+
* @category Random numbers
|
|
49
60
|
*/
|
|
50
61
|
export function rnorm(n, options = {}) {
|
|
51
62
|
const { mean = 0, sd = 1 } = options;
|
|
@@ -57,6 +68,14 @@ export function rnorm(n, options = {}) {
|
|
|
57
68
|
* @param n Number of values to generate (fractions are truncated, as in R).
|
|
58
69
|
* @param options.rate Rate. Default `1`.
|
|
59
70
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* setSeed(1);
|
|
75
|
+
* rexp(2, { rate: 2 }); // [0.3775909, 0.5908214]
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @category Random numbers
|
|
60
79
|
*/
|
|
61
80
|
export function rexp(n, options = {}) {
|
|
62
81
|
const scale = 1 / (options.rate ?? 1);
|
|
@@ -73,6 +92,14 @@ export function rexp(n, options = {}) {
|
|
|
73
92
|
* @param options.meanlog Mean on the log scale. Default `0`.
|
|
74
93
|
* @param options.sdlog Standard deviation on the log scale. Default `1`.
|
|
75
94
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* setSeed(1);
|
|
99
|
+
* rlnorm(2, { meanlog: 0, sdlog: 1 }); // [0.5344838, 1.201587]
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @category Random numbers
|
|
76
103
|
*/
|
|
77
104
|
export function rlnorm(n, options = {}) {
|
|
78
105
|
const { meanlog = 0, sdlog = 1 } = options;
|
|
@@ -89,6 +116,14 @@ export function rlnorm(n, options = {}) {
|
|
|
89
116
|
* @param options.location Location. Default `0`.
|
|
90
117
|
* @param options.scale Scale. Default `1`.
|
|
91
118
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* setSeed(1);
|
|
123
|
+
* rlogis(2, { location: 0, scale: 1 }); // [-1.017531, -0.523116]
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* @category Random numbers
|
|
92
127
|
*/
|
|
93
128
|
export function rlogis(n, options = {}) {
|
|
94
129
|
const { location = 0, scale = 1 } = options;
|
|
@@ -108,6 +143,14 @@ export function rlogis(n, options = {}) {
|
|
|
108
143
|
* @param options.location Location. Default `0`.
|
|
109
144
|
* @param options.scale Scale. Default `1`.
|
|
110
145
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* setSeed(1);
|
|
150
|
+
* rcauchy(2, { location: 0, scale: 1 }); // [1.10252, 2.353831]
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @category Random numbers
|
|
111
154
|
*/
|
|
112
155
|
export function rcauchy(n, options = {}) {
|
|
113
156
|
const { location = 0, scale = 1 } = options;
|
|
@@ -126,6 +169,14 @@ export function rcauchy(n, options = {}) {
|
|
|
126
169
|
* @param shape Shape parameter (required, as in R).
|
|
127
170
|
* @param options.scale Scale. Default `1`.
|
|
128
171
|
* @returns An array of `n` values; `NaN` for invalid parameters.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* setSeed(1);
|
|
176
|
+
* rweibull(2, 2, { scale: 1 }); // [1.151568, 0.9942477]
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @category Random numbers
|
|
129
180
|
*/
|
|
130
181
|
export function rweibull(n, shape, options = {}) {
|
|
131
182
|
const { scale = 1 } = options;
|