@numero-uno-jpn/rstats 0.15.2 → 0.16.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 (297) hide show
  1. package/CHANGELOG.md +280 -0
  2. package/README.ja.md +40 -33
  3. package/README.md +36 -28
  4. package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
  5. package/dist/chunks/chunk-5UQCRBAG.js +2 -0
  6. package/dist/chunks/chunk-6CSTWA5E.js +2 -0
  7. package/dist/chunks/chunk-72Y3HZID.js +2 -0
  8. package/dist/chunks/chunk-BG2KLEHM.js +2 -0
  9. package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
  10. package/dist/chunks/chunk-FLJNSXSW.js +2 -0
  11. package/dist/chunks/chunk-GM53LWSR.js +2 -0
  12. package/dist/chunks/chunk-JBCA56ZS.js +3 -0
  13. package/dist/chunks/chunk-K3RJAD2T.js +2 -0
  14. package/dist/chunks/chunk-KAZRWIB5.js +2 -0
  15. package/dist/chunks/chunk-KBVVIMXX.js +3 -0
  16. package/dist/chunks/chunk-N3SIFCZX.js +2 -0
  17. package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
  18. package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
  19. package/dist/chunks/chunk-SYWOBOTR.js +2 -0
  20. package/dist/chunks/chunk-URIO5LY5.js +2 -0
  21. package/dist/chunks/chunk-VTUGG2SV.js +2 -0
  22. package/dist/chunks/chunk-W26LRYOJ.js +2 -0
  23. package/dist/chunks/chunk-XHETEYVC.js +2 -0
  24. package/dist/chunks/{chunk-PWVQVJTP.js → chunk-YRZWONZB.js} +1 -1
  25. package/dist/chunks/chunk-ZGABZMWX.js +2 -0
  26. package/dist/data/grouping.js +20 -5
  27. package/dist/descriptive/basic.js +90 -8
  28. package/dist/descriptive/cov.js +12 -4
  29. package/dist/descriptive/dataFrames.d.ts +4 -2
  30. package/dist/descriptive/dataFrames.js +56 -18
  31. package/dist/descriptive/factanal.js +16 -7
  32. package/dist/descriptive/hist.d.ts +8 -5
  33. package/dist/descriptive/hist.js +71 -31
  34. package/dist/descriptive/missing.js +3 -1
  35. package/dist/descriptive/moments.js +72 -19
  36. package/dist/descriptive/omega.d.ts +1 -1
  37. package/dist/descriptive/omega.js +68 -9
  38. package/dist/descriptive/pivot.d.ts +21 -9
  39. package/dist/descriptive/pivot.js +161 -35
  40. package/dist/descriptive/quantile.js +2 -1
  41. package/dist/descriptive/rank.js +4 -1
  42. package/dist/descriptive/table.d.ts +3 -2
  43. package/dist/descriptive/table.js +24 -9
  44. package/dist/descriptive.js +2 -2
  45. package/dist/distributions/arcsine.js +19 -2
  46. package/dist/distributions/beta.js +35 -5
  47. package/dist/distributions/binomial.js +37 -14
  48. package/dist/distributions/bvnorm.js +11 -2
  49. package/dist/distributions/cauchy.js +87 -8
  50. package/dist/distributions/chisq.js +7 -3
  51. package/dist/distributions/exponential.js +11 -1
  52. package/dist/distributions/f.js +73 -22
  53. package/dist/distributions/gamma.js +61 -6
  54. package/dist/distributions/geometric.js +53 -14
  55. package/dist/distributions/hypergeometric.d.ts +5 -1
  56. package/dist/distributions/hypergeometric.js +93 -6
  57. package/dist/distributions/invgamma.js +6 -1
  58. package/dist/distributions/kumaraswamy.js +22 -4
  59. package/dist/distributions/laplace.js +31 -7
  60. package/dist/distributions/logistic.js +11 -2
  61. package/dist/distributions/lognormal.js +20 -3
  62. package/dist/distributions/multinomial.js +54 -11
  63. package/dist/distributions/nbeta.js +128 -35
  64. package/dist/distributions/nchisq.js +85 -14
  65. package/dist/distributions/negbinomial.js +100 -18
  66. package/dist/distributions/normal.js +35 -5
  67. package/dist/distributions/nt.d.ts +28 -3
  68. package/dist/distributions/nt.js +536 -35
  69. package/dist/distributions/pareto.js +9 -1
  70. package/dist/distributions/poisson.js +28 -3
  71. package/dist/distributions/signrank.js +7 -4
  72. package/dist/distributions/summary.js +450 -60
  73. package/dist/distributions/t.js +55 -3
  74. package/dist/distributions/triangular.js +45 -13
  75. package/dist/distributions/tukey.js +21 -2
  76. package/dist/distributions/uniform.js +2 -0
  77. package/dist/distributions/weibull.js +33 -3
  78. package/dist/distributions/wilcox.d.ts +9 -3
  79. package/dist/distributions/wilcox.js +155 -26
  80. package/dist/distributions.js +2 -2
  81. package/dist/htest/agreement.d.ts +3 -2
  82. package/dist/htest/agreement.js +76 -14
  83. package/dist/htest/ansariTest.d.ts +6 -1
  84. package/dist/htest/ansariTest.js +75 -8
  85. package/dist/htest/aov.d.ts +1 -1
  86. package/dist/htest/aov.js +48 -34
  87. package/dist/htest/binomTest.d.ts +5 -0
  88. package/dist/htest/binomTest.js +41 -17
  89. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  90. package/dist/htest/brunnerMunzelTest.js +7 -5
  91. package/dist/htest/chisqTest.d.ts +2 -1
  92. package/dist/htest/chisqTest.js +130 -43
  93. package/dist/htest/cor.d.ts +2 -1
  94. package/dist/htest/cor.js +17 -19
  95. package/dist/htest/core/ansari.d.ts +9 -0
  96. package/dist/htest/core/ansari.js +21 -1
  97. package/dist/htest/core/kolmogorov.d.ts +25 -0
  98. package/dist/htest/core/kolmogorov.js +123 -0
  99. package/dist/htest/core/mantelhaen.js +6 -4
  100. package/dist/htest/core/shapiro.d.ts +1 -1
  101. package/dist/htest/core/shapiro.js +5 -1
  102. package/dist/htest/dunnett.js +14 -5
  103. package/dist/htest/effectSize.d.ts +2 -0
  104. package/dist/htest/effectSize.js +91 -29
  105. package/dist/htest/fisherTest.d.ts +2 -2
  106. package/dist/htest/fisherTest.js +36 -18
  107. package/dist/htest/groups.d.ts +7 -0
  108. package/dist/htest/groups.js +21 -4
  109. package/dist/htest/ksTest.d.ts +3 -1
  110. package/dist/htest/ksTest.js +23 -2
  111. package/dist/htest/leveneTest.js +19 -3
  112. package/dist/htest/mantelhaenTest.js +125 -25
  113. package/dist/htest/mcnemarTest.js +32 -11
  114. package/dist/htest/multipleComparisons.d.ts +8 -1
  115. package/dist/htest/multipleComparisons.js +44 -21
  116. package/dist/htest/normalityTests.js +44 -10
  117. package/dist/htest/onewayTest.js +96 -5
  118. package/dist/htest/pAdjust.d.ts +3 -2
  119. package/dist/htest/pAdjust.js +38 -6
  120. package/dist/htest/pairwise.d.ts +1 -1
  121. package/dist/htest/pairwise.js +17 -15
  122. package/dist/htest/poissonTest.d.ts +1 -1
  123. package/dist/htest/poissonTest.js +111 -59
  124. package/dist/htest/polycor.js +27 -7
  125. package/dist/htest/power.d.ts +11 -1
  126. package/dist/htest/power.js +112 -8
  127. package/dist/htest/propTest.js +21 -4
  128. package/dist/htest/pwr.js +121 -24
  129. package/dist/htest/rankTests.js +29 -5
  130. package/dist/htest/tTest.js +44 -21
  131. package/dist/htest/trendTests.d.ts +7 -3
  132. package/dist/htest/trendTests.js +114 -20
  133. package/dist/htest/types.d.ts +5 -0
  134. package/dist/htest/types.js +7 -0
  135. package/dist/htest/varTest.js +45 -14
  136. package/dist/htest/wilcoxTest.js +114 -21
  137. package/dist/integrate/quadpack.js +27 -5
  138. package/dist/internal/covmat.d.ts +5 -0
  139. package/dist/internal/covmat.js +20 -0
  140. package/dist/internal/ddexp.d.ts +23 -1
  141. package/dist/internal/ddexp.js +86 -6
  142. package/dist/internal/discrete.d.ts +14 -0
  143. package/dist/internal/discrete.js +78 -5
  144. package/dist/internal/extremes.d.ts +9 -0
  145. package/dist/internal/extremes.js +24 -0
  146. package/dist/internal/fround.js +13 -4
  147. package/dist/internal/integer.d.ts +9 -0
  148. package/dist/internal/integer.js +12 -0
  149. package/dist/internal/invert.d.ts +5 -0
  150. package/dist/internal/invert.js +25 -7
  151. package/dist/internal/noncentral.d.ts +7 -0
  152. package/dist/internal/noncentral.js +9 -0
  153. package/dist/internal/poissonMixture.js +29 -6
  154. package/dist/internal/scaledInverse.d.ts +10 -0
  155. package/dist/internal/scaledInverse.js +22 -0
  156. package/dist/internal/seq.js +15 -2
  157. package/dist/internal/summary.d.ts +90 -0
  158. package/dist/internal/summary.js +458 -39
  159. package/dist/internal/uniroot.d.ts +7 -0
  160. package/dist/internal/uniroot.js +28 -3
  161. package/dist/linalg/basic.d.ts +3 -1
  162. package/dist/linalg/basic.js +51 -7
  163. package/dist/linalg/chol.js +4 -1
  164. package/dist/linalg/dot.d.ts +29 -0
  165. package/dist/linalg/dot.js +132 -0
  166. package/dist/linalg/eigen.d.ts +6 -1
  167. package/dist/linalg/eigen.js +71 -14
  168. package/dist/linalg/expm.d.ts +3 -2
  169. package/dist/linalg/expm.js +109 -15
  170. package/dist/linalg/kappa.js +58 -10
  171. package/dist/linalg/lu.d.ts +2 -1
  172. package/dist/linalg/lu.js +35 -5
  173. package/dist/linalg/matrix.d.ts +14 -0
  174. package/dist/linalg/matrix.js +24 -0
  175. package/dist/linalg/ops.js +60 -13
  176. package/dist/linalg/qr.d.ts +2 -1
  177. package/dist/linalg/qr.js +91 -23
  178. package/dist/linalg/schur.js +11 -3
  179. package/dist/linalg/solve.d.ts +6 -1
  180. package/dist/linalg/solve.js +147 -26
  181. package/dist/linalg/svd.d.ts +9 -2
  182. package/dist/linalg/svd.js +29 -11
  183. package/dist/linalg.js +2 -2
  184. package/dist/mixed.js +4 -4
  185. package/dist/models/anovaSequential.d.ts +16 -1
  186. package/dist/models/anovaSequential.js +124 -25
  187. package/dist/models/aovEz.js +140 -30
  188. package/dist/models/aovStrata.js +55 -22
  189. package/dist/models/carAnova.d.ts +1 -1
  190. package/dist/models/carAnova.js +83 -32
  191. package/dist/models/coxph.js +148 -66
  192. package/dist/models/estimable.d.ts +15 -0
  193. package/dist/models/estimable.js +68 -0
  194. package/dist/models/formula.d.ts +1 -1
  195. package/dist/models/formula.js +26 -8
  196. package/dist/models/glm.d.ts +35 -5
  197. package/dist/models/glm.js +398 -55
  198. package/dist/models/glmAnova.js +84 -13
  199. package/dist/models/glmConfint.d.ts +1 -1
  200. package/dist/models/glmConfint.js +115 -27
  201. package/dist/models/glmFamily.d.ts +10 -0
  202. package/dist/models/glmFamily.js +244 -40
  203. package/dist/models/lm.d.ts +12 -1
  204. package/dist/models/lm.js +235 -36
  205. package/dist/models/lmer.d.ts +20 -1
  206. package/dist/models/lmer.js +109 -27
  207. package/dist/models/lmerAnova.js +37 -4
  208. package/dist/models/lmerHypotheses.d.ts +6 -2
  209. package/dist/models/lmerHypotheses.js +14 -2
  210. package/dist/models/lmerTest.d.ts +9 -2
  211. package/dist/models/lmerTest.js +64 -19
  212. package/dist/models/mauchly.d.ts +4 -1
  213. package/dist/models/mauchly.js +34 -13
  214. package/dist/models/mixedStructure.js +6 -0
  215. package/dist/models/modelFrame.js +8 -2
  216. package/dist/models/modelMatrix.d.ts +3 -1
  217. package/dist/models/modelMatrix.js +40 -15
  218. package/dist/models/scaledTables.d.ts +8 -0
  219. package/dist/models/scaledTables.js +6 -0
  220. package/dist/models/survival.d.ts +6 -1
  221. package/dist/models/survival.js +67 -27
  222. package/dist/models/tiedTimes.d.ts +2 -0
  223. package/dist/models/tiedTimes.js +34 -0
  224. package/dist/numerics/deriv.d.ts +1 -1
  225. package/dist/numerics/deriv.js +64 -8
  226. package/dist/numerics/integrate.d.ts +9 -1
  227. package/dist/numerics/integrate.js +103 -16
  228. package/dist/numerics/interp.d.ts +8 -2
  229. package/dist/numerics/interp.js +352 -50
  230. package/dist/numerics/ode.js +55 -1
  231. package/dist/numerics.js +2 -2
  232. package/dist/optim/localmin.js +4 -0
  233. package/dist/optim/optim.d.ts +3 -1
  234. package/dist/optim/optim.js +80 -8
  235. package/dist/random/beta.js +53 -4
  236. package/dist/random/binomial.d.ts +14 -3
  237. package/dist/random/binomial.js +59 -24
  238. package/dist/random/continuous.js +33 -5
  239. package/dist/random/extra.js +42 -9
  240. package/dist/random/gamma.d.ts +4 -1
  241. package/dist/random/gamma.js +36 -8
  242. package/dist/random/hyper.js +9 -6
  243. package/dist/random/mt.js +17 -2
  244. package/dist/random/poisson.js +26 -3
  245. package/dist/random/r2dtable.d.ts +8 -1
  246. package/dist/random/r2dtable.js +24 -3
  247. package/dist/random/rank.d.ts +2 -0
  248. package/dist/random/rank.js +44 -4
  249. package/dist/random/sample.d.ts +12 -2
  250. package/dist/random/sample.js +57 -20
  251. package/dist/random/streams.js +4 -2
  252. package/dist/rstats.js +14 -14
  253. package/dist/special/besselIK.d.ts +12 -0
  254. package/dist/special/besselIK.js +168 -23
  255. package/dist/special/besselJY.d.ts +11 -1
  256. package/dist/special/besselJY.js +127 -19
  257. package/dist/special/besselSeries.d.ts +27 -0
  258. package/dist/special/besselSeries.js +45 -2
  259. package/dist/special/beta.js +11 -1
  260. package/dist/special/choose.js +17 -8
  261. package/dist/special/dd.js +122 -18
  262. package/dist/special/extra.js +14 -2
  263. package/dist/special/gamma.js +15 -2
  264. package/dist/special/igam.js +33 -6
  265. package/dist/special/incbet.js +100 -10
  266. package/dist/special/incbetLargeA.js +7 -2
  267. package/dist/special/incbetPrefactor.d.ts +5 -0
  268. package/dist/special/incbetPrefactor.js +14 -3
  269. package/dist/special/incbetTemme.d.ts +7 -0
  270. package/dist/special/incbetTemme.js +45 -12
  271. package/dist/special/loader.d.ts +1 -1
  272. package/dist/special/loader.js +179 -33
  273. package/dist/special/log1pmx.js +4 -0
  274. package/dist/special.js +2 -2
  275. package/dist/tests.js +11 -11
  276. package/llms.txt +12 -6
  277. package/package.json +1 -1
  278. package/dist/chunks/chunk-2CUKVVRO.js +0 -2
  279. package/dist/chunks/chunk-3WQCKGKP.js +0 -2
  280. package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
  281. package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
  282. package/dist/chunks/chunk-D4F7GQAY.js +0 -2
  283. package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
  284. package/dist/chunks/chunk-HBMHR5SS.js +0 -2
  285. package/dist/chunks/chunk-IBPWRQIF.js +0 -2
  286. package/dist/chunks/chunk-JLAH3X77.js +0 -2
  287. package/dist/chunks/chunk-LG74QY5U.js +0 -2
  288. package/dist/chunks/chunk-PEYGUK32.js +0 -2
  289. package/dist/chunks/chunk-Q4PGYESP.js +0 -2
  290. package/dist/chunks/chunk-RCAL4VHM.js +0 -3
  291. package/dist/chunks/chunk-SREPSVTT.js +0 -2
  292. package/dist/chunks/chunk-UFT662QZ.js +0 -2
  293. package/dist/chunks/chunk-VWJ74UHC.js +0 -2
  294. package/dist/chunks/chunk-XNPU54NH.js +0 -2
  295. package/dist/chunks/chunk-XPLDEK6D.js +0 -2
  296. package/dist/chunks/chunk-YGT4P7EU.js +0 -2
  297. package/dist/chunks/chunk-YVREYVI2.js +0 -2
