@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.
Files changed (168) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.ja.md +36 -20
  3. package/README.md +34 -18
  4. package/THIRD_PARTY_NOTICES.md +25 -5
  5. package/dist/chunks/chunk-2JCIB5H2.js +2 -0
  6. package/dist/chunks/chunk-7JVSDGQQ.js +2 -0
  7. package/dist/chunks/chunk-7NK4SA6B.js +2 -0
  8. package/dist/chunks/chunk-AJYQNFXK.js +2 -0
  9. package/dist/chunks/chunk-DRNMIEYH.js +2 -0
  10. package/dist/chunks/chunk-NAYDS7CL.js +2 -0
  11. package/dist/chunks/chunk-NOCEAM44.js +2 -0
  12. package/dist/chunks/chunk-QNAIK4PY.js +2 -0
  13. package/dist/chunks/chunk-TPC6MTYC.js +2 -0
  14. package/dist/chunks/chunk-UGUQYWRQ.js +2 -0
  15. package/dist/chunks/chunk-XM2TVD63.js +2 -0
  16. package/dist/chunks/chunk-Y4IZV7XK.js +2 -0
  17. package/dist/chunks/chunk-ZWJ42QN6.js +2 -0
  18. package/dist/descriptive/basic.d.ts +73 -0
  19. package/dist/descriptive/basic.js +256 -0
  20. package/dist/descriptive/cov.d.ts +39 -0
  21. package/dist/descriptive/cov.js +105 -0
  22. package/dist/descriptive/hist.d.ts +64 -0
  23. package/dist/descriptive/hist.js +304 -0
  24. package/dist/descriptive/moments.d.ts +63 -0
  25. package/dist/descriptive/moments.js +158 -0
  26. package/dist/descriptive/quantile.d.ts +50 -0
  27. package/dist/descriptive/quantile.js +131 -0
  28. package/dist/descriptive/rank.d.ts +23 -0
  29. package/dist/descriptive/rank.js +72 -0
  30. package/dist/descriptive/table.d.ts +54 -0
  31. package/dist/descriptive/table.js +160 -0
  32. package/dist/descriptive.js +2 -0
  33. package/dist/distributions/arcsine.d.ts +24 -0
  34. package/dist/distributions/arcsine.js +73 -0
  35. package/dist/distributions/invgamma.d.ts +29 -0
  36. package/dist/distributions/invgamma.js +85 -0
  37. package/dist/distributions/kumaraswamy.d.ts +17 -0
  38. package/dist/distributions/kumaraswamy.js +96 -0
  39. package/dist/distributions/laplace.d.ts +23 -0
  40. package/dist/distributions/laplace.js +69 -0
  41. package/dist/distributions/multinomial.d.ts +24 -0
  42. package/dist/distributions/multinomial.js +70 -0
  43. package/dist/distributions/nbeta.js +28 -10
  44. package/dist/distributions/nchisq.js +20 -3
  45. package/dist/distributions/pareto.d.ts +23 -0
  46. package/dist/distributions/pareto.js +76 -0
  47. package/dist/distributions/summary.d.ts +25 -0
  48. package/dist/distributions/summary.js +289 -0
  49. package/dist/distributions/triangular.d.ts +25 -0
  50. package/dist/distributions/triangular.js +87 -0
  51. package/dist/distributions.js +2 -0
  52. package/dist/entries/descriptive.d.ts +7 -0
  53. package/dist/entries/descriptive.js +8 -0
  54. package/dist/entries/distributions.d.ts +41 -0
  55. package/dist/entries/distributions.js +41 -0
  56. package/dist/entries/linalg.d.ts +11 -0
  57. package/dist/entries/linalg.js +11 -0
  58. package/dist/entries/numerics.d.ts +8 -0
  59. package/dist/entries/numerics.js +6 -0
  60. package/dist/entries/special.d.ts +9 -0
  61. package/dist/entries/special.js +9 -0
  62. package/dist/entries/tests.d.ts +21 -0
  63. package/dist/entries/tests.js +20 -0
  64. package/dist/htest/ansariTest.d.ts +13 -0
  65. package/dist/htest/ansariTest.js +80 -0
  66. package/dist/htest/aov.d.ts +85 -0
  67. package/dist/htest/aov.js +178 -0
  68. package/dist/htest/binomTest.d.ts +36 -0
  69. package/dist/htest/binomTest.js +121 -0
  70. package/dist/htest/cor.d.ts +14 -0
  71. package/dist/htest/cor.js +4 -3
  72. package/dist/htest/core/ansari.d.ts +18 -0
  73. package/dist/htest/core/ansari.js +121 -0
  74. package/dist/htest/core/kolmogorov.d.ts +26 -0
  75. package/dist/htest/core/kolmogorov.js +221 -0
  76. package/dist/htest/core/mantelhaen.d.ts +26 -0
  77. package/dist/htest/core/mantelhaen.js +145 -0
  78. package/dist/htest/core/shapiro.d.ts +24 -0
  79. package/dist/htest/core/shapiro.js +142 -0
  80. package/dist/htest/core/smirnov.d.ts +20 -0
  81. package/dist/htest/core/smirnov.js +75 -0
  82. package/dist/htest/fisherTest.js +134 -120
  83. package/dist/htest/groups.d.ts +20 -0
  84. package/dist/htest/groups.js +62 -0
  85. package/dist/htest/ksTest.d.ts +31 -0
  86. package/dist/htest/ksTest.js +106 -0
  87. package/dist/htest/mantelhaenTest.d.ts +26 -0
  88. package/dist/htest/mantelhaenTest.js +196 -0
  89. package/dist/htest/mcnemarTest.d.ts +26 -0
  90. package/dist/htest/mcnemarTest.js +99 -0
  91. package/dist/htest/onewayTest.d.ts +26 -0
  92. package/dist/htest/onewayTest.js +97 -0
  93. package/dist/htest/pairwise.d.ts +64 -0
  94. package/dist/htest/pairwise.js +152 -0
  95. package/dist/htest/poissonTest.d.ts +35 -0
  96. package/dist/htest/poissonTest.js +170 -0
  97. package/dist/htest/propTest.d.ts +37 -0
  98. package/dist/htest/propTest.js +164 -0
  99. package/dist/htest/rankTests.d.ts +52 -0
  100. package/dist/htest/rankTests.js +234 -0
  101. package/dist/htest/types.d.ts +2 -0
  102. package/dist/htest/types.js +6 -0
  103. package/dist/htest/varTest.d.ts +48 -0
  104. package/dist/htest/varTest.js +118 -0
  105. package/dist/htest/wilcoxTest.js +136 -174
  106. package/dist/index.d.ts +6 -50
  107. package/dist/index.js +8 -50
  108. package/dist/integrate/quadpack.d.ts +29 -0
  109. package/dist/integrate/quadpack.js +525 -0
  110. package/dist/internal/seq.d.ts +5 -0
  111. package/dist/internal/seq.js +14 -0
  112. package/dist/internal/uniroot.js +10 -71
  113. package/dist/linalg/lu.d.ts +45 -0
  114. package/dist/linalg/lu.js +88 -0
  115. package/dist/linalg/ops.d.ts +90 -0
  116. package/dist/linalg/ops.js +320 -0
  117. package/dist/linalg.js +2 -0
  118. package/dist/models/lm.d.ts +98 -0
  119. package/dist/models/lm.js +187 -0
  120. package/dist/numerics/deriv.d.ts +37 -0
  121. package/dist/numerics/deriv.js +150 -0
  122. package/dist/numerics/integrate.d.ts +91 -0
  123. package/dist/numerics/integrate.js +264 -0
  124. package/dist/numerics/interp.d.ts +103 -0
  125. package/dist/numerics/interp.js +581 -0
  126. package/dist/numerics/ode.d.ts +23 -0
  127. package/dist/numerics/ode.js +59 -0
  128. package/dist/numerics.js +2 -0
  129. package/dist/optim/localmin.d.ts +16 -0
  130. package/dist/optim/localmin.js +97 -0
  131. package/dist/random/beta.js +1 -1
  132. package/dist/random/binomial.js +1 -1
  133. package/dist/random/continuous.js +1 -1
  134. package/dist/random/extra.d.ts +33 -0
  135. package/dist/random/extra.js +62 -0
  136. package/dist/random/gamma.js +1 -1
  137. package/dist/random/hyper.js +1 -1
  138. package/dist/random/mt.js +0 -1
  139. package/dist/random/poisson.js +0 -1
  140. package/dist/random/rank.js +1 -1
  141. package/dist/random/rng.js +1 -1
  142. package/dist/random/sample.js +1 -1
  143. package/dist/rstats.js +2 -2
  144. package/dist/special/besselIK.d.ts +38 -0
  145. package/dist/special/besselIK.js +315 -0
  146. package/dist/special/besselJY.d.ts +32 -0
  147. package/dist/special/besselJY.js +508 -0
  148. package/dist/special/besselSeries.d.ts +8 -0
  149. package/dist/special/besselSeries.js +24 -0
  150. package/dist/special/beta.js +22 -5
  151. package/dist/special/choose.d.ts +75 -0
  152. package/dist/special/choose.js +233 -0
  153. package/dist/special/erf.d.ts +28 -2
  154. package/dist/special/erf.js +37 -10
  155. package/dist/special/extra.d.ts +47 -0
  156. package/dist/special/extra.js +220 -0
  157. package/dist/special/igam.d.ts +4 -0
  158. package/dist/special/igam.js +57 -2
  159. package/dist/special/incbet.js +77 -22
  160. package/dist/special/lgammaDiff.d.ts +7 -0
  161. package/dist/special/lgammaDiff.js +16 -3
  162. package/dist/special/psigamma.d.ts +48 -0
  163. package/dist/special/psigamma.js +318 -0
  164. package/dist/special/rCompatible.d.ts +8 -0
  165. package/dist/special/rCompatible.js +1 -0
  166. package/dist/special.js +2 -0
  167. package/dist/tests.js +2 -0
  168. package/package.json +26 -1
