@numero-uno-jpn/rstats 0.15.1 → 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 (88) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.ja.md +8 -8
  3. package/README.md +8 -8
  4. package/dist/chunks/{chunk-735NNFAS.js → chunk-3THFTB36.js} +2 -2
  5. package/dist/chunks/chunk-3TSWYE5L.js +2 -0
  6. package/dist/chunks/{chunk-7J4HW6VF.js → chunk-3WPU6LGK.js} +2 -2
  7. package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-5ITR67MB.js} +2 -2
  8. package/dist/chunks/chunk-5NRFU6F6.js +2 -0
  9. package/dist/chunks/{chunk-SAZAEPZC.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-5OSQ76OO.js → chunk-HD73UKPT.js} +2 -2
  13. package/dist/chunks/{chunk-4ILI2FSB.js → chunk-IUULP3LE.js} +1 -1
  14. package/dist/chunks/{chunk-LY2T6DA6.js → chunk-JQG3YQOC.js} +2 -2
  15. package/dist/chunks/chunk-KT66SB7R.js +2 -0
  16. package/dist/chunks/{chunk-IQOVOHZG.js → chunk-MAUAOQG3.js} +2 -2
  17. package/dist/chunks/{chunk-GEJCBCMP.js → chunk-MDYE7QP2.js} +2 -2
  18. package/dist/chunks/{chunk-5SQFDLNG.js → chunk-NM3Z2UBQ.js} +2 -2
  19. package/dist/chunks/{chunk-5XQZNKHZ.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-3QTVXDUM.js → chunk-USPEBXSR.js} +2 -2
  23. package/dist/chunks/{chunk-H4CHJMFU.js → chunk-VLUL2HFW.js} +1 -1
  24. package/dist/chunks/{chunk-VOIJKNL2.js → chunk-ZIX7NYQF.js} +1 -1
  25. package/dist/descriptive.js +2 -2
  26. package/dist/distributions/arcsine.js +48 -4
  27. package/dist/distributions/beta.d.ts +1 -1
  28. package/dist/distributions/beta.js +75 -2
  29. package/dist/distributions/cauchy.js +76 -5
  30. package/dist/distributions/exponential.js +16 -2
  31. package/dist/distributions/f.js +143 -24
  32. package/dist/distributions/gamma.d.ts +13 -2
  33. package/dist/distributions/gamma.js +122 -12
  34. package/dist/distributions/geometric.js +19 -3
  35. package/dist/distributions/invgamma.js +60 -10
  36. package/dist/distributions/kumaraswamy.js +116 -17
  37. package/dist/distributions/laplace.js +5 -2
  38. package/dist/distributions/logistic.js +8 -2
  39. package/dist/distributions/lognormal.js +40 -5
  40. package/dist/distributions/nbeta.js +597 -52
  41. package/dist/distributions/nchisq.d.ts +25 -0
  42. package/dist/distributions/nchisq.js +381 -96
  43. package/dist/distributions/negbinomial.js +26 -6
  44. package/dist/distributions/normal.js +29 -1
  45. package/dist/distributions/nt.d.ts +28 -3
  46. package/dist/distributions/nt.js +401 -63
  47. package/dist/distributions/pareto.js +73 -19
  48. package/dist/distributions/t.js +102 -12
  49. package/dist/distributions/triangular.js +93 -27
  50. package/dist/distributions/tukey.js +110 -50
  51. package/dist/distributions/uniform.js +14 -9
  52. package/dist/distributions/weibull.js +65 -10
  53. package/dist/distributions.js +2 -2
  54. package/dist/internal/ddexp.d.ts +13 -0
  55. package/dist/internal/ddexp.js +55 -3
  56. package/dist/internal/discrete.js +30 -7
  57. package/dist/internal/invert.d.ts +5 -0
  58. package/dist/internal/invert.js +59 -5
  59. package/dist/internal/noncentral.d.ts +41 -0
  60. package/dist/internal/noncentral.js +124 -2
  61. package/dist/internal/poissonMixture.d.ts +7 -2
  62. package/dist/internal/poissonMixture.js +142 -38
  63. package/dist/linalg.js +2 -2
  64. package/dist/mixed.js +2 -2
  65. package/dist/numerics.js +2 -2
  66. package/dist/rstats.js +14 -14
  67. package/dist/special/beta.js +7 -0
  68. package/dist/special/dd.d.ts +1 -1
  69. package/dist/special/dd.js +4 -1
  70. package/dist/special/gamma.js +4 -0
  71. package/dist/special/igam.js +126 -21
  72. package/dist/special/incbet.d.ts +10 -0
  73. package/dist/special/incbet.js +218 -48
  74. package/dist/special/incbetLargeA.js +53 -14
  75. package/dist/special/incbetPrefactor.d.ts +57 -0
  76. package/dist/special/incbetPrefactor.js +103 -0
  77. package/dist/special/incbetTemme.js +15 -3
  78. package/dist/special/loader.js +97 -22
  79. package/dist/special.js +2 -2
  80. package/dist/tests.js +2 -2
  81. package/package.json +1 -1
  82. package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
  83. package/dist/chunks/chunk-BDJPSEA7.js +0 -2
  84. package/dist/chunks/chunk-KO6QQPC6.js +0 -2
  85. package/dist/chunks/chunk-OLPTFKIZ.js +0 -3
  86. package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
  87. package/dist/chunks/chunk-UWBYDIOH.js +0 -2
  88. package/dist/chunks/chunk-UZHY5DWN.js +0 -2
@@ -9,35 +9,57 @@
9
9
  // Quantiles use Newton's method starting from Patnaik's (1949) approximation (c χ²_ν, c = (k + 2λ)/(k + λ),