@@ -31,9 +31,16 @@ const TOL = Number.EPSILON ** 0.25;
31
31
  * @category Numerical methods
32
32
  */
33
33
  export function integrate(f, lower, upper, options = {}) {
34
- const limit = options.subdivisions ?? 100;
34
+ const subdivisions = options.subdivisions ?? 100;
35
+ const limit = Math.trunc(subdivisions);
35
36
  const relTol = options.relTol ?? TOL;
36
37
  const absTol = options.absTol ?? relTol;
38
+ // infinite or negative tolerances are errors (R accepts an infinite one and returns the first, coarse estimate as converged);
39
+ // subdivisions as R's as.integer: beyond the integer range it is NA and an error in R (2^32 reached the typed-array
40
+ // allocation, #369)
41
+ if (!(Math.abs(subdivisions) <= 2147483647) || !Number.isFinite(relTol) || !Number.isFinite(absTol) || relTol < 0 || absTol < 0) {
42
+ throw new RangeError("invalid parameter values");
43
+ }
37
44
  const stopOnError = options.stopOnError ?? true;
38
45
  if (Number.isNaN(lower) || Number.isNaN(upper))
39
46
  throw new RangeError("a limit is NA or NaN");
@@ -45,19 +52,42 @@ export function integrate(f, lower, upper, options = {}) {
45
52
  throw new RangeError("non-finite function value");
46
53
  return v;
47
54
  };
48
- const r = Number.isFinite(lower) && Number.isFinite(upper)
49
- ? dqagse(g, lower, upper, absTol, relTol, limit)
50
- : Number.isFinite(lower)
51
- ? dqagie(g, lower, 1, absTol, relTol, limit)
52
- : Number.isFinite(upper)
53
- ? dqagie(g, upper, -1, absTol, relTol, limit)
54
- : dqagie(g, 0, 2, absTol, relTol, limit);
55
+ // An infinite limit is taken with its sign: from a to -Inf is minus the integral over (-Inf, a), from Inf to b minus
56
+ // that over (b, Inf), from Inf to -Inf minus the whole line (R integrates over (a, Inf) and (-Inf, Inf) there, so
57
+ // integrate(f, 0, -Inf) had the sign and the side wrong), and between equal infinite limits 0
58
+ if (lower === upper && !Number.isFinite(lower))
59
+ return { value: 0, absError: 0, subdivisions: 0, ier: 0, message: MESSAGES[0] };
60
+ let sign = 1;
61
+ if (lower === Infinity || upper === -Infinity) {
62
+ [lower, upper] = [upper, lower];
63
+ sign = -1;
64
+ }
65
+ let r;
66
+ if (Number.isFinite(lower) && Number.isFinite(upper)) {
67
+ // b - a or a midpoint of the subintervals (up to 2 max(|a|, |b|)) overflows (both finite): x = 2u, the integral of
68
+ // f(2u) over (a/2, b/2) doubled, with the same nodes (it ended in "roundoff error" for a density spread over
69
+ // (-1e308, 1e308), and 1/x over (1e308, 1.1e308) was 0 with every node at Inf)
70
+ if (Number.isFinite(2 * Math.max(Math.abs(lower), Math.abs(upper))))
71
+ r = dqagse(g, lower, upper, absTol, relTol, limit);
72
+ else {
73
+ // (a positive absTol kept positive: MIN halved to 0 made the input invalid; issue #122)
74
+ const halfTol = absTol > 0 ? Math.max(absTol * 0.5, Number.MIN_VALUE) : absTol * 0.5;
75
+ const h = dqagse((u) => g(2 * u), lower * 0.5, upper * 0.5, halfTol, relTol, limit);
76
+ r = { ...h, result: 2 * h.result, abserr: 2 * h.abserr };
77
+ }
78
+ }
79
+ else if (Number.isFinite(lower))
80
+ r = dqagie(g, lower, 1, absTol, relTol, limit);
81
+ else if (Number.isFinite(upper))
82
+ r = dqagie(g, upper, -1, absTol, relTol, limit);
83
+ else
84
+ r = dqagie(g, 0, 2, absTol, relTol, limit);
55
85
  const message = MESSAGES[r.ier] ?? "unknown error";
56
86
  if (r.ier === 6)
57
87
  throw new RangeError("the input is invalid");
58
88
  if (r.ier !== 0 && stopOnError)
59
89
  throw new RangeError(message);
60
- return { value: r.result, absError: r.abserr, subdivisions: r.last, ier: r.ier, message };
90
+ return { value: sign * r.result, absError: r.abserr, subdivisions: r.last, ier: r.ier, message };
61
91
  }
