@numero-uno-jpn/rstats 0.15.3 → 0.17.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 (322) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/README.ja.md +68 -35
  3. package/README.md +65 -30
  4. package/dist/chunks/chunk-2C33MXSW.js +3 -0
  5. package/dist/chunks/chunk-34S5SQY6.js +2 -0
  6. package/dist/chunks/chunk-4WE3OO62.js +2 -0
  7. package/dist/chunks/chunk-5AYHCITP.js +2 -0
  8. package/dist/chunks/chunk-6JJQL6HL.js +2 -0
  9. package/dist/chunks/chunk-C7V2FH6L.js +3 -0
  10. package/dist/chunks/chunk-CZI7KUO3.js +2 -0
  11. package/dist/chunks/chunk-DXI5N2ET.js +2 -0
  12. package/dist/chunks/chunk-FY3FVOBM.js +2 -0
  13. package/dist/chunks/chunk-H3WVPZDL.js +2 -0
  14. package/dist/chunks/chunk-LHUIJQHY.js +2 -0
  15. package/dist/chunks/chunk-LV3W2F7N.js +2 -0
  16. package/dist/chunks/chunk-M4ITG7YR.js +2 -0
  17. package/dist/chunks/chunk-NSYZTS26.js +2 -0
  18. package/dist/chunks/chunk-ONY5CISH.js +2 -0
  19. package/dist/chunks/chunk-RFE7MVP7.js +2 -0
  20. package/dist/chunks/chunk-SE7V2KLO.js +2 -0
  21. package/dist/chunks/chunk-THIGHOKK.js +2 -0
  22. package/dist/chunks/chunk-TNMJPQPV.js +2 -0
  23. package/dist/chunks/chunk-WM7VFXTV.js +2 -0
  24. package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
  25. package/dist/chunks/chunk-XLSFJTTK.js +2 -0
  26. package/dist/chunks/chunk-Z37NELOS.js +2 -0
  27. package/dist/chunks/chunk-ZFQWB222.js +2 -0
  28. package/dist/data/grouping.js +26 -7
  29. package/dist/descriptive/basic.js +110 -8
  30. package/dist/descriptive/cov.js +23 -4
  31. package/dist/descriptive/dataFrames.d.ts +4 -2
  32. package/dist/descriptive/dataFrames.js +69 -23
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +26 -9
  35. package/dist/descriptive/hist.d.ts +8 -5
  36. package/dist/descriptive/hist.js +77 -31
  37. package/dist/descriptive/missing.js +3 -1
  38. package/dist/descriptive/moments.js +86 -19
  39. package/dist/descriptive/omega.d.ts +1 -1
  40. package/dist/descriptive/omega.js +74 -10
  41. package/dist/descriptive/pivot.d.ts +25 -10
  42. package/dist/descriptive/pivot.js +182 -42
  43. package/dist/descriptive/quantile.js +10 -1
  44. package/dist/descriptive/rank.js +8 -1
  45. package/dist/descriptive/table.d.ts +23 -3
  46. package/dist/descriptive/table.js +36 -21
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.js +27 -2
  49. package/dist/distributions/beta.js +33 -4
  50. package/dist/distributions/binomial.js +46 -14
  51. package/dist/distributions/bvnorm.js +11 -2
  52. package/dist/distributions/cauchy.js +21 -4
  53. package/dist/distributions/chisq.js +16 -3
  54. package/dist/distributions/exponential.js +14 -0
  55. package/dist/distributions/f.js +41 -17
  56. package/dist/distributions/gamma.js +35 -3
  57. package/dist/distributions/geometric.js +58 -13
  58. package/dist/distributions/hypergeometric.d.ts +5 -1
  59. package/dist/distributions/hypergeometric.js +101 -6
  60. package/dist/distributions/invgamma.js +10 -1
  61. package/dist/distributions/kumaraswamy.js +9 -0
  62. package/dist/distributions/laplace.js +36 -6
  63. package/dist/distributions/logistic.js +13 -1
  64. package/dist/distributions/lognormal.js +16 -2
  65. package/dist/distributions/multinomial.js +58 -11
  66. package/dist/distributions/nbeta.js +62 -11
  67. package/dist/distributions/nchisq.js +30 -6
  68. package/dist/distributions/negbinomial.js +102 -17
  69. package/dist/distributions/normal.js +17 -4
  70. package/dist/distributions/nt.js +392 -17
  71. package/dist/distributions/pareto.js +12 -1
  72. package/dist/distributions/poisson.js +37 -3
  73. package/dist/distributions/signrank.js +16 -4
  74. package/dist/distributions/summary.d.ts +2 -1
  75. package/dist/distributions/summary.js +481 -61
  76. package/dist/distributions/t.js +9 -0
  77. package/dist/distributions/triangular.js +19 -5
  78. package/dist/distributions/tukey.js +11 -2
  79. package/dist/distributions/uniform.js +8 -0
  80. package/dist/distributions/weibull.js +18 -0
  81. package/dist/distributions/wilcox.d.ts +9 -3
  82. package/dist/distributions/wilcox.js +164 -26
  83. package/dist/distributions.js +2 -2
  84. package/dist/entries/descriptive.d.ts +3 -1
  85. package/dist/entries/descriptive.js +1 -1
  86. package/dist/entries/distributions.d.ts +1 -0
  87. package/dist/entries/distributions.js +2 -0
  88. package/dist/entries/linalg.d.ts +3 -1
  89. package/dist/entries/mixed.d.ts +3 -0
  90. package/dist/entries/mixed.js +2 -0
  91. package/dist/entries/numerics.d.ts +2 -1
  92. package/dist/entries/tests.d.ts +8 -5
  93. package/dist/entries/tests.js +3 -1
  94. package/dist/htest/agreement.d.ts +16 -2
  95. package/dist/htest/agreement.js +122 -21
  96. package/dist/htest/ansariTest.d.ts +8 -2
  97. package/dist/htest/ansariTest.js +116 -18
  98. package/dist/htest/aov.d.ts +29 -23
  99. package/dist/htest/aov.js +90 -47
  100. package/dist/htest/binomTest.d.ts +5 -0
  101. package/dist/htest/binomTest.js +47 -17
  102. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  103. package/dist/htest/brunnerMunzelTest.js +12 -5
  104. package/dist/htest/chisqTest.d.ts +5 -2
  105. package/dist/htest/chisqTest.js +151 -46
  106. package/dist/htest/cor.d.ts +9 -2
  107. package/dist/htest/cor.js +57 -26
  108. package/dist/htest/core/ansari.d.ts +9 -0
  109. package/dist/htest/core/ansari.js +21 -1
  110. package/dist/htest/core/kolmogorov.d.ts +25 -0
  111. package/dist/htest/core/kolmogorov.js +123 -0
  112. package/dist/htest/core/mantelhaen.js +6 -4
  113. package/dist/htest/core/shapiro.d.ts +1 -1
  114. package/dist/htest/core/shapiro.js +5 -1
  115. package/dist/htest/core/simulate.d.ts +6 -0
  116. package/dist/htest/core/simulate.js +20 -0
  117. package/dist/htest/dunnett.js +14 -5
  118. package/dist/htest/effectSize.d.ts +5 -0
  119. package/dist/htest/effectSize.js +117 -35
  120. package/dist/htest/fisherTest.d.ts +4 -3
  121. package/dist/htest/fisherTest.js +88 -24
  122. package/dist/htest/groups.d.ts +7 -0
  123. package/dist/htest/groups.js +21 -4
  124. package/dist/htest/ksTest.d.ts +7 -1
  125. package/dist/htest/ksTest.js +36 -6
  126. package/dist/htest/leveneTest.d.ts +2 -0
  127. package/dist/htest/leveneTest.js +28 -6
  128. package/dist/htest/mantelhaenTest.js +132 -25
  129. package/dist/htest/mcnemarTest.js +40 -11
  130. package/dist/htest/multipleComparisons.d.ts +24 -26
  131. package/dist/htest/multipleComparisons.js +70 -64
  132. package/dist/htest/normalityTests.js +54 -14
  133. package/dist/htest/onewayTest.js +106 -5
  134. package/dist/htest/pAdjust.d.ts +3 -2
  135. package/dist/htest/pAdjust.js +42 -6
  136. package/dist/htest/pairwise.d.ts +3 -1
  137. package/dist/htest/pairwise.js +46 -21
  138. package/dist/htest/poissonTest.d.ts +1 -1
  139. package/dist/htest/poissonTest.js +117 -59
  140. package/dist/htest/polycor.js +47 -17
  141. package/dist/htest/power.d.ts +15 -1
  142. package/dist/htest/power.js +128 -10
  143. package/dist/htest/propTest.d.ts +3 -0
  144. package/dist/htest/propTest.js +44 -6
  145. package/dist/htest/pwr.d.ts +38 -1
  146. package/dist/htest/pwr.js +159 -39
  147. package/dist/htest/rankTests.js +36 -6
  148. package/dist/htest/tTest.d.ts +2 -0
  149. package/dist/htest/tTest.js +53 -21
  150. package/dist/htest/testFormula.js +11 -11
  151. package/dist/htest/trendTests.d.ts +28 -4
  152. package/dist/htest/trendTests.js +124 -35
  153. package/dist/htest/types.d.ts +7 -0
  154. package/dist/htest/types.js +7 -0
  155. package/dist/htest/varTest.js +52 -14
  156. package/dist/htest/wilcoxTest.js +164 -29
  157. package/dist/integrate/quadpack.js +27 -5
  158. package/dist/internal/checkOptions.d.ts +11 -0
  159. package/dist/internal/checkOptions.js +31 -0
  160. package/dist/internal/covmat.d.ts +5 -0
  161. package/dist/internal/covmat.js +20 -0
  162. package/dist/internal/ddexp.d.ts +15 -1
  163. package/dist/internal/ddexp.js +59 -6
  164. package/dist/internal/discrete.d.ts +14 -0
  165. package/dist/internal/discrete.js +78 -5
  166. package/dist/internal/extremes.d.ts +9 -0
  167. package/dist/internal/extremes.js +24 -0
  168. package/dist/internal/fround.js +13 -4
  169. package/dist/internal/integer.d.ts +9 -0
  170. package/dist/internal/integer.js +12 -0
  171. package/dist/internal/invert.js +15 -2
  172. package/dist/internal/optionalArgs.d.ts +12 -0
  173. package/dist/internal/optionalArgs.js +46 -0
  174. package/dist/internal/scaledInverse.d.ts +10 -0
  175. package/dist/internal/scaledInverse.js +22 -0
  176. package/dist/internal/seq.js +15 -2
  177. package/dist/internal/summary.d.ts +90 -0
  178. package/dist/internal/summary.js +458 -39
  179. package/dist/internal/uniroot.d.ts +7 -0
  180. package/dist/internal/uniroot.js +28 -3
  181. package/dist/internal/warnings.d.ts +66 -0
  182. package/dist/internal/warnings.js +122 -0
  183. package/dist/linalg/basic.d.ts +3 -1
  184. package/dist/linalg/basic.js +58 -7
  185. package/dist/linalg/chol.js +10 -1
  186. package/dist/linalg/dot.d.ts +29 -0
  187. package/dist/linalg/dot.js +132 -0
  188. package/dist/linalg/eigen.d.ts +6 -1
  189. package/dist/linalg/eigen.js +75 -14
  190. package/dist/linalg/expm.d.ts +3 -2
  191. package/dist/linalg/expm.js +113 -15
  192. package/dist/linalg/kappa.js +64 -10
  193. package/dist/linalg/lu.d.ts +2 -1
  194. package/dist/linalg/lu.js +40 -5
  195. package/dist/linalg/matrix.d.ts +14 -0
  196. package/dist/linalg/matrix.js +24 -0
  197. package/dist/linalg/norm.js +4 -0
  198. package/dist/linalg/ops.d.ts +27 -2
  199. package/dist/linalg/ops.js +74 -26
  200. package/dist/linalg/qr.d.ts +2 -1
  201. package/dist/linalg/qr.js +105 -23
  202. package/dist/linalg/schur.js +11 -3
  203. package/dist/linalg/solve.d.ts +6 -1
  204. package/dist/linalg/solve.js +156 -26
  205. package/dist/linalg/svd.d.ts +9 -2
  206. package/dist/linalg/svd.js +33 -11
  207. package/dist/linalg.js +2 -2
  208. package/dist/mixed.js +4 -4
  209. package/dist/models/anovaSequential.d.ts +32 -2
  210. package/dist/models/anovaSequential.js +170 -28
  211. package/dist/models/aovEz.js +154 -39
  212. package/dist/models/aovStrata.js +68 -24
  213. package/dist/models/carAnova.d.ts +1 -1
  214. package/dist/models/carAnova.js +78 -33
  215. package/dist/models/coxph.d.ts +6 -0
  216. package/dist/models/coxph.js +166 -69
  217. package/dist/models/estimable.d.ts +15 -0
  218. package/dist/models/estimable.js +68 -0
  219. package/dist/models/formula.d.ts +1 -1
  220. package/dist/models/formula.js +26 -8
  221. package/dist/models/glm.d.ts +57 -7
  222. package/dist/models/glm.js +427 -74
  223. package/dist/models/glmAnova.js +90 -14
  224. package/dist/models/glmConfint.d.ts +1 -1
  225. package/dist/models/glmConfint.js +121 -29
  226. package/dist/models/glmFamily.d.ts +11 -6
  227. package/dist/models/glmFamily.js +247 -62
  228. package/dist/models/lm.d.ts +12 -1
  229. package/dist/models/lm.js +246 -37
  230. package/dist/models/lmer.d.ts +31 -7
  231. package/dist/models/lmer.js +129 -42
  232. package/dist/models/lmerAnova.d.ts +3 -3
  233. package/dist/models/lmerAnova.js +47 -9
  234. package/dist/models/lmerHypotheses.d.ts +6 -2
  235. package/dist/models/lmerHypotheses.js +14 -2
  236. package/dist/models/lmerTest.d.ts +13 -6
  237. package/dist/models/lmerTest.js +82 -29
  238. package/dist/models/mauchly.d.ts +6 -1
  239. package/dist/models/mauchly.js +41 -15
  240. package/dist/models/mixedStructure.js +6 -0
  241. package/dist/models/modelFrame.js +10 -4
  242. package/dist/models/modelMatrix.d.ts +3 -1
  243. package/dist/models/modelMatrix.js +47 -17
  244. package/dist/models/scaledTables.d.ts +8 -0
  245. package/dist/models/scaledTables.js +6 -0
  246. package/dist/models/survival.d.ts +12 -1
  247. package/dist/models/survival.js +75 -28
  248. package/dist/models/tiedTimes.d.ts +2 -0
  249. package/dist/models/tiedTimes.js +34 -0
  250. package/dist/numerics/deriv.d.ts +1 -1
  251. package/dist/numerics/deriv.js +72 -8
  252. package/dist/numerics/integrate.d.ts +9 -1
  253. package/dist/numerics/integrate.js +111 -16
  254. package/dist/numerics/interp.d.ts +40 -9
  255. package/dist/numerics/interp.js +375 -55
  256. package/dist/numerics/ode.js +59 -1
  257. package/dist/numerics.js +2 -2
  258. package/dist/optim/localmin.js +4 -0
  259. package/dist/optim/optim.d.ts +3 -1
  260. package/dist/optim/optim.js +97 -10
  261. package/dist/random/beta.js +58 -4
  262. package/dist/random/binomial.d.ts +14 -3
  263. package/dist/random/binomial.js +59 -24
  264. package/dist/random/continuous.js +50 -5
  265. package/dist/random/extra.js +55 -9
  266. package/dist/random/gamma.d.ts +4 -1
  267. package/dist/random/gamma.js +47 -8
  268. package/dist/random/hyper.js +9 -6
  269. package/dist/random/mt.js +17 -2
  270. package/dist/random/poisson.js +31 -3
  271. package/dist/random/r2dtable.d.ts +8 -1
  272. package/dist/random/r2dtable.js +24 -3
  273. package/dist/random/rank.d.ts +2 -0
  274. package/dist/random/rank.js +44 -4
  275. package/dist/random/rng.js +4 -0
  276. package/dist/random/sample.d.ts +12 -2
  277. package/dist/random/sample.js +63 -20
  278. package/dist/random/streams.js +4 -2
  279. package/dist/rstats.js +17 -14
  280. package/dist/special/besselIK.d.ts +12 -0
  281. package/dist/special/besselIK.js +175 -23
  282. package/dist/special/besselJY.d.ts +11 -1
  283. package/dist/special/besselJY.js +127 -19
  284. package/dist/special/besselSeries.d.ts +27 -0
  285. package/dist/special/besselSeries.js +45 -2
  286. package/dist/special/beta.js +4 -1
  287. package/dist/special/choose.js +24 -8
  288. package/dist/special/dd.js +122 -18
  289. package/dist/special/extra.js +14 -2
  290. package/dist/special/gamma.js +11 -2
  291. package/dist/special/incbet.js +30 -4
  292. package/dist/special/incbetTemme.d.ts +7 -0
  293. package/dist/special/incbetTemme.js +45 -12
  294. package/dist/special/loader.d.ts +1 -1
  295. package/dist/special/loader.js +117 -20
  296. package/dist/special/log1pmx.js +4 -0
  297. package/dist/special/psigamma.js +7 -0
  298. package/dist/special.js +2 -2
  299. package/dist/tests.js +15 -11
  300. package/llms.txt +15 -6
  301. package/package.json +19 -10
  302. package/dist/chunks/chunk-3THFTB36.js +0 -2
  303. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  304. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  305. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  306. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  307. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  308. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  309. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  310. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  311. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  312. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  313. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  314. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  315. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  316. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  317. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  318. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  319. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  320. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  321. package/dist/chunks/chunk-VLUL2HFW.js +0 -3
  322. package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