10
10
  // ν = (k + λ)^2/(k + 2λ)).
11
11
  // With a huge λ the mixture is slow and inaccurate far from the centre; there a Chernoff bound decides first whether
12
- // the far tail is below the double range (then it is 0 and the near tail 1). For x λ > 4e18 the density comes from the
13
- // large-argument expansion of the Bessel function and the distribution function from integrating it numerically.
12
+ // the far tail is below the double range (then it is 0 and the near tail 1). Where the mixture's terms spread over
13
+ // too many indices (the Bessel argument z = sqrt(λ x) large, or ν = k/2 - 1 large) the density f(x) =
14
+ // exp(-(x+λ)/2) (x/λ)^(ν/2) I_ν(z) / 2 comes from an asymptotic expansion of the Bessel function (the large-argument
15
+ // one where ν² <= z/100, the uniform one in ν otherwise) and the distribution function from integrating it numerically
16
+ // (asymptoticMethod). Quantiles whose distribution function steps within a few doubles end in a search over the doubles.
14
17
  import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
15
18
  import { invertContinuous } from "../internal/invert.js";
16
- import { dpoisLogDD, mixtureCdf, mixtureCdfBoth, mixtureDensity } from "../internal/poissonMixture.js";
19
+ import { kronrodAdaptive, narrowQuantile, negligibleNcp, quadratureControl, searchDoubles } from "../internal/noncentral.js";
20
+ import { dpoisLogDD, mixtureCdf, mixtureDensity, mixtureTail } from "../internal/poissonMixture.js";
17
21
  import { gammaInc } from "../special/igam.js";
18
22
  import { dpoisRaw } from "../special/loader.js";
19
23
  import { ddAdd, ddDiv, twoProd, twoSum } from "../special/dd.js";
20
24
  import { digamma } from "../special/psigamma.js";
25
+ import { lgamma1p } from "../special/gamma.js";
26
+ import { log1pmx } from "../special/log1pmx.js";
21
27
  import { dgamma1, qgamma1 } from "./gamma.js";
28
+ import { qnorm } from "./normal.js";
22
29
  function gammaKernel(a, y) {
23
- // 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)
24
32
  const tiny = y < 1e-250;
25
- const ys = tiny ? y * 2 ** 500 : y;
33
+ const huge = y > 1e250;
34
+ const ys = tiny ? y * 2 ** 500 : huge ? y * 2 ** -500 : y;
26
35
  return {
27
- ratioK: tiny ? -1 : 0,
36
+ ratioK: tiny ? -1 : huge ? 1 : 0,
28
37
  cdf: (j, lower, log) => gammaInc(a + j, y, lower, log),
29
38
  inc: (j, log) => {
30
39
  const [s, corr] = incCorrection(a, j, y);
40
+ if (s < 1e-8) {
41
+ const l = tinyShapeLog(s, y);
42
+ return log ? l : Math.exp(l);
43
+ }
31
44
  return log ? dpoisRaw(s, y, true) + corr : dpoisRaw(s, y, false) * (1 + corr);
32
45
  },
33
46
  // d_j / d_{j-1} = y / (a + j)
34
47
  ratio: (j) => ddDiv([ys, 0], twoSum(a, j)),
35
48
  incLogDD: (j) => {
36
49
  const [s, corr] = incCorrection(a, j, y);
50
+ if (s < 1e-8)
51
+ return ddAdd([-y, 0], [s * Math.log(y) - lgamma1p(s), 0]);
37
52
  return ddAdd(dpoisLogDD(s, y), [corr, 0]);
38
53
  },
39
54
  };
40
55
  }
56
+ /**
57
+ * log d_0 = s log y - y - log Γ(1 + s) for a tiny shape s (df < 2e-8): dpoisRaw's Stirling form cancels there (its terms
58
+ * are of order log s, 255 at s = 1e-222, and lose that many ulps)
59
+ */
60
+ function tinyShapeLog(s, y) {
61
+ return s * Math.log(y) - y - lgamma1p(s);
62
+ }
41
63
  /**
42
64
  * The shape a + j rounded to double, s, and the first-order change in log d_j from s to the exact a + j:
43
65
  * δ ∂log d/∂s with δ the rounding and ∂log dpois(s; y)/∂s = log y - ψ(s + 1). Far in a tail |log y - ψ(s + 1)| is
@@ -54,28 +76,56 @@ function validParams(df, ncp) {
54
76
  }
55
77
  /**
56
78
  * Linear value and log of P[X <= x] (lower) / P[X > x] (x > 0 finite, ncp > 0).
57
- * The log of a value near 1 is built with log1p from the small value of the other side
79
+ * A value near 1, and its log, are built from the small value of the other side
58
80
  */
59
81
  function pnchisqBoth(x, df, ncp, lower) {
60
82
  const mu = ncp / 2;
61
- // x λ is very large: the peak of the series is too far away, so integrate the density (besselAsymptotic)
62
- if (mu * (x / 2) > 1e18)
63
- return besselTails(x, df, ncp, lower);
83
+ // The series has too many terms (or gammaInc is slow for its huge shapes): integrate an asymptotic form of the density
84
+ const method = asymptoticMethod(df, besselArg(x, ncp));
85
+ if (method !== undefined)
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
+ }
64
100
  // x so small that x/2 underflows to 0 (the smallest subnormal) is approximated by the smallest positive number
65
101
  const kernel = gammaKernel(df / 2, Math.max(x / 2, Number.MIN_VALUE));
