@numero-uno-jpn/rstats 0.5.0 → 0.7.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 (179) hide show
  1. package/CHANGELOG.md +125 -0
  2. package/README.ja.md +36 -20
  3. package/README.md +34 -18
  4. package/THIRD_PARTY_NOTICES.md +25 -5
  5. package/dist/chunks/chunk-6R7YDHA2.js +2 -0
  6. package/dist/chunks/chunk-AFKYWGKN.js +2 -0
  7. package/dist/chunks/chunk-ANYGLFT4.js +2 -0
  8. package/dist/chunks/chunk-BI5RSUZ2.js +2 -0
  9. package/dist/chunks/chunk-J3SNI7KY.js +2 -0
  10. package/dist/chunks/chunk-JCZAB6C5.js +2 -0
  11. package/dist/chunks/chunk-MVG5LUZU.js +2 -0
  12. package/dist/chunks/chunk-PTPPP4TI.js +2 -0
  13. package/dist/chunks/chunk-QNQJA6KZ.js +2 -0
  14. package/dist/chunks/chunk-SXSJXDD3.js +2 -0
  15. package/dist/chunks/chunk-UIOAWLAN.js +2 -0
  16. package/dist/chunks/chunk-XYXHHF5L.js +2 -0
  17. package/dist/chunks/chunk-YKN6OGJL.js +2 -0
  18. package/dist/chunks/chunk-ZW52Z3GY.js +2 -0
  19. package/dist/chunks/chunk-ZYA4ISAT.js +2 -0
  20. package/dist/descriptive/basic.d.ts +73 -0
  21. package/dist/descriptive/basic.js +256 -0
  22. package/dist/descriptive/cov.d.ts +39 -0
  23. package/dist/descriptive/cov.js +105 -0
  24. package/dist/descriptive/hist.d.ts +64 -0
  25. package/dist/descriptive/hist.js +304 -0
  26. package/dist/descriptive/moments.d.ts +63 -0
  27. package/dist/descriptive/moments.js +158 -0
  28. package/dist/descriptive/quantile.d.ts +50 -0
  29. package/dist/descriptive/quantile.js +131 -0
  30. package/dist/descriptive/rank.d.ts +23 -0
  31. package/dist/descriptive/rank.js +72 -0
  32. package/dist/descriptive/table.d.ts +54 -0
  33. package/dist/descriptive/table.js +160 -0
  34. package/dist/descriptive.js +2 -0
  35. package/dist/distributions/arcsine.d.ts +24 -0
  36. package/dist/distributions/arcsine.js +73 -0
  37. package/dist/distributions/invgamma.d.ts +29 -0
  38. package/dist/distributions/invgamma.js +85 -0
  39. package/dist/distributions/kumaraswamy.d.ts +17 -0
  40. package/dist/distributions/kumaraswamy.js +96 -0
  41. package/dist/distributions/laplace.d.ts +23 -0
  42. package/dist/distributions/laplace.js +69 -0
  43. package/dist/distributions/multinomial.d.ts +24 -0
  44. package/dist/distributions/multinomial.js +70 -0
  45. package/dist/distributions/nbeta.js +28 -10
  46. package/dist/distributions/nchisq.js +20 -3
  47. package/dist/distributions/pareto.d.ts +23 -0
  48. package/dist/distributions/pareto.js +76 -0
  49. package/dist/distributions/summary.d.ts +25 -0
  50. package/dist/distributions/summary.js +289 -0
  51. package/dist/distributions/triangular.d.ts +25 -0
  52. package/dist/distributions/triangular.js +87 -0
  53. package/dist/distributions.js +2 -0
  54. package/dist/entries/descriptive.d.ts +7 -0
  55. package/dist/entries/descriptive.js +8 -0
  56. package/dist/entries/distributions.d.ts +42 -0
  57. package/dist/entries/distributions.js +42 -0
  58. package/dist/entries/linalg.d.ts +11 -0
  59. package/dist/entries/linalg.js +11 -0
  60. package/dist/entries/numerics.d.ts +8 -0
  61. package/dist/entries/numerics.js +6 -0
  62. package/dist/entries/special.d.ts +10 -0
  63. package/dist/entries/special.js +10 -0
  64. package/dist/entries/tests.d.ts +21 -0
  65. package/dist/entries/tests.js +20 -0
  66. package/dist/htest/ansariTest.d.ts +30 -0
  67. package/dist/htest/ansariTest.js +253 -0
  68. package/dist/htest/aov.d.ts +85 -0
  69. package/dist/htest/aov.js +178 -0
  70. package/dist/htest/binomTest.d.ts +36 -0
  71. package/dist/htest/binomTest.js +121 -0
  72. package/dist/htest/chisqTest.d.ts +34 -2
  73. package/dist/htest/chisqTest.js +140 -37
  74. package/dist/htest/cor.d.ts +14 -0
  75. package/dist/htest/cor.js +4 -3
  76. package/dist/htest/core/ansari.d.ts +25 -0
  77. package/dist/htest/core/ansari.js +186 -0
  78. package/dist/htest/core/kolmogorov.d.ts +26 -0
  79. package/dist/htest/core/kolmogorov.js +221 -0
  80. package/dist/htest/core/mantelhaen.d.ts +26 -0
  81. package/dist/htest/core/mantelhaen.js +145 -0
  82. package/dist/htest/core/shapiro.d.ts +24 -0
  83. package/dist/htest/core/shapiro.js +142 -0
  84. package/dist/htest/core/simulate.d.ts +2 -0
  85. package/dist/htest/core/simulate.js +16 -0
  86. package/dist/htest/core/smirnov.d.ts +20 -0
  87. package/dist/htest/core/smirnov.js +75 -0
  88. package/dist/htest/fisherTest.d.ts +12 -1
  89. package/dist/htest/fisherTest.js +187 -127
  90. package/dist/htest/groups.d.ts +20 -0
  91. package/dist/htest/groups.js +62 -0
  92. package/dist/htest/ksTest.d.ts +31 -0
  93. package/dist/htest/ksTest.js +106 -0
  94. package/dist/htest/mantelhaenTest.d.ts +26 -0
  95. package/dist/htest/mantelhaenTest.js +196 -0
  96. package/dist/htest/mcnemarTest.d.ts +26 -0
  97. package/dist/htest/mcnemarTest.js +99 -0
  98. package/dist/htest/onewayTest.d.ts +26 -0
  99. package/dist/htest/onewayTest.js +97 -0
  100. package/dist/htest/pairwise.d.ts +64 -0
  101. package/dist/htest/pairwise.js +152 -0
  102. package/dist/htest/poissonTest.d.ts +35 -0
  103. package/dist/htest/poissonTest.js +170 -0
  104. package/dist/htest/propTest.d.ts +37 -0
  105. package/dist/htest/propTest.js +164 -0
  106. package/dist/htest/rankTests.d.ts +52 -0
  107. package/dist/htest/rankTests.js +234 -0
  108. package/dist/htest/types.d.ts +2 -0
  109. package/dist/htest/types.js +6 -0
  110. package/dist/htest/varTest.d.ts +48 -0
  111. package/dist/htest/varTest.js +118 -0
  112. package/dist/htest/wilcoxTest.js +136 -174
  113. package/dist/index.d.ts +6 -50
  114. package/dist/index.js +8 -50
  115. package/dist/integrate/quadpack.d.ts +29 -0
  116. package/dist/integrate/quadpack.js +525 -0
  117. package/dist/internal/seq.d.ts +5 -0
  118. package/dist/internal/seq.js +14 -0
  119. package/dist/internal/uniroot.js +10 -71
  120. package/dist/linalg/lu.d.ts +45 -0
  121. package/dist/linalg/lu.js +88 -0
  122. package/dist/linalg/ops.d.ts +90 -0
  123. package/dist/linalg/ops.js +320 -0
  124. package/dist/linalg.js +2 -0
  125. package/dist/models/lm.d.ts +98 -0
  126. package/dist/models/lm.js +187 -0
  127. package/dist/numerics/deriv.d.ts +37 -0
  128. package/dist/numerics/deriv.js +150 -0
  129. package/dist/numerics/integrate.d.ts +91 -0
  130. package/dist/numerics/integrate.js +264 -0
  131. package/dist/numerics/interp.d.ts +103 -0
  132. package/dist/numerics/interp.js +581 -0
  133. package/dist/numerics/ode.d.ts +23 -0
  134. package/dist/numerics/ode.js +59 -0
  135. package/dist/numerics.js +2 -0
  136. package/dist/optim/localmin.d.ts +16 -0
  137. package/dist/optim/localmin.js +97 -0
  138. package/dist/random/beta.js +1 -1
  139. package/dist/random/binomial.js +1 -1
  140. package/dist/random/continuous.js +1 -1
  141. package/dist/random/extra.d.ts +33 -0
  142. package/dist/random/extra.js +62 -0
  143. package/dist/random/gamma.js +1 -1
  144. package/dist/random/hyper.js +1 -1
  145. package/dist/random/mt.js +0 -1
  146. package/dist/random/poisson.js +0 -1
  147. package/dist/random/r2dtable.d.ts +26 -0
  148. package/dist/random/r2dtable.js +153 -0
  149. package/dist/random/rank.js +1 -1
  150. package/dist/random/rng.js +1 -1
  151. package/dist/random/sample.js +1 -1
  152. package/dist/rstats.js +3 -2
  153. package/dist/special/besselIK.d.ts +38 -0
  154. package/dist/special/besselIK.js +315 -0
  155. package/dist/special/besselJY.d.ts +32 -0
  156. package/dist/special/besselJY.js +508 -0
  157. package/dist/special/besselSeries.d.ts +8 -0
  158. package/dist/special/besselSeries.js +24 -0
  159. package/dist/special/beta.js +22 -5
  160. package/dist/special/choose.d.ts +75 -0
  161. package/dist/special/choose.js +233 -0
  162. package/dist/special/erf.d.ts +28 -2
  163. package/dist/special/erf.js +37 -10
  164. package/dist/special/extra.d.ts +47 -0
  165. package/dist/special/extra.js +220 -0
  166. package/dist/special/igam.d.ts +4 -0
  167. package/dist/special/igam.js +57 -2
  168. package/dist/special/incbet.js +77 -22
  169. package/dist/special/lgammaDiff.d.ts +7 -0
  170. package/dist/special/lgammaDiff.js +16 -3
  171. package/dist/special/log1p.d.ts +53 -0
  172. package/dist/special/log1p.js +93 -0
  173. package/dist/special/psigamma.d.ts +48 -0
  174. package/dist/special/psigamma.js +318 -0
  175. package/dist/special/rCompatible.d.ts +8 -0
  176. package/dist/special/rCompatible.js +1 -0
  177. package/dist/special.js +2 -0
  178. package/dist/tests.js +3 -0
  179. package/package.json +26 -1
