@numero-uno-jpn/rstats 0.15.3 → 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 (285) hide show
  1. package/CHANGELOG.md +166 -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-VLUL2HFW.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 +24 -4
  47. package/dist/distributions/binomial.js +37 -14
  48. package/dist/distributions/bvnorm.js +11 -2
  49. package/dist/distributions/cauchy.js +12 -4
  50. package/dist/distributions/chisq.js +7 -3
  51. package/dist/distributions/exponential.js +5 -0
  52. package/dist/distributions/f.js +32 -17
  53. package/dist/distributions/gamma.js +27 -3
  54. package/dist/distributions/geometric.js +49 -13
  55. package/dist/distributions/hypergeometric.d.ts +5 -1
  56. package/dist/distributions/hypergeometric.js +93 -6
  57. package/dist/distributions/invgamma.js +2 -1
  58. package/dist/distributions/laplace.js +27 -6
  59. package/dist/distributions/logistic.js +4 -1
  60. package/dist/distributions/lognormal.js +8 -2
  61. package/dist/distributions/multinomial.js +54 -11
  62. package/dist/distributions/nbeta.js +62 -11
  63. package/dist/distributions/nchisq.js +30 -6
  64. package/dist/distributions/negbinomial.js +94 -17
  65. package/dist/distributions/normal.js +9 -4
  66. package/dist/distributions/nt.js +392 -17
  67. package/dist/distributions/pareto.js +4 -1
  68. package/dist/distributions/poisson.js +28 -3
  69. package/dist/distributions/signrank.js +7 -4
  70. package/dist/distributions/summary.js +450 -60
  71. package/dist/distributions/triangular.js +10 -5
  72. package/dist/distributions/tukey.js +5 -2
  73. package/dist/distributions/weibull.js +10 -0
  74. package/dist/distributions/wilcox.d.ts +9 -3
  75. package/dist/distributions/wilcox.js +155 -26
  76. package/dist/distributions.js +2 -2
  77. package/dist/htest/agreement.d.ts +3 -2
  78. package/dist/htest/agreement.js +76 -14
  79. package/dist/htest/ansariTest.d.ts +6 -1
  80. package/dist/htest/ansariTest.js +75 -8
  81. package/dist/htest/aov.d.ts +1 -1
  82. package/dist/htest/aov.js +48 -34
  83. package/dist/htest/binomTest.d.ts +5 -0
  84. package/dist/htest/binomTest.js +41 -17
  85. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  86. package/dist/htest/brunnerMunzelTest.js +7 -5
  87. package/dist/htest/chisqTest.d.ts +2 -1
  88. package/dist/htest/chisqTest.js +130 -43
  89. package/dist/htest/cor.d.ts +2 -1
  90. package/dist/htest/cor.js +17 -19
  91. package/dist/htest/core/ansari.d.ts +9 -0
  92. package/dist/htest/core/ansari.js +21 -1
  93. package/dist/htest/core/kolmogorov.d.ts +25 -0
  94. package/dist/htest/core/kolmogorov.js +123 -0
  95. package/dist/htest/core/mantelhaen.js +6 -4
  96. package/dist/htest/core/shapiro.d.ts +1 -1
  97. package/dist/htest/core/shapiro.js +5 -1
  98. package/dist/htest/dunnett.js +14 -5
  99. package/dist/htest/effectSize.d.ts +2 -0
  100. package/dist/htest/effectSize.js +91 -29
  101. package/dist/htest/fisherTest.d.ts +2 -2
  102. package/dist/htest/fisherTest.js +36 -18
  103. package/dist/htest/groups.d.ts +7 -0
  104. package/dist/htest/groups.js +21 -4
  105. package/dist/htest/ksTest.d.ts +3 -1
  106. package/dist/htest/ksTest.js +23 -2
  107. package/dist/htest/leveneTest.js +19 -3
  108. package/dist/htest/mantelhaenTest.js +125 -25
  109. package/dist/htest/mcnemarTest.js +32 -11
  110. package/dist/htest/multipleComparisons.d.ts +8 -1
  111. package/dist/htest/multipleComparisons.js +44 -21
  112. package/dist/htest/normalityTests.js +44 -10
  113. package/dist/htest/onewayTest.js +96 -5
  114. package/dist/htest/pAdjust.d.ts +3 -2
  115. package/dist/htest/pAdjust.js +38 -6
  116. package/dist/htest/pairwise.d.ts +1 -1
  117. package/dist/htest/pairwise.js +17 -15
  118. package/dist/htest/poissonTest.d.ts +1 -1
  119. package/dist/htest/poissonTest.js +111 -59
  120. package/dist/htest/polycor.js +27 -7
  121. package/dist/htest/power.d.ts +11 -1
  122. package/dist/htest/power.js +112 -8
  123. package/dist/htest/propTest.js +21 -4
  124. package/dist/htest/pwr.js +121 -24
  125. package/dist/htest/rankTests.js +29 -5
  126. package/dist/htest/tTest.js +44 -21
  127. package/dist/htest/trendTests.d.ts +7 -3
  128. package/dist/htest/trendTests.js +114 -20
  129. package/dist/htest/types.d.ts +5 -0
  130. package/dist/htest/types.js +7 -0
  131. package/dist/htest/varTest.js +45 -14
  132. package/dist/htest/wilcoxTest.js +114 -21
  133. package/dist/integrate/quadpack.js +27 -5
  134. package/dist/internal/covmat.d.ts +5 -0
  135. package/dist/internal/covmat.js +20 -0
  136. package/dist/internal/ddexp.d.ts +15 -1
  137. package/dist/internal/ddexp.js +59 -6
  138. package/dist/internal/discrete.d.ts +14 -0
  139. package/dist/internal/discrete.js +78 -5
  140. package/dist/internal/extremes.d.ts +9 -0
  141. package/dist/internal/extremes.js +24 -0
  142. package/dist/internal/fround.js +13 -4
  143. package/dist/internal/integer.d.ts +9 -0
  144. package/dist/internal/integer.js +12 -0
  145. package/dist/internal/invert.js +15 -2
  146. package/dist/internal/scaledInverse.d.ts +10 -0
  147. package/dist/internal/scaledInverse.js +22 -0
  148. package/dist/internal/seq.js +15 -2
  149. package/dist/internal/summary.d.ts +90 -0
  150. package/dist/internal/summary.js +458 -39
  151. package/dist/internal/uniroot.d.ts +7 -0
  152. package/dist/internal/uniroot.js +28 -3
  153. package/dist/linalg/basic.d.ts +3 -1
  154. package/dist/linalg/basic.js +51 -7
  155. package/dist/linalg/chol.js +4 -1
  156. package/dist/linalg/dot.d.ts +29 -0
  157. package/dist/linalg/dot.js +132 -0
  158. package/dist/linalg/eigen.d.ts +6 -1
  159. package/dist/linalg/eigen.js +71 -14
  160. package/dist/linalg/expm.d.ts +3 -2
  161. package/dist/linalg/expm.js +109 -15
  162. package/dist/linalg/kappa.js +58 -10
  163. package/dist/linalg/lu.d.ts +2 -1
  164. package/dist/linalg/lu.js +35 -5
  165. package/dist/linalg/matrix.d.ts +14 -0
  166. package/dist/linalg/matrix.js +24 -0
  167. package/dist/linalg/ops.js +60 -13
  168. package/dist/linalg/qr.d.ts +2 -1
  169. package/dist/linalg/qr.js +91 -23
  170. package/dist/linalg/schur.js +11 -3
  171. package/dist/linalg/solve.d.ts +6 -1
  172. package/dist/linalg/solve.js +147 -26
  173. package/dist/linalg/svd.d.ts +9 -2
  174. package/dist/linalg/svd.js +29 -11
  175. package/dist/linalg.js +2 -2
  176. package/dist/mixed.js +4 -4
  177. package/dist/models/anovaSequential.d.ts +16 -1
  178. package/dist/models/anovaSequential.js +124 -25
  179. package/dist/models/aovEz.js +140 -30
  180. package/dist/models/aovStrata.js +55 -22
  181. package/dist/models/carAnova.d.ts +1 -1
  182. package/dist/models/carAnova.js +83 -32
  183. package/dist/models/coxph.js +148 -66
  184. package/dist/models/estimable.d.ts +15 -0
  185. package/dist/models/estimable.js +68 -0
  186. package/dist/models/formula.d.ts +1 -1
  187. package/dist/models/formula.js +26 -8
  188. package/dist/models/glm.d.ts +35 -5
  189. package/dist/models/glm.js +398 -55
  190. package/dist/models/glmAnova.js +84 -13
  191. package/dist/models/glmConfint.d.ts +1 -1
  192. package/dist/models/glmConfint.js +115 -27
  193. package/dist/models/glmFamily.d.ts +10 -0
  194. package/dist/models/glmFamily.js +244 -40
  195. package/dist/models/lm.d.ts +12 -1
  196. package/dist/models/lm.js +235 -36
  197. package/dist/models/lmer.d.ts +20 -1
  198. package/dist/models/lmer.js +109 -27
  199. package/dist/models/lmerAnova.js +37 -4
  200. package/dist/models/lmerHypotheses.d.ts +6 -2
  201. package/dist/models/lmerHypotheses.js +14 -2
  202. package/dist/models/lmerTest.d.ts +9 -2
  203. package/dist/models/lmerTest.js +64 -19
  204. package/dist/models/mauchly.d.ts +4 -1
  205. package/dist/models/mauchly.js +34 -13
  206. package/dist/models/mixedStructure.js +6 -0
  207. package/dist/models/modelFrame.js +8 -2
  208. package/dist/models/modelMatrix.d.ts +3 -1
  209. package/dist/models/modelMatrix.js +40 -15
  210. package/dist/models/scaledTables.d.ts +8 -0
  211. package/dist/models/scaledTables.js +6 -0
  212. package/dist/models/survival.d.ts +6 -1
  213. package/dist/models/survival.js +67 -27
  214. package/dist/models/tiedTimes.d.ts +2 -0
  215. package/dist/models/tiedTimes.js +34 -0
  216. package/dist/numerics/deriv.d.ts +1 -1
  217. package/dist/numerics/deriv.js +64 -8
  218. package/dist/numerics/integrate.d.ts +9 -1
  219. package/dist/numerics/integrate.js +103 -16
  220. package/dist/numerics/interp.d.ts +8 -2
  221. package/dist/numerics/interp.js +352 -50
  222. package/dist/numerics/ode.js +55 -1
  223. package/dist/numerics.js +2 -2
  224. package/dist/optim/localmin.js +4 -0
  225. package/dist/optim/optim.d.ts +3 -1
  226. package/dist/optim/optim.js +80 -8
  227. package/dist/random/beta.js +53 -4
  228. package/dist/random/binomial.d.ts +14 -3
  229. package/dist/random/binomial.js +59 -24
  230. package/dist/random/continuous.js +33 -5
  231. package/dist/random/extra.js +42 -9
  232. package/dist/random/gamma.d.ts +4 -1
  233. package/dist/random/gamma.js +36 -8
  234. package/dist/random/hyper.js +9 -6
  235. package/dist/random/mt.js +17 -2
  236. package/dist/random/poisson.js +26 -3
  237. package/dist/random/r2dtable.d.ts +8 -1
  238. package/dist/random/r2dtable.js +24 -3
  239. package/dist/random/rank.d.ts +2 -0
  240. package/dist/random/rank.js +44 -4
  241. package/dist/random/sample.d.ts +12 -2
  242. package/dist/random/sample.js +57 -20
  243. package/dist/random/streams.js +4 -2
  244. package/dist/rstats.js +14 -14
  245. package/dist/special/besselIK.d.ts +12 -0
  246. package/dist/special/besselIK.js +168 -23
  247. package/dist/special/besselJY.d.ts +11 -1
  248. package/dist/special/besselJY.js +127 -19
  249. package/dist/special/besselSeries.d.ts +27 -0
  250. package/dist/special/besselSeries.js +45 -2
  251. package/dist/special/beta.js +4 -1
  252. package/dist/special/choose.js +17 -8
  253. package/dist/special/dd.js +122 -18
  254. package/dist/special/extra.js +14 -2
  255. package/dist/special/gamma.js +11 -2
  256. package/dist/special/incbet.js +30 -4
  257. package/dist/special/incbetTemme.d.ts +7 -0
  258. package/dist/special/incbetTemme.js +45 -12
  259. package/dist/special/loader.d.ts +1 -1
  260. package/dist/special/loader.js +117 -20
  261. package/dist/special/log1pmx.js +4 -0
  262. package/dist/special.js +2 -2
  263. package/dist/tests.js +11 -11
  264. package/llms.txt +12 -6
  265. package/package.json +1 -1
  266. package/dist/chunks/chunk-3THFTB36.js +0 -2
  267. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  268. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  269. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  270. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  271. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  272. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  273. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  274. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  275. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  276. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  277. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  278. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  279. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  280. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  281. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  282. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  283. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  284. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  285. package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
