@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
@@ -1,7 +1,18 @@
1
1
  // The power-series prefix shared by the Bessel functions I and J (besselIK.ts, besselJY.ts).
2
- import { gamma } from "./gamma.js";
2
+ import { gamma, lgamma } from "./gamma.js";
3
3
  import { dpoisRaw } from "./loader.js";
4
4
  const DBL_MIN = 2.2250738585072014e-308;
5
+ const EPS = Number.EPSILON;
6
+ /** Below this x, x / 2 is not exact (Number.MIN_VALUE / 2 is 0) */
7
+ const HALF_EXACT = 2 ** -1021;
8
+ /** log(x/2), also for subnormal x (log(Number.MIN_VALUE / 2) was -Infinity) */
9
+ export function logHalf(x) {
10
+ return x >= HALF_EXACT ? Math.log(x / 2) : Math.log(x) - Math.LN2;
11
+ }
12
+ /** (x/2)^v, also for subnormal x */
13
+ export function powHalf(x, v) {
14
+ return x >= HALF_EXACT ? Math.pow(x / 2, v) : Math.pow(x, v) * Math.pow(2, -v);
15
+ }
5
16
  /**
6
17
  * (x/2)^v / Gamma(v + 1) * sum, the leading factor of the power series of I_v and J_v times the series sum.
7
18
  * For v >= 10 the factor is Loader's Poisson term dpois(v; x/2) e^(x/2), which is accurate where pow / gamma loses
@@ -11,7 +22,7 @@ const DBL_MIN = 2.2250738585072014e-308;
11
22
  */