66
- const own = mixtureCdfBoth(mu, kernel, lower);
67
- if (own.lin > 0.5) {
68
- // The log of a value near 1 is built with log1p from the small value of the other side (computed only when the log is needed)
102
+ if (mu > 1e8 && !lower) {
103
+ // With a large λ (here x is far below the mean k + λ, as z is small) the upper sum would start about 10 sqrt(λ/2)
104
+ // terms below λ/2 and take as many steps; the lower tail is the small one, so build the upper from it
105
+ const p = mixtureCdf(mu, kernel, true, false);
69
106
  let lg;
70
107
  return {
71
- lin: own.lin,
108
+ lin: 1 - p,
72
109
  get log() {
73
- lg ??= Math.log1p(-mixtureCdf(mu, kernel, !lower, false));
110
+ lg ??= Math.log1p(-p);
74
111
  return lg;
75
112
  },
76
113
  };
77
114
  }
78
- return own;
115
+ // (the lower tail is likely the smaller below the mean k + λ)
116
+ return mixtureTail(mu, kernel, lower, x < df + ncp);
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;
79
129
  }
80
130
  function pnchisqRaw(x, df, ncp, lower, log) {
81
131
  const v = pnchisqBoth(x, df, ncp, lower);
@@ -98,32 +148,107 @@ function memoCdf(df, ncp) {
98
148
  /** Core of the log density (x > 0 finite, ncp > 0) */
99
149
  function dnchisqRaw(x, df, ncp, log) {
100
150
  const a = df / 2;
101
- const y = x / 2;
151
+ // (x so small that x/2 underflows to 0 is approximated by the smallest positive number, as in pnchisqBoth)
152
+ const y = Math.max(x / 2, Number.MIN_VALUE);
102
153
  const mu = ncp / 2;
103
- // The ratio of terms w_j g_j is mu y / (j (a + j - 1)); the peak is at the j where it equals 1
104
- const jStar = (-(a - 1) + Math.sqrt((a - 1) * (a - 1) + 4 * mu * y)) / 2;
105
- if (mu * y > 1e18)
154
+ const z = besselArg(x, ncp);
155
+ const method = asymptoticMethod(df, z);
156
+ if (method === "hankel")
106
157
  return besselAsymptotic(x, df, ncp, log);
158
+ if (method === "debye") {
159
+ const l = debyeAt(x, df, ncp).logf;
160
+ return log ? l : Math.exp(l);
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
+ }
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
169
+ // equals 1, the positive root of j² + (a - 1) j - (z/2)² (written without cancellation for a > 1)
170
+ const jStar = a > 1 ? (z * z) / 2 / (a - 1 + Math.hypot(a - 1, z)) : (-(a - 1) + Math.hypot(a - 1, z)) / 2;
107
171
  // Component density y^(a+j-1) e^-y / Gamma(a+j) = d_j (a + j) / y
108
172
  return mixtureDensity(mu, gammaKernel(a, y), (j) => a + j, jStar, x, log);
109
173
  }
174
+ /** sqrt(λ x), the argument of the Bessel function in the density (without overflow) */
175
+ function besselArg(x, ncp) {
176
+ return Math.sqrt(ncp) * Math.sqrt(x);
177
+ }
178
+ /**
179
+ * Above these Bessel arguments z the asymptotic expansions of I_ν(z) replace the mixture, whose terms spread over
180
+ * about sqrt(z) indices (min(sqrt(z), z / sqrt(ν)) for large ν): the large-argument expansion where it applies, and
181
+ * the uniform expansion in ν for the other ν (then ν > 100)
182
+ */
183
+ const Z_HANKEL = 1e4;
184
+ const Z_ASYMPTOTIC = 1e6;
185
+ /**
186
+ * Above this ν the uniform expansion replaces the mixture at any z (it holds uniformly in z; the incomplete gamma
187
+ * function of the mixture's components can take 0.1 s for shapes of 1e37)
188
+ */
189
+ const NU_UNIFORM = 1e4;
190
+ /**
191
+ * The asymptotic expansion of I_ν(z) used, if any: the large-argument expansion (besselAsymptotic) where ν² <= z/100,
192
+ * so that its terms fall by a factor 200 or more from the first, otherwise the uniform expansion in ν (debyeAt; then
193
+ * ν > 100, where the large-argument expansion diverges for ν² >> z). Undefined: sum the mixture
194
+ */
195
+ function asymptoticMethod(df, z) {
196
+ const nu = df / 2 - 1;
197
+ if (z > Z_HANKEL && nu * nu <= z / 100)
198
+ return "hankel";
199
+ return z > Z_ASYMPTOTIC || nu > NU_UNIFORM ? "debye" : undefined;
200
+ }
201
+ /**
202
+ * sqrt(x) - sqrt(λ) in double-double, as (x - λ)/(sqrt(x) + sqrt(λ)) with the roots in double-double: the rounding of
203
+ * a root would put a relative error of 1e-16 into d, and so an error of 1e-16 d² into the exponent -d²/2 (2e-14 of the
204
+ * density at d = 14)
205
+ */
206
+ function rootDiff(x, ncp) {
207
+ return ddDiv(twoSum(x, -ncp), ddAdd(sqrtDD(x), sqrtDD(ncp)));
208
+ }
209
+ /** sqrt(x) in double-double (one Newton correction of the rounded root) */
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
+ }
216
+ const s = Math.sqrt(x);
217
+ const [p, pe] = twoProd(s, s);
218
+ return [s, (x - p - pe) / (2 * s)];
219
+ }
220
+ /** The square of a double-double, as the pair [p, e] of twoProd with the cross term added to e */
221
+ function squareDD(d) {
222
+ const [p, pe] = twoProd(d[0], d[0]);
223
+ return [p, pe + 2 * d[0] * d[1]];
224
+ }
225
+ /**
226
+ * log(sqrt(x)/sqrt(λ)) = log(1 + d/sqrt(λ)) for d = sqrt(x) - sqrt(λ) (computed without cancellation by the caller).
227
+ * Far below sqrt(λ) the ratio d/sqrt(λ) rounds to -1, so the log is taken of x and λ themselves
228
+ */
229
+ function logRootRatio(d, rl, x) {
230
+ return d / rl > -0.5 ? Math.log1p(d / rl) : 0.5 * Math.log(x) - Math.log(rl);
231
+ }
110
232
  /**
111
233
  * Density for very large x λ. Apply to f(x) = exp(-(x+λ)/2) (x/λ)^(ν/2) I_ν(sqrt(λ x)) / 2 (ν = k/2 - 1) the
112
234
  * large-z asymptotic expansion of I_ν(z), e^z / sqrt(2 pi z) Σ_m (-1)^m a_m(ν) / z^m.
113
- * (The series cannot be summed because its peak j ≈ sqrt(λ x / 4) is too far out. z > 1e9, so the expansion converges in a few terms)
235
+ * (The series cannot be summed because its peak j ≈ sqrt(λ x / 4) is too far out. z > 1e4 and ν² <= z/100, so the
236
+ * expansion converges in a few terms)
114
237
  */