@@ -0,0 +1,525 @@
1
+ // Adaptive quadrature from QUADPACK: dqagse (finite interval) and dqagie (infinite interval), the routines behind
2
+ // R's integrate().
3
+ //
4
+ // Ported from SciPy's C translation of QUADPACK, scipy/integrate/__quadpack.c (SciPy commit 5ccd743;
5
+ // Copyright (C) 2024 SciPy developers, BSD-3-Clause). QUADPACK itself (Piessens, de Doncker-Kapenga, Überhuber,
6
+ // Kahaner) is in the public domain. See THIRD_PARTY_NOTICES.md.
7
+ //
8
+ // The control flow (gotos in the C code) is kept step for step, so that the sequence of subdivisions, and hence the
9
+ // result, error estimate and counts, are those of the original.
10
+ const EPMACH = 2.220446049250313e-16;
11
+ const UFLOW = 2.2250738585072014e-308;
12
+ const OFLOW = 1.7976931348623157e308;
13
+ const K21_WG = [
14
+ 0.066671344308688137593568809893332, 0.149451349150580593145776339657697, 0.219086362515982043995534934228163,
15
+ 0.269266719309996355091226921569469, 0.295524224714752870173892994651338,
16
+ ];
17
+ const K21_XGK = [
18
+ 0.995657163025808080735527280689003, 0.973906528517171720077964012084452, 0.930157491355708226001207180059508,
19
+ 0.865063366688984510732096688423493, 0.780817726586416897063717578345042, 0.679409568299024406234327365114874,
20
+ 0.562757134668604683339000099272694, 0.433395394129247190799265943165784, 0.294392862701460198131126603103866,
21
+ 0.148874338981631210884826001129720, 0,
22
+ ];
23
+ const K21_WGK = [
24
+ 0.011694638867371874278064396062192, 0.032558162307964727478818972459390, 0.054755896574351996031381300244580,
25
+ 0.075039674810919952767043140916190, 0.093125454583697605535065465083366, 0.109387158802297641899210590325805,
26
+ 0.123491976262065851077958109831074, 0.134709217311473325928054001771707, 0.142775938577060080797094273138717,
27
+ 0.147739104901338491374841515972068, 0.149445554002916905664936468389821,
28
+ ];
29
+ /** Error estimate shared by the Gauss-Kronrod rules */
30
+ function ruleError(resk, resg, hlgth, resabs, resasc) {
31
+ let abserr = Math.abs((resk - resg) * hlgth);
32
+ if (resasc !== 0 && abserr !== 0)
33
+ abserr = resasc * Math.min(1, Math.pow((200 * abserr) / resasc, 1.5));
34
+ if (resabs > UFLOW / (50 * EPMACH))
35
+ abserr = Math.max(EPMACH * 50 * resabs, abserr);
36
+ return abserr;
37
+ }
38
+ /** 21-point Gauss-Kronrod rule on [a, b] */
39
+ function dqk21(f, a, b) {
40
+ const centr = 0.5 * (a + b);
41
+ const hlgth = 0.5 * (b - a);
42
+ const dhlgth = Math.abs(hlgth);
43
+ const fv1 = new Array(10);
44
+ const fv2 = new Array(10);
45
+ const fc = f(centr);
46
+ let resg = 0;
47
+ let resk = K21_WGK[10] * fc;
48
+ let resabs = Math.abs(resk);
49
+ for (let j = 0; j < 5; j++) {
50
+ const jtw = 2 * j + 1;
51
+ const absc = hlgth * K21_XGK[jtw];
52
+ const fval1 = f(centr - absc);
53
+ const fval2 = f(centr + absc);
54
+ fv1[jtw] = fval1;
55
+ fv2[jtw] = fval2;
56
+ const fsum = fval1 + fval2;
57
+ resg += K21_WG[j] * fsum;
58
+ resk += K21_WGK[jtw] * fsum;
59
+ resabs += K21_WGK[jtw] * (Math.abs(fval1) + Math.abs(fval2));
60
+ }
61
+ for (let j = 0; j < 5; j++) {
62
+ const jtwm1 = 2 * j;
63
+ const absc = hlgth * K21_XGK[jtwm1];
64
+ const fval1 = f(centr - absc);
65
+ const fval2 = f(centr + absc);
66
+ fv1[jtwm1] = fval1;
67
+ fv2[jtwm1] = fval2;
68
+ const fsum = fval1 + fval2;
69
+ resk += K21_WGK[jtwm1] * fsum;
70
+ resabs += K21_WGK[jtwm1] * (Math.abs(fval1) + Math.abs(fval2));
71
+ }
72
+ const reskh = resk * 0.5;
73
+ let resasc = K21_WGK[10] * Math.abs(fc - reskh);
74
+ for (let j = 0; j < 10; j++)
75
+ resasc += K21_WGK[j] * (Math.abs(fv1[j] - reskh) + Math.abs(fv2[j] - reskh));
76
+ resabs *= dhlgth;
77
+ resasc *= dhlgth;
78
+ return { result: resk * hlgth, abserr: ruleError(resk, resg, hlgth, resabs, resasc), resabs, resasc };
79
+ }
80
+ const K15I_WG = [
81
+ 0, 0.129484966168869693270611432679082, 0, 0.279705391489276667901467771423780, 0,
82
+ 0.381830050505118944950369775488975, 0, 0.417959183673469387755102040816327,
83
+ ];
84
+ const K15I_XGK = [
85
+ 0.991455371120812639206854697526329, 0.949107912342758524526189684047851, 0.864864423359769072789712788640926,
86
+ 0.741531185599394439863864773280788, 0.586087235467691130294144838258730, 0.405845151377397166906606412076961,
87
+ 0.207784955007898467600689403773245, 0,
88
+ ];
89
+ const K15I_WGK = [
90
+ 0.022935322010529224963732008058970, 0.063092092629978553290700663189204, 0.104790010322250183839876322541518,
91
+ 0.140653259715525918745189590510238, 0.169004726639267902826583426598550, 0.190350578064785409913256402421014,
92
+ 0.204432940075298892414161999234649, 0.209482141084727828012999174891714,
93
+ ];
94
+ /**
95
+ * 15-point transformed Gauss-Kronrod rule on [a, b] within (0, 1] for an infinite range: x = boun + dinf (1 - t) / t
96
+ * (inf = 1: (boun, inf), -1: (-inf, boun), 2: (-inf, inf) with boun = 0, f(x) + f(-x))
97
+ */
98
+ function dqk15i(f, boun, inf, a, b) {
99
+ const dinf = inf > 1 ? 1 : inf;
100
+ const centr = 0.5 * (a + b);
101
+ const hlgth = 0.5 * (b - a);
102
+ const fv1 = new Array(7);
103
+ const fv2 = new Array(7);
104
+ const at = (t) => {
105
+ const x = boun + (dinf * (1 - t)) / t;
106
+ let v = f(x);
107
+ if (inf === 2)
108
+ v += f(-x);
109
+ return v;
110
+ };
111
+ const fc = at(centr) / centr / centr;
112
+ let resg = fc * K15I_WG[7];
113
+ let resk = fc * K15I_WGK[7];
114
+ let resabs = Math.abs(resk);
115
+ for (let j = 0; j < 7; j++) {
116
+ const absc = hlgth * K15I_XGK[j];
117
+ const absc1 = centr - absc;
118
+ const absc2 = centr + absc;
119
+ let fval1 = at(absc1);
120
+ let fval2 = at(absc2);
121
+ fval1 = fval1 / absc1 / absc1;
122
+ fval2 = fval2 / absc2 / absc2;
123
+ fv1[j] = fval1;
124
+ fv2[j] = fval2;
125
+ const fsum = fval1 + fval2;
126
+ resg += K15I_WG[j] * fsum;
127
+ resk += K15I_WGK[j] * fsum;
128
+ resabs += K15I_WGK[j] * (Math.abs(fval1) + Math.abs(fval2));
129
+ }
130
+ const reskh = resk * 0.5;
131
+ let resasc = K15I_WGK[7] * Math.abs(fc - reskh);
132
+ for (let j = 0; j < 7; j++)
133
+ resasc += K15I_WGK[j] * (Math.abs(fv1[j] - reskh) + Math.abs(fv2[j] - reskh));
134
+ resabs *= hlgth;
135
+ resasc *= hlgth;
136
+ return { result: resk * hlgth, abserr: ruleError(resk, resg, hlgth, resabs, resasc), resabs, resasc };
137
+ }
138
+ /** Wynn's epsilon algorithm; returns [result, abserr] and updates the table */
139
+ function dqelg(t) {
140
+ const epstab = t.epstab;
141
+ const startingN = t.n;
142
+ t.nres += 1;
143
+ let abserr = OFLOW;
144
+ let result = epstab[t.n];
145
+ if (t.n < 2) {
146
+ abserr = Math.max(abserr, 5 * EPMACH * Math.abs(result));
147
+ return [result, abserr];
148
+ }
149
+ const limexp = 49;
150
+ epstab[t.n + 2] = epstab[t.n];
151
+ const newelm = Math.trunc(t.n / 2);
152
+ epstab[t.n] = OFLOW;
153
+ let k1 = t.n;
154
+ for (let i = 0; i < newelm; i++) {
155
+ const k2 = k1 - 1;
156
+ const k3 = k1 - 2;
157
+ let res = epstab[k1 + 2];
158
+ const e0 = epstab[k3];
159
+ const e1 = epstab[k2];
160
+ const e2 = res;
161
+ const e1abs = Math.abs(e1);
162
+ const delta2 = e2 - e1;
163
+ const err2 = Math.abs(delta2);
164
+ const tol2 = Math.max(Math.abs(e2), e1abs) * EPMACH;
165
+ const delta3 = e1 - e0;
166
+ const err3 = Math.abs(delta3);
167
+ const tol3 = Math.max(e1abs, Math.abs(e0)) * EPMACH;
168
+ if (!(err2 > tol2 || err3 > tol3)) {
169
+ result = res;
170
+ abserr = err2 + err3;
171
+ abserr = Math.max(abserr, 5 * EPMACH * Math.abs(result));
172
+ return [result, abserr];
173
+ }
174
+ const e3 = epstab[k1];
175
+ epstab[k1] = e1;
176
+ const delta1 = e1 - e3;
177
+ const err1 = Math.abs(delta1);
178
+ const tol1 = Math.max(e1abs, Math.abs(e3)) * EPMACH;
179
+ if (err1 <= tol1 || err2 <= tol2 || err3 <= tol3) {
180
+ t.n = i + i;
181
+ break;
182
+ }
183
+ const ss = 1 / delta1 + 1 / delta2 - 1 / delta3;
184
+ const epsinf = Math.abs(ss * e1);
185
+ if (!(epsinf > 1e-4)) {
186
+ t.n = i + i;
187
+ break;
188
+ }
189
+ res = e1 + 1 / ss;
190
+ epstab[k1] = res;
191
+ k1 -= 2;
192
+ const error = err2 + Math.abs(res - e2) + err3;
193
+ if (!(error > abserr)) {
194
+ abserr = error;
195
+ result = res;
196
+ }
197
+ }
198
+ if (t.n === limexp)
199
+ t.n = 2 * Math.trunc(limexp / 2);
200
+ const n2rem = startingN % 2;
201
+ for (let i = 0; i <= newelm; i++)
202
+ epstab[2 * i + n2rem] = epstab[2 * i + 2 + n2rem];
203
+ if (t.n !== startingN) {
204
+ let indx = startingN - t.n;
205
+ for (let i = 0; i <= t.n; i++) {
206
+ epstab[i] = epstab[indx];
207
+ indx++;
208
+ }
209
+ }
210
+ const r = t.res3la;
211
+ if (t.nres < 4) {
212
+ r[t.nres - 1] = result;
213
+ abserr = OFLOW;
214
+ }
215
+ else {
216
+ abserr = Math.abs(result - r[2]) + Math.abs(result - r[1]) + Math.abs(result - r[0]);
217
+ r[0] = r[1];
218
+ r[1] = r[2];
219
+ r[2] = result;
220
+ }
221
+ abserr = Math.max(abserr, 5 * EPMACH * Math.abs(result));
222
+ return [result, abserr];
223
+ }
224
+ /** Ordering of the error estimates (dqpsrt); returns [maxerr, ermax, nrmax] */
225
+ function dqpsrt(limit, last, maxerr, elist, iord, nrmax) {
226
+ if (last <= 2) {
227
+ iord[0] = 0;
228
+ iord[1] = 1;
229
+ const m = iord[nrmax];
230
+ return [m, elist[m], nrmax];
231
+ }
232
+ const errmax = elist[maxerr];
233
+ while (nrmax > 0 && errmax > elist[iord[nrmax - 1]]) {
234
+ iord[nrmax] = iord[nrmax - 1];
235
+ nrmax -= 1;
236
+ }
237
+ const jupbn = last > Math.trunc(limit / 2) + 2 ? limit - last + 2 : last - 1;
238
+ const errmin = elist[last - 1];
239
+ const jbnd = jupbn - 1;
240
+ const ibeg = nrmax + 1;
241
+ let i = ibeg;
242
+ let found = false;
243
+ if (ibeg <= jbnd) {
244
+ for (i = ibeg; i <= jbnd; i++) {
245
+ if (errmax >= elist[iord[i]]) {
246
+ found = true;
247
+ break;
248
+ }
249
+ iord[i - 1] = iord[i];
250
+ }
251
+ }
252
+ if (!found) {
253
+ iord[jbnd] = maxerr;
254
+ iord[jupbn] = last - 1;
255
+ }
256
+ else {
257
+ iord[i - 1] = maxerr;
258
+ let k = jbnd;
259
+ for (let j = i; j <= jbnd; j++) {
260
+ if (errmin < elist[iord[k]])
261
+ break;
262
+ iord[k + 1] = iord[k];
263
+ k--;
264
+ }
265
+ if (errmin < elist[iord[k]])
266
+ iord[k + 1] = last - 1;
267
+ else
268
+ iord[i] = last - 1;
269
+ }
270
+ const m = iord[nrmax];
271
+ return [m, elist[m], nrmax];
272
+ }
273
+ /**
274
+ * The globally adaptive loop shared by dqagse and dqagie: `rule(a, b)` integrates over [a, b] of the (possibly
275
+ * transformed) range [a0, b0]; `neval` gives the number of evaluations from the number of subintervals
276
+ */
277
+ function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
278
+ const alist = new Float64Array(limit + 1);
279
+ const blist = new Float64Array(limit + 1);
280
+ const rlist = new Float64Array(limit + 1);
281
+ const elist = new Float64Array(limit + 1);
282
+ const iord = new Int32Array(limit + 1);
283
+ let ier = 0;
284
+ let last = 0;
285
+ let result = 0;
286
+ let abserr = 0;
287
+ alist[0] = a0;
288
+ blist[0] = b0;
289
+ if (epsabs <= 0 && epsrel < Math.max(50 * EPMACH, 0.5e-28))
290
+ return { result: 0, abserr: 0, neval: 0, ier: 6, last: 0 };
291
+ // first approximation to the integral
292
+ const r0 = rule(a0, b0);
293
+ result = r0.result;
294
+ abserr = r0.abserr;
295
+ const defabs = r0.resabs;
296
+ const resabs0 = r0.resasc;
297
+ const dres = Math.abs(result);
298
+ let errbnd = Math.max(epsabs, epsrel * dres);
299
+ last = 1;
300
+ rlist[0] = result;
301
+ elist[0] = abserr;
302
+ iord[0] = 0;
303
+ if (abserr <= 100 * EPMACH * defabs && abserr > errbnd)
304
+ ier = 2;
305
+ if (limit === 1)
306
+ ier = 1;
307
+ if (ier !== 0 || (abserr <= errbnd && abserr !== resabs0) || abserr === 0) {
308
+ return { result, abserr, neval: neval(last), ier, last };
309
+ }
310
+ const eps = { epstab: new Float64Array(52), res3la: new Float64Array(3), n: 1, nres: 0 };
311
+ eps.epstab[0] = result;
312
+ let errmax = abserr;
313
+ let maxerr = 0;
314
+ let area = result;
315
+ let errsum = abserr;
316
+ abserr = OFLOW;
317
+ let nrmax = 0;
318
+ let ktmin = 0;
319
+ let extrap = false;
320
+ let noext = false;
321
+ let ierror = 0;
322
+ let iroff1 = 0;
323
+ let iroff2 = 0;
324
+ let iroff3 = 0;
325
+ let small = 0;
326
+ let erlarg = 0;
327
+ let ertest = 0;
328
+ let correc = 0;
329
+ // >= in both routines, as in QUADPACK's dqagse.f and dqagie.f and R's integrate.c (SciPy's dqagie has >)
330
+ const ksgn = dres >= (1 - 50 * EPMACH) * defabs ? 1 : -1;
331
+ let sumAll = false; // goto LINE115
332
+ let L = 1;
333
+ for (L = 1; L < limit; L++) {
334
+ last = L + 1;
335
+ // bisect the subinterval with the nrmax-th largest error estimate
336
+ const a1 = alist[maxerr];
337
+ const b1 = 0.5 * (alist[maxerr] + blist[maxerr]);
338
+ const a2 = b1;
339
+ const b2 = blist[maxerr];
340
+ const erlast = errmax;
341
+ const r1 = rule(a1, b1);
342
+ const r2 = rule(a2, b2);
343
+ const area1 = r1.result;
344
+ const error1 = r1.abserr;
345
+ const defab1 = r1.resasc;
346
+ const area2 = r2.result;
347
+ const error2 = r2.abserr;
348
+ const defab2 = r2.resasc;
349
+ // improve previous approximations to integral and error and test for accuracy
350
+ const area12 = area1 + area2;
351
+ const error12 = error1 + error2;
352
+ errsum = errsum + error12 - errmax;
353
+ area = area + area12 - rlist[maxerr];
354
+ if (defab1 !== error1 && defab2 !== error2) {
355
+ if (!(Math.abs(rlist[maxerr] - area12) > 1e-5 * Math.abs(area12) || error12 < 0.99 * errmax)) {
356
+ if (extrap)
357
+ iroff2++;
358
+ else
359
+ iroff1++;
360
+ }
361
+ if (L > 9 && error12 > errmax)
362
+ iroff3++;
363
+ }
364
+ rlist[maxerr] = area1;
365
+ rlist[L] = area2;
366
+ errbnd = Math.max(epsabs, epsrel * Math.abs(area));
367
+ // test for roundoff error and eventually set error flag
368
+ if (iroff1 + iroff2 >= 10 || iroff3 >= 20)
369
+ ier = 2;
370
+ if (iroff2 >= 5)
371
+ ierror = 3;
372
+ // number of subintervals equals limit
373
+ if (last === limit)
374
+ ier = 1;
375
+ // bad integrand behaviour at some points of the integration range
376
+ if (Math.max(Math.abs(a1), Math.abs(b2)) <= (1 + 100 * EPMACH) * (Math.abs(a2) + 1000 * UFLOW))
377
+ ier = 4;
378
+ // append the newly-created intervals to the list
379
+ if (!(error2 > error1)) {
380
+ alist[L] = a2;
381
+ blist[maxerr] = b1;
382
+ blist[L] = b2;
383
+ elist[maxerr] = error1;
384
+ elist[L] = error2;
385
+ }
386
+ else {
387
+ alist[maxerr] = a2;
388
+ alist[L] = a1;
389
+ blist[L] = b1;
390
+ rlist[maxerr] = area2;
391
+ rlist[L] = area1;
392
+ elist[maxerr] = error2;
393
+ elist[L] = error1;
394
+ }
395
+ // maintain the descending ordering of the error estimates and select the next subinterval
396
+ [maxerr, errmax, nrmax] = dqpsrt(limit, last, maxerr, elist, iord, nrmax);
397
+ if (errsum <= errbnd) {
398
+ sumAll = true;
399
+ break;
400
+ }
401
+ if (ier !== 0)
402
+ break;
403
+ if (L === 1) {
404
+ // LINE80
405
+ small = smallStart;
406
+ erlarg = errsum;
407
+ ertest = errbnd;
408
+ eps.epstab[1] = area;
409
+ continue;
410
+ }
411
+ if (noext)
412
+ continue;
413
+ erlarg -= erlast;
414
+ if (Math.abs(b1 - a1) > small)
415
+ erlarg += error12;
416
+ if (!extrap) {
417
+ // test whether the interval to be bisected next is the smallest interval
418
+ if (Math.abs(blist[maxerr] - alist[maxerr]) > small)
419
+ continue;
420
+ extrap = true;
421
+ nrmax = 1;
422
+ }
423
+ if (!(ierror === 3 || erlarg <= ertest)) {
424
+ // the smallest interval has the largest error: before bisecting, decrease the sum of the errors over the
425
+ // larger intervals (erlarg) and perform extrapolation
426
+ const jupbnd = last > 2 + Math.trunc(limit / 2) ? limit + 3 - last : last;
427
+ let larger = false;
428
+ for (let k = nrmax; k < jupbnd; k++) {
429
+ maxerr = iord[nrmax];
430
+ errmax = elist[maxerr];
431
+ if (Math.abs(blist[maxerr] - alist[maxerr]) > small) {
432
+ larger = true;
433
+ break;
434
+ }
435
+ nrmax++;
436
+ }
437
+ if (larger)
438
+ continue; // LINE90
439
+ }
440
+ // LINE60: perform extrapolation
441
+ eps.n += 1;
442
+ eps.epstab[eps.n] = area;
443
+ const [reseps, abseps] = dqelg(eps);
444
+ ktmin += 1;
445
+ if (ktmin > 5 && abserr < 1e-3 * errsum)
446
+ ier = 5;
447
+ if (abseps < abserr) {
448
+ ktmin = 0;
449
+ abserr = abseps;
450
+ result = reseps;
451
+ correc = erlarg;
452
+ ertest = Math.max(epsabs, epsrel * Math.abs(reseps));
453
+ if (abserr <= ertest)
454
+ break;
455
+ }
456
+ // LINE70: prepare bisection of the smallest interval
457
+ if (eps.n === 0)
458
+ noext = true;
459
+ if (ier === 5)
460
+ break;
461
+ maxerr = iord[0];
462
+ errmax = elist[maxerr];
463
+ nrmax = 0;
464
+ extrap = false;
465
+ small *= 0.5;
466
+ erlarg = errsum;
467
+ }
468
+ let label;
469
+ if (sumAll || abserr === OFLOW)
470
+ label = 115;
471
+ else if (ier + ierror === 0)
472
+ label = 110;
473
+ else {
474
+ if (ierror === 3)
475
+ abserr += correc;
476
+ if (ier === 0)
477
+ ier = 3;
478
+ if (result !== 0 && area !== 0)
479
+ label = 105;
480
+ else if (abserr > errsum)
481
+ label = 115;
482
+ else if (area === 0)
483
+ label = 130;
484
+ else
485
+ label = 110;
486
+ }
487
+ if (label === 105)
488
+ label = abserr / Math.abs(result) > errsum / Math.abs(area) ? 115 : 110;
489
+ if (label === 110) {
490
+ // test on divergence
491
+ if (!(ksgn === -1 && Math.max(Math.abs(result), Math.abs(area)) <= defabs * 0.01)) {
492
+ if (0.01 > result / area || result / area > 100 || errsum > Math.abs(area))
493
+ ier = 6;
494
+ }
495
+ }
496
+ else if (label === 115) {
497
+ // compute the global integral sum
498
+ result = 0;
499
+ for (let k = 0; k <= L; k++)
500
+ result += rlist[k];
501
+ abserr = errsum;
502
+ }
503
+ if (ier > 2)
504
+ ier -= 1;
505
+ return { result, abserr, neval: neval(last), ier, last };
506
+ }
507
+ /**
508
+ * Integral of f over the finite interval [a, b] (QUADPACK dqagse: 21-point Gauss-Kronrod rule, bisection of the
509
+ * interval with the largest error, epsilon-algorithm extrapolation).
510
+ *
511
+ * @param limit - Maximum number of subintervals (R's `subdivisions`).
512
+ */
513
+ export function dqagse(f, a, b, epsabs, epsrel, limit) {
514
+ return adaptive((x, y) => dqk21(f, x, y), a, b, epsabs, epsrel, limit, Math.abs(b - a) * 0.375, (last) => 42 * last - 21);
515
+ }
516
+ /**
517
+ * Integral of f over an infinite range (QUADPACK dqagie): (bound, inf) for inf = 1, (-inf, bound) for inf = -1,
518
+ * (-inf, inf) for inf = 2, mapped to (0, 1] by x = bound + (1 - t) / t.
519
+ *
520
+ * @param limit - Maximum number of subintervals (R's `subdivisions`).
521
+ */
522
+ export function dqagie(f, bound, inf, epsabs, epsrel, limit) {
523
+ const boun = inf === 2 ? 0 : bound;
524
+ return adaptive((x, y) => dqk15i(f, boun, inf, x, y), 0, 1, epsabs, epsrel, limit, 0.375, (last) => (30 * last - 15) * (inf === 2 ? 2 : 1));
525
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Equally spaced values from `from` to `to`, both ends exact (R's seq(from, to, length.out = n)). A fractional `n` is
3
+ * rounded up, as R's seq does.
4
+ */
5
+ export declare function seqLength(from: number, to: number, n: number): number[];
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Equally spaced values from `from` to `to`, both ends exact (R's seq(from, to, length.out = n)). A fractional `n` is
3
+ * rounded up, as R's seq does.
4
+ */
5
+ export function seqLength(from, to, n) {
6
+ const len = Math.ceil(n);
7
+ if (len === 1)
8
+ return [from];
9
+ const by = (to - from) / (len - 1);
10
+ const out = Array.from({ length: len }, (_, i) => from + i * by);
11
+ if (len > 0)
12
+ out[len - 1] = to;
13
+ return out;
14
+ }
@@ -1,75 +1,14 @@
1
- // Univariate root finding (zeroin from Brent 1973, "Algorithms for Minimization without Derivatives", chapter 4).
2
- // Combines bisection, the secant method and inverse quadratic interpolation, returning a point in [a, b] where f changes sign.
3
- // The procedure follows the ALGOL 60 procedure in Brent's book (the same as netlib's zeroin.f).
1
+ // Univariate root finding for internal callers (the confidence limits and estimates of fisherTest, wilcoxTest and
2
+ // mantelhaenTest): a thin wrapper around the public uniroot (src/numerics/integrate.ts, Brent's method with R's
3
+ // conventions), returning just the root.
4
+ import { uniroot as unirootFull } from "../numerics/integrate.js";
4
5
  export const UNIROOT_TOL = Math.pow(Number.EPSILON, 0.25);
5
6
  /** f(lower) and f(upper) must differ in sign. Returns one root */
6
7
  export function uniroot(f, lower, upper, opts = {}) {
7
- const tol = opts.tol ?? UNIROOT_TOL;
8
- const maxIter = opts.maxIter ?? 1000;
9
- let a = lower;
10
- let b = upper;
11
- let fa = opts.fLower ?? f(a);
12
- let fb = opts.fUpper ?? f(b);
13
- if (Number.isNaN(fa) || Number.isNaN(fb))
14
- throw new Error("the function value at an end point is NaN");
15
- if (fa === 0)
16
- return a;
17
- if (fb === 0)
18
- return b;
19
- if (fa > 0 === fb > 0)
20
- throw new Error("the function values at the end points have the same sign");
21
- let c = a;
22
- let fc = fa;
23
- for (let iter = 0; iter < maxIter; iter++) {
24
- const prevStep = b - a;
25
- if (Math.abs(fc) < Math.abs(fb)) {
26
- a = b;
27
- b = c;
28
- c = a;
29
- fa = fb;
30
- fb = fc;
31
- fc = fa;
32
- }
33
- const tolAct = 2 * Number.EPSILON * Math.abs(b) + tol / 2;
34
- let newStep = (c - b) / 2;
35
- if (Math.abs(newStep) <= tolAct || fb === 0)
36
- return b;
37
- // Try interpolation (when the previous step was large enough and b is closer to the root)
38
- if (Math.abs(prevStep) >= tolAct && Math.abs(fa) > Math.abs(fb)) {
39
- const cb = c - b;
40
- let p;
41
- let q;
42
- if (a === c) {
43
- // Secant method
44
- const t1 = fb / fa;
45
- p = cb * t1;
46
- q = 1 - t1;
47
- }
48
- else {
49
- // Inverse quadratic interpolation
50
- const qq = fa / fc;
51
- const t1 = fb / fc;
52
- const t2 = fb / fa;
53
- p = t2 * (cb * qq * (qq - t1) - (b - a) * (t1 - 1));
54
- q = (qq - 1) * (t1 - 1) * (t2 - 1);
55
- }
56
- if (p > 0)
57
- q = -q;
58
- else
59
- p = -p;
60
- if (p < 0.75 * cb * q - Math.abs(tolAct * q) / 2 && p < Math.abs((prevStep * q) / 2))
61
- newStep = p / q;
62
- }
63
- if (Math.abs(newStep) < tolAct)
64
- newStep = newStep > 0 ? tolAct : -tolAct;
65
- a = b;
66
- fa = fb;
67
- b += newStep;
68
- fb = f(b);
69
- if ((fb > 0 && fc > 0) || (fb < 0 && fc < 0)) {
70
- c = a;
71
- fc = fa;
72
- }
73
- }
74
- return b;
8
+ const options = { tol: opts.tol ?? UNIROOT_TOL, maxiter: opts.maxIter ?? 1000 };
9
+ if (opts.fLower !== undefined)
10
+ options.fLower = opts.fLower;
11
+ if (opts.fUpper !== undefined)
12
+ options.fUpper = opts.fUpper;
13
+ return unirootFull(f, lower, upper, options).root;
75
14
  }
@@ -0,0 +1,45 @@
1
+ import type { MatrixInput } from "./matrix.js";
2
+ /** Result of {@link lu}: `x = P L U`. */
3
+ export interface LuResult {
4
+ /** Permutation matrix (m x m). */
5
+ P: number[][];
6
+ /** Unit lower triangular (m x min(m, n)). */
7
+ L: number[][];
8
+ /** Upper triangular (min(m, n) x n). */
9
+ U: number[][];
10
+ }
11
+ /**
12
+ * LU decomposition with partial (row) pivoting, `x = P L U`. Equivalent to `Matrix::expand(Matrix::lu(x))`
13
+ * (LAPACK's dgetrf).
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const { P, L, U } = lu([[1, 2], [3, 4]]); // P = [[0, 1], [1, 0]], L = [[1, 0], [1/3, 1]], U = [[3, 4], [0, 2/3]]
18
+ * ```
19
+ */
20
+ export declare function lu(x: MatrixInput): LuResult;
21
+ /** Options for {@link prcomp}. */
22
+ export interface PrcompOptions {
23
+ /** Centre the columns (default `true`), or subtract the given values. */
24
+ center?: boolean | readonly number[];
25
+ /** Scale the columns to unit variance (default `false`), or divide by the given values. */
26
+ scale?: boolean | readonly number[];
27
+ }
28
+ /** Result of {@link prcomp}. */
29
+ export interface PrcompResult {
30
+ /** Standard deviations of the principal components. */
31
+ sdev: number[];
32
+ /** Loadings: the eigenvectors as columns (variables x components). */
33
+ rotation: number[][];
34
+ /** Scores: the data in the coordinates of the components (observations x components). */
35
+ x: number[][];
36
+ /** The centring used, when any. */
37
+ center?: number[];
38
+ /** The scaling used, when any. */
39
+ scale?: number[];
40
+ }
41
+ /**
42
+ * Principal components of the columns of `x` (observations as rows). Equivalent to R's `prcomp()`. The sign of each
43
+ * component is arbitrary, as in R.
44
+ */
45
+ export declare function prcomp(x: MatrixInput, options?: PrcompOptions): PrcompResult;