12
23
  export function seriesTimesPrefix(v, x, sum) {
13
24
  if (v < 10)
14
- return (Math.pow(x / 2, v) / gamma(v + 1)) * sum;
25
+ return (powHalf(x, v) / gamma(v + 1)) * sum;
15
26
  const lambda = x / 2;
16
27
  const d = dpoisRaw(v, lambda, false);
17
28
  if (d >= DBL_MIN && lambda < 700) {
@@ -22,3 +33,35 @@ export function seriesTimesPrefix(v, x, sum) {
22
33
  const ld = dpoisRaw(v, lambda, true) + lambda + Math.log(Math.abs(sum));
23
34
  return Math.sign(sum) * Math.exp(ld);
24
35
  }
36
+ /**
37
+ * Large orders. The recurrences in besselIK.ts and besselJY.ts take about v steps, which does not end for v = 1e12
38
+ * (besselK(1, 1e12)), while the result is far beyond the range of doubles. Above this order, bounds on the log of the
39
+ * result decide such cases first
40
+ */
41
+ export const LARGE_ORDER = 1000;
42
+ const LOG_MAX = Math.log(Number.MAX_VALUE);
43
+ /** Values below Number.MIN_VALUE / 2 round to 0 */
44
+ const LOG_MIN = Math.log(Number.MIN_VALUE) - Math.LN2;
45
+ /**
46
+ * Bounds on log I_v(x) (v >= 0) from its power series (x/2)^v / Gamma(v + 1) sum (x^2/4)^k / (k! (v+1)_k): the terms
47
+ * are positive, and the sum is at most exp(x^2 / (4 (v + 1))). The lower bound is also the upper bound of
48
+ * log abs(J_v(x)) (DLMF 10.14.4)
49
+ */
50
+ export function logISeriesBounds(v, x) {
51
+ const a = v * logHalf(x);
52
+ const g = lgamma(v + 1);
53
+ const t = (x * x) / (4 * (v + 1));
54
+ return { lo: a - g, hi: a - g + t, size: Math.abs(a) + Math.abs(g) + t };
55
+ }
56
+ /**
57
+ * 1 if a value whose log lies in [lo, hi] certainly overflows, -1 if it certainly rounds to 0, 0 otherwise. The bounds
58
+ * must clear the limits by a margin above the rounding of their terms
59
+ */
60
+ export function certainRange(b) {
61
+ const m = 1 + 64 * EPS * b.size;
62
+ if (b.lo - m > LOG_MAX)
63
+ return 1;
64
+ if (b.hi + m < LOG_MIN)
65
+ return -1;
66
+ return 0;
67
+ }
@@ -55,6 +55,9 @@ export function beta(a, b) {
55
55
  let sign = 1;
56
56
  if (a === 0 || b === 0)
57
57
  return Infinity;
58
+ // B(a, b) > 1/a overflows where 1/a does (the product below was NaN at beta(1e-310, 1e-310); R gives Inf)
59
+ if (1 / Math.min(a, b) === Infinity)
60
+ return Infinity;
58
61
  if (Math.abs(a) < Math.abs(b))
59
62
  [a, b] = [b, a];
60
63
  if (Math.abs(a) > ASYMP_FACTOR * Math.abs(b) && a > ASYMP_FACTOR) {
@@ -120,6 +123,10 @@ export function lbeta(a, b) {
120
123
  }
121
124
  if (a === 0 || b === 0)
122
125
  return Infinity;
126
+ // Γ of a shape below 1e-300 overflows (Γ(a) ≈ 1/a), and the product below was Infinity or NaN (lbeta(1e-310, 1e-310) was
127
+ // NaN, R gives 714.49452600871405): the logs instead, -log a each, which cancel by at most a few ulps
128
+ if (Math.min(a, b) < 1e-300)
129
+ return lgamma(a) + lgamma(b) - lgamma(a + b);
123
130
  if (Math.abs(a) < Math.abs(b))
124
131
  [a, b] = [b, a];
125
132
  if (Math.abs(a) > ASYMP_FACTOR * Math.abs(b) && a > ASYMP_FACTOR)
@@ -149,7 +156,10 @@ const HUGE = 2 ** 52;
149
156
  */
150
157
  function lbetaHuge(a, b) {
151
158
  const lead = ddMulNum(ddSub(ddLog([b, 0]), ddLog([a, 0])), b);
152
- const rest = -0.5 * Math.log(b) - (a + b - 0.5) * Math.log1p(b / a) + (stirlerr(a) + stirlerr(b) - stirlerr(a + b));
159
+ const l1p = Math.log1p(b / a);
160
+ // a + b can overflow (lbeta(1e308, 1e308) was -Infinity), while a L and b L stay finite
161
+ const sumTerm = Number.isFinite(a + b) ? (a + b - 0.5) * l1p : a * l1p + (b - 0.5) * l1p;
162
+ const rest = -0.5 * Math.log(b) - sumTerm + (stirlerr(a) + stirlerr(b) - stirlerr(a + b));
153
163
  return ddAdd(ddAdd(lead, LS2PI_DD), [rest, 0]);
154
164
  }
155
165
  /**
@@ -10,6 +10,7 @@
10
10
  // log B is taken from lbetaDD (double-double), since its value can be in the hundreds and exp would turn the
11
11
  // rounding of a double log into a relative error of 1e-13.
12
12
  import { nonInt, roundHalfEven } from "../internal/integer.js";
13
+ import { ldexp } from "../internal/summary.js";
13
14
  import { lbeta, lbetaDD } from "./beta.js";
14
15
  import { ddAdd, ddDiv, ddLog, ddMul, twoSum } from "./dd.js";
15
16
  import { gamma, lgamma, lgamma1p, sinpi } from "./gamma.js";
@@ -40,6 +41,16 @@ function fallingRatio(n, k) {
40
41
  return r;
41
42
  }
42
43
  /** log B(a, b) (a, b > 0): double-double where lbetaDD is accurate, else lbeta (whose value is then large) */
44
+ /** n (n - 1) ... (n - k + 1) / k! where the product overflows on the way, in the coordinates 2^-s with 2^s near abs(n) */
45
+ function fallingRatioScaled(n, k) {
46
+ const s = Math.floor(Math.log2(Math.abs(n)));
47
+ let r = [1, 0];
48
+ for (let j = 0; j < k; j++) {
49
+ const t = twoSum(n, -j);
50
+ r = ddDiv(ddMul(r, [ldexp(t[0], -s), ldexp(t[1], -s)]), [j + 1, 0]);
51
+ }
52
+ return ldexp(r[0] + r[1], s * k);
53
+ }
43
54
  function logBeta(a, b) {
44
55
  return Math.min(a, b) >= 1 && Math.max(a, b) < 2 ** 52 ? lbetaDD(a, b) : [lbeta(a, b), 0];
45
56
  }
@@ -123,11 +134,9 @@ export function choose(n, k, options) {
123
134
  const v = r[0] + r[1];
124
135
  if (Number.isFinite(v))
125
136
  return isInt(n) || snap ? Math.round(v) : v;
126
- // Overflow (the double-double steps then give NaN): the plain product overflows too
127
- let p = 1;
128
- for (let j = 0; j < k; j++)
129
- p = (p * (n - j)) / (j + 1);
130
- return p;
137
+ // The product overflows on the way (n (n - 1) at n = 1.5e154 while choose(n, 2) = 1.125e308; the double-double steps
138
+ // then give NaN): the factors n - j divided by 2^s (exact), and the result scaled back by 2^(s k)
139
+ return fallingRatioScaled(n, k);
131
140
  }
132
141
  if (rc) {
133
142
  n = rSnap(n, k);
@@ -186,9 +195,9 @@ export function lchoose(n, k, options) {
186
195
  const l = ddLog(r[0] < 0 ? [-r[0], -r[1]] : r);
187
196
  return l[0] + l[1];
188
197
  }
189
- if (v === 0)
190
- return -Infinity;
191
- // Out of range: sum of logs (the result is large in magnitude, so its rounding is relatively small)
198
+ // Out of range: sum of logs (the result is large in magnitude, so its rounding is relatively small). v = 0 also comes
199
+ // here: a coefficient below the doubles has a finite log (lchoose(MIN, 2) was -Infinity for -745.13; issue #107),
200
+ // and an exact zero has a factor n - j = 0, whose log makes the sum -Infinity
192
201
  let s = -lgamma(k + 1);
193
202
  for (let j = 0; j < k; j++)
194
203
  s += Math.log(Math.abs(n - j));
@@ -21,22 +21,66 @@ export function twoSum(a, b) {
21
21
  if (!ddEnabled)
22
22
  return [a + b, 0];
23
23
  const s = a + b;
24
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
25
+ if (!Number.isFinite(s))
26
+ return [s, 0];
24
27
  const bb = s - a;
25
28
  const e = a - (s - bb) + (b - bb);
26
29
  return [s, e];
27
30
  }
31
+ /** x 2^k, in two steps so that 2^k itself cannot overflow or underflow */
32
+ function scale2(x, k) {
33
+ const h = Math.trunc(k / 2);
34
+ return x * 2 ** h * 2 ** (k - h);
35
+ }
36
+ /**
37
+ * The power of 2 that brings a divisor below 1 to [1, 2) when the numerator is below 2^-900, 0 otherwise (or when the
38
+ * numerator would overflow). The remainders of the division, about |a| eps and |a| eps^2, fell below the subnormals
39
+ * there and the low part of the quotient was lost (ddDiv([MIN], [3 MIN]) gave [1/3, 0], ddDiv([1e-300], [3e-300]) a
40
+ * low part of 8.4e-20 for 1.85e-17; issue #105); the scaling by 2^k is exact and leaves the quotient unchanged
41
+ */
42
+ function tinyDivisionShift(a, b) {
43
+ if (!(Math.abs(a) < 2 ** -900) || !(Math.abs(b) < 1) || b === 0)
44
+ return 0;
45
+ const k = -Math.floor(Math.log2(Math.abs(b)));
46
+ return Number.isFinite(scale2(a, k)) ? k : 0;
47
+ }
48
+ /**
49
+ * The rounding error a * b - fl(a * b) for operands so large that Veltkamp splitting overflows: each operand is
50
+ * scaled near 1 by a power of 2 (exact), and the error of the scaled product scaled back
51
+ */
52
+ function prodErrScaled(a, b) {
53
+ const ka = a === 0 ? 0 : Math.floor(Math.log2(Math.abs(a)));
54
+ const kb = b === 0 ? 0 : Math.floor(Math.log2(Math.abs(b)));
55
+ const x = scale2(a, -ka);
56
+ const y = scale2(b, -kb);
57
+ const p = x * y;
58
+ let t = SPLITTER * x;
59
+ const xh = t - (t - x);
60
+ const xl = x - xh;
61
+ t = SPLITTER * y;
62
+ const yh = t - (t - y);
63
+ const yl = y - yh;
64
+ // scaled back by 2^(ka + kb) at once: 2^ka first underflowed the error of 1e-320 * 1e308 to 0 before 2^kb (#62)
65
+ return scale2(xh * yh - p + xh * yl + xl * yh + xl * yl, ka + kb);
66
+ }
28
67
  /** [p, e] satisfying a * b = p + e exactly (Veltkamp splitting) */
29
68
  export function twoProd(a, b) {
30
69
  if (!ddEnabled)
31
70
  return [a * b, 0];
32
71
  const p = a * b;
72
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
73
+ if (!Number.isFinite(p))
74
+ return [p, 0];
33
75
  let t = SPLITTER * a;
34
76
  const ah = t - (t - a);
35
77
  const al = a - ah;
36
78
  t = SPLITTER * b;
37
79
  const bh = t - (t - b);
38
80
  const bl = b - bh;
39
- const e = ah * bh - p + ah * bl + al * bh + al * bl;
81
+ let e = ah * bh - p + ah * bl + al * bh + al * bl;
82
+ if (!Number.isFinite(e) && Number.isFinite(p))
83
+ e = prodErrScaled(a, b);
40
84
  return [p, e];
41
85
  }
42
86
  // The helpers below expand the form that calls twoSum, quickTwoSum and twoProd (Dekker / Knuth), keeping the same
@@ -48,6 +92,9 @@ export function ddAdd(a, b) {
48
92
  if (!ddEnabled)
49
93
  return [a[0] + b[0], 0];
50
94
  const s1 = a[0] + b[0];
95
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
96
+ if (!Number.isFinite(s1))
97
+ return [s1, 0];
51
98
  const bb2 = s1 - a[0];
52
99
  const e3 = a[0] - (s1 - bb2) + (b[0] - bb2);
53
100
  const s4 = a[1] + b[1];
@@ -70,6 +117,9 @@ export function ddSub(a, b) {
70
117
  const n1 = -b[0];
71
118
  const n2 = -b[1];
72
119
  const s3 = a[0] + n1;
120
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
121
+ if (!Number.isFinite(s3))
122
+ return [s3, 0];
73
123
  const bb4 = s3 - a[0];
74
124
  const e5 = a[0] - (s3 - bb4) + (n1 - bb4);
75
125
  const s6 = a[1] + n2;
@@ -87,13 +137,18 @@ export function ddMul(a, b) {
87
137
  if (!ddEnabled)
88
138
  return [a[0] * b[0], 0];
89
139
  const p1 = a[0] * b[0];
140
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
141
+ if (!Number.isFinite(p1))
142
+ return [p1, 0];
90
143
  const t2 = SPLITTER * a[0];
91
144
  const ah3 = t2 - (t2 - a[0]);
92
145
  const al4 = a[0] - ah3;
93
146
  const t5 = SPLITTER * b[0];
94
147
  const bh6 = t5 - (t5 - b[0]);
95
148
  const bl7 = b[0] - bh6;
96
- const e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
149
+ let e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
150
+ if (!Number.isFinite(e8) && Number.isFinite(p1))
151
+ e8 = prodErrScaled(a[0], b[0]);
97
152
  const u9 = e8 + (a[0] * b[1] + a[1] * b[0]);
98
153
  const s10 = p1 + u9;
99
154
  const e11 = u9 - (s10 - p1);
@@ -103,13 +158,18 @@ export function ddMulNum(a, b) {
103
158
  if (!ddEnabled)
104
159
  return [a[0] * b, 0];
105
160
  const p1 = a[0] * b;
161
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
162
+ if (!Number.isFinite(p1))
163
+ return [p1, 0];
106
164
  const t2 = SPLITTER * a[0];
107
165
  const ah3 = t2 - (t2 - a[0]);
108
166
  const al4 = a[0] - ah3;
109
167
  const t5 = SPLITTER * b;
110
168
  const bh6 = t5 - (t5 - b);
111
169
  const bl7 = b - bh6;
112
- const e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
170
+ let e8 = ah3 * bh6 - p1 + ah3 * bl7 + al4 * bh6 + al4 * bl7;
171
+ if (!Number.isFinite(e8) && Number.isFinite(p1))
172
+ e8 = prodErrScaled(a[0], b);
113
173
  const u9 = e8 + a[1] * b;
114
174
  const s10 = p1 + u9;
115
175
  const e11 = u9 - (s10 - p1);
@@ -118,7 +178,14 @@ export function ddMulNum(a, b) {
118
178
  export function ddDiv(a, b) {
119
179
  if (!ddEnabled)
120
180
  return [a[0] / b[0], 0];
181
+ // (written by hand, as the prodErrScaled lines: a tiny numerator over a divisor below 1, both times 2^k first)
182
+ const k = tinyDivisionShift(a[0], b[0]);
183
+ if (k !== 0)
184
+ return ddDiv([scale2(a[0], k), scale2(a[1], k)], [scale2(b[0], k), scale2(b[1], k)]);
121
185
  const q1 = a[0] / b[0];
186
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
187
+ if (!Number.isFinite(q1))
188
+ return [q1, 0];
122
189
  const p2 = b[0] * q1;
123
190
  const t3 = SPLITTER * b[0];
124
191
  const ah4 = t3 - (t3 - b[0]);
@@ -126,7 +193,9 @@ export function ddDiv(a, b) {
126
193
  const t6 = SPLITTER * q1;
127
194
  const bh7 = t6 - (t6 - q1);
128
195
  const bl8 = q1 - bh7;
129
- const e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
196
+ let e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
197
+ if (!Number.isFinite(e9) && Number.isFinite(p2))
198
+ e9 = prodErrScaled(b[0], q1);
130
199
  const u10 = e9 + b[1] * q1;
131
200
  const s11 = p2 + u10;
132
201
  const e12 = u10 - (s11 - p2);
@@ -152,7 +221,9 @@ export function ddDiv(a, b) {
152
221
  const t32 = SPLITTER * q27;
153
222
  const bh33 = t32 - (t32 - q27);
154
223
  const bl34 = q27 - bh33;
155
- const e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
224
+ let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
225
+ if (!Number.isFinite(e35) && Number.isFinite(p28))
226
+ e35 = prodErrScaled(b[0], q27);
156
227
  const u36 = e35 + b[1] * q27;
157
228
  const s37 = p28 + u36;
158
229
  const e38 = u36 - (s37 - p28);
@@ -190,7 +261,14 @@ export function ddDiv(a, b) {
190
261
  export function ddDivNum(a, b) {
191
262
  if (!ddEnabled)
192
263
  return [a[0] / b, 0];
264
+ // (by hand, as in ddDiv)
265
+ const k = tinyDivisionShift(a[0], b);
266
+ if (k !== 0)
267
+ return ddDivNum([scale2(a[0], k), scale2(a[1], k)], scale2(b, k));
193
268
  const q1 = a[0] / b;
269
+ // A sum, product or quotient beyond the double range has no error term (the steps below would make it NaN)
270
+ if (!Number.isFinite(q1))
271
+ return [q1, 0];
194
272
  const p2 = b * q1;
195
273
  const t3 = SPLITTER * b;
196
274
  const ah4 = t3 - (t3 - b);
@@ -198,7 +276,9 @@ export function ddDivNum(a, b) {
198
276
  const t6 = SPLITTER * q1;
199
277
  const bh7 = t6 - (t6 - q1);
200
278
  const bl8 = q1 - bh7;
201
- const e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
279
+ let e9 = ah4 * bh7 - p2 + ah4 * bl8 + al5 * bh7 + al5 * bl8;
280
+ if (!Number.isFinite(e9) && Number.isFinite(p2))
281
+ e9 = prodErrScaled(b, q1);
202
282
  const u10 = e9 + 0 * q1;
203
283
  const s11 = p2 + u10;
204
284
  const e12 = u10 - (s11 - p2);
@@ -224,7 +304,9 @@ export function ddDivNum(a, b) {
224
304
  const t32 = SPLITTER * q27;
225
305
  const bh33 = t32 - (t32 - q27);
226
306
  const bl34 = q27 - bh33;
227
- const e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
307
+ let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
308
+ if (!Number.isFinite(e35) && Number.isFinite(p28))
309
+ e35 = prodErrScaled(b, q27);
228
310
  const u36 = e35 + 0 * q27;
229
311
  const s37 = p28 + u36;
230
312
  const e38 = u36 - (s37 - p28);
@@ -450,7 +532,9 @@ export function ddLog(x) {
450
532
  const t32 = SPLITTER * q27;
451
533
  const bh33 = t32 - (t32 - q27);
452
534
  const bl34 = q27 - bh33;
453
- const e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
535
+ let e35 = ah30 * bh33 - p28 + ah30 * bl34 + al31 * bh33 + al31 * bl34;
536
+ if (!Number.isFinite(e35) && Number.isFinite(p28))
537
+ e35 = prodErrScaled(s25, q27);
454
538
  const u36 = e35 + e26 * q27;
455
539
  const s37 = p28 + u36;
456
540
  const e38 = u36 - (s37 - p28);
@@ -476,7 +560,9 @@ export function ddLog(x) {
476
560
  const t58 = SPLITTER * q53;
477
561
  const bh59 = t58 - (t58 - q53);
478
562
  const bl60 = q53 - bh59;
479
- const e61 = ah56 * bh59 - p54 + ah56 * bl60 + al57 * bh59 + al57 * bl60;
563
+ let e61 = ah56 * bh59 - p54 + ah56 * bl60 + al57 * bh59 + al57 * bl60;
564
+ if (!Number.isFinite(e61) && Number.isFinite(p54))
565
+ e61 = prodErrScaled(s25, q53);
480
566
  const u62 = e61 + e26 * q53;
481
567
  const s63 = p54 + u62;
482
568
  const e64 = u62 - (s63 - p54);
@@ -516,7 +602,9 @@ export function ddLog(x) {
516
602
  const t98 = SPLITTER * s92;
517
603
  const bh99 = t98 - (t98 - s92);
518
604
  const bl100 = s92 - bh99;
519
- const e101 = ah96 * bh99 - p94 + ah96 * bl100 + al97 * bh99 + al97 * bl100;
605
+ let e101 = ah96 * bh99 - p94 + ah96 * bl100 + al97 * bh99 + al97 * bl100;
606
+ if (!Number.isFinite(e101) && Number.isFinite(p94))
607
+ e101 = prodErrScaled(s92, s92);
520
608
  const u102 = e101 + (s92 * e93 + e93 * s92);
521
609
  const s103 = p94 + u102;
522
610
  const e104 = u102 - (s103 - p94);
@@ -527,7 +615,9 @@ export function ddLog(x) {
527
615
  const t109 = SPLITTER * s103;
528
616
  const bh110 = t109 - (t109 - s103);
529
617
  const bl111 = s103 - bh110;
530
- const e112 = ah107 * bh110 - p105 + ah107 * bl111 + al108 * bh110 + al108 * bl111;
618
+ let e112 = ah107 * bh110 - p105 + ah107 * bl111 + al108 * bh110 + al108 * bl111;
619
+ if (!Number.isFinite(e112) && Number.isFinite(p105))
620
+ e112 = prodErrScaled(RECIP[5][0], s103);
531
621
  const u113 = e112 + (RECIP[5][0] * e104 + RECIP[5][1] * s103);
532
622
  const s114 = p105 + u113;
533
623
  const e115 = u113 - (s114 - p105);
@@ -550,7 +640,9 @@ export function ddLog(x) {
550
640
  const t132 = SPLITTER * s103;
551
641
  const bh133 = t132 - (t132 - s103);
552
642
  const bl134 = s103 - bh133;
553
- const e135 = ah130 * bh133 - p128 + ah130 * bl134 + al131 * bh133 + al131 * bl134;
643
+ let e135 = ah130 * bh133 - p128 + ah130 * bl134 + al131 * bh133 + al131 * bl134;
644
+ if (!Number.isFinite(e135) && Number.isFinite(p128))
645
+ e135 = prodErrScaled(s126, s103);
554
646
  const u136 = e135 + (s126 * e104 + e127 * s103);
555
647
  const s137 = p128 + u136;
556
648
  const e138 = u136 - (s137 - p128);
@@ -573,7 +665,9 @@ export function ddLog(x) {
573
665
  const t155 = SPLITTER * s103;
574
666
  const bh156 = t155 - (t155 - s103);
575
667
  const bl157 = s103 - bh156;
576
- const e158 = ah153 * bh156 - p151 + ah153 * bl157 + al154 * bh156 + al154 * bl157;
668
+ let e158 = ah153 * bh156 - p151 + ah153 * bl157 + al154 * bh156 + al154 * bl157;
669
+ if (!Number.isFinite(e158) && Number.isFinite(p151))
670
+ e158 = prodErrScaled(s149, s103);
577
671
  const u159 = e158 + (s149 * e104 + e150 * s103);
578
672
  const s160 = p151 + u159;
579
673
  const e161 = u159 - (s160 - p151);
@@ -596,7 +690,9 @@ export function ddLog(x) {
596
690
  const t178 = SPLITTER * s103;
597
691
  const bh179 = t178 - (t178 - s103);
598
692
  const bl180 = s103 - bh179;
599
- const e181 = ah176 * bh179 - p174 + ah176 * bl180 + al177 * bh179 + al177 * bl180;
693
+ let e181 = ah176 * bh179 - p174 + ah176 * bl180 + al177 * bh179 + al177 * bl180;
694
+ if (!Number.isFinite(e181) && Number.isFinite(p174))
695
+ e181 = prodErrScaled(s172, s103);
600
696
  const u182 = e181 + (s172 * e104 + e173 * s103);
601
697
  const s183 = p174 + u182;
602
698
  const e184 = u182 - (s183 - p174);
@@ -619,7 +715,9 @@ export function ddLog(x) {
619
715
  const t201 = SPLITTER * s103;
620
716
  const bh202 = t201 - (t201 - s103);
621
717
  const bl203 = s103 - bh202;
622
- const e204 = ah199 * bh202 - p197 + ah199 * bl203 + al200 * bh202 + al200 * bl203;
718
+ let e204 = ah199 * bh202 - p197 + ah199 * bl203 + al200 * bh202 + al200 * bl203;
719
+ if (!Number.isFinite(e204) && Number.isFinite(p197))
720
+ e204 = prodErrScaled(s195, s103);
623
721
  const u205 = e204 + (s195 * e104 + e196 * s103);
624
722
  const s206 = p197 + u205;
625
723
  const e207 = u205 - (s206 - p197);
@@ -642,7 +740,9 @@ export function ddLog(x) {
642
740
  const t224 = SPLITTER * s218;
643
741
  const bh225 = t224 - (t224 - s218);
644
742
  const bl226 = s218 - bh225;
645
- const e227 = ah222 * bh225 - p220 + ah222 * bl226 + al223 * bh225 + al223 * bl226;
743
+ let e227 = ah222 * bh225 - p220 + ah222 * bl226 + al223 * bh225 + al223 * bl226;
744
+ if (!Number.isFinite(e227) && Number.isFinite(p220))
745
+ e227 = prodErrScaled(s92, s218);
646
746
  const u228 = e227 + (s92 * e219 + e93 * s218);
647
747
  const s229 = p220 + u228;
648
748
  const e230 = u228 - (s229 - p220);
@@ -653,7 +753,9 @@ export function ddLog(x) {
653
753
  const t235 = SPLITTER * 2;
654
754
  const bh236 = t235 - (t235 - 2);
655
755
  const bl237 = 2 - bh236;
656
- const e238 = ah233 * bh236 - p231 + ah233 * bl237 + al234 * bh236 + al234 * bl237;
756
+ let e238 = ah233 * bh236 - p231 + ah233 * bl237 + al234 * bh236 + al234 * bl237;
757
+ if (!Number.isFinite(e238) && Number.isFinite(p231))
758
+ e238 = prodErrScaled(s229, 2);
657
759
  const u239 = e238 + e230 * 2;
658
760
  const s240 = p231 + u239;
659
761
  const e241 = u239 - (s240 - p231);
@@ -676,7 +778,9 @@ export function ddLog(x) {
676
778
  const t258 = SPLITTER * k;
677
779
  const bh259 = t258 - (t258 - k);
678
780
  const bl260 = k - bh259;
679
- const e261 = ah256 * bh259 - p254 + ah256 * bl260 + al257 * bh259 + al257 * bl260;
781
+ let e261 = ah256 * bh259 - p254 + ah256 * bl260 + al257 * bh259 + al257 * bl260;
782
+ if (!Number.isFinite(e261) && Number.isFinite(p254))
783
+ e261 = prodErrScaled(LN2[0], k);
680
784
  const u262 = e261 + LN2[1] * k;
681
785
  const s263 = p254 + u262;
682
786
  const e264 = u262 - (s263 - p254);
@@ -38,6 +38,11 @@ export function gammainc(x, a) {
38
38
  const lg = lgamma(a);
39
39
  const g = gamma(a);
40
40
  const tail = (lower) => {
41
+ const logP = gammaInc(a, x, lower, true);
42
+ // a tail that is exactly 0 (the upper at x = Infinity, the lower at x = 0): 0, also where Gamma(a) overflows
43
+ // (Infinity * 0 made gammainc(Infinity, 1e308)$upper NaN)
44
+ if (logP === -Infinity)
45
+ return [0, -Infinity];
41
46
  const p = gammaInc(a, x, lower, false);
42
47
  const v = g * p;
43
48
  // the product where it is a normal number (then also its log: lgamma(a) + log P can cancel)
@@ -86,6 +91,9 @@ function smallTail(x, a, lower) {
86
91
  export function erfcinv(z) {
87
92
  if (Number.isNaN(z) || z < 0 || z > 2)
88
93
  return NaN;
94
+ // Below 2^-1021, z / 2 is not exact (Number.MIN_VALUE / 2 is 0): the log probability instead
95
+ if (z < 2 ** -1021)
96
+ return -qnorm(Math.log(z) - Math.LN2, { logP: true }) * Math.SQRT1_2;
89
97
  return -qnorm(z / 2) * Math.SQRT1_2;
90
98
  }
91
99
  /**
@@ -136,7 +144,11 @@ export function perm(n, k) {
136
144
  const intK = k === Math.floor(k);
137
145
  if (n === Math.floor(n) && intK && k > n)
138
146
  return 0;
139
- if (!Number.isFinite(n) || !Number.isFinite(k))
147
+ // the limit n -> Infinity of a product ~ n^k for a fixed finite k (lgamma would give Infinity - Infinity);
148
+ // both infinite is indeterminate
149
+ if (n === Infinity)
150
+ return k === Infinity ? NaN : k === 0 ? 1 : Infinity;
151
+ if (!Number.isFinite(k))
140
152
  return permLgamma(n, k);
141
153
  if (intK && k <= 1000)
142
154
  return fallingFactorial(n, k);
@@ -228,7 +240,7 @@ function lgammaDD(z) {
228
240
  const e = ddSub(ddMul(twoSum(z, -0.5), ddLog([z, 0])), [z, 0]);
229
241
  return ddAdd(ddAdd(e, LS2PI_DD), [stirlerr(z), 0]);
230
242
  }
231
- /** Gamma(n + 1) / Gamma(n - k + 1) from lgamma (infinite n or k) */
243
+ /** Gamma(n + 1) / Gamma(n - k + 1) from lgamma (finite n, infinite k) */
232
244
  function permLgamma(n, k) {
233
245
  const num = lgammaSign(n + 1);
234
246
  const den = lgammaSign(n - k + 1);
@@ -41,7 +41,8 @@ export function sinpi(x) {
41
41
  * Replaces xsf's Stirling approximation (polynomial STIR), which has a relative error of 7e-14 at x = 170.
42
42
  */
43
43
  function stirf(x) {
44
- if (x >= MAXGAM)
44
+ // MAXGAM rounds to the largest double whose Gamma is finite, so only beyond it is the result Infinity
45
+ if (x > MAXGAM)
45
46
  return Infinity;
46
47
  const n = x - 1;
47
48
  const e = ddAdd(ddMulNum(ddLog([n, 0]), n), [-n + stirlerrForGamma(n), 0]);
@@ -97,7 +98,15 @@ export function gamma(x) {
97
98
  z = q * sinpi(z);
98
99
  if (z === 0)
99
100
  return sgngam * Infinity;
100
- return (sgngam * Math.PI) / (Math.abs(z) * stirf(q));
101
+ const denom = Math.abs(z) * stirf(q);
102
+ if (Number.isFinite(denom))
103
+ return (sgngam * Math.PI) / denom;
104
+ // The denominator overflows (q beyond 171.6, or near it) while the result is a subnormal down to q = 177.5:
105
+ // Gamma(q) = Gamma(q - 10) (q - 1) ... (q - 10), with q - 10 exact, and the result rounded once at the end
106
+ let prod = 1;
107
+ for (let i = 1; i <= 10; i++)
108
+ prod *= q - i;
109
+ return (sgngam * Math.PI) / (Math.abs(z) * stirf(q - 10)) / prod;
101
110
  }
102
111
  return stirf(x);
103
112
  }
@@ -170,6 +179,10 @@ export function lgammaSign(x) {
170
179
  return { value: Infinity, sign: 1 };
171
180
  return { value: LOGPI - Math.log(z) - w, sign };
172
181
  }
182
+ // log|Γ(x)| = -log|x| - γ x + O(x²), -log|x| to the double; the product below overflowed (1 / x) and lgamma was Infinity
183
+ // at a subnormal x (lgamma(5e-324) is 744.44007192138122, as R gives)
184
+ if (x !== 0 && Math.abs(x) < 1e-300)
185
+ return { value: -Math.log(Math.abs(x)), sign: x > 0 ? 1 : -1 };
173
186
  if (x < 13) {
174
187
  let z = 1;
175
188
  let p = 0;
@@ -23,17 +23,27 @@ const SMALLRATIO = 0.3;
23
23
  const LARGERATIO = 4.5;
24
24
  const BIG = 4.503599627370496e15;
25
25
  const BIGINV = 2.22044604925031308085e-16;
26
+ /**
27
+ * Below this the factor x^a e^-x / Gamma(a + 1) (dpoisRaw) is rounded on the subnormal grid or 0, and the linear value
28
+ * of the tail is exp of its log instead: the product with the series or continued fraction had lost digits, or was 0
29
+ * where the tail is a few subnormal ulps (pchisq(150856.77, 172464.33) was 0, its log -743.7469247408216, so 1e-323)
30
+ */
31
+ const MIN_NORMAL = 2 ** -1022;
26
32
  /** Series for P(a, x) (DLMF 8.11.4) */
27
33
  function igamSeries(a, x, log) {
28
34
  const ans = igamSeriesSum(a, x);
29
35
  // x^a e^-x / Gamma(a) * ans / a = dpoisRaw(a, x) * ans
30
- return log ? dpoisRaw(a, x, true) + Math.log(ans) : dpoisRaw(a, x, false) * ans;
36
+ if (log)
37
+ return dpoisRaw(a, x, true) + Math.log(ans);
38
+ const d = dpoisRaw(a, x, false);
39
+ return d >= MIN_NORMAL ? d * ans : Math.exp(dpoisRaw(a, x, true) + Math.log(ans));
31
40
  }
32
41
  /** [log, linear] of igamSeries */
33
42
  function igamSeriesBoth(a, x) {
34
43
  const ans = igamSeriesSum(a, x);
35
44
  const d = dpoisRawBoth(a, x);
36
- return [d[0] + Math.log(ans), d[1] * ans];
45
+ const l = d[0] + Math.log(ans);
46
+ return [l, d[1] >= MIN_NORMAL ? d[1] * ans : Math.exp(l)];
37
47
  }
38
48
  /** sum_n x^n / ((a + 1) ... (a + n)): P(a, x) = x^a e^-x / Gamma(a + 1) times this */
39
49
  export function igamSeriesSum(a, x) {
@@ -105,15 +115,17 @@ export function igamSeriesSumDD(a, x) {
105
115
  function igamcContinuedFraction(a, x, log) {
106
116
  const ans = igamcContinuedFractionValue(a, x);
107
117
  // x^a e^-x / Gamma(a) * ans = a * dpoisRaw(a, x) * ans
108
- return log
109
- ? Math.log(a) + dpoisRaw(a, x, true) + Math.log(ans)
110
- : a * dpoisRaw(a, x, false) * ans;
118
+ if (log)
119
+ return Math.log(a) + dpoisRaw(a, x, true) + Math.log(ans);
120
+ const d = dpoisRaw(a, x, false);
121
+ return d >= MIN_NORMAL ? a * d * ans : Math.exp(Math.log(a) + dpoisRaw(a, x, true) + Math.log(ans));
111
122
  }
112
123
  /** [log, linear] of igamcContinuedFraction */
113
124
  function igamcContinuedFractionBoth(a, x) {
114
125
  const ans = igamcContinuedFractionValue(a, x);
115
126
  const d = dpoisRawBoth(a, x);
116
- return [Math.log(a) + d[0] + Math.log(ans), a * d[1] * ans];
127
+ const l = Math.log(a) + d[0] + Math.log(ans);
128
+ return [l, d[1] >= MIN_NORMAL ? a * d[1] * ans : Math.exp(l)];
117
129
  }
118
130
  /** The continued fraction of DLMF 8.9.2: Q(a, x) = x^a e^-x / Gamma(a) times this */
119
131
  export function igamcContinuedFractionValue(a, x) {
@@ -325,6 +337,13 @@ function directTailBoth(a, x) {
325
337
  const q = igamcSeries(a, x);
326
338
  return { lower: false, log: Math.log(q), lin: q };
327
339
  }
340
+ /**
341
+ * Below this a, Q(a, x) is taken as (a / A_SUB) Q(A_SUB, x): Q(a, x) / a varies with a by a relative O(a log x) only
342
+ * (below 1e-297 here). Q is then of the order of a, so subnormal for a below 1e-308 or so, and the direct forms had
343
+ * lost digits before the log was taken (pgamma(1, 4.8e-316, scale = 2.06, lower = FALSE, log = TRUE) was 5e-9
344
+ * off)
345
+ */
346
+ const A_SUB = 1e-300;
328
347
  /**
329
348
  * Return [log, linear] of gammaInc from a single series/continued-fraction computation.
330
349
  * The values are bit-identical to gammaInc(.., true) and gammaInc(.., false) respectively
@@ -355,6 +374,10 @@ export function gammaIncBoth(a, x, lower) {
355
374
  return [Math.log1p(-Math.exp(lq)), -Math.expm1(lq)];
356
375
  return [lq, Math.exp(lq)];
357
376
  }
377
+ if (!lower && a < A_SUB) {
378
+ const [l, v] = gammaIncBoth(A_SUB, x, false);
379
+ return [l + Math.log(a / A_SUB), v * (a / A_SUB)];
380
+ }
358
381
  const absxmaA = Math.abs(x - a) / a;
359
382
  if (xsfAsymptotic(a, absxmaA)) {
360
383
  const v = asymptoticSeries(a, x, lower);
@@ -400,6 +423,10 @@ export function gammaInc(a, x, lower, log) {
400
423
  return log ? Math.log1p(-Math.exp(lq)) : -Math.expm1(lq);
401
424
  return log ? lq : Math.exp(lq);
402
425
  }
426
+ if (!lower && a < A_SUB) {
427
+ const v = gammaInc(A_SUB, x, false, log);
428
+ return log ? v + Math.log(a / A_SUB) : v * (a / A_SUB);
429
+ }
403
430
  /* Asymptotic regime where a ~ x: both tails can be computed directly */
404
431
  const absxmaA = Math.abs(x - a) / a;
405
432
  if (xsfAsymptotic(a, absxmaA)) {