@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
@@ -17,8 +17,8 @@ import { dbinomExponent, dbinomRawDD } from "./loader.js";
17
17
  import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, isDoubleDouble, SPLITTER, twoProd, twoSum } from "./dd.js";
18
18
  import { lgamma1p } from "./gamma.js";
19
19
  import { lgammaDiffDD } from "./lgammaDiff.js";
20
- import { HUGE_SHAPE, HUGE_SHAPE_MAX_SMALL, lgammaRatioHuge, prefactorParts, SMALL_SHAPE, smallShapeG, } from "./incbetPrefactor.js";
21
- import { betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
20
+ import { HUGE_SHAPE, HUGE_SHAPE_MAX_SMALL, lgammaRatioHuge, mulBig, prefactorParts, SMALL_SHAPE, smallShapeG, } from "./incbetPrefactor.js";
21
+ import { betaIncOverflowSum, betaIncTemme, TEMME_MIN_M } from "./incbetTemme.js";
22
22
  import { betaIncLargeA, LARGE_A_MIN, SMALL_B_MAX } from "./incbetLargeA.js";
23
23
  const BIG = 4.503599627370496e15;
24
24
  const BIGINV = 2.22044604925031308085e-16;
@@ -58,6 +58,11 @@ function smallPrefactorTimes(a, b, x, xc, m) {
58
58
  const c = m * ms;
59
59
  if (!(e[0] > -1400) || !Number.isFinite(c) || c === 0)
60
60
  return undefined;
61
+ // (exp(e) directly above -700, where the prefactor is below the normal range through c alone, a shape below 1e-305:
62
+ // e + 700 would round e to the ulp of 700, 1e-13, and pbeta(1 - 6e-14, 0.97, 4.7e-307, { lowerTail: false }) was
63
+ // 1 - 5.5e-14, not 1)
64
+ if (e[0] > -700)
65
+ return Math.exp(e[0]) * (1 + e[1]) * c || undefined;
61
66
  const v = Math.exp(e[0] + 700) * (1 + e[1]) * c * Math.exp(-700);
62
67
  return v > 0 ? v : undefined;
63
68
  }
@@ -631,6 +636,23 @@ function continuedFractionCoreOps(a, b, xd, xcd, second) {
631
636
  const huge = Math.max(a, b) >= 2 ** 52 || stepsInexact(a);
632
637
  // Above 1e150 the products of two of the parameters (h3 h4 = a (a + 1), ...) overflow: divide before multiplying
633
638
  const vast = Math.max(a, b) > 1e150;
639
+ // The splitting of an operand, or of a quotient, above about 1.3e300 overflows in ddDiv and ddMul: where one would be
640
+ // that large, the operands are scaled by 2^±64 first (exactly; the other operand is then small, or the result
641
+ // overflows anyway), and a quotient back. pbeta(1.5e-236, 9220.5, 2.6e301) and pbeta(1e-307, 55.1, 6.3e307) were NaN
642
+ const div = (p, q) => {
643
+ if (q[0] > 1e299)
644
+ return ddDiv(scaled(p, 2 ** -64), scaled(q, 2 ** -64));
645
+ if (Math.abs(p[0]) > 1e299 * q[0])
646
+ return scaled(ddDiv(scaled(p, 2 ** -64), q), 2 ** 64);
647
+ return ddDiv(p, q);
648
+ };
649
+ const mul = (p, q) => {
650
+ if (Math.abs(q[0]) > 1e299)
651
+ return ddMul(scaled(p, 2 ** 64), scaled(q, 2 ** -64));
652
+ if (Math.abs(p[0]) > 1e299)
653
+ return ddMul(scaled(p, 2 ** -64), scaled(q, 2 ** 64));
654
+ return ddMul(p, q);
655
+ };
634
656
  // Partial numerators k1 x k2 / (k3 k4) and k5 x k6 / (k7 k8). k1, k3, k4, k7, k8 are doubles, with
635
657
  // double-double copies (h1, ...) that are used and advanced only when huge
636
658
  let k1 = a;
@@ -654,7 +676,7 @@ function continuedFractionCoreOps(a, b, xd, xcd, second) {
654
676
  let ans = [1, 0];
655
677
  for (let n = 0; n < 100000; n++) {
656
678
  let xk = vast
657
- ? ddMul(ddMul(z, ddDiv(h1, h3)), ddDiv(k2, h4))
679
+ ? mul(mul(z, div(h1, h3)), div(k2, h4))
658
680
  : huge
659
681
  ? ddDiv(ddMul(ddMul(z, h1), k2), ddMul(h3, h4))
660
682
  : ddDiv(ddMul(ddMulNum(z, k1), k2), twoProd(k3, k4));
@@ -665,7 +687,7 @@ function continuedFractionCoreOps(a, b, xd, xcd, second) {
665
687
  qkm2 = qkm1;
666
688
  qkm1 = qk;
667
689
  xk = vast
668
- ? ddMul(ddMulNum(z, k5), ddDiv(ddDiv(k6, h7), h8))
690
+ ? mul(mul(z, [k5, 0]), div(div(k6, h7), h8))
669
691
  : ddDiv(ddMul(ddMulNum(z, k5), k6), huge ? ddMul(h7, h8) : twoProd(k7, k8));
670
692
  pk = ddAdd(pkm1, ddMul(pkm2, xk));
671
693
  qk = ddAdd(qkm1, ddMul(qkm2, xk));
@@ -713,6 +735,10 @@ function continuedFractionCoreOps(a, b, xd, xcd, second) {
713
735
  }
714
736
  return ans[0] + ans[1];
715
737
  }
738
+ /** p s for a power of two s */
739
+ function scaled(p, s) {
740
+ return [p[0] * s, p[1] * s];
741
+ }
716
742
  /** Whether the series can be used in frame (a, b, x) */
717
743
  function pseriesOk(a, b, x) {
718
744
  return b * x <= 1 && x <= 0.95;
@@ -723,9 +749,9 @@ function pseriesOk(a, b, x) {
723
749
  * { mu: 195.6 }) has 1 - x = 6.3e-286 against b/(a + b) = 8.5e-88, and the continued fraction above the mean was NaN)
724
750
  */
725
751
  function cfOk(a, b, x, xc) {
752
+ // (a/(a + b) as 1/(1 + b/a) and b/(a + b) as 1/(1 + a/b) where a + b overflows: the ratio was 0, below every x; #491)
726
753
  if (x[0] <= 0.5)
727
- return x[0] <= a / (a + b);
728
- // (b/(a + b) as 1/(1 + a/b) where a + b overflows)
754
+ return x[0] <= (a + b < Infinity ? a / (a + b) : 1 / (1 + b / a));
729
755
  return xc[0] >= (a + b < Infinity ? b / (a + b) : 1 / (1 + a / b));
730
756
  }
731
757
  /** Compute I_x(a, b) by continued fraction */
@@ -810,17 +836,24 @@ export function betaIncTinyX(a, b, x, lx, lower, wantLog) {
810
836
  let r = 1;
811
837
  let lr = 0;
812
838
  let e;
839
+ // a log x by mulBig (the splitting of a above 1e300 in ddMulNum overflowed: pbeta(1.5e-323, 1.4e304, 0.5) was NaN).
840
+ // Where it overflows to -Infinity, the terms of the order of a log a below are Infinity too, and the sum was NaN
841
+ // (pbeta(4.2e-310, 4.1e307, 1113))
842
+ const ax = mulBig(lx, a);
843
+ if (ax[0] === -Infinity)
844
+ return lower ? [-Infinity, 0] : [0, 1];
813
845
  if (Math.min(a, b) < SMALL_SHAPE) {
814
846
  const q = Math.min(a, b) / Math.max(a, b);
815
847
  [r, lr] = a <= b ? [1 / (1 + q), -Math.log1p(q)] : [q / (1 + q), Math.log(q) - Math.log1p(q)];
816
- e = ddAdd(ddAdd(ddMulNum(lx, a), smallShapeG(a, b)), [c, 0]);
848
+ e = ddAdd(ddAdd(ax, smallShapeG(a, b)), [c, 0]);
817
849
  }
818
850
  else {
819
851
  const lg = b >= HUGE_SHAPE && a <= HUGE_SHAPE_MAX_SMALL ? lgammaRatioHuge(a, b) : lgammaDiffDD(a, b);
820
- e = ddAdd(ddAdd(ddMulNum(lx, a), lg), [c - lgamma1p(a), 0]);
852
+ e = ddAdd(ddAdd(ax, lg), [c - lgamma1p(a), 0]);
821
853
  }
822
854
  const lt = lr + e[0] + e[1];
823
- const v = r * (Math.exp(e[0]) * (1 + e[1]));
855
+ // (0 below the subnormal range, where the low part of e can be far beyond 1 and made it -0)
856
+ const v = e[0] < -746 ? 0 : r * (Math.exp(e[0]) * (1 + e[1]));
824
857
  if (lower)
825
858
  return [lt, v];
826
859
  // The other tail: -expm1(lt), and its log from v where v is small (lt rounded to a double is off by up to ε |lt|,
@@ -836,14 +869,63 @@ export function betaIncTinyX(a, b, x, lx, lower, wantLog) {
836
869
  * pbeta(0.615, 2, 2.7e-158, { lowerTail: false }) was 1.0000000000000002), so it is capped at 1 (log at 0)
837
870
  */
838
871
  function betaIncCore(a, b, xd, xcd, lower, wantLog) {
839
- const r = betaIncUncapped(a, b, xd, xcd, lower, wantLog);
872
+ let r = betaIncUncapped(a, b, xd, xcd, lower, wantLog);
873
+ if (Number.isNaN(r[1]) || (wantLog && Number.isNaN(r[0])))
874
+ r = betaIncOverflowSum(a, b, xd, xcd, lower, wantLog) ?? r;
840
875
  return r[1] > 1 || r[0] > 0 ? [Math.min(r[0], 0), Math.min(r[1], 1)] : r;
841
876
  }
877
+ /**
878
+ * Below this shape s, the tail of the order of s (1 - I_x(s, b), or I_x(a, s)) is taken as (s / SUB_SHAPE) times its
879
+ * value at SUB_SHAPE, where the other shape is above SUB_OTHER: 1 - I_x(s, b) = s g(x, b) (1 + O(s / min(1, b)) times
880
+ * logs of x and b), so the relative error of the scaling is below 1e-19. The tail is subnormal for s below 1e-308 or
881
+ * so, and its log, taken of the subnormal value, had lost digits (dbeta(0.97, 0.13, 8.15e-322) did not match the
882
+ * differences of pbeta(x, 0.13, 8.15e-322, { logP: true }))
883
+ */
884
+ const SUB_SHAPE = 1e-300;
885
+ const SUB_OTHER = 1e-280;
886
+ /** [log, linear] of the tail t at SUB_SHAPE scaled to the shape s (small), or of the other tail (!small) */
887
+ function subShapeTail(t, s, small) {
888
+ const r = s / SUB_SHAPE;
889
+ const v = t[1] * r;
890
+ if (small)
891
+ return [t[0] + Math.log(r), v];
892
+ return [Math.log1p(-v), 1 - v];
893
+ }
894
+ /**
895
+ * [log, linear] for both shapes below SUB_SHAPE: I_x(a, b) = b/(a + b) and 1 - I_x(a, b) = a/(a + b) to double precision
896
+ * for every x in (0, 1). From the series, I_x(a, b) = x^a b/(a + b) Γ(a+b+1)/(Γ(a+1) Γ(b+1)) (1 + a S) with abs(S) at most
897
+ * about -log(1 - x) (below 37 for a double x below 1) and abs(a log x) at most 745 a, so the relative error is below
898
+ * 1e-297; the upper tail likewise from I_{1-x}(b, a). The series overflowed in 1/a and the prefactor a b/(a + b) underflowed
899
+ * to 0 there (pbeta(0.5, 5e-324, 5e-324) was NaN, pbeta(0.5, 1e-322, 1e-315) -Infinity). R (toms708) takes the same limit
900
+ * below 1e-3 ε. The shapes are scaled exactly by 2^1000 into the normal range and a + b is kept in double-double (a = b
901
+ * gives 1/2 exactly)
902
+ */
903
+ function bothSubShape(a, b, lower, wantLog) {
904
+ const sa = a * 2 ** 1000;
905
+ const sb = b * 2 ** 1000;
906
+ const sum = twoSum(sa, sb);
907
+ const ratio = (u) => {
908
+ const r = ddDiv([u, 0], sum);
909
+ return r[0] + r[1];
910
+ };
911
+ const v = ratio(lower ? sb : sa);
912
+ if (!wantLog)
913
+ return [NaN, v];
914
+ return [v > 0.5 ? Math.log1p(-ratio(lower ? sa : sb)) : Math.log(v), v];
915
+ }
842
916
  function betaIncUncapped(a, b, xd, xcd, lower, wantLog) {
843
917
  if (xd[0] <= 0)
844
918
  return lower ? [-Infinity, 0] : [0, 1];
845
919
  if (xcd[0] <= 0)
846
920
  return lower ? [0, 1] : [-Infinity, 0];
921
+ if (a < SUB_SHAPE && b < SUB_SHAPE)
922
+ return bothSubShape(a, b, lower, wantLog);
923
+ // The tail of the order of a shape below SUB_SHAPE (1 - I_x(a, b) for a tiny a, I_x(a, b) for a tiny b), from
924
+ // that at SUB_SHAPE scaled (see there), and the other tail as 1 minus it
925
+ if (a < SUB_SHAPE && b > SUB_OTHER)
926
+ return subShapeTail(betaIncUncapped(SUB_SHAPE, b, xd, xcd, false, wantLog), a, !lower);
927
+ if (b < SUB_SHAPE && a > SUB_OTHER)
928
+ return subShapeTail(betaIncUncapped(a, SUB_SHAPE, xd, xcd, true, wantLog), b, lower);
847
929
  // x (or 1 - x) is subnormal: the series with its prefactor in logs (betaIncTinyX). The binomial-probability helpers
848
930
  // break down on products of subnormals
849
931
  if (xd[0] < DBL_MIN)
@@ -918,6 +1000,14 @@ function betaIncUncapped(a, b, xd, xcd, lower, wantLog) {
918
1000
  const o = pseries(oa, ob, ox, oxc, false);
919
1001
  return [wantLog ? Math.log1p(-o) : NaN, 1 - o];
920
1002
  }
1003
+ // Likewise from the complement of the series where it passes the cancellation check (see below), as the log already
1004
+ // is: for a tiny shape the series value is 1 - O(shape) and its rounding of a few ulps was all of its distance from 1
1005
+ // (pbeta(1 - 6e-14, 0.97, 4.7e-307, { lowerTail: false }) was 1 - 1.1e-16 beside a lower tail of 1.4e-305)
1006
+ if (v > 0.5) {
1007
+ const o = pseriesComplement(pa, pb, px, false);
1008
+ if (pa * Math.abs(Math.log(px[0])) <= 32 * o)
1009
+ return [wantLog ? Math.log1p(-o) : NaN, 1 - o];
1010
+ }
921
1011
  return finish(v, otherDirect);
922
1012
  }
923
1013
  if (pseriesOk(oa, ob, ox[0])) {
@@ -126,7 +126,9 @@ export function betaIncLargeA(a, b, xd, yd, lower, wantLog) {
126
126
  // with 100/u steps, did not end)
127
127
  const lx = y < 1e-15 ? ddNeg(ddAdd(yd, [(y * y) / 2, 0])) : ddLog(xd);
128
128
  const ld = [-lx[0], -lx[1]];
129
- const ud = ddMul(td, ld);
129
+ // (td scaled by 2^-64 and ld by 2^64 above 1e299, where the splitting of td overflowed: pbeta(1e-305, 55.1, 6.3e307)
130
+ // was NaN)
131
+ const ud = td[0] > 1e299 ? ddMul([td[0] * 2 ** -64, td[1] * 2 ** -64], [ld[0] * 2 ** 64, ld[1] * 2 ** 64]) : ddMul(td, ld);
130
132
  const u = ud[0] + ud[1];
131
133
  if (!(u > 0 && u < Infinity))
132
134
  return undefined;
@@ -153,7 +155,10 @@ export function betaIncLargeA(a, b, xd, yd, lower, wantLog) {
153
155
  }
154
156
  else {
155
157
  const { e } = dbinomExponent(a, b, xd, yd);
156
- const md = ddMul(ddDiv(twoProd(a, b), twoSum(a, b)), INV_2PI);
158
+ // (with a and a + b scaled by 2^-64 above 1e299, where the splitting of a in the products would overflow)
159
+ const as = a > 1e299 ? a * 2 ** -64 : a;
160
+ const bs = a > 1e299 ? b * 2 ** -64 : b;
161
+ const md = ddMul(ddDiv(twoProd(as, b), twoSum(as, bs)), INV_2PI);
157
162
  const sh = Math.sqrt(md[0]);
158
163
  const sq = twoProd(sh, sh);
159
164
  [eHi, eLo, fac] = [-e[0], -e[1], sh];
@@ -50,3 +50,8 @@ export declare function lgammaRatioHuge(s: number, l: number): DD;
50
50
  * from lgammaRatioHuge (lgammaDiff loses s where s/l underflows)
51
51
  */
52
52
  export declare function smallShapeG(a: number, b: number): DD;
53
+ /**
54
+ * x a in double-double, also for a above 1e290 (where the splitting of a in ddMulNum would overflow); [±Infinity, 0]
55
+ * where the product overflows (the scaling back made it NaN)
56
+ */
57
+ export declare function mulBig(x: DD, a: number): DD;
@@ -54,7 +54,13 @@ export function prefactorParts(a, b, x, xc) {
54
54
  }
55
55
  const lx = x[0] <= 0.5 ? ddLog(x) : log1m(xc);
56
56
  const lxc = xc[0] <= 0.5 ? ddLog(xc) : log1m(x);
57
- return [c, ddAdd(ddAdd(mulBig(lx, a), mulBig(lxc, b)), g)];
57
+ const ta = mulBig(lx, a);
58
+ const tb = mulBig(lxc, b);
59
+ // (both terms are at most 0; where one overflows the exponent is -Infinity, which the double-double sums made NaN:
60
+ // pbeta(0.998, 55.1, 6.3e307) was NaN)
61
+ if (!Number.isFinite(ta[0]) || !Number.isFinite(tb[0]))
62
+ return [c, [ta[0] + tb[0], 0]];
63
+ return [c, ddAdd(ddAdd(ta, tb), g)];
58
64
  }
59
65
  /**
60
66
  * log Γ(l + s) - log Γ(l) in double-double for l >= HUGE_SHAPE and s <= HUGE_SHAPE_MAX_SMALL. From Stirling's series,
@@ -86,7 +92,12 @@ export function smallShapeG(a, b) {
86
92
  function log1m(y) {
87
93
  return y[0] < 1e-15 ? ddNeg(ddAdd(y, [(y[0] * y[0]) / 2, 0])) : ddLog(ddSub([1, 0], y));
88
94
  }
89
- /** x a in double-double, also for a above 1e290 (where the splitting of a in ddMulNum would overflow) */
90
- function mulBig(x, a) {
95
+ /**
96
+ * x a in double-double, also for a above 1e290 (where the splitting of a in ddMulNum would overflow); [±Infinity, 0]
97
+ * where the product overflows (the scaling back made it NaN)
98
+ */
99
+ export function mulBig(x, a) {
100
+ if (!Number.isFinite(x[0] * a))
101
+ return [x[0] * a, 0];
91
102
  return a > 1e290 ? ddMulNum(ddMulNum(x, a / 2 ** 64), 2 ** 64) : ddMulNum(x, a);
92
103
  }
@@ -17,3 +17,10 @@ export declare function halfErfc(y: number): number;
17
17
  * used (m >= TEMME_MIN_M and |τ| <= TAU_MAX); the caller then uses the series or continued fractions
18
18
  */
19
19
  export declare function betaIncTemme(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;
20
+ /**
21
+ * Fallback where a + b overflows and the other methods gave NaN (the continued fraction and the prefactor form
22
+ * a + b; pbeta(0.1, 1e308, 1e308) was NaN; #491). The smaller tail is e^-E times a factor whose log is of the order of
23
+ * the logs of the shapes (below about 2000 in size), so where E >= 1e22 its log is -E to double precision and its
24
+ * linear value 0. E in the halved shapes, doubled, as in betaIncTemme. Undefined where a + b is finite or E < 1e22
25
+ */
26
+ export declare function betaIncOverflowSum(a: number, b: number, xd: DD, xcd: DD, lower: boolean, wantLog: boolean): [number, number] | undefined;
@@ -169,26 +169,33 @@ function mulLarge(r, x) {
169
169
  * used (m >= TEMME_MIN_M and |τ| <= TAU_MAX); the caller then uses the series or continued fractions
170
170
  */
171
171
  export function betaIncTemme(a, b, xd, xcd, lower, wantLog) {
172
- const rr = twoSum(a, b);
172
+ // Where a + b overflows, r x - a, the deviance E and m are formed from the halved shapes and doubled: they are
173
+ // homogeneous of degree 1 in (a, b), while p, q and τ do not change (pbeta(0.5, 1e308, 1e308) was NaN)
174
+ const c = Number.isFinite(a + b) ? 1 : 2;
175
+ const ah = a / c;
176
+ const bh = b / c;
177
+ const rr = twoSum(ah, bh);
173
178
  const r = rr[0];
174
179
  // (a b overflows beyond 1.8e308: then a (b / r))
175
- const ab = a * b;
176
- const m = Number.isFinite(ab) ? ab / r : a * (b / r);
180
+ const ab = ah * bh;
181
+ const mh = Number.isFinite(ab) ? ab / r : ah * (bh / r);
182
+ const m = c * mh;
177
183
  if (!(m >= TEMME_MIN_M) || !Number.isFinite(r))
178
184
  return undefined;
179
- const p = a / r;
180
- const q = b / r;
181
- const pq = m / r;
185
+ const p = ah / r;
186
+ const q = bh / r;
187
+ const pq = mh / r;
182
188
  // Cheap filter: |τ| <= 1 needs |w| = |x - p| / pq below about 1.6
183
189
  const x = xd[0];
184
190
  if (Math.abs(x - p) > 2 * pq)
185
191
  return undefined;
186
192
  // r x - a exactly enough (sign of x - p, and g_0 in the tail form)
187
193
  const rx = mulLarge(rr, xd);
188
- const dx = ddSub(rx, [a, 0]);
189
- const d = dx[0] + dx[1];
194
+ const dx = ddSub(rx, [ah, 0]);
195
+ const d = c * (dx[0] + dx[1]);
190
196
  // E = bd0(a, r x) + bd0(b, r (1 - x)) in double-double
191
- const e = ddAdd(bd0dd(a, rx), bd0dd(b, mulLarge(rr, xcd)));
197
+ const eh = ddAdd(bd0dd(ah, rx), bd0dd(bh, mulLarge(rr, xcd)));
198
+ const e = [c * eh[0], c * eh[1]];
192
199
  const E = e[0] + e[1];
193
200
  const tau2 = (2 * E) / m;
194
201
  if (!(tau2 <= TAU_MAX * TAU_MAX))
@@ -197,7 +204,8 @@ export function betaIncTemme(a, b, xd, xcd, lower, wantLog) {
197
204
  const tau = d < 0 ? -absTau : absTau;
198
205
  const n = absTau <= 0.5 ? 20 : absTau <= 0.75 ? 24 : NMAX;
199
206
  gCoefficients(p, q, n);
200
- const eD = Math.exp(-(stirlerr(a) + stirlerr(b) - stirlerr(r)));
207
+ // (where a + b overflows, its stirlerr, about 1 / (12 (a + b)), is below 1e-309)
208
+ const eD = Math.exp(-(stirlerr(a) + stirlerr(b) - (c === 1 ? stirlerr(r) : 0)));
201
209
  const y = Math.sqrt(E);
202
210
  // The smaller tail is the lower one when x < p
203
211
  const smallIsLower = d < 0;
@@ -220,9 +228,34 @@ export function betaIncTemme(a, b, xd, xcd, lower, wantLog) {
220
228
  let small;
221
229
  if (e[0] < 700)
222
230
  small = Math.exp(-e[0]) * (1 - e[1]) * B;
231
+ // (beyond 1500 the result underflows; 1 - e[1] is then no longer near 1 and could make it -0)
223
232
  else
224
- small = Math.exp(700 - e[0]) * (1 - e[1]) * B * Math.exp(-700);
233
+ small = e[0] < 1500 ? Math.exp(700 - e[0]) * (1 - e[1]) * B * Math.exp(-700) : 0;
225
234
  if (lower === smallIsLower)
226
235
  return [wantLog ? -e[0] - e[1] + Math.log(B) : NaN, small];
227
- return [wantLog ? Math.log1p(-small) : NaN, 1 - small];
236
+ // (log1p(-0) would be -0)
237
+ return [wantLog ? (small === 0 ? 0 : Math.log1p(-small)) : NaN, 1 - small];
238
+ }
239
+ /**
240
+ * Fallback where a + b overflows and the other methods gave NaN (the continued fraction and the prefactor form
241
+ * a + b; pbeta(0.1, 1e308, 1e308) was NaN; #491). The smaller tail is e^-E times a factor whose log is of the order of
242
+ * the logs of the shapes (below about 2000 in size), so where E >= 1e22 its log is -E to double precision and its
243
+ * linear value 0. E in the halved shapes, doubled, as in betaIncTemme. Undefined where a + b is finite or E < 1e22
244
+ */
245
+ export function betaIncOverflowSum(a, b, xd, xcd, lower, wantLog) {
246
+ if (Number.isFinite(a + b))
247
+ return undefined;
248
+ const ah = a / 2;
249
+ const bh = b / 2;
250
+ const rr = twoSum(ah, bh);
251
+ const rx = mulLarge(rr, xd);
252
+ const dx = ddSub(rx, [ah, 0]);
253
+ const eh = ddAdd(bd0dd(ah, rx), bd0dd(bh, mulLarge(rr, xcd)));
254
+ const E = 2 * (eh[0] + eh[1]);
255
+ if (!(E >= 1e22))
256
+ return undefined;
257
+ // The smaller tail is the lower one when x < p
258
+ if (lower === dx[0] + dx[1] < 0)
259
+ return [wantLog ? -E : NaN, 0];
260
+ return [wantLog ? 0 : NaN, 1];
228
261
  }
@@ -28,7 +28,7 @@ export declare function dbinomRawSplit(x: number, nmx: number, p: number, q: num
28
28
  * The rounding error of p^x is amplified x times (1e-11 at x = 1e5), so where p is formed as 1 - (smaller side),
29
29
  * pass that difference exactly in double-double.
30
30
  */
31
- export declare function dbinomRawDD(x: number, nmx: number, p: DD, q: DD, log: boolean): number;
31
+ export declare function dbinomRawDD(x: number, nmx: number, p: DD, q: DD, log: boolean, size?: number, shift?: readonly [number, number]): number;
32
32
  /**
33
33
  * Return the exponent of the core binomial probability (0 < x, 0 < n - x, 0 < p, q) in double-double.
34
34
  * Probability = exp(-e) / sqrt(denom). Used when exp should be taken after combining with other factors (beta density).