@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
@@ -14,8 +14,8 @@ import { lbeta } from "../special/beta.js";
14
14
  import { ddAdd, ddDiv, ddDivNum, ddLog, ddMul, ddMulNum, ddNeg, ddSub, twoProd, twoSum } from "../special/dd.js";
15
15
  import { betaIncBothDD, betaIncDD } from "../special/incbet.js";
16
16
  import { bd0, bd0dd, stirlerr } from "../special/loader.js";
17
- import { pnorm, qnorm } from "./normal.js";
18
- import { dnt1, pnt1, qnt1 } from "./nt.js";
17
+ import { dnorm, pnorm, qnorm } from "./normal.js";
18
+ import { HUGE_DF, dnt1, hugeDfRatio, hugeDfTail, hugeDfTailLog, hugeDfTailQuantile, pnt1, qnt1 } from "./nt.js";
19
19
  const LN_SQRT_2PI = 0.918938533204672741780329736406;
20
20
  /** -log(sqrt(2 pi)) in double-double (computed with Rmpfr) */
21
21
  const LN_SQRT_2PI_DD_NEG = [-0.91893853320467278, 3.8782941580672414e-17];
@@ -68,8 +68,34 @@ export function dtDensity1(x, df, log) {
68
68
  const lv = -0.5 * x * x - LN_SQRT_2PI;
69
69
  return log ? lv : Math.exp(lv);
70
70
  }
71
+ if (df > HUGE_DF) {
72
+ if (hugeDfRatio(x, df) < 1e-17)
73
+ return dnorm(x, { log });
74
+ const lv = hugeDfTailLog(x, df);
75
+ return log ? lv : Math.exp(lv);
76
+ }
77
+ if (df < TINY_DF_DENSITY)
78
+ return dtTinyDf(x, df, log);
71
79
  return dtCore(x, df, log);
72
80
  }
81
+ /**
82
+ * Below this n the density is sqrt(n) / (2 sqrt(1 + x^2/n)) (dtTinyDf). dtCore loses digits there (1e-14 of the
83
+ * value at n = 1e-80), and for a subnormal n, h = n/2 is rounded (0 at the smallest subnormal: NaN)
84
+ */
85
+ const TINY_DF_DENSITY = 1e-20;
86
+ /**
87
+ * Density for n < TINY_DF_DENSITY: with Gamma(n/2) = (2/n)(1 + O(n)), f(x) = sqrt(n)/2 (1 + x^2/n)^(-1/2) times
88
+ * (1 + O(n)) and (1 + x^2/n)^(-n/2), whose log, n log(|x|/sqrt(n)) at most, is below 1e-17
89
+ */
90
+ function dtTinyDf(x, n, log) {
91
+ const sn = Math.sqrt(n);
92
+ const ax = Math.abs(x);
93
+ // u = |x|/sqrt(n) may overflow; beyond 1e150, sqrt(1 + u^2) = u
94
+ const u = ax / sn;
95
+ if (log)
96
+ return Math.log(0.5 * sn) - (u < 1e150 ? 0.5 * Math.log1p(u * u) : Math.log(ax) - Math.log(sn));
97
+ return u < 1e150 ? (0.5 * sn) / Math.sqrt(1 + u * u) : 0.5 * sn * (sn / ax);
98
+ }
73
99
  /** Below this n the upper tail is 1/2 - tinyDfDelta (the next terms are under 1e-23 absolute) */
74
100
  const TINY_DF = 1e-15;
75
101
  /**
@@ -103,6 +129,10 @@ function betaArgs(t, n) {
103
129
  }
104
130
  /** Log or value of P[T > t] (t > 0): half of s = P[|T| > t] = I_{n/(n+t^2)}(n/2, 1/2) */