62
92
  /**
63
93
  * Minimum (or maximum) of a function of one variable on an interval, by Brent's golden-section and parabolic
@@ -74,6 +104,9 @@ export function integrate(f, lower, upper, options = {}) {
74
104
  export function optimize(f, lower, upper, options = {}) {
75
105
  const maximum = options.maximum ?? false;
76
106
  const tol = options.tol ?? TOL;
107
+ // (a positive tol, as in R: with tol = 0 the search stopped narrowing among the subnormals, see localMin)
108
+ if (!(tol > 0 && tol < Infinity))
109
+ throw new RangeError("'tol' must be a positive finite number");
77
110
  // R checks the ends before calling f, lower first
78
111
  if (!Number.isFinite(lower))
79
112
  throw new RangeError("invalid 'xmin' value");
@@ -81,8 +114,36 @@ export function optimize(f, lower, upper, options = {}) {
81
114
  throw new RangeError("invalid 'xmax' value");
82
115
  if (!(lower < upper))
83
116
  throw new RangeError("'xmin' not less than 'xmax'");
84
- const r = localMin(maximum ? (x) => -f(x) : f, lower, upper, tol);
85
- return { optimum: r.x, objective: f(r.x) };
117
+ // as R, the search sees NaN and +Inf as the largest double and -Inf as the most negative (R warns, "NA/NaN replaced
118
+ // by maximum positive value"; the library does not report warnings); objective is f itself
119
+ const fn = (x) => {
120
+ const v = maximum ? -f(x) : f(x);
121
+ return Number.isNaN(v) || v === Infinity ? Number.MAX_VALUE : v === -Infinity ? -Number.MAX_VALUE : v;
122
+ };
123
+ // upper - lower or upper + lower overflows (both finite): search over u = x/2 on (lower/2, upper/2) with tol/2 (the
124
+ // search did not return: optimize((x / 1e308)^2, -1e308, 1e308), and with the midpoint 0.5 (a + b) = Infinity,
125
+ // optimize((x / 1e308 - 1.05)^2, 1e308, 1.1e308); R too); also where an end is beyond Number.MAX_VALUE / 2, as the
126
+ // midpoint of a bracket narrowed near it overflows (optimize((x / 1e308 + 1.6)^2, -1.7e308, 1e306), #212)
127
+ // (2 lower and 2 upper finite imply that upper - lower and upper + lower are)
128
+ const x = Number.isFinite(2 * lower) && Number.isFinite(2 * upper)
129
+ ? localMin(fn, lower, upper, tol).x
130
+ : 2 * localMin((u) => fn(2 * u), lower * 0.5, upper * 0.5, tol * 0.5).x;
131
+ return { optimum: x, objective: f(x) };
132
+ }
133
+ /**
134
+ * extendInt of uniroot, as R's match.arg(extendInt) against c("no", "yes", "downX", "upX"): the full name or a
135
+ * unique prefix; anything else is an error before f is evaluated (it was taken as "no", #247)
136
+ */
137
+ function extendIntArg(e) {
138
+ if (e === undefined)
139
+ return "no";
140
+ const choices = ["no", "yes", "downX", "upX"];
141
+ if (typeof e === "string" && e.length > 0) {
142
+ const hit = choices.filter((v) => v.startsWith(e));
143
+ if (hit.length === 1)
144
+ return hit[0];
145
+ }
146
+ throw new RangeError(`'extendInt' should be one of "no", "yes", "downX", "upX"`);
86
147
  }
