@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
@@ -16,7 +16,7 @@
16
16
  // (asymptoticMethod). Quantiles whose distribution function steps within a few doubles end in a search over the doubles.
17
17
  import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
18
18
  import { invertContinuous } from "../internal/invert.js";
19
- import { kronrodAdaptive, narrowQuantile, quadratureControl, searchDoubles } from "../internal/noncentral.js";
19
+ import { kronrodAdaptive, narrowQuantile, negligibleNcp, quadratureControl, searchDoubles } from "../internal/noncentral.js";
20
20
  import { dpoisLogDD, mixtureCdf, mixtureDensity, mixtureTail } from "../internal/poissonMixture.js";
21
21
  import { gammaInc } from "../special/igam.js";
22
22
  import { dpoisRaw } from "../special/loader.js";
@@ -27,11 +27,13 @@ import { log1pmx } from "../special/log1pmx.js";
27
27
  import { dgamma1, qgamma1 } from "./gamma.js";
28
28
  import { qnorm } from "./normal.js";
29
29
  function gammaKernel(a, y) {
30
- // For very small y the ratio y/(a+j) underflows, so keep it scaled by 2^500
30
+ // For very small y the ratio y/(a+j) underflows, so keep it scaled by 2^500; for a huge y scaled by 2^-500 (the
31
+ // splitting in the double-double quotient overflowed: dchisq(3e307, 0.5, { ncp: 2.2e-320 }) was NaN)
31
32
  const tiny = y < 1e-250;
32
- const ys = tiny ? y * 2 ** 500 : y;
33
+ const huge = y > 1e250;
34
+ const ys = tiny ? y * 2 ** 500 : huge ? y * 2 ** -500 : y;
33
35
  return {
34
- ratioK: tiny ? -1 : 0,
36
+ ratioK: tiny ? -1 : huge ? 1 : 0,
35
37
  cdf: (j, lower, log) => gammaInc(a + j, y, lower, log),
36
38
  inc: (j, log) => {
37
39
  const [s, corr] = incCorrection(a, j, y);
@@ -77,11 +79,31 @@ function validParams(df, ncp) {
77
79
  * A value near 1, and its log, are built from the small value of the other side
78
80
  */
79
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 };
80
85
  const mu = ncp / 2;
81
86
  // The series has too many terms (or gammaInc is slow for its huge shapes): integrate an asymptotic form of the density
82
87
  const method = asymptoticMethod(df, besselArg(x, ncp));
83
88
  if (method !== undefined)
84
89
  return method === "hankel" ? besselTails(x, df, ncp, lower) : debyeTails(x, df, ncp, lower);
90
+ const lt = subnormalHalf(x);
91
+ if (lt !== undefined) {
92
+ // P[X <= x] = P[X <= x2] (x/x2)^(df/2) (see subnormalHalf); the upper tail as Q(x2) - P(x2) ((x/x2)^(df/2) - 1),
93
+ // where the change is small beside Q(x2) (it is at most of the order of df/2, Q(x2) at least of df/2 log(2/x))
94
+ const x2 = 2 * (x / 2);
95
+ const e = Math.expm1((df / 2) * lt);
96
+ const lo = pnchisqBoth(x2, df, ncp, true);
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
+ }
103
+ const up = pnchisqBoth(x2, df, ncp, false);
104
+ const r = (lo.lin * e) / up.lin;
105
+ return Number.isFinite(r) ? { lin: up.lin - lo.lin * e, log: up.log + Math.log1p(-r) } : up;
106
+ }
85
107
  // x so small that x/2 underflows to 0 (the smallest subnormal) is approximated by the smallest positive number
86
108
  const kernel = gammaKernel(df / 2, Math.max(x / 2, Number.MIN_VALUE));
87
109
  if (mu > 1e8 && !lower) {
@@ -100,6 +122,18 @@ function pnchisqBoth(x, df, ncp, lower) {
100
122
  // (the lower tail is likely the smaller below the mean k + λ)
101
123
  return mixtureTail(mu, kernel, lower, x < df + ncp);
102
124
  }
125
+ /**
126
+ * log(x / x2) for a subnormal x with an odd last bit, where y = x/2 rounds to x2/2 (by 2.5e-324: 1e-9 of x near 1e-315,
127
+ * 3% at 1.6e-322, more than the accuracy of the mixture; with its density, the differences of pchisq were 2e-6 off at
128
+ * pchisq(7.02e-315, 0.5, { ncp: 24.3 })); undefined where x/2 is exact or 0. With y that small, e^-y and the series
129
+ * of the components in y are 1 to double precision, so each component scales as y^(k/2 + j) (P[X <= x], density
130
+ * times y), and the mixture as y^(k/2) times 1 + O(λ |x - x2|) (j > 0 adds j to the power, with weights of order
131
+ * (λ x)^j; λ |x - x2| is below 1e-15): the values at x come from those at x2 by the factor (x/x2)^(k/2)
132
+ */
133
+ function subnormalHalf(x) {
134
+ const x2 = 2 * (x / 2);
135
+ return x2 > 0 && x2 !== x ? Math.log1p((x - x2) / x2) : undefined;
136
+ }
103
137
  function pnchisqRaw(x, df, ncp, lower, log) {
104
138
  const v = pnchisqBoth(x, df, ncp, lower);
105
139
  return log ? v.log : v.lin;
@@ -132,6 +166,24 @@ function dnchisqRaw(x, df, ncp, log) {
132
166
  const l = debyeAt(x, df, ncp).logf;
133
167
  return log ? l : Math.exp(l);
134
168
  }
169
+ const lt = subnormalHalf(x);
170
+ if (lt !== undefined) {
171
+ // the density at x2 times (x/x2)^(k/2 - 1) (subnormalHalf)
172
+ const x2 = 2 * (x / 2);
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);
186
+ }
135
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
136
188
  // equals 1, the positive root of j² + (a - 1) j - (z/2)² (written without cancellation for a > 1)
137
189
  const jStar = a > 1 ? (z * z) / 2 / (a - 1 + Math.hypot(a - 1, z)) : (-(a - 1) + Math.hypot(a - 1, z)) / 2;
@@ -175,6 +227,11 @@ function rootDiff(x, ncp) {
175
227
  }
176
228
  /** sqrt(x) in double-double (one Newton correction of the rounded root) */
177
229
  function sqrtDD(x) {
230
+ // (from x/4, scaled back exactly, near the largest double, where s² can round to Infinity)
231
+ if (x > 1e300) {
232
+ const [h, l] = sqrtDD(x / 4);
233
+ return [2 * h, 2 * l];
234
+ }
178
235
  const s = Math.sqrt(x);
179
236
  const [p, pe] = twoProd(s, s);
180
237
  return [s, (x - p - pe) / (2 * s)];
@@ -208,8 +265,9 @@ function besselAsymptotic(x, df, ncp, log, dGiven) {
208
265
  const d = dd[0] + dd[1];
209
266
  const [p, pe] = squareDD(dd);
210
267
  const hi = -p / 2;
211
- // (ν/2) log(x/λ) = ν log(sqrt(x)/sqrt(λ))
212
- const rest = -Math.LN2 - pe / 2 + nu * logRootRatio(d, rl, x) - 0.5 * Math.log(2 * Math.PI * z) + Math.log(s);
268
+ // (ν/2) log(x/λ) = ν log(sqrt(x)/sqrt(λ)); log sqrt(2πz) in two terms (2πz overflows near 1e308: the log density at
269
+ // dchisq(1.79e308, 3, { ncp: 1e308 }) was -Infinity)
270
+ const rest = -Math.LN2 - pe / 2 + nu * logRootRatio(d, rl, x) - LN_SQRT_2PI - 0.5 * Math.log(z) + Math.log(s);
213
271
  if (log)
214
272
  return hi + rest;
215
273
  return hi > -700 ? Math.exp(hi) * Math.exp(rest) : Math.exp(hi + rest);
@@ -343,10 +401,16 @@ function debyeAt(x, df, ncp, nGiven) {
343
401
  const z = besselArg(x, ncp);
344
402
  const R = Math.hypot(nu, z);
345
403
  const rn = R + nu;
346
- // (nGiven: N from the caller, for an x known more precisely than its double)
347
- const N = nGiven ?? ddSum(ddAdd(ddAdd(twoSum(ncp, df), [-2, 0]), [-x, 0]));
404
+ // (nGiven: N from the caller, for an x known more precisely than its double; in halves above x = 1e307, where the
405
+ // error terms of the double-double sums overflowed: pchisq(1.8e308, 3.3e268, { ncp: 6.9e307 }) was NaN)
406
+ const N = nGiven ??
407
+ (x > 1e307
408
+ ? 2 * ddSum(ddAdd(ddAdd(twoSum(ncp / 2, df / 2), [-1, 0]), [-x / 2, 0]))
409
+ : ddSum(ddAdd(ddAdd(twoSum(ncp, df), [-2, 0]), [-x, 0])));
348
410
  const D = x + z * (z / rn);
349
- const e = -(x / D) * (N / rn);
411
+ // (where D overflows, as x + λ x / (R + ν) can for x and λ near 1e308, x / D = (R + ν) / (R + ν + λ), in halves:
412
+ // pchisq(1e308, 3.3e268, { ncp: 6.9e307 }) was 0.55 and NaN beyond)
413
+ const e = D < Infinity ? -(x / D) * (N / rn) : -(N / 2) / (rn / 2 + ncp / 2);
350
414
  const ls = Math.log(debyeSeries(nu, nu / R));
351
415
  // log(1 + e) - e; far below the mode e rounds to -1, and log(1 + e) = log r is taken from x and ν + R
352
416
  const lm = e > -0.5 ? log1pmx(e) : Math.log(x) - Math.log(rn) - e;
@@ -393,8 +457,8 @@ function debyeTails(x, df, ncp, lower, nGiven) {
393
457
  a = b;
394
458
  w *= 2;
395
459
  }
396
- // g(r0) = f(x0) 2 R0
397
- const lt = logf + Math.log(2 * R0) + Math.log(sum);
460
+ // g(r0) = f(x0) 2 R0 (log 2 + log R0: 2 R0 overflows for x and λ near 1e308)
461
+ const lt = logf + Math.LN2 + Math.log(R0) + Math.log(sum);
398
462
  const tail = Math.exp(lt);
399
463
  if (up !== lower)
400
464
  return { lin: tail, log: lt };
@@ -417,6 +481,8 @@ export function dnchisq1(x, df, ncp, log) {
417
481
  return log ? -ncp / 2 - Math.LN2 : Math.exp(-ncp / 2) / 2;
418
482
  return log ? -Infinity : 0;
419
483
  }
484
+ if (negligibleNcp(ncp / 2, x / df))
485
+ return dgamma1(x, df / 2, 2, log);
420
486
  return dnchisqRaw(x, df, ncp, log);
421
487
  }
422
488
  /** The value of a double-double */
@@ -583,8 +649,9 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
583
649
  }
584
650
  else
585
651
  x0 = c * qgamma1(logTarget, nu / 2, 2, lower, true);
652
+ // (df + ncp overflows where the quantile can lie beyond the doubles; issue #157)
586
653
  if (!(x0 > 0) || !Number.isFinite(x0))
587
- x0 = df + ncp;
654
+ x0 = Math.min(df + ncp, Number.MAX_VALUE);
588
655
  const passes = (x) => (lower ? cdf(x, true).log >= logTarget : cdf(x, false).log <= logTarget);
589
656
  const narrowSpread = Math.sqrt(2 * (df + 2 * ncp)) < 1e-10 * (df + ncp);
590
657
  const narrow = narrowSpread ? narrowQuantile(x0, Number.MAX_VALUE, passes) : undefined;
@@ -592,10 +659,14 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
592
659
  return narrow;
593
660
  // Bracket by doubling the upper end (the distribution function is expensive for very large x)
594
661
  let lo = 0;
595
- 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);
596
665
  for (let i = 0; i < 2000 && !passes(hi); i++) {
666
+ if (hi === Number.MAX_VALUE)
667
+ return Infinity;
597
668
  lo = hi;
598
- hi *= 2;
669
+ hi = Math.min(hi * 2, Number.MAX_VALUE);
599
670
  }
600
671
  const x = invertContinuous({
601
672
  logTarget,
@@ -7,36 +7,109 @@
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);
36
- return log ? b + Math.log(r) : b * r;
64
+ if (r >= MIN_NORMAL)
65
+ return log ? b + Math.log(r) : b * r;
66
+ // A subnormal size / (size + x) has lost digits (dnbinom(58, 3.94e-315, { prob: 0.125, log: true }) was 7.5e-12 off,
67
+ // 2.4e-8 at x = 57): log size - log(size + x), and the division by size + x before the product with size
68
+ return log ? b + (Math.log(size) - Math.log(size + x)) : (b / (size + x)) * size;
69
+ }
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)) };
37
75
  }
