@numero-uno-jpn/rstats 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/CHANGELOG.md +125 -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-6R7YDHA2.js +2 -0
  6. package/dist/chunks/chunk-AFKYWGKN.js +2 -0
  7. package/dist/chunks/chunk-ANYGLFT4.js +2 -0
  8. package/dist/chunks/chunk-BI5RSUZ2.js +2 -0
  9. package/dist/chunks/chunk-J3SNI7KY.js +2 -0
  10. package/dist/chunks/chunk-JCZAB6C5.js +2 -0
  11. package/dist/chunks/chunk-MVG5LUZU.js +2 -0
  12. package/dist/chunks/chunk-PTPPP4TI.js +2 -0
  13. package/dist/chunks/chunk-QNQJA6KZ.js +2 -0
  14. package/dist/chunks/chunk-SXSJXDD3.js +2 -0
  15. package/dist/chunks/chunk-UIOAWLAN.js +2 -0
  16. package/dist/chunks/chunk-XYXHHF5L.js +2 -0
  17. package/dist/chunks/chunk-YKN6OGJL.js +2 -0
  18. package/dist/chunks/chunk-ZW52Z3GY.js +2 -0
  19. package/dist/chunks/chunk-ZYA4ISAT.js +2 -0
  20. package/dist/descriptive/basic.d.ts +73 -0
  21. package/dist/descriptive/basic.js +256 -0
  22. package/dist/descriptive/cov.d.ts +39 -0
  23. package/dist/descriptive/cov.js +105 -0
  24. package/dist/descriptive/hist.d.ts +64 -0
  25. package/dist/descriptive/hist.js +304 -0
  26. package/dist/descriptive/moments.d.ts +63 -0
  27. package/dist/descriptive/moments.js +158 -0
  28. package/dist/descriptive/quantile.d.ts +50 -0
  29. package/dist/descriptive/quantile.js +131 -0
  30. package/dist/descriptive/rank.d.ts +23 -0
  31. package/dist/descriptive/rank.js +72 -0
  32. package/dist/descriptive/table.d.ts +54 -0
  33. package/dist/descriptive/table.js +160 -0
  34. package/dist/descriptive.js +2 -0
  35. package/dist/distributions/arcsine.d.ts +24 -0
  36. package/dist/distributions/arcsine.js +73 -0
  37. package/dist/distributions/invgamma.d.ts +29 -0
  38. package/dist/distributions/invgamma.js +85 -0
  39. package/dist/distributions/kumaraswamy.d.ts +17 -0
  40. package/dist/distributions/kumaraswamy.js +96 -0
  41. package/dist/distributions/laplace.d.ts +23 -0
  42. package/dist/distributions/laplace.js +69 -0
  43. package/dist/distributions/multinomial.d.ts +24 -0
  44. package/dist/distributions/multinomial.js +70 -0
  45. package/dist/distributions/nbeta.js +28 -10
  46. package/dist/distributions/nchisq.js +20 -3
  47. package/dist/distributions/pareto.d.ts +23 -0
  48. package/dist/distributions/pareto.js +76 -0
  49. package/dist/distributions/summary.d.ts +25 -0
  50. package/dist/distributions/summary.js +289 -0
  51. package/dist/distributions/triangular.d.ts +25 -0
  52. package/dist/distributions/triangular.js +87 -0
  53. package/dist/distributions.js +2 -0
  54. package/dist/entries/descriptive.d.ts +7 -0
  55. package/dist/entries/descriptive.js +8 -0
  56. package/dist/entries/distributions.d.ts +42 -0
  57. package/dist/entries/distributions.js +42 -0
  58. package/dist/entries/linalg.d.ts +11 -0
  59. package/dist/entries/linalg.js +11 -0
  60. package/dist/entries/numerics.d.ts +8 -0
  61. package/dist/entries/numerics.js +6 -0
  62. package/dist/entries/special.d.ts +10 -0
  63. package/dist/entries/special.js +10 -0
  64. package/dist/entries/tests.d.ts +21 -0
  65. package/dist/entries/tests.js +20 -0
  66. package/dist/htest/ansariTest.d.ts +30 -0
  67. package/dist/htest/ansariTest.js +253 -0
  68. package/dist/htest/aov.d.ts +85 -0
  69. package/dist/htest/aov.js +178 -0
  70. package/dist/htest/binomTest.d.ts +36 -0
  71. package/dist/htest/binomTest.js +121 -0
  72. package/dist/htest/chisqTest.d.ts +34 -2
  73. package/dist/htest/chisqTest.js +140 -37
  74. package/dist/htest/cor.d.ts +14 -0
  75. package/dist/htest/cor.js +4 -3
  76. package/dist/htest/core/ansari.d.ts +25 -0
  77. package/dist/htest/core/ansari.js +186 -0
  78. package/dist/htest/core/kolmogorov.d.ts +26 -0
  79. package/dist/htest/core/kolmogorov.js +221 -0
  80. package/dist/htest/core/mantelhaen.d.ts +26 -0
  81. package/dist/htest/core/mantelhaen.js +145 -0
  82. package/dist/htest/core/shapiro.d.ts +24 -0
  83. package/dist/htest/core/shapiro.js +142 -0
  84. package/dist/htest/core/simulate.d.ts +2 -0
  85. package/dist/htest/core/simulate.js +16 -0
  86. package/dist/htest/core/smirnov.d.ts +20 -0
  87. package/dist/htest/core/smirnov.js +75 -0
  88. package/dist/htest/fisherTest.d.ts +12 -1
  89. package/dist/htest/fisherTest.js +187 -127
  90. package/dist/htest/groups.d.ts +20 -0
  91. package/dist/htest/groups.js +62 -0
  92. package/dist/htest/ksTest.d.ts +31 -0
  93. package/dist/htest/ksTest.js +106 -0
  94. package/dist/htest/mantelhaenTest.d.ts +26 -0
  95. package/dist/htest/mantelhaenTest.js +196 -0
  96. package/dist/htest/mcnemarTest.d.ts +26 -0
  97. package/dist/htest/mcnemarTest.js +99 -0
  98. package/dist/htest/onewayTest.d.ts +26 -0
  99. package/dist/htest/onewayTest.js +97 -0
  100. package/dist/htest/pairwise.d.ts +64 -0
  101. package/dist/htest/pairwise.js +152 -0
  102. package/dist/htest/poissonTest.d.ts +35 -0
  103. package/dist/htest/poissonTest.js +170 -0
  104. package/dist/htest/propTest.d.ts +37 -0
  105. package/dist/htest/propTest.js +164 -0
  106. package/dist/htest/rankTests.d.ts +52 -0
  107. package/dist/htest/rankTests.js +234 -0
  108. package/dist/htest/types.d.ts +2 -0
  109. package/dist/htest/types.js +6 -0
  110. package/dist/htest/varTest.d.ts +48 -0
  111. package/dist/htest/varTest.js +118 -0
  112. package/dist/htest/wilcoxTest.js +136 -174
  113. package/dist/index.d.ts +6 -50
  114. package/dist/index.js +8 -50
  115. package/dist/integrate/quadpack.d.ts +29 -0
  116. package/dist/integrate/quadpack.js +525 -0
  117. package/dist/internal/seq.d.ts +5 -0
  118. package/dist/internal/seq.js +14 -0
  119. package/dist/internal/uniroot.js +10 -71
  120. package/dist/linalg/lu.d.ts +45 -0
  121. package/dist/linalg/lu.js +88 -0
  122. package/dist/linalg/ops.d.ts +90 -0
  123. package/dist/linalg/ops.js +320 -0
  124. package/dist/linalg.js +2 -0
  125. package/dist/models/lm.d.ts +98 -0
  126. package/dist/models/lm.js +187 -0
  127. package/dist/numerics/deriv.d.ts +37 -0
  128. package/dist/numerics/deriv.js +150 -0
  129. package/dist/numerics/integrate.d.ts +91 -0
  130. package/dist/numerics/integrate.js +264 -0
  131. package/dist/numerics/interp.d.ts +103 -0
  132. package/dist/numerics/interp.js +581 -0
  133. package/dist/numerics/ode.d.ts +23 -0
  134. package/dist/numerics/ode.js +59 -0
  135. package/dist/numerics.js +2 -0
  136. package/dist/optim/localmin.d.ts +16 -0
  137. package/dist/optim/localmin.js +97 -0
  138. package/dist/random/beta.js +1 -1
  139. package/dist/random/binomial.js +1 -1
  140. package/dist/random/continuous.js +1 -1
  141. package/dist/random/extra.d.ts +33 -0
  142. package/dist/random/extra.js +62 -0
  143. package/dist/random/gamma.js +1 -1
  144. package/dist/random/hyper.js +1 -1
  145. package/dist/random/mt.js +0 -1
  146. package/dist/random/poisson.js +0 -1
  147. package/dist/random/r2dtable.d.ts +26 -0
  148. package/dist/random/r2dtable.js +153 -0
  149. package/dist/random/rank.js +1 -1
  150. package/dist/random/rng.js +1 -1
  151. package/dist/random/sample.js +1 -1
  152. package/dist/rstats.js +3 -2
  153. package/dist/special/besselIK.d.ts +38 -0
  154. package/dist/special/besselIK.js +315 -0
  155. package/dist/special/besselJY.d.ts +32 -0
  156. package/dist/special/besselJY.js +508 -0
  157. package/dist/special/besselSeries.d.ts +8 -0
  158. package/dist/special/besselSeries.js +24 -0
  159. package/dist/special/beta.js +22 -5
  160. package/dist/special/choose.d.ts +75 -0
  161. package/dist/special/choose.js +233 -0
  162. package/dist/special/erf.d.ts +28 -2
  163. package/dist/special/erf.js +37 -10
  164. package/dist/special/extra.d.ts +47 -0
  165. package/dist/special/extra.js +220 -0
  166. package/dist/special/igam.d.ts +4 -0
  167. package/dist/special/igam.js +57 -2
  168. package/dist/special/incbet.js +77 -22
  169. package/dist/special/lgammaDiff.d.ts +7 -0
  170. package/dist/special/lgammaDiff.js +16 -3
  171. package/dist/special/log1p.d.ts +53 -0
  172. package/dist/special/log1p.js +93 -0
  173. package/dist/special/psigamma.d.ts +48 -0
  174. package/dist/special/psigamma.js +318 -0
  175. package/dist/special/rCompatible.d.ts +8 -0
  176. package/dist/special/rCompatible.js +1 -0
  177. package/dist/special.js +2 -0
  178. package/dist/tests.js +3 -0
  179. package/package.json +26 -1
