@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
@@ -8,10 +8,16 @@
8
8
  import { vectorize } from "../internal/vectorize.js";
9
9
  import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
10
10
  import { discreteQuantile } from "../internal/discrete.js";
11
- import { nonInt, roundHalfEven } from "../internal/integer.js";
11
+ import { countAtMost, nonInt, roundHalfEven } from "../internal/integer.js";
12
12
  import { bd0dd, dbinomExponent } from "../special/loader.js";
13
- import { ddAdd, ddDiv, ddLog, ddMul, ddNeg, ddSub, twoProd } from "../special/dd.js";
13
+ import { ddAdd, ddDiv, ddLog, ddMul, ddNeg, ddSub, twoProd, twoSum } from "../special/dd.js";
14
14
  import { qnorm } from "./normal.js";
15
+ import { dbinom1, pbinom1, qbinom1 } from "./binomial.js";
16
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
17
+ import { checkOptions } from "../internal/checkOptions.js";
18
+ const DHYPER_KEYS = ["log"];
19
+ const PHYPER_KEYS = ["lowerTail", "logP"];
20
+ const QHYPER_KEYS = ["lowerTail", "logP"];
15
21
  /**
16
22
  * Binomial probability b(x; x + nmx, p) as an ExpForm (0 <= x, 0 <= nmx, 0 < p < 1).
17
23
  * At the ends (x = 0 or nmx = 0), q^n / p^n is represented by the exponent alone.
@@ -53,6 +59,14 @@ function evalForm(f, s) {
53
59
  function logForm(f) {
54
60
  return -f.e[0] - f.e[1] - 0.5 * f.logDenom;
55
61
  }
62
+ /**
63
+ * Whether m + n is exact in double precision. Beyond 2^53 it may round (1 + 1e16 to 1e16: k = 1e16 was taken for the
64
+ * whole population and dhyper(0, 1, 1e16, 1e16) was 1 for 1e-16; issue #118), and the probabilities inside the support
65
+ * are then NaN, as for the other integers beyond 2^53 (the ends, 0 and 1, and the binomial limit still answer)
66
+ */
67
+ function exactTotal(m, n) {
68
+ return twoSum(m, n)[1] === 0;
69
+ }
56
70
  /** Support [lo, hi] */
