@numero-uno-jpn/rstats 0.15.2 → 0.15.3

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 (77) hide show
  1. package/CHANGELOG.md +114 -0
  2. package/README.ja.md +8 -8
  3. package/README.md +8 -8
  4. package/dist/chunks/{chunk-PEYGUK32.js → chunk-3THFTB36.js} +2 -2
  5. package/dist/chunks/chunk-3TSWYE5L.js +2 -0
  6. package/dist/chunks/{chunk-IBPWRQIF.js → chunk-3WPU6LGK.js} +2 -2
  7. package/dist/chunks/{chunk-VWJ74UHC.js → chunk-5ITR67MB.js} +2 -2
  8. package/dist/chunks/chunk-5NRFU6F6.js +2 -0
  9. package/dist/chunks/{chunk-XNPU54NH.js → chunk-E2LCSVQV.js} +1 -1
  10. package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
  11. package/dist/chunks/chunk-FKAKALOZ.js +2 -0
  12. package/dist/chunks/{chunk-HBMHR5SS.js → chunk-HD73UKPT.js} +2 -2
  13. package/dist/chunks/{chunk-4Z2XDWKV.js → chunk-IUULP3LE.js} +1 -1
  14. package/dist/chunks/{chunk-Q4PGYESP.js → chunk-JQG3YQOC.js} +2 -2
  15. package/dist/chunks/{chunk-LG74QY5U.js → chunk-KT66SB7R.js} +1 -1
  16. package/dist/chunks/{chunk-SREPSVTT.js → chunk-MAUAOQG3.js} +2 -2
  17. package/dist/chunks/{chunk-YVREYVI2.js → chunk-MDYE7QP2.js} +2 -2
  18. package/dist/chunks/{chunk-XPLDEK6D.js → chunk-NM3Z2UBQ.js} +2 -2
  19. package/dist/chunks/{chunk-AJBLCNZ7.js → chunk-PMJI5QO6.js} +2 -2
  20. package/dist/chunks/chunk-UF7KOTHF.js +3 -0
  21. package/dist/chunks/chunk-UOE57JGT.js +2 -0
  22. package/dist/chunks/{chunk-3WQCKGKP.js → chunk-USPEBXSR.js} +2 -2
  23. package/dist/chunks/{chunk-PWVQVJTP.js → chunk-VLUL2HFW.js} +1 -1
  24. package/dist/chunks/{chunk-YGT4P7EU.js → chunk-ZIX7NYQF.js} +1 -1
  25. package/dist/descriptive.js +2 -2
  26. package/dist/distributions/beta.js +11 -1
  27. package/dist/distributions/cauchy.js +76 -5
  28. package/dist/distributions/exponential.js +6 -1
  29. package/dist/distributions/f.js +41 -5
  30. package/dist/distributions/gamma.js +34 -3
  31. package/dist/distributions/geometric.js +4 -1
  32. package/dist/distributions/invgamma.js +4 -0
  33. package/dist/distributions/kumaraswamy.js +22 -4
  34. package/dist/distributions/laplace.js +5 -2
  35. package/dist/distributions/logistic.js +8 -2
  36. package/dist/distributions/lognormal.js +12 -1
  37. package/dist/distributions/nbeta.js +66 -24
  38. package/dist/distributions/nchisq.js +58 -11
  39. package/dist/distributions/negbinomial.js +6 -1
  40. package/dist/distributions/normal.js +26 -1
  41. package/dist/distributions/nt.d.ts +28 -3
  42. package/dist/distributions/nt.js +145 -19
  43. package/dist/distributions/pareto.js +5 -0
  44. package/dist/distributions/t.js +55 -3
  45. package/dist/distributions/triangular.js +35 -8
  46. package/dist/distributions/tukey.js +16 -0
  47. package/dist/distributions/uniform.js +2 -0
  48. package/dist/distributions/weibull.js +23 -3
  49. package/dist/distributions.js +2 -2
  50. package/dist/internal/ddexp.d.ts +8 -0
  51. package/dist/internal/ddexp.js +27 -0
  52. package/dist/internal/invert.d.ts +5 -0
  53. package/dist/internal/invert.js +10 -5
  54. package/dist/internal/noncentral.d.ts +7 -0
  55. package/dist/internal/noncentral.js +9 -0
  56. package/dist/internal/poissonMixture.js +29 -6
  57. package/dist/linalg.js +2 -2
  58. package/dist/mixed.js +2 -2
  59. package/dist/numerics.js +2 -2
  60. package/dist/rstats.js +14 -14
  61. package/dist/special/beta.js +7 -0
  62. package/dist/special/gamma.js +4 -0
  63. package/dist/special/igam.js +33 -6
  64. package/dist/special/incbet.js +70 -6
  65. package/dist/special/incbetLargeA.js +7 -2
  66. package/dist/special/incbetPrefactor.d.ts +5 -0
  67. package/dist/special/incbetPrefactor.js +14 -3
  68. package/dist/special/loader.js +70 -21
  69. package/dist/special.js +2 -2
  70. package/dist/tests.js +2 -2
  71. package/package.json +1 -1
  72. package/dist/chunks/chunk-2CUKVVRO.js +0 -2
  73. package/dist/chunks/chunk-D4F7GQAY.js +0 -2
  74. package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
  75. package/dist/chunks/chunk-JLAH3X77.js +0 -2
  76. package/dist/chunks/chunk-RCAL4VHM.js +0 -3
  77. package/dist/chunks/chunk-UFT662QZ.js +0 -2
@@ -6,10 +6,11 @@
6
6
  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