@@ -0,0 +1,318 @@
1
+ // Digamma and polygamma functions: digamma, trigamma, psigamma.
2
+ //
3
+ // digamma is ported from xsf include/xsf/cephes/psi.h and include/xsf/digamma.h (xsf commit c6a3afe; the
4
+ // rational approximation on [1, 2] in psi.h is from Boost.Math, (C) John Maddock 2006, BSL-1.0).
5
+ // Cephes Math Library Release 2.8, Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier.
6
+ // xsf Copyright (c) 2024, SciPy. BSD-3-Clause. See THIRD_PARTY_NOTICES.md.
7
+ //
8
+ // The derivatives (deriv >= 1) are written for rStats.js:
9
+ // psi^(n)(x) = (-1)^(n+1) n! zeta(n + 1, x) (x > 0)
10
+ // with the Hurwitz zeta function summed by Euler-Maclaurin as in xsf include/xsf/cephes/zeta.h, but scaled by
11
+ // x^(n+1) so that n! / x^(n+1) and the sum can be combined without overflow, and with direct terms up to
12
+ // a >= n + 1 so that the Bernoulli corrections converge for large n. For x < 0 the reflection formula
13
+ // psi^(n)(x) = (-1)^n psi^(n)(1 - x) - pi^(n+1) P_n(cot(pi x))
14
+ // is used, where d^n/dx^n cot(pi x) = pi^n P_n(cot(pi x)), P_0(t) = t, P_(k+1)(t) = -(1 + t^2) P_k'(t)
15
+ // (P_n has terms of one parity only, all of the same sign, so it is evaluated without cancellation).
16
+ import { roundHalfEven } from "../internal/integer.js";
17
+ import { EULER, MACHEP } from "./constants.js";
18
+ import { polevl } from "./polevl.js";
19
+ const PSI_A = [
20
+ 8.33333333333333333333e-2, -2.10927960927960927961e-2, 7.57575757575757575758e-3, -4.16666666666666666667e-3,
21
+ 3.96825396825396825397e-3, -8.33333333333333333333e-3, 8.33333333333333333333e-2,
22
+ ];
23
+ const PSI_Y = 0.99558162689208984; // a float constant in Boost
24
+ const PSI_ROOT1 = 1569415565.0 / 1073741824.0;
25
+ const PSI_ROOT2 = 381566830.0 / 1073741824.0 / 1073741824.0;
26
+ const PSI_ROOT3 = 0.9016312093258695918615325266959189453125e-19;
27
+ const PSI_P = [
28
+ -0.0020713321167745952, -0.045251321448739056, -0.28919126444774784, -0.65031853770896507, -0.32555031186804491,
29
+ 0.25479851061131551,
30
+ ];
31
+ const PSI_Q = [
32
+ -0.55789841321675513e-6, 0.0021284987017821144, 0.054151797245674225, 0.43593529692665969, 1.4606242909763515,
33
+ 2.0767117023730469, 1.0,
34
+ ];
35
+ /** Rational approximation on [1, 2]: digamma(x) = (x - root) (Y + R(x - 1)) */
36
+ function digammaImp12(x) {
37
+ let g = x - PSI_ROOT1;
38
+ g -= PSI_ROOT2;
39
+ g -= PSI_ROOT3;
40
+ const r = polevl(x - 1, PSI_P) / polevl(x - 1, PSI_Q);
41
+ return g * PSI_Y + g * r;
42
+ }
43
+ function psiAsy(x) {
44
+ let y = 0;
45
+ if (x < 1e17) {
46
+ const z = 1 / (x * x);
47
+ y = z * polevl(z, PSI_A);
48
+ }
49
+ return Math.log(x) - 0.5 / x - y;
50
+ }
51
+ /** Cephes psi */
52
+ function psi(x) {
53
+ let y = 0;
54
+ if (Number.isNaN(x) || x === Infinity)
55
+ return x;
56
+ if (x === -Infinity)
57
+ return NaN;
58
+ if (x === 0)
59
+ return NaN;
60
+ if (x < 0) {
61
+ // argument reduction before evaluating tan(pi x)
62
+ const r = x - Math.trunc(x);
63
+ if (r === 0)
64
+ return NaN;
65
+ y = -Math.PI / Math.tan(Math.PI * r);
66
+ x = 1 - x;
67
+ }
68
+ // positive integers up to 10
69
+ if (x <= 10 && x === Math.floor(x)) {
70
+ const n = x;
71
+ for (let i = 1; i < n; i++)
72
+ y += 1 / i;
73
+ return y - EULER;
74
+ }
75
+ // use the recurrence to move x into [1, 2]
76
+ if (x < 1) {
77
+ y -= 1 / x;
78
+ x += 1;
79
+ }
80
+ else if (x < 10) {
81
+ while (x > 2) {
82
+ x -= 1;
83
+ y += 1 / x;
84
+ }
85
+ }
86
+ if (x >= 1 && x <= 2)
87
+ return y + digammaImp12(x);
88
+ return y + psiAsy(x);
89
+ }
90
+ // Series around the negative root closest to 0 (xsf digamma.h; values computed with mpmath there)
91
+ const NEGROOT = -0.504083008264455409;
92
+ const NEGROOTVAL = 7.2897639029768949e-17;
93
+ /** zeta(n + 1, NEGROOT) for n = 1, 2, ... (the series coefficients), computed on first use */
94
+ let negrootZeta;
95
+ function digammaNearNegroot(x) {
96
+ negrootZeta ??= Array.from({ length: 100 }, (_, i) => hurwitzZeta(i + 2, NEGROOT));
97
+ let res = NEGROOTVAL;
98
+ let coeff = -1;
99
+ const z = x - NEGROOT;
100
+ for (let n = 1; n < 100; n++) {
101
+ coeff *= -z;
102
+ const term = coeff * negrootZeta[n - 1];
103
+ res += term;
104
+ if (Math.abs(term) < Number.EPSILON * Math.abs(res))
105
+ break;
106
+ }
107
+ return res;
108
+ }
109
+ /** Expansion coefficients (2k)! / B_2k for the Euler-Maclaurin formula (xsf cephes/zeta.h) */
110
+ const ZETA_A = [
111
+ 12.0, -720.0, 30240.0, -1209600.0, 47900160.0, -1.8924375803183791606e9, 7.47242496e10, -2.950130727918164224e12,
112
+ 1.1646782814350067249e14, -4.5979787224074726105e15, 1.8152105401943546773e17, -7.1661652561756670113e18,
113
+ ];
114
+ /**
115
+ * Hurwitz zeta function zeta(s, q) times q^s, for s > 1 and q > 0 (Euler-Maclaurin summation as in Cephes, with
116
+ * every term scaled by q^s). Direct terms are added until the term is negligible or a = q + i exceeds both 9 and s,
117
+ * so that the Bernoulli corrections, whose ratio is about ((s + 2i) / (2 pi a))^2, converge
118
+ */
119
+ function hurwitzZetaScaled(s, q) {
120
+ let sum = 1;
121
+ let a = q;
122
+ let b = 1;
123
+ let i = 0;
124
+ // Above 1e8 no direct terms: the expansion at a = q itself, q / (s - 1) + 1/2 + s / (12 q) - ... (cutting it after
125
+ // the 1/2 left out s / (12 q), 1e-14 relative at q = 1e8, s = 39)
126
+ while (q <= 1e8 && (i < 9 || a <= 9 || a < s)) {
127
+ i += 1;
128
+ a += 1;
129
+ b = Math.exp(-s * Math.log1p(i / q)); // (q / a)^s
130
+ sum += b;
131
+ if (Math.abs(b / sum) < MACHEP)
132
+ return sum;
133
+ }
134
+ const w = a;
135
+ sum += (b * w) / (s - 1);
136
+ sum -= 0.5 * b;
137
+ let fac = 1;
138
+ let k = 0;
139
+ for (let j = 0; j < 12; j++) {
140
+ fac *= s + k;
141
+ b /= w;
142
+ const t = (fac * b) / ZETA_A[j];
143
+ sum += t;
144
+ if (Math.abs(t / sum) < MACHEP)
145
+ break;
146
+ k += 1;
147
+ fac *= s + k;
148
+ b /= w;
149
+ k += 1;
150
+ }
151
+ return sum;
152
+ }
153
+ /**
154
+ * Hurwitz zeta function zeta(s, q) for integer s > 1 and any q that is not a non-positive integer, unscaled as in
155
+ * Cephes (negative q are allowed for integer s: the direct sum runs until q + i > 9). Used for the series
156
+ * coefficients around the negative root of digamma
157
+ */
158
+ function hurwitzZeta(s, q) {
159
+ let sum = Math.pow(q, -s);
160
+ let a = q;
161
+ let b = 0;
162
+ let i = 0;
163
+ while (i < 9 || a <= 9 || a < s) {
164
+ i += 1;
165
+ a += 1;
166
+ b = Math.pow(a, -s);
167
+ sum += b;
168
+ if (Math.abs(b / sum) < MACHEP)
169
+ return sum;
170
+ }
171
+ const w = a;
172
+ sum += (b * w) / (s - 1);
173
+ sum -= 0.5 * b;
174
+ let fac = 1;
175
+ let k = 0;
176
+ for (let j = 0; j < 12; j++) {
177
+ fac *= s + k;
178
+ b /= w;
179
+ const t = (fac * b) / ZETA_A[j];
180
+ sum += t;
181
+ if (Math.abs(t / sum) < MACHEP)
182
+ break;
183
+ k += 1;
184
+ fac *= s + k;
185
+ b /= w;
186
+ k += 1;
187
+ }
188
+ return sum;
189
+ }
190
+ /** psi^(n)(x) for x > 0, n >= 1: (-1)^(n+1) n! / x^(n+1) * [x^(n+1) zeta(n + 1, x)] */
191
+ function polygammaPositive(n, x) {
192
+ if (x === Infinity)
193
+ return n % 2 === 0 ? -0 : 0;
194
+ // n! / x^n as a running product (no overflow before the result does), then the sum divided by x
195
+ let p = 1;
196
+ for (let k = 1; k <= n; k++)
197
+ p *= k / x;
198
+ const v = p * (hurwitzZetaScaled(n + 1, x) / x);
199
+ return n % 2 === 0 ? -v : v;
200
+ }
201
+ /** Coefficients of P_n in t^2: P_n(t) = t^(n+1 mod 2) * sum_j c_j t^(2j), computed on first use */
202
+ const cotDerivCache = [];
203
+ function cotDerivPoly(n) {
204
+ // Dense coefficients of P_k(t) by recurrence, then keep those of the parity of n + 1
205
+ if (cotDerivCache[n] !== undefined)
206
+ return cotDerivCache[n];
207
+ let p = [0, 1]; // P_0 = t
208
+ for (let k = 0; k < n; k++) {
209
+ // P' then -(1 + t^2) P'
210
+ const d = p.slice(1).map((c, i) => c * (i + 1));
211
+ const next = new Array(d.length + 2).fill(0);
212
+ d.forEach((c, i) => {
213
+ next[i] = next[i] - c;
214
+ next[i + 2] = next[i + 2] - c;
215
+ });
216
+ p = next;
217
+ }
218
+ const parity = (n + 1) % 2;
219
+ const coef = [];
220
+ for (let i = parity; i < p.length; i += 2)
221
+ coef.push(p[i]);
222
+ cotDerivCache[n] = coef;
223
+ return coef;
224
+ }
225
+ /** P_n(t) (all coefficients of one sign, so no cancellation) */
226
+ function cotDeriv(n, t) {
227
+ const c = cotDerivPoly(n);
228
+ const t2 = t * t;
229
+ // Horner from the leading coefficient (starting from 0 would give 0 * Infinity = NaN when t^2 overflows)
230
+ let s = c[c.length - 1];
231
+ for (let j = c.length - 2; j >= 0; j--)
232
+ s = s * t2 + c[j];
233
+ return (n + 1) % 2 === 1 ? s * t : s;
234
+ }
235
+ /**
236
+ * cot(pi x), with x reduced exactly to r in [-1/2, 1/2]. The cosine is taken as sin(pi (1/2 - |r|)), where 1/2 - |r|
237
+ * is exact near 1/2, so that cot is exactly 0 at half-integers and keeps its relative accuracy near them (P_n
238
+ * multiplies the error of a cot that should be 0 by its huge linear coefficient: 1e174 instead of -6e117 for
239
+ * psigamma(-1.5, 100))
240
+ */
241
+ function cotpi(x) {
242
+ const r = x - Math.round(x);
243
+ const c = Math.sin(Math.PI * (0.5 - Math.abs(r)));
244
+ return c / Math.sin(Math.PI * r);
245
+ }
246
+ function polygamma(n, x, rCompatible) {
247
+ if (Number.isNaN(x))
248
+ return NaN;
249
+ // R treats -Infinity like the poles (it equals its own rounding)
250
+ if (x === -Infinity)
251
+ return rCompatible && n % 2 === 1 ? Infinity : NaN;
252
+ if (x <= 0 && x === Math.floor(x))
253
+ return n % 2 === 1 ? Infinity : NaN;
254
+ if (x > 0)
255
+ return polygammaPositive(n, x);
256
+ const refl = polygammaPositive(n, 1 - x);
257
+ return (n % 2 === 0 ? refl : -refl) - Math.pow(Math.PI, n + 1) * cotDeriv(n, cotpi(x));
258
+ }
259
+ /**
260
+ * Digamma function, the derivative of `lgamma`. Equivalent to R's `digamma(x)` for a single number.
261
+ *
262
+ * @param x - Argument.
263
+ * @returns psi(x) (`NaN` at zero and the negative integers).
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * digamma(1); // -0.5772156649015329 (minus Euler's constant)
268
+ * ```
269
+ */
270
+ export function digamma(x) {
271
+ if (Math.abs(x - NEGROOT) < 0.3)
272
+ return digammaNearNegroot(x);
273
+ return psi(x);
274
+ }
275
+ /**
276
+ * Trigamma function, the second derivative of `lgamma`. Equivalent to R's `trigamma(x)` for a single number.
277
+ *
278
+ * `trigamma(-Infinity)` is `Infinity` as in R (`NaN` with `rCompatible: false`).
279
+ *
280
+ * @param x - Argument.
281
+ * @param options - `rCompatible` (default `true`).
282
+ * @returns psi'(x) (`Infinity` at zero and the negative integers).
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * trigamma(1); // 1.6449340668482264 (pi^2 / 6)
287
+ * ```
288
+ */
289
+ export function trigamma(x, options) {
290
+ return polygamma(1, x, options?.rCompatible ?? true);
291
+ }
292
+ /**
293
+ * Polygamma function, the derivative of order `deriv` of `digamma`. Equivalent to R's
294
+ * `psigamma(x, deriv)` for a single number.
295
+ *
296
+ * `psigamma(-Infinity, deriv)` is `Infinity` for odd `deriv` as in R, which treats it like the poles
297
+ * (`NaN` with `rCompatible: false`). Unlike R, which returns `NaN` for `x < 0` with `deriv >= 6` and
298
+ * where the result overflows, the value is computed there.
299
+ *
300
+ * @param x - Argument.
301
+ * @param deriv - Order of the derivative, an integer from 0 to 100 (rounded half to even; default 0, digamma).
302
+ * @param options - `rCompatible` (default `true`).
303
+ * @returns psi^(deriv)(x). At zero and the negative integers, `Infinity` for odd `deriv` and `NaN` for even.
304
+ * `NaN` if `deriv` is outside 0 to 100.
305
+ *
306
+ * @example
307
+ * ```ts
308
+ * psigamma(2, 2); // -0.40411380631918853 (-2 (zeta(3) - 1))
309
+ * ```
310
+ */
311
+ export function psigamma(x, deriv = 0, options) {
312
+ const n = roundHalfEven(deriv);
313
+ if (Number.isNaN(n) || n < 0 || n > 100)
314
+ return NaN;
315
+ if (n === 0)
316
+ return digamma(x);
317
+ return polygamma(n, x, options?.rCompatible ?? true);
318
+ }
@@ -0,0 +1,8 @@
1
+ /** Option shared by functions whose edge cases R defines differently from the mathematical function */
2
+ export interface RCompatibleOptions {
3
+ /**
4
+ * `true` (default) returns what R returns at the edge cases listed for each function; `false` returns the
5
+ * mathematical value there instead (see docs/differences-from-r.md).
6
+ */
7
+ rCompatible?: boolean;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ /*! rStats.js 0.7.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{e as ht}from"./chunks/chunk-BI5RSUZ2.js";import{a as wt,b as Lt,c as _t,d as Et}from"./chunks/chunk-J3SNI7KY.js";import{c as G,d as St,e as jt}from"./chunks/chunk-YKN6OGJL.js";import{B as Dt,C as Ft,b as $,c as qt,h as ft,k as d,l as q,m as U,n as _,o as C,p as Z,q as ct,u as z,v as lt,w as mt,x as ut,z as v}from"./chunks/chunk-ANYGLFT4.js";import{b as E,d as j,f as T,g as J,h as at,i as O,k as L}from"./chunks/chunk-ZW52Z3GY.js";var Mt=22250738585072014e-324;function W(t,e,n){if(t<10)return Math.pow(e/2,t)/q(t+1)*n;let s=e/2,r=z(t,s,!1);if(r>=Mt&&s<700){let a=r*Math.exp(s)*n;if(Math.abs(a)>=Mt)return a}let o=z(t,s,!0)+s+Math.log(Math.abs(n));return Math.sign(n)*Math.exp(o)}var D=Number.EPSILON,H=1e6;function pt(t){return Math.expm1(C(t))}function Tt(t,e){let n=pt(t),s=pt(-t),r=Math.log(e/2),o=Math.exp(t*r),a=-r*t,i=Math.abs(t)<D?1:d(t)/(t*Math.PI),c=Math.abs(a)<D?1:Math.sinh(a)/a,l=Math.abs(t)<D?-.5772156649015329:.5/t*(n-s)*i,f=(2+n+s)*i/2,m=(n+1)/(2*o),u=(1+s)*o/2,M=(Math.cosh(a)*l+c*-r*f)/i,p=m,h=1,S=h*M,k=h*p;for(let I=1;I<H&&(M=(I*M+m+u)/(I*I-t*t),m/=I-t,u/=I+t,p=m-I*M,h*=e*e/(4*I),S+=h*M,k+=h*p,!(Math.abs(h*M)<Math.abs(S)*D));I++);return[S,2*k/e]}function At(t,e){let n=Math.sqrt(Number.MIN_VALUE*4503599627370496),s=n,r=n,o=0;for(let a=1;a<H;a++){let i=2*(t+a)/e;s=i+1/s,o=i+o,s===0&&(s=n),o===0&&(o=n),o=1/o;let c=s*o;if(r*=c,Math.abs(c-1)<=2*D)break}return r}function Jt(t,e){let n=t*t-.25,s=2*(e+1),r=1/s,o=r,a=r,i=0,c=1,l=-n,f=l,m=1+f*a;for(let p=2;p<H;p++){n-=2*(p-1),s+=2,r=1/(s+n*r),a*=s*r-1,o+=a;let h=(i-(s-2)*c)/n;if(i=c,c=h,l*=-n/p,f+=l*h,m+=f*a,h<D&&(l*=h,i/=h,c/=h),Math.abs(f*a)<Math.abs(m)*D)break}let u=(.5+t+e+(t*t-.25)*o)/e,M=Math.sqrt(Math.PI/(2*e))/m;return[M,M*u]}function V(t,e){if(!Number.isFinite(e))return e>0?t*(1/0):t*0;for(;e>1e3;)t*=2**1e3,e-=1e3;for(;e<-1e3;)t*=2**-1e3,e+=1e3;return t*2**e}function bt(t,e){let n=Math.round(t),s=t-n,r,o;if(s===-.5)r=o=Math.sqrt(Math.PI/(2*e));else if(e<=2){let[i,c]=Tt(s,e),l=Math.exp(e);r=i*l,o=c*l}else[r,o]=Jt(s,e);let a=0;for(let i=1;i<=n;i++){let c=2*(s+i)/e,l=Math.log2(Math.abs(o))+Math.log2(c);if(l>900&&Number.isFinite(l)){let m=Math.ceil(l-800);r=V(r,-m),o=V(o,-m),a+=m}let f=c*o+r;r=o,o=f}return{k:r,k1:o,exp2:a}}function Kt(t,e){let n=e*e/4,s=1,r=0;for(let o=1;o<H&&(r+=s,s*=n/o,s/=o+t,!(Math.abs(s)<=D*Math.abs(r)));o++);return W(t,e,r+s)}function Rt(t,e,n){if(t===0||!Number.isFinite(t))return t;let s=t,r=e,o=n>0?700:-700,a=Math.exp(o);for(;Math.abs(n)>700;){n-=o,s*=a;let i=Math.floor(Math.log2(Math.abs(s)));s=V(s,-i),r+=i}return V(s*Math.exp(n),r)}function x(t,e,n){let s=t.ex+n;return s===0?V(t.m,t.e2):Rt(t.m,t.e2,s*e)}function Yt(t,e){if(t===.5)return{m:Math.sqrt(2/(e*Math.PI))*(-Math.expm1(-2*e)/2),e2:0,ex:1};if(t>0&&e/t<.25)return{m:Kt(t,e),e2:0,ex:0};let n=(4*t*t+10)/(8*e);if(n*=n,n*=n,n/=24,n<D*10&&e>100){let i=4*t*t,c=8*e,l=i-1,f=c,m=1-l/f;return l*=i-9,f*=c*2,m+=l/f,l*=i-25,f*=c*3,m-=l/f,{m:m/Math.sqrt(2*e*Math.PI),e2:0,ex:1}}let{k:s,k1:r,exp2:o}=bt(t,e),a=At(t,e);return{m:1/(e*(s*a+r)),e2:-o,ex:1}}function Nt(t,e){let n=4*t*t,s=8*e,r=(n-1)/s,o=r*(n-9)/(2*s),a=o*(n-25)/(3*s),i=a*(n-49)/(4*s),c=1+r+o+a;if(Math.abs(i)<D*Math.abs(c))return{m:c*Math.sqrt(Math.PI)/Math.sqrt(2*e),e2:0,ex:-1};let{k:l,exp2:f}=bt(t,e);return{m:l,e2:f,ex:-1}}function Ct(t,e,n={}){let s=n.exponScaled??!1;if(Number.isNaN(t)||Number.isNaN(e)||t<0||!Number.isFinite(e))return NaN;let r=Math.abs(e),o=e<0&&e!==Math.floor(e);if(t===0)return o?d(r)>0?1/0:-1/0:e===0?1:0;if(t===1/0)return s?0:1/0;let a=s?-1:0,i=x(Yt(r,t),t,a);if(!o)return i;let c=Nt(r,t);return i+x({...c,m:2/Math.PI*d(r)*c.m},t,a)}function Qt(t,e,n={}){let s=n.exponScaled??!1;if(Number.isNaN(t)||Number.isNaN(e)||t<0||!Number.isFinite(e))return NaN;let r=Math.abs(e);return t===0?1/0:t===1/0?0:x(Nt(r,t),t,s?1:0)}var y=Number.EPSILON;function A(t,e){if(!Number.isFinite(e))return e>0?t*(1/0):t*0;for(;e>1e3;)t*=2**1e3,e-=1e3;for(;e<-1e3;)t*=2**-1e3,e+=1e3;return t*2**e}function gt(t,e,n,s,r){let o=s,a=r,i=0;for(let c=1;c<=n;c++){let l=2*(t+c)/e,f=Math.log2(Math.abs(a))+Math.log2(l);if(f>900&&Number.isFinite(f)){let u=Math.ceil(f-800);o=A(o,-u),a=A(a,-u),i+=u}let m=l*a-o;o=a,a=m}return{y:o,y1:a,e2:i}}var K=1e6,g=Math.sqrt(22250738585072014e-324),Ut=Number.MAX_VALUE;function It(t){return Math.expm1(C(t))}function Q(t){return d(.5-Math.abs(t%2))}function dt(t,e){let n=-(e/2)*(e/2),s=1,r=0;for(let o=1;o<K&&(r+=s,s*=n/(o*(o+t)),!(Math.abs(s)<=y*Math.abs(r)));o++);return W(t,e,r+s)}function Vt(t,e){let n=Math.log(e/2),s=0,r=t>=170||709.78/t<Math.abs(n),o;if(r){let u=_(t);if(n=t*n,o=u-Math.log(Math.PI)-n,709.78<o){let M=Math.ceil((o-700)/Math.LN2);o-=M*Math.LN2,s=M}o=-Math.exp(o)}else{let u=q(t);n=Math.pow(e/2,t),Ut*n<u&&(u*=2**-1e3,s=1e3),o=-u/(Math.PI*n)}let a=-(e/2)*(e/2),i=1,c=0;for(let u=1;u<K&&(c+=i,i*=a/(u*(u-t)),!(Math.abs(i)<=y*Math.abs(c)));u++);let l=(c+i)*o,f;if(!r)f=q(-t)*Q(t)*n/Math.PI;else{let u=U(-t);f=Math.exp(u.value+n)*u.sign/Math.PI}i=1;let m=0;for(let u=1;u<K&&(m+=i,i*=a/(u*(u+t)),!(Math.abs(i)<=y*Math.abs(m)));u++);return m+=i,l-=A(f*m,-s),{m:l,e2:s}}function Xt(t,e){let n=1,s=0,r=1,o=8*e,a=1,i=4*t*t,c=1,l=!0;do{c*=(i-a*a)/(r*o),s+=c,r+=1,a+=2;let f=(a*a-i)/(r*o);l=Math.abs(f)<.5,c*=f,n+=c,r+=1,a+=2}while(Math.abs(c)>2*y*n&&l);return l?{p:n,q:s}:void 0}function Bt(t,e){let n=It(t),s=It(-t),r=d(t),o=d(t/2),a=Math.pow(e/2,t),i=Math.log(e/2),c=-i*t,l=Math.abs(c)<y?1:Math.sinh(c)/c,f=Math.abs(t)<y?t*Math.PI*Math.PI/2:2*o*o/t,m=t===0?-.5772156649015329:(n-s)/((1+n)*(1+s)*2*t),u=(2+n+s)/((1+n)*(1+s)*2),M=Math.abs(t)<y?1/Math.PI:t/r,p=(m*Math.cosh(c)-u*i*l)*2*M,h=M/(a*(1+s)),S=M*a/(1+n),k=p+f*S,I=h,F=1,R=F*k,X=F*I,B=t*t,b=-e*e/4;for(let N=1;N<K&&(p=(N*p+h+S)/(N*N-B),h/=N-t,S/=N+t,k=p+f*S,I=h-N*k,F*=b/N,R+=F*k,X+=F*I,!(Math.abs(F*k)<Math.abs(R)*y));N++);return[-R,-2*X/e]}function Ot(t,e){let n=[g,0],s=[g,0],r=[0,0],o=1,a=[e,0];for(let i=1;i<K*100;i++){let c=E(t,i),l=O([2*c[0],2*c[1]],a);n=T(l,O([1,0],n)),r=T(l,r),n[0]===0&&(n=[g,0]),r[0]===0&&(r=[g,0]),r=O([1,0],r);let f=J(n,r);if(s=J(s,f),r[0]<0&&(o=-o),Math.abs(f[0]-1+f[1])<y*y)break}return{fv:-(s[0]+s[1]),sign:o}}function yt(t,e){if(e>30)return Ot(t,e);let n=g,s=g,r=0,o=1;for(let a=1;a<K*100;a++){let i=2*(t+a)/e;n=i-1/n,r=i-r,n===0&&(n=g),r===0&&(r=g),r=1/r;let c=n*r;if(s*=c,r<0&&(o=-o),Math.abs(c-1)<2*y)break}return{fv:-s,sign:o}}function Pt(t,e){let n=-.5/e,s=1,r=n,o=s,a=t*t,i=(.25-a)/e,c=2*e,l=2,f=r*r+1;o=l+i*r/f,r=c+i/f;let m=c,u=l;Math.abs(r)+Math.abs(o)<g&&(r=g),Math.abs(m)+Math.abs(u)<g&&(m=g),f=m*m+u*u,m=m/f,u=-u/f;let M=r*m-o*u,p=o*m+r*u;f=n,n=f*M-s*p,s=f*p+s*M;for(let h=2;h<K&&(i=h-.5,i*=i,i-=a,l+=2,f=r*r+o*o,r=c+i*r/f,o=l-i*o/f,m=c+i*m,u=l+i*u,Math.abs(r)+Math.abs(o)<g&&(r=g),Math.abs(m)+Math.abs(u)<g&&(m=g),f=m*m+u*u,m=m/f,u=-u/f,M=r*m-o*u,p=o*m+r*u,f=n,n=f*M-s*p,s=f*p+s*M,!(Math.abs(M-1)+Math.abs(p)<2*y));h++);return{p:n,q:s}}function Zt(t,e){let n=1,s=4*t*t,r=2*e;return r*=r,n+=(s-1)/(2*r),n+=3*(s-1)*(s-9)/(r*r*8),n+=15*(s-1)*(s-9)*(s-25)/(r*r*r*8*6),Math.sqrt(n*2/(Math.PI*e))}function Gt(t,e){let n=4*t*t,s=4*e,r=s*s,o=0;return o+=(n-1)/(2*s),s*=r,o+=(n-1)*(n-25)/(6*s),s*=r,o+=(n-1)*(n*n-114*n+1073)/(5*s),s*=r,o+=(n-1)*(5*n*n*n-1535*n*n+54703*n-375733)/(14*s),o}function Wt(t,e){let n=Zt(t,e),s=Gt(t,e),r=Math.cos(e),o=Math.sin(e),a=Q(t/2+.25),i=d(t/2+.25),c=Math.sin(s),l=Math.cos(s),f=(c*(r*a+o*i)+l*(o*a-r*i))*n;return{j:(l*(r*a+o*i)-c*(o*a-r*i))*n,y:f}}function tt(t,e,n){let s=Math.round(t),r=t-s,o=2/(e*Math.PI);if(!n&&(e<5||t>e*e/4))return{j:dt(t,e),y:{m:NaN,e2:0}};if(e<1&&r!==0&&Math.log(y/2)>t*Math.log(e/2*(e/2)/t))return{j:dt(t,e),y:Vt(t,e)};if(Math.max(t,1)<e*.011048543456039806){let b=Wt(t,e);return{j:b.j,y:{m:b.y,e2:0}}}if(e>8){let b=Xt(t,e);if(b!==void 0){let N=(t/2+.25)%2,P=Math.sin(e),w=Math.cos(e),Y=d(N),rt=Q(N),st=P*rt-Y*w,ot=w*rt+P*Y,it=Math.SQRT2/(Math.sqrt(Math.PI)*Math.sqrt(e));return{j:it*(b.p*ot-b.q*st),y:{m:it*(b.p*st+b.q*ot),e2:0}}}}if(e<=2){let[b,N]=Bt(r,e),P=gt(r,e,s,b,N),{fv:w}=yt(t,e);return{j:A(o/(P.y*w-P.y1),-P.e2),y:{m:P.y,e2:P.e2}}}let{fv:a,sign:i}=yt(t,e);if(e>=t&&t>2){let{p:b,q:N}=Pt(t,e),P=t/e-a,w=(b-P)/N;w===0&&(w=t*y/e);let Y=i*Math.sqrt(o/(N+w*(b-P)));return{j:Y,y:{m:w*Y,e2:0}}}let c=g,l=a*i*c,f=i*c,m=0;for(let b=s;b>0;b--){let N=2*(r+b)/e;Math.abs(f)>2**900&&(l*=2**-900,f*=2**-900,m+=900);let P=N*f-l;P===0&&(P=l*y/2),l=f,f=P}let u=A(i*c/f,-m),M=l/f,{p,q:h}=Pt(r,e),S=r/e-M,k=(p-S)/h;k===0&&(k=r*y/e);let I=Math.sign(f)*Math.sqrt(o/(h+k*(p-S))),F=I*u,R=k*I,X=R*(r/e-p-h/k);if(!n)return{j:F,y:{m:NaN,e2:0}};let B=gt(r,e,s,R,X);return{j:F,y:{m:B.y,e2:B.e2}}}function Ht(t,e){if(Number.isNaN(t)||Number.isNaN(e)||t<0||!Number.isFinite(e))return NaN;let n=Math.abs(e),s=e<0&&e!==Math.floor(e);if(t===0)return e===0?1:s?d(n)>0?1/0:-1/0:0;if(t===1/0)return 0;if(!s){let o=tt(n,t,!1).j;return e<0&&n%2===1?-o:o}let r=tt(n,t,!0);return Q(n)*r.j-A(d(n)*r.y.m,r.y.e2)}function $t(t,e){if(Number.isNaN(t)||Number.isNaN(e)||t<0||!Number.isFinite(e))return NaN;let n=Math.abs(e),s=e<0;if(t===0){if(!s)return-1/0;let i=Q(n);return i===0?0:i>0?-1/0:1/0}if(t===1/0)return 0;let r=tt(n,t,!0);if(!s)return A(r.y.m,r.y.e2);let o=Q(n),a=d(n);return(a!==0?a*r.j:0)+(o===0?0:A(o*r.y.m,r.y.e2))}function zt(t,e){if(Number.isNaN(t)||Number.isNaN(e)||t<0||!(e>0))return{lower:NaN,upper:NaN,logLower:NaN,logUpper:NaN};let n=_(e),s=q(e),r=l=>{let f=v(e,t,l,!1),m=s*f;if(Number.isFinite(s)&&f>kt&&Number.isFinite(m)&&m>kt)return[m,Math.log(m)];let u=n+v(e,t,l,!0);return u<710&&u>-746&&t>0&&(l?t<e:t>e)?vt(t,e,l):[Math.exp(u),u]},[o,a]=r(!0),[i,c]=r(!1);return{lower:o,upper:i,logLower:a,logUpper:c}}var kt=22250738585072014e-324;function vt(t,e,n){let s=j(at(L([t,0]),e),[-t,0]);n?s=j(s,L([mt(e,t)/e,0])):s=j(s,L([ut(e,t),0]));let r=s[0]+s[1];return[s[0]>-708?Math.exp(s[0])*(1+s[1]):Math.exp(r),r]}function et(t){return Number.isNaN(t)||t<0||t>2?NaN:-ft(t/2)*Math.SQRT1_2}function xt(t){if(Number.isNaN(t)||t<-1||t>1)return NaN;let e=t<0?-1:1,n=Math.abs(t);if(n>=.5)return e*et(1-n);if(n<1e-8)return t*.886226925452758*(1+Math.PI/12*t*t);let s=et(1-n);for(let r=0;r<3;r++){let o=($(s)-n)/(1.1283791670955126*Math.exp(-s*s));if(s-=o,Math.abs(o)<=1e-17*s)break}return e*s}function te(t,e){if(Number.isNaN(t)||Number.isNaN(e)||t<0||e<0)return NaN;let n=e===Math.floor(e);return t===Math.floor(t)&&n&&e>t?0:!Number.isFinite(t)||!Number.isFinite(e)?ie(t,e):n&&e<=1e3?ee(t,e):ne(t,e)}function ee(t,e){let n=[1,0];for(let s=0;s<e;s++){let r=E(t,-s),o=n[0]*r[0];if(!Number.isFinite(o)){let a=Math.sign(o);for(let i=s+1;i<e;i++)t-i<0&&(a=-a);return a*(1/0)}n=Math.abs(n[0])>1e290||Math.abs(r[0])>1e290?[(n[0]+n[1])*(r[0]+r[1]),0]:J(n,r)}return n[0]+n[1]}function ne(t,e){let[n,s]=E(t,-e);if(n+1<=0)return re(t,n,s);if(t>1e280)return e*e<1e-17*t?Math.pow(t,e):1/0;let r;if(n>=15){if(e*Math.log(n+1)>720)return 1/0;let o=E(t,-n),a=T(J(o,L([t,0])),ct(n,[t,0]));a=j(a,[Z(t)-Z(n),0]),r=Math.sqrt(t/n)*Math.exp(a[0])*(1+a[1])}else if(t<30)r=q(t+1)/q(n+1);else{if(_(t+1)-_(n+1)>720)return 1/0;let o=T(nt(t+1),L([q(n+1),0]));r=Math.exp(o[0])*(1+o[1])}return s!==0&&Number.isFinite(r)&&r!==0&&(r*=Math.exp(-s*G(n+1))),r}function re(t,e,n){let s=j(E(e,1),[n,0]),r=d(s[0])+d(s[0]+.5)*Math.PI*s[1];if(r===0)return 0;let o=Math.sign(r),a=j(T(L([Math.abs(r),0]),oe),[-n*G(-e),0]);if(_(t+1)+_(-e)+a[0]>720)return o*(1/0);let i=j(j(nt(t+1),nt(-e)),a);return o*Math.exp(i[0])*(1+i[1])}var se=[.9189385332046728,-38782941580672414e-33],oe=[1.1447298858494002,10265951162707826e-33];function nt(t){if(t<15)return L([q(t),0]);let e=T(J(E(t,-.5),L([t,0])),[t,0]);return j(j(e,se),[Z(t),0])}function ie(t,e){let n=U(t+1),s=U(t-e+1);return n.sign*s.sign*Math.exp(n.value-s.value)}function ae(t){return Number.isNaN(t)||t<-1?NaN:t===-1||t===1/0?-1/0:lt(t)}function fe(t){return Number.isNaN(t)||t===-1/0?NaN:t===1/0||t<=-1&&t===Math.floor(t)?1/0:C(t)}function ce(t){return Number.isNaN(t)||t<0?NaN:t===0?-1/0:t===1/0?0:ht(-t)}function le(t){return Number.isNaN(t)?NaN:t<=-37?Math.exp(t):t<=18?Math.log1p(Math.exp(t)):t<=33.3?t+Math.exp(-t):t}export{Ct as besselI,Ht as besselJ,Qt as besselK,$t as besselY,Dt as beta,wt as choose,G as digamma,$ as erf,qt as erfc,et as erfcinv,xt as erfinv,_t as factorial,q as gamma,zt as gammainc,Ft as lbeta,Lt as lchoose,Et as lfactorial,_ as lgamma,fe as lgamma1p,ce as log1mexp,le as log1pexp,ae as log1pmx,te as perm,jt as psigamma,St as trigamma};
package/dist/tests.js ADDED
@@ -0,0 +1,3 @@
1
+ /*! rStats.js 0.7.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{$ as qt,B as At,C as at,G as ut,H as $t,M as ht,N as be,R as pt,S as Me,W as ye,X as ve,Y as xe,_ as Rt,c as D,d as Ot,l as zt,m as st,r as G,s as wt,x as _t}from"./chunks/chunk-6R7YDHA2.js";import{h as ke,k as Ie,l as Se,t as Le,y as Ve}from"./chunks/chunk-UIOAWLAN.js";import{a as C,b as Ae,c as U,f as cn}from"./chunks/chunk-SXSJXDD3.js";import{k as Y,l as it}from"./chunks/chunk-PTPPP4TI.js";import{c as Te}from"./chunks/chunk-QNQJA6KZ.js";import"./chunks/chunk-ZYA4ISAT.js";import{a as Nt,b as Z,c as K,e as Ee,f as W,g as lt,h as ct,i as Tt}from"./chunks/chunk-MVG5LUZU.js";import{a as Re,d as qe}from"./chunks/chunk-J3SNI7KY.js";import{b as Ne}from"./chunks/chunk-YKN6OGJL.js";import{g as P,h as V,s as we}from"./chunks/chunk-ANYGLFT4.js";import{b as jt,c as vt,d as F,e as de,f as Q,g as xt,i as Et,k as ge}from"./chunks/chunk-ZW52Z3GY.js";function Ht(e,t,o={}){let n;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?o=t:n=t;let r=C(o.alternative),s=o.mu??0,i=o.paired??!1,l=o.varEqual??!1,a=o.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(Ae(a),i&&n===void 0)throw new TypeError("y is required when paired is true");let f=Array.from(e),c=n===void 0?void 0:Array.from(n);if(i){let[E,R]=Tt(f,c);f=E.map((k,v)=>k-R[v]),c=void 0}else f=ct(f),c!==void 0&&(c=ct(c));let m,u,h,p,M,d,b,w=f.length;if(w<2)throw new Error("not enough 'x' observations");let g=Z(f),N=K(f);if(c===void 0){if(u=w-1,h=Math.sqrt(N/w),h<10*Number.EPSILON*Math.abs(g))throw new Error("data are essentially constant");m=(g-s)/h,d=i?"Paired t-test":"One Sample t-test",p=g,M=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let E=c.length;if(E<1||!l&&E<2)throw new Error("not enough 'y' observations");if(l&&w+E<3)throw new Error("not enough observations");let R=Z(c),k=K(c);if(d=`${l?"":"Welch"} Two Sample t-test`,p=[g,R],M=["mean of x","mean of y"],b="difference in means",l){u=w+E-2;let v=0;w>1&&(v+=(w-1)*N),E>1&&(v+=(E-1)*k),v/=u,h=Math.sqrt(v*(1/w+1/E))}else{let v=N/w,T=k/E;h=Math.sqrt(v+T),u=(h*h)**2/(v*v/(w-1)+T*T/(E-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(g),Math.abs(R)))throw new Error("data are essentially constant");m=(g-R-s)/h}let y,x;if(r==="less")y=Y(m,u,!0,!1),x=[-1/0,m+it(a,u,!0,!1)];else if(r==="greater")y=Y(m,u,!1,!1),x=[m-it(a,u,!0,!1),1/0];else{y=2*Y(-Math.abs(m),u,!0,!1);let E=1-a,R=it(1-E/2,u,!0,!1);x=[m-R,m+R]}return x=[s+x[0]*h,s+x[1]*h],{method:d,alternative:r,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:y,confInt:x,confLevel:a,estimate:p,estimateNames:M,nullValue:s,nullValueName:b,stdErr:h}}function bt(e){return` with simulated p-value
3
+ (based on ${fn(e)} replicates)`}function fn(e){if(!Number.isFinite(e)||e===0)return String(e);let t=e.toExponential(14).replace(/\.?0+e/,"e"),[o,n]=t.split("e"),r=Number(n),s=`${o}e${r<0?"-":"+"}${String(Math.abs(r)).padStart(2,"0")}`,i=String(Number(e.toPrecision(15)));return i.length<=s.length?i:s}function J(e){let t=[0,0],o=0;for(let n of e)t=F(t,[n,0]),o+=n;return Number.isFinite(o)?t[0]:o}function mn(e,t,o){return Array.isArray(t)?un(e,t,o??{}):hn(e,t??{})}var Fe=(e,t)=>e<t?-1:e>t?1:0;function un(e,t,o){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=u=>u==null;for(let u of[...e,...t])if(!n(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let r=[],s=[];e.forEach((u,h)=>{let p=t[h];!n(u)&&!n(p)&&(r.push(u),s.push(p))});let i=[...new Set(r)].sort(Fe),l=[...new Set(s)].sort(Fe);if(i.length<2||l.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(i.map((u,h)=>[u,h])),f=new Map(l.map((u,h)=>[u,h])),c=i.map(()=>l.map(()=>0));return r.forEach((u,h)=>c[a.get(u)][f.get(s[h])]++),{...ze(c,o),rowLevels:i,colLevels:l}}function hn(e,t){if(e.length>0&&typeof e[0]!="number"){let n=e.map(s=>Array.from(s)),r=n[0].length;if(n.some(s=>s.length!==r))throw new TypeError("all rows of the matrix must have the same length");return n.length===1?Wt(n[0],t):r===1?Wt(n.map(s=>s[0]),t):ze(n,t)}return Wt(Array.from(e),t)}function Ce(e){let t=e.B??2e3;if(!(t>=0&&t<2**31))throw new RangeError("'B' must be a non-negative number");return t}function Pe(e,t,o){let n=(1-64*Number.EPSILON)*t,r=0;for(let s=0;s<e.length;s++)e[s]>=n&&r++;return(1+r)/(o+1)}function je(e,t){return Number.isNaN(e)?NaN:D(e,t/2,2,!1,!1)}function Oe(e){if(!e.some(t=>t<5)&&e.some(Number.isNaN))throw new RangeError("missing value where TRUE/FALSE needed (the expected counts contain NaN)")}function Wt(e,t){let o=e.length;if(e.some(m=>Number.isNaN(m)||m<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o<2)throw new TypeError("'x' must have at least 2 elements");let n=J(e);if(!(n>0))throw new RangeError("at least one entry of 'x' must be positive");let r=t.p===void 0?e.map(()=>1/o):Array.from(t.p);if(r.length!==o)throw new TypeError("'x' and 'p' must have the same number of elements");if(r.some(Number.isNaN))throw new RangeError("p contains missing values");if(r.some(m=>m<0))throw new RangeError("probabilities must be non-negative");if(t.rescaleP??!1){let m=J(r);r=r.map(u=>u/m)}else if(Math.abs(J(r)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r.map(m=>n*m);Oe(s);let i=r.map((m,u)=>s[u]*(1-m)),l=J(e.map((m,u)=>(m-s[u])**2/s[u])),a="Chi-squared test for given probabilities",f=o-1,c;if(t.simulatePValue??!1){let m=Ce(t);a+=bt(m),f=NaN,c=Pe(pn(n,r,s,m),l,m)}else c=je(l,f);return{method:a,statistic:l,statisticName:"X-squared",parameter:f,parameterName:"df",pValue:c,observed:e,expected:s,residuals:e.map((m,u)=>(m-s[u])/Math.sqrt(s[u])),stdres:e.map((m,u)=>(m-s[u])/Math.sqrt(i[u]))}}function pn(e,t,o,n){let r=t.length,s=xe(r,{size:Math.trunc(n*e),replace:!0,prob:t}),i=Math.trunc(e);if(s.length===0)return new Float64Array(0);if(i===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let l=Math.ceil(s.length/i),a=new Float64Array(l),f=new Float64Array(r);for(let c=0;c<l;c++){f.fill(0);for(let u=0;u<i;u++)f[s[(c*i+u)%s.length]]++;let m=0;for(let u=0;u<r;u++)m+=(f[u]-o[u])**2/o[u];a[c]=m}return a}function dn(e,t,o,n){let r=e.length,s=t.length,i=e.reduce((c,m)=>c+m,0),l=Rt(i),a=new Int32Array(r*s),f=new Float64Array(Math.trunc(n));for(let c=0;c<f.length;c++){qt(e,t,l,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<r;h++){let p=o[h][u];m+=(a[h+u*r]-p)**2/p}f[c]=m}return f}function ze(e,t){let o=t.correct??!0,n=e.length,r=e[0].length,s=e.flat();if(s.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let i=J(s);if(!(i>0))throw new RangeError("at least one entry of 'x' must be positive");let l=e.map(w=>J(w)),a=e[0].map((w,g)=>J(e.map(N=>N[g]))),f=l.map(w=>a.map(g=>w*g/i));Oe(f.flat());let c=e.map((w,g)=>w.map((N,y)=>N-f[g][y])),m="Pearson's Chi-squared test",u,h=(n-1)*(r-1),p,M=(t.simulatePValue??!1)&&l.every(w=>w>0)&&a.every(w=>w>0),d=!M&&o&&n===2&&r===2?Math.min(.5,...c.flat().map(Math.abs)):0;if(M){let w=Ce(t);m+=bt(w),u=J(c.flatMap((N,y)=>N.map((x,E)=>x*x/f[y][E]))),h=NaN;let g=dn(l.map(N=>Math.trunc(N)),a.map(N=>Math.trunc(N)),f,w);p=Pe(g,u,w)}else d>0?(m+=" with Yates' continuity correction",u=J(c.flatMap((w,g)=>w.map((N,y)=>(Math.abs(N)-d)**2/f[g][y])))):u=J(c.flatMap((w,g)=>w.map((N,y)=>N*N/f[g][y])));let b=f.map((w,g)=>w.map((N,y)=>N*(1-l[g]/i)*(1-a[y]/i)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??je(u,h),observed:e,expected:f,residuals:c.map((w,g)=>w.map((N,y)=>N/Math.sqrt(f[g][y]))),stdres:c.map((w,g)=>w.map((N,y)=>N/Math.sqrt(b[g][y])))}}var gn=Math.pow(Number.EPSILON,.25);function X(e,t,o,n={}){let r={tol:n.tol??gn,maxiter:n.maxIter??1e3};return n.fLower!==void 0&&(r.fLower=n.fLower),n.fUpper!==void 0&&(r.fUpper=n.fUpper),Te(e,t,o,r).root}function wn(e,t={}){let o=e.length,n=o===0?0:e[0].length;if(e.some(a=>a.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(o<2||n<2)throw new TypeError("'x' must have at least 2 rows and columns");let r=e.map(a=>Array.from(a));if(r.flat().some(a=>Number.isNaN(a)||a<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(r.flat().some(a=>a===1/0))throw new RangeError("'x' has entries too large to be integer");let s=r.map(a=>a.map(Ne)),i="Fisher's Exact Test for Count Data";if(o===2&&n===2)return yn(s,t,i);let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let a=t.B??2e3;if(!(a>=0&&a<2**31))throw new RangeError("'B' must be a non-negative number");return{method:i+bt(a),alternative:l,pValue:Nn(s,a)}}return{method:i,alternative:l,pValue:vn(s)}}function Nn(e,t){let o=e.filter(M=>M.some(d=>d>0)),n=e[0].map((M,d)=>d).filter(M=>e.some(d=>d[M]>0));if(o.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(n.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let r=o.map(M=>n.map(d=>M[d])),s=r.map(M=>M.reduce((d,b)=>d+b,0)),i=r[0].map((M,d)=>r.reduce((b,w)=>b+w[d],0)),l=s.length,a=i.length,f=[0,0];for(let M of r.flat())f=F(f,[-qe(M),0]);let c=f[0]/(1+64*Number.EPSILON),m=Rt(s.reduce((M,d)=>M+d,0)),u=new Int32Array(l*a),h=0,p=Math.trunc(t);for(let M=0;M<p;M++){qt(s,i,m,u);let d=0;for(let b=0;b<u.length;b++)d-=m[u[b]];d<=c&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var bn=1+1e-7,Mn=16;function Bt(e,t,o){let n=[0,0];for(let r=Math.max(0,t);r<=Math.min(o,e.length-1);r++)e[r]!==0&&(n=F(n,[e[r],0]));return n[0]}var Kt=class{m;n;k;lo;hi;logCentral;constructor(t,o,n){this.m=t,this.n=o,this.k=n,this.lo=Math.max(0,n-o),this.hi=Math.min(n,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let r=0,s=1;for(let i=this.lo;i<=this.hi;i++){let l=i-this.lo;l%Mn===0||!(s>0&&s<1/0)?(r=ut(i,t,o,n,!0),s=1,this.logCentral[l]=r):this.logCentral[l]=r+Math.log(s),s*=(t-i)*(n-i)/((i+1)*(o-n+i+1))}}density(t){let o=this.logCentral.length,n=Math.log(t),r=new Float64Array(o),s=-1/0;for(let l=0;l<o;l++){let a=this.logCentral[l]+(this.lo+l)*n;r[l]=a,a>s&&(s=a)}for(let l=0;l<o;l++){let a=r[l]-s;r[l]=a<-746?0:Math.exp(a)}let i=Bt(r,0,o-1);for(let l=0;l<o;l++)r[l]!==0&&(r[l]=r[l]/i);return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let o=this.density(t),n=[0,0];for(let r=0;r<o.length;r++)o[r]!==0&&(n=F(n,[(this.lo+r)*o[r],0]));return n[0]}tail(t,o,n){if(o===1)return n?$t(t-1,this.m,this.n,this.k,!1,!1):$t(t,this.m,this.n,this.k,!0,!1);if(o===0||o===1/0){let i=o===0?this.lo:this.hi;return(n?t<=i:t>=i)?1:0}let r=this.density(o),s=t-this.lo;return n?Bt(r,s,r.length-1):Bt(r,0,s)}};function Dt(e,t){return t?X(e,0,1):1/X(o=>e(1/o),Number.EPSILON,1)}function yn(e,t,o){let n=C(t.alternative),r=t.or??1;if(!(r>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,l],[a,f]]=e,c=i,m=new Kt(i+a,l+f,i+l),{lo:u,hi:h}=m,p;if(n==="less")p=m.tail(c,r,!1);else if(n==="greater")p=m.tail(c,r,!0);else if(r===0)p=c===u?1:0;else if(r===1/0)p=c===h?1:0;else{let N=m.density(r),y=N[c-u]*bn,x=[0,0];for(let E of N)E<=y&&(x=F(x,[E,0]));p=x[0]}let M;if(c===u)M=0;else if(c===h)M=1/0;else{let N=m.mean(1);M=N===c?1:Dt(y=>m.mean(y)-c,N>c)}let d={method:o,alternative:n,pValue:p,estimate:M,estimateNames:"odds ratio",nullValue:r,nullValueName:"odds ratio"};if(!(t.confInt??!0))return d;let b=N=>{if(c===h)return 1/0;let y=m.tail(c,1,!1);return y===N?1:Dt(x=>m.tail(c,x,!1)-N,y<N)},w=N=>{if(c===u)return 0;let y=m.tail(c,1,!0);return y===N?1:Dt(x=>m.tail(c,x,!0)-N,y>N)},g;if(n==="less")g=[0,b(1-s)];else if(n==="greater")g=[w(1-s),1/0];else{let N=(1-s)/2;g=[w(N),b(N)]}return{...d,confInt:g,confLevel:s}}var kt=[[0,0]];function tt(e){for(let t=kt.length;t<=e;t++)kt.push(F(kt[t-1],ge([t,0])));return kt[e]}function It(e,t,o,n){let r=e+o,s=r-e,i=e-(r-s)+(o-s)+t+n,l=r+i;return[l,i-(l-r)]}var Ut=class{scale=-1/0;hi=0;lo=0;add(t,o,n){if(t>this.scale){let a=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=a,this.lo*=a,this.scale=t}let r=n*Math.exp(t-this.scale)*(1+o),s=this.hi+r,i=s-this.hi,l=this.hi-(s-i)+(r-i)+this.lo;this.hi=s+l,this.lo=l-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function vn(e){let t=e.filter(g=>g.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let o=t[0].map((g,N)=>t.some(y=>y[N]>0));if(t=t.map(g=>g.filter((N,y)=>o[y])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((g,N)=>t.map(y=>y[N])));let n=t.length,r=t.map(g=>g.reduce((N,y)=>N+y,0)),s=t[0].map((g,N)=>t.reduce((y,x)=>y+x[N],0)).sort((g,N)=>N-g),i=s.length,l=r.reduce((g,N)=>g+N,0),a=de(tt(l));for(let g of r)a=F(a,tt(g));for(let g of s)a=F(a,tt(g));let f=[0,0];for(let g of t.flat())f=Q(f,tt(g));let c=F(f,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(c[0])),[u,h]=c,p=Array.from({length:i},()=>new Map),M=(g,N)=>{let y=N.join(","),x=p[g].get(y);if(x!==void 0)return x;let E=F(a,tt(N.reduce((A,L)=>A+L,0)));for(let A of N)E=Q(E,tt(A));for(let A=g;A<i;A++)E=Q(E,tt(s[A]));let R={R:N,KZ:E,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[g].set(y,R),g===i-1){let A=[0,0];for(let L of N)A=Q(A,tt(L));return R.max=R.min=A[0],R.edges.push({to:R,hi:A[0],lo:A[1],mult:1}),R}let k=new Map,v=new Array(n+1).fill(0);for(let A=n-1;A>=0;A--)v[A]=v[A+1]+N[A];let T=new Array(n).fill(0),I=(A,L,j)=>{if(A===n-1){T[A]=L;let z=Q(j,tt(L)),et=N.map((rt,dt)=>rt-T[dt]).sort((rt,dt)=>rt-dt),$=M(g+1,et),B=`${et.join(",")}|${z[0]}`,nt=k.get(B);nt===void 0?k.set(B,{to:$,hi:z[0],lo:z[1],mult:1}):nt.mult++;return}let O=Math.max(0,L-v[A+1]),H=Math.min(N[A],L);for(let z=O;z<=H;z++)T[A]=z,I(A+1,L-z,Q(j,tt(z)))};if(I(0,s[g],[0,0]),R.edges=[...k.values()],R.max=Math.max(...R.edges.map(A=>A.hi+A.to.max)),R.min=Math.min(...R.edges.map(A=>A.hi+A.to.min)),g===i-2){let A=R.edges.map($=>{let B=$.to.edges[0];return{v:F([$.hi,$.lo],[B.hi,B.lo]),mult:$.mult}});A.sort(($,B)=>$.v[0]-B.v[0]||$.v[1]-B.v[1]);let L=A.length,j=A[L-1].v[0],O=new Float64Array(L),H=new Float64Array(L),z=new Float64Array(L),et=[0,0];A.forEach(({v:$,mult:B},nt)=>{O[nt]=$[0],H[nt]=$[1];let rt=Q($,[j,0]);et=F(et,[B*Math.exp(rt[0])*(1+rt[1]),0]),z[nt]=et[0]}),R.dist={hi:O,lo:H,cum:z,vmax:j,kv:F(a,[j,0])}}return R},d=M(0,[...r].sort((g,N)=>g-N)),b=new Ut,w=(g,N,y,x)=>{if(N+g.max<=u-m){let[I,A]=It(g.KZ[0],g.KZ[1],N,y);return b.add(I,A,x),!0}if(N+g.min>u+m)return!0;let E=g.dist;if(E===void 0)return!1;let[R,k]=It(u,h,-N,-y),v=0,T=E.hi.length;for(;v<T;){let I=v+T>>>1,A=E.hi[I];A<R||A===R&&E.lo[I]<=k?v=I+1:T=I}if(v>0){let[I,A]=It(E.kv[0],E.kv[1],N,y);b.add(I,A,x*E.cum[v-1])}return!0};w(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let g=0;g<i-2;g++)for(let N of p[g].values()){let{pastHi:y,pastLo:x,count:E}=N;if(E.length!==0){for(let{to:R,hi:k,lo:v,mult:T}of N.edges)for(let I=0;I<E.length;I++){let[A,L]=It(y[I],x[I],k,v),j=E[I]*T;if(w(R,A,L,j))continue;let O=R.index.get(A);O===void 0?(R.index.set(A,R.count.length),R.pastHi.push(A),R.pastLo.push(L),R.count.push(j)):R.count[O]+=j}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,b.value())}function Xt(e,t,o={}){let n;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?o=t:n=t;let r=C(o.alternative),s=o.mu??0,i=o.paired??!1,l=o.exact??null,a=o.correct??!0,f=o.confInt??!1,c=o.confLevel??.95,m=o.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(f&&!(c>0&&c<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&n===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[M,d]=Tt(Array.from(e),Array.from(n));u=ct(M.map((b,w)=>b-d[w]))}else u=ct(Array.from(e)),n!==void 0&&(h=ct(Array.from(n)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:r,mu:s,correct:a,confInt:f,confLevel:c,tolRoot:m};return h===void 0?En(u,l,i,p):qn(u,h,l,p)}function St(e,t,o){return o?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function $e(e,t){return t==="less"?P(e):t==="greater"?P(e,{lowerTail:!1}):2*Math.min(P(e),P(e,{lowerTail:!1}))}function xn(e){if(!(e>0)||!Number.isFinite(e))return e;let o=1-Math.floor(Math.log10(e)),n=o>=0?e*10**o:e/10**-o,r=Math.round(n);return r-n===.5&&r%2!==0&&(r-=1),o>=0?r/10**o:r*10**-o}function Qt(e){let t=e.length;if(t===0)return NaN;let o=Math.floor(t/2);return t%2===1?e[o]:Z([e[o-1],e[o]])}var Gt=(e,t)=>e-t;function He(e){let t=e.length,o=W(e.map(Math.abs)),n=0;for(let i=0;i<t;i++)e[i]>0&&(n+=o[i]);let r=0;for(let i of lt(o))r+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-r/48);return[n,n-t*(t+1)/4,s]}function En(e,t,o,n){let{alternative:r,mu:s,correct:i}=n,l=e.map(x=>x-s),a=l.some(x=>x===0);a&&(l=l.filter(x=>x!==0));let f=l.length,c=t??f<50,m=W(l.map(Math.abs)),u=lt(m).length>0,[h,p,M]=He(l),d,b,w=c&&!u&&!a;if(w){b="Wilcoxon signed rank exact test";let x=f*(f+1)/2;if(r==="less")d=ht(h,f,!0,!1);else if(r==="greater")d=ht(h-1,f,!1,!1);else{let E=h>x/2?ht(h-1,f,!1,!1):ht(h,f,!0,!1);d=Math.min(1,2*E)}}else b=`Wilcoxon signed rank test${i?" with continuity correction":""}`,d=$e((p-St(p,r,i))/M,r);let g={method:b,alternative:r,statistic:h,statisticName:"V",pValue:d,nullValue:s,nullValueName:o?"location shift":"location"};if(!n.confInt)return g;let N=l.map(x=>x+s),y=w?An(N,n):Rn(N,n);return{...g,...y}}function Lt(e,t,o){return e==="less"?[-1/0,o]:e==="greater"?[t,1/0]:[t,o]}function _e(e,t,o){let n={confInt:Lt(e,NaN,NaN),confLevel:0,estimate:t};return o!==void 0&&(n.estimateNames=o),n}function We(e,t,o){let n=e(t==="two.sided"?o/2:o);return n===0?1:n}function Be(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function An(e,t){let{alternative:o,confLevel:n}=t,r=e.length,s=[];for(let m=0;m<r;m++)for(let u=m;u<r;u++)s.push((e[m]+e[u])/2);s.sort(Gt);let i=s.length,l=1-n,a=We(m=>be(m,r,!0,!1),o,l),f=ht(a-1,r,!0,!1),c=o==="two.sided"?2*f:f;return{confInt:Lt(o,s[a-1],s[i-a]),confLevel:c-l>l/2?1-xn(c):n,estimate:Qt(s),estimateNames:"(pseudo)median"}}function Rn(e,t){let{alternative:o,correct:n,confLevel:r,tolRoot:s}=t,i=e.length;if(i===0)return _e(o,NaN);let l=Math.min(...e),a=Math.max(...e);if(!Number.isFinite(l)||!Number.isFinite(a)||i===1||l===a)return _e(o,(l+a)/2,"midrange");let f=(g,N)=>{let[,y,x]=He(e.map(E=>E-g).filter(E=>E!==0));return(y-St(y,o,N))/x},c=f(l,n),m=f(a,n),u=g=>-V(g),h=g=>o==="greater"?!(c<u(g)):o==="less"?!(m>V(g/2)):!(c<u(g/2))&&!(m>V(g/2)),p=1-r,M=!1;for(;!h(p)&&p<1;)p*=2,M=!0;Be(s);let d=g=>X(N=>f(N,n)-g,l,a,{tol:s,fLower:c-g,fUpper:m-g}),b;if(p>=1){let g=Qt([...e].sort(Gt));b=Lt(o,g,g)}else o==="greater"?b=[d(u(p)),1/0]:o==="less"?b=[-1/0,d(V(p))]:b=[d(u(p/2)),d(V(p/2))];let w=X(g=>f(g,!1),l,a,{tol:s,fLower:f(l,!1),fUpper:f(a,!1)});return{confInt:b,confLevel:M?1-Math.min(1,p):r,estimate:w,estimateNames:"(pseudo)median"}}function De(e,t){let o=e.length,n=t.length,r=W([...e,...t]),s=0;for(let c=0;c<o;c++)s+=r[c];let i=s-o*(o+1)/2,l=0;for(let c of lt(r))l+=c*c*c-c;let a=o+n,f=Math.sqrt(o*n/12*(a+1-l/(a*(a-1))));return[i,i-o*n/2,f]}function qn(e,t,o,n){let{alternative:r,mu:s,correct:i}=n,l=e.length,a=t.length,f=e.map(N=>N-s),c=o??(l<50&&a<50),m=lt([...f,...t]).length>0,[u,h,p]=De(f,t),M,d,b=c&&!m;if(b)if(d="Wilcoxon rank sum exact test",r==="less")M=pt(u,l,a,!0,!1);else if(r==="greater")M=pt(u-1,l,a,!1,!1);else{let N=u>l*a/2?pt(u-1,l,a,!1,!1):pt(u,l,a,!0,!1);M=Math.min(1,2*N)}else d=`Wilcoxon rank sum test${i?" with continuity correction":""}`,M=$e((h-St(h,r,i))/p,r);let w={method:d,alternative:r,statistic:u,statisticName:"W",pValue:M,nullValue:s,nullValueName:"location shift"};if(!n.confInt)return w;let g=b?Tn(e,t,n):kn(e,t,n);return{...w,...g}}function Tn(e,t,o){let{alternative:n,confLevel:r}=o,s=e.length,i=t.length,l=[];for(let h of e)for(let p of t)l.push(h-p);l.sort(Gt);let a=l.length,f=1-r,c=We(h=>Me(h,s,i,!0,!1),n,f),m=pt(c-1,s,i,!0,!1),u=n==="two.sided"?2*m:m;return{confInt:Lt(n,l[c-1],l[a-c]),confLevel:u-f>f/2?1-u:r,estimate:Qt(l),estimateNames:"difference in location"}}function kn(e,t,o){let{alternative:n,correct:r,confLevel:s,tolRoot:i}=o,l=Math.min(...e)-Math.max(...t),a=Math.max(...e)-Math.min(...t);if(!Number.isFinite(l)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let f=(h,p)=>{let[,M,d]=De(e.map(b=>b-h),t);return(M-St(M,n,p))/d},c=(h,p)=>{let M=f(l,p)-h,d=f(a,p)-h;if(Number.isNaN(M)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return M<=0?l:d>=0?a:(Be(i),X(b=>f(b,p)-h,l,a,{tol:i,fLower:M,fUpper:d}))},m=1-s,u;return n==="greater"?u=[c(-V(m),r),1/0]:n==="less"?u=[-1/0,c(V(m),r)]:u=[c(-V(m/2),r),c(V(m/2),r)],{confInt:u,confLevel:s,estimate:c(0,!1),estimateNames:"difference in location"}}var Ke=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function In(e){if(Ke.includes(e))return e;let t=Ke.filter(o=>o.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Yt(e,t={}){let o=typeof t=="string"?{method:t}:t,n=In(o.method??"holm"),r=Array.from(e),s=[];for(let m=0;m<r.length;m++)Number.isNaN(r[m])||s.push(m);let i=s.length,l=o.n??i;if(!(l>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(l<=1)return r;let a=s.map(m=>r[m]),f=Sn(a,n,l),c=r.slice();return s.forEach((m,u)=>c[m]=f[u]),c}function Vt(e,t){let o=e.map((n,r)=>r);return o.sort((n,r)=>(t?e[r]-e[n]:e[n]-e[r])||n-r),o}function Sn(e,t,o){let n=e.length,r=new Array(n);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,o*s));case"holm":{let s=Vt(e,!1),i=-1/0;return s.forEach((l,a)=>{i=Math.max(i,(o-a)*e[l]),r[l]=Math.min(1,i)}),r}case"hochberg":{let s=Vt(e,!0),i=1/0;return s.forEach((l,a)=>{let f=n-a;i=Math.min(i,(o-f+1)*e[l]),r[l]=Math.min(1,i)}),r}case"BH":case"fdr":case"BY":{let s=t==="BY"?Ee(Array.from({length:Math.floor(o)},(a,f)=>1/(f+1))):1,i=Vt(e,!0),l=1/0;return i.forEach((a,f)=>{let c=n-f;l=Math.min(l,s*o/c*e[a]),r[a]=Math.min(1,l)}),r}case"hommel":return Ln(e,o)}}function Ln(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");let o=e.length,n=[...e,...new Array(t-o).fill(1)],r=Vt(n,!1),s=r.map(a=>n[a]),i=s.slice();for(let a=t;a>=2;a--){let f=1/0;for(let c=1;c<=a;c++)f=Math.min(f,a*s[t-a+c-1]/c);for(let c=t-a;c<t;c++)i[c]=Math.max(i[c],f);for(let c=0;c<t-a;c++)i[c]=Math.max(i[c],Math.min(a*s[c],f))}let l=new Array(o);return r.forEach((a,f)=>{a<o&&(l[a]=i[f])}),l}var Vn=1+1e-7;function Ft(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function Fn(e,t,o){if(o===0)return e===0?1:0;if(o===1)return e===t?1:0;let n=t*o;if(e===n)return 1;let r=At(e,t,o,!1)*Vn,s=0;if(e<n){for(let a=Math.ceil(n);a<=t;a++)At(a,t,o,!1)<=r&&s++;let l=s===0?0:at(t-s,t,o,!1,!1);return Math.min(1,at(e,t,o,!0,!1)+l)}for(let l=0;l<=Math.floor(n);l++)At(l,t,o,!1)<=r&&s++;let i=s===0?0:at(s-1,t,o,!0,!1);return Math.min(1,at(e-1,t,o,!1,!1)+i)}function Zt(e,t,o={}){let n,r;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(o=t),!Ft(e[0])||!Ft(e[1]))throw new RangeError("'x' must be nonnegative and integer");n=Math.round(e[0]),r=Math.round(e[0])+Math.round(e[1])}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Ft(e))throw new RangeError("'x' must be nonnegative and integer");if(n=Math.round(e),!(Ft(t)&&Math.round(t)>=n&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");r=Math.round(t)}let s=o.p??.5;if(!(s>=0&&s<=1))throw new RangeError("'p' must be a single number between 0 and 1");let i=C(o.alternative),l=o.confLevel??.95;U(l);let a;i==="less"?a=at(n,r,s,!0,!1):i==="greater"?a=at(n-1,r,s,!1,!1):a=Fn(n,r,s);let f=u=>n===0?0:_t(u,n,r-n+1,!0,!1),c=u=>n===r?1:_t(1-u,n+1,r-n,!0,!1),m;if(i==="less")m=[0,c(1-l)];else if(i==="greater")m=[f(1-l),1];else{let u=(1-l)/2;m=[f(u),c(u)]}return{method:"Exact binomial test",alternative:i,statistic:n,statisticName:"number of successes",parameter:r,parameterName:"number of trials",pValue:a,confInt:m,confLevel:l,estimate:n/r,estimateNames:"probability of success",nullValue:s,nullValueName:"probability of success"}}var Cn=1+1e-7;function Pn(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function jn(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let o=zt(e,t,!1)*Cn,n=l=>zt(l,t,!1)<=o,r=Math.floor(t);if(e<t){let l=Math.max(e+1,r),a=l;if(!n(l)){let f=l,c=1;for(a=l+1;!n(a);)f=a,c*=2,a=l+c;for(;a-f>1;){let m=f+Math.floor((a-f)/2);n(m)?a=m:f=m}}return Math.min(1,st(e,t,!0,!1)+st(a-1,t,!1,!1))}let s=Math.min(e-1,r),i;if(!n(0))i=-1;else if(n(s))i=s;else{i=0;let l=s;for(;l-i>1;){let a=i+Math.floor((l-i)/2);n(a)?i=a:l=a}}return Math.min(1,st(i,t,!0,!1)+st(e-1,t,!1,!1))}function On(e,t={}){let o=typeof e=="number"?[e]:Array.from(e);if(o.length<1||o.length>2)throw new RangeError("incorrect length of 'x'");if(!o.every(Pn))throw new RangeError("'x' must be finite, nonnegative, and integer");let n=o.map(Math.round),r=n.length,s=t.T??1,i=typeof s=="number"?new Array(r).fill(s):Array.from(s);if(i.length!==r)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>=0))throw new RangeError("'T' must be nonnegative");let l=t.r??1;if(!(Number.isFinite(l)&&l>=0))throw new RangeError("'r' must be a single positive number");let a=C(t.alternative),f=t.confLevel??.95;if(U(f),r===2){let[b,w]=n,[g,N]=i,y=b+w,x=l*g/(l*g+N),E=Zt([b,w],{p:x,alternative:a,confLevel:f}),R=k=>k/(1-k)*(N/g);return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:y*l*g/(g+l*N),parameterName:"expected count1",pValue:E.pValue,confInt:[R(E.confInt[0]),R(E.confInt[1])],confLevel:f,estimate:b/g/(w/N),estimateNames:"rate ratio",nullValue:l,nullValueName:"rate ratio"}}let c=n[0],m=i[0],u=l*m,h;a==="less"?h=st(c,u,!0,!1):a==="greater"?h=st(c-1,u,!1,!1):h=jn(c,u);let p=b=>c===0?0:Ot(b,c,1,!0,!1),M=b=>Ot(1-b,c+1,1,!0,!1),d;if(a==="less")d=[0,M(1-f)];else if(a==="greater")d=[p(1-f),1/0];else{let b=(1-f)/2;d=[p(b),M(b)]}return{method:"Exact Poisson test",alternative:a,statistic:c,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[d[0]/m,d[1]/m],confLevel:f,estimate:c/m,estimateNames:"event rate",nullValue:l,nullValueName:"event rate"}}function Jt(e,t,o={}){let n,r,s=o;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let v=e;if(v.some(T=>T.length!==2))throw new RangeError("'x' must have 2 columns");n=v.map(T=>T[0]),r=v.map(T=>T[0]+T[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");n=typeof e=="number"?[e]:Array.from(e),r=typeof t=="number"?[t]:Array.from(t)}if(n.length!==r.length)throw new RangeError("'x' and 'n' must have the same length");let i=n.length,l=[];for(let v=0;v<i;v++)!Number.isNaN(n[v])&&!Number.isNaN(r[v])&&l.push(v);let a=l.length;if(a<1)throw new RangeError("not enough data");if(n=l.map(v=>n[v]),r=l.map(v=>r[v]),r.some(v=>!(v>0)))throw new RangeError("elements of 'n' must be positive");if(n.some(v=>!(v>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(n.some((v,T)=>v>r[T]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let f;if(s.p!==void 0){let v=typeof s.p=="number"?[s.p]:Array.from(s.p);if(v.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(f=l.map(T=>v[T]),f.some(T=>!(T>0&&T<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let c=f!==void 0;a===1&&f===void 0&&(f=[.5]);let m=s.confLevel??.95;U(m);let u=C(s.alternative);(a>2||a===2&&c)&&(u="two.sided");let h=n.map((v,T)=>v/r[T]),p=a===2?h[0]-h[1]:0,M=a===2?1/r[0]+1/r[1]:0,d=(s.correct??!0)&&a<=2?.5:0,b=!1;if(d>0&&a===1){let v=Math.abs(n[0]-r[0]*f[0]);v<=d&&([d,b]=[v,!0])}else if(d>0&&a===2&&!c){let v=Math.abs(p)/M;v<=d&&([d,b]=[v,!0])}let w=n.reduce((v,T)=>v+T,0)/r.reduce((v,T)=>v+T,0),g=0;if(!b)for(let v=0;v<a;v++){let T=f?.[v]??w,I=r[v]*T,A=r[v]*(1-T);g+=(Math.abs(n[v]-I)-d)**2/I,g+=(Math.abs(r[v]-n[v]-A)-d)**2/A}let N=f!==void 0?a:a-1,y;if(u==="two.sided")y=D(g,N/2,2,!1,!1);else{let v=Math.sign(a===1?h[0]-f[0]:p);y=P(v*Math.sqrt(g),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!c){let v=V(u==="two.sided"?(1+m)/2:m);if(a===1){let T=r[0],I=v*v,A=(z,et)=>(z+I/(2*T)+et*v*Math.sqrt(z*(1-z)/T+I/(4*T*T)))/(1+I/T),L=h[0]+d/T,j=h[0]-d/T,O=Math.min(1,Math.max(0,L>=1?1:A(L,1))),H=Math.min(1,Math.max(0,j<=0?0:A(j,-1)));x=u==="two.sided"?[H,O]:u==="greater"?[H,1]:[0,O]}else{let T=Math.sqrt(h.reduce((j,O,H)=>j+O*(1-O)/r[H],0)),I=v*T+d*M,A=Math.max(-1,p-I),L=Math.min(1,p+I);x=u==="two.sided"?[A,L]:u==="greater"?[A,1]:[-1,L]}}let E=a===1?"p":l.map(v=>`prop ${v+1}`),k={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${c?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:g,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:E};return f!==void 0&&(k.nullValue=a===1?f[0]:f,k.nullValueName=E),x!==void 0&&(k.confInt=x,k.confLevel=m),k}function zn(e,t,o={}){let n;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(o=t),n=e.map(c=>Array.from(c));let f=n.length;if(f<2||n.some(c=>c.length!==f))throw new RangeError("'x' must be square with at least two rows and columns");if(n.some(c=>c.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(t))throw new TypeError("'y' is required when 'x' is not a table");n=_n(e,t)}let r=o.correct??!0,s=n.length,i,l=r&&s===2&&n[0][1]!==n[1][0];if(l){let f=n[0][1],c=n[1][0];i=(Math.abs(f-c)-1)**2/(f+c)}else{i=0;for(let f=0;f<s;f++)for(let c=f+1;c<s;c++){let m=n[f][c],u=n[c][f];i+=(m-u)**2/(m+u)}}let a=s*(s-1)/2;return{method:l?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:D(i,a/2,2,!1,!1)}}function _n(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let o=[];for(let f=0;f<e.length;f++){let c=e[f],m=t[f];typeof c=="number"&&Number.isNaN(c)||typeof m=="number"&&Number.isNaN(m)||o.push([c,m])}let n=f=>Array.from(new Set(f)).sort((c,m)=>typeof c=="number"&&typeof m=="number"?c-m:String(c)<String(m)?-1:String(c)>String(m)?1:0),r=n(o.map(f=>f[0])),s=n(o.map(f=>f[1]));if(r.length<2||r.length!==s.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let i=new Map(r.map((f,c)=>[f,c])),l=new Map(s.map((f,c)=>[f,c])),a=r.map(()=>new Array(r.length).fill(0));for(let[f,c]of o)a[i.get(f)][l.get(c)]++;return a}function $n(e,t,o={}){let n=Array.from(e).filter(Number.isFinite),r=Array.from(t).filter(Number.isFinite),s=o.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=C(o.alternative),l=o.confLevel??.95;U(l);let a=n.length-1,f=r.length-1;if(a<1)throw new Error("not enough 'x' observations");if(f<1)throw new Error("not enough 'y' observations");let c=K(n)/K(r),m=c/s,u=G(m,a,f,!0,!1),h=G(m,a,f,!1,!1),p,M;if(i==="less")p=u,M=[0,c/wt(1-l,a,f,!0,!1)];else if(i==="greater")p=h,M=[c/wt(l,a,f,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let d=(1-l)/2;M=[c/wt(1-d,a,f,!0,!1),c/wt(d,a,f,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:m,statisticName:"F",parameter:[a,f],parameterName:["num df","denom df"],pValue:p,confInt:M,confLevel:l,estimate:c,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances"}}function Hn(e){let t=e.length;if(t<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=[],n=[];for(let c of e){let m=Array.from(c).filter(Number.isFinite);if(m.length<2)throw new RangeError("there must be at least 2 observations in each group");o.push(m.length-1),n.push(K(m))}let r=0,s=0,i=0,l=0;for(let c=0;c<t;c++)r+=o[c],s+=o[c]*n[c],i+=o[c]*Math.log(n[c]),l+=1/o[c];s/=r;let a=(r*Math.log(s)-i)/(1+(l-1/r)/(3*(t-1))),f=t-1;return{method:"Bartlett test of homogeneity of variances",statistic:a,statisticName:"Bartlett's K-squared",parameter:f,parameterName:"df",pValue:D(a,f/2,2,!1,!1)}}function te(e,t,o="drop"){if(t===void 0){let i=e;if(i.length>0&&typeof i[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let l=i.map(c=>Array.from(c).filter(m=>!Number.isNaN(m)));if(o==="error"&&l.some(c=>c.length===0))throw new RangeError("all groups must contain data");let a=[],f=[];return l.forEach((c,m)=>{c.length!==0&&(a.push(String(m+1)),f.push(c))}),{labels:a,groups:f}}let n=Array.from(e);if(n.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let r=new Map;for(let i=0;i<n.length;i++){let l=n[i],a=t[i];if(Number.isNaN(l)||typeof a=="number"&&Number.isNaN(a))continue;let f=r.get(a);f===void 0&&r.set(a,f=[]),f.push(l)}let s=Array.from(r.keys()).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0);return{labels:s.map(String),groups:s.map(i=>r.get(i))}}function ft(e,t,o="drop"){return te(e,t,o).groups}function Ue(e){let t=[...e].sort((n,r)=>n-r),o=t.length;return o%2===1?t[(o-1)/2]:(t[o/2-1]+t[o/2])/2}function ee(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(o=>o.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(o=>o.every(n=>!Number.isNaN(n))).map(o=>Array.from(o))}function ne(e,t,o,n){return{method:e,statistic:o,statisticName:t,parameter:n,parameterName:"df",pValue:D(o,n/2,2,!1,!1)}}function Xe(e){let t=0;for(let o of lt(e))t+=o*o*o-o;return t}function Wn(e,t){let o=ft(e,t,"error"),n=o.length;if(n<2)throw new RangeError("all observations are in the same group");let r=o.flat(),s=r.length,i=W(r),l=0,a=0;for(let f of o){let c=0;for(let m=0;m<f.length;m++)c+=i[a+m];a+=f.length,l+=c*c/f.length}return l=(12*l/(s*(s+1))-3*(s+1))/(1-Xe(r)/(s*s*s-s)),ne("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",l,n-1)}function Bn(e){let t=ee(e),o=t.length,n=e[0]?.length??0;if(o<2||n<2)throw new RangeError("'y' must have at least two complete rows and two columns");let r=new Array(n).fill(0),s=0;for(let a of t){let f=W(a);for(let c=0;c<n;c++)r[c]+=f[c];s+=Xe(a)}let i=0;for(let a of r)i+=(a-o*(n+1)/2)**2;let l=12*i/(o*n*(n+1)-s/(n-1));return ne("Friedman rank sum test","Friedman chi-squared",l,n-1)}function Dn(e){let t=ee(e),o=e.length,n=e[0]?.length??0;if(t.length<2||n<2)throw new RangeError("'y' must have at least two complete rows and two columns");let r=W(t.map(h=>Math.max(...h)-Math.min(...h))),s=new Array(n).fill(0),i=0;t.forEach((h,p)=>{let M=W(h);for(let d=0;d<n;d++){let b=r[p]*(M[d]-(n+1)/2);i+=b*b,s[d]+=b}});let l=0;for(let h of s)l+=h*h;l/=o;let a=n-1,f=(o-1)*(n-1),c,m,u=[a,f];if(i===l){c=NaN,u=[NaN,NaN];let h=0;for(let p=2;p<=n;p++)h+=Math.log(p);m=Math.exp((1-o)*h)}else c=(o-1)*l/(i-l),m=G(c,a,f,!1,!1);return{method:"Quade test",statistic:c,statisticName:"Quade F",parameter:u,parameterName:["num df","denom df"],pValue:m}}function Kn(e,t){let o=ft(e,t,"error"),n=o.length;if(n<2)throw new RangeError("all observations are in the same group");let r=o.flatMap(u=>{let h=Ue(u);return u.map(p=>Math.abs(p-h))}),s=r.length,l=W(r).map(u=>V((1+u/(s+1))/2)),a=0;for(let u of l)a+=u;let f=a/s,c=0,m=0;for(let u of o){let h=0;for(let p=0;p<u.length;p++)h+=l[m+p];m+=u.length,c+=u.length*(h/u.length-f)**2}return c/=K(l),ne("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",c,n-1)}function Un(e,t,o={}){let n=Array.from(e).filter(Number.isFinite),r=Array.from(t).filter(Number.isFinite),s=C(o.alternative),i=n.length,l=r.length,a=i+l;if(a<3)throw new RangeError("not enough observations");let f=n.concat(r),c=Array.from({length:a},(N,y)=>y).sort((N,y)=>f[N]-f[y]),m=new Array(a),u=0;for(;u<a;){let N=u;for(;N+1<a&&f[c[N+1]]===f[c[u]];)N++;let y=0;for(let x=u;x<=N;x++)y+=(x+1-(a+1)/2)**2;y/=N-u+1;for(let x=u;x<=N;x++)m[c[x]]=y;u=N+1}let h=0;for(let N=0;N<i;N++)h+=m[N];let p=0;for(let N of m)p+=N;p/=a;let M=0;for(let N of m)M+=(N-p)**2;let d=i*p,b=i*l/(a*(a-1))*M,w=(h-d)/Math.sqrt(b),g=s==="less"?P(w):s==="greater"?P(w,{lowerTail:!1}):2*P(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:s,statistic:w,statisticName:"Z",pValue:g}}function mt(e,t){let o=0;for(let n=e.length-1;n>=0;n--)o=o*t+e[n];return o}var Xn=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Qn=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Gn=[.544,-.39978,.025054,-6714e-7],Yn=[1.3822,-.77857,.062767,-.0020322],Zn=[-1.5861,-.31082,-.083751,.0038915],Jn=[-.4803,-.082676,.0030302],tr=[-2.273,.459];function er(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let o=Math.floor(e/2),n=new Float64Array(o),r=0;for(let m=0;m<o;m++)n[m]=-V((m+1-.375)/(e+.25)),r+=n[m]*n[m];r*=2;let s=Math.sqrt(r),i=1/Math.sqrt(e),l=n[0]/s+mt(Xn,i),a=new Float64Array(o);a[0]=l;let f,c;if(e>5){let m=n[1]/s+mt(Qn,i);a[1]=m,f=2,c=Math.sqrt((r-2*n[0]*n[0]-2*n[1]*n[1])/(1-2*l*l-2*m*m))}else f=1,c=Math.sqrt((r-2*n[0]*n[0])/(1-2*l*l));for(let m=f;m<o;m++)a[m]=n[m]/c;for(let m=0;m<o;m++)t[e-1-m]=a[m],t[m]=-a[m];return t}function Qe(e){let t=e.length,o=er(t),n=e[t-1]-e[0],r=0;for(let f of e)r+=f/n;r/=t;let s=0,i=0,l=0;for(let f=0;f<t;f++){let c=e[f]/n-r;s+=o[f]*o[f],i+=c*c,l+=o[f]*c}let a=Math.sqrt(s*i);return(a-l)*(a+l)/(s*i)}var nr=1.90985931710274,rr=1.0471975511966;function Ge(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=nr*(Math.asin(Math.sqrt(1-e))-rr);return Math.max(i,0)}let o=Math.log(e);if(t<=11){let i=mt(tr,t);if(o>=i)return 1e-99;let l=mt(Gn,t),a=Math.exp(mt(Yn,t));return P(-Math.log(i-o),{mean:l,sd:a,lowerTail:!1})}let n=Math.log(t),r=mt(Zn,n),s=Math.exp(mt(Jn,n));return P(o,{mean:r,sd:s,lowerTail:!1})}function or(e,t,o={}){let n;t!==void 0&&!Array.isArray(t)?o=t:n=t;let r=ft(e,n),s=o.varEqual??!1,i=r.length;if(i<2)throw new RangeError("not enough groups");let l=r.map(h=>h.length);if(l.some(h=>h<2))throw new RangeError("not enough observations");let a=r.map(Z),f=r.map(K),c=l.reduce((h,p)=>h+p,0),m,u;if(s){let h=Z(r.flat()),p=0,M=0;for(let d=0;d<i;d++)p+=l[d]*(a[d]-h)**2,M+=(l[d]-1)*f[d];u=c-i,m=p/(i-1)/(M/u)}else{let h=l.map((w,g)=>w/f[g]),p=h.reduce((w,g)=>w+g,0),M=0,d=0;for(let w=0;w<i;w++)M+=(1-h[w]/p)**2/(l[w]-1),d+=h[w]*a[w];M/=i*i-1,d/=p;let b=0;for(let w=0;w<i;w++)b+=h[w]*(a[w]-d)**2;m=b/((i-1)*(1+2*(i-2)*M)),u=1/(3*M)}return{method:s?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:m,statisticName:"F",parameter:[i-1,u],parameterName:["num df","denom df"],pValue:G(m,i-1,u,!1,!1)}}function sr(e){let t=Array.from(e).filter(r=>!Number.isNaN(r)).sort((r,s)=>r-s),o=t.length;if(o<3||o>5e3)throw new RangeError("sample size must be between 3 and 5000");if(!Number.isFinite(t[0])||!Number.isFinite(t[o-1]))throw new RangeError("'x' must be finite");if(t[o-1]===t[0])throw new RangeError("all 'x' values are identical");let n=Qe(t);return{method:"Shapiro-Wilk normality test",statistic:1-n,statisticName:"W",pValue:Ge(n,o)}}function re(e,t,o){let n=[0,0];for(let r=Math.max(0,t);r<Math.min(o,e.length);r++)n=F(n,[e[r],0]);return n[0]}function ar([[e,t],[o,n]]){let r=e+o,s=t+n,i=e+t,l=Math.max(0,i-s),a=Math.min(i,r),f=new Float64Array(a-l+1);for(let c=l;c<=a;c++)f[c-l]=ut(c,r,s,i,!1);return{lo:l,d:f}}function ir(e,t){let o=new Float64Array(e.length+t.length-1);for(let n=0;n<e.length;n++){let r=e[n];if(r!==0)for(let s=0;s<t.length;s++)o[n+s]+=r*t[s]}return o}var Ct=class{lo;hi;logNull;constructor(t){let o=0,n=new Float64Array([1]);for(let r of t){let s=ar(r);o+=s.lo,n=ir(n,s.d)}this.lo=o,this.hi=o+n.length-1,this.logNull=n.map(Math.log)}density(t){let o=this.logNull.length,n=new Float64Array(o);if(t===0||t===1/0)return n[t===0?0:o-1]=1,n;let r=Math.log(t),s=-1/0;for(let l=0;l<o;l++){let a=this.logNull[l]+(this.lo+l)*r;n[l]=a,a>s&&(s=a)}for(let l=0;l<o;l++)n[l]=Math.exp(n[l]-s);let i=re(n,0,o);for(let l=0;l<o;l++)n[l]=n[l]/i;return n}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let o=this.density(t),n=[0,0];for(let r=0;r<o.length;r++)n=F(n,[(this.lo+r)*o[r],0]);return n[0]}cdf(t,o,n){let r=this.density(o),s=t-this.lo;return n?re(r,s,r.length):re(r,0,s+1)}};function oe(e,t){return t?X(e,0,1):1/X(o=>e(1/o),Number.EPSILON,1)}function Ye(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let o=e.mean(1);return o===t?1:oe(n=>e.mean(n)-t,o>t)}function se(e,t,o){if(t===e.hi)return 1/0;let n=e.cdf(t,1,!1);return n===o?1:oe(r=>o-e.cdf(t,r,!1),n<o)}function ae(e,t,o){if(t===e.lo)return 0;let n=e.cdf(t,1,!0);return n===o?1:oe(r=>e.cdf(t,r,!0)-o,n>o)}function lr(e,t={}){let o=e.length;if(o<1)throw new RangeError("'x' must have at least one stratum");let n=e[0].length,r=e[0][0]?.length??0;if(n<2||r<2||o<2)throw new RangeError("each dimension in table must be >= 2");for(let c of e){if(c.length!==n||c.some(m=>m.length!==r))throw new RangeError("all strata must have the same dimensions");if(c.some(m=>m.some(u=>!Number.isFinite(u)||u<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}let s=e.map(c=>c.reduce((m,u)=>m+u.reduce((h,p)=>h+p,0),0));if(s.some(c=>c<2))throw new RangeError("sample size in each stratum must be > 1");let i=C(t.alternative),l=t.correct??!0,a=t.exact??!1,f=t.confLevel??.95;if(U(f),n===2&&r===2){let c=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return a?fr(c,i,f):cr(c,s,i,l,f)}return mr(e,s,n,r)}function cr(e,t,o,n,r){let s=0,i=0,l=0,a=0,f=0,c=0,m=0,u=0;e.forEach(([[N,y],[x,E]],R)=>{let k=t[R],v=N+y,T=x+E,I=N+x,A=y+E;s+=N,i+=v*I/k,l+=v*T*I*A/(k*k*(k-1));let L=(N+E)/k,j=(y+x)/k,O=N*E/k,H=y*x/k;a+=O,f+=H,c+=L*O,m+=L*H+j*O,u+=j*H});let h=s-i,p=n&&Math.abs(h)>=.5?.5:0,M=(Math.abs(h)-p)**2/l,d=o==="two.sided"?D(M,.5,2,!1,!1):P(Math.sign(h)*Math.sqrt(M),{lowerTail:o==="less"}),b=a/f,w=Math.sqrt(c/(2*a*a)+m/(2*a*f)+u/(2*f*f)),g;if(o==="less")g=[0,b*Math.exp(V(r)*w)];else if(o==="greater")g=[b*Math.exp(V(r,{lowerTail:!1})*w),1/0];else{let N=V((1+r)/2);g=[b*Math.exp(-N*w),b*Math.exp(N*w)]}return{method:`Mantel-Haenszel chi-squared test ${p!==0?"with":"without"} continuity correction`,alternative:o,statistic:M,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:d,confInt:g,confLevel:r,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function fr(e,t,o){let n=new Ct(e),r=e.reduce((l,a)=>l+a[0][0],0),s;if(t==="two.sided"){let l=n.density(1),a=l[r-n.lo]*(1+1e-7),f=[0,0];for(let c of l)c<=a&&(f=F(f,[c,0]));s=Math.min(1,f[0])}else s=n.cdf(r,1,t==="greater");let i;if(t==="less")i=[0,se(n,r,1-o)];else if(t==="greater")i=[ae(n,r,1-o),1/0];else{let l=(1-o)/2;i=[ae(n,r,l),se(n,r,l)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:r,statisticName:"S",pValue:s,confInt:i,confLevel:o,estimate:Ye(n,r),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function mr(e,t,o,n){let r=(o-1)*(n-1),s=new Array(r).fill(0),i=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((f,c)=>{let m=t[c],u=f.map(M=>M.reduce((d,b)=>d+b,0)),h=Array.from({length:n},(M,d)=>f.reduce((b,w)=>b+w[d],0)),p=1/(m*m*(m-1));for(let M=0;M<n-1;M++)for(let d=0;d<o-1;d++){let b=d+(o-1)*M;s[b]+=f[d][M]-u[d]*h[M]/m;for(let w=0;w<n-1;w++)for(let g=0;g<o-1;g++){let N=(d===g?m*u[d]:0)-u[d]*u[g],y=(M===w?m*h[M]:0)-h[M]*h[w];i[b][g+(o-1)*w]+=p*N*y}}});let l=Le(i,s),a=0;for(let f=0;f<r;f++)a+=s[f]*l[f];return{method:"Cochran-Mantel-Haenszel test",statistic:a,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:r,parameterName:"df",pValue:D(a,r/2,2,!1,!1)}}var Ze=new Map;function ur(e,t){let o=`${e},${t}`,n=Ze.get(o);if(n!==void 0)return n;let r=e+t,s=[];for(let u=1;u<=r;u++)s.push(Math.min(u,r+1-u));let i=s.reduce((u,h)=>u+h,0),l=Array.from({length:e+1},()=>new Float64Array(i+1));l[0][0]=1;for(let u=0;u<r;u++){let h=r-u,p=s[u],M=Array.from({length:e+1},()=>new Float64Array(i+1));for(let d=0;d<=e;d++){let b=l[d],w=e-d;if(w>h)continue;let g=w/h;for(let N=0;N<=i;N++){let y=b[N];y!==0&&(w>0&&(M[d+1][N+p]+=y*g),w<h&&(M[d][N]+=y*(1-g)))}}l=M}let a=l[e],f=0;for(;f<a.length&&a[f]===0;)f++;let c=a.length-1;for(;c>f&&a[c]===0;)c--;let m={min:f,probs:a.slice(f,c+1)};return Ze.set(o,m),m}function ie(e,t,o,n=!0){if(Number.isNaN(e)||!(t>=1)||!(o>=1)||t!==Math.floor(t)||o!==Math.floor(o))return NaN;let{min:r,probs:s}=ur(t,o),i=Math.floor(e+1e-7)-r,l=0;if(n)for(let a=0;a<=Math.min(i,s.length-1);a++)l+=s[a];else for(let a=Math.max(i+1,0);a<s.length;a++)l+=s[a];return Math.min(l,1)}var Je=new Map;function hr(e,t){let o=`${e},${t}`,n=Je.get(o);if(n!==void 0)return n;let r=e+t,s=0;for(let m=1;m<=r;m++)s+=Math.min(m,r+1-m);let i=Array.from({length:e+1},()=>new Float64Array(s+1));i[0][0]=1;for(let m=0;m<r;m++){let u=Math.min(m+1,r-m),h=Array.from({length:e+1},()=>new Float64Array(s+1));for(let p=0;p<=e;p++){if(e-p>r-m)continue;let M=i[p];for(let d=0;d<=s;d++){let b=M[d];b!==0&&(p<e&&(h[p+1][d+u]+=b),e-p<r-m&&(h[p][d]+=b))}}i=h}let l=i[e],a=0;for(;l[a]===0;)a++;let f=l.length-1;for(;l[f]===0;)f--;let c={min:a,counts:l.slice(a,f+1)};return Je.set(o,c),c}function Pt(e,t,o){let{min:n,counts:r}=hr(t,o);if(e<=0)return n;if(e>=1)return n+r.length-1;let s=Re(t+o,t),i=0;for(let l=0;l<r.length;l++)if(i+=r[l]/s,i>=e)return n+l;return n+r.length-1}function pr(e,t,o={}){let n=Array.from(e).filter(g=>!Number.isNaN(g)),r=Array.from(t).filter(g=>!Number.isNaN(g)),s=n.length,i=r.length;if(s<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let l=C(o.alternative),a=o.confInt??!1,f=o.confLevel??.95;a&&U(f);let c=s+i,m=W(n.concat(r)),u=m.map(g=>Math.min(g,c-g+1)),h=0;for(let g=0;g<s;g++)h+=u[g];let p=new Set(m).size<c,M=o.exact??(s<50&&i<50&&!p),d,b;if(M&&!p){let g=y=>ie(y-1,s,i,!1),N=y=>ie(y,s,i,!0);if(l==="two.sided"){let y=Math.floor((s+1)**2/4)+Math.floor(s*i/2)/2;d=Math.min(1,2*(h>y?g(h):N(h)))}else l==="less"?d=g(h):d=N(h);a&&(b=gr(n,r,l,f))}else{let g=0;for(let R of u)g+=R*R;let N=c%2===0,y=N?s*(c+2)/4:s*(c+1)**2/(4*c),x=N?s*i*(16*g-c*(c+2)**2)/(16*c*(c-1)):s*i*(16*c*g-(c+1)**4)/(16*c*c*(c-1)),E=(h-y)/Math.sqrt(x);d=l==="two.sided"?2*Math.min(P(E),P(E,{lowerTail:!1})):l==="less"?P(E,{lowerTail:!1}):P(E),a&&(b=wr(n,r,l,f))}let w={method:"Ansari-Bradley test",alternative:l,statistic:h,statisticName:"AB",pValue:d};return b!==void 0&&(w.confInt=b.confInt,w.confLevel=f,w.estimate=b.estimate,w.estimateNames="ratio of scales"),w.nullValue=1,w.nullValueName="ratio of scales",w}function tn(e,t,o){let n=e.length,r=n+t.length,s=dr(e.map(a=>a/o).concat(t)),i=s.map(a=>Math.min(a,r-a+1)),l=0;for(let a=0;a<n;a++)l+=i[a];return{ab:l,a:i,ties:new Set(s).size<r}}function dr(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return W(e);let o=W(t),n=0,r=t.length;return e.map(s=>Number.isNaN(s)?++r:o[n++])}function le(e,t,o){return e==="two.sided"?o(t):e==="greater"?[o(2*t)[0],1/0]:[0,o(2*t)[1]]}function gr(e,t,o,n){let r=e.length,s=t.length,i=[];for(let d of e)for(let b of t)d/b>=0&&i.push(d/b);i.sort((d,b)=>d-b);let l=i.length;if(l===0)return{confInt:le(o,1-n,()=>[NaN,NaN]),estimate:NaN};let a=i.map((d,b)=>tn(e,t,d+(b<l-1?(i[b+1]-d)/2:d*1.01)).ab),f=(d,b,w)=>{let g=1/0;for(let y of a)(b?y-d>0:y-d>=0)&&y-d<g&&(g=y-d);let N=w?-1/0:1/0;return a.forEach((y,x)=>{y-d===g&&(N=w?Math.max(N,i[x]):Math.min(N,i[x]))}),N},c=d=>{let b=Pt(d/2,r,s),w=Pt(1-d/2,r,s);if(!a.some(y=>y-b>=0)||!a.some(y=>y-w>0))return[NaN,NaN];let g=f(b,!1,!1),N=f(w,!0,!0);return g>N?[f(w,!1,!1),f(b,!0,!0)]:[g,N]},m=le(o,1-n,c),u=Pt(.5,r,s),h=-1/0,p=1/0;a.forEach((d,b)=>{d-u<=0?h=Math.max(h,i[b]):p=Math.min(p,i[b])});let M=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:M}}function wr(e,t,o,n){let r=e.length,s=t.length,i=r+s,l=i%2===0,a=(b,w)=>{let{ab:g,a:N,ties:y}=tn(e,t,b),x=l?g-r*(i+2)/4:g-r*(i+1)**2/(4*i),E;if(!y)E=Math.sqrt(l?r*s*(i+2)*(i-2)/(48*(i-1)):r*s*(i+1)*(3+i*i)/(48*i*i));else{let R=0;for(let k of N)R+=k*k;E=Math.sqrt(l?r*s*(16*R-i*(i+2)**2)/(16*i*(i-1)):r*s*(16*i*R-(i+1)**4)/(16*i*i*(i-1)))}return x/E-w},f=[],c=(b,w)=>{b.length===0||w.length===0||f.push(Math.min(...b)/Math.max(...w),Math.max(...b)/Math.min(...w))};if(c(e.filter(b=>b>0),t.filter(b=>b>0)),c(e.filter(b=>b<=0),t.filter(b=>b<0)),f.some(b=>!Number.isFinite(b)))return;let m=Math.min(...f),u=Math.max(...f),h=b=>X(w=>a(w,b),m,u,{tol:1e-4}),p=b=>{let w=V(b/2),g=V(b/2,{lowerTail:!1});if(a(m,w)>0||a(u,g)<0)return[NaN,NaN];let N=h(w),y=h(g);return N<=y?[N,y]:[y,N]},M=le(o,1-n,p),d=a(m,0)>0||a(u,0)<0?NaN:h(0);return{confInt:M,estimate:d}}var Nr=[1.2337005501361697,7831619385924639e-32];function br(e,t,o){let n=(c,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let M=c[h*t+p];if(M!==0)for(let d=0;d<t;d++)u[h*t+d]+=M*m[p*t+d]}return u},r=c=>{let m=0;for(let p of c)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<c.length;p++)c[p]*=h;return u},s,i=0,l=e.slice(),a=0,f=o;for(;f>0;)f&1&&(s=s===void 0?l.slice():n(s,l),i+=a,i+=r(s)),f>>=1,f>0&&(l=n(l,l),a*=2,a+=r(l));return[s,i]}function en(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[c,m]=vt(e,t),u=2*c-1+2*m,h=1,p=0;for(let M=1;M<=e;M++)h*=M/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let o=Math.floor(e*t)+1,n=2*o-1,r=o-e*t,s=new Float64Array(n*n);for(let c=0;c<n;c++)for(let m=0;m<n;m++)s[c*n+m]=c-m+1>=0?1:0;for(let c=0;c<n;c++)s[c*n]-=Math.pow(r,c+1),s[(n-1)*n+c]-=Math.pow(r,n-c);2*r-1>0&&(s[(n-1)*n]+=Math.pow(2*r-1,n));for(let c=0;c<n;c++)for(let m=0;m<n;m++){let u=c-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;s[c*n+m]/=h}}let[i,l]=br(s,n,e),a=i[(o-1)*n+o-1],f=l;for(let c=1;c<=e;c++)a=a*c/e,Math.abs(a)<2**-500&&(a*=2**500,f-=500);for(;f>1e3;)a*=2**1e3,f-=1e3;for(;f<-1e3;)a*=2**-1e3,f+=1e3;return a*2**f}function ce(e,t=!0){if(Number.isNaN(e))return NaN;if(e<=0)return t?0:1;if(e===1/0)return t?1:0;if(e<1){let r=Et(Nr,vt(e,e)),s=r[0]+r[1],i=Math.exp(-r[0])*(1-r[1]),l=0;for(let f=2;f<100;f++){let c=Math.exp(-(2*f-1)*(2*f-1)*s);if(l+=c,c<=1e-17*(l+i))break}let a=Math.sqrt(2*Math.PI)/e*(i+l);return t?a:1-a}let o=0;for(let r=1;r<100;r++){let s=Math.exp(-2*r*r*e*e);if(o+=r%2===1?s:-s,s<=1e-17*Math.abs(o))break}let n=2*o;return t?1-n:n}function nn(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[o,n]=vt(e,t),r=Math.floor(e-o-n),s=0;for(let i=0;i<=r;i++){let l=F(jt(e-i,-o),[-n,0]);if(l[0]<=0)break;let a=F(jt(i,o),[n,0]),f=Et(a,[e,0]),c=Et(l,[e,0]);s+=we(i,e-i,f,c,!1)/(f[0]+f[1])}return Math.min(t*s,1)}function rn(e,t,o,n={}){let r=n.alternative??"two.sided";if(Number.isNaN(e)||!(t>=1)||!(o>=1)||t!==Math.floor(t)||o!==Math.floor(o))return NaN;if(e<=0)return 1;if(e>1)return 0;let s=t+o,i;if(n.z!==void 0){if(n.z.length!==s)throw new RangeError("z must have length m + n");let u=[...n.z].sort((h,p)=>h-p);i=new Array(s+1).fill(!1);for(let h=1;h<s;h++)i[h]=u[h]!==u[h-1]}let l=e*t*o*(1-1e-10),a=(u,h)=>{let p=u*o-h*t;return(r==="two.sided"?Math.abs(p):r==="greater"?p:-p)>=l},f=new Float64Array(o+1),c=new Float64Array(o+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=o;h++){if(u===0&&h===0){c[0]=1;continue}let p=u+h,M=(u>0?u/p*f[h]:0)+(h>0?h/p*c[h-1]:0);(i===void 0?p<s:i[p])&&a(u,h)?(M>0&&(m+=M*ut(u,t,o,p,!1)),c[h]=0):c[h]=M}[f,c]=[c,f],c.fill(0)}return Math.min(m,1)}function on(e,t){return e==="two.sided"?"two-sided":`the CDF of x lies ${e==="less"?"below":"above"} ${t?"the null hypothesis":"that of y"}`}function Mr(e,t,o={}){let n=Array.from(e).filter(w=>!Number.isNaN(w)).sort((w,g)=>w-g),r=n.length;if(r<1)throw new RangeError("not enough 'x' data");let s=C(o.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let w=-1/0,g=-1/0;for(let R=0;R<r;R++){let k=t(n[R]);w=Math.max(w,(R+1)/r-k),g=Math.max(g,k-R/r)}let N=s==="two.sided"?Math.max(w,g):s==="greater"?w:g,y=new Set(n).size<r,x=o.exact??(r<100&&!y),E;return s==="two.sided"?E=x?1-en(r,N):ce(Math.sqrt(r)*N,!1):E=x?nn(r,N):Math.exp(-2*r*N*N),{method:`${x?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:on(s,!0),exact:x,statistic:N,statisticName:i,pValue:Math.min(1,Math.max(0,E))}}let l=Array.from(t).filter(w=>!Number.isNaN(w)),a=l.length;if(a<1)throw new RangeError("not enough 'y' data");let f=n.map(w=>[w,0]).concat(l.map(w=>[w,1]));f.sort((w,g)=>w[0]-g[0]);let c=0,m=0,u=0,h=0;for(let w=0;w<f.length;w++){if(f[w][1]===0?c++:m++,w+1<f.length&&f[w+1][0]===f[w][0])continue;let g=c/r-m/a;u=Math.max(u,g),h=Math.max(h,-g)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,M=new Set(f.map(w=>w[0])).size<f.length,d=o.exact??r*a<1e4,b;if(d)b=rn(p,r,a,M?{alternative:s,z:f.map(w=>w[0])}:{alternative:s});else{let w=r*a/(r+a);b=s==="two.sided"?ce(Math.sqrt(w)*p,!1):Math.exp(-2*w*p*p)}return{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:on(s,!1),exact:d,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,b))}}function sn(e,t,o){let n=Array.from(e);if(n.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let r=new Map;for(let i=0;i<n.length;i++){let l=t[i];if(typeof l=="number"&&Number.isNaN(l)||o&&Number.isNaN(n[i]))continue;let a=r.get(l);a===void 0&&r.set(l,a=[]),a.push(n[i])}let s=Array.from(r.keys()).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0);return{levels:s,groups:s.map(i=>r.get(i))}}function fe(e,t,o){let n=[],r=[];for(let l=0;l<e-1;l++)for(let a=l+1;a<e;a++)n.push(t(a,l)),r.push([a-1,l]);let s=Yt(n,o),i=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return r.forEach(([l,a],f)=>i[l][a]=s[f]),i}function me(e,t,o,n){return{method:e,pValue:o,rowNames:t.slice(1).map(String),colNames:t.slice(0,-1).map(String),pAdjustMethod:n}}function yr(e,t,o={}){let n=o.paired??!1,r=o.poolSd??!n;if(n&&r)throw new RangeError("pooling of SD is incompatible with paired tests");let s=C(o.alternative),i=o.pAdjustMethod??"holm",{levels:l,groups:a}=sn(e,t,!n),f=l.length;if(f<2)throw new RangeError("there must be at least two groups");let c,m;if(r){m="t tests with pooled SD";let u=a.map(b=>b.length),h=a.map(b=>b.length>0?Z(b):NaN),p=0,M=0;a.forEach(b=>{b.length>1&&(p+=K(b)*(b.length-1),M+=b.length-1)});let d=Math.sqrt(p/M);c=(b,w)=>{let g=(h[b]-h[w])/(d*Math.sqrt(1/u[b]+1/u[w]));return s==="less"?Y(g,M,!0,!1):s==="greater"?Y(g,M,!1,!1):2*Y(-Math.abs(g),M,!0,!1)}}else m=n?"paired t tests":"t tests with non-pooled SD",c=(u,h)=>Ht(a[u],a[h],{paired:n,alternative:s}).pValue;return me(m,l,fe(f,c,i),i)}function vr(e,t,o={}){let{pAdjustMethod:n,alternative:r,...s}=o,i=n??"holm",{levels:l,groups:a}=sn(e,t,!(s.paired??!1)),f=l.length;if(f<2)throw new RangeError("there must be at least two groups");let c="",m=fe(f,(u,h)=>{let p=Xt(a[u],a[h],{...s,...r===void 0?{}:{alternative:r}});return c=p.method,p.pValue},i);return me(c,l,m,i)}function xr(e,t,o={}){if(e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let n=[],r=[];e.forEach((c,m)=>{Number.isNaN(c)||Number.isNaN(t[m])||(n.push(c),r.push(t[m]))});let s=n.length;if(s<2)throw new RangeError("too few groups");let i=o.pAdjustMethod??"holm",l=o.correct??!0,a=fe(s,(c,m)=>Jt([n[c],n[m]],[r[c],r[m]],{correct:l}).pValue,i),f=Array.from({length:s},(c,m)=>m+1);return me("Pairwise comparison of proportions",f,a,i)}function an(e){let t=e.flat(),o=Nt(t),n=[0,0],r=[0,0],s=[];for(let i of e){let l=Nt(i);s.push(l[0]);let a=Q(l,o);n=F(n,xt(xt(a,a),[i.length,0]));for(let f of i){let c=Q([f,0],l);r=F(r,xt(c,c))}}return{ssb:n[0],ssw:r[0],means:s}}function Er(e,t){let o=ft(e,t),n=o.length;if(n<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let r=o.reduce((u,h)=>u+h.length,0),{ssb:s,ssw:i}=an(o),l=n-1,a=r-n,f=s/l,c=i/a,m=f/c;return{df:[l,a],sumSq:[s,i],meanSq:[f,c],fValue:m,pValue:a>0?G(m,l,a,!1,!1):NaN}}function Ar(e,t,o={}){let n;t!==void 0&&!Array.isArray(t)?o=t:n=t;let{labels:r,groups:s}=te(e,n),i=s.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let l=o.confLevel??.95,f=s.reduce((M,d)=>M+d.length,0)-i,{ssw:c,means:m}=an(s),u=c/f,h=ve(l,i,f),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let M=0;M<i-1;M++)for(let d=M+1;d<i;d++){let b=m[d]-m[M],w=Math.sqrt(u/2*(1/s[d].length+1/s[M].length));p.names.push(`${r[d]}-${r[M]}`),p.i.push(d+1),p.j.push(M+1),p.diff.push(b),p.lwr.push(b-h*w),p.upr.push(b+h*w),p.pAdj.push(ye(Math.abs(b)/w,i,f,{lowerTail:!1}))}return p}function Rr(e,t,o={}){let n;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?o=t:n=t;let{sigmaX:r,sigmaY:s}=o,i=o.mu??0;if(r===void 0&&n===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(r===void 0||n!==void 0&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(i))throw new RangeError("mu must be a number");let l=C(o.alternative),a=o.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let f=Array.from(e).filter(g=>!Number.isNaN(g)),c=n===void 0?void 0:Array.from(n).filter(g=>!Number.isNaN(g));if(f.length<3)throw new RangeError("x needs at least 3 non-missing values");if(c!==void 0&&c.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Nt(f)[0],u=c===void 0?NaN:Nt(c)[0],h=c===void 0?r/Math.sqrt(f.length):Math.sqrt(r**2/f.length+s**2/c.length),p=c===void 0?m:m-u,M=(p-i)/h,d,b;if(l==="less")d=P(M),b=[-1/0,p+V(a)*h];else if(l==="greater")d=P(M,{lowerTail:!1}),b=[p-V(a)*h,1/0];else{d=2*P(-Math.abs(M));let g=V(1-(1-a)/2)*h;b=[p-g,p+g]}let w=c!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:l,statistic:M,statisticName:"z",pValue:d,confInt:b,confLevel:a,estimate:w?[m,u]:m,estimateNames:w?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:w?"difference in means":"mean"}}function qr(e,t,o={}){let n=o.intercept??!0,r=e.length>0&&typeof e[0]!="number"?e.map(q=>Array.from(q)):Array.from(e,q=>[q]),s=Array.from(t);if(r.length!==s.length)throw new RangeError("variable lengths differ (found for 'x')");let i=o.weights===void 0?void 0:Array.from(o.weights);if(i!==void 0&&i.length!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(i?.some(q=>q<0))throw new RangeError("missing or negative weights not allowed");let l=s.map((q,S)=>!Number.isNaN(q)&&r[S].every(_=>!Number.isNaN(_))&&(i===void 0||!Number.isNaN(i[S]))),a=r.filter((q,S)=>l[S]).map(q=>n?[1,...q]:q),f=s.filter((q,S)=>l[S]),c=i?.filter((q,S)=>l[S]);if(f.length===0)throw new RangeError("0 (non-NA) cases");let m=f.map((q,S)=>c===void 0||c[S]!==0),u=a.filter((q,S)=>m[S]),h=f.filter((q,S)=>m[S]),p=c?.filter((q,S)=>m[S]),M=h.length;if(M===0)throw new RangeError("all weights are zero");let d=p?.map(Math.sqrt),b=d===void 0?u:u.map((q,S)=>q.map(_=>_*d[S])),w=d===void 0?h:h.map((q,S)=>q*d[S]),g=ke(b),N=g.rank,y=Ie(g,w),x=Se(g,w),E=d===void 0?x:x.map((q,S)=>q/d[S]),R=[],k=[],v=0;f.forEach((q,S)=>{if(m[S])R.push(E[v]),k.push(q-E[v]),v++;else{let _=a[S].reduce((yt,ln,pe)=>Number.isNaN(y[pe])?yt:yt+ln*y[pe],0);k.push(_),R.push(q-_)}});let T=k.filter((q,S)=>m[S]),I=M-N,A=p??new Array(M).fill(1),L=0;for(let q=0;q<M;q++)L+=A[q]*E[q]**2;let j=L/I,O=Math.sqrt(j),H=g.qr.slice(0,N).map(q=>q.slice(0,N)),z=N>0?Ve(H):[],$=g.pivot.slice(0,N).map((q,S)=>[q,S]).sort((q,S)=>q[0]-S[0]),B=$.map(([q])=>y[q]),nt=$.map(([,q])=>Math.sqrt(z[q][q]*j)),rt=B.map((q,S)=>q/nt[S]),dt=rt.map(q=>2*Y(-Math.abs(q),I,!0,!1)),ot={coefficients:y,residuals:R,fittedValues:k,rank:N,dfResidual:I,coefTable:{estimate:B,stdError:nt,tValue:rt,pValue:dt},sigma:O,rSquared:0,adjRSquared:0,intercept:n,qr:g},ue=n?1:0,Mt=N-ue;if(Mt===0)return ot;let gt=0;if(n){let q=A.reduce((_,yt)=>_+yt,0),S=0;for(let _=0;_<M;_++)S+=A[_]*T[_]/q;for(let _=0;_<M;_++)gt+=A[_]*(T[_]-S)**2}else for(let q=0;q<M;q++)gt+=A[q]*T[q]**2;ot.rSquared=gt/(gt+L),ot.adjRSquared=1-(1-ot.rSquared)*((M-ue)/I);let he=gt/Mt/j;return ot.fStatistic={value:he,numdf:Mt,dendf:I},ot.fPValue=G(he,Mt,I,!1,!1),ot}function Tr(e,t={}){let o=t.level??.95,n=it((1+o)/2,e.dfResidual,!0,!1),{estimate:r,stdError:s}=e.coefTable,i=e.coefficients.map(()=>NaN),l=e.coefficients.map(()=>NaN),a=0;return e.coefficients.forEach((f,c)=>{Number.isNaN(f)||(i[c]=r[a]-n*s[a],l[c]=r[a]+n*s[a],a++)}),{lower:i,upper:l}}function kr(e,t,o={}){let r=(t.length>0&&typeof t[0]!="number"?t.map(p=>Array.from(p)):Array.from(t,p=>[p])).map(p=>e.intercept?[1,...p]:p),s=e.rank,i=e.qr.pivot.slice(0,s),l=e.qr.qr.slice(0,s).map(p=>p.slice(0,s)),a=e.sigma**2,f=r.map(p=>p.reduce((M,d,b)=>Number.isNaN(e.coefficients[b])?M:M+d*e.coefficients[b],0)),c=r.map(p=>{let M=new Array(s).fill(0);for(let d=0;d<s;d++){let b=p[i[d]];for(let w=0;w<d;w++)b-=l[w][d]*M[w];M[d]=b/l[d][d]}return Math.sqrt(M.reduce((d,b)=>d+b*b,0)*a)}),m=o.interval??"none";if(m==="none")return{fit:f,seFit:c};let u=it((1+(o.level??.95))/2,e.dfResidual,!0,!1),h=c.map(p=>u*Math.sqrt(p*p+(m==="prediction"?a:0)));return{fit:f,seFit:c,lwr:f.map((p,M)=>p-h[M]),upr:f.map((p,M)=>p+h[M])}}export{Ar as TukeyHSD,pr as ansariTest,Er as aov,Hn as bartlettTest,Zt as binomTest,mn as chisqTest,Tr as confint,cn as corTest,wn as fisherTest,Kn as flignerTest,Bn as friedmanTest,Wn as kruskalTest,Mr as ksTest,qr as lm,lr as mantelhaenTest,zn as mcnemarTest,Un as moodTest,or as onewayTest,Yt as pAdjust,xr as pairwisePropTest,yr as pairwiseTTest,vr as pairwiseWilcoxTest,On as poissonTest,kr as predict,Jt as propTest,Dn as quadeTest,sr as shapiroTest,Ht as tTest,$n as varTest,Xt as wilcoxTest,Rr as zTest};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@numero-uno-jpn/rstats",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "R-compatible statistical functions for the browser: distributions, random numbers that match set.seed(), hypothesis tests and linear algebra, tested against R.",
5
5
  "keywords": [
6
6
  "statistics",
@@ -30,6 +30,30 @@
30
30
  ".": {
31
31
  "types": "./dist/index.d.ts",
32
32
  "import": "./dist/index.js"
33
+ },
34
+ "./distributions": {
35
+ "types": "./dist/entries/distributions.d.ts",
36
+ "import": "./dist/entries/distributions.js"
37
+ },
38
+ "./tests": {
39
+ "types": "./dist/entries/tests.d.ts",
40
+ "import": "./dist/entries/tests.js"
41
+ },
42
+ "./linalg": {
43
+ "types": "./dist/entries/linalg.d.ts",
44
+ "import": "./dist/entries/linalg.js"
45
+ },
46
+ "./descriptive": {
47
+ "types": "./dist/entries/descriptive.d.ts",
48
+ "import": "./dist/entries/descriptive.js"
49
+ },
50
+ "./numerics": {
51
+ "types": "./dist/entries/numerics.d.ts",
52
+ "import": "./dist/entries/numerics.js"
53
+ },
54
+ "./special": {
55
+ "types": "./dist/entries/special.d.ts",
56
+ "import": "./dist/entries/special.js"
33
57
  }
34
58
  },
35
59
  "types": "./dist/index.d.ts",
@@ -63,6 +87,7 @@
63
87
  "devDependencies": {
64
88
  "@types/node": "^26.6.5",
65
89
  "esbuild": "^0.28.2",
90
+ "jstat": "1.9.6",
66
91
  "playwright": "^1.64.1",
67
92
  "typescript": "^7.0.2",
68
93
  "vitest": "^5.0.5"