57
71
  function support(m, n, k) {
58
72
  return [Math.max(0, k - n), Math.min(k, m)];
@@ -67,13 +81,19 @@ export function dhyper1(x, m, n, k, log) {
67
81
  k = roundHalfEven(k);
68
82
  if (m < 0 || n < 0 || k < 0 || k > m + n)
69
83
  return NaN;
84
+ if (binomialLimit(m, n, k))
85
+ return dbinom1(x, k, whiteFraction(m, n), log);
70
86
  if (nonInt(x))
71
87
  return d0(log);
72
88
  x = roundHalfEven(x);
73
89
  const [lo, hi] = support(m, n, k);
74
90
  if (x < lo || x > hi)
75
91
  return d0(log);
76
- if (k === 0 || k === m + n)
92
+ if (k === 0)
93
+ return log ? 0 : 1;
94
+ if (!exactTotal(m, n))
95
+ return NaN;
96
+ if (k === m + n)
77
97
  return log ? 0 : 1;
78
98
  const f = hyperForm(x, m, n, k);
79
99
  return log ? logForm(f) : evalForm(f, 1);
@@ -137,6 +157,18 @@ function directTail(x, m, n, k, lowerSide, lo, hi) {
137
157
  return { form, sum, value: evalForm(form, sum) };
138
158
  }
139
159
  /** Check and round the parameters of phyper / qhyper. undefined if invalid */
160
+ /** m / (m + n) without forming m + n, which may overflow */
161
+ export function whiteFraction(m, n) {
162
+ return m >= n ? 1 / (1 + n / m) : n / m === Infinity ? 0 : m / n / (1 + m / n);
163
+ }
164
+ /**
165
+ * Whether k draws from m + n differ from k independent draws by less than double precision: k / (m + n) < 2^-60 (the
166
+ * hypergeometric and Bin(k, m / (m + n)) probabilities differ by a relative k / (m + n) or so). Always so when m + n
167
+ * overflows with k below 2^290, where the exact computation (which forms m + n) gave NaN
168
+ */
169
+ function binomialLimit(m, n, k) {
170
+ return k > 0 && k < m * 2 ** -60 + n * 2 ** -60;
171
+ }
140
172
  function hyperParams(m, n, k) {
141
173
  if (![m, n, k].every(Number.isFinite))
142
174
  return undefined;
@@ -154,30 +186,90 @@ export function phyper1(q, m, n, k, lowerTail, logP) {
154
186
  if (params === undefined)
155
187
  return NaN;
156
188
  [m, n, k] = params;
157
- const x = Math.floor(q + 1e-7);
189
+ if (binomialLimit(m, n, k))
190
+ return pbinom1(q, k, whiteFraction(m, n), lowerTail, logP);
191
+ const x = countAtMost(q);
158
192
  const [lo, hi] = support(m, n, k);
159
193
  if (x < lo)
160
194
  return dt0(lowerTail, logP);
161
195
  if (x >= hi)
162
196
  return dt1(lowerTail, logP);
197
+ if (hi > MAX_EXACT || !exactTotal(m, n))
198
+ return NaN;
199
+ const terms = phyperTerms(x, m, n, k, lo, hi);
200
+ if (terms > MAX_TERMS) {
201
+ throw new RangeError(`phyper: m = ${m}, n = ${n}, k = ${k} needs about ${Math.round(terms)} terms at q = ${q} (at most 2^26)`);
202
+ }
163
203
  return phyperRaw(x, m, n, k, lowerTail, logP);
164
204
  }
165
- export function qhyper1(p, m, n, k, lowerTail, logP) {
205
+ /** Variance of the hypergeometric distribution (N = m + n) */
206
+ function hyperVariance(m, n, k, N) {
207
+ return (((k * m) / N) * (n / N) * (N - k)) / Math.max(N - 1, 1);
208
+ }
209
+ /**
210
+ * Estimated number of terms of tailSum from start (lower: downwards), from the normal approximation: the terms fall
211
+ * below 2^-60 of the sum about where (v^2 - u^2) / (2 sigma^2) = 60 ln 2 (u, v: signed distances of start and of the
212
+ * last term past the mean in the direction of the sum), so about sqrt(u^2 + 83 sigma^2) - u terms, and at most the
213
+ * distance to the end of the support. (Counted with a copy of the loop for sigma up to 4e4: at most 1.02 times this.)
214
+ */
215
+ function tailTerms(start, lower, mu, variance, lo, hi) {
216
+ const u = lower ? mu - start : start - mu;
217
+ const w = Math.sqrt(u * u + 83 * variance);
218
+ return Math.min(u >= 0 ? (83 * variance) / (w + u) : w - u, lower ? start - lo : hi - start);
219
+ }
220
+ /**
221
+ * Estimated number of terms phyperRaw sums at x: the tail away from the mean, and the other one as well where the
222
+ * first may exceed 1/2, which needs x within 1 of the mean (the median of the distribution is that close to it)
223
+ */
224
+ function phyperTerms(x, m, n, k, lo, hi) {
225
+ const N = m + n;
226
+ const mu = (k * m) / N;
227
+ const variance = hyperVariance(m, n, k, N);
228
+ const lowerSide = x * N < k * m;
229
+ const start = lowerSide ? x : x + 1;
230
+ const far = tailTerms(start, lowerSide, mu, variance, lo, hi);
231
+ if (Math.abs(start - mu) >= 1)
232
+ return far;
233
+ return far + tailTerms(lowerSide ? x + 1 : x, !lowerSide, mu, variance, lo, hi);
234
+ }
235
+ /**
236
+ * Bounds on the time of phyper and qhyper (the tail sums take about 80 to 170 ns per term on the development machine):
237
+ * phyper's estimated number of terms at most 2^26 (about 10 s; sigma about 7e6 at the center), qhyper's standard
238
+ * deviation at most 2^21 (its search calls the distribution function a few times, about 2.5 times 9.1 sigma terms)
239
+ */
240
+ const MAX_TERMS = 2 ** 26;
241
+ const MAX_QHYPER_SIGMA = 2 ** 21;
242
+ /**
243
+ * Largest upper end of the support for phyper and qhyper: the tail sums step through the integers one by one, which
244
+ * beyond 2^53 no longer move (phyper(5e16, 1e17, 1e17, 1e17) did not return), so the answer is NaN there, as for
245
+ * other integers beyond 2^53. Below it the time grows as the standard deviation, bounded by MAX_TERMS and
246
+ * MAX_QHYPER_SIGMA
247
+ */
248
+ const MAX_EXACT = 2 ** 53;
249
+ /** (limitTime false: without the bound on sigma, for rhyper's inversion, which is unchanged) */
250
+ export function qhyper1(p, m, n, k, lowerTail, logP, limitTime = true) {
166
251
  if ([p, m, n, k].some(Number.isNaN))
167
252
  return NaN;
168
253
  const params = hyperParams(m, n, k);
169
254
  if (params === undefined)
170
255
  return NaN;
171
256
  [m, n, k] = params;
257
+ if (binomialLimit(m, n, k))
258
+ return qbinom1(p, k, whiteFraction(m, n), lowerTail, logP);
172
259
  const [lo, hi] = support(m, n, k);
173
260
  const b = quantileBoundaries(p, lo, hi, lowerTail, logP);
174
261
  if (b !== undefined)
175
262
  return b;
176
263
  if (lo === hi)
177
264
  return lo;
265
+ if (hi > MAX_EXACT || !exactTotal(m, n))
266
+ return NaN;
178
267
  const N = m + n;
179
268
  const mu = (k * m) / N;
180
- const sigma = Math.sqrt((((k * m) / N) * (n / N) * (N - k)) / Math.max(N - 1, 1));
269
+ const sigma = Math.sqrt(hyperVariance(m, n, k, N));
270
+ if (limitTime && sigma > MAX_QHYPER_SIGMA) {
271
+ throw new RangeError(`qhyper: m = ${m}, n = ${n}, k = ${k} has a standard deviation of about ${sigma.toPrecision(3)} (at most 2^21)`);
272
+ }
181
273
  const z = qnorm(p, { lowerTail, logP });
182
274
  const y0 = mu + sigma * z;
183
275
  return discreteQuantile({
@@ -209,6 +301,7 @@ export function qhyper1(p, m, n, k, lowerTail, logP) {
209
301
  * @category Distributions
210
302
  */
211
303
  export function dhyper(x, m, n, k, options = {}) {
304
+ checkOptions(options, DHYPER_KEYS, "dhyper"); // generated: option-keys.mjs
212
305
  const log = options.log ?? false;
213
306
  return vectorize(x, (v) => dhyper1(v, m, n, k, log));
214
307
  }
@@ -231,6 +324,7 @@ export function dhyper(x, m, n, k, options = {}) {
231
324
  * @category Distributions
232
325
  */
233
326
  export function phyper(q, m, n, k, options = {}) {
327
+ checkOptions(options, PHYPER_KEYS, "phyper"); // generated: option-keys.mjs
234
328
  const { lowerTail = true, logP = false } = options;
235
329
  return vectorize(q, (v) => phyper1(v, m, n, k, lowerTail, logP));
236
330
  }
@@ -253,6 +347,7 @@ export function phyper(q, m, n, k, options = {}) {
253
347
  * @category Distributions
254
348
  */
255
349
  export function qhyper(p, m, n, k, options = {}) {
350
+ checkOptions(options, QHYPER_KEYS, "qhyper"); // generated: option-keys.mjs
256
351
  const { lowerTail = true, logP = false } = options;
257
352
  return vectorize(p, (v) => qhyper1(v, m, n, k, lowerTail, logP));
258
353
  }
@@ -13,6 +13,11 @@ import { ddExp, logRatioDD, standardizeDD } from "../internal/ddexp.js";
13
13
  import { ddAdd, ddLog, ddMulNum, ddSub } from "../special/dd.js";
14
14
  import { lgamma } from "../special/gamma.js";
15
15
  import { dgamma1, logTinyGammaQuantile, pgamma1, qgamma1 } from "./gamma.js";
16
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
17
+ import { checkOptions } from "../internal/checkOptions.js";
18
+ const DINVGAMMA_KEYS = ["beta", "log"];
19
+ const PINVGAMMA_KEYS = ["beta", "lowerTail", "logP"];
20
+ const QINVGAMMA_KEYS = ["beta", "lowerTail", "logP"];
16
21
  const valid = (alpha, beta) => alpha > 0 && beta > 0 && Number.isFinite(beta);
17
22
  // alpha = Inf is taken as the limit (the distribution concentrates at 0), as extraDistr does
18
23
  const MIN_NORMAL = 2 ** -1022;
@@ -23,8 +28,9 @@ export function dinvgamma1(x, alpha, beta, log) {
23
28
  return NaN;
24
29
  if (x <= 0 || x === Infinity)
25
30
  return d0(log);
31
+ // alpha = Inf: all the mass at 0 (the limit pinvgamma and qinvgamma take), so the density at x > 0 is 0 (it was NaN)
26
32
  if (alpha === Infinity)
27
- return NaN;
33
+ return d0(log);
28
34
  const [z, zl] = standardizeDD(beta, 0, x);
29
35
  if (z === Infinity)
30
36
  return d0(log);
@@ -122,6 +128,7 @@ export function qinvgamma1(p, alpha, beta, lowerTail, logP) {
122
128
  * @category Distributions
123
129
  */
124
130
  export function dinvgamma(x, alpha, options = {}) {
131
+ checkOptions(options, DINVGAMMA_KEYS, "dinvgamma"); // generated: option-keys.mjs
125
132
  const { beta = 1, log = false } = options;
126
133
  return vectorize(x, (v) => dinvgamma1(v, alpha, beta, log));
127
134
  }
@@ -138,6 +145,7 @@ export function dinvgamma(x, alpha, options = {}) {
138
145
  * @category Distributions
139
146
  */
140
147
  export function pinvgamma(q, alpha, options = {}) {
148
+ checkOptions(options, PINVGAMMA_KEYS, "pinvgamma"); // generated: option-keys.mjs
141
149
  const { beta = 1, lowerTail = true, logP = false } = options;
142
150
  return vectorize(q, (v) => pinvgamma1(v, alpha, beta, lowerTail, logP));
143
151
  }
@@ -154,6 +162,7 @@ export function pinvgamma(q, alpha, options = {}) {
154
162
  * @category Distributions
155
163
  */
156
164
  export function qinvgamma(p, alpha, options = {}) {
165
+ checkOptions(options, QINVGAMMA_KEYS, "qinvgamma"); // generated: option-keys.mjs
157
166
  const { beta = 1, lowerTail = true, logP = false } = options;
158
167
  return vectorize(p, (v) => qinvgamma1(v, alpha, beta, lowerTail, logP));
159
168
  }
@@ -7,6 +7,12 @@ import { vectorize } from "../internal/vectorize.js";
7
7
  import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
8
8
  import { ddExp, log1mExp } from "../internal/ddexp.js";
9
9
  import { ddAdd, ddDivNum, ddLog, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
10
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
11
+ import { checkOptions } from "../internal/checkOptions.js";
12
+ const DKUMAR_KEYS = ["log"];
13
+ const PKUMAR_KEYS = ["lowerTail", "logP"];
14
+ const QKUMAR_KEYS = ["lowerTail", "logP"];
15
+ // Generated by `node scripts/option-keys.mjs --write` (end)
10
16
  const valid = (a, b) => a > 0 && b > 0 && Number.isFinite(b);
11
17
  // a = Inf is taken as the limit (the distribution concentrates at 1), as extraDistr does
12
18
  const MIN_NORMAL = 2.2250738585072014e-308;
@@ -188,6 +194,7 @@ export function qkumar1(p, a, b, lowerTail, logP) {
188
194
  * @category Distributions
189
195
  */
190
196
  export function dkumar(x, a, b, options = {}) {
197
+ checkOptions(options, DKUMAR_KEYS, "dkumar"); // generated: option-keys.mjs
191
198
  const { log = false } = options;
192
199
  return vectorize(x, (v) => dkumar1(v, a, b, log));
193
200
  }
@@ -203,6 +210,7 @@ export function dkumar(x, a, b, options = {}) {
203
210
  * @category Distributions
204
211
  */
205
212
  export function pkumar(q, a, b, options = {}) {
213
+ checkOptions(options, PKUMAR_KEYS, "pkumar"); // generated: option-keys.mjs
206
214
  const { lowerTail = true, logP = false } = options;
207
215
  return vectorize(q, (v) => pkumar1(v, a, b, lowerTail, logP));
208
216
  }
@@ -218,6 +226,7 @@ export function pkumar(q, a, b, options = {}) {
218
226
  * @category Distributions
219
227
  */
220
228
  export function qkumar(p, a, b, options = {}) {
229
+ checkOptions(options, QKUMAR_KEYS, "qkumar"); // generated: option-keys.mjs
221
230
  const { lowerTail = true, logP = false } = options;
222
231
  return vectorize(p, (v) => qkumar1(v, a, b, lowerTail, logP));
223
232
  }
@@ -6,26 +6,52 @@
6
6
  import { vectorize } from "../internal/vectorize.js";
7
7
  import { quantileBoundaries, smallerTail } from "../internal/dpq.js";
8
8
  import { expNeg, expNegOver, locationScale, standardizeDD } from "../internal/ddexp.js";
9
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
10
+ import { checkOptions } from "../internal/checkOptions.js";
11
+ const DLAPLACE_KEYS = ["mu", "sigma", "log"];
12
+ const PLAPLACE_KEYS = ["mu", "sigma", "lowerTail", "logP"];
13
+ const QLAPLACE_KEYS = ["mu", "sigma", "lowerTail", "logP"];
14
+ // Generated by `node scripts/option-keys.mjs --write` (end)
9
15
  const MIN_NORMAL = 2 ** -1022;
10
- const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0 && Number.isFinite(sigma);
11
- // an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does
16
+ const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0;
17
+ // an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does. sigma = Inf is
18
+ // the limit too (review 24: density 0, probability 1/2 at every finite x, as extraDistr; the quantiles -Inf, mu and Inf),
19
+ // except at an infinite x or mu, where it is not defined (NaN, as extraDistr)
12
20
  export function dlaplace1(x, mu, sigma, log) {
13
21
  if (Number.isNaN(x) || !valid(mu, sigma))
14
22
  return NaN;
15
- if (!Number.isFinite(mu))
23
+ if (sigma === Infinity)
24
+ return Number.isFinite(x) && Number.isFinite(mu) ? (log ? -Infinity : 0) : NaN;
25
+ if (!Number.isFinite(mu)) {
26
+ // x and mu the same infinity: x - mu is Inf - Inf and the limit depends on the path (NaN, as in extraDistr; #338)
27
+ if (x === mu)
28
+ return NaN;
16
29
  return log ? -Infinity : 0;
30
+ }
17
31
  const z = standardizeDD(x, mu, sigma);
18
32
  const a = z[0] < 0 ? [-z[0], -z[1]] : [z[0], z[1]];
33
+ // 2 sigma overflows for sigma above half the largest double: then the factor 1/2 is taken apart
34
+ const twoSigma = 2 * sigma;
19
35
  if (log)
20
- return -a[0] - a[1] - Math.log(2 * sigma);
36
+ return -a[0] - a[1] - (twoSigma < Infinity ? Math.log(twoSigma) : Math.LN2 + Math.log(sigma));
21
37
  // (from exp(-|z|) below the normal range the quotient is formed without rounding it there: see expNegOver)
22
38
  const e = expNeg(a);
23
- return e >= MIN_NORMAL ? e / (2 * sigma) : expNegOver(a, 2 * sigma);
39
+ // (the factor 1/2 on e first, exactly: (e / sigma) / 2 rounded twice onto the subnormals, an ulp off at x = sigma / 2
40
+ // for sigma = 1.447e308; issue #110. Below 2 MIN_NORMAL the quotient is below e^-708 / 1.8e308, 0 either way)
41
+ if (twoSigma === Infinity)
42
+ return e >= 2 * MIN_NORMAL ? e / 2 / sigma : expNegOver(a, sigma) / 2;
43
+ return e >= MIN_NORMAL ? e / twoSigma : expNegOver(a, twoSigma);
24
44
  }
25
45
  export function plaplace1(q, mu, sigma, lowerTail, logP) {
26
46
  if (Number.isNaN(q) || !valid(mu, sigma))
27
47
  return NaN;
48
+ if (sigma === Infinity)
49
+ return Number.isFinite(q) && Number.isFinite(mu) ? (logP ? -Math.LN2 : 0.5) : NaN;
28
50
  if (!Number.isFinite(mu)) {
51
+ // q and mu the same infinity: the limit is 0 or 1 depending on which goes first (NaN, as in extraDistr, pnorm,
52
+ // plogis and pcauchy; #338)
53
+ if (q === mu)
54
+ return NaN;
29
55
  const lower = mu === Infinity ? 0 : 1;
30
56
  const v = lowerTail ? lower : 1 - lower;
31
57
  return logP ? Math.log(v) : v;
@@ -38,8 +64,9 @@ export function plaplace1(q, mu, sigma, lowerTail, logP) {
38
64
  return logP ? Math.log1p(-half) : 1 - half;
39
65
  }
40
66
  export function qlaplace1(p, mu, sigma, lowerTail, logP) {
41
- if (Number.isNaN(p) || !valid(mu, sigma))
67
+ if (Number.isNaN(p) || !valid(mu, sigma) || (sigma === Infinity && !Number.isFinite(mu)))
42
68
  return NaN;
69
+ // (sigma = Inf: locationScale gives -Inf below p = 1/2, mu at 1/2 and Inf above)
43
70
  const bd = quantileBoundaries(p, -Infinity, Infinity, lowerTail, logP);
44
71
  if (bd !== undefined)
45
72
  return bd;
@@ -65,6 +92,7 @@ export function qlaplace1(p, mu, sigma, lowerTail, logP) {
65
92
  * @category Distributions
66
93
  */
67
94
  export function dlaplace(x, options = {}) {
95
+ checkOptions(options, DLAPLACE_KEYS, "dlaplace"); // generated: option-keys.mjs
68
96
  const { mu = 0, sigma = 1, log = false } = options;
69
97
  return vectorize(x, (v) => dlaplace1(v, mu, sigma, log));
70
98
  }
@@ -80,6 +108,7 @@ export function dlaplace(x, options = {}) {
80
108
  * @category Distributions
81
109
  */
82
110
  export function plaplace(q, options = {}) {
111
+ checkOptions(options, PLAPLACE_KEYS, "plaplace"); // generated: option-keys.mjs
83
112
  const { mu = 0, sigma = 1, lowerTail = true, logP = false } = options;
84
113
  return vectorize(q, (v) => plaplace1(v, mu, sigma, lowerTail, logP));
85
114
  }
@@ -95,6 +124,7 @@ export function plaplace(q, options = {}) {
95
124
  * @category Distributions
96
125
  */
97
126
  export function qlaplace(p, options = {}) {
127
+ checkOptions(options, QLAPLACE_KEYS, "qlaplace"); // generated: option-keys.mjs
98
128
  const { mu = 0, sigma = 1, lowerTail = true, logP = false } = options;
99
129
  return vectorize(p, (v) => qlaplace1(v, mu, sigma, lowerTail, logP));
100
130
  }
@@ -7,6 +7,12 @@
7
7
  import { vectorize } from "../internal/vectorize.js";
8
8
  import { quantileBoundaries } from "../internal/dpq.js";
9
9
  import { expNeg, expNegOver, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
10
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
11
+ import { checkOptions } from "../internal/checkOptions.js";
12
+ const DLOGIS_KEYS = ["location", "scale", "log"];
13
+ const PLOGIS_KEYS = ["location", "scale", "lowerTail", "logP"];
14
+ const QLOGIS_KEYS = ["location", "scale", "lowerTail", "logP"];
15
+ // Generated by `node scripts/option-keys.mjs --write` (end)
10
16
  const MIN_NORMAL = 2 ** -1022;
11
17
  export function dlogis1(x, location, scale, log) {
12
18
  if (Number.isNaN(x) || Number.isNaN(location) || Number.isNaN(scale))
@@ -25,7 +31,10 @@ export function dlogis1(x, location, scale, log) {
25
31
  // 4.81926e-319 }) was 1.3e-6 off)
26
32
  if (e < MIN_NORMAL)
27
33
  return expNegOver(a, scale);
28
- return scale >= MIN_NORMAL ? e / (scale * (1 + e) * (1 + e)) : e / ((1 + e) * (1 + e)) / scale;
34
+ // (the division by scale last also where the denominator overflows: dlogis(0, { scale: 1.28e308 }) was 0 for
35
+ // 1.95e-309; issue #109)
36
+ const d = scale * (1 + e) * (1 + e);
37
+ return scale >= MIN_NORMAL && d < Infinity ? e / d : e / ((1 + e) * (1 + e)) / scale;
29
38
  }
30
39
  export function plogis1(q, location, scale, lowerTail, logP) {
31
40
  if (Number.isNaN(q) || Number.isNaN(location) || Number.isNaN(scale))
@@ -81,6 +90,7 @@ export function qlogis1(p, location, scale, lowerTail, logP) {
81
90
  * @category Distributions
82
91
  */
83
92
  export function dlogis(x, options = {}) {
93
+ checkOptions(options, DLOGIS_KEYS, "dlogis"); // generated: option-keys.mjs
84
94
  const { location = 0, scale = 1, log = false } = options;
85
95
  return vectorize(x, (v) => dlogis1(v, location, scale, log));
86
96
  }
@@ -100,6 +110,7 @@ export function dlogis(x, options = {}) {
100
110
  * @category Distributions
101
111
  */
102
112
  export function plogis(q, options = {}) {
113
+ checkOptions(options, PLOGIS_KEYS, "plogis"); // generated: option-keys.mjs
103
114
  const { location = 0, scale = 1, lowerTail = true, logP = false } = options;
104
115
  return vectorize(q, (v) => plogis1(v, location, scale, lowerTail, logP));
105
116
  }
@@ -119,6 +130,7 @@ export function plogis(q, options = {}) {
119
130
  * @category Distributions
120
131
  */
121
132
  export function qlogis(p, options = {}) {
133
+ checkOptions(options, QLOGIS_KEYS, "qlogis"); // generated: option-keys.mjs
122
134
  const { location = 0, scale = 1, lowerTail = true, logP = false } = options;
123
135
  return vectorize(p, (v) => qlogis1(v, location, scale, lowerTail, logP));
124
136
  }
@@ -10,6 +10,11 @@ import { ddExp, expNeg, mulDD } from "../internal/ddexp.js";
10
10
  import { ddAdd, ddDiv, ddLog, ddMul, ddSub, twoSum } from "../special/dd.js";
11
11
  import { erfcxNonNegative } from "../special/erf.js";
12
12
  import { pnorm, qnorm } from "./normal.js";
13
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
14
+ import { checkOptions } from "../internal/checkOptions.js";
15
+ const DLNORM_KEYS = ["meanlog", "sdlog", "log"];
16
+ const PLNORM_KEYS = ["meanlog", "sdlog", "lowerTail", "logP"];
17
+ const QLNORM_KEYS = ["meanlog", "sdlog", "lowerTail", "logP"];
13
18
  const LN_SQRT_2PI = 0.918938533204672741780329736406;
14
19
  const INV_SQRT_2PI = 0.398942280401432677939946059934;
15
20
  const MIN_NORMAL = 2 ** -1022;
@@ -32,8 +37,14 @@ function zOf(x, meanlog, sdlog) {
32
37
  }
33
38
  /** z^2 / 2 in double-double */
34
39
  function halfSquare(z) {
35
- if (Math.abs(z[0]) > 1e150)
36
- return [(z[0] * z[0]) / 2, 0];
40
+ if (Math.abs(z[0]) > 1e150) {
41
+ // z times 2^-512 (exact) so that the splitting in ddMul does not overflow, and the square back by 2^1024 / 2 (the
42
+ // low part of z was dropped here: an ulp off where z^2 / 2 is finite; issue #112)
43
+ const t = [z[0] * 2 ** -512, z[1] * 2 ** -512];
44
+ const s = ddMul(t, t);
45
+ const hi = s[0] * 2 ** 1023;
46
+ return Number.isFinite(hi) ? [hi, s[1] * 2 ** 1023] : [hi, 0];
47
+ }
37
48
  const s = ddMul(z, z);
38
49
  return [s[0] / 2, s[1] / 2];
39
50
  }
@@ -174,6 +185,7 @@ export function qlnorm1(p, meanlog, sdlog, lowerTail, logP) {
174
185
  * @category Distributions
175
186
  */
176
187
  export function dlnorm(x, options = {}) {
188
+ checkOptions(options, DLNORM_KEYS, "dlnorm"); // generated: option-keys.mjs
177
189
  const { meanlog = 0, sdlog = 1, log = false } = options;
178
190
  return vectorize(x, (v) => dlnorm1(v, meanlog, sdlog, log));
179
191
  }
@@ -193,6 +205,7 @@ export function dlnorm(x, options = {}) {
193
205
  * @category Distributions
194
206
  */
195
207
  export function plnorm(q, options = {}) {
208
+ checkOptions(options, PLNORM_KEYS, "plnorm"); // generated: option-keys.mjs
196
209
  const { meanlog = 0, sdlog = 1, lowerTail = true, logP = false } = options;
197
210
  return vectorize(q, (v) => plnorm1(v, meanlog, sdlog, lowerTail, logP));
198
211
  }
@@ -212,6 +225,7 @@ export function plnorm(q, options = {}) {
212
225
  * @category Distributions
213
226
  */
214
227
  export function qlnorm(p, options = {}) {
228
+ checkOptions(options, QLNORM_KEYS, "qlnorm"); // generated: option-keys.mjs
215
229
  const { meanlog = 0, sdlog = 1, lowerTail = true, logP = false } = options;
216
230
  return vectorize(p, (v) => qlnorm1(v, meanlog, sdlog, lowerTail, logP));
217
231
  }
@@ -6,8 +6,13 @@
6
6
  // The sums of the weights are kept in double-double: q_k is raised to the power x_k, so a rounded sum would give an
7
7
  // error of x_k ε (1e-9 at x_k = 1e7).
8
8
  // Summing lgamma terms instead loses digits to cancellation for large counts.
9
+ import { lchoose } from "../special/choose.js";
9
10
  import { dbinomRawDD } from "../special/loader.js";
10
11
  import { ddAdd, ddDiv } from "../special/dd.js";
12
+ import { ldexp } from "../internal/summary.js";
13
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
14
+ import { checkOptions } from "../internal/checkOptions.js";
15
+ const DMULTINOM_KEYS = ["size", "log"];
11
16
  /**
12
17
  * Probability function of the multinomial distribution. Equivalent to R's
13
18
  * `dmultinom(x, size = NULL, prob, log = FALSE)`.
@@ -27,6 +32,7 @@ import { ddAdd, ddDiv } from "../special/dd.js";
27
32
  * @category Distributions
28
33
  */
29
34
  export function dmultinom(x, prob, options = {}) {
35
+ checkOptions(options, DMULTINOM_KEYS, "dmultinom"); // generated: option-keys.mjs
30
36
  const log = options.log ?? false;
31
37
  const K = prob.length;
32
38
  if (x.length !== K)
@@ -40,29 +46,63 @@ export function dmultinom(x, prob, options = {}) {
40
46
  const N = x.reduce((a, b) => a + b, 0);
41
47
  if (options.size !== undefined && options.size !== N)
42
48
  throw new RangeError("size must equal the sum of x");
49
+ // Suffix sums of the weights (double-double) and the counts
50
+ let w = prob;
51
+ let ps = suffixSums(w);
52
+ // the weights are divided by 2^scale (0 unless their sum overflows)
53
+ let scale = 0;
54
+ if (ps[0][0] === Infinity) {
55
+ // Finite weights whose sum overflows: divided by 2^s >= K, which only the ratios matter for (exact unless a
56
+ // weight becomes subnormal; one that becomes 0 is below 1e-600 of the largest); weights with a sum in range are
57
+ // used as given
58
+ scale = Math.ceil(Math.log2(K));
59
+ w = prob.map((p) => ldexp(p, -scale));
60
+ ps = suffixSums(w);
61
+ }
62
+ const ns = new Array(K + 1).fill(0);
63
+ for (let k = K - 1; k >= 0; k--)
64
+ ns[k] = ns[k + 1] + x[k];
43
65
  // A category with probability 0 and a positive count makes the probability 0
44
66
  for (let k = 0; k < K; k++)
45
67
  if (prob[k] === 0 && x[k] > 0)
46
68
  return log ? -Infinity : 0;
47
- // Suffix sums of the weights (double-double) and the counts
48
- const ps = new Array(K + 1).fill([0, 0]);
49
- const ns = new Array(K + 1).fill(0);
50
- for (let k = K - 1; k >= 0; k--) {
51
- ps[k] = ddAdd(ps[k + 1], [prob[k], 0]);
52
- ns[k] = ns[k + 1] + x[k];
53
- }
69
+ // A total of the counts that overflows (each count finite) is NaN on both scales, as dnbinom where size + x overflows
70
+ // (reviews 22 and 55) and dhyper's m + n: the integer range beyond 2^53. (The linear value was 0, the log NaN; the
71
+ // true value of dmultinom([1e308, 1e308], [0.5, 0.5]) is 5.64e-155; issue #152)
72
+ if (N === Infinity)
73
+ return NaN;
74
+ // The scaled weights are used only where the sum of the weights from k on overflows: a weight that the scaling takes
75
+ // to 0 (or into the subnormals) is not lost from the later ratios. Where nothing overflows this is ps itself
76
+ const psOrig = scale === 0 ? ps : suffixSums(prob);
77
+ const logDD = (d) => Math.log(d[0]) + d[1] / d[0];
54
78
  let logSum = 0;
55
79
  let prodLin = 1;
56
80
  for (let k = 0; k < K; k++) {
57
81
  if (prob[k] === 0)
58
82
  continue;
59
- const rest = ps[k + 1];
60
- if (rest[0] === 0)
83
+ const restOrig = psOrig[k + 1];
84
+ if (restOrig[0] === 0)
61
85
  break; // the remaining counts all fall here: probability 1
62
- const q = ddDiv([prob[k], 0], ps[k]);
63
- const qc = ddDiv(rest, ps[k]);
86
+ const scaled = !(psOrig[k][0] < Infinity);
87
+ const sum = scaled ? ps[k] : psOrig[k];
88
+ const sc = scaled ? scale : 0;
89
+ const q = ddDiv([scaled ? w[k] : prob[k], 0], sum);
90
+ const qc = ddDiv(scaled ? ps[k + 1] : restOrig, sum);
64
91
  const xk = x[k];
65
92
  const nmx = ns[k + 1];
93
+ if (q[0] < 2 ** -1022 || qc[0] < 2 ** -1022) {
94
+ // q or 1 - q below the normal range (dmultinom([1, 0, 0], [5e-324, 1, 1], { log: true }) was -Infinity,
95
+ // -745.13 to the double; R gives -Infinity too): the term from the log of the small one, log(1 - r) = -r (to r²)
96
+ const small = q[0] < 2 ** -1022;
97
+ const lr = (small ? Math.log(prob[k]) : logDD(restOrig)) - sc * Math.LN2 - logDD(sum);
98
+ const [xr, xo] = small ? [xk, nmx] : [nmx, xk];
99
+ const term = lchoose(xk + nmx, xk, { rCompatible: false }) + (xr === 0 ? 0 : xr * lr) - xo * Math.exp(lr);
100
+ if (log)
101
+ logSum += term;
102
+ else
103
+ prodLin *= Math.exp(term);
104
+ continue;
105
+ }
66
106
  if (log)
67
107
  logSum += dbinomRawDD(xk, nmx, q, qc, true);
68
108
  else
@@ -70,3 +110,10 @@ export function dmultinom(x, prob, options = {}) {
70
110
  }
71
111
  return log ? logSum : prodLin;
72
112
  }
113
+ /** Suffix sums w_k + ... + w_(K-1) in double-double, for k = 0..K (the last is 0) */
114
+ function suffixSums(w) {
115
+ const ps = new Array(w.length + 1).fill([0, 0]);
116
+ for (let k = w.length - 1; k >= 0; k--)
117
+ ps[k] = ddAdd(ps[k + 1], [w[k], 0]);
118
+ return ps;
119
+ }