38
76
  /** P[X <= x] (lower) / P[X > x] (!lower) (x a nonnegative integer) */
39
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
+ }
40
113
  const up = upperSeries(x, par);
41
114
  if (up !== undefined) {
42
115
  // The upper tail (the smaller side) was computed directly by the series
@@ -90,10 +163,18 @@ function upperSeries(x, par) {
90
163
  return undefined;
91
164
  }
92
165
  function quantileRaw(p, par, lowerTail, logP) {
93
- // Starting value from the Cornish-Fisher expansion
94
- 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
+ }
95
176
  const Q = 1 / prob;
96
- const P = (par.qd[0] + par.qd[1]) * Q;
177
+ const P = qv * Q;
97
178
  const mu = par.size * P;
98
179
  const sigma = Math.sqrt(par.size * P * Q);
99
180
  const gamma = (Q + P) / sigma;
@@ -103,7 +184,7 @@ function quantileRaw(p, par, lowerTail, logP) {
103
184
  // this is a much closer starting value than Cornish-Fisher, reducing distribution-function evaluations
104
185
  // -log q = log(1 + p/q) (taking log of a rounded q loses digits for small p)
105
186
  const g = qgamma1(p, par.size, 1, lowerTail, logP);
106
- y0 = g / Math.log1p(prob / (par.qd[0] + par.qd[1]));
187
+ y0 = g / Math.log1p(prob / qv);
107
188
  // A finite gamma quantile divided by a tiny -log q beyond the doubles: so is the quantile (the distribution
108
189
  // function cannot be evaluated out there; qnbinom(2.2e-284, 2.7e37, { prob: 1.9e-279 }) threw a TypeError)
109
190
  if (y0 === Infinity && g > 0 && Number.isFinite(g))
@@ -196,7 +277,7 @@ export function pnbinom1(q, size, prob, lowerTail, logP) {
196
277
  return dt0(lowerTail, logP);
197
278
  if (size === 0 || prob === 1 || !Number.isFinite(q))
198
279
  return dt1(lowerTail, logP);
199
- return pnbinomRaw(Math.floor(q + 1e-7), fromProb(size, prob), lowerTail, logP);
280
+ return pnbinomRaw(countAtMost(q), fromProb(size, prob), lowerTail, logP);
200
281
  }
201
282
  export function qnbinom1(p, size, prob, lowerTail, logP) {
202
283
  if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(prob))
@@ -237,7 +318,7 @@ export function dnbinomMu1(x, size, mu, log) {
237
318
  return dpoisRaw(n, mu, log);
238
319
  if (mu === 0)
239
320
  return n === 0 ? (log ? 0 : 1) : d0(log);
240
- return dnbinomRaw(n, fromMu(size, mu), log);
321
+ return dnbinomRaw(n, fromMuDensity(size, mu), log);
241
322
  }
242
323
  export function pnbinomMu1(q, size, mu, lowerTail, logP) {
243
324
  if (Number.isNaN(q) || Number.isNaN(size) || Number.isNaN(mu))
@@ -250,25 +331,26 @@ export function pnbinomMu1(q, size, mu, lowerTail, logP) {
250
331
  return dt1(lowerTail, logP);
251
332
  if (size === Infinity)
252
333
  return ppois1(q, mu, lowerTail, logP);
253
- return pnbinomRaw(Math.floor(q + 1e-7), fromMu(size, mu), lowerTail, logP);
334
+ return pnbinomRaw(countAtMost(q), fromMuDensity(size, mu), lowerTail, logP);
254
335
  }
255
336
  export function qnbinomMu1(p, size, mu, lowerTail, logP) {
256
337
  if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(mu))
257
338
  return NaN;
258
- // 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;
259
343
  if (size === Infinity)
260
344
  return qpois1(p, mu, lowerTail, logP);
261
345
  if (size === 0 || mu === 0)
262
346
  return 0;
263
- if (size < 0 || mu < 0)
264
- return NaN;
265
347
  // mu = Inf answers only at the ends (p = 0, 1) and is NaN otherwise (as in R)
266
348
  if (mu === Infinity)
267
349
  return quantileBoundaries(p, 0, Infinity, lowerTail, logP) ?? NaN;
268
350
  const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
269
351
  if (b !== undefined)
270
352
  return b;
271
- return quantileRaw(p, fromMu(size, mu), lowerTail, logP);
353
+ return quantileRaw(p, fromMuDensity(size, mu), lowerTail, logP);
272
354
  }
273
355
  /** Accept exactly one of prob and mu (as in R, giving both or neither is an error) */
274
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,13 +123,38 @@ 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)
125
130
  return -(LN_SQRT_2PI + 0.5 * z * z + Math.log(sd));
126
131
  // Rounding of z^2 is amplified by the exponential in exp(-z^2/2), so split it as in pnorm
127
- return scaledExpNegHalfSq(Math.abs(z), INV_SQRT_2PI / sd);
132
+ const c = INV_SQRT_2PI / sd;
133
+ return c < Infinity ? scaledExpNegHalfSq(Math.abs(z), c) : tinySdDensity(Math.abs(z), sd);
134
+ }
135
+ /**
136
+ * exp(-a^2/2) / (sd sqrt(2π)) for an sd so far below the normal range that 1 / (sd sqrt(2π)) overflows (it was NaN, as
137
+ * Infinity times exp(-a^2/2) = 0: dnorm(4.85e-319, { sd: 1.7e-322 })), with the split of scaledExpNegHalfSq: sd is
138
+ * scaled by 2^600 (exactly) and the factor 2^600 put back where neither the partial product overflows nor its last
139
+ * factor exp(-700) makes it subnormal
140
+ */
141
+ function tinySdDensity(a, sd) {
142
+ if (a > 1e10)
143
+ return 0;
144
+ const hi = Math.floor(a * 16) / 16;
145
+ const lo = a - hi;
146
+ const halfHiSq = (hi * hi) / 2;
147
+ const m = (INV_SQRT_2PI / (sd * 2 ** 600)) * Math.exp((-lo * (a + hi)) / 2);
148
+ if (halfHiSq <= 700)
149
+ return m * Math.exp(-halfHiSq) * 2 ** 600;
150
+ if (halfHiSq > 1400) {
151
+ // (exp(700 - a^2/2) is itself subnormal from a^2/2 = 1408 on, where the density can still be normal: dnorm(-1.59e-321,
152
+ // { sd: 3e-323 }) was 3e-4 off. m exp(1400 - a^2/2) is above e^276 wherever the density is normal, so each partial
153
+ // product below stays normal until the last factor)
154
+ return m * Math.exp(1400 - halfHiSq) * Math.exp(-700) * 2 ** 600 * Math.exp(-700);
155
+ }
156
+ const t = m * Math.exp(700 - halfHiSq);
157
+ return t >= 1 ? t * Math.exp(-700) * 2 ** 600 : t * 2 ** 600 * Math.exp(-700);
128
158
  }
