@numero-uno-jpn/rstats 0.15.3 → 0.17.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 (322) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/README.ja.md +68 -35
  3. package/README.md +65 -30
  4. package/dist/chunks/chunk-2C33MXSW.js +3 -0
  5. package/dist/chunks/chunk-34S5SQY6.js +2 -0
  6. package/dist/chunks/chunk-4WE3OO62.js +2 -0
  7. package/dist/chunks/chunk-5AYHCITP.js +2 -0
  8. package/dist/chunks/chunk-6JJQL6HL.js +2 -0
  9. package/dist/chunks/chunk-C7V2FH6L.js +3 -0
  10. package/dist/chunks/chunk-CZI7KUO3.js +2 -0
  11. package/dist/chunks/chunk-DXI5N2ET.js +2 -0
  12. package/dist/chunks/chunk-FY3FVOBM.js +2 -0
  13. package/dist/chunks/chunk-H3WVPZDL.js +2 -0
  14. package/dist/chunks/chunk-LHUIJQHY.js +2 -0
  15. package/dist/chunks/chunk-LV3W2F7N.js +2 -0
  16. package/dist/chunks/chunk-M4ITG7YR.js +2 -0
  17. package/dist/chunks/chunk-NSYZTS26.js +2 -0
  18. package/dist/chunks/chunk-ONY5CISH.js +2 -0
  19. package/dist/chunks/chunk-RFE7MVP7.js +2 -0
  20. package/dist/chunks/chunk-SE7V2KLO.js +2 -0
  21. package/dist/chunks/chunk-THIGHOKK.js +2 -0
  22. package/dist/chunks/chunk-TNMJPQPV.js +2 -0
  23. package/dist/chunks/chunk-WM7VFXTV.js +2 -0
  24. package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
  25. package/dist/chunks/chunk-XLSFJTTK.js +2 -0
  26. package/dist/chunks/chunk-Z37NELOS.js +2 -0
  27. package/dist/chunks/chunk-ZFQWB222.js +2 -0
  28. package/dist/data/grouping.js +26 -7
  29. package/dist/descriptive/basic.js +110 -8
  30. package/dist/descriptive/cov.js +23 -4
  31. package/dist/descriptive/dataFrames.d.ts +4 -2
  32. package/dist/descriptive/dataFrames.js +69 -23
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +26 -9
  35. package/dist/descriptive/hist.d.ts +8 -5
  36. package/dist/descriptive/hist.js +77 -31
  37. package/dist/descriptive/missing.js +3 -1
  38. package/dist/descriptive/moments.js +86 -19
  39. package/dist/descriptive/omega.d.ts +1 -1
  40. package/dist/descriptive/omega.js +74 -10
  41. package/dist/descriptive/pivot.d.ts +25 -10
  42. package/dist/descriptive/pivot.js +182 -42
  43. package/dist/descriptive/quantile.js +10 -1
  44. package/dist/descriptive/rank.js +8 -1
  45. package/dist/descriptive/table.d.ts +23 -3
  46. package/dist/descriptive/table.js +36 -21
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.js +27 -2
  49. package/dist/distributions/beta.js +33 -4
  50. package/dist/distributions/binomial.js +46 -14
  51. package/dist/distributions/bvnorm.js +11 -2
  52. package/dist/distributions/cauchy.js +21 -4
  53. package/dist/distributions/chisq.js +16 -3
  54. package/dist/distributions/exponential.js +14 -0
  55. package/dist/distributions/f.js +41 -17
  56. package/dist/distributions/gamma.js +35 -3
  57. package/dist/distributions/geometric.js +58 -13
  58. package/dist/distributions/hypergeometric.d.ts +5 -1
  59. package/dist/distributions/hypergeometric.js +101 -6
  60. package/dist/distributions/invgamma.js +10 -1
  61. package/dist/distributions/kumaraswamy.js +9 -0
  62. package/dist/distributions/laplace.js +36 -6
  63. package/dist/distributions/logistic.js +13 -1
  64. package/dist/distributions/lognormal.js +16 -2
  65. package/dist/distributions/multinomial.js +58 -11
  66. package/dist/distributions/nbeta.js +62 -11
  67. package/dist/distributions/nchisq.js +30 -6
  68. package/dist/distributions/negbinomial.js +102 -17
  69. package/dist/distributions/normal.js +17 -4
  70. package/dist/distributions/nt.js +392 -17
  71. package/dist/distributions/pareto.js +12 -1
  72. package/dist/distributions/poisson.js +37 -3
  73. package/dist/distributions/signrank.js +16 -4
  74. package/dist/distributions/summary.d.ts +2 -1
  75. package/dist/distributions/summary.js +481 -61
  76. package/dist/distributions/t.js +9 -0
  77. package/dist/distributions/triangular.js +19 -5
  78. package/dist/distributions/tukey.js +11 -2
  79. package/dist/distributions/uniform.js +8 -0
  80. package/dist/distributions/weibull.js +18 -0
  81. package/dist/distributions/wilcox.d.ts +9 -3
  82. package/dist/distributions/wilcox.js +164 -26
  83. package/dist/distributions.js +2 -2
  84. package/dist/entries/descriptive.d.ts +3 -1
  85. package/dist/entries/descriptive.js +1 -1
  86. package/dist/entries/distributions.d.ts +1 -0
  87. package/dist/entries/distributions.js +2 -0
  88. package/dist/entries/linalg.d.ts +3 -1
  89. package/dist/entries/mixed.d.ts +3 -0
  90. package/dist/entries/mixed.js +2 -0
  91. package/dist/entries/numerics.d.ts +2 -1
  92. package/dist/entries/tests.d.ts +8 -5
  93. package/dist/entries/tests.js +3 -1
  94. package/dist/htest/agreement.d.ts +16 -2
  95. package/dist/htest/agreement.js +122 -21
  96. package/dist/htest/ansariTest.d.ts +8 -2
  97. package/dist/htest/ansariTest.js +116 -18
  98. package/dist/htest/aov.d.ts +29 -23
  99. package/dist/htest/aov.js +90 -47
  100. package/dist/htest/binomTest.d.ts +5 -0
  101. package/dist/htest/binomTest.js +47 -17
  102. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  103. package/dist/htest/brunnerMunzelTest.js +12 -5
  104. package/dist/htest/chisqTest.d.ts +5 -2
  105. package/dist/htest/chisqTest.js +151 -46
  106. package/dist/htest/cor.d.ts +9 -2
  107. package/dist/htest/cor.js +57 -26
  108. package/dist/htest/core/ansari.d.ts +9 -0
  109. package/dist/htest/core/ansari.js +21 -1
  110. package/dist/htest/core/kolmogorov.d.ts +25 -0
  111. package/dist/htest/core/kolmogorov.js +123 -0
  112. package/dist/htest/core/mantelhaen.js +6 -4
  113. package/dist/htest/core/shapiro.d.ts +1 -1
  114. package/dist/htest/core/shapiro.js +5 -1
  115. package/dist/htest/core/simulate.d.ts +6 -0
  116. package/dist/htest/core/simulate.js +20 -0
  117. package/dist/htest/dunnett.js +14 -5
  118. package/dist/htest/effectSize.d.ts +5 -0
  119. package/dist/htest/effectSize.js +117 -35
  120. package/dist/htest/fisherTest.d.ts +4 -3
  121. package/dist/htest/fisherTest.js +88 -24
  122. package/dist/htest/groups.d.ts +7 -0
  123. package/dist/htest/groups.js +21 -4
  124. package/dist/htest/ksTest.d.ts +7 -1
  125. package/dist/htest/ksTest.js +36 -6
  126. package/dist/htest/leveneTest.d.ts +2 -0
  127. package/dist/htest/leveneTest.js +28 -6
  128. package/dist/htest/mantelhaenTest.js +132 -25
  129. package/dist/htest/mcnemarTest.js +40 -11
  130. package/dist/htest/multipleComparisons.d.ts +24 -26
  131. package/dist/htest/multipleComparisons.js +70 -64
  132. package/dist/htest/normalityTests.js +54 -14
  133. package/dist/htest/onewayTest.js +106 -5
  134. package/dist/htest/pAdjust.d.ts +3 -2
  135. package/dist/htest/pAdjust.js +42 -6
  136. package/dist/htest/pairwise.d.ts +3 -1
  137. package/dist/htest/pairwise.js +46 -21
  138. package/dist/htest/poissonTest.d.ts +1 -1
  139. package/dist/htest/poissonTest.js +117 -59
  140. package/dist/htest/polycor.js +47 -17
  141. package/dist/htest/power.d.ts +15 -1
  142. package/dist/htest/power.js +128 -10
  143. package/dist/htest/propTest.d.ts +3 -0
  144. package/dist/htest/propTest.js +44 -6
  145. package/dist/htest/pwr.d.ts +38 -1
  146. package/dist/htest/pwr.js +159 -39
  147. package/dist/htest/rankTests.js +36 -6
  148. package/dist/htest/tTest.d.ts +2 -0
  149. package/dist/htest/tTest.js +53 -21
  150. package/dist/htest/testFormula.js +11 -11
  151. package/dist/htest/trendTests.d.ts +28 -4
  152. package/dist/htest/trendTests.js +124 -35
  153. package/dist/htest/types.d.ts +7 -0
  154. package/dist/htest/types.js +7 -0
  155. package/dist/htest/varTest.js +52 -14
  156. package/dist/htest/wilcoxTest.js +164 -29
  157. package/dist/integrate/quadpack.js +27 -5
  158. package/dist/internal/checkOptions.d.ts +11 -0
  159. package/dist/internal/checkOptions.js +31 -0
  160. package/dist/internal/covmat.d.ts +5 -0
  161. package/dist/internal/covmat.js +20 -0
  162. package/dist/internal/ddexp.d.ts +15 -1
  163. package/dist/internal/ddexp.js +59 -6
  164. package/dist/internal/discrete.d.ts +14 -0
  165. package/dist/internal/discrete.js +78 -5
  166. package/dist/internal/extremes.d.ts +9 -0
  167. package/dist/internal/extremes.js +24 -0
  168. package/dist/internal/fround.js +13 -4
  169. package/dist/internal/integer.d.ts +9 -0
  170. package/dist/internal/integer.js +12 -0
  171. package/dist/internal/invert.js +15 -2
  172. package/dist/internal/optionalArgs.d.ts +12 -0
  173. package/dist/internal/optionalArgs.js +46 -0
  174. package/dist/internal/scaledInverse.d.ts +10 -0
  175. package/dist/internal/scaledInverse.js +22 -0
  176. package/dist/internal/seq.js +15 -2
  177. package/dist/internal/summary.d.ts +90 -0
  178. package/dist/internal/summary.js +458 -39
  179. package/dist/internal/uniroot.d.ts +7 -0
  180. package/dist/internal/uniroot.js +28 -3
  181. package/dist/internal/warnings.d.ts +66 -0
  182. package/dist/internal/warnings.js +122 -0
  183. package/dist/linalg/basic.d.ts +3 -1
  184. package/dist/linalg/basic.js +58 -7
  185. package/dist/linalg/chol.js +10 -1
  186. package/dist/linalg/dot.d.ts +29 -0
  187. package/dist/linalg/dot.js +132 -0
  188. package/dist/linalg/eigen.d.ts +6 -1
  189. package/dist/linalg/eigen.js +75 -14
  190. package/dist/linalg/expm.d.ts +3 -2
  191. package/dist/linalg/expm.js +113 -15
  192. package/dist/linalg/kappa.js +64 -10
  193. package/dist/linalg/lu.d.ts +2 -1
  194. package/dist/linalg/lu.js +40 -5
  195. package/dist/linalg/matrix.d.ts +14 -0
  196. package/dist/linalg/matrix.js +24 -0
  197. package/dist/linalg/norm.js +4 -0
  198. package/dist/linalg/ops.d.ts +27 -2
  199. package/dist/linalg/ops.js +74 -26
  200. package/dist/linalg/qr.d.ts +2 -1
  201. package/dist/linalg/qr.js +105 -23
  202. package/dist/linalg/schur.js +11 -3
  203. package/dist/linalg/solve.d.ts +6 -1
  204. package/dist/linalg/solve.js +156 -26
  205. package/dist/linalg/svd.d.ts +9 -2
  206. package/dist/linalg/svd.js +33 -11
  207. package/dist/linalg.js +2 -2
  208. package/dist/mixed.js +4 -4
  209. package/dist/models/anovaSequential.d.ts +32 -2
  210. package/dist/models/anovaSequential.js +170 -28
  211. package/dist/models/aovEz.js +154 -39
  212. package/dist/models/aovStrata.js +68 -24
  213. package/dist/models/carAnova.d.ts +1 -1
  214. package/dist/models/carAnova.js +78 -33
  215. package/dist/models/coxph.d.ts +6 -0
  216. package/dist/models/coxph.js +166 -69
  217. package/dist/models/estimable.d.ts +15 -0
  218. package/dist/models/estimable.js +68 -0
  219. package/dist/models/formula.d.ts +1 -1
  220. package/dist/models/formula.js +26 -8
  221. package/dist/models/glm.d.ts +57 -7
  222. package/dist/models/glm.js +427 -74
  223. package/dist/models/glmAnova.js +90 -14
  224. package/dist/models/glmConfint.d.ts +1 -1
  225. package/dist/models/glmConfint.js +121 -29
  226. package/dist/models/glmFamily.d.ts +11 -6
  227. package/dist/models/glmFamily.js +247 -62
  228. package/dist/models/lm.d.ts +12 -1
  229. package/dist/models/lm.js +246 -37
  230. package/dist/models/lmer.d.ts +31 -7
  231. package/dist/models/lmer.js +129 -42
  232. package/dist/models/lmerAnova.d.ts +3 -3
  233. package/dist/models/lmerAnova.js +47 -9
  234. package/dist/models/lmerHypotheses.d.ts +6 -2
  235. package/dist/models/lmerHypotheses.js +14 -2
  236. package/dist/models/lmerTest.d.ts +13 -6
  237. package/dist/models/lmerTest.js +82 -29
  238. package/dist/models/mauchly.d.ts +6 -1
  239. package/dist/models/mauchly.js +41 -15
  240. package/dist/models/mixedStructure.js +6 -0
  241. package/dist/models/modelFrame.js +10 -4
  242. package/dist/models/modelMatrix.d.ts +3 -1
  243. package/dist/models/modelMatrix.js +47 -17
  244. package/dist/models/scaledTables.d.ts +8 -0
  245. package/dist/models/scaledTables.js +6 -0
  246. package/dist/models/survival.d.ts +12 -1
  247. package/dist/models/survival.js +75 -28
  248. package/dist/models/tiedTimes.d.ts +2 -0
  249. package/dist/models/tiedTimes.js +34 -0
  250. package/dist/numerics/deriv.d.ts +1 -1
  251. package/dist/numerics/deriv.js +72 -8
  252. package/dist/numerics/integrate.d.ts +9 -1
  253. package/dist/numerics/integrate.js +111 -16
  254. package/dist/numerics/interp.d.ts +40 -9
  255. package/dist/numerics/interp.js +375 -55
  256. package/dist/numerics/ode.js +59 -1
  257. package/dist/numerics.js +2 -2
  258. package/dist/optim/localmin.js +4 -0
  259. package/dist/optim/optim.d.ts +3 -1
  260. package/dist/optim/optim.js +97 -10
  261. package/dist/random/beta.js +58 -4
  262. package/dist/random/binomial.d.ts +14 -3
  263. package/dist/random/binomial.js +59 -24
  264. package/dist/random/continuous.js +50 -5
  265. package/dist/random/extra.js +55 -9
  266. package/dist/random/gamma.d.ts +4 -1
  267. package/dist/random/gamma.js +47 -8
  268. package/dist/random/hyper.js +9 -6
  269. package/dist/random/mt.js +17 -2
  270. package/dist/random/poisson.js +31 -3
  271. package/dist/random/r2dtable.d.ts +8 -1
  272. package/dist/random/r2dtable.js +24 -3
  273. package/dist/random/rank.d.ts +2 -0
  274. package/dist/random/rank.js +44 -4
  275. package/dist/random/rng.js +4 -0
  276. package/dist/random/sample.d.ts +12 -2
  277. package/dist/random/sample.js +63 -20
  278. package/dist/random/streams.js +4 -2
  279. package/dist/rstats.js +17 -14
  280. package/dist/special/besselIK.d.ts +12 -0
  281. package/dist/special/besselIK.js +175 -23
  282. package/dist/special/besselJY.d.ts +11 -1
  283. package/dist/special/besselJY.js +127 -19
  284. package/dist/special/besselSeries.d.ts +27 -0
  285. package/dist/special/besselSeries.js +45 -2
  286. package/dist/special/beta.js +4 -1
  287. package/dist/special/choose.js +24 -8
  288. package/dist/special/dd.js +122 -18
  289. package/dist/special/extra.js +14 -2
  290. package/dist/special/gamma.js +11 -2
  291. package/dist/special/incbet.js +30 -4
  292. package/dist/special/incbetTemme.d.ts +7 -0
  293. package/dist/special/incbetTemme.js +45 -12
  294. package/dist/special/loader.d.ts +1 -1
  295. package/dist/special/loader.js +117 -20
  296. package/dist/special/log1pmx.js +4 -0
  297. package/dist/special/psigamma.js +7 -0
  298. package/dist/special.js +2 -2
  299. package/dist/tests.js +15 -11
  300. package/llms.txt +15 -6
  301. package/package.json +19 -10
  302. package/dist/chunks/chunk-3THFTB36.js +0 -2
  303. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  304. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  305. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  306. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  307. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  308. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  309. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  310. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  311. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  312. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  313. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  314. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  315. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  316. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  317. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  318. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  319. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  320. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  321. package/dist/chunks/chunk-VLUL2HFW.js +0 -3
  322. package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