@@ -48,8 +48,14 @@ export function ddExp(x) {
48
48
  */
49
49
  export function standardizeDD(x, location, scale) {
50
50
  const diff = x - location;
51
- if (!Number.isFinite(diff))
51
+ if (!Number.isFinite(diff)) {
52
+ // x - location overflows although both are finite: from the halves (exact), then times 2
53
+ if (Number.isFinite(x) && Number.isFinite(location)) {
54
+ const h = standardizeDD(x * 0.5, location * 0.5, scale);
55
+ return [h[0] * 2, h[1] * 2];
56
+ }
52
57
  return [diff / scale, 0];
58
+ }
53
59
  const d = location === 0 ? [x, 0] : twoSum(x, -location);
54
60
  if (scale === 1)
55
61
  return d;
@@ -86,11 +92,11 @@ function divideTail(d, scale, q1) {
86
92
  export function logRatioDD(a, b) {
87
93
  return ddSub(ddLog([a, 0]), ddLog([b, 0]));
88
94
  }
89
- /** a b as double-double. Only the double product when Veltkamp splitting would overflow (above 1e300) or for non-finite values */
95
+ /**
96
+ * a b as double-double: twoProd, which keeps the error term of a finite product also where Veltkamp splitting would
97
+ * overflow (it splits the operands scaled by powers of 2); only the double product when that is not finite
98
+ */
90
99
  export function mulDD(a, b) {
91
- const p = a * b;
92
- if (!Number.isFinite(p) || Math.abs(a) > 1e300 || Math.abs(b) > 1e300)
93
- return [p, 0];
94
100
  return twoProd(a, b);
95
101
  }
96
102
  /** exp(-t) (t is double-double). The low-order part of t enters to first order */
@@ -132,15 +138,62 @@ export function expNegOver(t, s) {
132
138
  export function log1mExp(x) {
133
139
  return x > -Math.LN2 ? Math.log(-Math.expm1(x)) : Math.log1p(-Math.exp(x));
134
140
  }
141
+ /** log(e^a + e^b) */
142
+ export function logAddExp(a, b) {
143
+ const m = Math.max(a, b);
144
+ // both ends: -Inf + -Inf and Inf + Inf (Inf - Inf in the shift would be NaN)
145
+ if (m === -Infinity || m === Infinity)
146
+ return m;
147
+ return m + Math.log1p(Math.exp(Math.min(a, b) - m));
148
+ }
149
+ /** log(e^a - e^b) for a >= b */
150
+ export function logSubExp(a, b) {
151
+ return b === -Infinity ? a : a + log1mExp(b - a);
152
+ }
135
153
  /** Log of the lower probability 1 - exp(-t) (t double-double, t > 0). For large t, exp(-t) includes the low-order part of t */
136
154
  export function logOneMinusExpNeg(t) {
137
155
  return t[0] > Math.LN2 ? Math.log1p(-expNeg(t)) : Math.log(-Math.expm1(-(t[0] + t[1])));
138
156
  }
139
157
  /** location + scale t with a single rounding (the rounding of scale t would show up in the subtraction with location) */
140
158
  export function locationScale(location, scale, t) {
159
+ // scale = Inf at the median (t = 0): location, the limit (Inf times 0 would be NaN; other t give ±Inf)
160
+ if (t === 0 && scale === Infinity)
161
+ return location;
141
162
  const st = mulDD(scale, t);
142
- if (!Number.isFinite(st[0]) || !Number.isFinite(location))
163
+ if (!Number.isFinite(st[0]) || !Number.isFinite(location)) {
164
+ // scale t overflows although the three are finite: location and scale divided by a power of 2 (exact) first, the
165
+ // result multiplied back (finite whenever it is representable)
166
+ if (Number.isFinite(location) && Number.isFinite(scale) && Number.isFinite(t)) {
167
+ const k = Math.ceil(Math.log2(Math.abs(scale)));
168
+ const r = ddAdd(mulDD(ldexpSafe(scale, -k), t), [ldexpSafe(location, -k), 0]);
169
+ return ldexpSafe(r[0] + r[1], k);
170
+ }
143
171
  return location + st[0];
172
+ }
144
173
  const r = ddAdd(st, [location, 0]);
145
174
  return r[0] + r[1];
146
175
  }
176
+ /** v 2^k in two steps (2^k alone may overflow or underflow) */
177
+ function ldexpSafe(v, k) {
178
+ const a = Math.trunc(k / 2);
179
+ return v * 2 ** a * 2 ** (k - a);
180
+ }
181
+ /**
182
+ * (x - location) / scale in double, as the plain formula gives it, but from the halves of x and location when their
183
+ * difference overflows although both are finite (z itself may be in range)
184
+ */
185
+ export function standardizeValue(x, location, scale) {
186
+ const diff = x - location;
187
+ if (Number.isFinite(diff) || !Number.isFinite(x) || !Number.isFinite(location))
188
+ return diff / scale;
189
+ return ((x * 0.5 - location * 0.5) / scale) * 2;
190
+ }
191
+ /** location + scale t in double, as the plain formula gives it, unless scale t overflows (then as locationScale) */
192
+ export function affine(location, scale, t) {
193
+ if (t === 0 && scale === Infinity)
194
+ return location; // the limit at the median, as in locationScale
195
+ const st = scale * t;
196
+ if (Number.isFinite(st) || !Number.isFinite(scale) || !Number.isFinite(t) || !Number.isFinite(location))
197
+ return location + st;
198
+ return locationScale(location, scale, t);
199
+ }
@@ -1,3 +1,5 @@
1
+ export declare const FUZZ_LINEAR: number;
2
+ export declare const FUZZ_LOG: number;
1
3
  export interface DiscreteQuantileProblem {
2
4
  /** Probability supplied by the user (0 < p < 1; log if logP) */
3
5
  p: number;
@@ -12,3 +14,15 @@ export interface DiscreteQuantileProblem {
12
14
  hi: number;
13
15
  }
14
16
  export declare function discreteQuantile(problem: DiscreteQuantileProblem): number;
17
+ /**
18
+ * For a predicate on the integers from lo on that is false up to some integer and true from there on: the first
19
+ * integer >= lo where it holds, at most hi + 1 (hi + 1 when none up to hi; hi may be Infinity). Found by doubling the
20
+ * step from lo and then bisecting, so in O(log) evaluations: the two-sided exact tests use it on the tail beyond the
21
+ * mode of a unimodal distribution, where "probability at most that of the observation" switches once
22
+ */
23
+ export declare function firstTrue(pred: (i: number) => boolean, lo: number, hi: number): number;
24
+ /**
25
+ * For a predicate on lo..hi that is true up to some integer and false from there on: the last integer <= hi where it
26
+ * holds (lo - 1 when none). By bisection (the mirror image of firstTrue, on a finite range)
27
+ */
28
+ export declare function lastTrue(pred: (i: number) => boolean, lo: number, hi: number): number;
@@ -6,8 +6,8 @@
6
6
  // The slack is applied to p on the scale the user supplied (lowerTail, logP). Its size is 8 ε on the linear scale
7
7
  // and 2 ε on the log scale, both chosen by comparison with R's answers (for p ≈ 1, converting to the smaller tail
8
8
  // before testing gives answers off by 1; the slack must be larger than 1.8 ε and smaller than 49 ε).
9
- const FUZZ_LINEAR = 8 * Number.EPSILON;
10
- const FUZZ_LOG = 2 * Number.EPSILON;
9
+ export const FUZZ_LINEAR = 8 * Number.EPSILON;
10
+ export const FUZZ_LOG = 2 * Number.EPSILON;
11
11
  /** Probabilities below this are near subnormal with few significant digits, so compare on the log scale */
12
12
  const TINY = 1e-290;
13
13
  export function discreteQuantile(problem) {
@@ -83,9 +83,19 @@ export function discreteQuantile(problem) {
83
83
  y = good;
84
84
  step *= 2;
85
85
  good = y + step;
86
- // Past the largest double (the step was stuck at Infinity before): the quantile lies beyond the doubles
87
- if (good === Infinity)
88
- return hi;
86
+ if (good === Infinity && hi === Infinity) {
87
+ // The step overflowed: the quantile may still lie below the largest double (a threshold at 1.7e308 searched from
88
+ // 1 gave Infinity), so test that before concluding it lies beyond the doubles. (With a finite hi, the loop ends
89
+ // and the search below runs up to hi)
90
+ const top = Number.MAX_VALUE;
91
+ const atTop = ok(top);
92
+ if (nan)
93
+ return NaN;
94
+ if (!atTop)
95
+ return hi;
96
+ good = top;
97
+ break;
98
+ }
89
99
  }
90
100
  if (good > hi)
91
101
  good = hi;
@@ -106,3 +116,66 @@ function bisect(bad, good, ok, stop) {
106
116
  }
107
117
  return good;
108
118
  }
119
+ /**
120
+ * For a predicate on the integers from lo on that is false up to some integer and true from there on: the first
121
+ * integer >= lo where it holds, at most hi + 1 (hi + 1 when none up to hi; hi may be Infinity). Found by doubling the
122
+ * step from lo and then bisecting, so in O(log) evaluations: the two-sided exact tests use it on the tail beyond the
123
+ * mode of a unimodal distribution, where "probability at most that of the observation" switches once
124
+ */
125
+ export function firstTrue(pred, lo, hi) {
126
+ if (lo > hi || pred(lo))
127
+ return lo;
128
+ let fails = lo; // largest integer known not to hold
129
+ let step = 1;
130
+ let j = lo + 1; // an integer known to hold, or hi + 1 (taken as holding, never evaluated)
131
+ for (;;) {
132
+ if (j > hi) {
133
+ j = hi + 1;
134
+ break;
135
+ }
136
+ if (pred(j))
137
+ break;
138
+ fails = j;
139
+ if (j === Number.MAX_VALUE)
140
+ return hi + 1;
141
+ step *= 2;
142
+ j = lo + step;
143
+ // past the largest double (hi = Infinity): try it once; Infinity was taken as the bound (a finite threshold gave
144
+ // Infinity, and a predicate false everywhere never stopped)
145
+ if (j === Infinity)
146
+ j = Number.MAX_VALUE;
147
+ }
148
+ while (j - fails > 1) {
149
+ const mid = fails + Math.floor((j - fails) / 2);
150
+ // beyond 2^53 the midpoint can round to an end, which then never moved (binomTest(0, 1e20) did not return)
151
+ if (mid === fails || mid === j)
152
+ break;
153
+ if (pred(mid))
154
+ j = mid;
155
+ else
156
+ fails = mid;
157
+ }
158
+ return j;
159
+ }
160
+ /**
161
+ * For a predicate on lo..hi that is true up to some integer and false from there on: the last integer <= hi where it
162
+ * holds (lo - 1 when none). By bisection (the mirror image of firstTrue, on a finite range)
163
+ */
164
+ export function lastTrue(pred, lo, hi) {
165
+ if (lo > hi || !pred(lo))
166
+ return lo - 1;
167
+ if (pred(hi))
168
+ return hi;
169
+ let j = lo;
170
+ let fails = hi; // smallest integer known not to hold
171
+ while (fails - j > 1) {
172
+ const mid = j + Math.floor((fails - j) / 2);
173
+ if (mid === fails || mid === j)
174
+ break; // (as in firstTrue)
175
+ if (pred(mid))
176
+ j = mid;
177
+ else
178
+ fails = mid;
179
+ }
180
+ return j;
181
+ }
@@ -0,0 +1,9 @@
1
+ /** Math.max(...v) without spreading (-Infinity for an empty array) */
2
+ export declare function maxOf(v: ArrayLike<number>): number;
3
+ /**
4
+ * (a + b) / 2, falling back to a / 2 + b / 2 only when a and b are finite and their sum overflows (so that the
5
+ * value is the usual one otherwise)
6
+ */
7
+ export declare function midpoint(a: number, b: number): number;
8
+ /** Math.min(...v) without spreading (Infinity for an empty array) */
9
+ export declare function minOf(v: ArrayLike<number>): number;
@@ -0,0 +1,24 @@
1
+ // Largest and smallest values of an array, as Math.max(...v) and Math.min(...v) (NaN if any value is NaN) but
2
+ // without spreading the array into arguments, which overflows the call stack for large arrays.
3
+ /** Math.max(...v) without spreading (-Infinity for an empty array) */
4
+ export function maxOf(v) {
5
+ let m = -Infinity;
6
+ for (let i = 0; i < v.length; i++)
7
+ m = Math.max(m, v[i]);
8
+ return m;
9
+ }
10
+ /**
11
+ * (a + b) / 2, falling back to a / 2 + b / 2 only when a and b are finite and their sum overflows (so that the
12
+ * value is the usual one otherwise)
13
+ */
14
+ export function midpoint(a, b) {
15
+ const m = 0.5 * (a + b);
16
+ return Number.isFinite(m) || !Number.isFinite(a) || !Number.isFinite(b) ? m : 0.5 * a + 0.5 * b;
17
+ }
18
+ /** Math.min(...v) without spreading (Infinity for an empty array) */
19
+ export function minOf(v) {
20
+ let m = Infinity;
21
+ for (let i = 0; i < v.length; i++)
22
+ m = Math.min(m, v[i]);
23
+ return m;
24
+ }
@@ -17,12 +17,21 @@ function binaryExponent(x) {
17
17
  const bits = hi !== 0 ? 32 + (31 - Math.clz32(hi)) : 31 - Math.clz32(lo);
18
18
  return bits - 1074;
19
19
  }
20
- /** 10^k for an integer k >= 0, by repeated multiplication in double arithmetic */
20
+ /**
21
+ * 10^k for an integer k >= 0 by squaring, as R's round forms it (k multiplications by 10 rounded differently from 10^23
22
+ * on and moved the result: round(0x1.0b7fa925827cbp-287, 100) and round(0x1.ec75dfa8b17cbp+84, -25); issue #101)
23
+ */
21
24
  function powerOf10(k) {
22
25
  let p = 1;
23
- for (let i = 0; i < k; i++)
24
- p *= 10;
25
- return p;
26
+ let b = 10;
27
+ for (let n = k;;) {
28
+ if (n & 1)
29
+ p *= b;
30
+ n >>= 1;
31
+ if (n === 0)
32
+ return p;
33
+ b *= b;
34
+ }
26
35
  }
27
36
  /** The nearest integer, ties to even (C's nearbyint in the default rounding mode); the sign of x is kept */
28
37
  function roundHalfEven(x) {
@@ -1,4 +1,13 @@
1
1
  /** Whether x is not an integer (relative slack 1e-9) */
2
2
  export declare function nonInt(x: number): boolean;
3
+ /**
4
+ * The largest count k with k <= q, for the distribution functions of discrete distributions on the integers. A q within
5
+ * min(1e-7, 1e-9 max(1, |q|)) of an integer is that integer (a computed 2.9999999999999996 counts as 3); any other q
6
+ * gives floor(q). R adds 1e-7 before taking the floor (or, in psignrank, rounding to the nearest integer), which counts
7
+ * q in [k - 1e-7, k) as k although P[X <= q] is P[X <= k - 1] there, and does not agree with its densities
8
+ * (dpois(1 - 1e-8, 1) is 0 while ppois(1 - 1e-8, 1) is P[X <= 1]) (#145, #146). The slack of {@link nonInt} alone
9
+ * would be wider than R's above q = 100 (ppois(1000 - 5e-7, 1000) would be P[X <= 1000]), so it is capped at 1e-7.
10
+ */
11
+ export declare function countAtMost(q: number): number;
3
12
  /** Round half to even (0.5 goes to the nearest even number) */
4
13
  export declare function roundHalfEven(x: number): number;
@@ -7,6 +7,18 @@
7
7
  export function nonInt(x) {
8
8
  return Math.abs(x - roundHalfEven(x)) > 1e-9 * Math.max(1, Math.abs(x));
9
9
  }
10
+ /**
11
+ * The largest count k with k <= q, for the distribution functions of discrete distributions on the integers. A q within
12
+ * min(1e-7, 1e-9 max(1, |q|)) of an integer is that integer (a computed 2.9999999999999996 counts as 3); any other q
13
+ * gives floor(q). R adds 1e-7 before taking the floor (or, in psignrank, rounding to the nearest integer), which counts
14
+ * q in [k - 1e-7, k) as k although P[X <= q] is P[X <= k - 1] there, and does not agree with its densities
15
+ * (dpois(1 - 1e-8, 1) is 0 while ppois(1 - 1e-8, 1) is P[X <= 1]) (#145, #146). The slack of {@link nonInt} alone
16
+ * would be wider than R's above q = 100 (ppois(1000 - 5e-7, 1000) would be P[X <= 1000]), so it is capped at 1e-7.
17
+ */
18
+ export function countAtMost(q) {
19
+ const r = roundHalfEven(q);
20
+ return Math.abs(q - r) <= Math.min(1e-7, 1e-9 * Math.max(1, Math.abs(q))) ? r : Math.floor(q);
21
+ }
10
22
  /** Round half to even (0.5 goes to the nearest even number) */
11
23
  export function roundHalfEven(x) {
12
24
  if (!Number.isFinite(x))
@@ -145,8 +145,11 @@ export function invertContinuous(problem) {
145
145
  }
146
146
  if (!(xl > 0 && xh > xl && Number.isFinite(xh)))
147
147
  return x0;
148
- for (let k = 0; k < 64; k++) {
149
- const mid = xl + (xh - xl) / 2;
148
+ // halved on the bit patterns of the doubles (positive x: ordered as integers), so that any bracket shrinks to
149
+ // adjacent doubles within 64 steps; halving the values took 64 steps per factor 2^64 of width, and a bracket 1e55
150
+ // times wider than the answer ended far from it (qgamma(0.75, 1e145) was 151 ulps below the right 1e145)
151
+ for (let k = 0; k < 70; k++) {
152
+ const mid = bitMidpoint(xl, xh);
150
153
  if (mid <= xl || mid >= xh)
151
154
  break;
152
155
  const r = evaluate(mid)[1];
@@ -162,6 +165,16 @@ export function invertContinuous(problem) {
162
165
  return Math.abs(evaluate(xl)[1]) < Math.abs(evaluate(xh)[1]) ? xl : xh;
163
166
  }
164
167
  }
168
+ const bitView = new DataView(new ArrayBuffer(8));
169
+ /** The double halfway between positive finite a < b in the order of the doubles (the mean of their bit patterns) */
170
+ function bitMidpoint(a, b) {
171
+ bitView.setFloat64(0, a);
172
+ const ia = bitView.getBigUint64(0);
173
+ bitView.setFloat64(0, b);
174
+ const ib = bitView.getBigUint64(0);
175
+ bitView.setBigUint64(0, (ia + ib) / 2n);
176
+ return bitView.getFloat64(0);
177
+ }
165
178
  /** Midpoint of the bracketing interval. If an end is infinite, expand outward from the current value */
166
179
  function midpoint(lo, hi, x) {
167
180
  if (Number.isFinite(lo) && Number.isFinite(hi))
@@ -0,0 +1,12 @@
1
+ /** An options object: a plain object, not an array, a typed array or a function */
2
+ export declare function isOptionsObject(v: unknown): v is Record<string, unknown>;
3
+ /**
4
+ * The optional arguments from `args` (the arguments from the first optional one on), each named by `names` in
5
+ * order. An options object at a position ends the positional arguments; one may also follow all of them. A key given
6
+ * both by position and in the options is a RangeError. Positional `undefined` counts as not given. With `fnName`, a key
7
+ * of the options that is not in `names` is a RangeError (checkOptions), for a function whose options are not checked
8
+ * by the generated code (the function splinefun returns).
9
+ */
10
+ export declare function optionalArgs<O extends object>(names: readonly (keyof O & string)[], args: readonly unknown[], fnName?: string): Partial<O>;
11
+ /** The first options object among `args` (the optional arguments of optionalArgs), for the check of its keys */
12
+ export declare function optionsIn(args: readonly unknown[]): Record<string, unknown> | undefined;
@@ -0,0 +1,46 @@
1
+ // Optional arguments that a function takes either by position (R's order) or in an options object: the object in
2
+ // place of the first optional argument, or after the positional ones. A value given both ways is a RangeError.
3
+ import { checkOptions } from "./checkOptions.js";
4
+ /** An options object: a plain object, not an array, a typed array or a function */
5
+ export function isOptionsObject(v) {
6
+ return typeof v === "object" && v !== null && !Array.isArray(v) && !ArrayBuffer.isView(v);
7
+ }
8
+ /**
9
+ * The optional arguments from `args` (the arguments from the first optional one on), each named by `names` in
10
+ * order. An options object at a position ends the positional arguments; one may also follow all of them. A key given
11
+ * both by position and in the options is a RangeError. Positional `undefined` counts as not given. With `fnName`, a key
12
+ * of the options that is not in `names` is a RangeError (checkOptions), for a function whose options are not checked
13
+ * by the generated code (the function splinefun returns).
14
+ */
15
+ export function optionalArgs(names, args, fnName) {
16
+ const out = {};
17
+ let options;
18
+ for (let i = 0; i < args.length; i++) {
19
+ const a = args[i];
20
+ if (i <= names.length && isOptionsObject(a)) {
21
+ options = a;
22
+ break;
23
+ }
24
+ if (i < names.length && a !== undefined)
25
+ out[names[i]] = a;
26
+ }
27
+ if (options !== undefined) {
28
+ if (fnName !== undefined)
29
+ checkOptions(options, names, fnName);
30
+ for (const name of names) {
31
+ if (options[name] === undefined)
32
+ continue;
33
+ if (Object.hasOwn(out, name))
34
+ throw new RangeError(`'${name}' is given both as an argument and in the options`);
35
+ out[name] = options[name];
36
+ }
37
+ }
38
+ return out;
39
+ }
40
+ /** The first options object among `args` (the optional arguments of optionalArgs), for the check of its keys */
41
+ export function optionsIn(args) {
42
+ for (const a of args)
43
+ if (isOptionsObject(a))
44
+ return a;
45
+ return undefined;
46
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * (R'R)^-1 of an upper triangular R as inv_jk 2^(-e_j - e_k): the columns of R divided by 2^e_j first (exact). The
3
+ * inverse of R itself overflowed for columns of 1e-200 (and underflowed for 1e200), so standard errors were Infinity
4
+ * or 0 though they scale with the column (R's too). e_j is the exponent of the largest |R_ij| of a column outside
5
+ * [2^-400, 2^400], else 0, which leaves the computation as before
6
+ */
7
+ export declare function chol2invColumns(R: readonly (readonly number[])[]): {
8
+ inv: number[][];
9
+ e: number[];
10
+ };
@@ -0,0 +1,22 @@
1
+ // (R'R)^-1 of a triangular factor whose columns may be far from 1 (review 64).
2
+ import { chol2inv } from "../linalg/chol.js";
3
+ import { ldexp } from "./summary.js";
4
+ /**
5
+ * (R'R)^-1 of an upper triangular R as inv_jk 2^(-e_j - e_k): the columns of R divided by 2^e_j first (exact). The
6
+ * inverse of R itself overflowed for columns of 1e-200 (and underflowed for 1e200), so standard errors were Infinity
7
+ * or 0 though they scale with the column (R's too). e_j is the exponent of the largest |R_ij| of a column outside
8
+ * [2^-400, 2^400], else 0, which leaves the computation as before
9
+ */
10
+ export function chol2invColumns(R) {
11
+ const p = R.length;
12
+ const e = new Array(p).fill(0);
13
+ for (let j = 0; j < p; j++) {
14
+ let top = 0;
15
+ for (let i = 0; i <= j; i++)
16
+ top = Math.max(top, Math.abs(R[i][j]));
17
+ if (top > 0 && Number.isFinite(top) && (top < 2 ** -400 || top > 2 ** 400))
18
+ e[j] = Math.floor(Math.log2(top));
19
+ }
20
+ const Rs = e.every((v) => v === 0) ? R.map((r) => r.slice()) : R.map((r) => r.map((v, j) => ldexp(v, -e[j])));
21
+ return { inv: chol2inv(Rs), e };
22
+ }
@@ -4,10 +4,23 @@
4
4
  */
5
5
  export function seqLength(from, to, n) {
6
6
  const len = Math.ceil(n);
7
+ // (a length that is not finite or rounds up to a negative number was an empty array; issue #98)
8
+ if (!Number.isFinite(n) || len < 0)
9
+ throw new RangeError("'length.out' must be a non-negative number");
7
10
  if (len === 1)
8
11
  return [from];
9
- const by = (to - from) / (len - 1);
10
- const out = Array.from({ length: len }, (_, i) => from + i * by);
12
+ let out;
13
+ if (Number.isFinite(to - from) || !Number.isFinite(from) || !Number.isFinite(to)) {
14
+ const by = (to - from) / (len - 1);
15
+ out = Array.from({ length: len }, (_, i) => from + i * by);
16
+ }
17
+ else {
18
+ // finite ends whose difference overflows: in half coordinates, doubled (seqLength(-1e308, 1e308, 3) was NaN,
19
+ // Infinity, 1e308; R's seq gives -1e308, 0, 1e308)
20
+ const h = from / 2;
21
+ const by = (to / 2 - h) / (len - 1);
22
+ out = Array.from({ length: len }, (_, i) => 2 * (h + i * by));
23
+ }
11
24
  if (len > 0)
12
25
  out[len - 1] = to;
13
26
  return out;
@@ -14,13 +14,77 @@ export declare function wideSumAdd(acc: WideSum, v: number, n: number): void;
14
14
  export declare function wideSumValue(acc: WideSum): number;
15
15
  /** v 2^k for an integer k, in two steps so that 2^k itself does not overflow or underflow */
16
16
  export declare function ldexp(v: number, k: number): number;
17
+ /** v 2^k for any integer k (beyond the +-2046 of ldexp), in steps of at most 2^1000 */
18
+ export declare function ldexpWide(v: number, k: number): number;
17
19
  /** Mean as double-double. x contains no NaN and has length >= 1 */
18
20
  export declare function meanDD(x: readonly number[]): DD;
19
21
  export declare function mean(x: readonly number[]): number;
20
22
  /** Unbiased variance. NaN for length below 2 (R's var gives NA) */
21
23
  export declare function variance(x: readonly number[]): number;
24
+ /**
25
+ * Sample standard deviation from the scaled deviations: sqrt(sum d^2 / (n - 1)) in the coordinates 2^-k, times 2^k.
26
+ * The same bits as sqrt(variance(x)) where the variance is a normal number (the square root of 4^k y is 2^k sqrt(y)),
27
+ * and the right value where it overflows or underflows (sd of c(-1e308, -1e308, -1e308, -1e308, 1.5e308) is 1.118e308
28
+ * while its variance is 1.25e616). x contains no NaN
29
+ */
30
+ export declare function standardDeviation(x: readonly number[]): number;
22
31
  /** Unbiased covariance. x and y have the same length and contain no NaN */
23
32
  export declare function covariance(x: readonly number[], y: readonly number[]): number;
33
+ /**
34
+ * The deviations from the mean of x scaled by 2^-k (the scaling first, see the rules above), in double-double, with k
35
+ * and the scaled mean. undefined when x has an infinite value (the mean is not finite)
36
+ */
37
+ export declare function centered(x: readonly number[]): {
38
+ d: DD[];
39
+ k: number;
40
+ mean: DD;
41
+ } | undefined;
42
+ /**
43
+ * (x - mean) / sd, from the scaled deviations (no overflow or underflow at any scale). NaN values when the standard
44
+ * deviation is 0 or x has fewer than 2 values or an infinite one
45
+ */
46
+ export declare function standardize(x: readonly number[]): number[];
47
+ /**
48
+ * Moments of groups on one common scale 2^-k (see the rules above): per group the size, mean, within sum of squares
49
+ * and variance (NaN below 2 values), the grand mean, the between and pooled within sums of squares (accumulated in
50
+ * double-double), and the scaled values. Means are scaled back by 2^k, sums of squares and variances by 2^2k
51
+ */
52
+ export interface GroupMoments {
53
+ k: number;
54
+ groups: number[][];
55
+ n: number[];
56
+ mean: number[];
57
+ ss: number[];
58
+ variance: number[];
59
+ grandMean: number;
60
+ ssBetween: number;
61
+ ssWithin: number;
62
+ }
63
+ export declare function groupMoments(groups: readonly (readonly number[])[]): GroupMoments;
64
+ /** x - y for paired values, both scaled by one 2^-k first (so the differences cannot overflow), with k */
65
+ export declare function pairedDifferences(x: readonly number[], y: readonly number[]): {
66
+ d: number[];
67
+ k: number;
68
+ };
69
+ /**
70
+ * Satterthwaite's degrees of freedom (sum w)^2 / sum(w^2 / df) of a sum of independent variance estimates w with
71
+ * degrees of freedom df. The w are scaled by a power of 2 first (no rounding), so the squares neither overflow nor
72
+ * underflow when the w do not; the ratio does not depend on the units of the data. The form of irr's icc (the same
73
+ * order of operations); tTest, cohensD and gamesHowellTest use R's forms below and this only where those are not finite
74
+ */
75
+ export declare function satterthwaiteDf(w: readonly number[], df: readonly number[]): number;
76
+ /**
77
+ * Welch's degrees of freedom in the form of R's t.test and effectsize: se^4 / sum(se_i^4 / df_i) with se_i = sqrt(w_i)
78
+ * and se = sqrt(sum se_i^2), on the w scaled by an even power of 2 (exact, also through sqrt, so the same bits as R
79
+ * wherever R's terms are in range, and no overflow). {@link satterthwaiteDf} where that is not finite and positive
80
+ * (revision of review 2)
81
+ */
82
+ export declare function satterthwaiteDfSe(w: readonly number[], df: readonly number[]): number;
83
+ /**
84
+ * Welch's degrees of freedom in the form of PMCMRplus's gamesHowellTest: A^2 / sum(s2_i^2 / (n_i^2 (n_i - 1))) with
85
+ * A = sum s2_i / n_i, for the group variances s2 and sizes n, scaled as {@link satterthwaiteDfSe} (revision of review 2)
86
+ */
87
+ export declare function satterthwaiteDfPM(s2: readonly number[], n: readonly number[]): number;
24
88
  /**
25
89
  * Sum (accumulated in double-double without overflow, then rounded; ±Infinity beyond the double range). With
26
90
  * infinite or NaN values, as in double arithmetic
@@ -37,3 +101,29 @@ export declare function tieCounts(x: readonly number[]): number[];
37
101
  export declare function dropNaN(x: readonly number[]): number[];
38
102
  /** Keep only the pairs where neither x nor y is NaN (R's complete.cases) */
39
103
  export declare function completePairs(x: readonly number[], y: readonly number[]): [number[], number[]];
104
+ /**
105
+ * d^2 / e, as for a Pearson chi-squared term. When d^2 overflows (or loses precision below the normal range) although
106
+ * the quotient is representable, it is formed as (d / sqrt(e))^2 instead
107
+ */
108
+ export declare function squareOver(d: number, e: number): number;
109
+ /**
110
+ * a b / n, as for an expected count from two margins. When the product overflows, underflows or is subnormal (with a
111
+ * and b not 0), the three are scaled to [1, 2) by powers of 2 and the exponents added back at the end
112
+ */
113
+ export declare function productOver(a: number, b: number, n: number): number;
114
+ /**
115
+ * Data scaled by a power of 2, 2^-k, so that the largest finite absolute value is in [1, 2) (k = 0 when there is
116
+ * none). The scaling adds no rounding error, so statistics that do not depend on the units (t, F, chi-squared,
117
+ * ratios of variances, degrees of freedom) come out as from the original data, but their squares and variances
118
+ * can no longer overflow or underflow. Quantities with units are multiplied back by 2^k (see ldexp)
119
+ */
120
+ export declare function scaleToUnit(groups: readonly (readonly number[])[]): {
121
+ groups: number[][];
122
+ k: number;
123
+ };
124
+ /**
125
+ * Euclidean norm sqrt(sum x_i^2): the plain sum where it is in range (so the value is unchanged there), otherwise from
126
+ * the values divided by the largest |x_i| (as BLAS's dnrm2), so that squares of values around 1e200 or 1e-200 neither
127
+ * overflow nor underflow
128
+ */
129
+ export declare function norm2(x: readonly number[]): number;