105
131
  function upperTailPositive(t, n, log) {
132
+ // (qt's solver can step to t = Infinity, which gave NaN and left it at a wrong point: qt(0.499999999, 1e-10, upper)
133
+ // was 0.59, not 2425.8)
134
+ if (t === Infinity)
135
+ return log ? -Infinity : 0;
106
136
  if (n < TINY_DF) {
107
137
  const d = tinyDfDelta(t, n);
108
138
  return log ? Math.log1p(-2 * d) - Math.LN2 : 0.5 - d;
@@ -123,7 +153,7 @@ function upperTailPositive(t, n, log) {
123
153
  }
124
154
  /** [log, linear] of upperTailPositive (each value identical to upperTailPositive) */
125
155
  function upperTailPositiveBoth(t, n) {
126
- if (n < TINY_DF)
156
+ if (n < TINY_DF || t === Infinity)
127
157
  return [upperTailPositive(t, n, true), upperTailPositive(t, n, false)];
128
158
  const t2 = t * t;
129
159
  const x = n / (n + t2);
@@ -147,6 +177,8 @@ export function pt1(q, df, lowerTail, logP) {
147
177
  return pnorm(q, { lowerTail, logP });
148
178
  if (q === 0)
149
179
  return logP ? -Math.LN2 : 0.5;
180
+ if (df > HUGE_DF)
181
+ return hugeDfRatio(q, df) < 1e-17 ? pnorm(q, { lowerTail, logP }) : hugeDfTail(q, df, lowerTail, logP);
150
182
  // Lower tail for q < 0 = upper tail at |q|. If the requested side is the smaller tail compute it directly, otherwise 1 - (smaller tail)
151
183
  const small = q < 0 === lowerTail;
152
184
  const a = Math.abs(q);
@@ -166,8 +198,25 @@ export function qt1(p, df, lowerTail, logP) {
166
198
  if (!Number.isFinite(df))
167
199
  return qnorm(p, { lowerTail, logP });
168
200
  const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
201
+ if (df > HUGE_DF) {
202
+ // The quantiles of the limits of pt1
203
+ const z = qnorm(p, { lowerTail, logP });
204
+ if (hugeDfRatio(z, df) < 1e-17)
205
+ return z;
206
+ const y = hugeDfTailQuantile(logTarget, df);
207
+ return lower ? -y : y;
208
+ }
169
209
  if (logTarget === -Math.LN2 && linearTarget === 0.5)
170
210
  return 0;
211
+ if (df < TINY_DF) {
212
+ // Invert P[T > t] = 1/2 - (n/2) asinh(t / sqrt(n)) (tinyDfDelta) for t: Infinity unless p is within about
213
+ // 400 n of 1/2. (Through the general code below lbeta(n/2, 1/2) is Infinity at a subnormal n, and the solver stopped
214
+ // on the flat tail: qt(1e-5, 1e-310, upper) was 4.26, although P[T > t] is 1/2 to the double there)
215
+ const d = linearTarget !== undefined ? 0.5 - linearTarget : -0.5 * Math.expm1(logTarget + Math.LN2);
216
+ const A = (2 * d) / df;
217
+ const y = A < 700 ? Math.sqrt(df) * Math.sinh(A) : Math.exp(A - Math.LN2 + 0.5 * Math.log(df));
218
+ return lower ? -y : y;
219
+ }
171
220
  // Smaller tail: if lower, the answer is negative. Solve the upper tail in |t|
172
221
  // Starting value: in the deep tail P[T > t] ≈ n^(n/2) t^(-n) / (n B(n/2, 1/2))
173
222
  const n = df;
@@ -195,6 +244,9 @@ export function qt1(p, df, lowerTail, logP) {
195
244
  const g3 = (z * (((3 * z2 + 19) * z2 + 17) * z2 - 15)) / 384;
196
245
  const g4 = (z * ((((79 * z2 + 776) * z2 + 1482) * z2 - 1920) * z2 - 945)) / 92160;
197
246
  x0 = z + (g1 + (g2 + (g3 + g4 / n) / n) / n) / n;
247
+ // (the terms overflow for a huge z: qt(-1e100, 1e150, log = TRUE) was -1, solved from x0 = 1)
248
+ if (!Number.isFinite(x0))
249
+ x0 = z;
198
250
  }
199
251
  }
200
252
  if (!(x0 > 0) || !Number.isFinite(x0))
@@ -7,6 +7,8 @@
7
7
  // of lengths (each at most 1), so that wide and narrow intervals neither overflow nor underflow on the way.
8
8
  import { vectorize } from "../internal/vectorize.js";
9
9
  import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
10
+ import { midpoint } from "../internal/extremes.js";
11
+ const MIN_NORMAL = 2 ** -1022;
10
12
  const valid = (a, b, c) => Number.isFinite(a) && Number.isFinite(b) && a < b && a <= c && c <= b;
11
13
  /** 1/2 when b - a overflows (x, a, b and c are all halved then; the probabilities do not change), else 1 */
12
14
  const halfIfOverflow = (a, b) => (Number.isFinite(b - a) ? 1 : 0.5);
@@ -28,21 +30,43 @@ export function dtriang1(x, a, b, c, log) {
28
30
  if (r < 0.5)
29
31
  return log ? Math.LN2 - Math.log(w) + Math.log1p(-r) : (2 * (1 - r)) / w;
30
32
  const s = x < c ? (x - a) / (c - a) : (b - x) / (b - c);
31
- return log ? Math.LN2 - Math.log(w) + Math.log(s) : (2 * s) / w;
33
+ if (s >= MIN_NORMAL)
34
+ return log ? Math.LN2 - Math.log(w) + Math.log(s) : (2 * s) / w;
35
+ // (a subnormal s has lost digits: from the lengths instead; dtriang(3.1e-190, { a: 0, b: 1.24e133, c: 5.58e132,
36
+ // log: true }) was 1% off)
37
+ const l = Math.LN2 - Math.log(w) + (x < c ? Math.log(x - a) - Math.log(c - a) : Math.log(b - x) - Math.log(b - c));
38
+ return log ? l : Math.exp(l);
32
39
  }
33
40
  /**
34
41
  * The tails [lower, upper] at a < q < b (b - a finite), from ratios of lengths that are at most 1, and the log of the
35
- * near tail (the one from the end on q's side of the mode) as a sum of logs, which stays exact where the tail is subnormal
42
+ * near tail (the one from the end on q's side of the mode) as a sum of logs, which stays exact where the tail is
43
+ * subnormal (from the lengths themselves where a ratio is subnormal too: ptriang(3.1e-190, { a: 0, b: 1.24e133, c:
44
+ * 5.58e132, logP: true }) had lost its digits)
36
45
  */
37
46
  function tails(q, a, b, c) {
38
47
  const w = b - a;
48
+ const lnear = (s, t, d, part) => s >= MIN_NORMAL && t >= MIN_NORMAL ? Math.log(s) + Math.log(t) : 2 * Math.log(d) - Math.log(part) - Math.log(w);
39
49
  // the far tail as a sum of positive terms: 1 - (q - a)^2 / (w (c - a)) = (b - c) / w + (c - q)(q - a + c - a) / (w (c - a))
40
50
  if (q <= c) {
41
51
  const [s, t] = [(q - a) / (c - a), (q - a) / w];
42
- return [s * t, (b - c) / w + ((c - q) / (c - a)) * (t + (c - a) / w), Math.log(s) + Math.log(t)];
52
+ return [s * t, (b - c) / w + ((c - q) / (c - a)) * (t + (c - a) / w), lnear(s, t, q - a, c - a)];
43
53
  }
44
54
  const [s, t] = [(b - q) / (b - c), (b - q) / w];
45
- return [(c - a) / w + ((q - c) / (b - c)) * (t + (b - c) / w), s * t, Math.log(s) + Math.log(t)];
55
+ return [(c - a) / w + ((q - c) / (b - c)) * (t + (b - c) / w), s * t, lnear(s, t, b - q, b - c)];
56
+ }
57
+ /**
58
+ * log of the far tail (see tails) where it is subnormal, from the logs of its terms: the log of the subnormal sum had
59
+ * lost digits (ptriang(2.28e-257, { a: 0, b: 1.33e63, c: 0, logP: true }) was 25% off in its slope)
60
+ */
61
+ function logFar(q, a, b, c) {
62
+ const w = b - a;
63
+ const [first, d, part, t, share] = q <= c ? [b - c, c - q, c - a, (q - a) / w, (c - a) / w] : [c - a, q - c, b - c, (b - q) / w, (b - c) / w];
64
+ const l2 = Math.log(d) - Math.log(part) + Math.log(t + share);
65
+ if (!(first > 0))
66
+ return l2;
67
+ const l1 = Math.log(first) - Math.log(w);
68
+ const m = Math.max(l1, l2);
69
+ return m + Math.log(Math.exp(l1 - m) + Math.exp(l2 - m));
46
70
  }
47
71
  export function ptriang1(q, a, b, c, lowerTail, logP) {
48
72
  if (Number.isNaN(q) || !valid(a, b, c))
@@ -60,7 +84,9 @@ export function ptriang1(q, a, b, c, lowerTail, logP) {
60
84
  // the log of a probability above 1/2 from the other tail; a subnormal near tail from its factors
61
85
  if (v > 0.5)
62
86
  return Math.log1p(-(lowerTail ? upper : lower));
63
- return v < 2 ** -1022 && lowerTail === q <= c ? lnear : Math.log(v);
87
+ if (v >= MIN_NORMAL)
88
+ return Math.log(v);
89
+ return lowerTail === q <= c ? lnear : logFar(q * h, a * h, b * h, c * h);
64
90
  }
65
91
  export function qtriang1(p, a, b, c, lowerTail, logP) {
66
92
  if (Number.isNaN(p) || !valid(a, b, c))
@@ -80,23 +106,29 @@ export function qtriang1(p, a, b, c, lowerTail, logP) {
80
106
  // Left of the mode x - a = (c - a) sqrt(t) with t = lower w / (c - a), the share of the left part's probability;
81
107
  // right of it b - x = (b - c) sqrt(t), t = upper w / (b - c). Near the mode (t > 1/2) the distance from it instead,
82
108
  // (1 - sqrt(t)) = (1 - t) / (1 + sqrt(t)) with 1 - t from the other tail (for c = a or c = b that tail itself, so
83
- // no cancellation near the end). The square roots are taken factor by factor so that no product over- or underflows
109
+ // no cancellation near the end). The square roots are taken factor by factor so that no product over- or underflows,
110
+ // and t as the ratio of the probability to the part's share (lower / (c - a) overflowed for a subnormal width, which
111
+ // put x at the mode: qtriang(1.6e-8, { a: 0, b: 1.03e-316, c: 8.7e-317, lowerTail: false }))
84
112
  const cl = (c - a) / w;
85
113
  const cr = (b - c) / w;
114
+ // For a mode at an end the distance from it is w times the tail given (c = a: the lower one), which was 0 where that
115
+ // tail, exp(p), underflows: w exp(p / 2)^2 then, exp(p / 2) being normal (qtriang(-1000, { a: 0, b: 1e308, c: 0,
116
+ // logP: true }) was 0 for 2.5e-127; issue #121)
117
+ const tinyTail = logP && pv < 2 ** -1022;
86
118
  let x;
87
119
  if (cl > 0 && lower <= cl) {
88
- const t = (lower / (c - a)) * w;
120
+ const t = lower / cl;
89
121
  x =
90
122
  t <= 0.5
91
123
  ? a + (lowerTail ? sqrtP : Math.sqrt(lower)) * Math.sqrt(w) * Math.sqrt(c - a)
92
- : c - ((c - a) * ((upper - cr) / cl)) / (1 + Math.sqrt(t));
124
+ : c - (cr === 0 && !lowerTail && tinyTail ? w * sqrtP * sqrtP : (c - a) * ((upper - cr) / cl)) / (1 + Math.sqrt(t));
93
125
  }
94
126
  else {
95
- const t = (upper / (b - c)) * w;
127
+ const t = upper / cr;
96
128
  x =
97
129
  t <= 0.5
98
130
  ? b - (lowerTail ? Math.sqrt(upper) : sqrtP) * Math.sqrt(w) * Math.sqrt(b - c)
99
- : c + ((b - c) * ((lower - cl) / cr)) / (1 + Math.sqrt(t));
131
+ : c + (cl === 0 && lowerTail && tinyTail ? w * sqrtP * sqrtP : (b - c) * ((lower - cl) / cr)) / (1 + Math.sqrt(t));
100
132
  }
101
133
  // Rounding must not take x out of the support
102
134
  return Math.min(Math.max(x, a), b);
@@ -117,7 +149,7 @@ export function qtriang1(p, a, b, c, lowerTail, logP) {
117
149
  */
118
150
  export function dtriang(x, options = {}) {
119
151
  const { a = -1, b = 1, log = false } = options;
120
- const c = options.c ?? (a + b) / 2;
152
+ const c = options.c ?? midpoint(a, b);
121
153
  return vectorize(x, (v) => dtriang1(v, a, b, c, log));
122
154
  }
123
155
  /**
@@ -133,7 +165,7 @@ export function dtriang(x, options = {}) {
133
165
  */
134
166
  export function ptriang(q, options = {}) {
135
167
  const { a = -1, b = 1, lowerTail = true, logP = false } = options;
136
- const c = options.c ?? (a + b) / 2;
168
+ const c = options.c ?? midpoint(a, b);
137
169
  return vectorize(q, (v) => ptriang1(v, a, b, c, lowerTail, logP));
138
170
  }
139
171
  /**
@@ -149,6 +181,6 @@ export function ptriang(q, options = {}) {
149
181
  */
150
182
  export function qtriang(p, options = {}) {
151
183
  const { a = -1, b = 1, lowerTail = true, logP = false } = options;
152
- const c = options.c ?? (a + b) / 2;
184
+ const c = options.c ?? midpoint(a, b);
153
185
  return vectorize(p, (v) => qtriang1(v, a, b, c, lowerTail, logP));
154
186
  }
@@ -475,8 +475,11 @@ function logRangeCdf(w, k) {
475
475
  * (the range exceeds w essentially only when a single pair differs by more than w; the correction is below e^-833)
476
476
  */
477
477
  function logRangeCdfC(w, k) {
478
- if (w > 100)
479
- return Math.log(k * (k - 1)) + logPhiC(w / Math.SQRT2);
478
+ if (w > 100) {
479
+ // the number of pairs overflows above k ≈ 1.3e154: the sum of the logs only then (the same bits below)
480
+ const pairs = k * (k - 1);
481
+ return (Number.isFinite(pairs) ? Math.log(pairs) : Math.log(k) + Math.log(k - 1)) + logPhiC(w / Math.SQRT2);
482
+ }
480
483
  const lf = (z) => {
481
484
  const lP = logPhi(z);
482
485
  const lt = logPhiShift(z, w) - lP; // log(a / P)
@@ -690,6 +693,11 @@ function logRangeMaxCdf(w, k, r, upper) {
690
693
  const lWc = Wc > 1e-290 ? Math.log(Wc) : logRangeCdfC(w, k);
691
694
  if (r === 1)
692
695
  return lWc;
696
+ // 1 - (1 - Wc)^r = r Wc (1 - (r - 1) Wc/2 + ...): log r + log Wc where Wc is below e^-700 (the form below was
697
+ // -Infinity once Wc underflowed, from q = 54.5 at k = 3, r = 2, and the outer integral over that edge was bisected to
698
+ // its limit at seconds per call: qtukey(1.5e-287, 3, 4.7e10, { nranges: 2, lowerTail: false }))
699
+ if (lWc < -700)
700
+ return Math.log(r) + lWc;
693
701
  // 1 - (1 - Wc)^r
694
702
  return Math.log(-Math.expm1(r * Math.log1p(-Math.exp(lWc))));
695
703
  }
@@ -806,6 +814,15 @@ function validParams(k, df, r) {
806
814
  * Calls with the same (q, nmeans, df, nranges) differing only in tail or log use the same integral, so the last result is cached.
807
815
  */
808
816
  function ptukeyRaw(q, k, df, r, lowerTail, logP) {
817
+ if (q < TINY_Q) {
818
+ // The lower tail is c q^((k-1) r) (1 + O(q²)) there (W(w) is of the order of w^(k-1) near 0): scaled in logs from
819
+ // q = 2^-1000. The integrals at a subnormal q took 20 to 50 ms a call (the subdivision chasing the rounding of the
820
+ // subnormal arguments), so that qtukey(-726.9, 2, 486.9, { logP: true }), whose answer is 3.5e-316, took 1.2 s
821
+ const lv = ptukeyRaw(TINY_Q, k, df, r, true, true) + (k - 1) * r * Math.log(q * 2 ** 1000);
822
+ if (lowerTail)
823
+ return logP ? lv : Math.exp(lv);
824
+ return logP ? -Math.exp(lv) : -Math.expm1(lv);
825
+ }
809
826
  let t;
810
827
  if (memo !== undefined && memo.q === q && memo.k === k && memo.df === df && memo.r === r)
811
828
  t = memo.tail;
@@ -818,6 +835,8 @@ function ptukeyRaw(q, k, df, r, lowerTail, logP) {
818
835
  return logP ? Math.log1p(-t.value) : 1 - t.value;
819
836
  }
820
837
  let memo;
838
+ /** Below this (2^-1000) the lower tail of ptukeyRaw is scaled from its value here */
839
+ const TINY_Q = 2 ** -1000;
821
840
  /**
822
841
  * The smaller tail. Which tail is smaller is guessed from the df = ∞ value (one inner integral); if the guess is
823
842
  * wrong and the value exceeds 1/2, the other side is computed. preferUpper is the side to compute when there is no
@@ -51,6 +51,8 @@ export function qunif1(p, min, max, lowerTail, logP) {
51
51
  // (1 - p would lose them); otherwise min + (1 - p)(max - min) with 1 - p exact (or -expm1(log p)), as R gives
52
52
  const half = halfIfOverflow(min, max) !== 1;
53
53
  const at = (q) => (half ? (1 - q) * min + q * max : min + q * (max - min));
54
+ // (exp(p) is rounded on the subnormal grid for a log p below -708, which the range can magnify back into the normal
55
+ // range; kept so, as R does, and as punif takes the log of the subnormal ratio (test/helpers/distributionKnown.ts))
54
56
  const pp = logP ? Math.exp(p) : p;
55
57
  if (lowerTail)
56
58
  return at(pp);
@@ -7,13 +7,14 @@
7
7
  // R's output (where Inf and NaN appear).
8
8
  import { vectorize } from "../internal/vectorize.js";
9
9
  import { d0, dt0, quantileBoundaries } from "../internal/dpq.js";
10
- import { ddExp, expNeg, log1mExp, logOneMinusExpNeg, standardizeDD } from "../internal/ddexp.js";
11
- import { ddLog, ddMulNum, ddSub } from "../special/dd.js";
10
+ import { ddExp, expNeg, expNegOver, log1mExp, logOneMinusExpNeg, standardizeDD } from "../internal/ddexp.js";
11
+ import { ddDivNum, ddLog, ddMulNum, ddSub } from "../special/dd.js";
12
12
  /** 1^y = 1, as with C's pow (JS Math.pow(1, Infinity) is NaN) */
13
13
  function cpow(a, b) {
14
14
  return a === 1 ? 1 : Math.pow(a, b);
15
15
  }
16
16
  const MIN_NORMAL = 2 ** -1022;
17
+ const LOG_MIN_NORMAL = -1022 * Math.LN2;
17
18
  /**
18
19
  * log u (u = x / scale) in double-double. x > 0, finite parameters. When x / scale is subnormal, 0 or overflows,
19
20
  * log x - log scale instead; also for x above 1e300, where the exact remainder in standardizeDD overflows
@@ -33,6 +34,12 @@ function mulLarge(a, s) {
33
34
  }
34
35
  /** log(a / b) for positive finite a, b, also when a / b over- or underflows */
35
36
  function logRatio(a, b) {
37
+ // a and b within 2^-20 of each other: log1p of their exact difference (log(a / b) took the rounding of the ratio,
38
+ // half an ulp of 1, for its value of the order of the difference: dweibull(2^52 - 0.5, 2^52, { scale: 2^52 - 0.5,
39
+ // log: true }) was an ulp off, as R; issue #111). Elsewhere as R
40
+ const d = a - b;
41
+ if (Math.abs(d) <= b * 2 ** -20)
42
+ return Math.log1p(d / b);
36
43
  const r = a / b;
37
44
  return r >= MIN_NORMAL && r < Infinity ? Math.log(r) : Math.log(a) - Math.log(b);
38
45
  }
@@ -49,6 +56,10 @@ export function dweibull1(x, shape, scale, log) {
49
56
  if (x === 0 && shape < 1)
50
57
  return Infinity;
51
58
  if (!allFinite(shape, scale)) {
59
+ // scale = Inf moves all the probability out to infinity: the density at a finite x > 0 is 0 in the limit (shape
60
+ // (x / scale)^(shape - 1) / scale -> 0), where the formula gives Inf * 0 = NaN (as R does)
61
+ if (scale === Infinity && x > 0)
62
+ return d0(log);
52
63
  // The defining formula as is
53
64
  const t1 = cpow(x / scale, shape - 1);
54
65
  const t2 = t1 * (x / scale);
@@ -64,7 +75,10 @@ export function dweibull1(x, shape, scale, log) {
64
75
  if (t[0] === Infinity)
65
76
  return d0(log);
66
77
  // E = (shape - 1) log u - t
67
- const e = ddSub(shape === 1 ? [0, 0] : mulLarge(lu, shape - 1), t);
78
+ const m = shape === 1 ? [0, 0] : mulLarge(lu, shape - 1);
79
+ // (-Infinity where (shape - 1) log u overflows, which the double-double difference made NaN: dweibull(1.9e-255,
80
+ // 6.6e306, scale 98.6) was NaN)
81
+ const e = m[0] === -Infinity ? m : ddSub(m, t);
68
82
  const r = shape / scale;
69
83
  if (log)
70
84
  return logRatio(shape, scale) + e[0] + e[1];
@@ -111,6 +125,22 @@ export function qweibull1(p, shape, scale, lowerTail, logP) {
111
125
  if (b !== undefined)
112
126
  return b;
113
127
  // -log P[X > x] = t
128
+ // (an infinite shape or scale is left to the forms below, which give R's NaN for Infinity times 0)
129
+ if (logP && lowerTail && p < LOG_MIN_NORMAL && allFinite(shape, scale)) {
130
+ // -log(1 - P) is P = exp(p) to the double, but rounded on the subnormal grid there: x = scale exp(p / shape), with
131
+ // p / shape in double-double (qweibull(-720.6, 1, { scale: 1.36e257, logP: true }) was 5e-15 off)
132
+ // (in double where the shape or the quotient is beyond the range of Veltkamp splitting)
133
+ const q = shape < 1e300 && Math.abs(p / shape) < 1e300 ? ddDivNum([p, 0], shape) : [p / shape, 0];
134
+ const e = ddExp(q);
135
+ const v = e[0] + e[1];
136
+ if (v >= MIN_NORMAL && v < Infinity)
137
+ return scale * v;
138
+ // (exp(p / shape) itself below the normal range: divided by 1 / scale without rounding it there)
139
+ const r = 1 / scale;
140
+ if (r >= MIN_NORMAL && r < Infinity && q[0] < 0)
141
+ return expNegOver([-q[0], -q[1]], r);
142
+ return Math.exp(Math.log(scale) + q[0]);
143
+ }
114
144
  let lu;
115
145
  if (logP)
116
146
  lu = lowerTail ? log1mExp(p) : p;
@@ -8,7 +8,9 @@ export declare function qwilcox1(p: number, m: number, n: number, lowerTail: boo
8
8
  *
9
9
  * Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
10
10
  *
11
- * @throws RangeError if `m` and `n` are so large that the required table cannot be allocated.
11
+ * @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
12
+ * 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
13
+ * center for m = n >= 550 or so; the tails stay available).
12
14
  *
13
15
  * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
14
16
  * @param m - Size of the first sample.
@@ -30,7 +32,9 @@ export declare function dwilcox<T extends Vectorizable>(x: T, m: number, n: numb
30
32
  *
31
33
  * Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
32
34
  *
33
- * @throws RangeError if `m` and `n` are so large that the required table cannot be allocated.
35
+ * @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
36
+ * 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
37
+ * center for m = n >= 550 or so; the tails stay available).
34
38
  *
35
39
  * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
36
40
  * @param m - Size of the first sample.
@@ -52,7 +56,9 @@ export declare function pwilcox<T extends Vectorizable>(q: T, m: number, n: numb
52
56
  *
53
57
  * Unlike R, memory use grows only with the range of the statistic (at most `m * n / 2`), so large samples such as `m = n = 500` work.
54
58
  *
55
- * @throws RangeError if `m` and `n` are so large that the required table cannot be allocated.
59
+ * @throws RangeError if `m` and `n` are so large that the required table cannot be allocated or would take more than
60
+ * 2^29 steps to build (min(m, n) times its length; about 6 to 8 s), or that the table loses its accuracy (near the
61
+ * center for m = n >= 550 or so; the tails stay available).
56
62
  *
57
63
  * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
58
64
  * @param m - Size of the first sample.