@@ -24,6 +24,11 @@
24
24
  // the rounding (laplace); for a small ν (a < 1e-5) formula B is used at any t, and a t so near 0 that P is Φ(-δ) to
25
25
  // the double is given that value (nearZero). The smaller tail is computed, the larger is 1 - (smaller).
26
26
  //
27
+ // Far tails of a large δ (a log beyond about 1e9, or |δ| > 2^32), where the logs of the integrand in double precision
28
+ // no longer place its peak: Laplace's method with the derivatives scaled and t S - δ in double-double (laplaceWide),
29
+ // the trapezoidal sum with the logs in double-double where the expansion is not accurate enough, the integral of the
30
+ // ratio to Φ(-δ) where the peak is broad (integrateShifted), and for t >> sqrt(ν) an expansion over Z (zTail).
31
+ //
27
32
  // For accuracy, the node w and, on top of it, e^(w/2), the exponent a (e^w - 1 - w) and the normal argument
28
33
  // t S - δ are formed in double-double (rounding in each is amplified hundreds of times in the tails). Nodes
29
34
  // where the rounding effect is estimated to be at most eps times the peak value are computed in double
@@ -154,14 +159,27 @@ function logIntegrand(pr, w) {
154
159
  let L = pr.logC - a * m;
155
160
  let d1 = -a * Math.expm1(w);
156
161
  let d2 = -a * ew;
162
+ if (pr.shift !== undefined && w > 700) {
163
+ // (a e^w from its log, where e^w overflows: for a subnormal a the peak of integrateShifted is near w = 723)
164
+ const aew = Math.exp(Math.log(a) + w);
165
+ L = pr.logC - (aew - a * (1 + w));
166
+ d1 = -(aew - a);
167
+ d2 = -aew;
168
+ }
157
169
  if (pr.kind === "cdf") {
158
170
  const r = millsInv(z);
159
- L += pnorm(z, { logP: true });
171
+ if (pr.complement) {
172
+ // log |ρ - 1| and its derivative (the second is left to bracketedPeak)
173
+ const lr = logPhiRatio(tS, delta);
174
+ // (e^lr / (e^lr - 1) is 1 where both overflow)
175
+ return [L + logAbsExpm1(lr), d1 + (tS / 2) * r * (lr > 40 ? 1 : Math.exp(lr) / Math.expm1(lr)), NaN];
176
+ }
177
+ L += pr.shift === undefined ? pnorm(z, { logP: true }) : logPhiRatio(tS, delta);
160
178
  d1 += (tS / 2) * r;
161
179
  d2 += (tS / 4) * r - (tS / 2) * (tS / 2) * r * (z + r);
162
180
  }
163
181
  else {
164
- L += w / 2 - 0.5 * z * z - LN_SQRT_2PI;
182
+ L += pr.shift === undefined ? w / 2 - 0.5 * z * z - LN_SQRT_2PI : w / 2 + tS * (delta - tS / 2);
165
183
  d1 += 0.5 - (z * tS) / 2;
166
184
  d2 += -(tS * tS) / 4 - (z * tS) / 4;
167
185
  }
@@ -207,8 +225,9 @@ function logPhiDD(zd) {
207
225
  const corr = zd[1] * millsInv(z);
208
226
  if (z > -5)
209
227
  return twoSum(pnorm(z, { logP: true }), corr);
210
- const zz = ddMul(zd, zd);
211
- return ddAdd([-zz[0] / 2, -zz[1] / 2], [Math.log(0.5 * erfcxNonNegative(-z * Math.SQRT1_2)), 0]);
228
+ // (z²/2 as z (z/2), the same to the bit: z² alone overflows from |z| = 1.34e154, z²/2 from 1.9e154)
229
+ const zh = ddMul(zd, [zd[0] / 2, zd[1] / 2]);
230
+ return ddAdd([-zh[0], -zh[1]], [Math.log(0.5 * erfcxNonNegative(-z * Math.SQRT1_2)), 0]);
212
231
  }
213
232
  /**
214
233
  * Value of the integrand at node w (double-double). If log, return the log in double-double
@@ -254,11 +273,23 @@ function integrand(pr, w, log, peak = Infinity) {
254
273
  const g = x > X_TINY ? gammaInc(a, x, lower, false) : Math.exp(gammaLog(a, w[0] + w[1], lower)[0]);
255
274
  return [(t / 2) * (S[0] + S[1]) * Math.exp(-0.5 * zz[0] - LN_SQRT_2PI) * (1 - zz[1] / 2) * g, 0];
256
275
  }
257
- // a (e^w - 1 - w); (a tiny a reaches e^w near the overflow threshold, where the splitting in ddMulNum overflows)
276
+ // a (e^w - 1 - w); (a tiny a reaches e^w near the overflow threshold, where the splitting in ddMulNum overflows;
277
+ // in integrateShifted beyond it, from the log of a e^w)
258
278
  const m = expm1mx(w, ew);
259
- const E = m[0] < 1e290 ? ddMulNum(m, a) : twoSum(a * m[0], a * m[1]);
279
+ const E = pr.shift !== undefined && w[0] > 700
280
+ ? twoSum(Math.exp(Math.log(a) + w[0] + w[1]), -a * (1 + w[0]))
281
+ : m[0] < 1e290
282
+ ? ddMulNum(m, a)
283
+ : twoSum(a * m[0], a * m[1]);
260
284
  if (!(E[0] < Infinity))
261
285
  return [log ? -Infinity : 0, 0];
286
+ if (pr.shift !== undefined) {
287
+ // (integrateShifted: the log less log Φ(-δ) or log φ(δ), of a moderate size)
288
+ const y = ddMulNum(S, t)[0];
289
+ const lr = pr.kind === "cdf" ? logPhiRatio(y, delta) : y * (delta - y / 2);
290
+ const l = ddAdd([pr.logC - E[0], -E[1]], [pr.complement ? logAbsExpm1(lr) : lr, 0]);
291
+ return pr.kind === "cdf" ? l : ddAdd(l, [w[0] / 2, w[1] / 2]);
292
+ }
262
293
  const zd = ddAdd(ddMulNum(S, t), [-delta, 0]);
263
294
  const z = zd[0];
264
295
  if (pr.kind === "cdf") {
@@ -280,7 +311,7 @@ function integrand(pr, w, log, peak = Infinity) {
280
311
  return [Math.exp(lv) * (1 - E[1] - zz[1] / 2) * (S[0] + S[1]), 0];
281
312
  }
282
313
  /** Trapezoidal sum (step h in z, extending both ways until negligible) */
283
- function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
314
+ function trapezoid(pr, w0, sigma, h, log, offset, odd, peak, offsetLow) {
284
315
  // If odd, only the odd-indexed nodes (the points added by halving the step)
285
316
  let sum = 0;
286
317
  const node = (z) => {
@@ -289,6 +320,8 @@ function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
289
320
  const d = (sigma / 2) * (z - Math.expm1(-z));
290
321
  const v = integrand(pr, twoSum(w0, d), log, peak);
291
322
  const jac = (sigma / 2) * (1 + ez);
323
+ if (offsetLow !== undefined)
324
+ return Math.exp(v[0] - offset + (v[1] - offsetLow)) * jac;
292
325
  return log ? Math.exp(v[0] - offset) * (1 + v[1]) * jac : v[0] * jac;
293
326
  };
294
327
  const start = odd ? 1 : 0;
@@ -317,11 +350,31 @@ function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
317
350
  return sum;
318
351
  }
319
352
  function integrate(pr, needLog) {
353
+ // Far tails of a large δ (LOG_LARGE, D_LARGE): over z for a large t (zTail; the density is symmetric under
354
+ // (t, δ) -> (-t, -δ), and for the distribution function a negative δ is the upper tail), otherwise laplaceWide below
355
+ const large = farLogEstimate(pr) > LOG_LARGE || Math.abs(pr.delta) > D_LARGE;
356
+ if (large && pr.t * pr.delta > 0 && Math.abs(pr.t) / Math.sqrt(2 * pr.a) >= T_Z) {
357
+ return zTail(pr.a, Math.abs(pr.t), Math.abs(pr.delta), pr.kind === "pdf" ? "pdf" : pr.delta > 0 ? "lower" : "upper");
358
+ }
320
359
  if (pr.a > A_LAPLACE) {
321
360
  const v = laplace(pr);
322
361
  if (v !== undefined)
323
362
  return v;
324
363
  }
364
+ // (for the distribution function a negative δ here is the upper tail near 1, whose logs are of a moderate size)
365
+ if (large && !(pr.kind === "cdf" && pr.delta < 0)) {
366
+ const sharp = { w: NaN, sigma: NaN };
367
+ const v = laplaceWide(pr, sharp);
368
+ if (v !== undefined)
369
+ return v;
370
+ // A sharp peak (L'' <= -1e5) where the expansion is not accurate enough (u at the peak is of order -10: log Φ(u)
371
+ // is not yet nearly quadratic): its log there is at most about 1e17 (beyond, the expansion's error is below the
372
+ // rounding), so the trapezoidal sum with the logs of the nodes and of the peak in double-double resolves it. A
373
+ // broad peak: integrateShifted
374
+ if (!Number.isNaN(sharp.w))
375
+ return integrateCore(pr, true, { w: sharp.w, sigma: sharp.sigma, L: NaN }, true);
376
+ return integrateShifted(pr);
377
+ }
325
378
  if (pr.kind === "cdf") {
326
379
  // (for a tiny a the density of w is spread over w down to about -1/a, and the peak of formula A is not found;
327
380
  // formula B is used at any t)
@@ -355,6 +408,20 @@ function integrate(pr, needLog) {
355
408
  }
356
409
  return integrateCore(pr, needLog);
357
410
  }
411
+ /**
412
+ * -log of the far tail (cdf, δ > 0 and t below it) or of the density far from the centre, roughly: the minimum over
413
+ * S of (t S - δ)²/2 + a S², δ²/2 / (1 + t²/(2a)) for t δ > 0, and δ²/2 otherwise (within a factor of about 2 where
414
+ * it is large). The peak of the integrand itself (findPeak) is no guide: where Φ(t S - δ) is a steep step, formula A's
415
+ * peak is not found, and its value can be that of a node far in the left tail of Φ
416
+ */
417
+ function farLogEstimate(pr) {
418
+ const { a, t, delta, kind } = pr;
419
+ if (kind === "cdf" && !(delta > 0))
420
+ return 0;
421
+ const d = Math.abs(delta);
422
+ const h = t * delta > 0 ? d / Math.hypot(1, t / Math.sqrt(2 * a)) : d;
423
+ return (h * h) / 2;
424
+ }
358
425
  /**
359
426
  * Below this a, formula B is used at any t: the peak of formula A is not found for a below about 1e-15, and from
360
427
  * 1e-5 down its error reaches 5e-15 of P, where formula B, a correction of order a log(t²/a) to Φ(-δ), keeps 1e-16
@@ -469,6 +536,9 @@ function laplace(pr) {
469
536
  const ew = Math.exp(w);
470
537
  if (!(w > -1390 && L2 >= 1e5))
471
538
  return undefined;
539
+ // (the peak beyond the bracket, for a large δ: pt(1e9, 1e6, ncp 1e20) was -3.9e38, its log is -5e27; laplaceWide)
540
+ if (p.d1 > 0 && 50 - w < 1e-9)
541
+ return undefined;
472
542
  const r3 = (-a * ew + f[2]) / L2;
473
543
  const r4 = (-a * ew + f[3]) / L2;
474
544
  const c1 = r4 / L2 / 8 + (5 * r3 * r3) / L2 / 24;
@@ -495,6 +565,282 @@ function laplace(pr) {
495
565
  const q = cdf ? Math.abs(v) / 2 / (Math.sqrt(L2) * Math.max(1, -u)) : 0;
496
566
  return 1 / (L2 * L2) + q ** 6 <= 1e-17 * Math.max(1, Math.abs(value.log)) ? value : undefined;
497
567
  }
568
+ /**
569
+ * laplace where the log of the integrand is beyond LOG_LARGE (a large δ, at any a): there the peak can lie beyond
570
+ * w = 50 (S* is about t δ/(t² + 2a) for t > 0), and v = t S, v² and a e^w can overflow while the answer is finite. The
571
+ * derivatives d1, L2 and f are formed in units of 2^(2k), with 2^k about max(|v|, sqrt(a e^w)) / 2^400, and u from v
572
+ * in double-double (where t² >> a, u = v - δ is a small part of δ)
573
+ */
574
+ function laplaceWide(pr, sharp) {
575
+ const { a, t, delta, kind } = pr;
576
+ const cdf = kind === "cdf";
577
+ const la = Math.log(a);
578
+ const at = (w) => {
579
+ const lg2 = Math.max(Math.log2(Math.abs(t)) + w / 2 / Math.LN2, (la + w) / 2 / Math.LN2);
580
+ const k = Math.max(0, Math.ceil(lg2) - 400);
581
+ // (beyond the double range of v the derivatives are not formed: the peak is below there)
582
+ if (lg2 > 1100) {
583
+ const f = [NaN, NaN, NaN, NaN];
584
+ return { u: NaN, v: NaN, vs: NaN, sk: 1, k, f, d1: NaN, L2: NaN, aew: NaN };
585
+ }
586
+ const sk = 2 ** -k;
587
+ const vs = t * Math.exp(w / 2 - k * Math.LN2);
588
+ const v = vs / sk;
589
+ const u = Math.abs(w) < 1 ? t - delta + t * Math.expm1(w / 2) : v - delta;
590
+ // a e^w 2^-2k (from its log where e^w overflows: a subnormal a puts the peak near w = 735)
591
+ const direct = k === 0 && w < 700;
592
+ const aew = direct ? a * Math.exp(w) : Math.exp(la + w - 2 * k * Math.LN2);
593
+ const v2 = vs * vs;
594
+ let f;
595
+ if (cdf) {
596
+ const [l1, l2, l3, l4] = logPhiDerivs(u);
597
+ const l1v = l1 * sk * vs;
598
+ const l3v = (l3 / sk) * vs;
599
+ const l4v2 = (((l4 / sk) * vs) / sk) * vs;
600
+ f = [
601
+ l1v / 2,
602
+ (l2 * v2 + l1v) / 4,
603
+ (l3v * v2 + 3 * l2 * v2 + l1v) / 8,
604
+ (l4v2 * v2 + 6 * l3v * v2 + 7 * l2 * v2 + l1v) / 16,
605
+ ];
606
+ }
607
+ else {
608
+ const uv = u * sk * vs;
609
+ f = [0.5 * sk * sk - uv / 2, -(v2 + uv) / 4, -(3 * v2 + uv) / 8, -(7 * v2 + uv) / 16];
610
+ }
611
+ const d1 = (direct ? -a * Math.expm1(w) : -(aew - a * sk * sk)) + f[0];
612
+ return { u, v, vs, sk, k, f, d1, L2: aew - f[1], aew };
613
+ };
614
+ // w*: as in laplace, over w up to 1418 (e^(w/2) up to the overflow threshold), from the estimate of the peak; a d1
615
+ // that is not formed (beyond the double range) moves the upper end down. Where L' is about -a e^w Newton's steps are
616
+ // about 1 long, so a step that does not halve the one before last is replaced by bisection (as in rtsafe)
617
+ let lo = -1400;
618
+ let hi = 1418;
619
+ let w = 0;
620
+ if (t > 0 && delta > 0) {
621
+ const w0 = 2 * Math.log(delta / (t + (2 * a) / t));
622
+ if (w0 > lo && w0 < hi)
623
+ w = w0;
624
+ }
625
+ let p = at(w);
626
+ let dx = hi - lo;
627
+ let dxOld = dx;
628
+ for (let i = 0; i < 400 && hi - lo > 4e-16 * Math.max(1, Math.abs(w)); i++) {
629
+ if (Number.isNaN(p.d1) || Number.isNaN(p.L2)) {
630
+ hi = w;
631
+ w = (lo + hi) / 2;
632
+ p = at(w);
633
+ continue;
634
+ }
635
+ const step = p.d1 / p.L2;
636
+ if ((Math.abs(step) * Math.sqrt(p.L2)) / p.sk < 1e-9)
637
+ break;
638
+ if (p.d1 > 0)
639
+ lo = w;
640
+ else if (p.d1 < 0)
641
+ hi = w;
642
+ else
643
+ break;
644
+ const next = w + step;
645
+ if (next > lo && next < hi && Math.abs(2 * step) <= Math.abs(dxOld)) {
646
+ dxOld = dx;
647
+ dx = step;
648
+ w = next;
649
+ }
650
+ else {
651
+ dxOld = dx;
652
+ dx = (hi - lo) / 2;
653
+ w = lo + dx;
654
+ }
655
+ p = at(w);
656
+ }
657
+ const { u, vs, sk, k, f, L2, aew } = p;
658
+ if (!(w > -1390 && (k > 0 || L2 >= 1e5)) || !Number.isFinite(L2))
659
+ return undefined;
660
+ // (the peak, for the trapezoidal sum where the expansion is not accurate enough)
661
+ sharp.w = w;
662
+ sharp.sigma = Math.min(sk / Math.sqrt(L2), 10);
663
+ const r3 = (-aew + f[2]) / L2;
664
+ const r4 = (-aew + f[3]) / L2;
665
+ const c1 = ((r4 / L2 / 8 + (5 * r3 * r3) / L2 / 24) * sk) * sk;
666
+ // log(L2/a) (L2 here in units of 2^(2k)), without cancellation near w = 0
667
+ let lr;
668
+ if (k === 0 && w < 700) {
669
+ const x = Math.expm1(w) - f[1] / a;
670
+ lr = Math.abs(x) < 0.5 ? Math.log1p(x) : Math.log(Math.exp(w) - f[1] / a);
671
+ }
672
+ else
673
+ lr = Math.log(L2) + 2 * k * Math.LN2 - la;
674
+ // a (e^w - 1 - w), with a e^w from its log beyond the overflow of e^w (it may still overflow: the log is then -Inf)
675
+ const aE = w > 700 ? Math.exp(la + w) - a * (1 + w) : a * expm1mxNum(w);
676
+ const rest = -0.5 * lr - stirlerr(a) + Math.log1p(c1) - aE;
677
+ const ud = Math.abs(w) < 1
678
+ ? ddAdd(twoSum(t, -delta), twoProd(t, Math.expm1(w / 2)))
679
+ : ddAdd(ddMulNum(ddExp([w / 2, 0]), t), [-delta, 0]);
680
+ let value;
681
+ if (cdf) {
682
+ const l = logPhiDD(ud);
683
+ const log = l[0] + (l[1] + rest);
684
+ value = { lin: log < -745 ? 0 : pnorm(ud[0]) * (1 + ud[1] * millsInv(ud[0])) * Math.exp(rest), log };
685
+ }
686
+ else {
687
+ // (u²/2 as u (u/2): u² alone overflows from |u| = 1.34e154, u²/2 from 1.9e154)
688
+ const uh = ddMul(ud, [ud[0] / 2, ud[1] / 2]);
689
+ const log = -uh[0] + (rest + w / 2 - LN_SQRT_2PI - uh[1]);
690
+ value = { lin: log < -745 ? 0 : Math.exp(log), log };
691
+ }
692
+ const q = cdf ? Math.abs(vs) / 2 / (Math.sqrt(L2) * Math.max(1, -u)) : 0;
693
+ const inv = (sk * sk) / L2;
694
+ return inv * inv + q ** 6 <= 1e-17 * Math.max(1, Math.abs(value.log)) ? value : undefined;
695
+ }
696
+ /**
697
+ * Beyond this far log estimate (farLogEstimate), the double-precision log of the integrand has an absolute error (eps
698
+ * times it) that no longer locates the peak of the trapezoidal sum (the sum overflowed or vanished from an estimate of
699
+ * about 5e11 on: pt(5, 3, ncp 1e7) gave a log of +Infinity, pt(1, 10, ncp 1e9) NaN). There the integral is taken by
700
+ * zTail, laplaceWide or, where the peak is broad, with Φ(-δ) (φ(δ) for the density) taken out (integrateShifted)
701
+ */
702
+ const LOG_LARGE = 2 ** 30;
703
+ /**
704
+ * Beyond this |δ| the logs of the integrand reach δ²/2 at some nodes even where the answer is moderate (the old
705
+ * integral gave pt(1.6e15, 0.5, ncp 1e20) a log of +Infinity and pt(7.9e17, 1e5, ncp 1e20) -δ²/2). There the far log
706
+ * estimate is below LOG_LARGE only for t/sqrt(2a) above 2^16, where zTail applies
707
+ */
708
+ const D_LARGE = 2 ** 32;
709
+ /**
710
+ * From this t/sqrt(2a) on (t δ > 0, beyond LOG_LARGE) the integrals are taken over z = Z (zTail). In w the peak narrows
711
+ * as t grows: placing it on the nearest double leaves an error of about L'' ulp(w)²/2, w² eps t²/(8a) relative to the
712
+ * log (180 eps at t/sqrt(2a) = 2^26, w = 27, where pt(t, 3, ncp 1e14) stepped by 0.1 in 1e12); here it is below
713
+ * eps/500 for any w. The terms zTail leaves out are about 64 a⁴/t⁶ in the log, far below eps |log| from here on
714
+ */
715
+ const T_Z = 2 ** 12;
716
+ /**
717
+ * The tails and the density for t/sqrt(2a) >= T_Z, t δ > 0 (here both positive), over z = Z (formula B):
718
+ * P[T <= t] = Φ(-δ) + ∫ φ(z) Q(a, x(z)) dz, P[T > t] = ∫ φ(z) P(a, x(z)) dz,
719
+ * f(t) = 2/(t Γ(a)) ∫ φ(z) x(z)^a e^(-x(z)) dz,
720
+ * x(z) = a ((δ + z)/t)² = x0 (1 + 2z/δ + z²/δ²), x0 = a (δ/t)² (the integrals over z > -δ). With g = log Q, log P or
721
+ * a log x - x, and its derivatives in log x, g1 = x g', g2 = x² g'' (these do not overflow for a tiny a, where g' is
722
+ * about 1/(x log(1/x))), the integral is e^g(x0) E[e^(b Z + k Z²/2)] = e^g(x0) exp(b²/(2(1 - k))) / sqrt(1 - k),
723
+ * b = 2 g1/δ, k = (4 g2 + 2 g1)/δ² (g to second order in z). The terms left out are about 64 a⁴/t⁶ in the log, and
724
+ * z below -δ (cut off) lies some δ beyond the peak at z = b (|b| is at most about 2a δ/t², below δ 2^-23)
725
+ */
726
+ function zTail(a, t, delta, kind) {
727
+ const y = delta / t;
728
+ const x0 = a * y * y;
729
+ if (!Number.isFinite(x0)) {
730
+ // (x0 beyond the double range: Q and the density are 0 to the double, P is 1)
731
+ const log = kind === "upper" ? 0 : kind === "lower" ? pnorm(-delta, { logP: true }) : -Infinity;
732
+ return { lin: Math.exp(log), log };
733
+ }
734
+ let base;
735
+ let g1;
736
+ let g2;
737
+ if (kind === "pdf") {
738
+ base = Math.LN2 - lgamma1p(a) + Math.log(a) - Math.log(t) + a * Math.log(x0) - x0;
739
+ g1 = a - x0;
740
+ g2 = -a;
741
+ }
742
+ else {
743
+ const upper = kind === "upper";
744
+ base = gammaInc(a, x0, upper, true);
745
+ if (!upper && x0 > 1e4 * (a + 1)) {
746
+ // Q = h S, h = x^(a-1) e^-x / Γ(a), S = Σ (a-1)...(a-k)/x^k: x d log Q/dx = -x/S, and x² d²log Q/dx² = x² S'/S²
747
+ // (from the logs of h and Q, which are both about -x, the derivative would keep an error of eps x)
748
+ let term = 1;
749
+ let sum = 1;
750
+ let dsum = 0;
751
+ for (let j = 1; j < 20; j++) {
752
+ term *= (a - j) / x0;
753
+ sum += term;
754
+ dsum -= j * term;
755
+ if (Math.abs(term) < 1e-20)
756
+ break;
757
+ }
758
+ g1 = -x0 / sum;
759
+ g2 = (dsum * x0) / (sum * sum);
760
+ }
761
+ else {
762
+ // x d log G/dx = ∓x^a e^-x / (Γ(a) G) for G = Q, P, and x² d²log G/dx² = g1 (a - 1 - x - g1)
763
+ const h = Math.exp(dpoisRaw(a, x0, true) + Math.log(a) - base);
764
+ g1 = upper ? h : -h;
765
+ g2 = g1 * (a - 1 - x0 - g1);
766
+ }
767
+ }
768
+ const b = (2 * g1) / delta;
769
+ const k = (4 * g2 + 2 * g1) / delta / delta;
770
+ const logI = base + (b * b) / (2 * (1 - k)) - 0.5 * Math.log1p(-k);
771
+ const log = kind === "lower" ? logAddExp(pnorm(-delta, { logP: true }), logI) : logI;
772
+ return { lin: log < -745 ? 0 : Math.exp(log), log };
773
+ }
774
+ /** log |e^x - 1| (x beyond 40: x, to 1e-17) */
775
+ function logAbsExpm1(x) {
776
+ return x > 40 ? x : Math.log(Math.abs(Math.expm1(x)));
777
+ }
778
+ /** log Φ(y - δ) - log Φ(-δ), where both are far in the left tail as the difference of the exponents and erfcx */
779
+ function logPhiRatio(y, delta) {
780
+ const z1 = y - delta;
781
+ if (z1 < -5 && delta > 5) {
782
+ return y * (delta - y / 2) + Math.log(erfcxNonNegative(-z1 * Math.SQRT1_2) / erfcxNonNegative(delta * Math.SQRT1_2));
783
+ }
784
+ return pnorm(z1, { logP: true }) - pnorm(-delta, { logP: true });
785
+ }
786
+ /**
787
+ * The integral beyond LOG_LARGE where Laplace's method does not apply (a broad peak: t S small beside δ), as Φ(-δ) times
788
+ * E[Φ(t S - δ)/Φ(-δ)] (the density: φ(δ) times E[S φ(t S - δ)/φ(δ)]): the log of the ratio, log Φ(t S - δ) - log Φ(-δ)
789
+ * or t S (δ - t S/2), is of a moderate size there, and the trapezoidal sum of formula A finds its peak
790
+ */
791
+ function integrateShifted(pr) {
792
+ const shift = pr.kind === "cdf" ? pnorm(-pr.delta, { logP: true }) : -(pr.delta * (pr.delta / 2)) - LN_SQRT_2PI;
793
+ if (shift === -Infinity)
794
+ return { lin: 0, log: -Infinity };
795
+ const { a, t, delta } = pr;
796
+ // log λ, λ = |t δ|: for t δ < 0 the ratio is about e^(-λ S) (with S for the density), and the peak, at S = (2a + m)/λ
797
+ // (m = 0, 1 for the distribution function and the density), falls out of the range of w (S below e^-700) from
798
+ // λ = e^690 on. There the integral is the Laplace transform of the density of S near 0, 2 a^a/Γ(a) S^(2a-1):
799
+ // E[S^m e^(-λ S)] = 2 a^a Γ(2a + m) / (Γ(a) λ^(2a + m)), to relative order (2a + m)/λ (also beside 1/sqrt(a), the scale
800
+ // on which the density of S departs from its power, and the departure of the ratio from e^(-λ S), of order m/δ²)
801
+ const lLam = Math.log(Math.abs(t)) + Math.log(Math.abs(delta));
802
+ if (t * delta < 0 && lLam > 690) {
803
+ const p = pr.kind === "cdf" ? 2 * a : 2 * a + 1;
804
+ const log = shift + Math.LN2 + a * Math.log(a) - (lgamma1p(a) - Math.log(a)) + (lgamma1p(p) - Math.log(p)) - p * lLam;
805
+ return { lin: log < -745 ? 0 : Math.exp(log), log };
806
+ }
807
+ if (!(pr.kind === "cdf" && a < A_TINY)) {
808
+ const sp = { ...pr, shift };
809
+ return integrateCore(sp, true, bracketedPeak(sp));
810
+ }
811
+ // For a tiny a most of the probability of S lies near 0, spread over w down to about -1/a, where ρ is 1: the integral
812
+ // is 1 ∓ E[|ρ - 1|] (t < 0, t > 0), whose integrand peaks where t S δ is of order 1
813
+ const sp = { ...pr, shift, complement: true };
814
+ const lc = integrateCore(sp, true, bracketedPeak(sp)).log;
815
+ const log = shift + (t < 0 ? Math.log1p(-Math.exp(lc)) : logAddExp(0, lc));
816
+ return { lin: log < -745 ? 0 : Math.exp(log), log };
817
+ }
818
+ /**
819
+ * The peak of the log integrand by bisection on the sign of L' (the peak of integrateShifted is broad, so 1e-12 in w is
820
+ * ample). findPeak's Newton steps stall where L falls steeply over the range: for a large negative t S, L' is about
821
+ * t S δ / 2 at w = 0, and its steps of 1e-14 were taken for convergence
822
+ */
823
+ function bracketedPeak(pr) {
824
+ let lo = -1400;
825
+ let hi = 1418;
826
+ for (let i = 0; i < 200 && hi - lo > 1e-12 * Math.max(1, Math.abs(lo)); i++) {
827
+ const m = (lo + hi) / 2;
828
+ const d1 = logIntegrand(pr, m)[1];
829
+ // (a d1 that is not formed lies beyond the double range on that side)
830
+ if (d1 > 0 || (Number.isNaN(d1) && m < 0))
831
+ lo = m;
832
+ else
833
+ hi = m;
834
+ }
835
+ const w = (lo + hi) / 2;
836
+ let [L, , d2] = logIntegrand(pr, w);
837
+ if (Number.isNaN(d2)) {
838
+ // (L'' from the differences of L')
839
+ const h = 1e-4;
840
+ d2 = (logIntegrand(pr, w + h)[1] - logIntegrand(pr, w - h)[1]) / (2 * h);
841
+ }
842
+ return { w, sigma: d2 < 0 ? Math.min(1 / Math.sqrt(-d2), 10) : 1, L };
843
+ }
498
844
  function logAddExp(x, y) {
499
845
  if (x === -Infinity)
500
846
  return y;
@@ -503,24 +849,26 @@ function logAddExp(x, y) {
503
849
  return x > y ? x + Math.log1p(Math.exp(y - x)) : y + Math.log1p(Math.exp(x - y));
504
850
  }
505
851
  /** Linear value and log of the integral (the log may be NaN unless needLog) */
506
- function integrateCore(pr, needLog, peak) {
852
+ function integrateCore(pr, needLog, peak, ddOffset = false) {
507
853
  const { w, sigma, L } = peak ?? findPeak(pr);
508
854
  // With a peak value this small the linear answer underflows to 0 (even after multiplying by the width)
509
855
  if (!needLog && L < -800)
510
856
  return { lin: 0, log: NaN };
511
857
  // If summing linearly would overflow or underflow, subtract the peak value L before summing
512
- const useLog = L < -650 || L > 650;
513
- const offset = useLog ? L : 0;
858
+ const useLog = L < -650 || L > 650 || pr.shift !== undefined || ddOffset;
859
+ // (with ddOffset, the log of the peak in double-double: subtracted from the nodes' in double-double)
860
+ const off = ddOffset ? integrand(pr, [w, 0], true) : [useLog ? L : 0, 0];
861
+ const offset = off[0];
514
862
  // Peak value (approximate) used by the fast-path test
515
863
  const peakVal = useLog ? 0 : Math.exp(L);
516
864
  // Convergence test: halving the step roughly squares the error, so if the difference is below this the finer
517
865
  // value is accurate enough. In deep tails (summed on the log scale) the node values themselves carry an error
518
866
  // of about eps |L|, so stop there
519
- const tol = useLog ? Math.max(1e-15, 2e-16 * Math.abs(L)) : 1e-15;
867
+ const tol = useLog && !ddOffset ? Math.max(1e-15, 2e-16 * Math.abs(L)) : 1e-15;
520
868
  let h = 0.5;
521
- let s = trapezoid(pr, w, sigma, h, useLog, offset, false, peakVal) * h;
869
+ let s = trapezoid(pr, w, sigma, h, useLog, offset, false, peakVal, ddOffset ? off[1] : undefined) * h;
522
870
  for (let level = 0; level < 6; level++) {
523
- const odd = trapezoid(pr, w, sigma, h / 2, useLog, offset, true, peakVal) * (h / 2);
871
+ const odd = trapezoid(pr, w, sigma, h / 2, useLog, offset, true, peakVal, ddOffset ? off[1] : undefined) * (h / 2);
524
872
  const s2 = s / 2 + odd;
525
873
  h /= 2;
526
874
  const conv = Math.abs(s2 - s) <= tol * Math.abs(s2);
@@ -530,6 +878,15 @@ function integrateCore(pr, needLog, peak) {
530
878
  }
531
879
  if (!useLog)
532
880
  return { lin: s, log: Math.log(s) };
881
+ if (pr.shift !== undefined) {
882
+ // (only beyond LOG_LARGE, where the linear value is 0; with complement, E|ρ - 1| itself, without the shift)
883
+ const log = (pr.complement ? 0 : pr.shift) + offset + Math.log(s);
884
+ return { lin: log < -745 ? 0 : Math.exp(log), log };
885
+ }
886
+ if (ddOffset) {
887
+ const log = offset + (off[1] + Math.log(s));
888
+ return { lin: log < -745 ? 0 : Math.exp(log), log };
889
+ }
533
890
  // exp(offset) s: take exp of offset first and multiply (exp(offset + log s) would have an error |offset| times larger)
534
891
  const lin = offset > -700 ? Math.exp(offset) * s : Math.exp(offset + 700) * s * Math.exp(-700);
535
892
  return { lin, log: offset + Math.log(s) };
@@ -585,7 +942,16 @@ function farTails(t, a, delta, lower) {
585
942
  function nearZero(t, a, delta) {
586
943
  // (|t|/sqrt(a) first, and sqrt(y1) as a hypot: y1/a overflowed for a below about 1e-300 with a large δ, and at t = 0
587
944
  // the test was NaN, false, so that the integral was taken at t = 0 and gave NaN to qt)
588
- return (Math.abs(t) / Math.sqrt(a)) * Math.hypot(Math.SQRT2 * delta, Math.sqrt(4 * a + 800)) * (Math.abs(delta) + 2) <= 1e-17;
945
+ const r = (Math.abs(t) / Math.sqrt(a)) * Math.hypot(Math.SQRT2 * delta, Math.sqrt(4 * a + 800)) * (Math.abs(delta) + 2);
946
+ if (Number.isFinite(r))
947
+ return r <= 1e-17;
948
+ // (a factor overflowed, or |t|/sqrt(a) underflowed against it, at δ near the overflow threshold: by logs)
949
+ const l = Math.log(Math.abs(t)) -
950
+ 0.5 * Math.log(a) +
951
+ 0.5 * Math.LN2 +
952
+ Math.log(Math.hypot(delta, Math.sqrt(2 * a + 400))) +
953
+ Math.log(Math.abs(delta) + 2);
954
+ return l <= Math.log(1e-17);
589
955
  }
590
956
  /** Linear value and log of P[T <= t] (lower) / P[T > t] (the log may be NaN unless needLog) */
591
957
  function pntBoth(t, df, delta, lower, needLog = true) {
@@ -750,9 +1116,10 @@ export function qnt1(p, df0, ncp, lowerTail, logP) {
750
1116
  return b;
751
1117
  if (df === Infinity)
752
1118
  return ncp + qnorm(p, { lowerTail, logP });
753
- // ncp = ±Inf concentrates the distribution at ±∞ and the quantile is undefined, so NaN (R's answer mixes NaN and ±Inf depending on p)
1119
+ // ncp = ±Inf puts all the probability at ±∞ (pt is 0 or 1 at every finite t, in R too), so every quantile inside
1120
+ // (0, 1) is ±∞, as already for df = Inf above (R gives NaN for a finite df)
754
1121
  if (!Number.isFinite(ncp))
755
- return NaN;
1122
+ return ncp;
756
1123
  const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
757
1124
  if (df > HUGE_DF) {
758
1125
  // The quantiles of the limits of pnt1
@@ -786,6 +1153,14 @@ export function qnt1(p, df0, ncp, lowerTail, logP) {
786
1153
  }
787
1154
  return log ? last.log : last.lin;
788
1155
  };
1156
+ // Where the normal start is beyond 1e300 or not finite (ncp near the largest double, or a huge ncp^2 / df), the
1157
+ // quantile can lie beyond the largest double for any df: if the target is not reached there, it is ±Infinity. (The
1158
+ // solver broke down outside the doubles and returned 1 for qt(0.9, 2, ncp = 1e308); issue #136)
1159
+ if (!(Math.abs(x0) <= 1e300)) {
1160
+ const lMax = cdfY(Number.MAX_VALUE, lowerY, true);
1161
+ if (lowerY ? lMax < logTarget : lMax > logTarget)
1162
+ return s * Infinity;
1163
+ }
789
1164
  // In the far tail, if the target is smaller than the probability at |t| = T0, solve directly with the power law
790
1165
  const T0 = farLimit(df / 2, ncp);
791
1166
  let hi = Infinity;
@@ -793,7 +1168,7 @@ export function qnt1(p, df0, ncp, lowerTail, logP) {
793
1168
  const lT0 = farTailLog(s * T0, df / 2, ncp);
794
1169
  if (logTarget < lT0) {
795
1170
  const ly = Math.log(T0) + (lT0 - logTarget) / df;
796
- return ly > 709.78 ? s * Infinity : s * Math.exp(ly);
1171
+ return s * Math.exp(ly); // (Infinity where the quantile is beyond the largest double)
797
1172
  }
798
1173
  hi = T0;
799
1174
  }
@@ -8,6 +8,11 @@
8
8
  import { vectorize } from "../internal/vectorize.js";
9
9
  import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
10
10
  import { log1mExp, logRatioDD, standardizeDD } from "../internal/ddexp.js";
11
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
12
+ import { checkOptions } from "../internal/checkOptions.js";
13
+ const DPARETO_KEYS = ["a", "b", "log"];
14
+ const PPARETO_KEYS = ["a", "b", "lowerTail", "logP"];
15
+ const QPARETO_KEYS = ["a", "b", "lowerTail", "logP"];
11
16
  const valid = (a, b) => a > 0 && b > 0 && Number.isFinite(b);
12
17
  // a = Inf is taken as the limit (the distribution concentrates at b), as extraDistr does
13
18
  const MIN_NORMAL = 2 ** -1022;
@@ -77,9 +82,12 @@ export function dpareto1(x, a, b, log) {
77
82
  export function ppareto1(q, a, b, lowerTail, logP) {
78
83
  if (Number.isNaN(q) || !valid(a, b))
79
84
  return NaN;
85
+ // a = Infinity: the point mass at b, counted at q = b (as qpareto gives b and pnorm(0, 0, 0) 1)
86
+ if (a === Infinity)
87
+ return q < b ? dt0(lowerTail, logP) : dt1(lowerTail, logP);
80
88
  if (q <= b)
81
89
  return dt0(lowerTail, logP);
82
- if (q === Infinity || a === Infinity)
90
+ if (q === Infinity)
83
91
  return dt1(lowerTail, logP);
84
92
  const rt = ratio(q, b);
85
93
  const lu = -a * rt[2]; // log P[X > q]
@@ -123,6 +131,7 @@ export function qpareto1(p, a, b, lowerTail, logP) {
123
131
  * @category Distributions
124
132
  */
125
133
  export function dpareto(x, options = {}) {
134
+ checkOptions(options, DPARETO_KEYS, "dpareto"); // generated: option-keys.mjs
126
135
  const { a = 1, b = 1, log = false } = options;
127
136
  return vectorize(x, (v) => dpareto1(v, a, b, log));
128
137
  }
@@ -138,6 +147,7 @@ export function dpareto(x, options = {}) {
138
147
  * @category Distributions
139
148
  */
140
149
  export function ppareto(q, options = {}) {
150
+ checkOptions(options, PPARETO_KEYS, "ppareto"); // generated: option-keys.mjs
141
151
  const { a = 1, b = 1, lowerTail = true, logP = false } = options;
142
152
  return vectorize(q, (v) => ppareto1(v, a, b, lowerTail, logP));
143
153
  }
@@ -153,6 +163,7 @@ export function ppareto(q, options = {}) {
153
163
  * @category Distributions
154
164
  */
155
165
  export function qpareto(p, options = {}) {
166
+ checkOptions(options, QPARETO_KEYS, "qpareto"); // generated: option-keys.mjs
156
167
  const { a = 1, b = 1, lowerTail = true, logP = false } = options;
157
168
  return vectorize(p, (v) => qpareto1(v, a, b, lowerTail, logP));
158
169
  }