@numero-uno-jpn/rstats 0.5.0 → 0.6.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 +101 -0
- package/README.ja.md +36 -20
- package/README.md +34 -18
- package/THIRD_PARTY_NOTICES.md +25 -5
- package/dist/chunks/chunk-2JCIB5H2.js +2 -0
- package/dist/chunks/chunk-7JVSDGQQ.js +2 -0
- package/dist/chunks/chunk-7NK4SA6B.js +2 -0
- package/dist/chunks/chunk-AJYQNFXK.js +2 -0
- package/dist/chunks/chunk-DRNMIEYH.js +2 -0
- package/dist/chunks/chunk-NAYDS7CL.js +2 -0
- package/dist/chunks/chunk-NOCEAM44.js +2 -0
- package/dist/chunks/chunk-QNAIK4PY.js +2 -0
- package/dist/chunks/chunk-TPC6MTYC.js +2 -0
- package/dist/chunks/chunk-UGUQYWRQ.js +2 -0
- package/dist/chunks/chunk-XM2TVD63.js +2 -0
- package/dist/chunks/chunk-Y4IZV7XK.js +2 -0
- package/dist/chunks/chunk-ZWJ42QN6.js +2 -0
- package/dist/descriptive/basic.d.ts +73 -0
- package/dist/descriptive/basic.js +256 -0
- package/dist/descriptive/cov.d.ts +39 -0
- package/dist/descriptive/cov.js +105 -0
- package/dist/descriptive/hist.d.ts +64 -0
- package/dist/descriptive/hist.js +304 -0
- package/dist/descriptive/moments.d.ts +63 -0
- package/dist/descriptive/moments.js +158 -0
- package/dist/descriptive/quantile.d.ts +50 -0
- package/dist/descriptive/quantile.js +131 -0
- package/dist/descriptive/rank.d.ts +23 -0
- package/dist/descriptive/rank.js +72 -0
- package/dist/descriptive/table.d.ts +54 -0
- package/dist/descriptive/table.js +160 -0
- package/dist/descriptive.js +2 -0
- package/dist/distributions/arcsine.d.ts +24 -0
- package/dist/distributions/arcsine.js +73 -0
- package/dist/distributions/invgamma.d.ts +29 -0
- package/dist/distributions/invgamma.js +85 -0
- package/dist/distributions/kumaraswamy.d.ts +17 -0
- package/dist/distributions/kumaraswamy.js +96 -0
- package/dist/distributions/laplace.d.ts +23 -0
- package/dist/distributions/laplace.js +69 -0
- package/dist/distributions/multinomial.d.ts +24 -0
- package/dist/distributions/multinomial.js +70 -0
- package/dist/distributions/nbeta.js +28 -10
- package/dist/distributions/nchisq.js +20 -3
- package/dist/distributions/pareto.d.ts +23 -0
- package/dist/distributions/pareto.js +76 -0
- package/dist/distributions/summary.d.ts +25 -0
- package/dist/distributions/summary.js +289 -0
- package/dist/distributions/triangular.d.ts +25 -0
- package/dist/distributions/triangular.js +87 -0
- package/dist/distributions.js +2 -0
- package/dist/entries/descriptive.d.ts +7 -0
- package/dist/entries/descriptive.js +8 -0
- package/dist/entries/distributions.d.ts +41 -0
- package/dist/entries/distributions.js +41 -0
- package/dist/entries/linalg.d.ts +11 -0
- package/dist/entries/linalg.js +11 -0
- package/dist/entries/numerics.d.ts +8 -0
- package/dist/entries/numerics.js +6 -0
- package/dist/entries/special.d.ts +9 -0
- package/dist/entries/special.js +9 -0
- package/dist/entries/tests.d.ts +21 -0
- package/dist/entries/tests.js +20 -0
- package/dist/htest/ansariTest.d.ts +13 -0
- package/dist/htest/ansariTest.js +80 -0
- package/dist/htest/aov.d.ts +85 -0
- package/dist/htest/aov.js +178 -0
- package/dist/htest/binomTest.d.ts +36 -0
- package/dist/htest/binomTest.js +121 -0
- package/dist/htest/cor.d.ts +14 -0
- package/dist/htest/cor.js +4 -3
- package/dist/htest/core/ansari.d.ts +18 -0
- package/dist/htest/core/ansari.js +121 -0
- package/dist/htest/core/kolmogorov.d.ts +26 -0
- package/dist/htest/core/kolmogorov.js +221 -0
- package/dist/htest/core/mantelhaen.d.ts +26 -0
- package/dist/htest/core/mantelhaen.js +145 -0
- package/dist/htest/core/shapiro.d.ts +24 -0
- package/dist/htest/core/shapiro.js +142 -0
- package/dist/htest/core/smirnov.d.ts +20 -0
- package/dist/htest/core/smirnov.js +75 -0
- package/dist/htest/fisherTest.js +134 -120
- package/dist/htest/groups.d.ts +20 -0
- package/dist/htest/groups.js +62 -0
- package/dist/htest/ksTest.d.ts +31 -0
- package/dist/htest/ksTest.js +106 -0
- package/dist/htest/mantelhaenTest.d.ts +26 -0
- package/dist/htest/mantelhaenTest.js +196 -0
- package/dist/htest/mcnemarTest.d.ts +26 -0
- package/dist/htest/mcnemarTest.js +99 -0
- package/dist/htest/onewayTest.d.ts +26 -0
- package/dist/htest/onewayTest.js +97 -0
- package/dist/htest/pairwise.d.ts +64 -0
- package/dist/htest/pairwise.js +152 -0
- package/dist/htest/poissonTest.d.ts +35 -0
- package/dist/htest/poissonTest.js +170 -0
- package/dist/htest/propTest.d.ts +37 -0
- package/dist/htest/propTest.js +164 -0
- package/dist/htest/rankTests.d.ts +52 -0
- package/dist/htest/rankTests.js +234 -0
- package/dist/htest/types.d.ts +2 -0
- package/dist/htest/types.js +6 -0
- package/dist/htest/varTest.d.ts +48 -0
- package/dist/htest/varTest.js +118 -0
- package/dist/htest/wilcoxTest.js +136 -174
- package/dist/index.d.ts +6 -50
- package/dist/index.js +8 -50
- package/dist/integrate/quadpack.d.ts +29 -0
- package/dist/integrate/quadpack.js +525 -0
- package/dist/internal/seq.d.ts +5 -0
- package/dist/internal/seq.js +14 -0
- package/dist/internal/uniroot.js +10 -71
- package/dist/linalg/lu.d.ts +45 -0
- package/dist/linalg/lu.js +88 -0
- package/dist/linalg/ops.d.ts +90 -0
- package/dist/linalg/ops.js +320 -0
- package/dist/linalg.js +2 -0
- package/dist/models/lm.d.ts +98 -0
- package/dist/models/lm.js +187 -0
- package/dist/numerics/deriv.d.ts +37 -0
- package/dist/numerics/deriv.js +150 -0
- package/dist/numerics/integrate.d.ts +91 -0
- package/dist/numerics/integrate.js +264 -0
- package/dist/numerics/interp.d.ts +103 -0
- package/dist/numerics/interp.js +581 -0
- package/dist/numerics/ode.d.ts +23 -0
- package/dist/numerics/ode.js +59 -0
- package/dist/numerics.js +2 -0
- package/dist/optim/localmin.d.ts +16 -0
- package/dist/optim/localmin.js +97 -0
- package/dist/random/beta.js +1 -1
- package/dist/random/binomial.js +1 -1
- package/dist/random/continuous.js +1 -1
- package/dist/random/extra.d.ts +33 -0
- package/dist/random/extra.js +62 -0
- package/dist/random/gamma.js +1 -1
- package/dist/random/hyper.js +1 -1
- package/dist/random/mt.js +0 -1
- package/dist/random/poisson.js +0 -1
- package/dist/random/rank.js +1 -1
- package/dist/random/rng.js +1 -1
- package/dist/random/sample.js +1 -1
- package/dist/rstats.js +2 -2
- package/dist/special/besselIK.d.ts +38 -0
- package/dist/special/besselIK.js +315 -0
- package/dist/special/besselJY.d.ts +32 -0
- package/dist/special/besselJY.js +508 -0
- package/dist/special/besselSeries.d.ts +8 -0
- package/dist/special/besselSeries.js +24 -0
- package/dist/special/beta.js +22 -5
- package/dist/special/choose.d.ts +75 -0
- package/dist/special/choose.js +233 -0
- package/dist/special/erf.d.ts +28 -2
- package/dist/special/erf.js +37 -10
- package/dist/special/extra.d.ts +47 -0
- package/dist/special/extra.js +220 -0
- package/dist/special/igam.d.ts +4 -0
- package/dist/special/igam.js +57 -2
- package/dist/special/incbet.js +77 -22
- package/dist/special/lgammaDiff.d.ts +7 -0
- package/dist/special/lgammaDiff.js +16 -3
- package/dist/special/psigamma.d.ts +48 -0
- package/dist/special/psigamma.js +318 -0
- package/dist/special/rCompatible.d.ts +8 -0
- package/dist/special/rCompatible.js +1 -0
- package/dist/special.js +2 -0
- package/dist/tests.js +2 -0
- package/package.json +26 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// Numerical integration, minimisation and root finding of a function of one variable: R's integrate, optimize and
|
|
2
|
+
// uniroot.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js (the integration core is src/integrate/quadpack.ts, ported from SciPy's QUADPACK, and the
|
|
5
|
+
// minimiser src/optim/localmin.ts). integrate uses QUADPACK's dqags on finite ranges and dqagi on infinite ones with
|
|
6
|
+
// R's defaults (100 subdivisions, relative and absolute tolerance eps^0.25); optimize is Brent's localmin. uniroot's
|
|
7
|
+
// zero finder (`zero` below) is a port of netlib's public-domain zeroin.f (https://netlib.org/go/zeroin.f; Brent 1973,
|
|
8
|
+
// "Algorithms for Minimization without Derivatives", chapter 4, procedure zero). Around it, uniroot follows R's
|
|
9
|
+
// documented and observed behaviour: the interval can be extended outwards (extendInt), non-finite values of f are replaced by the
|
|
10
|
+
// largest finite numbers, and the result reports the iterations and Brent's estimate of the precision.
|
|
11
|
+
import { dqagie, dqagse } from "../integrate/quadpack.js";
|
|
12
|
+
import { localMin } from "../optim/localmin.js";
|
|
13
|
+
const MESSAGES = [
|
|
14
|
+
"OK",
|
|
15
|
+
"maximum number of subdivisions reached",
|
|
16
|
+
"roundoff error was detected",
|
|
17
|
+
"extremely bad integrand behaviour",
|
|
18
|
+
"roundoff error is detected in the extrapolation table",
|
|
19
|
+
"the integral is probably divergent",
|
|
20
|
+
"the input is invalid",
|
|
21
|
+
];
|
|
22
|
+
const TOL = Number.EPSILON ** 0.25;
|
|
23
|
+
/**
|
|
24
|
+
* Integral of a function of one variable over a finite or infinite range. Equivalent to R's `integrate()`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* integrate((x) => Math.exp(-x * x / 2), -Infinity, Infinity).value; // 2.5066282746310002 (sqrt(2 pi))
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function integrate(f, lower, upper, options = {}) {
|
|
32
|
+
const limit = options.subdivisions ?? 100;
|
|
33
|
+
const relTol = options.relTol ?? TOL;
|
|
34
|
+
const absTol = options.absTol ?? relTol;
|
|
35
|
+
const stopOnError = options.stopOnError ?? true;
|
|
36
|
+
if (Number.isNaN(lower) || Number.isNaN(upper))
|
|
37
|
+
throw new RangeError("a limit is NA or NaN");
|
|
38
|
+
if (limit < 1 || (absTol <= 0 && relTol < Math.max(50 * Number.EPSILON, 0.5e-28)))
|
|
39
|
+
throw new RangeError("invalid parameter values");
|
|
40
|
+
const g = (x) => {
|
|
41
|
+
const v = f(x);
|
|
42
|
+
if (!Number.isFinite(v))
|
|
43
|
+
throw new RangeError("non-finite function value");
|
|
44
|
+
return v;
|
|
45
|
+
};
|
|
46
|
+
const r = Number.isFinite(lower) && Number.isFinite(upper)
|
|
47
|
+
? dqagse(g, lower, upper, absTol, relTol, limit)
|
|
48
|
+
: Number.isFinite(lower)
|
|
49
|
+
? dqagie(g, lower, 1, absTol, relTol, limit)
|
|
50
|
+
: Number.isFinite(upper)
|
|
51
|
+
? dqagie(g, upper, -1, absTol, relTol, limit)
|
|
52
|
+
: dqagie(g, 0, 2, absTol, relTol, limit);
|
|
53
|
+
const message = MESSAGES[r.ier] ?? "unknown error";
|
|
54
|
+
if (r.ier === 6)
|
|
55
|
+
throw new RangeError("the input is invalid");
|
|
56
|
+
if (r.ier !== 0 && stopOnError)
|
|
57
|
+
throw new RangeError(message);
|
|
58
|
+
return { value: r.result, absError: r.abserr, subdivisions: r.last, ier: r.ier, message };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Minimum (or maximum) of a function of one variable on an interval, by Brent's golden-section and parabolic
|
|
62
|
+
* search. Equivalent to R's `optimize()`: `optimum` is the location (R's `minimum`/`maximum`) and `objective` the
|
|
63
|
+
* function there. `lower` must be less than `upper`, as in R.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* optimize((x) => (x - 1 / 3) ** 2, 0, 1).optimum; // 0.3333333333333333
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function optimize(f, lower, upper, options = {}) {
|
|
71
|
+
const maximum = options.maximum ?? false;
|
|
72
|
+
const tol = options.tol ?? TOL;
|
|
73
|
+
if (!(lower < upper))
|
|
74
|
+
throw new RangeError("'xmin' not less than 'xmax'");
|
|
75
|
+
const r = localMin(maximum ? (x) => -f(x) : f, lower, upper, tol);
|
|
76
|
+
return { optimum: r.x, objective: f(r.x) };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A root of a function of one variable in an interval, by Brent's zeroin. Equivalent to R's `uniroot()`.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* uniroot((x) => x * x - 2, 0, 2, { tol: 1e-12 }).root; // 1.4142135623731364
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function uniroot(f, lower, upper, options = {}) {
|
|
87
|
+
const tol = options.tol ?? TOL;
|
|
88
|
+
const maxiter = options.maxiter ?? 1000;
|
|
89
|
+
const extendInt = options.extendInt ?? "no";
|
|
90
|
+
if (!(lower < upper))
|
|
91
|
+
throw new RangeError("lower < upper is not fulfilled");
|
|
92
|
+
let fLo = options.fLower ?? f(lower);
|
|
93
|
+
let fUp = options.fUpper ?? f(upper);
|
|
94
|
+
if (Number.isNaN(fLo))
|
|
95
|
+
throw new RangeError("f.lower = f(lower) is NA");
|
|
96
|
+
if (Number.isNaN(fUp))
|
|
97
|
+
throw new RangeError("f.upper = f(upper) is NA");
|
|
98
|
+
// Extend the interval outwards (R's extendInt), in steps that double each time
|
|
99
|
+
const bound = (v) => Math.max(-Number.MAX_VALUE, Math.min(Number.MAX_VALUE, v));
|
|
100
|
+
const step0 = (u) => 0.01 * Math.max(1e-4, Math.abs(u));
|
|
101
|
+
const sig = extendInt === "downX" ? -1 : extendInt === "upX" ? 1 : 0;
|
|
102
|
+
const extend = extendInt === "yes" ? bound(fLo) * bound(fUp) > 0 : sig !== 0 && (sig * bound(fLo) > 0 || sig * bound(fUp) < 0);
|
|
103
|
+
let extIter = 0;
|
|
104
|
+
const count = () => {
|
|
105
|
+
if (++extIter > maxiter)
|
|
106
|
+
throw new RangeError(`no sign change found in ${extIter - 1} iterations`);
|
|
107
|
+
};
|
|
108
|
+
if (extend && extendInt === "yes") {
|
|
109
|
+
let dLo = step0(lower);
|
|
110
|
+
let dUp = step0(upper);
|
|
111
|
+
while (fLo * fUp > 0 && (Number.isFinite(lower) || Number.isFinite(upper))) {
|
|
112
|
+
count();
|
|
113
|
+
if (Number.isFinite(lower)) {
|
|
114
|
+
const x = lower - dLo;
|
|
115
|
+
const fx = f(x);
|
|
116
|
+
if (Number.isNaN(fx))
|
|
117
|
+
dLo /= 4;
|
|
118
|
+
else
|
|
119
|
+
[lower, fLo] = [x, fx];
|
|
120
|
+
}
|
|
121
|
+
if (Number.isFinite(upper)) {
|
|
122
|
+
const x = upper + dUp;
|
|
123
|
+
const fx = f(x);
|
|
124
|
+
if (Number.isNaN(fx))
|
|
125
|
+
dUp /= 4;
|
|
126
|
+
else
|
|
127
|
+
[upper, fUp] = [x, fx];
|
|
128
|
+
}
|
|
129
|
+
dLo *= 2;
|
|
130
|
+
dUp *= 2;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (extend) {
|
|
134
|
+
let d = step0(lower);
|
|
135
|
+
while (sig * fLo > 0) {
|
|
136
|
+
count();
|
|
137
|
+
lower -= d;
|
|
138
|
+
fLo = f(lower);
|
|
139
|
+
d *= 2;
|
|
140
|
+
}
|
|
141
|
+
d = step0(upper);
|
|
142
|
+
while (sig * fUp < 0) {
|
|
143
|
+
count();
|
|
144
|
+
upper += d;
|
|
145
|
+
fUp = f(upper);
|
|
146
|
+
d *= 2;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (!(Math.sign(fLo) * Math.sign(fUp) <= 0)) {
|
|
150
|
+
throw new RangeError(extend ? "did not succeed extending the interval endpoints for f(lower) * f(upper) <= 0" : "f() values at end points not of opposite sign");
|
|
151
|
+
}
|
|
152
|
+
const z = zero((x) => finiteValue(f(x)), lower, upper, finiteValue(fLo), finiteValue(fUp), tol, maxiter);
|
|
153
|
+
return {
|
|
154
|
+
root: z.root,
|
|
155
|
+
fRoot: f(z.root),
|
|
156
|
+
iter: extend ? z.iter + extIter : z.iter,
|
|
157
|
+
initIt: extend ? extIter : NaN,
|
|
158
|
+
estimPrec: z.estimPrec,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** Non-finite values of f are replaced by the largest finite numbers of their sign (NaN by the positive one). */
|
|
162
|
+
function finiteValue(v) {
|
|
163
|
+
if (v === -Infinity)
|
|
164
|
+
return -Number.MAX_VALUE;
|
|
165
|
+
return v === Infinity || Number.isNaN(v) ? Number.MAX_VALUE : v;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* A zero of f between a and b, where f(a) and f(b) (given) have opposite signs or one is 0. Port of netlib's
|
|
169
|
+
* public-domain zeroin.f (Brent 1973, chapter 4, procedure zero), with its labelled jumps written as a loop.
|
|
170
|
+
* Adaptations for uniroot: f(a) and f(b) are passed in; an endpoint where f is 0 is returned at once; at most
|
|
171
|
+
* maxiter + 1 passes, `iter` being the number of passes completed before convergence (maxiter if it did not
|
|
172
|
+
* converge); `estimPrec` is the final bracket width |c - b|.
|
|
173
|
+
*/
|
|
174
|
+
function zero(f, ax, bx, fax, fbx, tol, maxiter) {
|
|
175
|
+
if (fax === 0)
|
|
176
|
+
return { root: ax, iter: 0, estimPrec: 0 };
|
|
177
|
+
if (fbx === 0)
|
|
178
|
+
return { root: bx, iter: 0, estimPrec: 0 };
|
|
179
|
+
const eps = Number.EPSILON;
|
|
180
|
+
let a = ax;
|
|
181
|
+
let b = bx;
|
|
182
|
+
let fa = fax;
|
|
183
|
+
let fb = fbx;
|
|
184
|
+
// zeroin.f label 20: c is the end of the bracket opposite b; d is the latest step, e the one before it
|
|
185
|
+
let c = a;
|
|
186
|
+
let fc = fa;
|
|
187
|
+
let d = b - a;
|
|
188
|
+
let e = d;
|
|
189
|
+
for (let pass = 0; pass <= maxiter; pass++) {
|
|
190
|
+
// R's variant: the interpolation is judged against the step just taken (b - a), not the one before it as in
|
|
191
|
+
// zeroin.f; with zeroin.f's own rule the iterates, and so R's tolerance-limited roots, differ
|
|
192
|
+
e = b - a;
|
|
193
|
+
// label 30: keep b the better estimate, |f(b)| <= |f(c)|
|
|
194
|
+
if (Math.abs(fc) < Math.abs(fb)) {
|
|
195
|
+
a = b;
|
|
196
|
+
b = c;
|
|
197
|
+
c = a;
|
|
198
|
+
fa = fb;
|
|
199
|
+
fb = fc;
|
|
200
|
+
fc = fa;
|
|
201
|
+
}
|
|
202
|
+
// label 40: convergence test
|
|
203
|
+
const tol1 = 2 * eps * Math.abs(b) + 0.5 * tol;
|
|
204
|
+
const xm = 0.5 * (c - b);
|
|
205
|
+
if (Math.abs(xm) <= tol1 || fb === 0)
|
|
206
|
+
return { root: b, iter: pass, estimPrec: Math.abs(c - b) };
|
|
207
|
+
if (Math.abs(e) >= tol1 && Math.abs(fa) > Math.abs(fb)) {
|
|
208
|
+
// label 50: interpolation step p / q
|
|
209
|
+
let s = fb / fa;
|
|
210
|
+
let p;
|
|
211
|
+
let q;
|
|
212
|
+
if (a === c) {
|
|
213
|
+
// linear interpolation
|
|
214
|
+
p = 2 * xm * s;
|
|
215
|
+
q = 1 - s;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
// inverse quadratic interpolation
|
|
219
|
+
q = fa / fc;
|
|
220
|
+
const r = fb / fc;
|
|
221
|
+
p = s * (2 * xm * q * (q - r) - (b - a) * (r - 1));
|
|
222
|
+
q = (q - 1) * (r - 1) * (s - 1);
|
|
223
|
+
}
|
|
224
|
+
// label 70: make p >= 0
|
|
225
|
+
if (p > 0)
|
|
226
|
+
q = -q;
|
|
227
|
+
else
|
|
228
|
+
p = -p;
|
|
229
|
+
// label 90: accept the interpolation only when it falls well inside the bracket and shrinks fast enough
|
|
230
|
+
s = e;
|
|
231
|
+
e = d;
|
|
232
|
+
if (2 * p >= 3 * xm * q - Math.abs(tol1 * q) || p >= Math.abs(0.5 * s * q)) {
|
|
233
|
+
d = xm;
|
|
234
|
+
e = d;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
d = p / q;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
// bisection forced
|
|
242
|
+
d = xm;
|
|
243
|
+
e = d;
|
|
244
|
+
}
|
|
245
|
+
// label 110: take the step, at least tol1
|
|
246
|
+
a = b;
|
|
247
|
+
fa = fb;
|
|
248
|
+
if (Math.abs(d) > tol1)
|
|
249
|
+
b += d;
|
|
250
|
+
else if (xm > 0)
|
|
251
|
+
b += tol1;
|
|
252
|
+
else
|
|
253
|
+
b -= tol1;
|
|
254
|
+
// label 140: new value; when f(b) and f(c) have the same sign, restart the bracket from a (label 20)
|
|
255
|
+
fb = f(b);
|
|
256
|
+
if (fb * (fc / Math.abs(fc)) > 0) {
|
|
257
|
+
c = a;
|
|
258
|
+
fc = fa;
|
|
259
|
+
d = b - a;
|
|
260
|
+
e = d;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return { root: b, iter: maxiter, estimPrec: Math.abs(c - b) };
|
|
264
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Sample } from "../htest/types.js";
|
|
2
|
+
type Ties = "mean" | "min" | "max" | ((v: number[]) => number);
|
|
3
|
+
/** Options for {@link approx}. */
|
|
4
|
+
export interface ApproxOptions {
|
|
5
|
+
/** Points to interpolate at. Default: `n` equally spaced points over the range of `x`. */
|
|
6
|
+
xout?: Sample;
|
|
7
|
+
/** `"linear"` (default) or `"constant"` (step function). */
|
|
8
|
+
method?: "linear" | "constant";
|
|
9
|
+
/** Number of points when `xout` is not given. Default `50`. */
|
|
10
|
+
n?: number;
|
|
11
|
+
/** Value left of the data. Default: NaN (rule 1) or the first y (rule 2). */
|
|
12
|
+
yleft?: number;
|
|
13
|
+
/** Value right of the data. Default: NaN (rule 1) or the last y (rule 2). */
|
|
14
|
+
yright?: number;
|
|
15
|
+
/** Extrapolation rule (1: NaN, 2: the end value), for both sides or `[left, right]`. Default `1`. */
|
|
16
|
+
rule?: number | readonly [number, number];
|
|
17
|
+
/** For `"constant"`: the step value is (1 - f) y_left + f y_right. Default `0`. */
|
|
18
|
+
f?: number;
|
|
19
|
+
/** How to merge equal x values. Default `"mean"`. */
|
|
20
|
+
ties?: Ties;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Linear or constant interpolation. Equivalent to R's `approx()`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* approx([1, 2, 3], [10, 20, 40], { xout: [1.5, 2.5] }).y; // [15, 30]
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function approx(x: Sample, y: Sample, options?: ApproxOptions): {
|
|
31
|
+
x: number[];
|
|
32
|
+
y: number[];
|
|
33
|
+
};
|
|
34
|
+
/** Cubic spline method. */
|
|
35
|
+
export type SplineMethod = "fmm" | "natural" | "periodic" | "hyman";
|
|
36
|
+
/** Options for {@link spline}. */
|
|
37
|
+
export interface SplineOptions {
|
|
38
|
+
/** Number of equally spaced points from `xmin` to `xmax`. Default `3 * x.length`. */
|
|
39
|
+
n?: number;
|
|
40
|
+
/** `"fmm"` (default), `"natural"`, `"periodic"` or `"hyman"`. */
|
|
41
|
+
method?: SplineMethod;
|
|
42
|
+
xmin?: number;
|
|
43
|
+
xmax?: number;
|
|
44
|
+
/** Points to interpolate at (then `n`, `xmin` and `xmax` are not used). */
|
|
45
|
+
xout?: Sample;
|
|
46
|
+
/** How to merge equal x values. Default `"mean"`. */
|
|
47
|
+
ties?: Ties;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Cubic spline interpolation at given or equally spaced points. Equivalent to R's `spline()`.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* spline([1, 2, 3, 4], [1, 4, 9, 16], { xout: [2.5] }).y; // [6.25]
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function spline(x: Sample, y: Sample, options?: SplineOptions): {
|
|
58
|
+
x: number[];
|
|
59
|
+
y: number[];
|
|
60
|
+
};
|
|
61
|
+
/** Options for {@link splinefun}. */
|
|
62
|
+
export interface SplinefunOptions {
|
|
63
|
+
/** `"fmm"` (default), `"natural"`, `"periodic"`, `"monoH.FC"` or `"hyman"`. */
|
|
64
|
+
method?: SplineMethod | "monoH.FC";
|
|
65
|
+
/** How to merge equal x values. Default `"mean"`. */
|
|
66
|
+
ties?: Ties;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The cubic spline through the points, as a function of `x` and the order of derivative (0 to 3; a fraction is
|
|
70
|
+
* truncated, as R does). Equivalent to R's `splinefun()`, except that the third derivative of `"monoH.FC"` is the
|
|
71
|
+
* exact one (see {@link splinefunH}).
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const s = splinefun([1, 2, 3, 4], [1, 4, 9, 16]);
|
|
76
|
+
* s([2.5]); // [6.25]
|
|
77
|
+
* s([2.5], 1); // [5]
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function splinefun(x: Sample, y: Sample, options?: SplinefunOptions): (xq: Sample, deriv?: number) => number[];
|
|
81
|
+
/**
|
|
82
|
+
* The cubic Hermite spline with values `y` and slopes `m` at the points `x`, as a function of `x` and the order of
|
|
83
|
+
* derivative (0 to 3; a fraction is truncated, as R does); linear outside the data. Equivalent to R's `splinefunH()`,
|
|
84
|
+
* 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
|
|
85
|
+
* 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).
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* splinefunH([0, 2], [0, 0], [1, 1])([1], 3); // [3] (R: 6)
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function splinefunH(x: Sample, y: Sample, m: Sample): (xq: Sample, deriv?: number) => number[];
|
|
93
|
+
/**
|
|
94
|
+
* Lagrange interpolation in barycentric form through the points (xi, yi). Equivalent to `pracma::barylag()`; the
|
|
95
|
+
* points must lie within the range of the nodes.
|
|
96
|
+
*/
|
|
97
|
+
export declare function barylag(xi: Sample, yi: Sample, x: Sample): number[];
|
|
98
|
+
/**
|
|
99
|
+
* The polynomial of degree 2n - 1 through n points with given values and derivatives (global Hermite
|
|
100
|
+
* interpolation, jStat's hermite), evaluated at `x`. Computed by divided differences on the doubled nodes.
|
|
101
|
+
*/
|
|
102
|
+
export declare function hermiteInterp(xi: Sample, yi: Sample, di: Sample, x: Sample): number[];
|
|
103
|
+
export {};
|