@@ -79,6 +79,9 @@ function validParams(df, ncp) {
79
79
  * A value near 1, and its log, are built from the small value of the other side
80
80
  */
81
81
  function pnchisqBoth(x, df, ncp, lower) {
82
+ // the end point (the asymptotic forms do not end for x = Infinity: qchisq's bracket reached it; issue #157)
83
+ if (x === Infinity)
84
+ return lower ? { lin: 1, log: 0 } : { lin: 0, log: -Infinity };
82
85
  const mu = ncp / 2;
83
86
  // The series has too many terms (or gammaInc is slow for its huge shapes): integrate an asymptotic form of the density
84
87
  const method = asymptoticMethod(df, besselArg(x, ncp));
@@ -91,8 +94,12 @@ function pnchisqBoth(x, df, ncp, lower) {
91
94
  const x2 = 2 * (x / 2);
92
95
  const e = Math.expm1((df / 2) * lt);
93
96
  const lo = pnchisqBoth(x2, df, ncp, true);
94
- if (lower)
95
- return { lin: lo.lin * (1 + e), log: lo.log + (df / 2) * lt };
97
+ if (lower) {
98
+ // (a factor beyond the doubles, for a large df, times a probability that underflowed to 0 made NaN: from the log;
99
+ // issue #115)
100
+ const l = lo.log + (df / 2) * lt;
101
+ return { lin: 1 + e < Infinity ? lo.lin * (1 + e) : Math.exp(l), log: l };
102
+ }
96
103
  const up = pnchisqBoth(x2, df, ncp, false);
97
104
  const r = (lo.lin * e) / up.lin;
98
105
  return Number.isFinite(r) ? { lin: up.lin - lo.lin * e, log: up.log + Math.log1p(-r) } : up;
@@ -163,7 +170,19 @@ function dnchisqRaw(x, df, ncp, log) {
163
170
  if (lt !== undefined) {
164
171
  // the density at x2 times (x/x2)^(k/2 - 1) (subnormalHalf)
165
172
  const x2 = 2 * (x / 2);
166
- return log ? dnchisqRaw(x2, df, ncp, true) + (a - 1) * lt : dnchisqRaw(x2, df, ncp, false) * Math.exp((a - 1) * lt);
173
+ // The j = 1 component (power y^a) is not negligible against j = 0 (y^(a - 1)) where a is not large against mu y:
174
+ // their ratio at x2 is rho = a/(mu y2), and the density at x is that at x2 times r^(a - 1) (rho + r)/(rho + 1),
175
+ // r = x/x2. For df = 0 (no j = 0 term, rho = 0) the factor is r^a = 1. Where rho > 2^60 the change is below
176
+ // double precision and the factor is r^(a - 1) alone (issue #147)
177
+ const rho = a / (x2 / 2) / mu;
178
+ const mix = rho < 2 ** 60 ? Math.log1p((x - x2) / x2 / (rho + 1)) : 0;
179
+ if (log)
180
+ return dnchisqRaw(x2, df, ncp, true) + (a - 1) * lt + mix;
181
+ // (where the factor overflows, for a large df, the density at x2 has underflowed to 0: from the log; issue #115)
182
+ const f = Math.exp((a - 1) * lt + mix);
183
+ return f < Infinity
184
+ ? dnchisqRaw(x2, df, ncp, false) * f
185
+ : Math.exp(dnchisqRaw(x2, df, ncp, true) + (a - 1) * lt + mix);
167
186
  }
168
187
  // The ratio of terms w_j g_j is mu y / (j (a + j - 1)) = (z/2)² / (j (a + j - 1)); the peak is at the j where it
169
188
  // equals 1, the positive root of j² + (a - 1) j - (z/2)² (written without cancellation for a > 1)
@@ -630,8 +649,9 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
630
649
  }
631
650
  else
632
651
  x0 = c * qgamma1(logTarget, nu / 2, 2, lower, true);
652
+ // (df + ncp overflows where the quantile can lie beyond the doubles; issue #157)
633
653
  if (!(x0 > 0) || !Number.isFinite(x0))
634
- x0 = df + ncp;
654
+ x0 = Math.min(df + ncp, Number.MAX_VALUE);
635
655
  const passes = (x) => (lower ? cdf(x, true).log >= logTarget : cdf(x, false).log <= logTarget);
636
656
  const narrowSpread = Math.sqrt(2 * (df + 2 * ncp)) < 1e-10 * (df + ncp);
637
657
  const narrow = narrowSpread ? narrowQuantile(x0, Number.MAX_VALUE, passes) : undefined;
@@ -639,10 +659,14 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
639
659
  return narrow;
640
660
  // Bracket by doubling the upper end (the distribution function is expensive for very large x)
641
661
  let lo = 0;
642
- let hi = Math.max(x0, df + ncp, 1);
662
+ // up to the largest double: not passing the target there, the quantile is beyond it (issue #157: Infinity was
663
+ // reached and the distribution function did not end)
664
+ let hi = Math.min(Math.max(x0, df + ncp, 1), Number.MAX_VALUE);
643
665
  for (let i = 0; i < 2000 && !passes(hi); i++) {
666
+ if (hi === Number.MAX_VALUE)
667
+ return Infinity;
644
668
  lo = hi;
645
- hi *= 2;
669
+ hi = Math.min(hi * 2, Number.MAX_VALUE);
646
670
  }
647
671
  const x = invertContinuous({
648
672
  logTarget,
@@ -7,29 +7,57 @@
7
7
  // When mu is given, p = n / (n + mu) and q = mu / (n + mu) are formed in double-double
8
8
  // (for n ≫ mu, rounding 1 - p gives q a large relative error). size = Inf is the Poisson distribution.
9
9
  // Quantiles use an integer search from a Cornish-Fisher starting value.
10
+ import { logAddExp, logSubExp } from "../internal/ddexp.js";
10
11
  import { vectorize } from "../internal/vectorize.js";
11
12
  import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
12
13
  import { discreteQuantile } from "../internal/discrete.js";
13
- import { nonInt } from "../internal/integer.js";
14
- import { betaIncDD } from "../special/incbet.js";
14
+ import { countAtMost, nonInt } from "../internal/integer.js";
15
+ import { betaIncDD, betaIncTinyX } from "../special/incbet.js";
15
16
  import { prefactorParts } from "../special/incbetPrefactor.js";
16
17
  import { dbinomRawDD, dpoisRaw } from "../special/loader.js";
17
- import { ddAdd, ddDiv, ddMul, twoSum } from "../special/dd.js";
18
+ import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, twoSum } from "../special/dd.js";
18
19
  import { qnorm } from "./normal.js";
19
20
  import { qgamma1 } from "./gamma.js";
20
21
  import { ppois1, qpois1 } from "./poisson.js";
21
22
  import { probPair } from "./geometric.js";
22
23
  function fromMu(size, mu) {
23
24
  const s = twoSum(size, mu);
24
- return { size, pd: ddDiv([size, 0], s), qd: ddDiv([mu, 0], s) };
25
+ if (Number.isFinite(s[0]))
26
+ return { size, pd: ddDiv([size, 0], s), qd: ddDiv([mu, 0], s) };
27
+ // size + mu overflows (both near the largest double): from the ratio r of the smaller to the larger, 1 / (1 + r)
28
+ // and r / (1 + r) (p and q were both 0)
29
+ const big = size >= mu;
30
+ const r = big ? mu / size : size / mu;
31
+ const one = ddDiv([1, 0], twoSum(1, r));
32
+ const other = ddDiv([r, 0], twoSum(1, r));
33
+ return big ? { size, pd: one, qd: other } : { size, pd: other, qd: one };
34
+ }
35
+ /**
36
+ * fromMu for the density: where the smaller of p and q falls below the normal range (0 for a ratio below 2^-1074:
37
+ * dnbinom(1, 1e-200, { mu: 1e200 }) was 0 for 1e-200, dnbinom(1, MAX, { mu: MIN, log: true }) -Inf for -744.44;
38
+ * issue #95), it is kept times 2^1100 (from size 2^550 / ((size + mu) 2^-550), exact powers of 2), a normal number,
39
+ * and dbinomRawDD takes the shift. Otherwise as fromMu
40
+ */
41
+ function fromMuDensity(size, mu) {
42
+ const par = fromMu(size, mu);
43
+ const pSmall = par.pd[0] < MIN_NORMAL;
44
+ if (!pSmall && !(par.qd[0] < MIN_NORMAL))
45
+ return par;
46
+ const s = twoSum(size, mu);
47
+ const scaled = ddDiv([(pSmall ? size : mu) * 2 ** 550, 0], [s[0] * 2 ** -550, s[1] * 2 ** -550]);
48
+ return pSmall ? { ...par, pd: scaled, shift: [1100, 0] } : { ...par, qd: scaled, shift: [0, 1100] };
25
49
  }
26
50
  function fromProb(size, prob) {
27
51
  return { size, ...probPair(prob) };
28
52
  }
29
53
  /** P[X = x] (x a nonnegative integer, 0 < size < ∞, 0 < p < 1) */
30
54
  function dnbinomRaw(x, par, log) {
31
- const { size, pd, qd } = par;
32
- const b = dbinomRawDD(size, x, pd, qd, log);
55
+ const { size, pd, qd, shift } = par;
56
+ // size + x (the trials of the binomial density below) overflows: not computed (it gave 0, and NaN with log; the
57
+ // density, 2.8e-155 at size = x = 1e308, would not agree with pnbinom there, as recorded in round 22)
58
+ if (!Number.isFinite(size + x))
59
+ return NaN;
60
+ const b = dbinomRawDD(size, x, pd, qd, log, undefined, shift);
33
61
  if (x === 0)
34
62
  return b;
35
63
  const r = size / (size + x);
@@ -40,8 +68,48 @@ function dnbinomRaw(x, par, log) {
40
68
  return log ? b + (Math.log(size) - Math.log(size + x)) : (b / (size + x)) * size;
41
69
  }
42
70
  const MIN_NORMAL = 2 ** -1022;
71
+ const LN2_DD = [0.6931471805599453, 2.3190468138462996e-17];
72
+ /** A probability kept times 2^k (fromMuDensity): its value (0 below the doubles) and its log in double-double */
73
+ function unshift(v, k) {
74
+ return { value: v[0] * 2 ** -(k / 2) * 2 ** -(k / 2), log: ddSub(ddLog(v), ddMulNum(LN2_DD, k)) };
75
+ }
43
76
  /** P[X <= x] (lower) / P[X > x] (!lower) (x a nonnegative integer) */
44
77
  function pnbinomRaw(x, par, lower, log) {
78
+ if (par.shift !== undefined) {
79
+ // p or q below the normal range (it was 0, and the incomplete beta took its end: pnbinom(0, MIN, { mu: MAX }) was 0
80
+ // for 1, qnbinom(0.5, ...) Infinity for 0; issue #103): I_p(size, x + 1) by the series for x below DBL_MIN, from
81
+ // the exact log of p (or of q, the other tail); (x + 1) p < 4 there, as the series needs
82
+ const [kp, kq] = par.shift;
83
+ // (the quantile search may ask for x = Infinity when its first step from the largest double overflows)
84
+ if (x === Infinity)
85
+ return lower ? (log ? 0 : 1) : log ? -Infinity : 0;
86
+ const r = kp > 0
87
+ ? (({ value, log: lp }) => betaIncTinyX(par.size, x + 1, value, lp, lower, log))(unshift(par.pd, kp))
88
+ : (({ value, log: lq }) => betaIncTinyX(x + 1, par.size, value, lq, !lower, log))(unshift(par.qd, kq));
89
+ return log ? r[0] : r[1];
90
+ }
91
+ if (x + 1 - x !== 1) {
92
+ // Beyond 2^53, x + 1 rounds (to x, or up to x + 2) and I_p(size, x + 1) became I_p(size, x) (pnbinom(1e16, 1e16, 0.5) was 0.5; R too).
93
+ // Without x + 1: I_p(size, x + 1) = I_p(size, x) + dnbinom(x)
94
+ let v;
95
+ let term;
96
+ if (!log) {
97
+ const t = betaIncDD(par.size, x, par.pd, par.qd, lower, false);
98
+ term = dnbinomRaw(x, par, false);
99
+ v = lower ? t + term : t - term;
100
+ }
101
+ else {
102
+ const lt = betaIncDD(par.size, x, par.pd, par.qd, lower, true);
103
+ term = dnbinomRaw(x, par, true);
104
+ v = lower ? logAddExp(lt, term) : logSubExp(lt, term);
105
+ }
106
+ if (Number.isNaN(term))
107
+ return NaN;
108
+ // The upper tail as a difference: where dnbinom is not resolved against it (both logs near -3.5e48, or the plain
109
+ // difference cancels), one unit of x is below the resolution of the result, and the rounded parameter below serves
110
+ if (lower || (log ? Number.isFinite(v) : v > 0))
111
+ return v;
112
+ }
45
113
  const up = upperSeries(x, par);
46
114
  if (up !== undefined) {
47
115
  // The upper tail (the smaller side) was computed directly by the series
@@ -95,10 +163,18 @@ function upperSeries(x, par) {
95
163
  return undefined;
96
164
  }
97
165
  function quantileRaw(p, par, lowerTail, logP) {
98
- // Starting value from the Cornish-Fisher expansion
99
- const prob = par.pd[0] + par.pd[1];
166
+ // Starting value from the Cornish-Fisher expansion (p or q kept times 2^k: scaled back, 0 below the doubles; #103)
167
+ const [kp, kq] = par.shift ?? [0, 0];
168
+ const prob = kp > 0 ? unshift(par.pd, kp).value : par.pd[0] + par.pd[1];
169
+ const qv = kq > 0 ? unshift(par.qd, kq).value : par.qd[0] + par.qd[1];
170
+ if (kp > 0) {
171
+ // X p is nearly gamma (shape = size): y0 = g / p from the logs (p may be 0 here)
172
+ const g = qgamma1(p, par.size, 1, lowerTail, logP);
173
+ const y0 = g > 0 ? Math.min(Math.exp(Math.log(g) - unshift(par.pd, kp).log[0]), Number.MAX_VALUE) : 0;
174
+ return discreteQuantile({ p, lowerTail, logP, cdf: (y, l, lg) => pnbinomRaw(y, par, l, lg), y0, lo: 0, hi: Infinity });
175
+ }
100
176
  const Q = 1 / prob;
101
- const P = (par.qd[0] + par.qd[1]) * Q;
177
+ const P = qv * Q;
102
178
  const mu = par.size * P;
103
179
  const sigma = Math.sqrt(par.size * P * Q);
104
180
  const gamma = (Q + P) / sigma;
@@ -108,7 +184,7 @@ function quantileRaw(p, par, lowerTail, logP) {
108
184
  // this is a much closer starting value than Cornish-Fisher, reducing distribution-function evaluations
109
185
  // -log q = log(1 + p/q) (taking log of a rounded q loses digits for small p)
110
186
  const g = qgamma1(p, par.size, 1, lowerTail, logP);
111
- y0 = g / Math.log1p(prob / (par.qd[0] + par.qd[1]));
187
+ y0 = g / Math.log1p(prob / qv);
112
188
  // A finite gamma quantile divided by a tiny -log q beyond the doubles: so is the quantile (the distribution
113
189
  // function cannot be evaluated out there; qnbinom(2.2e-284, 2.7e37, { prob: 1.9e-279 }) threw a TypeError)
114
190
  if (y0 === Infinity && g > 0 && Number.isFinite(g))
@@ -201,7 +277,7 @@ export function pnbinom1(q, size, prob, lowerTail, logP) {
201
277
  return dt0(lowerTail, logP);
202
278
  if (size === 0 || prob === 1 || !Number.isFinite(q))
203
279
  return dt1(lowerTail, logP);
204
- return pnbinomRaw(Math.floor(q + 1e-7), fromProb(size, prob), lowerTail, logP);
280
+ return pnbinomRaw(countAtMost(q), fromProb(size, prob), lowerTail, logP);
205
281
  }
206
282
  export function qnbinom1(p, size, prob, lowerTail, logP) {
207
283
  if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(prob))
@@ -242,7 +318,7 @@ export function dnbinomMu1(x, size, mu, log) {
242
318
  return dpoisRaw(n, mu, log);
243
319
  if (mu === 0)
244
320
  return n === 0 ? (log ? 0 : 1) : d0(log);
245
- return dnbinomRaw(n, fromMu(size, mu), log);
321
+ return dnbinomRaw(n, fromMuDensity(size, mu), log);
246
322
  }
247
323
  export function pnbinomMu1(q, size, mu, lowerTail, logP) {
248
324
  if (Number.isNaN(q) || Number.isNaN(size) || Number.isNaN(mu))
@@ -255,25 +331,26 @@ export function pnbinomMu1(q, size, mu, lowerTail, logP) {
255
331
  return dt1(lowerTail, logP);
256
332
  if (size === Infinity)
257
333
  return ppois1(q, mu, lowerTail, logP);
258
- return pnbinomRaw(Math.floor(q + 1e-7), fromMu(size, mu), lowerTail, logP);
334
+ return pnbinomRaw(countAtMost(q), fromMuDensity(size, mu), lowerTail, logP);
259
335
  }
260
336
  export function qnbinomMu1(p, size, mu, lowerTail, logP) {
261
337
  if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(mu))
262
338
  return NaN;
263
- // As in R, size = Inf is the Poisson distribution, and size = 0 or mu = 0 gives 0 regardless of the other parameter or p
339
+ // A negative size or mu defines no distribution, as in dnbinom and pnbinom (R's qnbinom returns 0 for mu = 0 before
340
+ // checking them); size = Inf is the Poisson distribution, and size = 0 or mu = 0 gives 0 regardless of p
341
+ if (size < 0 || mu < 0)
342
+ return NaN;
264
343
  if (size === Infinity)
265
344
  return qpois1(p, mu, lowerTail, logP);
266
345
  if (size === 0 || mu === 0)
267
346
  return 0;
268
- if (size < 0 || mu < 0)
269
- return NaN;
270
347
  // mu = Inf answers only at the ends (p = 0, 1) and is NaN otherwise (as in R)
271
348
  if (mu === Infinity)
272
349
  return quantileBoundaries(p, 0, Infinity, lowerTail, logP) ?? NaN;
273
350
  const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
274
351
  if (b !== undefined)
275
352
  return b;
276
- return quantileRaw(p, fromMu(size, mu), lowerTail, logP);
353
+ return quantileRaw(p, fromMuDensity(size, mu), lowerTail, logP);
277
354
  }
278
355
  /** Accept exactly one of prob and mu (as in R, giving both or neither is an error) */
279
356
  function which(options) {
@@ -6,6 +6,7 @@
6
6
  import { erf, erfcxNonNegative } from "../special/erf.js";
7
7
  import { ndtri, ndtriExp } from "../special/ndtri.js";
8
8
  import { vectorize } from "../internal/vectorize.js";
9
+ import { affine, standardizeValue } from "../internal/ddexp.js";
9
10
  const SQRT1_2 = Math.SQRT1_2;
10
11
  /**
11
12
  * exp(-a^2 / 2) (a >= 0) multiplied by scale.
@@ -41,7 +42,9 @@ function pnormStd(t, logP) {
41
42
  if (t > -1)
42
43
  return Math.log1p(-lowerTailProb(-t));
43
44
  const a = -t;
44
- return Math.log(0.5 * erfcxNonNegative(a * SQRT1_2)) - (a * a) / 2;
45
+ // (a / 2) a, not (a a) / 2: the same value, without the overflow of a a above 1.34e154 (pnorm(-1.5e154, { logP: true })
46
+ // was -Infinity for -1.125e308; issue #33)
47
+ return Math.log(0.5 * erfcxNonNegative(a * SQRT1_2)) - (a / 2) * a;
45
48
  }
46
49
  function pnorm1(q, mean, sd, lowerTail, logP) {
47
50
  if (Number.isNaN(q) || Number.isNaN(mean) || Number.isNaN(sd))
@@ -54,7 +57,8 @@ function pnorm1(q, mean, sd, lowerTail, logP) {
54
57
  return NaN;
55
58
  if (sd === 0)
56
59
  return q < mean ? p0 : p1;
57
- const x = (q - mean) / sd;
60
+ // (q - mean) / sd, also where q - mean overflows but the standardized value does not
61
+ const x = standardizeValue(q, mean, sd);
58
62
  if (!Number.isFinite(x))
59
63
  return q < mean ? p0 : p1;
60
64
  return pnormStd(lowerTail ? x : -x, logP);
@@ -85,7 +89,8 @@ function qnorm1(p, mean, sd, lowerTail, logP) {
85
89
  if (sd === 0)
86
90
  return mean;
87
91
  const z = logP ? ndtriLogRefined(p) : ndtri(p);
88
- return mean + sd * (lowerTail ? z : -z);
92
+ // mean + sd z, also where sd z overflows but the quantile does not
93
+ return affine(mean, sd, lowerTail ? z : -z);
89
94
  }
90
95
  const SQRT_2_OVER_PI = 0.797884560802865355879892119869;
91
96
  /**
@@ -118,7 +123,7 @@ function dnorm1(x, mean, sd, log) {
118
123
  return NaN; // Inf - Inf
119
124
  if (sd === 0)
120
125
  return x === mean ? Infinity : log ? -Infinity : 0;
121
- const z = (x - mean) / sd;
126
+ const z = standardizeValue(x, mean, sd);
122
127
  if (!Number.isFinite(z))
123
128
  return log ? -Infinity : 0;
124
129
  if (log)