129
159
  /**
130
160
  * Density of the normal distribution. Equivalent to R's `dnorm(x, mean = 0, sd = 1, log = FALSE)`.
@@ -1,3 +1,28 @@
1
- export declare function dnt1(x: number, df: number, ncp: number, log: boolean): number;
2
- export declare function pnt1(q: number, df: number, ncp: number, lowerTail: boolean, logP: boolean): number;
3
- export declare function qnt1(p: number, df: number, ncp: number, lowerTail: boolean, logP: boolean): number;
1
+ /**
2
+ * Above this df the central and noncentral t are taken from their limits (hugeDf*; shared with t.ts). The t
3
+ * differs from the normal by terms of relative order t²/ν and 1/ν: where y = t²/ν < 1e-17 it is the normal to the
4
+ * double (the absolute error in the log, about t⁴/ν, is below 1e-180 wherever the linear value is above the
5
+ * underflow), and beyond, |t| > 3e91, the log of the far tail and of the density is -(ν/2) log1p(y) up to terms
6
+ * below 1e-179 of it. The general code fails from about 1.3e300 on (NaN where its intermediate terms overflow)
7
+ */
8
+ export declare const HUGE_DF = 1e+200;
9
+ /** y = t²/ν (t² may overflow on its own) */
10
+ export declare function hugeDfRatio(t: number, n: number): number;
11
+ /**
12
+ * Log of the far tail beyond |t| and of the density at t (relative error about eps) for n > HUGE_DF and
13
+ * hugeDfRatio(t, n) >= 1e-17: -(n/2) log1p(t²/n), formed without overflow
14
+ */
15
+ export declare function hugeDfTailLog(t: number, n: number): number;
16
+ /**
17
+ * |t| with hugeDfTailLog(t, n) = logTarget (logTarget < 0), Infinity beyond the largest double: t² = n expm1(-2 logTarget / n).
18
+ * (sqrt(n) is taken apart: through exp(log t) its log, up to 354, would multiply the rounding error)
19
+ */
20
+ export declare function hugeDfTailQuantile(logTarget: number, n: number): number;
21
+ /**
22
+ * P[T <= t] (lower) / P[T > t] for n > HUGE_DF and hugeDfRatio(t, n) >= 1e-17 from hugeDfTailLog: the tail beyond |t|
23
+ * is its exp (0), the other is 1
24
+ */
25
+ export declare function hugeDfTail(t: number, n: number, lowerTail: boolean, logP: boolean): number;
26
+ export declare function dnt1(x: number, df0: number, ncp: number, log: boolean): number;
27
+ export declare function pnt1(q: number, df0: number, ncp: number, lowerTail: boolean, logP: boolean): number;
28
+ export declare function qnt1(p: number, df0: number, ncp: number, lowerTail: boolean, logP: boolean): number;