115
- function besselAsymptotic(x, df, ncp, log) {
238
+ function besselAsymptotic(x, df, ncp, log, dGiven) {
116
239
  const nu = df / 2 - 1;
117
- const z = Math.sqrt(ncp * x);
240
+ const z = besselArg(x, ncp);
118
241
  const s = besselSeries(nu, z);
119
- // -(x + λ)/2 + z = -(sqrt(x) - sqrt(λ))^2 / 2, with sqrt(x) - sqrt(λ) written without cancellation (the difference
120
- // of the rounded roots carries an error of ulp(sqrt(λ))) and its square in double-double, as in besselTails
242
+ // -(x + λ)/2 + z = -(sqrt(x) - sqrt(λ))^2 / 2, with sqrt(x) - sqrt(λ) in double-double (rootDiff) and its square
243
+ // too, as in besselTails (dGiven: that difference from the caller, for an x known more precisely than its double)
121
244
  const rl = Math.sqrt(ncp);
122
- const d = (x - ncp) / (Math.sqrt(x) + rl);
123
- const [p, pe] = twoProd(d, d);
245
+ const dd = dGiven ?? rootDiff(x, ncp);
246
+ const d = dd[0] + dd[1];
247
+ const [p, pe] = squareDD(dd);
124
248
  const hi = -p / 2;
125
- // (ν/2) log(x/λ) = ν log(sqrt(x)/sqrt(λ))
126
- const rest = -Math.LN2 - pe / 2 + nu * Math.log1p(d / rl) - 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);
127
252
  if (log)
128
253
  return hi + rest;
129
254
  return hi > -700 ? Math.exp(hi) * Math.exp(rest) : Math.exp(hi + rest);
@@ -162,15 +287,21 @@ function besselTails(x, df, ncp, lower) {
162
287
  const rl = Math.sqrt(ncp);
163
288
  const t0 = Math.sqrt(x);
164
289
  // t0 - sqrt(λ) without cancellation
165
- const d0 = (x - ncp) / (t0 + rl);
166
- const mode = (rl + Math.sqrt(Math.max(ncp + 4 * c, 0))) / 2;
167
- const up = t0 >= mode;
290
+ const d0dd = rootDiff(x, ncp);
291
+ const d0 = d0dd[0] + d0dd[1];
292
+ // Side of the mode (rl + sqrt(λ + 4c))/2, compared through its offset from sqrt(λ): t0 itself rounds to sqrt(λ) for x
293
+ // within an ulp or two of a huge λ, where d0 can still be far from 0
294
+ const modeOffset = ncp + 4 * c >= 0 ? (2 * c) / (Math.sqrt(ncp + 4 * c) + rl) : -rl / 2;
295
+ const up = d0 >= modeOffset;
168
296
  const dir = up ? 1 : -1;
169
297
  const ls0 = Math.log(besselSeries(nu, t0 * rl));
170
298
  // log(g(t0 + dir u) / g(t0)), written with the offset h so that it has no cancellation near u = 0
171
299
  const integrand = (u) => {
172
300
  const h = dir * u;
173
- return Math.exp((-h * (2 * d0 + h)) / 2 + c * Math.log1p(h / t0) + Math.log(besselSeries(nu, (t0 + h) * rl)) - ls0);
301
+ const v = Math.exp((-h * (2 * d0 + h)) / 2 + c * Math.log1p(h / t0) + Math.log(besselSeries(nu, (t0 + h) * rl)) - ls0);
302
+ // Below the mode the integral runs down to t = 0, where the expansion of S fails (z = t sqrt(λ) small); g is
303
+ // negligible there (a factor e^-(z0 - z) or less), so a non-finite value is taken as 0
304
+ return Number.isFinite(v) ? v : 0;
174
305
  };
175
306
  // Rate at which log g falls at distance u from t0 (the derivative of log g, without the O(1/(t sqrt(λ))) part of S)
176
307
  const rate = (u) => -dir * (c / (t0 + dir * u) - (d0 + dir * u));
@@ -180,61 +311,139 @@ function besselTails(x, df, ncp, lower) {
180
311
  let w = 1 / (1 + Math.abs(rate(0)));
181
312
  let a = 0;
182
313
  let sum = 0;
314
+ const control = quadratureControl();
183
315
  for (let i = 0; i < 200 && a < end; i++) {
184
316
  const b = Math.min(a + w, end);
185
- sum += kronrodAdaptive(integrand, a, b, sum, 0);
317
+ sum += kronrodAdaptive(integrand, a, b, sum, 0, control);
186
318
  const r = rate(b);
187
319
  if (r > 0 && integrand(b) <= 1e-17 * r * sum)
188
320
  break;
189
321
  a = b;
190
322
  w *= 2;
191
323
  }
192
- const [p, pe] = twoProd(d0, d0);
324
+ const [p, pe] = squareDD(d0dd);
193
325
  const hi = -p / 2;
194
- const rest = -pe / 2 + c * Math.log1p(d0 / rl) - LN_SQRT_2PI + ls0 + Math.log(sum);
326
+ const rest = -pe / 2 + c * logRootRatio(d0, rl, x) - LN_SQRT_2PI + ls0 + Math.log(sum);
195
327
  const tail = hi > -700 ? Math.exp(hi) * Math.exp(rest) : Math.exp(hi + rest);
196
328
  if (up !== lower)
197
329
  return { lin: tail, log: hi + rest };
198
330
  return { lin: 1 - tail, log: Math.log1p(-tail) };
199
331
  }
200
- // Gauss-Kronrod 7-15 rule (QUADPACK, public domain; as in src/integrate/quadpack.ts): Kronrod nodes, from the end of
201
- // [-1, 1] to the centre, their weights, and the Gauss weights of the odd-numbered nodes
202
- const GK15_X = [
203
- 0.991455371120812639206854697526329, 0.949107912342758524526189684047851, 0.864864423359769072789712788640926,
204
- 0.741531185599394439863864773280788, 0.58608723546769113029414483825873, 0.405845151377397166906606412076961,
205
- 0.207784955007898467600689403773245, 0,
206
- ];
207
- const GK15_WK = [
208
- 0.02293532201052922496373200805897, 0.063092092629978553290700663189204, 0.104790010322250183839876322541518,
209
- 0.140653259715525918745189590510238, 0.16900472663926790282658342659855, 0.190350578064785409913256402421014,
210
- 0.204432940075298892414161999234649, 0.209482141084727828012999174891714,
211
- ];
212
- const GK15_WG = [
213
- 0.129484966168869693270611432679082, 0.27970539148927666790146777142378, 0.381830050505118944950369775488975,
214
- 0.417959183673469387755102040816327,
215
- ];
216
332
  /**
217
- * Integral of f over [a, b] by the 15-point Kronrod rule, bisecting while it differs from the 7-point Gauss rule by
218
- * more than 1e-15 of the total (`total`, the integral accumulated so far, or this part's own value). The difference
219
- * overestimates the error of the Kronrod value by far, so the result is good to about the rounding of the sum
333
+ * Polynomials U_k(p) of the uniform asymptotic expansion of I_ν(ν w) (DLMF 10.41.3, 10.41.10), as coefficients of
334
+ * p^0, p^1, ..., built with the recurrence U_(k+1)(p) = p²(1 - p²) U_k'(p) / 2 + ∫_0^p (1 - 5t²) U_k(t) dt / 8
335
+ * (DLMF 10.41.11). |U_k(p)| <= 1.1 on [0, 1] for k <= 10 (11 at k = 12).
220
336
  */
221
- function kronrodAdaptive(f, a, b, total, depth) {
222
- const m = (a + b) / 2;
223
- const h = (b - a) / 2;
224
- const fc = f(m);
225
- let k = GK15_WK[7] * fc;
226
- let g = GK15_WG[3] * fc;
227
- for (let j = 0; j < 7; j++) {
228
- const fs = f(m - h * GK15_X[j]) + f(m + h * GK15_X[j]);
229
- k += GK15_WK[j] * fs;
230
- if (j % 2 === 1)
231
- g += GK15_WG[(j - 1) / 2] * fs;
337
+ const DEBYE_U = (() => {
338
+ const us = [[1]];
339
+ for (let k = 1; k <= 12; k++) {
340
+ const u = us[k - 1];
341
+ const v = new Array(u.length + 3).fill(0);
342
+ u.forEach((c, i) => {
343
+ if (c === 0)
344
+ return;
345
+ v[i + 1] = v[i + 1] + (i * c) / 2 + c / (8 * (i + 1));
346
+ v[i + 3] = v[i + 3] - (i * c) / 2 - (5 * c) / (8 * (i + 3));
347
+ });
348
+ us.push(v);
232
349
  }
233
- k *= h;
234
- g *= h;
235
- if (Math.abs(k - g) <= 1e-15 * Math.max(Math.abs(k), total) || depth >= 40)
236
- return k;
237
- return kronrodAdaptive(f, a, m, total, depth + 1) + kronrodAdaptive(f, m, b, total, depth + 1);
350
+ return us;
351
+ })();
352
+ /** Σ_k U_k(p) / ν^k of the uniform expansion (ν > 100: the terms left out are below 1e-19) */
353
+ function debyeSeries(nu, p) {
354
+ let s = 1;
355
+ let scale = 1;
356
+ for (let k = 1; k < DEBYE_U.length; k++) {
357
+ scale /= nu;
358
+ if (scale < 1e-20)
359
+ break;
360
+ const c = DEBYE_U[k];
361
+ let v = 0;
362
+ for (let i = c.length - 1; i >= 0; i--)
363
+ v = v * p + c[i];
364
+ s += v * scale;
365
+ }
366
+ return s;
367
+ }
368
+ /**
369
+ * Density for large ν (asymptoticMethod), from the uniform expansion of I_ν(ν w), w = z/ν
370
+ * (DLMF 10.41.3: e^(νη) / (sqrt(2 pi ν) (1 + w²)^(1/4)) Σ_k U_k(p) / ν^k, p = (1 + w²)^(-1/2),
371
+ * νη = R + ν log(z / (ν + R)), R = sqrt(ν² + z²)):
372
+ * log f(x) = -log 2 + E - log sqrt(2 pi) - log(R)/2 + log Σ_k U_k(ν/R) / ν^k,
373
+ * E = -(x + λ)/2 + R + ν log(x / (ν + R)).
374
+ * The terms of E are of order x (1e6 or more here) where E can be of order 1. In r = x/(ν + R) (so that
375
+ * x = r (λ r + 2ν), R = λ r + ν) the exponent is the sum of two terms that are never positive,
376
+ * E = -(λ/2) e² + ν (log(1 + e) - e), e = r - 1,
377
+ * and e = (x - ν - R)/(ν + R) = -x N / (D (ν + R)), N = λ + 2ν - x (in double-double),
378
+ * D = x - ν + R = x + z²/(R + ν), has no cancellation either.
379
+ */
380
+ function debyeAt(x, df, ncp, nGiven) {
381
+ const nu = df / 2 - 1;
382
+ const z = besselArg(x, ncp);
383
+ const R = Math.hypot(nu, z);
384
+ const rn = R + nu;
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])));
391
+ const D = x + z * (z / 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);
395
+ const ls = Math.log(debyeSeries(nu, nu / R));
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
397
+ const lm = e > -0.5 ? log1pmx(e) : Math.log(x) - Math.log(rn) - e;
398
+ const logf = -Math.LN2 - (ncp / 2) * e * e + nu * lm - LN_SQRT_2PI - 0.5 * Math.log(R) + ls;
399
+ return { r: x / rn, e, R, ls, logf };
400
+ }
401
+ /**
402
+ * Linear value and log of P[X <= x] (lower) / P[X > x] for large ν (asymptoticMethod), by integrating the density
403
+ * of debyeAt, as besselTails does in sqrt(x), here in r: dx = 2R dr, so the density in r is
404
+ * g(r) = exp(E) sqrt(R) S / sqrt(2 pi), log g = -(λ/2) e² + ν (log(1 + e) - e) + log(R)/2 - log sqrt(2 pi) + log S,
405
+ * which reduces to that of besselTails for ν = 0 (r = t/sqrt(λ)). log g is concave (its second derivative is
406
+ * -λ - ν/r² up to O(1/R)), with its mode near e = 0. With r = r0 + h,
407
+ * log(g(r)/g(r0)) = -(λ/2) h (h + 2 e0) - ν h e0 / r0 + ν (log(1 + h/r0) - h/r0) + log(1 + λ h / R0)/2 + log(S/S0),
408
+ * whose terms all have the sign of the result away from the mode.
409
+ */
410
+ function debyeTails(x, df, ncp, lower, nGiven) {
411
+ const nu = df / 2 - 1;
412
+ const { r: r0, e: e0, R: R0, ls: ls0, logf } = debyeAt(x, df, ncp, nGiven);
413
+ // Derivative of log g at r = r0 + h (without the O(1/R²) part of S)
414
+ const slope = (h) => -(e0 + h) * (ncp + nu / (r0 + h)) + ncp / (2 * (R0 + ncp * h));
415
+ const up = slope(0) <= 0;
416
+ const dir = up ? 1 : -1;
417
+ const integrand = (u) => {
418
+ const h = dir * u;
419
+ const R = R0 + ncp * h;
420
+ const v = Math.exp(-(ncp / 2) * h * (h + 2 * e0) - (nu * h * e0) / r0 + nu * log1pmx(h / r0) + 0.5 * Math.log1p((ncp * h) / R0) +
421
+ Math.log(debyeSeries(nu, nu / R)) - ls0);
422
+ return Number.isNaN(v) ? 0 : v;
423
+ };
424
+ const rate = (u) => -dir * slope(dir * u);
425
+ // Integrate over intervals of doubling width from about the width over which g falls by e^-1; the rest beyond u is
426
+ // at most g(u)/rate(u) (log-concavity)
427
+ const end = up ? Infinity : r0;
428
+ let w = 1 / (Math.abs(rate(0)) + Math.hypot(Math.sqrt(ncp), Math.sqrt(nu) / r0));
429
+ let a = 0;
430
+ let sum = 0;
431
+ const control = quadratureControl();
432
+ for (let i = 0; i < 200 && a < end; i++) {
433
+ const b = Math.min(a + w, end);
434
+ sum += kronrodAdaptive(integrand, a, b, sum, 0, control);
435
+ const rb = rate(b);
436
+ if (rb > 0 && integrand(b) <= 1e-17 * rb * sum)
437
+ break;
438
+ a = b;
439
+ w *= 2;
440
+ }
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);
443
+ const tail = Math.exp(lt);
444
+ if (up !== lower)
445
+ return { lin: tail, log: lt };
446
+ return { lin: 1 - tail, log: Math.log1p(-tail) };
238
447
  }