87
148
  /**
88
149
  * A root of a function of one variable in an interval, by Brent's zeroin. Equivalent to R's `uniroot()`.
@@ -95,9 +156,27 @@ export function optimize(f, lower, upper, options = {}) {
95
156
  * @category Numerical methods
96
157
  */
97
158
  export function uniroot(f, lower, upper, options = {}) {
159
+ // R's "invalid 'tol' value" for tol <= 0 (tol = 0 was accepted, Brent's method then ran to maxiter or the doubles)
160
+ const tol = options.tol ?? TOL;
161
+ if (!(tol > 0 && tol < Infinity))
162
+ throw new RangeError("'tol' must be a positive finite number");
163
+ return unirootCore(f, lower, upper, options);
164
+ }
165
+ /**
166
+ * {@link uniroot} without the check of tol > 0: the internal callers that run Brent's method until the bracket is as
167
+ * narrow as the doubles allow (tol = 0, the noncentrality roots of src/htest/effectSize.ts) or whose tolerance is
168
+ * scaled by a standard error (src/internal/uniroot.ts)
169
+ *
170
+ * @internal
171
+ */
172
+ export function unirootCore(f, lower, upper, options = {}) {
98
173
  const tol = options.tol ?? TOL;
99
- const maxiter = options.maxiter ?? 1000;
100
- const extendInt = options.extendInt ?? "no";
174
+ const maxiter = Math.trunc(options.maxiter ?? 1000);
175
+ if (!(tol >= 0 && tol < Infinity))
176
+ throw new RangeError("'tol' must be a non-negative finite number");
177
+ if (!(maxiter >= 1 && maxiter < Infinity))
178
+ throw new RangeError("'maxiter' must be a positive integer");
179
+ const extendInt = extendIntArg(options.extendInt);
101
180
  if (!(lower < upper))
102
181
  throw new RangeError("lower < upper is not fulfilled");
103
182
  let fLo = options.fLower ?? f(lower);
@@ -110,7 +189,9 @@ export function uniroot(f, lower, upper, options = {}) {
110
189
  const bound = (v) => Math.max(-Number.MAX_VALUE, Math.min(Number.MAX_VALUE, v));
111
190
  const step0 = (u) => 0.01 * Math.max(1e-4, Math.abs(u));
112
191
  const sig = extendInt === "downX" ? -1 : extendInt === "upX" ? 1 : 0;
113
- const extend = extendInt === "yes" ? bound(fLo) * bound(fUp) > 0 : sig !== 0 && (sig * bound(fLo) > 0 || sig * bound(fUp) < 0);
192
+ // same sign from the signs, not the product (1e-300 * 2e-300 underflows to 0, and the interval was not extended)
193
+ const sameSign = (a, b) => Math.sign(a) * Math.sign(b) > 0;
194
+ const extend = extendInt === "yes" ? sameSign(fLo, fUp) : sig !== 0 && (sig * bound(fLo) > 0 || sig * bound(fUp) < 0);
114
195
  let extIter = 0;
115
196
  const count = () => {
116
197
  if (++extIter > maxiter)
@@ -119,7 +200,7 @@ export function uniroot(f, lower, upper, options = {}) {
119
200
  if (extend && extendInt === "yes") {
120
201
  let dLo = step0(lower);
121
202
  let dUp = step0(upper);
122
- while (fLo * fUp > 0 && (Number.isFinite(lower) || Number.isFinite(upper))) {
203
+ while (sameSign(fLo, fUp) && (Number.isFinite(lower) || Number.isFinite(upper))) {
123
204
  count();
124
205
  if (Number.isFinite(lower)) {
125
206
  const x = lower - dLo;
@@ -165,7 +246,13 @@ export function uniroot(f, lower, upper, options = {}) {
165
246
  throw new RangeError("invalid 'xmin' value");
166
247
  if (!Number.isFinite(upper))
167
248
  throw new RangeError("invalid 'xmax' value");
168
- const z = zero((x) => finiteValue(f(x)), lower, upper, finiteValue(fLo), finiteValue(fUp), tol, maxiter);
249
+ // upper - lower overflows (both finite): the root of f(2u) on (lower/2, upper/2) with tol/2, doubled (uniroot(x,
250
+ // -1e308, 1e308) gave -Infinity)
251
+ const wide = !Number.isFinite(upper - lower);
252
+ const zh = wide
253
+ ? zero((u) => finiteValue(f(2 * u)), lower * 0.5, upper * 0.5, finiteValue(fLo), finiteValue(fUp), tol * 0.5, maxiter)
254
+ : zero((x) => finiteValue(f(x)), lower, upper, finiteValue(fLo), finiteValue(fUp), tol, maxiter);
255
+ const z = wide ? { ...zh, root: 2 * zh.root, estimPrec: 2 * zh.estimPrec } : zh;
169
256
  return {
170
257
  root: z.root,
171
258
  fRoot: f(z.root),
@@ -14,7 +14,10 @@ export type Ties = "mean" | "min" | "max" | ((v: number[]) => number);
14
14
  export interface ApproxOptions {
15
15
  /** Points to interpolate at. Default: `n` equally spaced points over the range of `x`. */
16
16
  xout?: Sample;
17
- /** `"linear"` (default) or `"constant"` (step function). */
17
+ /**
18
+ * `"linear"` (default) or `"constant"` (step function). A unique prefix is accepted and anything else is an
19
+ * error, as in R.
20
+ */
18
21
  method?: "linear" | "constant";
19
22
  /** Number of points when `xout` is not given. Default `50`. */
20
23
  n?: number;
@@ -22,7 +25,10 @@ export interface ApproxOptions {
22
25
  yleft?: number;
23
26
  /** Value right of the data. Default: NaN (rule 1) or the last y (rule 2). */
24
27
  yright?: number;
25
- /** Extrapolation rule (1: NaN, 2: the end value), for both sides or `[left, right]`. Default `1`. */
28
+ /**
29
+ * Extrapolation rule (1: NaN, 2: the end value), for both sides or `[left, right]`. Default `1`. Any other value is
30
+ * an error (R takes every value other than 1 as 2).
31
+ */
26
32
  rule?: number | readonly [number, number];
27
33
  /** For `"constant"`: the step value is (1 - f) y_left + f y_right, with f in [0, 1] (else an error, as in R). Default `0`. */
28
34
  f?: number;