@@ -9,6 +9,7 @@
9
9
  // - The prefactor x^a e^-x / Gamma(a) is computed with Loader's dpoisRaw (= x^a e^-x / Gamma(a+1)),
10
10
  // to avoid cancellation in the log for large a and x
11
11
  import { erfc } from "./erf.js";
12
+ import { SPLITTER } from "./dd.js";
12
13
  import { MACHEP } from "./constants.js";
13
14
  import { lgamma, lgamma1p } from "./gamma.js";
14
15
  import { IGAM_ASYMP_COEFF_D, IGAM_ASYMP_COEFF_K, IGAM_ASYMP_COEFF_N } from "./igamAsympCoeff.js";
@@ -34,7 +35,10 @@ function igamSeriesBoth(a, x) {
34
35
  const d = dpoisRawBoth(a, x);
35
36
  return [d[0] + Math.log(ans), d[1] * ans];
36
37
  }
37
- function igamSeriesSum(a, x) {
38
+ /** sum_n x^n / ((a + 1) ... (a + n)): P(a, x) = x^a e^-x / Gamma(a + 1) times this */
39
+ export function igamSeriesSum(a, x) {
40
+ if (a > 1000 && x > 0.97 * a)
41
+ return igamSeriesSumDD(a, x);
38
42
  let r = a;
39
43
  let c = 1;
40
44
  let ans = 1;
@@ -47,6 +51,56 @@ function igamSeriesSum(a, x) {
47
51
  }
48
52
  return ans;
49
53
  }
54
+ /**
55
+ * igamSeriesSum in double-double, for large a with x above 0.97 a (the mpmath audit needs it from about there; below,
56
+ * the series is short enough for double). The terms then shrink slowly (ratio x / (a + n) close to 1, thousands
57
+ * of terms), and in double precision the roundings of the ratios, correlated between neighbouring terms, add up to
58
+ * 2e-14 relative; stopping at c <= eps ans also leaves out a tail of up to c (a + n) / (a + n - x). Written out on
59
+ * scalars (no arrays), as the loop is long
60
+ */
61
+ const TAIL = 2 ** -64;
62
+ function igamSeriesSumDD(a, x) {
63
+ let ch = 1;
64
+ let cl = 0;
65
+ let ah = 1;
66
+ let al = 0;
67
+ for (let i = 1; i < MAXITER; i++) {
68
+ // d = a + i exactly (dh + dl)
69
+ const dh = a + i;
70
+ const bb = dh - a;
71
+ const dl = a - (dh - bb) + (i - bb);
72
+ // q = x / d: q1 + q2
73
+ const q1 = x / dh;
74
+ let t = SPLITTER * q1;
75
+ const q1h = t - (t - q1);
76
+ const q1l = q1 - q1h;
77
+ t = SPLITTER * dh;
78
+ const dhh = t - (t - dh);
79
+ const dhl = dh - dhh;
80
+ const p = q1 * dh;
81
+ const pe = q1h * dhh - p + q1h * dhl + q1l * dhh + q1l * dhl;
82
+ const q2 = (x - p - pe - q1 * dl) / dh;
83
+ // c = c q
84
+ t = SPLITTER * ch;
85
+ const chh = t - (t - ch);
86
+ const chl = ch - chh;
87
+ const m = ch * q1;
88
+ const me = chh * q1h - m + chh * q1l + chl * q1h + chl * q1l + (ch * q2 + cl * q1);
89
+ ch = m + me;
90
+ cl = me - (ch - m);
91
+ // ans += c
92
+ const s = ah + ch;
93
+ const sb = s - ah;
94
+ const e = ah - (s - sb) + (ch - sb) + (al + cl);
95
+ ah = s + e;
96
+ al = e - (ah - s);
97
+ // the rest is at most c q / (1 - q) (q decreasing); stop once that is below 2^-64 ans, well under the
98
+ // rounding of the double result
99
+ if (ch * q1 <= TAIL * ah * (1 - q1))
100
+ break;
101
+ }
102
+ return ah + al;
103
+ }
50
104
  /** Continued fraction for Q(a, x) (DLMF 8.9.2) */
51
105
  function igamcContinuedFraction(a, x, log) {
52
106
  const ans = igamcContinuedFractionValue(a, x);
@@ -61,7 +115,8 @@ function igamcContinuedFractionBoth(a, x) {
61
115
  const d = dpoisRawBoth(a, x);
62
116
  return [Math.log(a) + d[0] + Math.log(ans), a * d[1] * ans];
63
117
  }
64
- function igamcContinuedFractionValue(a, x) {
118
+ /** The continued fraction of DLMF 8.9.2: Q(a, x) = x^a e^-x / Gamma(a) times this */
119
+ export function igamcContinuedFractionValue(a, x) {
65
120
  let y = 1 - a;
66
121
  let z = x + y + 1;
67
122
  let c = 0;
@@ -12,8 +12,8 @@
12
12
  // Loader's dbinomRaw (avoids cancellation in the log for large a, b)
13
13
  // - The log is formed directly from the result of each computation
14
14
  import { MACHEP } from "./constants.js";
15
- import { dbinomRawDD } from "./loader.js";
16
- import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, twoProd, twoSum } from "./dd.js";
15
+ import { dbinomExponent, dbinomRawDD } from "./loader.js";
16
+ import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, twoProd, twoSum } from "./dd.js";
17
17
  import { lgamma1p } from "./gamma.js";
18
18
  import { lgammaDiff } from "./lgammaDiff.js";
19
19
  const BIG = 4.503599627370496e15;
@@ -33,6 +33,21 @@ function prefactor(a, b, x, xc) {
33
33
  }
34
34
  /** Lower limit of normal numbers. If the prefactor is larger, multiply on the linear scale */
35
35
  const NORMAL_MIN = 2.2250738585072014e-308 * 1e3;
36
+ /**
37
+ * m x^a (1-x)^b / B(a, b) for a prefactor below NORMAL_MIN (m a moderate factor such as the series sum).
38
+ * Going through the log would round an exponent near -708 to 0.5 ulp, which is 1e-13 relative after exp,
39
+ * so the exponent of dbinom(a; a+b, x) is kept in double-double and exp is taken once at the end.
40
+ * (multiplying by exp(-700) last rounds once, so a subnormal result is still correctly rounded).
41
+ * Returns undefined when the result underflows to 0 (the caller then works on the log scale)
42
+ */
43
+ function smallPrefactorTimes(a, b, x, xc, m) {
44
+ const { e } = dbinomExponent(a, b, x, xc);
45
+ const c = (m * ((a * b) / (a + b))) / Math.sqrt(2 * Math.PI * a * (b / (a + b)));
46
+ if (!(e[0] < 1400) || !Number.isFinite(c) || c === 0)
47
+ return undefined;
48
+ const v = Math.exp(700 - e[0]) * (1 - e[1]) * c * Math.exp(-700);
49
+ return v > 0 ? v : undefined;
50
+ }
36
51
  /**
37
52
  * Series sum S = Σ_{n>=1} (1-b)_n / n! x^n / (a + n) (the core of xsf's pseries).
38
53
  * xsf stops on the relative error of the sum including 1/a; here we stop on S itself
@@ -98,6 +113,9 @@ function pseries(a, b, xd, xcd, log) {
98
113
  const pf = prefactor(a, b, xd, xcd);
99
114
  if (pf > NORMAL_MIN)
100
115
  return (s * pf) / Math.exp(b * l1m);
116
+ const v = smallPrefactorTimes(a, b, xd, xcd, s / Math.exp(b * l1m));
117
+ if (v !== undefined)
118
+ return v;
101
119
  }
102
120
  const lv = Math.log(s) + logPrefactor(a, b, xd, xcd) - b * l1m;
103
121
  return log ? lv : Math.exp(lv);
@@ -110,32 +128,47 @@ function pseries(a, b, xd, xcd, log) {
110
128
  * the rounding of x = 0.999 (1e-19) alone shifts the result by 6e-13 relative (measured at a = 1e5, b = 100;
111
129
  * reference values checked with Rmpfr). So x is kept as an exact double-double and the recurrence also runs in
112
130
  * double-double.
131
+ *
132
+ * For the same reason a + b and b - 1 are kept in double-double: with a ≫ b, rounding a + b loses digits of b
133
+ * (5e-11 relative at a = 1e8, b = 0.01, x = 1 - 1e-6; checked against mpmath). From 2^52 on, the steps k += 1 of
134
+ * the other coefficients would be lost as well, so there every coefficient is a double-double (huge)
113
135
  */
114
136
  function continuedFractionCore(a, b, xd, xcd, second) {
115
137
  // #1: coefficients use x; #2: coefficients use z = x/(1-x)
116
138
  const z = second ? ddDiv(xd, xcd) : xd;
139
+ const huge = Math.max(a, b) >= 2 ** 52;
140
+ // Partial numerators k1 x k2 / (k3 k4) and k5 x k6 / (k7 k8). k1, k3, k4, k7, k8 are doubles, with
141
+ // double-double copies (h1, ...) that are used and advanced only when huge
117
142
  let k1 = a;
118
- let k2 = second ? b - 1 : a + b;
143
+ let k2 = second ? twoSum(b, -1) : twoSum(a, b);
119
144
  let k3 = a;
120
145
  let k4 = a + 1;
121
146
  let k5 = 1;
122
- let k6 = second ? a + b : b - 1;
147
+ let k6 = second ? twoSum(a, b) : twoSum(b, -1);
123
148
  let k7 = a + 1;
124
149
  let k8 = a + 2;
150
+ let h1 = [a, 0];
151
+ let h3 = [a, 0];
152
+ let h4 = twoSum(a, 1);
153
+ let h7 = twoSum(a, 1);
154
+ let h8 = twoSum(a, 2);
155
+ const d1 = second ? -1 : 1;
125
156
  let pkm2 = [0, 0];
126
157
  let qkm2 = [1, 0];
127
158
  let pkm1 = [1, 0];
128
159
  let qkm1 = [1, 0];
129
160
  let ans = [1, 0];
130
161
  for (let n = 0; n < 100000; n++) {
131
- let xk = ddDiv(ddMulNum(ddMulNum(z, -k1), k2), twoProd(k3, k4));
132
- let pk = ddAdd(pkm1, ddMul(pkm2, xk));
133
- let qk = ddAdd(qkm1, ddMul(qkm2, xk));
162
+ let xk = huge
163
+ ? ddDiv(ddMul(ddMul(z, h1), k2), ddMul(h3, h4))
164
+ : ddDiv(ddMul(ddMulNum(z, k1), k2), twoProd(k3, k4));
165
+ let pk = ddSub(pkm1, ddMul(pkm2, xk));
166
+ let qk = ddSub(qkm1, ddMul(qkm2, xk));
134
167
  pkm2 = pkm1;
135
168
  pkm1 = pk;
136
169
  qkm2 = qkm1;
137
170
  qkm1 = qk;
138
- xk = ddDiv(ddMulNum(ddMulNum(z, k5), k6), twoProd(k7, k8));
171
+ xk = ddDiv(ddMul(ddMulNum(z, k5), k6), huge ? ddMul(h7, h8) : twoProd(k7, k8));
139
172
  pk = ddAdd(pkm1, ddMul(pkm2, xk));
140
173
  qk = ddAdd(qkm1, ddMul(qkm2, xk));
141
174
  pkm2 = pkm1;
@@ -149,21 +182,23 @@ function continuedFractionCore(a, b, xd, xcd, second) {
149
182
  if (t < MACHEP && n > 1)
150
183
  break;
151
184
  }
152
- if (second) {
153
- k1 += 1;
154
- k2 -= 1;
155
- k6 += 1;
185
+ k2 = ddAdd(k2, [d1, 0]);
186
+ k6 = ddAdd(k6, [-d1, 0]);
187
+ k5 += 1;
188
+ if (huge) {
189
+ h1 = ddAdd(h1, [1, 0]);
190
+ h3 = ddAdd(h3, [2, 0]);
191
+ h4 = ddAdd(h4, [2, 0]);
192
+ h7 = ddAdd(h7, [2, 0]);
193
+ h8 = ddAdd(h8, [2, 0]);
156
194
  }
157
195
  else {
158
196
  k1 += 1;
159
- k2 += 1;
160
- k6 -= 1;
197
+ k3 += 2;
198
+ k4 += 2;
199
+ k7 += 2;
200
+ k8 += 2;
161
201
  }
162
- k3 += 2;
163
- k4 += 2;
164
- k5 += 1;
165
- k7 += 2;
166
- k8 += 2;
167
202
  const mag = Math.abs(qk[0]) + Math.abs(pk[0]);
168
203
  if (mag > BIG) {
169
204
  pkm2 = ddMulNum(pkm2, BIGINV);
@@ -198,8 +233,12 @@ function continuedFraction(a, b, xd, xcd, log) {
198
233
  // I = x^a (1-x)^b / (a B(a, b)) * w
199
234
  if (!log) {
200
235
  const pf = prefactor(a, b, xd, xcd);
236
+ // w / a first: pf / a can fall into the subnormal range for huge a (1e-316 at a = 7e18) and lose digits
201
237
  if (pf > NORMAL_MIN)
202
- return (pf / a) * w;
238
+ return pf * (w / a);
239
+ const v = smallPrefactorTimes(a, b, xd, xcd, w / a);
240
+ if (v !== undefined)
241
+ return v;
203
242
  }
204
243
  const lv = logPrefactor(a, b, xd, xcd) - Math.log(a) + Math.log(w);
205
244
  return log ? lv : Math.exp(lv);
@@ -292,8 +331,13 @@ function betaIncCore(a, b, xd, xcd, lower, wantLog) {
292
331
  const otherDirect = () => {
293
332
  if (pseriesOk(oa, ob, ox[0]))
294
333
  return pseries(oa, ob, ox, oxc, false);
295
- if (pseriesOk(pa, pb, px[0]))
296
- return pseriesComplement(pa, pb, px, false);
334
+ if (pseriesOk(pa, pb, px[0])) {
335
+ const v = pseriesComplement(pa, pb, px, false);
336
+ // Cancellation check as for the requested tail (see below)
337
+ if (pa * Math.abs(Math.log(px[0])) > 32 * v && cfOk(oa, ob, ox[0]))
338
+ return continuedFraction(oa, ob, ox, oxc, false);
339
+ return v;
340
+ }
297
341
  if (cfOk(oa, ob, ox[0]))
298
342
  return continuedFraction(oa, ob, ox, oxc, false);
299
343
  return undefined;
@@ -308,6 +352,17 @@ function betaIncCore(a, b, xd, xcd, lower, wantLog) {
308
352
  }
309
353
  if (pseriesOk(oa, ob, ox[0])) {
310
354
  const v = pseriesComplement(oa, ob, ox, false);
355
+ // The exponent L of pseriesComplement is a sum of terms of size a |log x| that can cancel, and their rounding
356
+ // (absolute, about ε a |log x|) is then large relative to a small result (1e-14 at a = 0.01, b = 1e4, x = 1e-4).
357
+ // When a |log x| exceeds the result 32 times and the continued fraction applies in this tail's frame, use it
358
+ // instead (a lower threshold would also send cases where the series is accurate, e.g. most of pt near 0, to the
359
+ // slower continued fraction)
360
+ if (oa * Math.abs(Math.log(ox[0])) > 32 * v && cfOk(pa, pb, px[0])) {
361
+ const w = continuedFraction(pa, pb, px, pxc, false);
362
+ if (wantLog && w < TINY)
363
+ return [continuedFraction(pa, pb, px, pxc, true), w];
364
+ return finish(w, otherDirect);
365
+ }
311
366
  return finish(v, () => pseries(oa, ob, ox, oxc, false));
312
367
  }
313
368
  if (cfOk(pa, pb, px[0])) {
@@ -1,2 +1,9 @@
1
+ import { type DD } from "./dd.js";
1
2
  /** lgamma(b + a) - lgamma(b). a >= 0, b > 0 */
2
3
  export declare function lgammaDiff(a: number, b: number): number;
4
+ /**
5
+ * lgammaDiff in double-double: the term a log(B + a), which reaches hundreds for large b (exp of it would turn its
6
+ * rounding into a relative error of 1e-14 at B(0.5, 1e300)), is formed in double-double; the others are of the size
7
+ * of a or less and stay in double precision
8
+ */
9
+ export declare function lgammaDiffDD(a: number, b: number): DD;
@@ -9,11 +9,21 @@
9
9
  // D(a, B) = (B - 1/2) log1p(a/B) + a log(B + a) - a + [stirlerr(B + a) - stirlerr(B)]
10
10
  // and compute the stirlerr difference with expm1 from the differences of the Stirling series terms,
11
11
  // B^-k ((1 + a/B)^-k - 1)
12
+ import { ddAdd, ddLog, ddMulNum, twoSum } from "./dd.js";
12
13
  const S = [1 / 12, -1 / 360, 1 / 1260, -1 / 1680, 1 / 1188, -691 / 360360, 1 / 156];
13
14
  /** lgamma(b + a) - lgamma(b). a >= 0, b > 0 */
14
15
  export function lgammaDiff(a, b) {
16
+ const d = lgammaDiffDD(a, b);
17
+ return d[0] + d[1];
18
+ }
19
+ /**
20
+ * lgammaDiff in double-double: the term a log(B + a), which reaches hundreds for large b (exp of it would turn its
21
+ * rounding into a relative error of 1e-14 at B(0.5, 1e300)), is formed in double-double; the others are of the size
22
+ * of a or less and stay in double precision
23
+ */
24
+ export function lgammaDiffDD(a, b) {
15
25
  if (a === 0)
16
- return 0;
26
+ return [0, 0];
17
27
  let acc = 0;
18
28
  let B = b;
19
29
  while (B <= 15) {
@@ -21,7 +31,10 @@ export function lgammaDiff(a, b) {
21
31
  B += 1;
22
32
  }
23
33
  const l1p = Math.log1p(a / B);
24
- let d = (B - 0.5) * l1p + a * Math.log(B + a) - a;
34
+ const lead0 = a * Math.log(B + a);
35
+ // (in double precision where it is small: its rounding is then below the others', and incbet calls this often)
36
+ const lead = Math.abs(lead0) > 2 && Number.isFinite(lead0) && a < 1e300 ? ddMulNum(ddLog(twoSum(B, a)), a) : [lead0, 0];
37
+ let d = (B - 0.5) * l1p - a;
25
38
  // stirlerr(B + a) - stirlerr(B) = Σ S_k [ (B + a)^-(2k+1) - B^-(2k+1) ]
26
39
  let pw = 1 / B;
27
40
  const inv2 = 1 / (B * B);
@@ -30,5 +43,5 @@ export function lgammaDiff(a, b) {
30
43
  d += S[k] * pw * Math.expm1(-m * l1p);
31
44
  pw *= inv2;
32
45
  }
33
- return acc + d;
46
+ return ddAdd(lead, [acc + d, 0]);
34
47
  }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * log(1 + x) - x, accurate for small |x| (where log1p(x) - x would cancel). As `log1pmx` in R's C API (Rmath.h).
3
+ *
4
+ * @param x A number >= -1.
5
+ * @returns log(1 + x) - x; `-Infinity` at x = -1 and x = Infinity, `NaN` for x < -1.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * log1pmx(1e-5); // -4.9999666669166655e-11
10
+ * ```
11
+ */
12
+ export declare function log1pmx(x: number): number;
13
+ /**
14
+ * lgamma(1 + x) = log|Gamma(1 + x)|, accurate near x = 0 and x = 1, where it is near 0. As `lgamma1p` in R's C
15
+ * API (Rmath.h).
16
+ *
17
+ * @param x A number.
18
+ * @returns log|Gamma(1 + x)|; `Infinity` at the poles (x a negative integer) and at x = Infinity, `NaN` for
19
+ * x = -Infinity.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * lgamma1p(1e-10); // -5.7721566481928616e-11 (lgamma(1 + 1e-10) keeps only about 6 digits)
24
+ * ```
25
+ */
26
+ export declare function lgamma1p(x: number): number;
27
+ /**
28
+ * log(1 - exp(-x)) for x >= 0, accurate for both small and large x. As `log1mexp` in R's C API (Rmath.h) and the DPQ
29
+ * package; note the sign of the argument.
30
+ *
31
+ * @param x A number >= 0.
32
+ * @returns log(1 - exp(-x)); `-Infinity` at x = 0, 0 at x = Infinity, `NaN` for x < 0.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * log1mexp(1e-10); // -23.025850929990458
37
+ * ```
38
+ */
39
+ export declare function log1mexp(x: number): number;
40
+ /**
41
+ * log(1 + exp(x)), without overflow for large x and accurate for very negative x. As `log1pexp` in R's C API
42
+ * (Rmath.h) and the DPQ package.
43
+ *
44
+ * @param x A number.
45
+ * @returns log(1 + exp(x)); 0 at x = -Infinity, `Infinity` at x = Infinity.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * log1pexp(800); // 800
50
+ * log1pexp(-40); // 4.248354255291589e-18
51
+ * ```
52
+ */
53
+ export declare function log1pexp(x: number): number;
@@ -0,0 +1,93 @@
1
+ // log1p-type functions of R's C API (Rmath.h: log1pmx, lgamma1p, log1mexp, log1pexp), which R does not export to R
2
+ // code; the definitions are those of Rmath and the DPQ package. The kernels are the ones rStats.js uses internally
3
+ // (log1pmx: xsf's series, src/special/log1pmx.ts; lgamma1p: src/special/gamma.ts; log(1 - exp(x)):
4
+ // src/internal/ddexp.ts); this file adds the special values and domains. log1pexp follows the switching points of
5
+ // Maechler (2012), "Accurately computing log(1 - exp(-|a|))", the Rmpfr package vignette.
6
+ import { log1mExp } from "../internal/ddexp.js";
7
+ import { lgamma1p as lgamma1pKernel } from "./gamma.js";
8
+ import { log1pmx as log1pmxKernel } from "./log1pmx.js";
9
+ /**
10
+ * log(1 + x) - x, accurate for small |x| (where log1p(x) - x would cancel). As `log1pmx` in R's C API (Rmath.h).
11
+ *
12
+ * @param x A number >= -1.
13
+ * @returns log(1 + x) - x; `-Infinity` at x = -1 and x = Infinity, `NaN` for x < -1.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * log1pmx(1e-5); // -4.9999666669166655e-11
18
+ * ```
19
+ */
20
+ export function log1pmx(x) {
21
+ if (Number.isNaN(x) || x < -1)
22
+ return NaN;
23
+ if (x === -1 || x === Infinity)
24
+ return -Infinity;
25
+ return log1pmxKernel(x);
26
+ }
27
+ /**
28
+ * lgamma(1 + x) = log|Gamma(1 + x)|, accurate near x = 0 and x = 1, where it is near 0. As `lgamma1p` in R's C
29
+ * API (Rmath.h).
30
+ *
31
+ * @param x A number.
32
+ * @returns log|Gamma(1 + x)|; `Infinity` at the poles (x a negative integer) and at x = Infinity, `NaN` for
33
+ * x = -Infinity.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * lgamma1p(1e-10); // -5.7721566481928616e-11 (lgamma(1 + 1e-10) keeps only about 6 digits)
38
+ * ```
39
+ */
40
+ export function lgamma1p(x) {
41
+ if (Number.isNaN(x) || x === -Infinity)
42
+ return NaN;
43
+ if (x === Infinity)
44
+ return Infinity;
45
+ if (x <= -1 && x === Math.floor(x))
46
+ return Infinity;
47
+ return lgamma1pKernel(x);
48
+ }
49
+ /**
50
+ * log(1 - exp(-x)) for x >= 0, accurate for both small and large x. As `log1mexp` in R's C API (Rmath.h) and the DPQ
51
+ * package; note the sign of the argument.
52
+ *
53
+ * @param x A number >= 0.
54
+ * @returns log(1 - exp(-x)); `-Infinity` at x = 0, 0 at x = Infinity, `NaN` for x < 0.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * log1mexp(1e-10); // -23.025850929990458
59
+ * ```
60
+ */
61
+ export function log1mexp(x) {
62
+ if (Number.isNaN(x) || x < 0)
63
+ return NaN;
64
+ if (x === 0)
65
+ return -Infinity;
66
+ if (x === Infinity)
67
+ return 0;
68
+ return log1mExp(-x);
69
+ }
70
+ /**
71
+ * log(1 + exp(x)), without overflow for large x and accurate for very negative x. As `log1pexp` in R's C API
72
+ * (Rmath.h) and the DPQ package.
73
+ *
74
+ * @param x A number.
75
+ * @returns log(1 + exp(x)); 0 at x = -Infinity, `Infinity` at x = Infinity.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * log1pexp(800); // 800
80
+ * log1pexp(-40); // 4.248354255291589e-18
81
+ * ```
82
+ */
83
+ export function log1pexp(x) {
84
+ if (Number.isNaN(x))
85
+ return NaN;
86
+ if (x <= -37)
87
+ return Math.exp(x); // log1p(e) = e to double precision
88
+ if (x <= 18)
89
+ return Math.log1p(Math.exp(x));
90
+ if (x <= 33.3)
91
+ return x + Math.exp(-x);
92
+ return x;
93
+ }
@@ -0,0 +1,48 @@
1
+ import type { RCompatibleOptions } from "./rCompatible.js";
2
+ /**
3
+ * Digamma function, the derivative of `lgamma`. Equivalent to R's `digamma(x)` for a single number.
4
+ *
5
+ * @param x - Argument.
6
+ * @returns psi(x) (`NaN` at zero and the negative integers).
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * digamma(1); // -0.5772156649015329 (minus Euler's constant)
11
+ * ```
12
+ */
13
+ export declare function digamma(x: number): number;
14
+ /**
15
+ * Trigamma function, the second derivative of `lgamma`. Equivalent to R's `trigamma(x)` for a single number.
16
+ *
17
+ * `trigamma(-Infinity)` is `Infinity` as in R (`NaN` with `rCompatible: false`).
18
+ *
19
+ * @param x - Argument.
20
+ * @param options - `rCompatible` (default `true`).
21
+ * @returns psi'(x) (`Infinity` at zero and the negative integers).
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * trigamma(1); // 1.6449340668482264 (pi^2 / 6)
26
+ * ```
27
+ */
28
+ export declare function trigamma(x: number, options?: RCompatibleOptions): number;
29
+ /**
30
+ * Polygamma function, the derivative of order `deriv` of `digamma`. Equivalent to R's
31
+ * `psigamma(x, deriv)` for a single number.
32
+ *
33
+ * `psigamma(-Infinity, deriv)` is `Infinity` for odd `deriv` as in R, which treats it like the poles
34
+ * (`NaN` with `rCompatible: false`). Unlike R, which returns `NaN` for `x < 0` with `deriv >= 6` and
35
+ * where the result overflows, the value is computed there.
36
+ *
37
+ * @param x - Argument.
38
+ * @param deriv - Order of the derivative, an integer from 0 to 100 (rounded half to even; default 0, digamma).
39
+ * @param options - `rCompatible` (default `true`).
40
+ * @returns psi^(deriv)(x). At zero and the negative integers, `Infinity` for odd `deriv` and `NaN` for even.
41
+ * `NaN` if `deriv` is outside 0 to 100.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * psigamma(2, 2); // -0.40411380631918853 (-2 (zeta(3) - 1))
46
+ * ```
47
+ */
48
+ export declare function psigamma(x: number, deriv?: number, options?: RCompatibleOptions): number;