239
448
  export function dnchisq1(x, df, ncp, log) {
240
449
  if (Number.isNaN(x) || Number.isNaN(df) || Number.isNaN(ncp))
@@ -253,8 +462,51 @@ export function dnchisq1(x, df, ncp, log) {
253
462
  return log ? -ncp / 2 - Math.LN2 : Math.exp(-ncp / 2) / 2;
254
463
  return log ? -Infinity : 0;
255
464
  }
465
+ if (negligibleNcp(ncp / 2, x / df))
466
+ return dgamma1(x, df / 2, 2, log);
256
467
  return dnchisqRaw(x, df, ncp, log);
257
468
  }
469
+ /** The value of a double-double */
470
+ function ddSum(v) {
471
+ return v[0] + v[1];
472
+ }
473
+ /**
474
+ * log f(x) at x = x0 e^v, with the offset v taken exactly where the density varies faster than the rounding of x can
475
+ * follow: in the asymptotic forms the exponent depends on x through sqrt(x) - sqrt(λ) or λ + 2ν - x, which are formed
476
+ * from x0 and v (by expm1) instead of from the double x. Used by integrals over log x (src/distributions/nbeta.ts),
477
+ * where x0 e^v rounded would put an error of eps |d log f / d log x| (eps sqrt(λ) near the mode) into each node.
478
+ */
479
+ export function dnchisqLogNear(x0, v, df, ncp) {
480
+ const x = Math.abs(v) < 700 ? x0 * Math.exp(v) : Math.exp(Math.log(x0) + v);
481
+ if (!(x > 0 && x < Infinity) || !validParams(df, ncp) || ncp === 0)
482
+ return dnchisq1(x, df, ncp, true);
483
+ const method = asymptoticMethod(df, besselArg(x, ncp));
484
+ if (method === "hankel") {
485
+ return besselAsymptotic(x, df, ncp, true, ddAdd(rootDiff(x0, ncp), [Math.sqrt(x0) * Math.expm1(v / 2), 0]));
486
+ }
487
+ if (method === "debye") {
488
+ const n0 = ddAdd(ddAdd(twoSum(ncp, df), [-2, 0]), [-x0, 0]);
489
+ return debyeAt(x, df, ncp, ddSum(ddAdd(n0, [-x0 * Math.expm1(v), 0]))).logf;
490
+ }
491
+ return dnchisqRaw(x, df, ncp, true);
492
+ }
493
+ /**
494
+ * P(s, t) (lower) or Q(s, t) of the central gamma distribution (its log if log) for a large shape (s > 1e4), as the
495
+ * central χ² with 2s degrees of freedom through debyeTails (λ = 0; the uniform expansion then reduces to Stirling's
496
+ * series), at t = t0 e^v with the offset v exact: N = 2s - 2 - 2t is formed from t0 and v (src/distributions/nbeta.ts
497
+ * integrates over log t, where the rounding of t0 e^v would carry an error of eps sqrt(s) into the log). The
498
+ * incomplete gamma function can take 40 ms for such shapes (s = 1e12, t < s; 0.1 s at s = 1e37)
499
+ */
500
+ export function gammaTailNear(s, t0, v, lower, log) {
501
+ const t = Math.abs(v) < 700 ? t0 * Math.exp(v) : Math.exp(Math.log(t0) + v);
502
+ if (!(t > 0))
503
+ return lower ? (log ? -Infinity : 0) : log ? 0 : 1;
504
+ if (t === Infinity)
505
+ return lower ? (log ? 0 : 1) : log ? -Infinity : 0;
506
+ const n = ddAdd(ddAdd(twoSum(2 * s, -2), [-2 * t0, 0]), [-2 * t0 * Math.expm1(v), 0]);
507
+ const r = debyeTails(2 * t, 2 * s, 0, lower, ddSum(n));
508
+ return log ? r.log : r.lin;
509
+ }
258
510
  export function pnchisq1(q, df, ncp, lowerTail, logP) {
259
511
  if (Number.isNaN(q) || Number.isNaN(df) || Number.isNaN(ncp))
260
512
  return NaN;
@@ -306,22 +558,43 @@ export function negligibleTail(far, lowerTail, logP) {
306
558
  * Both exponents are convex in s and are smallest at u = 1 ± 2s = (k + sqrt(k² + 4qλ)) / (2q), the positive root of
307
559
  * q u² - k u - λ = 0: u > 1 (q below the mean k + λ) for the lower tail, u < 1 for the upper. Returns the tail whose
308
560
  * bound is below the double range.
561
+ * Near the mean the terms of the exponents (of order q s) cancel; with the root they reduce to
562
+ * -2q s² - (k/2) (log(1 ± 2s) ∓ 2s), s = |λ + k - q| / (2q + 4qλ / (sqrt(k² + 4qλ) + k)),
563
+ * whose terms are both of order s² (q s² and k s²) instead of q s (λ + k - q in double-double).
309
564
  */
310
565
  function chisqTailBound(q, df, ncp) {
311
- const u = (df + Math.sqrt(df * df + 4 * q * ncp)) / (2 * q);
312
- if (!Number.isFinite(u))
566
+ const [h, l] = ddAdd(twoSum(ncp, df), [-q, 0]);
567
+ const diff = h + l;
568
+ const root = Math.hypot(df, 2 * Math.sqrt(q) * Math.sqrt(ncp));
569
+ const s = Math.abs(diff) / (2 * q + 4 * q * (ncp / (root + df)));
570
+ if (!(s > 0) || !Number.isFinite(s))
313
571
  return undefined;
314
- if (u > 1) {
315
- const s = (u - 1) / 2;
316
- const lb = s * q - (df / 2) * Math.log(u) - (ncp * s) / u;
317
- return lb < LOG_NEGLIGIBLE ? "lower" : undefined;
318
- }
319
- if (u < 1 && u > 0) {
320
- const s = (1 - u) / 2;
321
- const lb = -s * q - (df / 2) * Math.log(u) + (ncp * s) / u;
322
- return lb < LOG_NEGLIGIBLE ? "upper" : undefined;
323
- }
324
- return undefined;
572
+ const sign = diff > 0 ? 1 : -1;
573
+ // The reduced form cancels in turn for large s, where q is far below the mean: there the direct form, with
574
+ // u = 1 ± 2s (s can be near the overflow threshold for a tiny q, so log u and λs/u are formed without u)
575
+ const lb = s <= 0.25
576
+ ? -2 * q * s * s - (df / 2) * log1pmx(2 * sign * s)
577
+ : sign > 0
578
+ ? s * q - (df / 2) * (Math.LN2 + Math.log(s + 0.5)) - ncp / (2 + 1 / s)
579
+ : -s * q - (df / 2) * Math.log1p(-2 * s) + (ncp * s) / (1 - 2 * s);
580
+ if (!(lb < LOG_NEGLIGIBLE))
581
+ return undefined;
582
+ return diff > 0 ? "lower" : "upper";
583
+ }
584
+ /**
585
+ * The quantile x found by the Newton iteration if its log probability lp is within 1e-15 of the target, or within 1e-9
586
+ * and x is the quantile to 1e-14 (x reaches the target and x (1 - 1e-14) does not: where the distribution function is
587
+ * nearly flat in log x the iteration can stall 1e-8 away in log p, and where it is steep in log x, 1e-12 in log p can
588
+ * be 4e-13 in x), otherwise that of `search`. The iteration fails where the distribution function is too steep to be
589
+ * resolved by the doubles (for a huge df or ncp the spread can be below the spacing of x, so that the function steps
590
+ * from 0 to 1/2 at one double); a search over the doubles (searchDoubles) then gives the smallest x that reaches the
591
+ * target
592
+ */
593
+ export function confirmQuantile(x, lp, logTarget, search, passes) {
594
+ const r = Math.abs(lp - logTarget) / Math.max(1, Math.abs(logTarget));
595
+ if (r <= 1e-15 || (r <= 1e-9 && passes(x) && !passes(x * (1 - 1e-14))))
596
+ return x;
597
+ return search();
325
598
  }
326
599
  export function qnchisq1(p, df, ncp, lowerTail, logP) {
327
600
  if (Number.isNaN(p) || Number.isNaN(df) || Number.isNaN(ncp))
@@ -342,26 +615,36 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
342
615
  return qgamma1(p, df / 2, 2, lowerTail, logP);
343
616
  const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
344
617
  const cdf = memoCdf(df, ncp);
345
- // If the distribution function at the smallest positive number already passes the target, the answer is 0 (includes the point mass for df = 0)
346
- // (the upper tail there is built from the lower one: the series for the upper tail itself takes seconds for λ = 1e12)
347
- const atMin = pnchisqRaw(Number.MIN_VALUE, df, ncp, true, lower);
348
- if (lower ? atMin >= logTarget : Math.log1p(-atMin) <= logTarget)
618
+ // If the distribution function at the smallest positive number already passes the target, the answer is 0 (includes
619
+ // the point mass for df = 0). (For a large λ the upper tail there is built from the lower one: pnchisqBoth)
620
+ const atMin = pnchisqRaw(Number.MIN_VALUE, df, ncp, lower, true);
621
+ if (lower ? atMin >= logTarget : atMin <= logTarget)
349
622
  return 0;
350
- // Patnaik's approximation as the starting value
623
+ // Patnaik's approximation as the starting value (for a huge ν its quantile by Wilson and Hilferty's cube root)
351
624
  const c = (df + 2 * ncp) / (df + ncp);
352
- const nu = ((df + ncp) * (df + ncp)) / (df + 2 * ncp);
353
- let x0 = c * qgamma1(logTarget, nu / 2, 2, lower, true);
625
+ const nu = (df + ncp) * ((df + ncp) / (df + 2 * ncp));
626
+ let x0;
627
+ if (nu > 1e5) {
628
+ const h = 2 / (9 * nu);
629
+ x0 = c * nu * (1 - h + qnorm(logTarget, { lowerTail: lower, logP: true }) * Math.sqrt(h)) ** 3;
630
+ }
631
+ else
632
+ x0 = c * qgamma1(logTarget, nu / 2, 2, lower, true);
354
633
  if (!(x0 > 0) || !Number.isFinite(x0))
355
634
  x0 = df + ncp;
635
+ const passes = (x) => (lower ? cdf(x, true).log >= logTarget : cdf(x, false).log <= logTarget);
636
+ const narrowSpread = Math.sqrt(2 * (df + 2 * ncp)) < 1e-10 * (df + ncp);
637
+ const narrow = narrowSpread ? narrowQuantile(x0, Number.MAX_VALUE, passes) : undefined;
638
+ if (narrow !== undefined)
639
+ return narrow;
356
640
  // Bracket by doubling the upper end (the distribution function is expensive for very large x)
641
+ let lo = 0;
357
642
  let hi = Math.max(x0, df + ncp, 1);
358
- for (let i = 0; i < 2000; i++) {
359
- const v = pnchisqRaw(hi, df, ncp, lower, true);
360
- if (lower ? v >= logTarget : v <= logTarget)
361
- break;
643
+ for (let i = 0; i < 2000 && !passes(hi); i++) {
644
+ lo = hi;
362
645
  hi *= 2;
363
646
  }
364
- return invertContinuous({
647
+ const x = invertContinuous({
365
648
  logTarget,
366
649
  ...(linearTarget !== undefined && { linearTarget }),
367
650
  lower,
@@ -374,5 +657,7 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
374
657
  lo: 0,
375
658
  hi,
376
659
  positive: true,
660
+ maxIter: 60,
377
661
  });
662
+ return confirmQuantile(x, cdf(x, lower).log, logTarget, () => searchDoubles(x, lo, hi, passes), passes);
378
663
  }