- import { ddAdd, ddLog, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
9
+ import { ddAdd, ddDivNum, ddLog, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
10
10
  const valid = (a, b) => a > 0 && b > 0 && Number.isFinite(b);
11
11
  // a = Inf is taken as the limit (the distribution concentrates at 1), as extraDistr does
12
12
  const MIN_NORMAL = 2.2250738585072014e-308;
13
+ const LOG_MIN_NORMAL = -1022 * Math.LN2;
13
14
  /**
14
15
  * c log(1 - x^a) for 0 < x < 1 where x^a is subnormal: -c x^a (to double precision) from logs, since x^a itself has
15
16
  * lost digits
@@ -127,6 +128,11 @@ export function pkumar1(q, a, b, lowerTail, logP) {
127
128
  // x^a or 1 - exp(lu) underflows; elsewhere the forms below are accurate
128
129
  if (Math.max(b, 1) * xa < 1e-10)
129
130
  return Math.log(b) + a * Math.log(q) + Math.log1p((-(b - 1) * xa) / 2);
131
+ // Where b log(1 - x^a) is subnormal (a subnormal b), it has lost digits, and so would the log of the probability
132
+ // -lu taken from it: log b + log(-log(1 - x^a)) instead (pkumar(0.2, 0.42, 1.85e-317, { logP: true }) was 2.5e-10 off;
133
+ // extraDistr gives -Inf there, taking the log after the probability underflowed)
134
+ if (-lu < MIN_NORMAL)
135
+ return Math.log(b) + Math.log(-log1mPow(q, a));
130
136
  return lu < -Math.LN2 && !tiny ? Math.log1p(-powOneMinus(q, a, b)) : log1mExp(lu);
131
137
  }
132
138
  export function qkumar1(p, a, b, lowerTail, logP) {
@@ -142,13 +148,25 @@ export function qkumar1(p, a, b, lowerTail, logP) {
142
148
  // w^(1 / a) with w = 1 - exp(lu / b). Where lu / b is subnormal or underflows, w = -lu / b, in logs; for w > 1/2
143
149
  // log w = log1p(-exp(lu / b)) keeps the digits of 1 - w
144
150
  const t = lu / b;
145
- if (!(t <= -MIN_NORMAL))
146
- return Math.exp((Math.log(-lu) - Math.log(b)) / a);
151
+ // (log(-lu) is p itself where a lower tail given as its log is below the normal range: -lu = exp(p) was rounded on
152
+ // the subnormal grid (qkumar(-744.24, 1.13, 0.124, { logP: true }) was 17% off). The exponent, of the order of
153
+ // hundreds, in double-double: its rounding is the relative error of the quantile (3.7e-14 there)
154
+ if (!(t <= -MIN_NORMAL)) {
155
+ const llu = logP && lowerTail && p < LOG_MIN_NORMAL ? p : Math.log(-lu);
156
+ const n = ddSub([llu, 0], ddLog([b, 0]));
157
+ const q = n[0] / a;
158
+ if (!(a < 1e300 && Math.abs(q) < 1e300))
159
+ return Math.exp(q);
160
+ const e = ddExp(ddDivNum(n, a));
161
+ return e[0] + e[1];
162
+ }
147
163
  if (t < -Math.LN2)
148
164
  return Math.exp(Math.log1p(-Math.exp(t)) / a);
149
165
  const w = -Math.expm1(t);
150
166
  const r = 1 / a;
151
- if (!(a < 1e300))
167
+ // (for a below 1e-300, w <= 1/2 here and the quantile w^(1/a) underflows to 0; the correction below was NaN there,
168
+ // from the splitting of a subnormal a or 1 / a overflowing: qkumar(4.8e-52, 9.9e-314, 2.5))
169
+ if (!(a < 1e300) || !(r < 1e300))
152
170
  return Math.pow(w, r);
153
171
  // w^r corrected for the rounding of r = 1 / a (exactly 1 / a - r = (1 - a r) / a), which log w magnifies
154
172
  const [ar, e] = twoProd(a, r);
@@ -5,7 +5,8 @@
5
5
  // (and the mirror image above 1/2).
6
6
  import { vectorize } from "../internal/vectorize.js";
7
7
  import { quantileBoundaries, smallerTail } from "../internal/dpq.js";
8
- import { expNeg, locationScale, standardizeDD } from "../internal/ddexp.js";
8
+ import { expNeg, expNegOver, locationScale, standardizeDD } from "../internal/ddexp.js";
9
+ const MIN_NORMAL = 2 ** -1022;
9
10
  const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0 && Number.isFinite(sigma);
10
11
  // an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does
11
12
  export function dlaplace1(x, mu, sigma, log) {
@@ -17,7 +18,9 @@ export function dlaplace1(x, mu, sigma, log) {
17
18
  const a = z[0] < 0 ? [-z[0], -z[1]] : [z[0], z[1]];
18
19
  if (log)
19
20
  return -a[0] - a[1] - Math.log(2 * sigma);
20
- return expNeg(a) / (2 * sigma);
21
+ // (from exp(-|z|) below the normal range the quotient is formed without rounding it there: see expNegOver)
22
+ const e = expNeg(a);
23
+ return e >= MIN_NORMAL ? e / (2 * sigma) : expNegOver(a, 2 * sigma);
21
24
  }
22
25
  export function plaplace1(q, mu, sigma, lowerTail, logP) {
23
26
  if (Number.isNaN(q) || !valid(mu, sigma))
@@ -6,7 +6,8 @@
6
6
  // Quantiles compute logit(p) = log(p / (1 - p)) in a cancellation-free form.
7
7
  import { vectorize } from "../internal/vectorize.js";
8
8
  import { quantileBoundaries } from "../internal/dpq.js";
9
- import { expNeg, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
9
+ import { expNeg, expNegOver, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
10
+ const MIN_NORMAL = 2 ** -1022;
10
11
  export function dlogis1(x, location, scale, log) {
11
12
  if (Number.isNaN(x) || Number.isNaN(location) || Number.isNaN(scale))
12
13
  return NaN;
@@ -19,7 +20,12 @@ export function dlogis1(x, location, scale, log) {
19
20
  const e = expNeg(a);
20
21
  if (log)
21
22
  return -a[0] - a[1] - Math.log(scale) - 2 * Math.log1p(e);
22
- return e / (scale * (1 + e) * (1 + e));
23
+ // Neither e nor the denominator may be rounded on the subnormal grid: from e below the normal range (1 + e is then 1)
24
+ // the quotient by expNegOver, and for a subnormal scale the division by it last (dlogis(-3.5667e-316, { scale:
25
+ // 4.81926e-319 }) was 1.3e-6 off)
26
+ if (e < MIN_NORMAL)
27
+ return expNegOver(a, scale);
28
+ return scale >= MIN_NORMAL ? e / (scale * (1 + e) * (1 + e)) : e / ((1 + e) * (1 + e)) / scale;
23
29
  }
24
30
  export function plogis1(q, location, scale, lowerTail, logP) {
25
31
  if (Number.isNaN(q) || Number.isNaN(location) || Number.isNaN(scale))
@@ -21,7 +21,14 @@ function zOf(x, meanlog, sdlog) {
21
21
  if (sdlog === 1)
22
22
  return d;
23
23
  const hi = d[0] / sdlog;
24
- return Number.isFinite(hi) && Math.abs(hi) < 1e300 ? ddDiv(d, [sdlog, 0]) : [hi, 0];
24
+ if (!(Number.isFinite(hi) && Math.abs(hi) < 1e300))
25
+ return [hi, 0];
26
+ if (sdlog < 1e300)
27
+ return ddDiv(d, [sdlog, 0]);
28
+ // The splitting of sdlog in the division overflows above 1e300 (plnorm(2, sdlog = 1e302) was NaN): both scaled by
29
+ // 2^-64 (exactly, unless d is so small that z is far below the rounding of the terms it enters)
30
+ const s = 2 ** -64;
31
+ return ddDiv([d[0] * s, d[1] * s], [sdlog * s, 0]);
25
32
  }
26
33
  /** z^2 / 2 in double-double */
27
34
  function halfSquare(z) {
@@ -141,6 +148,10 @@ export function qlnorm1(p, meanlog, sdlog, lowerTail, logP) {
141
148
  }
142
149
  // exp(meanlog + sdlog z). Form the exponent in double-double and apply the low-order term to first order
143
150
  const zd = refineStdQuantile(z, p, lowerTail, logP);
151
+ // Above sdlog = 1e300 the splitting in the double-double product overflows (qlnorm(1e-30, 0, 9e307) was NaN). The
152
+ // exponent is then beyond the range of exp unless |z| < 1e-297 (p within that of 1/2), where it is close to meanlog
153
+ if (sdlog >= 1e300)
154
+ return Math.exp(meanlog + sdlog * zd[0]);
144
155
  const e = ddAdd(ddAdd(mulDD(sdlog, zd[0]), [sdlog * zd[1], 0]), [meanlog, 0]);
145
156
  // Beyond the range of exp the result is 0 or Infinity (for a huge exponent e[1] can exceed 1 and flip the sign)
146
157
  if (!(Math.abs(e[0]) < 750))
@@ -15,7 +15,7 @@
15
15
  // x and 1 - x are formed as a double-double pair (rounding is amplified by the parameters, as for the central distribution).
16
16
  import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
17
17
  import { invertContinuous } from "../internal/invert.js";
18
- import { kronrodAdaptive, narrowQuantile, quadratureControl, searchDoubles } from "../internal/noncentral.js";
18
+ import { kronrodAdaptive, narrowQuantile, negligibleNcp, quadratureControl, searchDoubles } from "../internal/noncentral.js";
19
19
  import { gammaInc } from "../special/igam.js";
20
20
  import { mixtureDensity, mixtureTail } from "../internal/poissonMixture.js";
21
21
  import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddNeg, twoProd, twoSum } from "../special/dd.js";
@@ -396,11 +396,14 @@ function gammaExpansion(a, b, ncp, o, lower, log) {
396
396
  if (!(t0 > 0 && t0 < Infinity) || ratio === 0)
397
397
  return base;
398
398
  // In terms of H = h t0 = b - 1 - t0, B = b - 1 and the cumulants relative to A^n (A = a + λ/2, t0 = A k), which do
399
- // not underflow or overflow where the cumulants themselves do: v h/2 = (w/2) t0 H, w = (a + λ)/A², and so on
399
+ // not underflow or overflow where the cumulants themselves do: v h/2 = (w/2) t0 H, w = (a + λ)/A², and so on (with a
400
+ // and λ divided by A first: 6 (a + 2λ) overflowed from λ = 1.5e307, and both tails of pf(2.5e300, 1828, 0.165,
401
+ // { ncp: 3.7e307 }) were 0)
400
402
  const A = a + ncp / 2;
401
- const w = (a + ncp) / A / A;
402
- const u3 = (2 * a + 3 * ncp) / A / A / A;
403
- const u4 = (6 * (a + 2 * ncp)) / A / A / A / A;
403
+ const [ra, rl] = [a / A, ncp / A];
404
+ const w = (ra + rl) / A;
405
+ const u3 = (2 * ra + 3 * rl) / A / A;
406
+ const u4 = (6 * (ra + 2 * rl)) / A / A / A;
404
407
  const H = b - 1 - t0;
405
408
  const B = b - 1;
406
409
  const series = (w / 2) * H + (u3 / 6) * (H * H - B) + ((u4 + 3 * w * w) / 24) * (H * H * H - 3 * H * B + 2 * B);
@@ -583,6 +586,8 @@ export function dnbeta1(x, a, b, ncp, log) {
583
586
  return Infinity;
584
587
  return log ? Math.log(a + mu) : a + mu;
585
588
  }
589
+ if (negligibleNcp(mu, x * ((a + b) / a)))
590
+ return dbeta1(x, a, b, log);
586
591
  return dnbetaCore(x, a, b, ncp, log);
587
592
  }
588
593
  export function pnbeta1(q, a, b, ncp, lowerTail, logP) {
@@ -753,7 +758,8 @@ export function dnf1(x, m, n, ncp, log) {
753
758
  return log ? -Infinity : 0;
754
759
  return log ? -ncp / 2 : Math.exp(-ncp / 2);
755
760
  }
756
- if (ncp === 0)
761
+ // (x (a + b)/a of the beta at x = m q/(n + m q), with a = m/2 and b = n/2)
762
+ if (ncp === 0 || negligibleNcp(ncp / 2, (1 + n / m) / (1 + n / (m * x))))
757
763
  return dfDensity1(x, m, n, log);
758
764
  return dnfCore(x, m, n, ncp, log);
759
765
  }
@@ -791,27 +797,31 @@ const LOG_NEGLIGIBLE = -746;
791
797
  * bound is below the double range.
792
798
  */
793
799
  function fTailBound(q, m, n, ncp) {
794
- const c = (q * m) / n;
795
- if (!(c > 0 && Number.isFinite(c)))
796
- return undefined;
797
- // (c s formed first: 2c overflows for q near the largest double)
798
- const lower = (s) => -(m / 2) * Math.log1p(2 * s) - (ncp * s) / (1 + 2 * s) - (n / 2) * Math.log1p(-2 * (c * s));
799
- const upper = (s) => -(m / 2) * Math.log1p(-2 * s) + (ncp * s) / (1 - 2 * s) - (n / 2) * Math.log1p(2 * (c * s));
800
- const best = (f, sMax) => {
801
- let b = Infinity;
802
- for (let i = 1; i <= 60; i++) {
803
- const v = Math.min(f(sMax * 2 ** -i), f(sMax * (1 - 2 ** -i)));
804
- if (v < b)
805
- b = v;
806
- }
807
- return b;
808
- };
809
- if (best(lower, 1 / (2 * c)) < LOG_NEGLIGIBLE)
800
+ if (fChernoffLog(q, m, n, ncp, true) < LOG_NEGLIGIBLE)
810
801
  return "lower";
811
- if (best(upper, 0.5) < LOG_NEGLIGIBLE)
802
+ if (fChernoffLog(q, m, n, ncp, false) < LOG_NEGLIGIBLE)
812
803
  return "upper";
813
804
  return undefined;
814
805
  }
806
+ /** The bound of fTailBound on log P[F <= q] (lower) or log P[F >= q], as a log (NaN where q m/n is 0 or overflows) */
807
+ function fChernoffLog(q, m, n, ncp, lower) {
808
+ // (as q (m/n) where q m overflows, as at the largest double)
809
+ const c = q * m < Infinity ? (q * m) / n : q * (m / n);
810
+ if (!(c > 0 && Number.isFinite(c)))
811
+ return NaN;
812
+ // (c s formed first: 2c overflows for q near the largest double)
813
+ const f = lower
814
+ ? (s) => -(m / 2) * Math.log1p(2 * s) - (ncp * s) / (1 + 2 * s) - (n / 2) * Math.log1p(-2 * (c * s))
815
+ : (s) => -(m / 2) * Math.log1p(-2 * s) + (ncp * s) / (1 - 2 * s) - (n / 2) * Math.log1p(2 * (c * s));
816
+ const sMax = lower ? 1 / (2 * c) : 0.5;
817
+ let b = Infinity;
818
+ for (let i = 1; i <= 60; i++) {
819
+ const v = Math.min(f(sMax * 2 ** -i), f(sMax * (1 - 2 ** -i)));
820
+ if (v < b)
821
+ b = v;
822
+ }
823
+ return b;
824
+ }
815
825
  export function qnf1(p, m, n, ncp, lowerTail, logP) {
816
826
  if (Number.isNaN(p) || Number.isNaN(m) || Number.isNaN(n) || Number.isNaN(ncp))
817
827
  return NaN;
@@ -831,6 +841,26 @@ export function qnf1(p, m, n, ncp, lowerTail, logP) {
831
841
  function qnfRaw(p, m, n, ncp, lowerTail, logP) {
832
842
  const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
833
843
  const cdfAt = memoCdf((x, l, log) => pnfRaw(x, m, n, ncp, l, log));
844
+ // The Chernoff bounds at the smallest and largest doubles (fChernoffLog), which decide the ends where the distribution
845
+ // function there is NaN (out of the range of its methods): where the bound B on log P[F <= MAX] is below the target,
846
+ // no double reaches a lower-tail target, and the upper tail stays above 1 - e^B, above an upper-tail target (at most
847
+ // 1/2) where B < -log 2; likewise at the smallest double. For a subnormal df1 and a huge λ, F lies beyond the doubles
848
+ // but for the point mass e^(-λ/2) at 0 (R does not converge, and the search over NaN took seconds: qf(5.4e-11, 3.7e-313,
849
+ // 1249, { ncp: 1.7e243 }))
850
+ if (fChernoffLog(Number.MAX_VALUE, m, n, ncp, true) < (lower ? logTarget : -Math.LN2))
851
+ return Infinity;
852
+ if (fChernoffLog(Number.MIN_VALUE, m, n, ncp, false) < (lower ? -Math.LN2 : logTarget))
853
+ return 0;
854
+ // A df2 so large that W = χ²_n/n is 1 on the scale of the quantile: the noncentral χ² limit, as for df2 = Inf. log W
855
+ // has a spread s = sqrt(2/n); it moves the quantile of log F by at most about s (sqrt(2 |log p|) + 1) where W alone
856
+ // would make the tail, and by s² L otherwise, L = |d log P/d log x| of the limit, at most of the order of
857
+ // m + 2 |log p| + 2 sqrt((m + λ)(|log p| + 1)) (the power m/2 near 0, the exponential tail, the normal bulk of a large
858
+ // λ); both are below 1e-20 here. (Out of the range of the distribution function, at both a huge df2 and λ, the search
859
+ // took seconds: qf(exp(-385.8), 1.05, 1.1e264, { ncp: 1.7e262 }))
860
+ const lp = -logTarget;
861
+ if (n > 1e40 * (lp + 1) && n > 1e20 * (m + 2 * lp + 1e3 + 4 * Math.sqrt((m + ncp) * (lp + 1)))) {
862
+ return qnchisq1(p, m, ncp, lowerTail, logP) / m;
863
+ }
834
864
  const atMin = cdfAt(Number.MIN_VALUE, lower, true);
835
865
  if (lower ? atMin >= logTarget : atMin <= logTarget)
836
866
  return 0;
@@ -841,7 +871,16 @@ function qnfRaw(p, m, n, ncp, lowerTail, logP) {
841
871
  // means of the χ² variables per degree of freedom) where the parameters are out of the central quantile's range
842
872
  const nu = (m + ncp) * ((m + ncp) / (m + 2 * ncp));
843
873
  const central = method(m / 2, n / 2, ncp) === "mixture" && inCentralRange(m) && inCentralRange(n);
844
- let x0 = ((m + ncp) / m) * (central ? qf1(logTarget, nu, n, lower, true) : 1);
874
+ // Out of the mixture's range (λ/2 large) χ²(m, λ)/m is (m + λ)/m to within its spread of order 1/sqrt(λ), so F is
875
+ // that times n/χ²_n, the F quantile at df1 = Inf (from (m + λ)/m alone the search took 1 s, through an integral of
876
+ // 0.5 s far in the left tail: qf(3.97e-237, 364.4, 1.476, { ncp: 1.94e11 }))
877
+ const limit = !central && inCentralRange(n) ? qf1(logTarget, Infinity, n, lower, true) : 1;
878
+ let x0 = ((m + ncp) / m) * (central ? qf1(logTarget, nu, n, lower, true) : limit);
879
+ // For a df2 above the central range, the quantile of the χ² limit (df2 = Inf): from (m + λ)/m the search took seconds
880
+ // in the far tails, at 0.1 s per call of the integral (qf(exp(-645), 314, 5.2e16, { ncp: 2393, lowerTail: false }))
881
+ const limitStart = n > 1e15 && inCentralRange(m);
882
+ if (limitStart)
883
+ x0 = qnchisq1(logTarget, m, ncp, lower, true) / m;
845
884
  if (!(x0 > 0) || !Number.isFinite(x0))
846
885
  x0 = 1;
847
886
  const passes = (x) => (lower ? cdfAt(x, true, true) >= logTarget : cdfAt(x, false, true) <= logTarget);
@@ -860,6 +899,9 @@ function qnfRaw(p, m, n, ncp, lowerTail, logP) {
860
899
  hi: Infinity,
861
900
  positive: true,
862
901
  maxIter: 60,
902
+ // (there the distribution function is the integral, accurate to about 1e-13 (see the known violations), at 0.2 s a
903
+ // call: the steps over its noise took seconds (qf(exp(-3.79), 314.2, 5.2e16, { ncp: 2393 })))
904
+ ...(limitStart && { residualTolerance: 1e-12 * Math.max(1, -logTarget) }),
863
905
  });
864
906
  const search = () => searchDoubles(x, Number.MIN_VALUE, Number.MAX_VALUE, passes);
865
907
  return confirmQuantile(x, cdfAt(x, lower, true), logTarget, search, passes);
@@ -16,7 +16,7 @@
16
16
  // (asymptoticMethod). Quantiles whose distribution function steps within a few doubles end in a search over the doubles.
17
17
  import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
18
18
  import { invertContinuous } from "../internal/invert.js";
19
- import { kronrodAdaptive, narrowQuantile, quadratureControl, searchDoubles } from "../internal/noncentral.js";
19
+ import { kronrodAdaptive, narrowQuantile, negligibleNcp, quadratureControl, searchDoubles } from "../internal/noncentral.js";
20
20
  import { dpoisLogDD, mixtureCdf, mixtureDensity, mixtureTail } from "../internal/poissonMixture.js";
21
21
  import { gammaInc } from "../special/igam.js";
22
22
  import { dpoisRaw } from "../special/loader.js";
@@ -27,11 +27,13 @@ import { log1pmx } from "../special/log1pmx.js";
27
27
  import { dgamma1, qgamma1 } from "./gamma.js";
28
28
  import { qnorm } from "./normal.js";
29
29
  function gammaKernel(a, y) {
30
- // For very small y the ratio y/(a+j) underflows, so keep it scaled by 2^500
30
+ // For very small y the ratio y/(a+j) underflows, so keep it scaled by 2^500; for a huge y scaled by 2^-500 (the
31
+ // splitting in the double-double quotient overflowed: dchisq(3e307, 0.5, { ncp: 2.2e-320 }) was NaN)
31
32
  const tiny = y < 1e-250;
32
- const ys = tiny ? y * 2 ** 500 : y;
33
+ const huge = y > 1e250;
34
+ const ys = tiny ? y * 2 ** 500 : huge ? y * 2 ** -500 : y;
33
35
  return {
34
- ratioK: tiny ? -1 : 0,
36
+ ratioK: tiny ? -1 : huge ? 1 : 0,
35
37
  cdf: (j, lower, log) => gammaInc(a + j, y, lower, log),
36
38
  inc: (j, log) => {
37
39
  const [s, corr] = incCorrection(a, j, y);
@@ -82,6 +84,19 @@ function pnchisqBoth(x, df, ncp, lower) {
82
84
  const method = asymptoticMethod(df, besselArg(x, ncp));
83
85
  if (method !== undefined)
84
86
  return method === "hankel" ? besselTails(x, df, ncp, lower) : debyeTails(x, df, ncp, lower);
87
+ const lt = subnormalHalf(x);
88
+ if (lt !== undefined) {
89
+ // P[X <= x] = P[X <= x2] (x/x2)^(df/2) (see subnormalHalf); the upper tail as Q(x2) - P(x2) ((x/x2)^(df/2) - 1),
90
+ // where the change is small beside Q(x2) (it is at most of the order of df/2, Q(x2) at least of df/2 log(2/x))
91
+ const x2 = 2 * (x / 2);
92
+ const e = Math.expm1((df / 2) * lt);
93
+ const lo = pnchisqBoth(x2, df, ncp, true);
94
+ if (lower)
95
+ return { lin: lo.lin * (1 + e), log: lo.log + (df / 2) * lt };
96
+ const up = pnchisqBoth(x2, df, ncp, false);
97
+ const r = (lo.lin * e) / up.lin;
98
+ return Number.isFinite(r) ? { lin: up.lin - lo.lin * e, log: up.log + Math.log1p(-r) } : up;
99
+ }
85
100
  // x so small that x/2 underflows to 0 (the smallest subnormal) is approximated by the smallest positive number
86
101
  const kernel = gammaKernel(df / 2, Math.max(x / 2, Number.MIN_VALUE));
87
102
  if (mu > 1e8 && !lower) {
@@ -100,6 +115,18 @@ function pnchisqBoth(x, df, ncp, lower) {
100
115
  // (the lower tail is likely the smaller below the mean k + λ)
101
116
  return mixtureTail(mu, kernel, lower, x < df + ncp);
102
117
  }
118
+ /**
119
+ * log(x / x2) for a subnormal x with an odd last bit, where y = x/2 rounds to x2/2 (by 2.5e-324: 1e-9 of x near 1e-315,
120
+ * 3% at 1.6e-322, more than the accuracy of the mixture; with its density, the differences of pchisq were 2e-6 off at
121
+ * pchisq(7.02e-315, 0.5, { ncp: 24.3 })); undefined where x/2 is exact or 0. With y that small, e^-y and the series
122
+ * of the components in y are 1 to double precision, so each component scales as y^(k/2 + j) (P[X <= x], density
123
+ * times y), and the mixture as y^(k/2) times 1 + O(λ |x - x2|) (j > 0 adds j to the power, with weights of order
124
+ * (λ x)^j; λ |x - x2| is below 1e-15): the values at x come from those at x2 by the factor (x/x2)^(k/2)
125
+ */
126
+ function subnormalHalf(x) {
127
+ const x2 = 2 * (x / 2);
128
+ return x2 > 0 && x2 !== x ? Math.log1p((x - x2) / x2) : undefined;
129
+ }
103
130
  function pnchisqRaw(x, df, ncp, lower, log) {
104
131
  const v = pnchisqBoth(x, df, ncp, lower);
105
132
  return log ? v.log : v.lin;
@@ -132,6 +159,12 @@ function dnchisqRaw(x, df, ncp, log) {
132
159
  const l = debyeAt(x, df, ncp).logf;
133
160
  return log ? l : Math.exp(l);
134
161
  }
162
+ const lt = subnormalHalf(x);
163
+ if (lt !== undefined) {
164
+ // the density at x2 times (x/x2)^(k/2 - 1) (subnormalHalf)
165
+ const x2 = 2 * (x / 2);
166
+ return log ? dnchisqRaw(x2, df, ncp, true) + (a - 1) * lt : dnchisqRaw(x2, df, ncp, false) * Math.exp((a - 1) * lt);
167
+ }
135
168
  // The ratio of terms w_j g_j is mu y / (j (a + j - 1)) = (z/2)² / (j (a + j - 1)); the peak is at the j where it
136
169
  // equals 1, the positive root of j² + (a - 1) j - (z/2)² (written without cancellation for a > 1)
137
170
  const jStar = a > 1 ? (z * z) / 2 / (a - 1 + Math.hypot(a - 1, z)) : (-(a - 1) + Math.hypot(a - 1, z)) / 2;
@@ -175,6 +208,11 @@ function rootDiff(x, ncp) {
175
208
  }
176
209
  /** sqrt(x) in double-double (one Newton correction of the rounded root) */
177
210
  function sqrtDD(x) {
211
+ // (from x/4, scaled back exactly, near the largest double, where s² can round to Infinity)
212
+ if (x > 1e300) {
213
+ const [h, l] = sqrtDD(x / 4);
214
+ return [2 * h, 2 * l];
215
+ }
178
216
  const s = Math.sqrt(x);
179
217
  const [p, pe] = twoProd(s, s);
180
218
  return [s, (x - p - pe) / (2 * s)];
@@ -208,8 +246,9 @@ function besselAsymptotic(x, df, ncp, log, dGiven) {
208
246
  const d = dd[0] + dd[1];
209
247
  const [p, pe] = squareDD(dd);
210
248
  const hi = -p / 2;
211
- // (ν/2) log(x/λ) = ν log(sqrt(x)/sqrt(λ))
212
- const rest = -Math.LN2 - pe / 2 + nu * logRootRatio(d, rl, x) - 0.5 * Math.log(2 * Math.PI * z) + Math.log(s);
249
+ // (ν/2) log(x/λ) = ν log(sqrt(x)/sqrt(λ)); log sqrt(2πz) in two terms (2πz overflows near 1e308: the log density at
250
+ // dchisq(1.79e308, 3, { ncp: 1e308 }) was -Infinity)
251
+ const rest = -Math.LN2 - pe / 2 + nu * logRootRatio(d, rl, x) - LN_SQRT_2PI - 0.5 * Math.log(z) + Math.log(s);
213
252
  if (log)
214
253
  return hi + rest;
215
254
  return hi > -700 ? Math.exp(hi) * Math.exp(rest) : Math.exp(hi + rest);
@@ -343,10 +382,16 @@ function debyeAt(x, df, ncp, nGiven) {
343
382
  const z = besselArg(x, ncp);
344
383
  const R = Math.hypot(nu, z);
345
384
  const rn = R + nu;
346
- // (nGiven: N from the caller, for an x known more precisely than its double)
347
- const N = nGiven ?? ddSum(ddAdd(ddAdd(twoSum(ncp, df), [-2, 0]), [-x, 0]));
385
+ // (nGiven: N from the caller, for an x known more precisely than its double; in halves above x = 1e307, where the
386
+ // error terms of the double-double sums overflowed: pchisq(1.8e308, 3.3e268, { ncp: 6.9e307 }) was NaN)
387
+ const N = nGiven ??
388
+ (x > 1e307
389
+ ? 2 * ddSum(ddAdd(ddAdd(twoSum(ncp / 2, df / 2), [-1, 0]), [-x / 2, 0]))
390
+ : ddSum(ddAdd(ddAdd(twoSum(ncp, df), [-2, 0]), [-x, 0])));
348
391
  const D = x + z * (z / rn);
349
- const e = -(x / D) * (N / rn);
392
+ // (where D overflows, as x + λ x / (R + ν) can for x and λ near 1e308, x / D = (R + ν) / (R + ν + λ), in halves:
393
+ // pchisq(1e308, 3.3e268, { ncp: 6.9e307 }) was 0.55 and NaN beyond)
394
+ const e = D < Infinity ? -(x / D) * (N / rn) : -(N / 2) / (rn / 2 + ncp / 2);
350
395
  const ls = Math.log(debyeSeries(nu, nu / R));
351
396
  // log(1 + e) - e; far below the mode e rounds to -1, and log(1 + e) = log r is taken from x and ν + R
352
397
  const lm = e > -0.5 ? log1pmx(e) : Math.log(x) - Math.log(rn) - e;
@@ -393,8 +438,8 @@ function debyeTails(x, df, ncp, lower, nGiven) {
393
438
  a = b;
394
439
  w *= 2;
395
440
  }
396
- // g(r0) = f(x0) 2 R0
397
- const lt = logf + Math.log(2 * R0) + Math.log(sum);
441
+ // g(r0) = f(x0) 2 R0 (log 2 + log R0: 2 R0 overflows for x and λ near 1e308)
442
+ const lt = logf + Math.LN2 + Math.log(R0) + Math.log(sum);
398
443
  const tail = Math.exp(lt);
399
444
  if (up !== lower)
400
445
  return { lin: tail, log: lt };
@@ -417,6 +462,8 @@ export function dnchisq1(x, df, ncp, log) {
417
462
  return log ? -ncp / 2 - Math.LN2 : Math.exp(-ncp / 2) / 2;
418
463
  return log ? -Infinity : 0;
419
464
  }
465
+ if (negligibleNcp(ncp / 2, x / df))
466
+ return dgamma1(x, df / 2, 2, log);
420
467
  return dnchisqRaw(x, df, ncp, log);
421
468
  }
422
469
  /** The value of a double-double */
@@ -33,8 +33,13 @@ function dnbinomRaw(x, par, log) {
33
33
  if (x === 0)
34
34
  return b;
35
35
  const r = size / (size + x);
36
- return log ? b + Math.log(r) : b * r;
36
+ if (r >= MIN_NORMAL)
37
+ return log ? b + Math.log(r) : b * r;
38
+ // A subnormal size / (size + x) has lost digits (dnbinom(58, 3.94e-315, { prob: 0.125, log: true }) was 7.5e-12 off,
39
+ // 2.4e-8 at x = 57): log size - log(size + x), and the division by size + x before the product with size
40
+ return log ? b + (Math.log(size) - Math.log(size + x)) : (b / (size + x)) * size;
37
41
  }
42
+ const MIN_NORMAL = 2 ** -1022;
38
43
  /** P[X <= x] (lower) / P[X > x] (!lower) (x a nonnegative integer) */
39
44
  function pnbinomRaw(x, par, lower, log) {
40
45
  const up = upperSeries(x, par);
@@ -124,7 +124,32 @@ function dnorm1(x, mean, sd, log) {
124
124
  if (log)
125
125
  return -(LN_SQRT_2PI + 0.5 * z * z + Math.log(sd));
126
126
  // Rounding of z^2 is amplified by the exponential in exp(-z^2/2), so split it as in pnorm
127
- return scaledExpNegHalfSq(Math.abs(z), INV_SQRT_2PI / sd);
127
+ const c = INV_SQRT_2PI / sd;
128
+ return c < Infinity ? scaledExpNegHalfSq(Math.abs(z), c) : tinySdDensity(Math.abs(z), sd);
129
+ }
130
+ /**
131
+ * exp(-a^2/2) / (sd sqrt(2π)) for an sd so far below the normal range that 1 / (sd sqrt(2π)) overflows (it was NaN, as
132
+ * Infinity times exp(-a^2/2) = 0: dnorm(4.85e-319, { sd: 1.7e-322 })), with the split of scaledExpNegHalfSq: sd is
133
+ * scaled by 2^600 (exactly) and the factor 2^600 put back where neither the partial product overflows nor its last
134
+ * factor exp(-700) makes it subnormal
135
+ */
136
+ function tinySdDensity(a, sd) {
137
+ if (a > 1e10)
138
+ return 0;
139
+ const hi = Math.floor(a * 16) / 16;
140
+ const lo = a - hi;
141
+ const halfHiSq = (hi * hi) / 2;
142
+ const m = (INV_SQRT_2PI / (sd * 2 ** 600)) * Math.exp((-lo * (a + hi)) / 2);
143
+ if (halfHiSq <= 700)
144
+ return m * Math.exp(-halfHiSq) * 2 ** 600;
145
+ if (halfHiSq > 1400) {
146
+ // (exp(700 - a^2/2) is itself subnormal from a^2/2 = 1408 on, where the density can still be normal: dnorm(-1.59e-321,
147
+ // { sd: 3e-323 }) was 3e-4 off. m exp(1400 - a^2/2) is above e^276 wherever the density is normal, so each partial
148
+ // product below stays normal until the last factor)
149
+ return m * Math.exp(1400 - halfHiSq) * Math.exp(-700) * 2 ** 600 * Math.exp(-700);
150
+ }
151
+ const t = m * Math.exp(700 - halfHiSq);
152
+ return t >= 1 ? t * Math.exp(-700) * 2 ** 600 : t * 2 ** 600 * Math.exp(-700);
128
153
  }
129
154
  /**
130
155
  * Density of the normal distribution. Equivalent to R's `dnorm(x, mean = 0, sd = 1, log = FALSE)`.
@@ -1,3 +1,28 @@
1
- export declare function dnt1(x: number, df: number, ncp: number, log: boolean): number;
2
- export declare function pnt1(q: number, df: number, ncp: number, lowerTail: boolean, logP: boolean): number;
3
- export declare function qnt1(p: number, df: number, ncp: number, lowerTail: boolean, logP: boolean): number;
1
+ /**
2
+ * Above this df the central and noncentral t are taken from their limits (hugeDf*; shared with t.ts). The t
3
+ * differs from the normal by terms of relative order t²/ν and 1/ν: where y = t²/ν < 1e-17 it is the normal to the
4
+ * double (the absolute error in the log, about t⁴/ν, is below 1e-180 wherever the linear value is above the
5
+ * underflow), and beyond, |t| > 3e91, the log of the far tail and of the density is -(ν/2) log1p(y) up to terms
6
+ * below 1e-179 of it. The general code fails from about 1.3e300 on (NaN where its intermediate terms overflow)
7
+ */
8
+ export declare const HUGE_DF = 1e+200;
9
+ /** y = t²/ν (t² may overflow on its own) */
10
+ export declare function hugeDfRatio(t: number, n: number): number;
11
+ /**
12
+ * Log of the far tail beyond |t| and of the density at t (relative error about eps) for n > HUGE_DF and
13
+ * hugeDfRatio(t, n) >= 1e-17: -(n/2) log1p(t²/n), formed without overflow
14
+ */
15
+ export declare function hugeDfTailLog(t: number, n: number): number;
16
+ /**
17
+ * |t| with hugeDfTailLog(t, n) = logTarget (logTarget < 0), Infinity beyond the largest double: t² = n expm1(-2 logTarget / n).
18
+ * (sqrt(n) is taken apart: through exp(log t) its log, up to 354, would multiply the rounding error)
19
+ */
20
+ export declare function hugeDfTailQuantile(logTarget: number, n: number): number;
21
+ /**
22
+ * P[T <= t] (lower) / P[T > t] for n > HUGE_DF and hugeDfRatio(t, n) >= 1e-17 from hugeDfTailLog: the tail beyond |t|
23
+ * is its exp (0), the other is 1
24
+ */
25
+ export declare function hugeDfTail(t: number, n: number, lowerTail: boolean, logP: boolean): number;
26
+ export declare function dnt1(x: number, df0: number, ncp: number, log: boolean): number;
27
+ export declare function pnt1(q: number, df0: number, ncp: number, lowerTail: boolean, logP: boolean): number;
28
+ export declare function qnt1(p: number, df0: number, ncp: number, lowerTail: boolean, logP: boolean): number;