@numero-uno-jpn/rstats 0.15.1 → 0.15.3

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.ja.md +8 -8
  3. package/README.md +8 -8
  4. package/dist/chunks/{chunk-735NNFAS.js → chunk-3THFTB36.js} +2 -2
  5. package/dist/chunks/chunk-3TSWYE5L.js +2 -0
  6. package/dist/chunks/{chunk-7J4HW6VF.js → chunk-3WPU6LGK.js} +2 -2
  7. package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-5ITR67MB.js} +2 -2
  8. package/dist/chunks/chunk-5NRFU6F6.js +2 -0
  9. package/dist/chunks/{chunk-SAZAEPZC.js → chunk-E2LCSVQV.js} +1 -1
  10. package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
  11. package/dist/chunks/chunk-FKAKALOZ.js +2 -0
  12. package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HD73UKPT.js} +2 -2
  13. package/dist/chunks/{chunk-4ILI2FSB.js → chunk-IUULP3LE.js} +1 -1
  14. package/dist/chunks/{chunk-LY2T6DA6.js → chunk-JQG3YQOC.js} +2 -2
  15. package/dist/chunks/chunk-KT66SB7R.js +2 -0
  16. package/dist/chunks/{chunk-IQOVOHZG.js → chunk-MAUAOQG3.js} +2 -2
  17. package/dist/chunks/{chunk-GEJCBCMP.js → chunk-MDYE7QP2.js} +2 -2
  18. package/dist/chunks/{chunk-5SQFDLNG.js → chunk-NM3Z2UBQ.js} +2 -2
  19. package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-PMJI5QO6.js} +2 -2
  20. package/dist/chunks/chunk-UF7KOTHF.js +3 -0
  21. package/dist/chunks/chunk-UOE57JGT.js +2 -0
  22. package/dist/chunks/{chunk-3QTVXDUM.js → chunk-USPEBXSR.js} +2 -2
  23. package/dist/chunks/{chunk-H4CHJMFU.js → chunk-VLUL2HFW.js} +1 -1
  24. package/dist/chunks/{chunk-VOIJKNL2.js → chunk-ZIX7NYQF.js} +1 -1
  25. package/dist/descriptive.js +2 -2
  26. package/dist/distributions/arcsine.js +48 -4
  27. package/dist/distributions/beta.d.ts +1 -1
  28. package/dist/distributions/beta.js +75 -2
  29. package/dist/distributions/cauchy.js +76 -5
  30. package/dist/distributions/exponential.js +16 -2
  31. package/dist/distributions/f.js +143 -24
  32. package/dist/distributions/gamma.d.ts +13 -2
  33. package/dist/distributions/gamma.js +122 -12
  34. package/dist/distributions/geometric.js +19 -3
  35. package/dist/distributions/invgamma.js +60 -10
  36. package/dist/distributions/kumaraswamy.js +116 -17
  37. package/dist/distributions/laplace.js +5 -2
  38. package/dist/distributions/logistic.js +8 -2
  39. package/dist/distributions/lognormal.js +40 -5
  40. package/dist/distributions/nbeta.js +597 -52
  41. package/dist/distributions/nchisq.d.ts +25 -0
  42. package/dist/distributions/nchisq.js +381 -96
  43. package/dist/distributions/negbinomial.js +26 -6
  44. package/dist/distributions/normal.js +29 -1
  45. package/dist/distributions/nt.d.ts +28 -3
  46. package/dist/distributions/nt.js +401 -63
  47. package/dist/distributions/pareto.js +73 -19
  48. package/dist/distributions/t.js +102 -12
  49. package/dist/distributions/triangular.js +93 -27
  50. package/dist/distributions/tukey.js +110 -50
  51. package/dist/distributions/uniform.js +14 -9
  52. package/dist/distributions/weibull.js +65 -10
  53. package/dist/distributions.js +2 -2
  54. package/dist/internal/ddexp.d.ts +13 -0
  55. package/dist/internal/ddexp.js +55 -3
  56. package/dist/internal/discrete.js +30 -7
  57. package/dist/internal/invert.d.ts +5 -0
  58. package/dist/internal/invert.js +59 -5
  59. package/dist/internal/noncentral.d.ts +41 -0
  60. package/dist/internal/noncentral.js +124 -2
  61. package/dist/internal/poissonMixture.d.ts +7 -2
  62. package/dist/internal/poissonMixture.js +142 -38
  63. package/dist/linalg.js +2 -2
  64. package/dist/mixed.js +2 -2
  65. package/dist/numerics.js +2 -2
  66. package/dist/rstats.js +14 -14
  67. package/dist/special/beta.js +7 -0
  68. package/dist/special/dd.d.ts +1 -1
  69. package/dist/special/dd.js +4 -1
  70. package/dist/special/gamma.js +4 -0
  71. package/dist/special/igam.js +126 -21
  72. package/dist/special/incbet.d.ts +10 -0
  73. package/dist/special/incbet.js +218 -48
  74. package/dist/special/incbetLargeA.js +53 -14
  75. package/dist/special/incbetPrefactor.d.ts +57 -0
  76. package/dist/special/incbetPrefactor.js +103 -0
  77. package/dist/special/incbetTemme.js +15 -3
  78. package/dist/special/loader.js +97 -22
  79. package/dist/special.js +2 -2
  80. package/dist/tests.js +2 -2
  81. package/package.json +1 -1
  82. package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
  83. package/dist/chunks/chunk-BDJPSEA7.js +0 -2
  84. package/dist/chunks/chunk-KO6QQPC6.js +0 -2
  85. package/dist/chunks/chunk-OLPTFKIZ.js +0 -3
  86. package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
  87. package/dist/chunks/chunk-UWBYDIOH.js +0 -2
  88. package/dist/chunks/chunk-UZHY5DWN.js +0 -2
