@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
@@ -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))
@@ -32,5 +32,10 @@ export interface InvertProblem {
32
32
  */
33
33
  positive?: boolean;
34
34
  maxIter?: number;
35
+ /**
36
+ * (Optional) The noise of log F (absolute): a point whose residual is within it is returned as it is (where the
37
+ * distribution function is an integral accurate to about 1e-13, the steps would otherwise wander over a few ulps of x)
38
+ */
39
+ residualTolerance?: number;
35
40
  }
36
41
  export declare function invertContinuous(problem: InvertProblem): number;
@@ -9,7 +9,7 @@ export function invertContinuous(problem) {
9
9
  const residual = (x, lF) => {
10
10
  if (linearTarget !== undefined && cdf !== undefined && lF > -700) {
11
11
  const F = cdf(x, lower);
12
- if (F > 0)
12
+ if (F > 0 && F / linearTarget < Infinity)
13
13
  return Math.log(F / linearTarget);
14
14
  }
15
15
  return lF - logTarget;
@@ -21,7 +21,9 @@ export function invertContinuous(problem) {
21
21
  return [lF, residual(x, lF)];
22
22
  }
23
23
  const [lF, F] = cdfBoth(x, lower);
24
- if (linearTarget !== undefined && lF > -700 && F > 0)
24
+ // (F / target overflows for a subnormal target; the residual was then Infinity and the steps went astray:
25
+ // qgamma(7.2e-317, 3.3e126, { scale: 0.136, lowerTail: false }) was 8.2e146, 1e21 times the mean)
26
+ if (linearTarget !== undefined && lF > -700 && F > 0 && F / linearTarget < Infinity)
25
27
  return [lF, Math.log(F / linearTarget)];
26
28
  return [lF, lF - logTarget];
27
29
  };
@@ -43,7 +45,7 @@ export function invertContinuous(problem) {
43
45
  bestResid = Math.abs(resid);
44
46
  best = u;
45
47
  }
46
- if (resid === 0)
48
+ if (resid === 0 || Math.abs(resid) <= (problem.residualTolerance ?? 0))
47
49
  return x;
48
50
  // The lower probability is increasing in x, the upper decreasing
49
51
  if (resid < 0 === lower)
@@ -94,8 +96,11 @@ export function invertContinuous(problem) {
94
96
  let x = x0;
95
97
  let [lF, r] = evaluate(x);
96
98
  for (let k = 0; k < 4 && r !== 0; k++) {
97
- const slope = (lower ? 1 : -1) * Math.exp(pdfLog(x) - lF);
98
- const xn = x - r / slope;
99
+ const lf = pdfLog(x) - lF;
100
+ const slope = (lower ? 1 : -1) * Math.exp(lf);
101
+ // (where f/F overflows, as at a subnormal x, the step relative to x: qbeta(3.8e-277, 27.9, 1.5e299) is 9.6e-309,
102
+ // and the step there was 0, which left the answer 5e-14 off)
103
+ const xn = Number.isFinite(slope) ? x - r / slope : x - (lower ? 1 : -1) * r * x * Math.exp(-(lf + Math.log(x)));
99
104
  if (!(xn > 0) || !Number.isFinite(xn) || xn === x)
100
105
  break;
101
106
  const [lFn, rn] = evaluate(xn);
@@ -140,8 +145,11 @@ export function invertContinuous(problem) {
140
145
  }
141
146
  if (!(xl > 0 && xh > xl && Number.isFinite(xh)))
142
147
  return x0;
143
- for (let k = 0; k < 64; k++) {
144
- 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);
145
153
  if (mid <= xl || mid >= xh)
146
154
  break;
147
155
  const r = evaluate(mid)[1];
@@ -157,6 +165,16 @@ export function invertContinuous(problem) {
157
165
  return Math.abs(evaluate(xl)[1]) < Math.abs(evaluate(xh)[1]) ? xl : xh;
158
166
  }
159
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
+ }
160
178
  /** Midpoint of the bracketing interval. If an end is infinite, expand outward from the current value */
161
179
  function midpoint(lo, hi, x) {
162
180
  if (Number.isFinite(lo) && Number.isFinite(hi))
@@ -46,3 +46,10 @@ export declare function quadratureControl(tol?: number, maxBisections?: number):
46
46
  * would otherwise be bisected to the full depth (2^40 intervals)
47
47
  */
48
48
  export declare function kronrodAdaptive(f: (u: number) => number, a: number, b: number, total: number, depth: number, control: QuadratureControl): number;
49
+ /**
50
+ * Whether a noncentral density equals the central one to the double: it is e^-μ Σ_j μ^j/j! f_j, and f_1/f_0 is r
51
+ * (x (a + b)/a for the beta, x/k for χ² with k df), with the ratios of later terms smaller still, so the terms from
52
+ * j = 1 on and the factor e^-μ change it by less than 2 μ max(1, r) relative. Below 1e-20 that is the central density
53
+ * itself (at a tiny or subnormal λ the mixture's weights were NaN: dbeta(1.05e-231, 0.19, 141, { ncp: 1.4e-303 }))
54
+ */
55
+ export declare function negligibleNcp(mu: number, r: number): boolean;
@@ -113,3 +113,12 @@ export function kronrodAdaptive(f, a, b, total, depth, control) {
113
113
  return k;
114
114
  return kronrodAdaptive(f, a, m, total, depth + 1, control) + kronrodAdaptive(f, m, b, total, depth + 1, control);
115
115
  }
116
+ /**
117
+ * Whether a noncentral density equals the central one to the double: it is e^-μ Σ_j μ^j/j! f_j, and f_1/f_0 is r
118
+ * (x (a + b)/a for the beta, x/k for χ² with k df), with the ratios of later terms smaller still, so the terms from
119
+ * j = 1 on and the factor e^-μ change it by less than 2 μ max(1, r) relative. Below 1e-20 that is the central density
120
+ * itself (at a tiny or subnormal λ the mixture's weights were NaN: dbeta(1.05e-231, 0.19, 141, { ncp: 1.4e-303 }))
121
+ */
122
+ export function negligibleNcp(mu, r) {
123
+ return mu * Math.max(1, r) < 1e-20;
124
+ }
@@ -84,7 +84,12 @@ export function dpoisLogDD(x, lambda) {
84
84
  if (x === 0)
85
85
  return [-lambda, 0];
86
86
  const DBL_MIN = 2.2250738585072014e-308;
87
- if (x <= lambda * DBL_MIN || lambda < x * DBL_MIN || !Number.isFinite(x)) {
87
+ // (λ < x DBL_MIN not for x in [1, 1e10], where lgamma1p(x) in double would put an absolute error of eps log Γ(x + 1)
88
+ // into a log that is not much larger: pchisq(1e-310, 1.2e-51, { ncp: 197 }) was 3.8e-15 off, and 1.4e-15 at x = 1e-307;
89
+ // bd0dd takes the logs of x and λ apart for such ratios. Below 1 log Γ(x + 1) is small, while stirlerr(x) and log x
90
+ // cancel in Loader's form; above 1e10 the error is below eps of x log λ)
91
+ const moderate = x >= 1 && x <= 1e10;
92
+ if (x <= lambda * DBL_MIN || (lambda < x * DBL_MIN && !moderate) || !Number.isFinite(x)) {
88
93
  const l = ddAdd(ddMul(ddLog([lambda, 0]), [x, 0]), [-lambda - lgamma1p(x), 0]);
89
94
  return l;
90
95
  }
@@ -298,6 +303,21 @@ function logRatio(a, b) {
298
303
  return Infinity;
299
304
  return Math.log(a.h / b.h) + (a.l / a.h - b.l / b.h) + (a.k - b.k) * LN_UNIT;
300
305
  }
306
+ /**
307
+ * w_j = w_(j-1) μ / j. Where μ is beyond 1e290 the splitting of the quotient in ddDivNum overflows (the noncentral χ²
308
+ * at λ = 1e302 and x = 1e-300 was NaN), so μ is multiplied in as a scaled number instead. (R 4.5.2 gives
309
+ * -1.0243631212370614e307 for dchisq(1e-308, 294.4, ncp = 2.9e304, log = TRUE), which is -λ/2 = -1.45e304 to the double)
310
+ */
311
+ function weightUp(w, mu, j) {
312
+ return mu < 1e290 ? mulDD(w, ddDivNum([mu, 0], j)) : divDD(mulDD(w, [mu, 0]), [j, 0]);
313
+ }
314
+ /**
315
+ * w_(j-1) = w_j j / μ, with μ split off as a scaled number where the splitting in ddDivNum would overflow: for a tiny
316
+ * μ (of j / μ; the densities at λ = 1.4e-303 were NaN) and for a huge one (of μ)
317
+ */
318
+ function weightDown(w, j, mu) {
319
+ return mu > 1e-290 && mu < 1e290 ? mulDD(w, ddDivNum([j, 0], mu)) : divDD(mulDD(w, [j, 0]), [mu, 0]);
320
+ }
301
321
  function weight(j, mu) {
302
322
  return fromValue(dpoisRaw(j, mu, false), () => dpoisLogDD(j, mu));
303
323
  }
@@ -389,7 +409,7 @@ function sumLower(mu, k) {
389
409
  break;
390
410
  // P_{j-1} = P_j + d_{j-1}, w_{j-1} = w_j j/μ, d_{j-2} = d_{j-1} / (d_{j-1}/d_{j-2})
391
411
  p = add(p, d);
392
- w = refresh(mulDD(w, ddDivNum([j, 0], mu)), () => weight(j - 1, mu));
412
+ w = refresh(weightDown(w, j, mu), () => weight(j - 1, mu));
393
413
  if (j >= 2)
394
414
  d = d.h === 0 ? increment(k, j - 2) : refresh(divRatio(d, k, j - 1), () => increment(k, j - 2));
395
415
  }
@@ -430,7 +450,7 @@ function sumUpper(mu, k) {
430
450
  prev = t;
431
451
  // Q_{j+1} = Q_j + d_j, w_{j+1} = w_j μ/(j+1), d_{j+1} = d_j (d_{j+1}/d_j)
432
452
  q = add(q, d);
433
- w = refresh(mulDD(w, ddDivNum([mu, 0], j + 1)), () => weight(j + 1, mu));
453
+ w = refresh(weightUp(w, mu, j + 1), () => weight(j + 1, mu));
434
454
  d = d.h === 0 ? increment(k, j + 1) : refresh(mulRatio(d, k, j + 1), () => increment(k, j + 1));
435
455
  }
436
456
  if (restart < 0)
@@ -445,7 +465,10 @@ function sumUpper(mu, k) {
445
465
  * Returns the sum divided by divisor (as a log if log).
446
466
  */
447
467
  export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
448
- const j0 = Number.isFinite(jStar) ? Math.max(0, Math.round(jStar)) : 0;
468
+ // (μ = 0 where λ/2 underflows, at λ = 5e-324: the j = 0 term alone, as for the central distribution, whose weight is
469
+ // the only one not 0; the terms from j = 1 on are of relative size μ times the ratio of the densities of components 1
470
+ // and 0, negligible unless that ratio is above 1e300)
471
+ const j0 = Number.isFinite(jStar) && mu > 0 ? Math.max(0, Math.round(jStar)) : 0;
449
472
  const sum = new ScaledSum();
450
473
  const w0 = weight(j0, mu);
451
474
  const d0 = increment(kernel, j0);
@@ -461,7 +484,7 @@ export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
461
484
  if (t.h === 0 && j > mu + 50 && j > j0 + 50)
462
485
  break; // all later terms are 0 too
463
486
  prev = t;
464
- w = refresh(mulDD(w, ddDivNum([mu, 0], j + 1)), () => weight(j + 1, mu));
487
+ w = refresh(weightUp(w, mu, j + 1), () => weight(j + 1, mu));
465
488
  d = d.h === 0 ? increment(kernel, j + 1) : refresh(mulRatio(d, kernel, j + 1), () => increment(kernel, j + 1));
466
489
  }
467
490
  // Downward
@@ -469,7 +492,7 @@ export function mixtureDensity(mu, kernel, coef, jStar, divisor, log) {
469
492
  d = d0;
470
493
  prev = ZERO;
471
494
  for (let j = j0 - 1; j >= 0; j--) {
472
- w = refresh(mulDD(w, ddDivNum([j + 1, 0], mu)), () => weight(j, mu));
495
+ w = refresh(weightDown(w, j + 1, mu), () => weight(j, mu));
473
496
  d = d.h === 0 ? increment(kernel, j) : refresh(divRatio(d, kernel, j + 1), () => increment(kernel, j));
474
497
  const t = mulDD(mul(w, d), [coef(j), 0]);
475
498
  sum.add(t);
@@ -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;