@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
@@ -18,6 +18,13 @@ export function checkConfLevel(level) {
18
18
  throw new RangeError("confLevel must be a number between 0 and 1");
19
19
  }
20
20
  }
21
+ /**
22
+ * Whether v is a nonnegative count. The slack is absolute: a count is an integer whatever its size, so a relative
23
+ * slack would accept 1e8 + 0.25 and silently round it
24
+ */
25
+ export function isCount(v) {
26
+ return Number.isFinite(v) && v >= 0 && Math.abs(v - Math.round(v)) <= 1e-7;
27
+ }
21
28
  /** Validate the confidence level strictly inside (0, 1), as most of R's tests require */
22
29
  export function checkConfLevelOpen(level) {
23
30
  if (!(Number.isFinite(level) && level > 0 && level < 1)) {
@@ -7,10 +7,16 @@
7
7
  // n_i = size - 1, N = sum n_i and s_p^2 the pooled variance; chi-squared with k - 1 degrees of freedom.
8
8
  import { pf1, qf1 } from "../distributions/f.js";
9
9
  import { pgamma1 } from "../distributions/gamma.js";
10
- import { variance } from "../internal/summary.js";
10
+ import { groupMoments, ldexpWide } from "../internal/summary.js";
11
11
  import { kSampleData, samplesByLevel, twoGroupData } from "./testFormula.js";
12
12
  import { checkConfLevelOpen, matchAlternative } from "./types.js";
13
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
14
+ import { checkOptions } from "../internal/checkOptions.js";
15
+ const VAR_TEST_KEYS = ["ratio", "alternative", "confLevel"];
16
+ // Generated by `node scripts/option-keys.mjs --write` (end)
17
+ const MIN_NORMAL = 2 ** -1022;
13
18
  export function varTest(x, y, options = {}) {
19
+ checkOptions(options, VAR_TEST_KEYS, "varTest"); // generated: option-keys.mjs
14
20
  if (typeof x === "string") {
15
21
  const d = twoGroupData(x, y);
16
22
  return varTest(d.x, d.y, options);
@@ -30,7 +36,22 @@ export function varTest(x, y, options = {}) {
30
36
  throw new Error("not enough 'x' observations");
31
37
  if (dfy < 1)
32
38
  throw new Error("not enough 'y' observations");
33
- const estimate = variance(xs) / variance(ys);
39
+ // scaled by a power of 2 (exact) so that the variances stay in range; the statistic does not depend on the units
40
+ let [vx, vy] = groupMoments([xs, ys]).variance;
41
+ let exponent = 0;
42
+ if (!(vx >= MIN_NORMAL && vy >= MIN_NORMAL)) {
43
+ // a variance below the normal range on the common scale (#393; as #270): each sample on its own scale 2^-e,
44
+ // the ratio times 2^(2 (e_x - e_y)). varTest([1e308, 1e308], [0, 1e-100]) had both variances 0
45
+ const mx = groupMoments([xs]);
46
+ const my = groupMoments([ys]);
47
+ vx = mx.variance[0];
48
+ vy = my.variance[0];
49
+ exponent = 2 * (mx.k - my.k);
50
+ }
51
+ // both variances 0: the ratio is 0 / 0 (R returns NaN); one of them 0 gives the limit 0 or Infinity as before
52
+ if (vx === 0 && vy === 0)
53
+ throw new RangeError("both samples have zero variance");
54
+ const estimate = exponent === 0 ? vx / vy : ldexpWide(vx / vy, exponent);
34
55
  const statistic = estimate / ratio;
35
56
  const lower = pf1(statistic, dfx, dfy, true, false);
36
57
  const upper = pf1(statistic, dfx, dfy, false, false);
@@ -47,7 +68,9 @@ export function varTest(x, y, options = {}) {
47
68
  else {
48
69
  pValue = Math.min(1, 2 * Math.min(lower, upper));
49
70
  const beta = (1 - confLevel) / 2;
50
- confInt = [estimate / qf1(1 - beta, dfx, dfy, true, false), estimate / qf1(beta, dfx, dfy, true, false)];
71
+ // (for alpha below 1e-8 the upper probability itself: 1 - alpha loses its digits and rounds to 1 below 1.1e-16; review 58)
72
+ const fHigh = beta < 1e-8 ? qf1(beta, dfx, dfy, false, false) : qf1(1 - beta, dfx, dfy, true, false);
73
+ confInt = [estimate / fHigh, estimate / qf1(beta, dfx, dfy, true, false)];
51
74
  }
52
75
  return {
53
76
  method: "F test to compare two variances",
@@ -63,6 +86,7 @@ export function varTest(x, y, options = {}) {
63
86
  estimateNames: "ratio of variances",
64
87
  nullValue: ratio,
65
88
  nullValueName: "ratio of variances",
89
+ warnings: [],
66
90
  };
67
91
  }
68
92
  export function bartlettTest(x, data) {
@@ -74,16 +98,13 @@ export function bartlettTest(x, data) {
74
98
  const k = groups.length;
75
99
  if (k < 2)
76
100
  throw new RangeError("'x' must be a list with at least 2 elements");
77
- const n = [];
78
- const v = [];
79
- for (const g of groups) {
80
- // as in R, infinite values are dropped along with NaN
81
- const s = Array.from(g).filter(Number.isFinite);
82
- if (s.length < 2)
83
- throw new RangeError("there must be at least 2 observations in each group");
84
- n.push(s.length - 1);
85
- v.push(variance(s));
86
- }
101
+ // as in R, infinite values are dropped along with NaN
102
+ const samples = groups.map((g) => Array.from(g).filter(Number.isFinite));
103
+ if (samples.some((s) => s.length < 2))
104
+ throw new RangeError("there must be at least 2 observations in each group");
105
+ const n = samples.map((s) => s.length - 1);
106
+ // scaled by a power of 2 (exact) so that the variances stay in range; the statistic does not depend on the units
107
+ const v = groupMoments(samples).variance;
87
108
  let N = 0;
88
109
  let pooled = 0;
89
110
  let sumLog = 0;
@@ -95,7 +116,23 @@ export function bartlettTest(x, data) {
95
116
  sumInv += 1 / n[i];
96
117
  }
97
118
  pooled /= N;
98
- const statistic = (N * Math.log(pooled) - sumLog) / (1 + (sumInv - 1 / N) / (3 * (k - 1)));
119
+ let logRatio = N * Math.log(pooled) - sumLog;
120
+ if (v.some((vi) => vi < MIN_NORMAL)) {
121
+ // A variance underflows (or is subnormal) on the common scale although the log ratio is finite: each group on
122
+ // its own scale 2^-e_i, log v_i = log w_i + 2 (e_i - E) log 2 relative to the largest exponent E (the E terms
123
+ // cancel, as sum n_i = N), and the log of the pooled variance by log-sum-exp
124
+ const own = samples.map((g) => {
125
+ const m = groupMoments([g]);
126
+ return { w: m.variance[0], e: m.k };
127
+ });
128
+ const top = Math.max(...own.map((o) => o.e));
129
+ const logs = own.map((o) => Math.log(o.w) + 2 * (o.e - top) * Math.LN2);
130
+ const terms = logs.map((l, i) => Math.log(n[i]) + l);
131
+ const peak = Math.max(...terms);
132
+ const logPooled = peak + Math.log(terms.reduce((a, t) => a + Math.exp(t - peak), 0)) - Math.log(N);
133
+ logRatio = N * logPooled - logs.reduce((a, l, i) => a + n[i] * l, 0);
134
+ }
135
+ const statistic = logRatio / (1 + (sumInv - 1 / N) / (3 * (k - 1)));
99
136
  const df = k - 1;
100
137
  return {
101
138
  method: "Bartlett test of homogeneity of variances",
@@ -104,5 +141,6 @@ export function bartlettTest(x, data) {
104
141
  parameter: df,
105
142
  parameterName: "df",
106
143
  pValue: pgamma1(statistic, df / 2, 2, false, false),
144
+ warnings: [],
107
145
  };
108
146
  }
@@ -23,9 +23,20 @@ import { pwilcox1, qwilcox1 } from "../distributions/wilcox.js";
23
23
  import { pnorm, qnorm } from "../distributions/normal.js";
24
24
  import { completePairs, dropNaN, mean, rank, tieCounts, } from "../internal/summary.js";
25
25
  import { uniroot } from "../internal/uniroot.js";
26
+ import { reported, Warnings } from "../internal/warnings.js";
26
27
  import { twoSampleData } from "./testFormula.js";
27
28
  import { matchAlternative } from "./types.js";
29
+ import { maxOf, midpoint, minOf } from "../internal/extremes.js";
30
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
31
+ import { checkOptions } from "../internal/checkOptions.js";
32
+ import { isOptionsObject } from "../internal/optionalArgs.js";
33
+ const WILCOX_TEST_KEYS = [
34
+ "alternative", "mu", "paired", "exact", "correct", "confInt", "confLevel", "tolRoot",
35
+ ];
28
36
  export function wilcoxTest(x, y, options = {}) {
37
+ if (typeof x !== "string" && isOptionsObject(y))
38
+ checkOptions(y, WILCOX_TEST_KEYS, "wilcoxTest"); // generated: option-keys.mjs
39
+ checkOptions(options, WILCOX_TEST_KEYS, "wilcoxTest"); // generated: option-keys.mjs
29
40
  if (typeof x === "string") {
30
41
  if (options.paired !== undefined)
31
42
  throw new Error("cannot use 'paired' in formula method");
@@ -78,10 +89,9 @@ export function wilcoxTest(x, y, options = {}) {
78
89
  confInt,
79
90
  confLevel,
80
91
  tolRoot,
92
+ warnings: new Warnings(),
81
93
  };
82
- return yv === undefined
83
- ? signedRank(xs, exact, paired, ctx)
84
- : rankSum(xs, yv, exact, ctx);
94
+ return reported("wilcoxTest", yv === undefined ? signedRank(xs, exact, paired, ctx) : rankSum(xs, yv, exact, ctx));
85
95
  }
86
96
  /** Continuity correction amount (two-sided: 0.5 in the direction of the statistic's sign) */
87
97
  function correction(z, alternative, correct) {
@@ -138,10 +148,19 @@ function signedRankStat(x) {
138
148
  function signedRank(x0, exactOpt, paired, ctx) {
139
149
  const { alternative, mu, correct } = ctx;
140
150
  let x = x0.map((v) => v - mu);
151
+ // A finite observation whose difference from mu overflows: the differences in half units (x / 2 - mu / 2). Then
152
+ // abs(mu) >= 2^970, so each half difference is exactly half the rounded difference (a subnormal x / 2 rounds far
153
+ // below the spacing of mu / 2) and the ranks, signs and zeros are those of the differences
154
+ const halved = x.some((v, i) => !Number.isFinite(v) && Number.isFinite(x0[i]));
155
+ if (halved)
156
+ x = x0.map((v) => v / 2 - mu / 2);
141
157
  const zeroes = x.some((v) => v === 0);
142
158
  if (zeroes)
143
159
  x = x.filter((v) => v !== 0);
144
160
  const n = x.length;
161
+ // only zero differences: nothing to rank (R returns a p-value of NaN)
162
+ if (n === 0)
163
+ throw new RangeError("no non-zero differences for the signed rank test");
145
164
  const exact = exactOpt ?? n < 50;
146
165
  const r = rank(x.map(Math.abs));
147
166
  const ties = tieCounts(r).length > 0;
@@ -175,14 +194,44 @@ function signedRank(x0, exactOpt, paired, ctx) {
175
194
  pValue,
176
195
  nullValue: mu,
177
196
  nullValueName: paired ? "location shift" : "location",
197
+ warnings: ctx.warnings.list,
178
198
  };
179
- if (!ctx.confInt)
199
+ // R's warnings of the exact method it could not use (after those of the interval)
200
+ const exactWarnings = () => {
201
+ if (exact && ties) {
202
+ ctx.warnings.add("cannot compute exact p-value with ties");
203
+ if (ctx.confInt)
204
+ ctx.warnings.add("cannot compute exact confidence interval with ties");
205
+ }
206
+ if (exact && zeroes) {
207
+ ctx.warnings.add("cannot compute exact p-value with zeroes");
208
+ if (ctx.confInt)
209
+ ctx.warnings.add("cannot compute exact confidence interval with zeroes");
210
+ }
211
+ };
212
+ if (!ctx.confInt) {
213
+ exactWarnings();
180
214
  return result;
215
+ }
181
216
  // The interval is on the original scale (add mu back to the data with zero differences removed)
182
- const xo = x.map((v) => v + mu);
217
+ // (with half units, the observations other than mu themselves)
218
+ // R also adds mu back. Where mu dwarfs the data, (x - mu) + mu loses the observations (rnorm(n, 0, 1e-200) with
219
+ // mu = 0.5 gave R's interval NaN NaN and estimate 0): then the observations themselves, which are the same values
220
+ // (x - mu = 0 exactly where x = mu). The bound 2^-26 of the largest observation leaves the rounding of ordinary
221
+ // data, an ulp or two, to R's values (#502)
222
+ let xo = halved ? x0.filter((v) => v !== mu) : x.map((v) => v + mu);
223
+ if (!halved) {
224
+ const direct = x0.filter((v) => v !== mu);
225
+ let big = 0;
226
+ for (const v of direct)
227
+ big = Math.max(big, Math.abs(v));
228
+ if (xo.some((v, i) => Math.abs(v - direct[i]) > 2 ** -26 * big))
229
+ xo = direct;
230
+ }
183
231
  const ci = useExact
184
232
  ? signedRankExactCI(xo, ctx)
185
233
  : signedRankNormalCI(xo, ctx);
234
+ exactWarnings();
186
235
  return { ...result, ...ci };
187
236
  }
188
237
  /** Interval with the given finite ends, the other end(s) open according to the alternative */
@@ -206,8 +255,8 @@ function exactRank(quantile, alternative, a) {
206
255
  return q === 0 ? 1 : q;
207
256
  }
208
257
  function checkTolRoot(tol) {
209
- if (!(tol > 0))
210
- throw new RangeError("tolRoot must be positive");
258
+ if (!(tol > 0 && tol < Infinity))
259
+ throw new RangeError("tolRoot must be a positive finite number");
211
260
  }
212
261
  /**
213
262
  * Exact interval from the order statistics of the Walsh averages (Bauer 1972) and the Hodges-Lehmann estimate, their
@@ -219,16 +268,19 @@ function signedRankExactCI(x, ctx) {
219
268
  const walsh = [];
220
269
  for (let i = 0; i < n; i++)
221
270
  for (let j = i; j < n; j++)
222
- walsh.push((x[i] + x[j]) / 2);
271
+ walsh.push(midpoint(x[i], x[j]));
223
272
  walsh.sort(ascending);
224
273
  const N = walsh.length;
225
274
  const a = 1 - confLevel;
226
275
  const q = exactRank((p) => qsignrank1(p, n, true, false), alternative, a);
227
276
  const tail = psignrank1(q - 1, n, true, false);
228
277
  const achieved = alternative === "two.sided" ? 2 * tail : tail;
278
+ const notAchievable = achieved - a > a / 2;
279
+ if (notAchievable)
280
+ ctx.warnings.add("requested conf.level not achievable");
229
281
  return {
230
282
  confInt: shape(alternative, walsh[q - 1], walsh[N - q]),
231
- confLevel: achieved - a > a / 2 ? 1 - signif2(achieved) : confLevel,
283
+ confLevel: notAchievable ? 1 - signif2(achieved) : confLevel,
232
284
  estimate: sortedMedian(walsh),
233
285
  estimateNames: "(pseudo)median",
234
286
  };
@@ -242,10 +294,13 @@ function signedRankNormalCI(x, ctx) {
242
294
  const n = x.length;
243
295
  if (n === 0)
244
296
  return noInterval(alternative, NaN);
245
- const lo = Math.min(...x);
246
- const hi = Math.max(...x);
297
+ const lo = minOf(x);
298
+ const hi = maxOf(x);
247
299
  if (!Number.isFinite(lo) || !Number.isFinite(hi) || n === 1 || lo === hi) {
248
- return noInterval(alternative, (lo + hi) / 2, "midrange");
300
+ // R's statistic of x - lo has no non-zero values when all observations are equal (n = 1 included)
301
+ if (Number.isFinite(lo) && lo === hi)
302
+ ctx.warnings.add("cannot compute confidence interval when all observations are zero or tied");
303
+ return noInterval(alternative, midpoint(lo, hi), "midrange");
249
304
  }
250
305
  const stat = (d, corr) => {
251
306
  const [, centre, sigma] = signedRankStat(x.map((v) => v - d).filter((v) => v !== 0));
@@ -267,6 +322,8 @@ function signedRankNormalCI(x, ctx) {
267
322
  a *= 2;
268
323
  enlarged = true;
269
324
  }
325
+ if (enlarged)
326
+ ctx.warnings.add("requested conf.level not achievable");
270
327
  checkTolRoot(tolRoot);
271
328
  const crossing = (z) => uniroot((d) => stat(d, correct) - z, lo, hi, { tol: tolRoot, fLower: sLo - z, fUpper: sHi - z });
272
329
  let confInt;
@@ -309,33 +366,89 @@ function rankSumStat(x, y) {
309
366
  const sigma = Math.sqrt(((nx * ny) / 12) * (N + 1 - ties / (N * (N - 1))));
310
367
  return [W, W - (nx * ny) / 2, sigma];
311
368
  }
369
+ /**
370
+ * rankSumStat for x_i - d given in half units h_i = x_i / 2 - d / 2 (used where a finite x_i - d overflows, so
371
+ * abs(d) >= 2^970), with whether there are ties. y stays in its own units: h_i is compared with y_j / 2, which is exact
372
+ * except for a subnormal y_j, and there h_i is 0 (compared with y_j itself) or of magnitude above 2^900, so the order
373
+ * is that of the differences. Halving y would merge neighbouring subnormals
374
+ */
375
+ function rankSumHalf(h, y) {
376
+ const nx = h.length;
377
+ const ny = y.length;
378
+ const N = nx + ny;
379
+ // sign of 2 h - v (h in half units, v in units)
380
+ const cmpHalf = (hv, v) => {
381
+ const w = hv === 0 || !Number.isFinite(hv) || !Number.isFinite(v) ? v : v / 2;
382
+ return hv < w ? -1 : hv > w ? 1 : 0;
383
+ };
384
+ const cmp = (a, b) => {
385
+ if (a < nx && b < nx)
386
+ return h[a] < h[b] ? -1 : h[a] > h[b] ? 1 : 0;
387
+ if (a >= nx && b >= nx)
388
+ return y[a - nx] < y[b - nx] ? -1 : y[a - nx] > y[b - nx] ? 1 : 0;
389
+ return a < nx ? cmpHalf(h[a], y[b - nx]) : -cmpHalf(h[b], y[a - nx]);
390
+ };
391
+ const idx = Array.from({ length: N }, (_, i) => i);
392
+ idx.sort((a, b) => cmp(a, b) || a - b);
393
+ let s = 0;
394
+ let ties = 0;
395
+ let i = 0;
396
+ while (i < N) {
397
+ let j = i;
398
+ while (j + 1 < N && cmp(idx[j + 1], idx[i]) === 0)
399
+ j++;
400
+ const avg = (i + j + 2) / 2;
401
+ for (let k = i; k <= j; k++)
402
+ if (idx[k] < nx)
403
+ s += avg;
404
+ const t = j - i + 1;
405
+ ties += t * t * t - t;
406
+ i = j + 1;
407
+ }
408
+ const W = s - (nx * (nx + 1)) / 2;
409
+ const sigma = Math.sqrt(((nx * ny) / 12) * (N + 1 - ties / (N * (N - 1))));
410
+ return [W, W - (nx * ny) / 2, sigma, ties > 0];
411
+ }
312
412
  function rankSum(x, y, exactOpt, ctx) {
313
413
  const { alternative, mu, correct } = ctx;
314
414
  const nx = x.length;
315
415
  const ny = y.length;
316
- const xs = x.map((v) => v - mu);
416
+ let xs = x.map((v) => v - mu);
317
417
  const exact = exactOpt ?? (nx < 50 && ny < 50);
318
- const ties = tieCounts([...xs, ...y]).length > 0;
319
- const [W, dz, sigma] = rankSumStat(xs, y);
418
+ // A finite x_i - mu that overflows: ranked from x / 2 - mu / 2 against y (see rankSumHalf)
419
+ const halved = xs.some((v, i) => !Number.isFinite(v) && Number.isFinite(x[i]));
420
+ let ties;
421
+ let W, dz, sigma;
422
+ if (halved) {
423
+ xs = x.map((v) => v / 2 - mu / 2);
424
+ [W, dz, sigma, ties] = rankSumHalf(xs, y);
425
+ }
426
+ else {
427
+ ties = tieCounts([...xs, ...y]).length > 0;
428
+ [W, dz, sigma] = rankSumStat(xs, y);
429
+ }
320
430
  let pValue;
321
431
  let method;
322
432
  const useExact = exact && !ties;
323
433
  if (useExact) {
324
434
  method = "Wilcoxon rank sum exact test";
325
- if (alternative === "less")
326
- pValue = pwilcox1(W, nx, ny, true, false);
327
- else if (alternative === "greater")
328
- pValue = pwilcox1(W - 1, nx, ny, false, false);
329
- else {
435
+ pValue = rankSumExact(() => {
436
+ if (alternative === "less")
437
+ return pwilcox1(W, nx, ny, true, false);
438
+ if (alternative === "greater")
439
+ return pwilcox1(W - 1, nx, ny, false, false);
330
440
  const p = W > (nx * ny) / 2
331
441
  ? pwilcox1(W - 1, nx, ny, false, false)
332
442
  : pwilcox1(W, nx, ny, true, false);
333
- pValue = Math.min(1, 2 * p);
334
- }
443
+ return Math.min(1, 2 * p);
444
+ });
335
445
  }
336
446
  else {
337
447
  method = `Wilcoxon rank sum test${correct ? " with continuity correction" : ""}`;
338
- pValue = normalP((dz - correction(dz, alternative, correct)) / sigma, alternative);
448
+ // all values tied: W always equals its expectation, so p = 1 (R 4.5.2: 0 / 0 = NaN two-sided; R 4.6 returns 1)
449
+ // (never with half units: an overflowing difference equals no y)
450
+ const allTied = !halved && xs.every((v) => v === y[0]) && y.every((v) => v === y[0]);
451
+ pValue = allTied ? 1 : normalP((dz - correction(dz, alternative, correct)) / sigma, alternative);
339
452
  }
340
453
  const result = {
341
454
  method,
@@ -345,12 +458,31 @@ function rankSum(x, y, exactOpt, ctx) {
345
458
  pValue,
346
459
  nullValue: mu,
347
460
  nullValueName: "location shift",
461
+ warnings: ctx.warnings.list,
348
462
  };
349
- if (!ctx.confInt)
350
- return result;
351
- const ci = useExact ? rankSumExactCI(x, y, ctx) : rankSumNormalCI(x, y, ctx);
463
+ const ci = !ctx.confInt ? {} : useExact ? rankSumExact(() => rankSumExactCI(x, y, ctx)) : rankSumNormalCI(x, y, ctx);
464
+ // R's warnings of the exact method it could not use (after those of the interval)
465
+ if (exact && ties) {
466
+ ctx.warnings.add("cannot compute exact p-value with ties");
467
+ if (ctx.confInt)
468
+ ctx.warnings.add("cannot compute exact confidence intervals with ties");
469
+ }
352
470
  return { ...result, ...ci };
353
471
  }
472
+ /**
473
+ * f, with the RangeErrors of the exact distribution (a table too large or too long to build, or inaccurate near the
474
+ * center of large samples) pointing to exact: false
475
+ */
476
+ function rankSumExact(f) {
477
+ try {
478
+ return f();
479
+ }
480
+ catch (e) {
481
+ if (e instanceof RangeError && e.message.startsWith("wilcox:"))
482
+ throw new RangeError(`${e.message}: use exact: false`);
483
+ throw e;
484
+ }
485
+ }
354
486
  /** Exact interval from the order statistics of the differences x_i - y_j and their median as the estimate */
355
487
  function rankSumExactCI(x, y, ctx) {
356
488
  const { alternative, confLevel } = ctx;
@@ -366,9 +498,12 @@ function rankSumExactCI(x, y, ctx) {
366
498
  const q = exactRank((p) => qwilcox1(p, nx, ny, true, false), alternative, a);
367
499
  const tail = pwilcox1(q - 1, nx, ny, true, false);
368
500
  const achieved = alternative === "two.sided" ? 2 * tail : tail;
501
+ const notAchievable = achieved - a > a / 2;
502
+ if (notAchievable)
503
+ ctx.warnings.add("Requested conf.level not achievable");
369
504
  return {
370
505
  confInt: shape(alternative, diffs[q - 1], diffs[M - q]),
371
- confLevel: achieved - a > a / 2 ? 1 - achieved : confLevel,
506
+ confLevel: notAchievable ? 1 - achieved : confLevel,
372
507
  estimate: sortedMedian(diffs),
373
508
  estimateNames: "difference in location",
374
509
  };
@@ -379,8 +514,8 @@ function rankSumExactCI(x, y, ctx) {
379
514
  */
380
515
  function rankSumNormalCI(x, y, ctx) {
381
516
  const { alternative, correct, confLevel, tolRoot } = ctx;
382
- const lo = Math.min(...x) - Math.max(...y);
383
- const hi = Math.max(...x) - Math.min(...y);
517
+ const lo = minOf(x) - maxOf(y);
518
+ const hi = maxOf(x) - minOf(y);
384
519
  if (!Number.isFinite(lo) || !Number.isFinite(hi)) {
385
520
  throw new Error("cannot compute the confidence interval because the differences are not finite");
386
521
  }
@@ -275,11 +275,31 @@ function dqpsrt(limit, last, maxerr, elist, iord, nrmax) {
275
275
  * transformed) range [a0, b0]; `neval` gives the number of evaluations from the number of subintervals
276
276
  */
277
277
  function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
278
- const alist = new Float64Array(limit + 1);
279
- const blist = new Float64Array(limit + 1);
280
- const rlist = new Float64Array(limit + 1);
281
- const elist = new Float64Array(limit + 1);
282
- const iord = new Int32Array(limit + 1);
278
+ // The work arrays (length limit + 1 in QUADPACK) start small and are doubled as subintervals are added, up to
279
+ // limit + 1: iteration L reads and writes indices up to L + 1 at most (the final sum reads rlist[L]), and the unused
280
+ // entries are 0 as in a full-length array, so the result is the same; a large subdivisions no longer allocates
281
+ // 36 (limit + 1) bytes up front (#369)
282
+ let cap = Math.min(limit + 1, 128);
283
+ let alist = new Float64Array(cap);
284
+ let blist = new Float64Array(cap);
285
+ let rlist = new Float64Array(cap);
286
+ let elist = new Float64Array(cap);
287
+ let iord = new Int32Array(cap);
288
+ const grow = (need) => {
289
+ cap = Math.min(limit + 1, Math.max(need, 2 * cap));
290
+ const f64 = (old) => {
291
+ const a = new Float64Array(cap);
292
+ a.set(old);
293
+ return a;
294
+ };
295
+ alist = f64(alist);
296
+ blist = f64(blist);
297
+ rlist = f64(rlist);
298
+ elist = f64(elist);
299
+ const io = new Int32Array(cap);
300
+ io.set(iord);
301
+ iord = io;
302
+ };
283
303
  let ier = 0;
284
304
  let last = 0;
285
305
  let result = 0;
@@ -332,6 +352,8 @@ function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
332
352
  let L = 1;
333
353
  for (L = 1; L < limit; L++) {
334
354
  last = L + 1;
355
+ if (cap < L + 2)
356
+ grow(L + 2);
335
357
  // bisect the subinterval with the nrmax-th largest error estimate
336
358
  const a1 = alist[maxerr];
337
359
  const b1 = 0.5 * (alist[maxerr] + blist[maxerr]);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Throws a RangeError if `options` has an enumerable key that is not in `keys`. `undefined` and `null` options, and
3
+ * values that are not objects, are left to the function. A key whose value is `undefined` is still checked. `path` is
4
+ * the key of a nested options object (such as `"control"`), written before the unknown key in the message.
5
+ */
6
+ export declare function checkOptions(options: unknown, keys: ReadonlySet<string> | readonly string[], fnName: string, path?: string): void;
7
+ /**
8
+ * The nested options object at `path` (keys joined by dots, as `"family.link"`) of `options`, or `undefined` where a
9
+ * step is not an object (a family given by name): what checkOptions is called with for a nested object.
10
+ */
11
+ export declare function nestedOptions(options: unknown, path: string): unknown;
@@ -0,0 +1,31 @@
1
+ // The check that an options object has no key the function does not know: a misspelt key ({ lowertail: false })
2
+ // would otherwise be ignored and give a wrong result without a sign. The key lists are generated from the option
3
+ // types by scripts/option-keys.mjs.
4
+ /**
5
+ * Throws a RangeError if `options` has an enumerable key that is not in `keys`. `undefined` and `null` options, and
6
+ * values that are not objects, are left to the function. A key whose value is `undefined` is still checked. `path` is
7
+ * the key of a nested options object (such as `"control"`), written before the unknown key in the message.
8
+ */
9
+ export function checkOptions(options, keys, fnName, path) {
10
+ if (typeof options !== "object" || options === null)
11
+ return;
12
+ for (const key in options) {
13
+ if (Array.isArray(keys) ? keys.includes(key) : keys.has(key))
14
+ continue;
15
+ const name = path === undefined ? key : `${path}.${key}`;
16
+ throw new RangeError(`unknown option '${name}' for ${fnName} (known: ${[...keys].join(", ")})`);
17
+ }
18
+ }
19
+ /**
20
+ * The nested options object at `path` (keys joined by dots, as `"family.link"`) of `options`, or `undefined` where a
21
+ * step is not an object (a family given by name): what checkOptions is called with for a nested object.
22
+ */
23
+ export function nestedOptions(options, path) {
24
+ let v = options;
25
+ for (const key of path.split(".")) {
26
+ if (typeof v !== "object" || v === null)
27
+ return undefined;
28
+ v = v[key];
29
+ }
30
+ return v;
31
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Throws unless the matrix is square, finite, symmetric (as R's isSymmetric: relative difference from its transpose at
3
+ * most 100 ε) and has a positive diagonal. R's factanal takes any matrix: an asymmetric one gave a result
4
+ */
5
+ export declare function checkCovmat(S: readonly (readonly number[])[]): void;
@@ -0,0 +1,20 @@
1
+ // The check of a covariance (or correlation) matrix given as `covmat` (factanal, omega).
2
+ import { isSymmetric } from "../linalg/eigen.js";
3
+ import { toMat } from "../linalg/matrix.js";
4
+ /**
5
+ * Throws unless the matrix is square, finite, symmetric (as R's isSymmetric: relative difference from its transpose at
6
+ * most 100 ε) and has a positive diagonal. R's factanal takes any matrix: an asymmetric one gave a result
7
+ */
8
+ export function checkCovmat(S) {
9
+ const p = S.length;
10
+ if (p === 0 || S.some((r) => r.length !== p))
11
+ throw new RangeError("'covmat' must be a square matrix");
12
+ if (S.some((r) => r.some((v) => !Number.isFinite(v))))
13
+ throw new RangeError("'covmat' must be finite");
14
+ if (S.some((r, i) => !(r[i] > 0)))
15
+ throw new RangeError("the diagonal of 'covmat' must be positive");
16
+ // in the matrix's own units, as R's isSymmetric (isSymmetric handles sums that overflow; the matrix scaled to its
17
+ // largest entry first took [[1e308, 1e-10], [0, 1]] for a symmetric one, issue #99)
18
+ if (!isSymmetric(toMat(S)))
19
+ throw new RangeError("'covmat' must be symmetric");
20
+ }
@@ -12,7 +12,10 @@ export declare function standardizeDD(x: number, location: number, scale: number
12
12
  * subnormal, 0 or overflows (the log of a subnormal ratio has lost them in the division)
13
13
  */
14
14
  export declare function logRatioDD(a: number, b: number): DD;
15
- /** a b as double-double. Only the double product when Veltkamp splitting would overflow (above 1e300) or for non-finite values */
15
+ /**
16
+ * a b as double-double: twoProd, which keeps the error term of a finite product also where Veltkamp splitting would
17
+ * overflow (it splits the operands scaled by powers of 2); only the double product when that is not finite
18
+ */
16
19
  export declare function mulDD(a: number, b: number): DD;
17
20
  /** exp(-t) (t is double-double). The low-order part of t enters to first order */
18
21
  export declare function expNeg(t: DD): number;
@@ -26,7 +29,18 @@ export declare function expNeg(t: DD): number;
26
29
  export declare function expNegOver(t: DD, s: number): number;
27
30
  /** log(1 - exp(x)) (x <= 0). Switches method by the size of x to avoid cancellation */
28
31
  export declare function log1mExp(x: number): number;
32
+ /** log(e^a + e^b) */
33
+ export declare function logAddExp(a: number, b: number): number;
34
+ /** log(e^a - e^b) for a >= b */
35
+ export declare function logSubExp(a: number, b: number): number;
29
36
  /** 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 */
30
37
  export declare function logOneMinusExpNeg(t: DD): number;
31
38
  /** location + scale t with a single rounding (the rounding of scale t would show up in the subtraction with location) */
32
39
  export declare function locationScale(location: number, scale: number, t: number): number;
40
+ /**
41
+ * (x - location) / scale in double, as the plain formula gives it, but from the halves of x and location when their
42
+ * difference overflows although both are finite (z itself may be in range)
43
+ */
44
+ export declare function standardizeValue(x: number, location: number, scale: number): number;
45
+ /** location + scale t in double, as the plain formula gives it, unless scale t overflows (then as locationScale) */
46
+ export declare function affine(location: number, scale: number, t: number): number;