@@ -1,2 +1,124 @@
1
- // Options for noncentral distributions (dchisq / pchisq / qchisq, dt / pt / qt, df / pf / qf, dbeta / pbeta / qbeta).
2
- export {};
1
+ // Options for noncentral distributions (dchisq / pchisq / qchisq, dt / pt / qt, df / pf / qf, dbeta / pbeta / qbeta),
2
+ // and numerical helpers shared by their implementations (src/distributions/nchisq.ts, nbeta.ts).
3
+ const VIEW = new DataView(new ArrayBuffer(8));
4
+ /** Position of x in the ordered set of doubles (a signed integer; -0 and 0 share 0) */
5
+ function doubleKey(x) {
6
+ VIEW.setFloat64(0, Math.abs(x));
7
+ const k = VIEW.getBigInt64(0);
8
+ return x < 0 ? -k : k;
9
+ }
10
+ /** The double at position k (inverse of doubleKey) */
11
+ function keyDouble(k) {
12
+ VIEW.setBigInt64(0, k < 0n ? -k : k);
13
+ const x = VIEW.getFloat64(0);
14
+ return k < 0n ? -x : x;
15
+ }
16
+ /**
17
+ * The smallest double in (lo, hi] at which `passes` holds, given that it fails at lo and holds at hi and changes once
18
+ * in between: searched from x outwards over the doubles themselves by steps of 1, 2, 4, ... and then bisected, so that
19
+ * it takes about 2 log2(d) steps for an x d doubles away (at most about 130). The last resort of the noncentral
20
+ * quantiles, for distribution functions too steep for the Newton iteration (a spread below the spacing of the
21
+ * doubles, where the function steps from 0 to 1/2 at one double, or a slope at which one double changes log p by
22
+ * more than its tolerance); x is then the Newton iteration's result, usually within a few doubles.
23
+ */
24
+ export function searchDoubles(x, lo, hi, passes) {
25
+ let a = doubleKey(lo);
26
+ let b = doubleKey(hi);
27
+ const k = doubleKey(Math.min(Math.max(x, lo), hi));
28
+ if (k > a && k < b) {
29
+ if (passes(keyDouble(k))) {
30
+ b = k;
31
+ for (let step = 1n; b - step > a; step *= 2n) {
32
+ if (!passes(keyDouble(b - step))) {
33
+ a = b - step;
34
+ break;
35
+ }
36
+ b -= step;
37
+ }
38
+ }
39
+ else {
40
+ a = k;
41
+ for (let step = 1n; a + step < b; step *= 2n) {
42
+ if (passes(keyDouble(a + step))) {
43
+ b = a + step;
44
+ break;
45
+ }
46
+ a += step;
47
+ }
48
+ }
49
+ }
50
+ while (b - a > 1n) {
51
+ const m = a + (b - a) / 2n;
52
+ if (passes(keyDouble(m)))
53
+ b = m;
54
+ else
55
+ a = m;
56
+ }
57
+ return keyDouble(b);
58
+ }
59
+ /**
60
+ * The quantile of a distribution whose relative spread is below about 1e-10, so that its distribution function steps
61
+ * within a few doubles of the start x0 (where the Newton iteration only wanders): searched for directly when the step
62
+ * lies within x0 (1 ± 1e-12), otherwise undefined. hi caps the upper end of that interval
63
+ */
64
+ export function narrowQuantile(x0, hi, passes) {
65
+ const a = x0 * (1 - 1e-12);
66
+ const b = Math.min(x0 * (1 + 1e-12), hi);
67
+ return a < b && !passes(a) && passes(b) ? searchDoubles(x0, a, b, passes) : undefined;
68
+ }
69
+ // Gauss-Kronrod 7-15 rule (QUADPACK, public domain; as in src/integrate/quadpack.ts): Kronrod nodes, from the end of
70
+ // [-1, 1] to the centre, their weights, and the Gauss weights of the odd-numbered nodes
71
+ const GK15_X = [
72
+ 0.991455371120812639206854697526329, 0.949107912342758524526189684047851, 0.864864423359769072789712788640926,
73
+ 0.741531185599394439863864773280788, 0.58608723546769113029414483825873, 0.405845151377397166906606412076961,
74
+ 0.207784955007898467600689403773245, 0,
75
+ ];
76
+ const GK15_WK = [
77
+ 0.02293532201052922496373200805897, 0.063092092629978553290700663189204, 0.104790010322250183839876322541518,
78
+ 0.140653259715525918745189590510238, 0.16900472663926790282658342659855, 0.190350578064785409913256402421014,
79
+ 0.204432940075298892414161999234649, 0.209482141084727828012999174891714,
80
+ ];
81
+ const GK15_WG = [
82
+ 0.129484966168869693270611432679082, 0.27970539148927666790146777142378, 0.381830050505118944950369775488975,
83
+ 0.417959183673469387755102040816327,
84
+ ];
85
+ /** Control for an integrand accurate to about the rounding (tolerance 1e-15, 4000 bisections) */
86
+ export function quadratureControl(tol = 1e-15, maxBisections = 4000) {
87
+ return { tol, maxBisections, bisections: 0 };
88
+ }
89
+ /**
90
+ * Integral of f over [a, b] by the 15-point Kronrod rule, bisecting while it differs from the 7-point Gauss rule by
91
+ * more than tol of the total (`total`, the integral accumulated so far, or this part's own value). The difference
92
+ * overestimates the error of the Kronrod value by far, so with tol = 1e-15 the result is good to about the rounding of
93
+ * the sum. The bisections are counted over the whole integral: an integrand with rounding noise above the tolerance
94
+ * would otherwise be bisected to the full depth (2^40 intervals)
95
+ */
96
+ export function kronrodAdaptive(f, a, b, total, depth, control) {
97
+ const m = (a + b) / 2;
98
+ const h = (b - a) / 2;
99
+ const fc = f(m);
100
+ let k = GK15_WK[7] * fc;
101
+ let g = GK15_WG[3] * fc;
102
+ for (let j = 0; j < 7; j++) {
103
+ const fs = f(m - h * GK15_X[j]) + f(m + h * GK15_X[j]);
104
+ k += GK15_WK[j] * fs;
105
+ if (j % 2 === 1)
106
+ g += GK15_WG[(j - 1) / 2] * fs;
107
+ }
108
+ k *= h;
109
+ g *= h;
110
+ if (Math.abs(k - g) <= control.tol * Math.max(Math.abs(k), total) || depth >= 40 || !Number.isFinite(k))
111
+ return k;
112
+ if (++control.bisections > control.maxBisections)
113
+ return k;
114
+ return kronrodAdaptive(f, a, m, total, depth + 1, control) + kronrodAdaptive(f, m, b, total, depth + 1, control);
115
+ }
116
+ /**
117
+ * Whether a noncentral density equals the central one to the double: it is e^-μ Σ_j μ^j/j! f_j, and f_1/f_0 is r
118
+ * (x (a + b)/a for the beta, x/k for χ² with k df), with the ratios of later terms smaller still, so the terms from
119
+ * j = 1 on and the factor e^-μ change it by less than 2 μ max(1, r) relative. Below 1e-20 that is the central density
120
+ * itself (at a tiny or subnormal λ the mixture's weights were NaN: dbeta(1.05e-231, 0.19, 141, { ncp: 1.4e-303 }))
121
+ */
122
+ export function negligibleNcp(mu, r) {
123
+ return mu * Math.max(1, r) < 1e-20;
124
+ }
@@ -18,8 +18,13 @@ export interface MixtureKernel {
18
18
  export declare function dpoisLogDD(x: number, lambda: number): DD;
19
19
  /** Σ_j w_j P_j (lower) or Σ_j w_j Q_j (!lower). Returned as a log if log */
20
20
  export declare function mixtureCdf(mu: number, kernel: MixtureKernel, lower: boolean, log: boolean): number;
21
- /** Return both the linear value and the log of mixtureCdf from a single summation */
22
- export declare function mixtureCdfBoth(mu: number, kernel: MixtureKernel, lower: boolean): {
21
+ /**
22
+ * Linear value and log of mixtureCdf, with the larger tail taken as 1 - (smaller): summed directly it can exceed 1 by
23
+ * a few ulps (the weights' rounding errors add up, while every P_j or Q_j is near 1), and its errors need not follow
24
+ * x, so it could also step back. lowerSmaller guesses which tail is the smaller (summed first; when the guess is wrong
25
+ * the other is summed too)
26
+ */
27
+ export declare function mixtureTail(mu: number, kernel: MixtureKernel, lower: boolean, lowerSmaller: boolean): {
23
28
  lin: number;
24
29
  log: number;
25
30
  };
@@ -71,6 +71,10 @@ function fromValue(lin, logv) {
71
71
  if (Number.isNaN(hi))
72
72
  return { h: NaN, l: 0, k: 0 };
73
73
  const k = Math.round(hi / LN_UNIT);
74
+ // Beyond 2^50 units the exponent k is not resolved to the integer (the log of the value, about -1e18 or less, is known
75
+ // only to a few units itself), so the remainder would be meaningless: keep the exponent alone
76
+ if (Math.abs(k) > 2 ** 50)
77
+ return norm(1, 0, k, true);
74
78
  const r = ddAdd([hi, lo], ddMul(LN_UNIT_DD, [-k, 0]));
75
79
  const m = Math.exp(r[0]);
76
80
  return typeof lv === "number" ? norm(m, 0, k, true) : norm(m, m * r[1], k);
@@ -80,7 +84,12 @@ export function dpoisLogDD(x, lambda) {
80
84
  if (x === 0)
81
85
  return [-lambda, 0];
82
86
  const DBL_MIN = 2.2250738585072014e-308;
83
- if (x <= lambda * DBL_MIN || lambda < x * DBL_MIN || !Number.isFinite(x)) {
87
+ // (λ < x DBL_MIN not for x in [1, 1e10], where lgamma1p(x) in double would put an absolute error of eps log Γ(x + 1)
88
+ // into a log that is not much larger: pchisq(1e-310, 1.2e-51, { ncp: 197 }) was 3.8e-15 off, and 1.4e-15 at x = 1e-307;
89
+ // bd0dd takes the logs of x and λ apart for such ratios. Below 1 log Γ(x + 1) is small, while stirlerr(x) and log x
90
+ // cancel in Loader's form; above 1e10 the error is below eps of x log λ)
91
+ const moderate = x >= 1 && x <= 1e10;
92
+ if (x <= lambda * DBL_MIN || (lambda < x * DBL_MIN && !moderate) || !Number.isFinite(x)) {
84
93
  const l = ddAdd(ddMul(ddLog([lambda, 0]), [x, 0]), [-lambda - lgamma1p(x), 0]);
85
94
  return l;
86
95
  }
@@ -120,8 +129,13 @@ function mul(a, b) {
120
129
  const p = ddMul([a.h, a.l], [b.h, b.l]);
121
130
  return norm(p[0], p[1], a.k + b.k, a.approx === true || b.approx === true);
122
131
  }
123
- /** Multiply by 2^(E (from - to)) to align to exponent to (to >= from) */
132
+ /**
133
+ * Multiply by 2^(E (from - to)) to align to exponent to (to >= from). The mantissa is below 2^1024, so from three
134
+ * steps on the result is 0 (the exponents can differ by 1e40 when a term is e^-1e42)
135
+ */
124
136
  function shift(h, l, from, to) {
137
+ if (to - from >= 3)
138
+ return [0, 0];
125
139
  for (let i = from; i < to && h !== 0; i++) {
126
140
  h *= DOWN;
127
141
  l *= DOWN;
@@ -172,6 +186,13 @@ class ScaledSum {
172
186
  this.c += x - t + this.s;
173
187
  this.s = t;
174
188
  }
189
+ get positive() {
190
+ return this.s + this.c > 0;
191
+ }
192
+ /** The sum as a Scaled value (not normalised) */
193
+ get scaled() {
194
+ return { h: this.s + this.c, l: 0, k: this.k };
195
+ }
175
196
  get log() {
176
197
  const v = this.s + this.c;
177
198
  if (Number.isNaN(v))
@@ -180,9 +201,10 @@ class ScaledSum {
180
201
  }
181
202
  get linear() {
182
203
  let v = this.s + this.c;
183
- for (let i = 0; i < this.k; i++)
204
+ // From five steps on the value has overflowed or underflowed (the exponent can be of order 1e40)
205
+ for (let i = 0; i < Math.min(this.k, 5); i++)
184
206
  v *= UP;
185
- for (let i = 0; i > this.k && v !== 0; i--)
207
+ for (let i = 0; i > Math.max(this.k, -5) && v !== 0; i--)
186
208
  v *= DOWN;
187
209
  return v;
188
210
  }
@@ -197,8 +219,12 @@ class ScaledSum {
197
219
  this.k -= b.k;
198
220
  }
199
221
  }
222
+ /** Above this mean the starts are searched for on the log scale (the walk below takes about 10 sqrt(mu) steps) */
223
+ const WALK_START = 1e8;
200
224
  /** Start of the lower sum: J such that Pois(> J) <= 3e-21 Pois(mode) */
201
225
  function upperStart(mu) {
226
+ if (mu > WALK_START)
227
+ return startSearch(mu, 1);
202
228
  let j = Math.floor(mu);
203
229
  let r = 1;
204
230
  for (;;) {
@@ -210,6 +236,8 @@ function upperStart(mu) {
210
236
  }
211
237
  /** Start of the upper sum: J such that Pois(< J) <= 3e-21 Pois(mode) */
212
238
  function lowerStart(mu) {
239
+ if (mu > WALK_START)
240
+ return startSearch(mu, -1);
213
241
  let j = Math.floor(mu);
214
242
  let r = 1;
215
243
  while (j > 0) {
@@ -220,15 +248,75 @@ function lowerStart(mu) {
220
248
  }
221
249
  return 0;
222
250
  }
251
+ /**
252
+ * The starts of upperStart (dir = 1) and lowerStart (dir = -1) for a large mean, by bisection on the log of the pmf
253
+ * relative to the mode: the first J beyond which the Poisson tail, bounded by the geometric series of the pmf ratio, is
254
+ * below 3e-21 of the mode. The ratio pmf(j ± 1)/pmf(j) is at most mu/(j + 1) (dir = 1) or j/mu (dir = -1) there.
255
+ */
256
+ function startSearch(mu, dir) {
257
+ const mode = Math.floor(mu);
258
+ const lm = dpoisRaw(mode, mu, true);
259
+ const ok = (j) => {
260
+ const next = j + dir;
261
+ if (next < 0)
262
+ return true;
263
+ const r = dir > 0 ? mu / (j + 2) : (j - 1) / mu;
264
+ return dpoisRaw(next, mu, true) - lm - Math.log1p(-r) < Math.log(3e-21);
265
+ };
266
+ // About 10 standard deviations; double the offset until it is far enough, then bisect
267
+ let lo = 0;
268
+ let hi = Math.ceil(10 * Math.sqrt(mu));
269
+ while (!ok(mode + dir * hi)) {
270
+ lo = hi;
271
+ hi *= 2;
272
+ }
273
+ for (let i = 0; i < 200 && hi - lo > 1; i++) {
274
+ const m = Math.floor((lo + hi) / 2);
275
+ if (m <= lo || m >= hi)
276
+ break;
277
+ if (ok(mode + dir * m))
278
+ hi = m;
279
+ else
280
+ lo = m;
281
+ }
282
+ return Math.max(0, mode + dir * hi);
283
+ }
223
284
  /**
224
285
  * Whether, after the peak (terms have started decreasing), the sum of the remaining terms is within relative TOL.
225
286
  * By log-concavity the rest is bounded by the geometric series term r/(1-r) with ratio r = term/prev (compared on the log scale).
287
+ * prev is ZERO before the first term
226
288
  */
227
- function negligible(lt, lprev, lsum) {
228
- if (lsum === -Infinity || !(lt < lprev))
289
+ function negligible(t, prev, sum) {
290
+ const lr = logRatio(t, prev);
291
+ if (!(lr < 0) || !sum.positive)
229
292
  return false;
230
- const lr = lt - lprev;
231
- return lt + lr - Math.log(-Math.expm1(lr)) <= LOG_TOL + lsum;
293
+ return logRatio(t, sum.scaled) + lr - Math.log(-Math.expm1(lr)) <= LOG_TOL;
294
+ }
295
+ /**
296
+ * log(a/b), from the mantissas and the difference of the exponents: the logs themselves lose their fraction when a term
297
+ * is as small as e^-1e34 (then every term would compare equal to the previous one)
298
+ */
299
+ function logRatio(a, b) {
300
+ if (a.h === 0)
301
+ return b.h === 0 ? NaN : -Infinity;
302
+ if (b.h === 0)
303
+ return Infinity;
304
+ return Math.log(a.h / b.h) + (a.l / a.h - b.l / b.h) + (a.k - b.k) * LN_UNIT;
305
+ }
306
+ /**
307
+ * w_j = w_(j-1) μ / j. Where μ is beyond 1e290 the splitting of the quotient in ddDivNum overflows (the noncentral χ²
308
+ * at λ = 1e302 and x = 1e-300 was NaN), so μ is multiplied in as a scaled number instead. (R 4.5.2 gives
309
+ * -1.0243631212370614e307 for dchisq(1e-308, 294.4, ncp = 2.9e304, log = TRUE), which is -λ/2 = -1.45e304 to the double)
310
+ */
311
+ function weightUp(w, mu, j) {
312
+ return mu < 1e290 ? mulDD(w, ddDivNum([mu, 0], j)) : divDD(mulDD(w, [mu, 0]), [j, 0]);
313
+ }
314
+ /**
315
+ * w_(j-1) = w_j j / μ, with μ split off as a scaled number where the splitting in ddDivNum would overflow: for a tiny
316
+ * μ (of j / μ; the densities at λ = 1.4e-303 were NaN) and for a huge one (of μ)
317
+ */
318
+ function weightDown(w, j, mu) {
319
+ return mu > 1e-290 && mu < 1e290 ? mulDD(w, ddDivNum([j, 0], mu)) : divDD(mulDD(w, [j, 0]), [mu, 0]);
232
320
  }
233
321
  function weight(j, mu) {
234
322
  return fromValue(dpoisRaw(j, mu, false), () => dpoisLogDD(j, mu));
@@ -240,10 +328,23 @@ function increment(k, j) {
240
328
  export function mixtureCdf(mu, kernel, lower, log) {
241
329
  return (lower ? sumLower(mu, kernel) : sumUpper(mu, kernel)).value(log);
242
330
  }
243
- /** Return both the linear value and the log of mixtureCdf from a single summation */
244
- export function mixtureCdfBoth(mu, kernel, lower) {
245
- const s = lower ? sumLower(mu, kernel) : sumUpper(mu, kernel);
246
- return { lin: s.linear, log: s.log };
331
+ /**
332
+ * Linear value and log of mixtureCdf, with the larger tail taken as 1 - (smaller): summed directly it can exceed 1 by
333
+ * a few ulps (the weights' rounding errors add up, while every P_j or Q_j is near 1), and its errors need not follow
334
+ * x, so it could also step back. lowerSmaller guesses which tail is the smaller (summed first; when the guess is wrong
335
+ * the other is summed too)
336
+ */
337
+ export function mixtureTail(mu, kernel, lower, lowerSmaller) {
338
+ let side = lowerSmaller;
339
+ let s = side ? sumLower(mu, kernel) : sumUpper(mu, kernel);
340
+ if (s.linear > 0.5) {
341
+ side = !side;
342
+ s = side ? sumLower(mu, kernel) : sumUpper(mu, kernel);
343
+ }
344
+ if (side === lower)
345
+ return { lin: s.linear, log: s.log };
346
+ const other = s.linear;
347
+ return { lin: 1 - other, log: Math.log1p(-other) };
247
348
  }
248
349
  /** Compute the log of the term w_j F_j directly (to locate the starting index) */
249
350
  function logTerm(k, mu, j, lower) {
@@ -251,7 +352,8 @@ function logTerm(k, mu, j, lower) {
251
352
  }
252
353
  /** Maximiser of a unimodal f on [lo, hi] (integer ternary search) */
253
354
  function argmax(f, lo, hi) {
254
- while (hi - lo > 2) {
355
+ // (beyond 2^53 the thirds may round to the ends, so the iterations are bounded)
356
+ for (let i = 0; i < 3000 && hi - lo > 2; i++) {
255
357
  const m1 = Math.floor(lo + (hi - lo) / 3);
256
358
  const m2 = Math.ceil(hi - (hi - lo) / 3);
257
359
  if (f(m1) < f(m2))
@@ -259,6 +361,8 @@ function argmax(f, lo, hi) {
259
361
  else
260
362
  hi = m2;
261
363
  }
364
+ if (hi - lo > 2)
365
+ return Math.round((lo + hi) / 2);
262
366
  let best = lo;
263
367
  for (let j = lo + 1; j <= hi; j++)
264
368
  if (f(j) > f(best))
@@ -293,19 +397,19 @@ function sumLower(mu, k) {
293
397
  let w = weight(J, mu);
294
398
  let d = J > 0 ? increment(k, J - 1) : ZERO;
295
399
  const sum = new ScaledSum();
296
- let prev = -Infinity;
400
+ let prev = ZERO;
297
401
  for (let j = J; j >= 0; j--) {
298
402
  const t = mul(w, p);
299
- const lt = logOf(t);
300
403
  sum.add(t);
301
- if (negligible(lt, prev, sum.log))
404
+ // (a NaN term, from a component out of its range, ends the sum: the result is NaN, not a loop to MAX_TERMS)
405
+ if (negligible(t, prev, sum) || Number.isNaN(t.h))
302
406
  break;
303
- prev = lt;
407
+ prev = t;
304
408
  if (j === 0)
305
409
  break;
306
410
  // P_{j-1} = P_j + d_{j-1}, w_{j-1} = w_j j/μ, d_{j-2} = d_{j-1} / (d_{j-1}/d_{j-2})
307
411
  p = add(p, d);
308
- w = refresh(mulDD(w, ddDivNum([j, 0], mu)), () => weight(j - 1, mu));
412
+ w = refresh(weightDown(w, j, mu), () => weight(j - 1, mu));
309
413
  if (j >= 2)
310
414
  d = d.h === 0 ? increment(k, j - 2) : refresh(divRatio(d, k, j - 1), () => increment(k, j - 2));
311
415
  }
@@ -319,23 +423,22 @@ function sumUpper(mu, k) {
319
423
  let w = weight(J, mu);
320
424
  let d = increment(k, J);
321
425
  const sum = new ScaledSum();
322
- let prev = -Infinity;
426
+ let prev = ZERO;
323
427
  let restart = -1;
324
428
  for (let j = J; j < J + MAX_TERMS; j++) {
325
429
  const t = mul(w, q);
326
- const lt = logOf(t);
327
430
  sum.add(t);
328
- if (negligible(lt, prev, sum.log))
431
+ if (negligible(t, prev, sum) || Number.isNaN(t.h))
329
432
  break;
330
- if (lt === -Infinity && j > mu + 50 && j > J + 50)
433
+ if (t.h === 0 && j > mu + 50 && j > J + 50)
331
434
  break; // all later terms are 0 too
332
- if (!searched && j - J >= LONG_WALK && lt >= prev) {
435
+ if (!searched && j - J >= LONG_WALK && !(logRatio(t, prev) < 0)) {
333
436
  // Not past the peak yet (Q_j grows rapidly with j: x far to the right of the mean).
334
437
  // Find an upper bound bracketing the peak by doubling and restart from slightly below the peak
335
438
  const f = (i) => logTerm(k, mu, i, false);
336
439
  const fj = f(j);
337
440
  let hi = j + 1;
338
- for (let step = 1; f(hi) >= fj; step *= 2)
441
+ for (let step = 1; f(hi) >= fj && step < 2 ** 60; step *= 2)
339
442
  hi = j + step;
340
443
  const r = farEdge(f, argmax(f, j, hi), -1, j);
341
444
  searched = true;
@@ -344,10 +447,10 @@ function sumUpper(mu, k) {
344
447
  break;
345
448
  }
346
449
  }
347
- prev = lt;
450
+ prev = t;
348
451
  // Q_{j+1} = Q_j + d_j, w_{j+1} = w_j μ/(j+1), d_{j+1} = d_j (d_{j+1}/d_j)
349
452
  q = add(q, d);
350
- w = refresh(mulDD(w, ddDivNum([mu, 0], j + 1)), () => weight(j + 1, mu));
453
+ w = refresh(weightUp(w, mu, j + 1), () => weight(j + 1, mu));
351
454
  d = d.h === 0 ? increment(k, j + 1) : refresh(mulRatio(d, k, j + 1), () => increment(k, j + 1));
352
455
  }
353
456
  if (restart < 0)
@@ -362,39 +465,40 @@ function sumUpper(mu, k) {
362
465
  * Returns the sum divided by divisor (as a log if log).
363
466
  */
364
467
  export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
365
- const j0 = Number.isFinite(jStar) ? Math.max(0, Math.round(jStar)) : 0;
468
+ // (μ = 0 where λ/2 underflows, at λ = 5e-324: the j = 0 term alone, as for the central distribution, whose weight is
469
+ // the only one not 0; the terms from j = 1 on are of relative size μ times the ratio of the densities of components 1
470
+ // and 0, negligible unless that ratio is above 1e300)
471
+ const j0 = Number.isFinite(jStar) && mu > 0 ? Math.max(0, Math.round(jStar)) : 0;
366
472
  const sum = new ScaledSum();
367
473
  const w0 = weight(j0, mu);
368
474
  const d0 = increment(kernel, j0);
369
475
  // Upward
370
476
  let w = w0;
371
477
  let d = d0;
372
- let prev = -Infinity;
478
+ let prev = ZERO;
373
479
  for (let j = j0; j < j0 + MAX_TERMS; j++) {
374
480
  const t = mulDD(mul(w, d), [coef(j), 0]);
375
- const lt = logOf(t);
376
481
  sum.add(t);
377
- if (negligible(lt, prev, sum.log))
482
+ if (negligible(t, prev, sum) || Number.isNaN(t.h))
378
483
  break;
379
- if (lt === -Infinity && j > mu + 50 && j > j0 + 50)
484
+ if (t.h === 0 && j > mu + 50 && j > j0 + 50)
380
485
  break; // all later terms are 0 too
381
- prev = lt;
382
- w = refresh(mulDD(w, ddDivNum([mu, 0], j + 1)), () => weight(j + 1, mu));
486
+ prev = t;
487
+ w = refresh(weightUp(w, mu, j + 1), () => weight(j + 1, mu));
383
488
  d = d.h === 0 ? increment(kernel, j + 1) : refresh(mulRatio(d, kernel, j + 1), () => increment(kernel, j + 1));
384
489
  }
385
490
  // Downward
386
491
  w = w0;
387
492
  d = d0;
388
- prev = -Infinity;
493
+ prev = ZERO;
389
494
  for (let j = j0 - 1; j >= 0; j--) {
390
- w = refresh(mulDD(w, ddDivNum([j + 1, 0], mu)), () => weight(j, mu));
495
+ w = refresh(weightDown(w, j + 1, mu), () => weight(j, mu));
391
496
  d = d.h === 0 ? increment(kernel, j) : refresh(divRatio(d, kernel, j + 1), () => increment(kernel, j));
392
497
  const t = mulDD(mul(w, d), [coef(j), 0]);
393
- const lt = logOf(t);
394
498
  sum.add(t);
395
- if (negligible(lt, prev, sum.log))
499
+ if (negligible(t, prev, sum) || Number.isNaN(t.h))
396
500
  break;
397
- prev = lt;
501
+ prev = t;
398
502
  }
399
503
  sum.divideBy(divisor);
400
504
  return sum.value(log);
package/dist/linalg.js CHANGED
@@ -1,2 +1,2 @@
1
- /*! rStats.js 0.15.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{F as rt,h as tt,i as nt}from"./chunks/chunk-LY2T6DA6.js";import"./chunks/chunk-7J4HW6VF.js";import{a as Ft,b as St}from"./chunks/chunk-GEJCBCMP.js";import{B as xt,D as L,E as Rt,F as Y,G as Z,a as q,b as K,c as R,d as T,e as k,g as j,h as mt,i as ht,j as dt,k as pt,l as gt,m as wt,n as Mt,o as bt,p as yt,q as vt,r as $,s as kt,t as U,u as W,v as H,w as Et,x as qt,y as At,z as Nt}from"./chunks/chunk-SAZAEPZC.js";import"./chunks/chunk-5OSQ76OO.js";import"./chunks/chunk-H4CHJMFU.js";import"./chunks/chunk-UWBYDIOH.js";function Tt(r,n){let t=T(r),e=n===void 0?t:T(n);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let u=t.nrow,a=q(t.ncol,e.ncol);for(let c=0;c<e.ncol;c++)for(let i=0;i<t.ncol;i++){if(n===void 0&&i>c)continue;let f=0;for(let o=0;o<u;o++)f+=t.data[o+i*u]*e.data[o+c*u];a.data[i+c*t.ncol]=f}return n===void 0&&et(a),k(a)}function _t(r,n){let t=T(r),e=n===void 0?t:T(n);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let u=q(t.nrow,e.nrow);for(let a=0;a<e.nrow;a++)for(let c=0;c<t.nrow;c++){if(n===void 0&&c>a)continue;let i=0;for(let f=0;f<t.ncol;f++)i+=t.data[c+f*t.nrow]*e.data[a+f*e.nrow];u.data[c+a*t.nrow]=i}return n===void 0&&et(u),k(u)}function et(r){let n=r.nrow;for(let t=0;t<n;t++)for(let e=t+1;e<n;e++)r.data[e+t*n]=r.data[t+e*n]}function Pt(r,n,t={}){return ot(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function jt(r,n,t={}){return ot(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function ot(r,n,t,e,u){let a=R(r),c=T(n),i=t??a.ncol;if(!Number.isInteger(i)||i<1||i>a.nrow||i>a.ncol||i>c.nrow)throw new RangeError(`invalid k (got ${i})`);for(let o=0;o<i;o++)if(a.data[o+o*a.nrow]===0)throw new Error(`singular matrix: diagonal element ${o+1} is zero`);let f=q(i,c.ncol);for(let o=0;o<c.ncol;o++)for(let l=0;l<i;l++)f.data[l+o*i]=c.data[l+o*c.nrow];return It(a,i,f,e,u),K(n)?k(f):Array.from(f.data)}function It(r,n,t,e,u){let a=r.nrow,c=r.data,i=t.nrow,f=t.data;for(let o=0;o<t.ncol;o++){let l=o*i;if(!u&&e){for(let h=n-1;h>=0;h--)if(f[l+h]!==0){f[l+h]/=c[h+h*a];let m=f[l+h];for(let s=0;s<h;s++)f[l+s]-=m*c[s+h*a]}}else if(u)if(e)for(let h=0;h<n;h++){let m=f[l+h];for(let s=0;s<h;s++)m-=c[s+h*a]*f[l+s];f[l+h]=m/c[h+h*a]}else for(let h=n-1;h>=0;h--){let m=f[l+h];for(let s=h+1;s<n;s++)m-=c[s+h*a]*f[l+s];f[l+h]=m/c[h+h*a]}else for(let h=0;h<n;h++)if(f[l+h]!==0){f[l+h]/=c[h+h*a];let m=f[l+h];for(let s=h+1;s<n;s++)f[l+s]-=m*c[s+h*a]}}}function Ot(r){let n=R(r);j(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let s of n.data)if(!Number.isFinite(s))throw new RangeError("the matrix contains NaN or infinite values");let e={nrow:t,ncol:t,data:n.data.slice()},u=q(t,t),a=new Float64Array(t),c=new Float64Array(t);Y(e,u),Z(e,u,a,c,!0);let i=e.data,f=u.data,o=(s,d)=>s+d*t,l=new Uint8Array(t);for(let s=0;s+1<t;s++)c[s]>0&&c[s+1]<0&&(l[s++]=1);for(let s=0;s<t;s++)for(let d=s+1;d<t;d++)d===s+1&&l[s]||(i[o(d,s)]=0);let h=new Array(t),m=new Array(t);for(let s=0;s<t;s++){if(!l[s]){h[s]=i[o(s,s)],m[s]=0;continue}let d=$t(i[o(s,s)],i[o(s,s+1)],i[o(s+1,s)],i[o(s+1,s+1)]);i[o(s,s)]=d.a,i[o(s,s+1)]=d.b,i[o(s+1,s)]=d.c,i[o(s+1,s+1)]=d.d;let{cs:p,sn:w}=d;for(let g=s+2;g<t;g++){let M=i[o(s,g)],b=i[o(s+1,g)];i[o(s,g)]=p*M+w*b,i[o(s+1,g)]=p*b-w*M}for(let g=0;g<s;g++){let M=i[o(g,s)],b=i[o(g,s+1)];i[o(g,s)]=p*M+w*b,i[o(g,s+1)]=p*b-w*M}for(let g=0;g<t;g++){let M=f[o(g,s)],b=f[o(g,s+1)];f[o(g,s)]=p*M+w*b,f[o(g,s+1)]=p*b-w*M}h[s]=d.rt1r,m[s]=d.rt1i,h[s+1]=d.rt2r,m[s+1]=d.rt2i,s++}return{T:k(e),Q:k(u),valuesRe:h,valuesIm:m}}var N=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function $t(r,n,t,e){let u=22250738585072014e-324,a=Number.EPSILON,c=4,i=2**Math.trunc(Math.log(u/a)/Math.log(2)/2),f=1/i,o,l;if(t===0)o=1,l=0;else if(n===0){o=0,l=1;let m=e;e=r,r=m,n=-t,t=0}else if(r-e===0&&N(1,n)!==N(1,t))o=1,l=0;else{let m=r-e,s=.5*m,d=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*N(1,n)*N(1,t),w=Math.max(Math.abs(s),d),g=s/w*s+d/w*p;if(g>=c*a){g=s+N(Math.sqrt(w)*Math.sqrt(g),s),r=e+g,e=e-d/g*p;let M=Math.hypot(t,g);o=g/M,l=t/M,n=n-t,t=0}else{let M=0,b=n+t;for(;M++,w=Math.max(Math.abs(m),Math.abs(b)),!(!(w>=f&&(b*=i,m*=i,M<=20))&&!(w<=i&&(b*=f,m*=f,M<=20))););s=.5*m;let x=Math.hypot(b,m);o=Math.sqrt(.5*(1+Math.abs(b)/x)),l=-(s/(x*o))*N(1,b);let F=r*o+n*l,S=-r*l+n*o,Q=t*o+e*l,X=-t*l+e*o;if(r=F*o+Q*l,n=S*o+X*l,t=-F*l+Q*o,e=-S*l+X*o,m=.5*(r+e),r=m,e=m,t!==0)if(n!==0){if(N(1,n)===N(1,t)){let B=Math.sqrt(Math.abs(n)),C=Math.sqrt(Math.abs(t));s=N(B*C,t),x=1/Math.sqrt(Math.abs(n+t)),r=m+s,e=m-s,n=n-t,t=0;let G=B*x,J=C*x;m=o*G-l*J,l=o*J+l*G,o=m}}else n=-t,t=0,m=o,o=-l,l=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:e,cs:o,sn:l,rt1r:r,rt1i:h,rt2r:e,rt2i:-h}}var Ut=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Ht(r,n={}){let t=n.method??"Higham08.b";if(!Ut.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=n.order??8;if(!Number.isInteger(e)||e<1)throw new RangeError("order must be a positive integer");let u=R(r);j(u);let a=u.nrow;if(a===0)return[];if(a===1)return[[Math.exp(u.data[0])]];if(t==="Taylor"||t==="TaylorO")return k(Dt(u,e));if(t==="Pade"||t==="PadeO")return k(Qt(u,e));let c,i;t==="Higham08.b"&&(c=L("P",u),i=L("S",u));let f=zt(u),o=f.data,l=(h,m)=>h+m*a;if(i){let h=i.scale;for(let m=0;m<a;m++)for(let s=0;s<a;s++)o[l(s,m)]=o[l(s,m)]*h[s]/h[m]}if(c){let h=c.scale,m=s=>{let d=h[s];if(d!==s){for(let p=0;p<a;p++)[o[l(p,s)],o[l(p,d)]]=[o[l(p,d)],o[l(p,s)]];for(let p=0;p<a;p++)[o[l(s,p)],o[l(d,p)]]=[o[l(d,p)],o[l(s,p)]]}};for(let s=c.ilo-1;s>=0;s--)m(s);for(let s=c.ihi+1;s<a;s++)m(s)}return k(f)}var Lt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Vt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function zt(r){let n=r.nrow,t=U(r),e=V(n),u,a,c=0;if(t<=2.1){let l=t<=.015?3:t<=.25?5:t<=.95?7:9,h=Lt[l],m=E(r,r),s=I([[h[1],e]]),d=I([[h[0],e]]),p=m;for(let w=1;w<=(l-1)/2;w++)w>1&&(p=E(p,m)),_(s,h[2*w+1],p),_(d,h[2*w],p);u=E(r,s),a=d}else{c=Math.max(0,Math.ceil(Math.log2(t/5.4)));let l=P(r,1/2**c),h=E(l,l),m=E(h,h),s=E(h,m),d=Vt,p=I([[d[13],s],[d[11],m],[d[9],h]]),w=E(s,p);for(let[M,b]of[[7,s],[5,m],[3,h],[1,e]])_(w,d[M],b);u=E(l,w);let g=I([[d[12],s],[d[10],m],[d[8],h]]);a=E(s,g);for(let[M,b]of[[6,s],[4,m],[2,h],[0,e]])_(a,d[M],b)}let i=q(n,n),f=q(n,n);for(let l=0;l<n*n;l++)i.data[l]=a.data[l]-u.data[l],f.data[l]=a.data[l]+u.data[l];let o=R(H(k(i),k(f)));for(let l=0;l<c;l++)o=E(o,o);return o}function st(r){let n=Math.sqrt(U(r)*W(r)),t=1;for(;2**t<n;)t++;return t+4}function ct(r,n){for(let t=0;t<n;t++)r=E(r,r);return r}function it(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Dt(r,n){let t=st(r),e=2**t,u=V(r.nrow);for(let a=n;a>=1;a--)u=P(E(u,r),1/(a*e)),it(u);return ct(u,t)}function at(r,n,t,e){let u=e===1?r:P(r,-1),a=V(r.nrow);for(let c=n;c>=1;c--){let i=(n-c+1)/(c*(2*n-c+1)*t);a=P(E(a,u),i),it(a)}return a}function Qt(r,n){let t=r.nrow,e=st(r),u=2**e,a=at(r,n,u,1),c=at(r,n,u,-1),i=q(t,t);for(let f=0;f<t;f++)i.data.set(Xt(c,a.data.subarray(f*t,(f+1)*t)),f*t);return ct(i,e)}function Xt(r,n){let t=r.nrow,e=1e-100,u=d=>{let p=new Float64Array(t);for(let w=0;w<t;w++){let g=0;for(let M=0;M<t;M++)g+=r.data[w+M*t]*d[M];p[w]=g}return p},a=(d,p)=>{let w=0;for(let g=0;g<t;g++)w+=d[g]*p[g];return w},c=(d,p,w)=>{let g=new Float64Array(t);for(let M=0;M<t;M++)g[M]=d[M]+p*w[M];return g},i=new Float64Array(t),f=Float64Array.from(n),o=Float64Array.from(n),l=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(a(f,f)),s=a(h,n);if(Math.abs(s)<=e)return i;for(let d=0;d<t;d++){let p=u(o),w=a(h,p);if(Math.abs(w)<=e)return i;let g=s/w;if(Math.abs(g)<=e)return i;let M=c(l,-g,p);p=c(l,1,M);let b=u(p);f=c(f,-g,b);let x=Math.sqrt(a(f,f));if(i=c(i,g,p),Math.abs(x/m)<=1e-30)return i;let F=a(h,f);if(Math.abs(F)<=e)return i;let S=F/s;s=F,l=c(f,S,M),b=c(M,S,o),o=c(l,S,b)}return i}function V(r){let n=q(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function E(r,n){let t=r.nrow,e=r.ncol,u=n.ncol,a=q(t,u);for(let c=0;c<u;c++)for(let i=0;i<t;i++){let f=0;for(let o=0;o<e;o++)f+=r.data[i+o*t]*n.data[o+c*e];a.data[i+c*t]=f}return a}function P(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function _(r,n,t){for(let e=0;e<r.data.length;e++)r.data[e]=r.data[e]+n*t.data[e]}function I(r){let[n,t]=r[0],e=P(t,n);for(let u=1;u<r.length;u++)_(e,r[u][0],r[u][1]);return e}function v(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function y(r){return typeof r=="number"?[r]:Array.from(r)}function A(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function z(r,n){let t,e;if(v(r)&&v(n))t=r.map(c=>[...c]),e=n.map(c=>[...c]);else if(v(r)){let c=y(n),i=A(r);t=r.map(f=>[...f]),e=i===c.length?c.map(f=>[f]):i===1?[c]:[]}else if(v(n)){let c=y(r);e=n.map(i=>[...i]),t=n.length===c.length?[c]:n.length===1?c.map(i=>[i]):[]}else{let c=y(r),i=y(n);if(c.length===i.length)t=[c],e=i.map(f=>[f]);else if(c.length===1)t=[c],e=[i];else if(i.length===1)t=c.map(f=>[f]),e=[i];else throw new RangeError("non-conformable arguments")}let u=t[0]?.length??0;if(t.length===0||e.length!==u)throw new RangeError("non-conformable arguments");A(t);let a=A(e);return t.map(c=>{let i=new Array(a).fill(0);for(let f=0;f<a;f++){let o=0;for(let l=0;l<u;l++)o+=c[l]*e[l][f];i[f]=o}return i})}function O(r,n,t){let e=v(r),u=v(n);if(e&&u){let f=A(r);if(r.length!==n.length||f!==A(n))throw new RangeError("non-conformable arrays");return r.map((o,l)=>o.map((h,m)=>t(h,n[l][m])))}if(e||u){let f=e?r:n,o=y(e?n:r),l=f.length,h=A(f);if(o.length===0)return f.map(m=>m.map(()=>NaN));if(o.length>l*h)throw new RangeError("dims [product "+l*h+"] do not match the length of object ["+o.length+"]");return f.map((m,s)=>m.map((d,p)=>e?t(d,o[(s+p*l)%o.length]):t(o[(s+p*l)%o.length],d)))}let a=y(r),c=y(n);if(a.length===0||c.length===0)return[];let i=Math.max(a.length,c.length);return Array.from({length:i},(f,o)=>t(a[o%a.length],c[o%c.length]))}function Bt(r,n){return O(r,n,(t,e)=>t+e)}function Ct(r,n){return O(r,n,(t,e)=>t-e)}function Gt(r,n){return O(r,n,(t,e)=>t*e)}function Jt(r,n){return O(r,n,(t,e)=>t/e)}var Kt={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function Wt(r,n,t="*"){let e=typeof t=="function"?t:Kt[t];if(e===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let u=Array.from(r),a=Array.from(n);return u.map(c=>a.map(i=>e(c,i)))}function D(r){if(!v(r))return[y(r)];let n=A(r);return Array.from({length:n},(t,e)=>r.map(u=>u[e]))}function Yt(r,n={}){if(v(r)){let i=A(r);return Array.from({length:Math.min(r.length,i)},(f,o)=>r[o][o])}let t=y(r),e,u;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(u=Math.trunc(t[0]),!(u>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,u=n.nrow??t.length;let a=n.ncol??u,c=Array.from({length:u},()=>new Array(a).fill(0));for(let i=0;i<Math.min(u,a);i++)c[i][i]=e[i%e.length];return u===0?[]:c}function ft(r,n){let t=r.map(a=>v(a)?n?D(a):a.map(c=>[...c]):[y(a)]),e=r.filter(v),u=e.length>0?n?e[0].length:A(e[0]):Math.max(...t.map(a=>a[0].length));return e.forEach((a,c)=>{if((n?a.length:A(a))!==u)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${c+2})`)}),t.flatMap(a=>a.map(c=>Array.from({length:u},(i,f)=>c[f%c.length])))}function Zt(r){return typeof r=="object"&&!Array.isArray(r)&&!ArrayBuffer.isView(r)}function ut(r){let n=r.filter(Zt);if(n.length!==0){if(n.length<r.length)throw new TypeError("cannot combine data frames with vectors or matrices");return n}}function tn(...r){let n=ut(r);return n===void 0?D(ft(r,!0)):nt(n)}function nn(...r){let n=ut(r);return n===void 0?ft(r,!1):tt(n)}function rn(r,n={}){let t=y(r),e=t.length,{nrow:u,ncol:a}=n;u===void 0&&a===void 0?(u=e,a=1):u===void 0?u=Math.ceil(e/a):a===void 0&&(a=Math.ceil(e/u));let c=n.byrow??!1;return Array.from({length:u},(i,f)=>Array.from({length:a},(o,l)=>e===0?NaN:t[(c?f*a+l:f+l*u)%e]))}function en(r,n){if(!v(r)&&!v(n)){let a=y(r),c=y(n);return a.flatMap(i=>c.map(f=>i*f))}let t=v(r)?r:y(r).map(a=>[a]),e=v(n)?n:y(n).map(a=>[a]),u=[];for(let a of t)for(let c of e)u.push(a.flatMap(i=>c.map(f=>i*f)));return u}function on(r,n,t={}){let{by:e,lengthOut:u}=t;if(n===void 0&&e===void 0&&u===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return lt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(u===void 0){if(e===void 0)return lt(r??1,n??1);let c=r??1,i=n??1,f=i-c;if(f===0&&i===0)return[i];let o=f/e;if(!Number.isFinite(o)){if(e===0&&f===0)return[c];throw new RangeError("invalid '(to - from)/by'")}if(o<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(f)/Math.max(Math.abs(i),Math.abs(c))<100*Number.EPSILON)return[c];let l=Math.trunc(o+1e-10);return Array.from({length:l+1},(h,m)=>e>0?Math.min(c+m*e,i):Math.max(c+m*e,i))}if(!Number.isFinite(u)||u<0)throw new RangeError("'length.out' must be a non-negative number");let a=Math.ceil(u);if(a===0)return[];if(e===void 0){let c=n??(r??1)+(a-1),i=r??c-(a-1);if(a<=2)return[i,c].slice(0,a);if(i===c)return new Array(a).fill(i);let f=(c-i)/(a-1);return Array.from({length:a},(o,l)=>l===0?i:l===a-1?c:i+l*f)}if(n===void 0){let c=r??1;return Array.from({length:a},(i,f)=>c+f*e)}if(r===void 0)return Array.from({length:a},(c,i)=>n-(a-1-i)*e);throw new RangeError("too many arguments")}function lt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),e=r<=n?1:-1;return Array.from({length:t},(u,a)=>r+e*a)}var an=22250738585072014e-324;function sn(r){let n=r.length,t=n===0?0:r[0].length,e=r.map(o=>{if(o.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(o)}),u=Array.from({length:n},(o,l)=>l),a=Math.min(n,t);for(let o=0;o<a;o++){let l=o,h=Math.abs(e[o][o]);for(let d=o+1;d<n;d++){let p=Math.abs(e[d][o]);(p>h||Number.isNaN(h))&&(h=p,l=d)}l!==o&&([e[l],e[o]]=[e[o],e[l]],[u[l],u[o]]=[u[o],u[l]]);let m=e[o][o];if(m===0)continue;let s=Math.abs(m)>=an?1/m:NaN;for(let d=o+1;d<n;d++){let p=Number.isNaN(s)?e[d][o]/m:e[d][o]*s;e[d][o]=p;for(let w=o+1;w<t;w++)e[d][w]=e[d][w]-p*e[o][w]}}let c=Array.from({length:n},(o,l)=>Array.from({length:a},(h,m)=>l===m?1:l>m?e[l][m]:0)),i=Array.from({length:a},(o,l)=>Array.from({length:t},(h,m)=>m>=l?e[l][m]:0)),f=Array.from({length:n},()=>new Array(n).fill(0));return u.forEach((o,l)=>f[o][l]=1),{P:f,L:c,U:i}}function cn(r,n={}){let{rank:t,tol:e}=n;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let u=r.map(m=>Array.from(m)),a=rt(u,{center:n.center??!0,scale:n.scale??!1});if(a.scale?.some(m=>m===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=$(a.scaled,{nu:0}),i=u.length,f=c.d.map(m=>m/Math.sqrt(Math.max(1,i-1))),o=t===void 0?f.length:Math.min(Math.trunc(t),f.length);e!==void 0&&(o=Math.min(o,f.filter(m=>m>f[0]*e).length));let l=c.v.map(m=>m.slice(0,o)),h={sdev:f,rotation:l,x:z(a.scaled,c.v).map(m=>m.slice(0,o))};return a.center!==void 0&&(h.center=a.center),a.scale!==void 0&&(h.scale=a.scale),h}export{Bt as add,Pt as backsolve,tn as cbind,Nt as chol,xt as chol2inv,Tt as crossprod,qt as det,Et as determinant,Yt as diag,Jt as divide,Rt as eigen,Ht as expm,jt as forwardsolve,St as ginv,Ft as kappa,en as kronecker,sn as lu,z as matMul,rn as matrix,Gt as multiply,kt as norm,Wt as outer,cn as prcomp,mt as qr,pt as qrCoef,wt as qrFitted,Mt as qrQ,ht as qrQty,dt as qrQy,bt as qrR,gt as qrResid,vt as qrSolve,yt as qrX,nn as rbind,At as rcond,Ot as schur,on as seq,H as solve,Ct as subtract,$ as svd,D as t,_t as tcrossprod};
1
+ /*! rStats.js 0.15.3 | 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{F as rt,h as tt,i as nt}from"./chunks/chunk-JQG3YQOC.js";import"./chunks/chunk-3WPU6LGK.js";import{a as Ft,b as St}from"./chunks/chunk-MDYE7QP2.js";import{B as xt,D as L,E as Rt,F as Y,G as Z,a as q,b as K,c as R,d as T,e as k,g as j,h as mt,i as ht,j as dt,k as pt,l as gt,m as wt,n as Mt,o as bt,p as yt,q as vt,r as $,s as kt,t as U,u as W,v as H,w as Et,x as qt,y as At,z as Nt}from"./chunks/chunk-E2LCSVQV.js";import"./chunks/chunk-HD73UKPT.js";import"./chunks/chunk-VLUL2HFW.js";import"./chunks/chunk-KT66SB7R.js";function Tt(r,n){let t=T(r),e=n===void 0?t:T(n);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let u=t.nrow,a=q(t.ncol,e.ncol);for(let c=0;c<e.ncol;c++)for(let i=0;i<t.ncol;i++){if(n===void 0&&i>c)continue;let f=0;for(let o=0;o<u;o++)f+=t.data[o+i*u]*e.data[o+c*u];a.data[i+c*t.ncol]=f}return n===void 0&&et(a),k(a)}function _t(r,n){let t=T(r),e=n===void 0?t:T(n);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let u=q(t.nrow,e.nrow);for(let a=0;a<e.nrow;a++)for(let c=0;c<t.nrow;c++){if(n===void 0&&c>a)continue;let i=0;for(let f=0;f<t.ncol;f++)i+=t.data[c+f*t.nrow]*e.data[a+f*e.nrow];u.data[c+a*t.nrow]=i}return n===void 0&&et(u),k(u)}function et(r){let n=r.nrow;for(let t=0;t<n;t++)for(let e=t+1;e<n;e++)r.data[e+t*n]=r.data[t+e*n]}function Pt(r,n,t={}){return ot(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function jt(r,n,t={}){return ot(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function ot(r,n,t,e,u){let a=R(r),c=T(n),i=t??a.ncol;if(!Number.isInteger(i)||i<1||i>a.nrow||i>a.ncol||i>c.nrow)throw new RangeError(`invalid k (got ${i})`);for(let o=0;o<i;o++)if(a.data[o+o*a.nrow]===0)throw new Error(`singular matrix: diagonal element ${o+1} is zero`);let f=q(i,c.ncol);for(let o=0;o<c.ncol;o++)for(let l=0;l<i;l++)f.data[l+o*i]=c.data[l+o*c.nrow];return It(a,i,f,e,u),K(n)?k(f):Array.from(f.data)}function It(r,n,t,e,u){let a=r.nrow,c=r.data,i=t.nrow,f=t.data;for(let o=0;o<t.ncol;o++){let l=o*i;if(!u&&e){for(let h=n-1;h>=0;h--)if(f[l+h]!==0){f[l+h]/=c[h+h*a];let m=f[l+h];for(let s=0;s<h;s++)f[l+s]-=m*c[s+h*a]}}else if(u)if(e)for(let h=0;h<n;h++){let m=f[l+h];for(let s=0;s<h;s++)m-=c[s+h*a]*f[l+s];f[l+h]=m/c[h+h*a]}else for(let h=n-1;h>=0;h--){let m=f[l+h];for(let s=h+1;s<n;s++)m-=c[s+h*a]*f[l+s];f[l+h]=m/c[h+h*a]}else for(let h=0;h<n;h++)if(f[l+h]!==0){f[l+h]/=c[h+h*a];let m=f[l+h];for(let s=h+1;s<n;s++)f[l+s]-=m*c[s+h*a]}}}function Ot(r){let n=R(r);j(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let s of n.data)if(!Number.isFinite(s))throw new RangeError("the matrix contains NaN or infinite values");let e={nrow:t,ncol:t,data:n.data.slice()},u=q(t,t),a=new Float64Array(t),c=new Float64Array(t);Y(e,u),Z(e,u,a,c,!0);let i=e.data,f=u.data,o=(s,d)=>s+d*t,l=new Uint8Array(t);for(let s=0;s+1<t;s++)c[s]>0&&c[s+1]<0&&(l[s++]=1);for(let s=0;s<t;s++)for(let d=s+1;d<t;d++)d===s+1&&l[s]||(i[o(d,s)]=0);let h=new Array(t),m=new Array(t);for(let s=0;s<t;s++){if(!l[s]){h[s]=i[o(s,s)],m[s]=0;continue}let d=$t(i[o(s,s)],i[o(s,s+1)],i[o(s+1,s)],i[o(s+1,s+1)]);i[o(s,s)]=d.a,i[o(s,s+1)]=d.b,i[o(s+1,s)]=d.c,i[o(s+1,s+1)]=d.d;let{cs:p,sn:w}=d;for(let g=s+2;g<t;g++){let M=i[o(s,g)],b=i[o(s+1,g)];i[o(s,g)]=p*M+w*b,i[o(s+1,g)]=p*b-w*M}for(let g=0;g<s;g++){let M=i[o(g,s)],b=i[o(g,s+1)];i[o(g,s)]=p*M+w*b,i[o(g,s+1)]=p*b-w*M}for(let g=0;g<t;g++){let M=f[o(g,s)],b=f[o(g,s+1)];f[o(g,s)]=p*M+w*b,f[o(g,s+1)]=p*b-w*M}h[s]=d.rt1r,m[s]=d.rt1i,h[s+1]=d.rt2r,m[s+1]=d.rt2i,s++}return{T:k(e),Q:k(u),valuesRe:h,valuesIm:m}}var N=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function $t(r,n,t,e){let u=22250738585072014e-324,a=Number.EPSILON,c=4,i=2**Math.trunc(Math.log(u/a)/Math.log(2)/2),f=1/i,o,l;if(t===0)o=1,l=0;else if(n===0){o=0,l=1;let m=e;e=r,r=m,n=-t,t=0}else if(r-e===0&&N(1,n)!==N(1,t))o=1,l=0;else{let m=r-e,s=.5*m,d=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*N(1,n)*N(1,t),w=Math.max(Math.abs(s),d),g=s/w*s+d/w*p;if(g>=c*a){g=s+N(Math.sqrt(w)*Math.sqrt(g),s),r=e+g,e=e-d/g*p;let M=Math.hypot(t,g);o=g/M,l=t/M,n=n-t,t=0}else{let M=0,b=n+t;for(;M++,w=Math.max(Math.abs(m),Math.abs(b)),!(!(w>=f&&(b*=i,m*=i,M<=20))&&!(w<=i&&(b*=f,m*=f,M<=20))););s=.5*m;let x=Math.hypot(b,m);o=Math.sqrt(.5*(1+Math.abs(b)/x)),l=-(s/(x*o))*N(1,b);let F=r*o+n*l,S=-r*l+n*o,Q=t*o+e*l,X=-t*l+e*o;if(r=F*o+Q*l,n=S*o+X*l,t=-F*l+Q*o,e=-S*l+X*o,m=.5*(r+e),r=m,e=m,t!==0)if(n!==0){if(N(1,n)===N(1,t)){let B=Math.sqrt(Math.abs(n)),C=Math.sqrt(Math.abs(t));s=N(B*C,t),x=1/Math.sqrt(Math.abs(n+t)),r=m+s,e=m-s,n=n-t,t=0;let G=B*x,J=C*x;m=o*G-l*J,l=o*J+l*G,o=m}}else n=-t,t=0,m=o,o=-l,l=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:e,cs:o,sn:l,rt1r:r,rt1i:h,rt2r:e,rt2i:-h}}var Ut=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Ht(r,n={}){let t=n.method??"Higham08.b";if(!Ut.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=n.order??8;if(!Number.isInteger(e)||e<1)throw new RangeError("order must be a positive integer");let u=R(r);j(u);let a=u.nrow;if(a===0)return[];if(a===1)return[[Math.exp(u.data[0])]];if(t==="Taylor"||t==="TaylorO")return k(Dt(u,e));if(t==="Pade"||t==="PadeO")return k(Qt(u,e));let c,i;t==="Higham08.b"&&(c=L("P",u),i=L("S",u));let f=zt(u),o=f.data,l=(h,m)=>h+m*a;if(i){let h=i.scale;for(let m=0;m<a;m++)for(let s=0;s<a;s++)o[l(s,m)]=o[l(s,m)]*h[s]/h[m]}if(c){let h=c.scale,m=s=>{let d=h[s];if(d!==s){for(let p=0;p<a;p++)[o[l(p,s)],o[l(p,d)]]=[o[l(p,d)],o[l(p,s)]];for(let p=0;p<a;p++)[o[l(s,p)],o[l(d,p)]]=[o[l(d,p)],o[l(s,p)]]}};for(let s=c.ilo-1;s>=0;s--)m(s);for(let s=c.ihi+1;s<a;s++)m(s)}return k(f)}var Lt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Vt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function zt(r){let n=r.nrow,t=U(r),e=V(n),u,a,c=0;if(t<=2.1){let l=t<=.015?3:t<=.25?5:t<=.95?7:9,h=Lt[l],m=E(r,r),s=I([[h[1],e]]),d=I([[h[0],e]]),p=m;for(let w=1;w<=(l-1)/2;w++)w>1&&(p=E(p,m)),_(s,h[2*w+1],p),_(d,h[2*w],p);u=E(r,s),a=d}else{c=Math.max(0,Math.ceil(Math.log2(t/5.4)));let l=P(r,1/2**c),h=E(l,l),m=E(h,h),s=E(h,m),d=Vt,p=I([[d[13],s],[d[11],m],[d[9],h]]),w=E(s,p);for(let[M,b]of[[7,s],[5,m],[3,h],[1,e]])_(w,d[M],b);u=E(l,w);let g=I([[d[12],s],[d[10],m],[d[8],h]]);a=E(s,g);for(let[M,b]of[[6,s],[4,m],[2,h],[0,e]])_(a,d[M],b)}let i=q(n,n),f=q(n,n);for(let l=0;l<n*n;l++)i.data[l]=a.data[l]-u.data[l],f.data[l]=a.data[l]+u.data[l];let o=R(H(k(i),k(f)));for(let l=0;l<c;l++)o=E(o,o);return o}function st(r){let n=Math.sqrt(U(r)*W(r)),t=1;for(;2**t<n;)t++;return t+4}function ct(r,n){for(let t=0;t<n;t++)r=E(r,r);return r}function it(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Dt(r,n){let t=st(r),e=2**t,u=V(r.nrow);for(let a=n;a>=1;a--)u=P(E(u,r),1/(a*e)),it(u);return ct(u,t)}function at(r,n,t,e){let u=e===1?r:P(r,-1),a=V(r.nrow);for(let c=n;c>=1;c--){let i=(n-c+1)/(c*(2*n-c+1)*t);a=P(E(a,u),i),it(a)}return a}function Qt(r,n){let t=r.nrow,e=st(r),u=2**e,a=at(r,n,u,1),c=at(r,n,u,-1),i=q(t,t);for(let f=0;f<t;f++)i.data.set(Xt(c,a.data.subarray(f*t,(f+1)*t)),f*t);return ct(i,e)}function Xt(r,n){let t=r.nrow,e=1e-100,u=d=>{let p=new Float64Array(t);for(let w=0;w<t;w++){let g=0;for(let M=0;M<t;M++)g+=r.data[w+M*t]*d[M];p[w]=g}return p},a=(d,p)=>{let w=0;for(let g=0;g<t;g++)w+=d[g]*p[g];return w},c=(d,p,w)=>{let g=new Float64Array(t);for(let M=0;M<t;M++)g[M]=d[M]+p*w[M];return g},i=new Float64Array(t),f=Float64Array.from(n),o=Float64Array.from(n),l=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(a(f,f)),s=a(h,n);if(Math.abs(s)<=e)return i;for(let d=0;d<t;d++){let p=u(o),w=a(h,p);if(Math.abs(w)<=e)return i;let g=s/w;if(Math.abs(g)<=e)return i;let M=c(l,-g,p);p=c(l,1,M);let b=u(p);f=c(f,-g,b);let x=Math.sqrt(a(f,f));if(i=c(i,g,p),Math.abs(x/m)<=1e-30)return i;let F=a(h,f);if(Math.abs(F)<=e)return i;let S=F/s;s=F,l=c(f,S,M),b=c(M,S,o),o=c(l,S,b)}return i}function V(r){let n=q(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function E(r,n){let t=r.nrow,e=r.ncol,u=n.ncol,a=q(t,u);for(let c=0;c<u;c++)for(let i=0;i<t;i++){let f=0;for(let o=0;o<e;o++)f+=r.data[i+o*t]*n.data[o+c*e];a.data[i+c*t]=f}return a}function P(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function _(r,n,t){for(let e=0;e<r.data.length;e++)r.data[e]=r.data[e]+n*t.data[e]}function I(r){let[n,t]=r[0],e=P(t,n);for(let u=1;u<r.length;u++)_(e,r[u][0],r[u][1]);return e}function v(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function y(r){return typeof r=="number"?[r]:Array.from(r)}function A(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function z(r,n){let t,e;if(v(r)&&v(n))t=r.map(c=>[...c]),e=n.map(c=>[...c]);else if(v(r)){let c=y(n),i=A(r);t=r.map(f=>[...f]),e=i===c.length?c.map(f=>[f]):i===1?[c]:[]}else if(v(n)){let c=y(r);e=n.map(i=>[...i]),t=n.length===c.length?[c]:n.length===1?c.map(i=>[i]):[]}else{let c=y(r),i=y(n);if(c.length===i.length)t=[c],e=i.map(f=>[f]);else if(c.length===1)t=[c],e=[i];else if(i.length===1)t=c.map(f=>[f]),e=[i];else throw new RangeError("non-conformable arguments")}let u=t[0]?.length??0;if(t.length===0||e.length!==u)throw new RangeError("non-conformable arguments");A(t);let a=A(e);return t.map(c=>{let i=new Array(a).fill(0);for(let f=0;f<a;f++){let o=0;for(let l=0;l<u;l++)o+=c[l]*e[l][f];i[f]=o}return i})}function O(r,n,t){let e=v(r),u=v(n);if(e&&u){let f=A(r);if(r.length!==n.length||f!==A(n))throw new RangeError("non-conformable arrays");return r.map((o,l)=>o.map((h,m)=>t(h,n[l][m])))}if(e||u){let f=e?r:n,o=y(e?n:r),l=f.length,h=A(f);if(o.length===0)return f.map(m=>m.map(()=>NaN));if(o.length>l*h)throw new RangeError("dims [product "+l*h+"] do not match the length of object ["+o.length+"]");return f.map((m,s)=>m.map((d,p)=>e?t(d,o[(s+p*l)%o.length]):t(o[(s+p*l)%o.length],d)))}let a=y(r),c=y(n);if(a.length===0||c.length===0)return[];let i=Math.max(a.length,c.length);return Array.from({length:i},(f,o)=>t(a[o%a.length],c[o%c.length]))}function Bt(r,n){return O(r,n,(t,e)=>t+e)}function Ct(r,n){return O(r,n,(t,e)=>t-e)}function Gt(r,n){return O(r,n,(t,e)=>t*e)}function Jt(r,n){return O(r,n,(t,e)=>t/e)}var Kt={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function Wt(r,n,t="*"){let e=typeof t=="function"?t:Kt[t];if(e===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let u=Array.from(r),a=Array.from(n);return u.map(c=>a.map(i=>e(c,i)))}function D(r){if(!v(r))return[y(r)];let n=A(r);return Array.from({length:n},(t,e)=>r.map(u=>u[e]))}function Yt(r,n={}){if(v(r)){let i=A(r);return Array.from({length:Math.min(r.length,i)},(f,o)=>r[o][o])}let t=y(r),e,u;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(u=Math.trunc(t[0]),!(u>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,u=n.nrow??t.length;let a=n.ncol??u,c=Array.from({length:u},()=>new Array(a).fill(0));for(let i=0;i<Math.min(u,a);i++)c[i][i]=e[i%e.length];return u===0?[]:c}function ft(r,n){let t=r.map(a=>v(a)?n?D(a):a.map(c=>[...c]):[y(a)]),e=r.filter(v),u=e.length>0?n?e[0].length:A(e[0]):Math.max(...t.map(a=>a[0].length));return e.forEach((a,c)=>{if((n?a.length:A(a))!==u)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${c+2})`)}),t.flatMap(a=>a.map(c=>Array.from({length:u},(i,f)=>c[f%c.length])))}function Zt(r){return typeof r=="object"&&!Array.isArray(r)&&!ArrayBuffer.isView(r)}function ut(r){let n=r.filter(Zt);if(n.length!==0){if(n.length<r.length)throw new TypeError("cannot combine data frames with vectors or matrices");return n}}function tn(...r){let n=ut(r);return n===void 0?D(ft(r,!0)):nt(n)}function nn(...r){let n=ut(r);return n===void 0?ft(r,!1):tt(n)}function rn(r,n={}){let t=y(r),e=t.length,{nrow:u,ncol:a}=n;u===void 0&&a===void 0?(u=e,a=1):u===void 0?u=Math.ceil(e/a):a===void 0&&(a=Math.ceil(e/u));let c=n.byrow??!1;return Array.from({length:u},(i,f)=>Array.from({length:a},(o,l)=>e===0?NaN:t[(c?f*a+l:f+l*u)%e]))}function en(r,n){if(!v(r)&&!v(n)){let a=y(r),c=y(n);return a.flatMap(i=>c.map(f=>i*f))}let t=v(r)?r:y(r).map(a=>[a]),e=v(n)?n:y(n).map(a=>[a]),u=[];for(let a of t)for(let c of e)u.push(a.flatMap(i=>c.map(f=>i*f)));return u}function on(r,n,t={}){let{by:e,lengthOut:u}=t;if(n===void 0&&e===void 0&&u===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return lt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(u===void 0){if(e===void 0)return lt(r??1,n??1);let c=r??1,i=n??1,f=i-c;if(f===0&&i===0)return[i];let o=f/e;if(!Number.isFinite(o)){if(e===0&&f===0)return[c];throw new RangeError("invalid '(to - from)/by'")}if(o<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(f)/Math.max(Math.abs(i),Math.abs(c))<100*Number.EPSILON)return[c];let l=Math.trunc(o+1e-10);return Array.from({length:l+1},(h,m)=>e>0?Math.min(c+m*e,i):Math.max(c+m*e,i))}if(!Number.isFinite(u)||u<0)throw new RangeError("'length.out' must be a non-negative number");let a=Math.ceil(u);if(a===0)return[];if(e===void 0){let c=n??(r??1)+(a-1),i=r??c-(a-1);if(a<=2)return[i,c].slice(0,a);if(i===c)return new Array(a).fill(i);let f=(c-i)/(a-1);return Array.from({length:a},(o,l)=>l===0?i:l===a-1?c:i+l*f)}if(n===void 0){let c=r??1;return Array.from({length:a},(i,f)=>c+f*e)}if(r===void 0)return Array.from({length:a},(c,i)=>n-(a-1-i)*e);throw new RangeError("too many arguments")}function lt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),e=r<=n?1:-1;return Array.from({length:t},(u,a)=>r+e*a)}var an=22250738585072014e-324;function sn(r){let n=r.length,t=n===0?0:r[0].length,e=r.map(o=>{if(o.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(o)}),u=Array.from({length:n},(o,l)=>l),a=Math.min(n,t);for(let o=0;o<a;o++){let l=o,h=Math.abs(e[o][o]);for(let d=o+1;d<n;d++){let p=Math.abs(e[d][o]);(p>h||Number.isNaN(h))&&(h=p,l=d)}l!==o&&([e[l],e[o]]=[e[o],e[l]],[u[l],u[o]]=[u[o],u[l]]);let m=e[o][o];if(m===0)continue;let s=Math.abs(m)>=an?1/m:NaN;for(let d=o+1;d<n;d++){let p=Number.isNaN(s)?e[d][o]/m:e[d][o]*s;e[d][o]=p;for(let w=o+1;w<t;w++)e[d][w]=e[d][w]-p*e[o][w]}}let c=Array.from({length:n},(o,l)=>Array.from({length:a},(h,m)=>l===m?1:l>m?e[l][m]:0)),i=Array.from({length:a},(o,l)=>Array.from({length:t},(h,m)=>m>=l?e[l][m]:0)),f=Array.from({length:n},()=>new Array(n).fill(0));return u.forEach((o,l)=>f[o][l]=1),{P:f,L:c,U:i}}function cn(r,n={}){let{rank:t,tol:e}=n;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let u=r.map(m=>Array.from(m)),a=rt(u,{center:n.center??!0,scale:n.scale??!1});if(a.scale?.some(m=>m===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=$(a.scaled,{nu:0}),i=u.length,f=c.d.map(m=>m/Math.sqrt(Math.max(1,i-1))),o=t===void 0?f.length:Math.min(Math.trunc(t),f.length);e!==void 0&&(o=Math.min(o,f.filter(m=>m>f[0]*e).length));let l=c.v.map(m=>m.slice(0,o)),h={sdev:f,rotation:l,x:z(a.scaled,c.v).map(m=>m.slice(0,o))};return a.center!==void 0&&(h.center=a.center),a.scale!==void 0&&(h.scale=a.scale),h}export{Bt as add,Pt as backsolve,tn as cbind,Nt as chol,xt as chol2inv,Tt as crossprod,qt as det,Et as determinant,Yt as diag,Jt as divide,Rt as eigen,Ht as expm,jt as forwardsolve,St as ginv,Ft as kappa,en as kronecker,sn as lu,z as matMul,rn as matrix,Gt as multiply,kt as norm,Wt as outer,cn as prcomp,mt as qr,pt as qrCoef,wt as qrFitted,Mt as qrQ,ht as qrQty,dt as qrQy,bt as qrR,gt as qrResid,vt as qrSolve,yt as qrX,nn as rbind,At as rcond,Ot as schur,on as seq,H as solve,Ct as subtract,$ as svd,D as t,_t as tcrossprod};