@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.
- package/CHANGELOG.md +147 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/chunks/{chunk-735NNFAS.js → chunk-3THFTB36.js} +2 -2
- package/dist/chunks/chunk-3TSWYE5L.js +2 -0
- package/dist/chunks/{chunk-7J4HW6VF.js → chunk-3WPU6LGK.js} +2 -2
- package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-5ITR67MB.js} +2 -2
- package/dist/chunks/chunk-5NRFU6F6.js +2 -0
- package/dist/chunks/{chunk-SAZAEPZC.js → chunk-E2LCSVQV.js} +1 -1
- package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
- package/dist/chunks/chunk-FKAKALOZ.js +2 -0
- package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HD73UKPT.js} +2 -2
- package/dist/chunks/{chunk-4ILI2FSB.js → chunk-IUULP3LE.js} +1 -1
- package/dist/chunks/{chunk-LY2T6DA6.js → chunk-JQG3YQOC.js} +2 -2
- package/dist/chunks/chunk-KT66SB7R.js +2 -0
- package/dist/chunks/{chunk-IQOVOHZG.js → chunk-MAUAOQG3.js} +2 -2
- package/dist/chunks/{chunk-GEJCBCMP.js → chunk-MDYE7QP2.js} +2 -2
- package/dist/chunks/{chunk-5SQFDLNG.js → chunk-NM3Z2UBQ.js} +2 -2
- package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-PMJI5QO6.js} +2 -2
- package/dist/chunks/chunk-UF7KOTHF.js +3 -0
- package/dist/chunks/chunk-UOE57JGT.js +2 -0
- package/dist/chunks/{chunk-3QTVXDUM.js → chunk-USPEBXSR.js} +2 -2
- package/dist/chunks/{chunk-H4CHJMFU.js → chunk-VLUL2HFW.js} +1 -1
- package/dist/chunks/{chunk-VOIJKNL2.js → chunk-ZIX7NYQF.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +48 -4
- package/dist/distributions/beta.d.ts +1 -1
- package/dist/distributions/beta.js +75 -2
- package/dist/distributions/cauchy.js +76 -5
- package/dist/distributions/exponential.js +16 -2
- package/dist/distributions/f.js +143 -24
- package/dist/distributions/gamma.d.ts +13 -2
- package/dist/distributions/gamma.js +122 -12
- package/dist/distributions/geometric.js +19 -3
- package/dist/distributions/invgamma.js +60 -10
- package/dist/distributions/kumaraswamy.js +116 -17
- package/dist/distributions/laplace.js +5 -2
- package/dist/distributions/logistic.js +8 -2
- package/dist/distributions/lognormal.js +40 -5
- package/dist/distributions/nbeta.js +597 -52
- package/dist/distributions/nchisq.d.ts +25 -0
- package/dist/distributions/nchisq.js +381 -96
- package/dist/distributions/negbinomial.js +26 -6
- package/dist/distributions/normal.js +29 -1
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +401 -63
- package/dist/distributions/pareto.js +73 -19
- package/dist/distributions/t.js +102 -12
- package/dist/distributions/triangular.js +93 -27
- package/dist/distributions/tukey.js +110 -50
- package/dist/distributions/uniform.js +14 -9
- package/dist/distributions/weibull.js +65 -10
- package/dist/distributions.js +2 -2
- package/dist/internal/ddexp.d.ts +13 -0
- package/dist/internal/ddexp.js +55 -3
- package/dist/internal/discrete.js +30 -7
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +59 -5
- package/dist/internal/noncentral.d.ts +41 -0
- package/dist/internal/noncentral.js +124 -2
- package/dist/internal/poissonMixture.d.ts +7 -2
- package/dist/internal/poissonMixture.js +142 -38
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +7 -0
- package/dist/special/dd.d.ts +1 -1
- package/dist/special/dd.js +4 -1
- package/dist/special/gamma.js +4 -0
- package/dist/special/igam.js +126 -21
- package/dist/special/incbet.d.ts +10 -0
- package/dist/special/incbet.js +218 -48
- package/dist/special/incbetLargeA.js +53 -14
- package/dist/special/incbetPrefactor.d.ts +57 -0
- package/dist/special/incbetPrefactor.js +103 -0
- package/dist/special/incbetTemme.js +15 -3
- package/dist/special/loader.js +97 -22
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
- package/dist/chunks/chunk-BDJPSEA7.js +0 -2
- package/dist/chunks/chunk-KO6QQPC6.js +0 -2
- package/dist/chunks/chunk-OLPTFKIZ.js +0 -3
- package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
- package/dist/chunks/chunk-UWBYDIOH.js +0 -2
- package/dist/chunks/chunk-UZHY5DWN.js +0 -2
|
@@ -8,31 +8,47 @@
|
|
|
8
8
|
// e_j = Gamma(a+b+j) / (Gamma(a+j+1) Gamma(b)) x^(a+j) (1-x)^b
|
|
9
9
|
// = dbinom(a + j; a + b + j, x) b / (a + b + j)
|
|
10
10
|
// The density of component j is e_j (a + j) / (x (1 - x)). The sums are done in src/internal/poissonMixture.ts.
|
|
11
|
+
// For a huge λ or extreme shapes the distribution is computed from X = Y / (Y + W) instead (see "Extreme parameters").
|
|
11
12
|
//
|
|
12
13
|
// The noncentral F is computed from the fact that X = m F / (n + m F) follows the noncentral beta (m/2, n/2, λ).
|
|
13
14
|
// Its density is f_F(q) = Σ_j w_j e_j (a + j) / q (dx/dq and x (1 - x) cancel).
|
|
14
15
|
// x and 1 - x are formed as a double-double pair (rounding is amplified by the parameters, as for the central distribution).
|
|
15
16
|
import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
16
17
|
import { invertContinuous } from "../internal/invert.js";
|
|
17
|
-
import {
|
|
18
|
+
import { kronrodAdaptive, narrowQuantile, negligibleNcp, quadratureControl, searchDoubles } from "../internal/noncentral.js";
|
|
19
|
+
import { gammaInc } from "../special/igam.js";
|
|
20
|
+
import { mixtureDensity, mixtureTail } from "../internal/poissonMixture.js";
|
|
18
21
|
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddNeg, twoProd, twoSum } from "../special/dd.js";
|
|
19
22
|
import { lbeta, lbetaDD } from "../special/beta.js";
|
|
23
|
+
import { lgamma, lgamma1p } from "../special/gamma.js";
|
|
24
|
+
import { log1pmx } from "../special/log1pmx.js";
|
|
20
25
|
import { digamma } from "../special/psigamma.js";
|
|
21
26
|
import { betaIncDD } from "../special/incbet.js";
|
|
22
27
|
import { dbinomExponent, dbinomRawDD } from "../special/loader.js";
|
|
23
28
|
import { dbeta1, pbeta1, qbeta1 } from "./beta.js";
|
|
24
29
|
import { dfDensity1, pf1, qf1 } from "./f.js";
|
|
25
|
-
import { dnchisq1, negligibleTail, pnchisq1, qnchisq1 } from "./nchisq.js";
|
|
30
|
+
import { confirmQuantile, dnchisq1, dnchisqLogNear, gammaTailNear, negligibleTail, pnchisq1, qnchisq1 } from "./nchisq.js";
|
|
31
|
+
import { dgamma1 } from "./gamma.js";
|
|
26
32
|
function betaKernel(a, b, xd, xcd) {
|
|
27
33
|
const ab = twoSum(a, b);
|
|
28
|
-
// For very small x the ratio underflows, so keep it scaled by 2^500
|
|
29
|
-
|
|
30
|
-
const
|
|
34
|
+
// For very small x the ratio underflows, so keep it scaled by 2^(500 K) (also where a + b is tiny: the ratio at j = 1
|
|
35
|
+
// is x (a + b)/(a + 1), down to 1e-504), K units enough to bring x min(1, a + b) above 1e-250
|
|
36
|
+
const low = Math.log2(xd[0]) + Math.log2(Math.min(1, a + b));
|
|
37
|
+
const K = low < -830 ? Math.ceil((-830 - low) / 500) : 0;
|
|
38
|
+
let xs = xd;
|
|
39
|
+
for (let i = 0; i < K; i++)
|
|
40
|
+
xs = [xs[0] * 2 ** 500, xs[1] * 2 ** 500];
|
|
31
41
|
return {
|
|
32
|
-
ratioK:
|
|
42
|
+
ratioK: -K,
|
|
33
43
|
cdf: (j, lower, log) => betaIncDD(a + j, b, xd, xcd, lower, log),
|
|
34
44
|
inc: (j, log) => {
|
|
35
45
|
const [s, corr] = incCorrection(a, b, j, xd);
|
|
46
|
+
if (!binomialHelpersApply(s, b, xd, xcd)) {
|
|
47
|
+
// Near subnormal x or for a tiny shape the binomial-probability helpers break down, as in incLogDD
|
|
48
|
+
const l = betaKernelIncLog(s, b, xd, xcd);
|
|
49
|
+
const lv = (typeof l === "number" ? l : l[0] + l[1]) + corr;
|
|
50
|
+
return log ? lv : Math.exp(lv);
|
|
51
|
+
}
|
|
36
52
|
const c = b / (s + b);
|
|
37
53
|
return log
|
|
38
54
|
? dbinomRawDD(s, b, xd, xcd, true) + Math.log(c) + corr
|
|
@@ -42,9 +58,10 @@ function betaKernel(a, b, xd, xcd) {
|
|
|
42
58
|
ratio: (j) => ddDiv(ddMul(xs, ddAdd(ab, [j - 1, 0])), twoSum(a, j)),
|
|
43
59
|
incLogDD: (j) => {
|
|
44
60
|
// log e_j = log dbinom(a + j; a + b + j, x) + log(b / (a + b + j))
|
|
45
|
-
// Near subnormal x
|
|
61
|
+
// Near subnormal x (n x underflows) or for a tiny shape (b = 1e-264: NaN) the binomial-probability helpers
|
|
62
|
+
// break down, so build it from the log of the beta function
|
|
46
63
|
const [s, corr] = incCorrection(a, b, j, xd);
|
|
47
|
-
if (!(
|
|
64
|
+
if (!binomialHelpersApply(s, b, xd, xcd)) {
|
|
48
65
|
const l = betaKernelIncLog(s, b, xd, xcd);
|
|
49
66
|
return typeof l === "number" ? l + corr : ddAdd(l, [corr, 0]);
|
|
50
67
|
}
|
|
@@ -53,6 +70,10 @@ function betaKernel(a, b, xd, xcd) {
|
|
|
53
70
|
},
|
|
54
71
|
};
|
|
55
72
|
}
|
|
73
|
+
/** Whether e_j can be taken from the binomial-probability helpers (dbinomRawDD, dbinomExponent) */
|
|
74
|
+
function binomialHelpersApply(s, b, xd, xcd) {
|
|
75
|
+
return xd[0] > 1e-305 && xcd[0] > 1e-305 && s > 1e-10 && b > 1e-10;
|
|
76
|
+
}
|
|
56
77
|
/**
|
|
57
78
|
* The shape a + j rounded to double, s, and the first-order change in log e_j from s to the exact a + j:
|
|
58
79
|
* δ ∂log e/∂s with δ the rounding and ∂log e_j/∂s = log x + ψ(s + b) - ψ(s + 1) (e_j = x^s (1 - x)^b / (s B(s, b))).
|
|
@@ -67,12 +88,16 @@ function incCorrection(a, b, j, xd) {
|
|
|
67
88
|
}
|
|
68
89
|
/**
|
|
69
90
|
* log e_j in double-double when x (or 1 - x) is near subnormal, for the shape s = a + j:
|
|
70
|
-
* log e_j = -log B(s, b) - log(s) + s log x + b log(1 - x)
|
|
91
|
+
* log e_j = -log B(s, b) - log(s) + s log x + b log(1 - x).
|
|
92
|
+
* For s, b < 1, log(s B(s, b)) = log Γ(1 + s) + log Γ(1 + b) - log Γ(1 + s + b) + log(1 + s/b), whose terms are small:
|
|
93
|
+
* log B(s, b) and log s are each about -log s (523 at s = 3e-228), and their difference would keep an error of 1e-13
|
|
71
94
|
*/
|
|
72
95
|
function betaKernelIncLog(s, b, xd, xcd) {
|
|
73
96
|
if (!(xd[0] > 0 && xcd[0] > 0))
|
|
74
97
|
return -Infinity;
|
|
75
98
|
const l = ddAdd(ddMulNum(ddLog(xd), s), ddMulNum(ddLog(xcd), b));
|
|
99
|
+
if (s < 1 && b < 1)
|
|
100
|
+
return ddAdd(l, [-(lgamma1p(s) + lgamma1p(b) - lgamma1p(s + b) + Math.log1p(s / b)), 0]);
|
|
76
101
|
return ddAdd(ddAdd(l, ddNeg(lbetaDD(s, b))), [-Math.log(s), 0]);
|
|
77
102
|
}
|
|
78
103
|
/** Form x and 1 - x as a double-double pair, based on the smaller of the two */
|
|
@@ -80,19 +105,67 @@ function pair(x) {
|
|
|
80
105
|
return x <= 0.5 ? [[x, 0], twoSum(1, -x)] : [twoSum(1, -(1 - x)), [1 - x, 0]];
|
|
81
106
|
}
|
|
82
107
|
/** One-sided probability (P[X <= x] if lower). 0 < x < 1 */
|
|
83
|
-
function pnbetaRaw(a, b, ncp, xd, xcd, lower, log) {
|
|
84
|
-
const
|
|
108
|
+
function pnbetaRaw(a, b, ncp, xd, xcd, lower, log, o = odds(xd, xcd)) {
|
|
109
|
+
const m = method(a, b, ncp);
|
|
110
|
+
if (m !== "mixture") {
|
|
111
|
+
const left = leftTailLog(a, b, ncp, xd, xcd);
|
|
112
|
+
if (left !== undefined) {
|
|
113
|
+
if (lower)
|
|
114
|
+
return log ? left : Math.exp(left);
|
|
115
|
+
return log ? Math.log1p(-Math.exp(left)) : -Math.expm1(left);
|
|
116
|
+
}
|
|
117
|
+
return extremeTail(m, a, b, ncp, o, lower, log);
|
|
118
|
+
}
|
|
119
|
+
// A value near 1, and its log, are built from the small value of the other side (the lower tail is likely the smaller
|
|
120
|
+
// below the mean of Beta(a + λ/2, b))
|
|
121
|
+
const v = mixtureTail(ncp / 2, betaKernel(a, b, xd, xcd), lower, xd[0] < (a + ncp / 2) / (a + ncp / 2 + b));
|
|
122
|
+
return log ? v.log : v.lin;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* log P[X <= x] far in the left tail for a large λ, as -λ/2 + log S, S = Σ_j (λ/2)^j / j! I_x(a + j, b) (the mixture
|
|
126
|
+
* with e^(-λ/2) taken out of the weights). The terms fall by factors of about r/(j + 1) once j > r, with
|
|
127
|
+
* r = (λ/2) ρ, ρ = x max(1, (a + b)/(a + 1)) (bounding I_x(s + 1, b)/I_x(s, b)), so for r <= 100 the sum takes at most a
|
|
128
|
+
* few hundred terms. Beyond that, S lies between its first term I_x(a, b) and I_x(a, b) e^r: where ρ <= 1e-16 the
|
|
129
|
+
* first term alone gives log P to 1e-16 of λ/2 (and so of |log P|). Undefined otherwise, or for shapes out of the
|
|
130
|
+
* central incomplete beta function's range. (The integral, at log P near -λ/2 = -1e27, cannot place its mode on values
|
|
131
|
+
* that are all -λ/2 to the double)
|
|
132
|
+
*/
|
|
133
|
+
function leftTailLog(a, b, ncp, xd, xcd) {
|
|
85
134
|
const mu = ncp / 2;
|
|
86
|
-
const
|
|
87
|
-
if (
|
|
88
|
-
return
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
135
|
+
const rho = xd[0] * Math.max(1, (a + b) / (a + 1));
|
|
136
|
+
if (a > 1e15 || b > 1e15)
|
|
137
|
+
return undefined;
|
|
138
|
+
if (mu * rho > 100)
|
|
139
|
+
return rho <= 1e-16 ? -mu + betaIncDD(a, b, xd, xcd, true, true) : undefined;
|
|
140
|
+
const lmu = Math.log(mu);
|
|
141
|
+
let max = -Infinity;
|
|
142
|
+
const terms = [];
|
|
143
|
+
for (let j = 0; j < 1000; j++) {
|
|
144
|
+
const t = j * lmu - lgamma1p(j) + betaIncDD(a + j, b, xd, xcd, true, true);
|
|
145
|
+
terms.push(t);
|
|
146
|
+
if (t > max)
|
|
147
|
+
max = t;
|
|
148
|
+
else if (t < max + LOG_TOL_SERIES && j > mu * rho)
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
let sum = 0;
|
|
152
|
+
for (const t of terms)
|
|
153
|
+
sum += Math.exp(t - max);
|
|
154
|
+
return -mu + max + Math.log(sum);
|
|
93
155
|
}
|
|
156
|
+
/** Below this, relative to the largest term, the terms of leftTailLog are negligible (they fall faster than 1/2) */
|
|
157
|
+
const LOG_TOL_SERIES = Math.log(1e-18);
|
|
94
158
|
/** Σ_j w_j e_j (a + j) / divisor. For the density, divisor = x (1 - x) (beta) or q (F) */
|
|
95
|
-
function densitySum(a, b, ncp, xd, xcd, divisor, log) {
|
|
159
|
+
function densitySum(a, b, ncp, xd, xcd, divisor, log, o = odds(xd, xcd)) {
|
|
160
|
+
const m = method(a, b, ncp);
|
|
161
|
+
if (m !== "mixture") {
|
|
162
|
+
// The sum is f(x) x (1 - x) / divisor
|
|
163
|
+
// (log x (1 - x) from log(1 - x) where 1 - x is below the double range)
|
|
164
|
+
const xx = ddMul(xd, xcd);
|
|
165
|
+
const lxx = xcd[0] > 1e-300 ? Math.log(xx[0] + xx[1]) : Math.log(xd[0] + xd[1]) + o.l1mx;
|
|
166
|
+
const l = extremeDensityLog(m, a, b, ncp, xd, o) + lxx - Math.log(divisor);
|
|
167
|
+
return log ? l : Math.exp(l);
|
|
168
|
+
}
|
|
96
169
|
const mu = ncp / 2;
|
|
97
170
|
const x = xd[0];
|
|
98
171
|
// The peak is at the j where the term ratio mu x (a + b + j - 1) / (j (a + j - 1)) equals 1
|
|
@@ -101,6 +174,383 @@ function densitySum(a, b, ncp, xd, xcd, divisor, log) {
|
|
|
101
174
|
const jStar = (-p + Math.sqrt(Math.max(0, p * p + 4 * c * (a + b - 1)))) / 2;
|
|
102
175
|
return mixtureDensity(mu, betaKernel(a, b, xd, xcd), (j) => a + j, jStar, divisor, log);
|
|
103
176
|
}
|
|
177
|
+
// ---- Extreme parameters ----
|
|
178
|
+
// X = Y / (Y + W) with Y ~ χ²(2a, λ) and W ~ χ²(2b) independent. Where the mixture would need too many terms (a huge
|
|
179
|
+
// λ, or a huge b against a + λ: its peak is near j = sqrt(λ x b / 2)), and where the central incomplete beta function
|
|
180
|
+
// of its components is out of its range (huge shapes), the distribution is computed from that of Y and W instead.
|
|
181
|
+
// Far in the left tail of a large λ the mixture is summed with e^(-λ/2) taken out of its weights (leftTailLog), and for
|
|
182
|
+
// a λ so large that log P = -(λ/2)(1 - x) to the double, that is the answer (farLeftLog).
|
|
183
|
+
/** Above this mean of the Poisson weights the mixture (at least 10 sqrt(λ/2) terms) is replaced by an integral */
|
|
184
|
+
const MU_MIXTURE = 1e6;
|
|
185
|
+
/**
|
|
186
|
+
* How the distribution is computed:
|
|
187
|
+
* - "chisq": b > 1e20 (a + j)² over the Poisson range of j. Then I_x(a + j, b) = P(a + j, -b log(1 - x)) to
|
|
188
|
+
* O((a + j)²/b) (in t = -b log(1 - X) the density of Beta(s, b) is (1 - e^(-t/b))^(s-1) e^-t up to a constant), so
|
|
189
|
+
* P[X <= x] = P[Y <= -2b log(1 - x)]
|
|
190
|
+
* - "gamma": Y is nearly constant on the scale of the gamma function below (its relative spread 2 sqrt(a + λ)/(2a + λ)
|
|
191
|
+
* is below 1e-4 / S, S = 40 sqrt(b) + 750 bounding |d log P(b, t)/d log t| where P is above the double range), so
|
|
192
|
+
* P[X > x] = P[W < Y k] = E P(b, T), T = Y k/2, k = (1 - x)/x, is P(b, t0) plus its expansion in the cumulants of T
|
|
193
|
+
* about t0 = E T (gammaExpansion)
|
|
194
|
+
* - "w": W is nearly constant on the scale of the distribution function F of Y (its relative spread 1/sqrt(b) is below
|
|
195
|
+
* 1e-5 / S, S = 40 m/σ + 750 bounding |d log F/d log y| likewise, m = 2a + λ, σ = 2 sqrt(a + λ)), so
|
|
196
|
+
* P[X <= x] = P[Y <= W/k] = E F(W/k) = F(y0) + (2b/k²) F''(y0) + ..., y0 = E W/k = 2b/k (the next term, with the
|
|
197
|
+
* third cumulant of W, is below 1e-15 of F)
|
|
198
|
+
* - "integral": a large λ or a huge shape otherwise (integralLog)
|
|
199
|
+
*/
|
|
200
|
+
function method(a, b, ncp) {
|
|
201
|
+
const mu = ncp / 2;
|
|
202
|
+
const s = a + mu + 10 * Math.sqrt(mu) + 10;
|
|
203
|
+
if (b > 1e20 * s * s)
|
|
204
|
+
return "chisq";
|
|
205
|
+
if (2 * Math.sqrt(a + ncp) * (40 * Math.sqrt(b) + 750) < 1e-4 * (2 * a + ncp))
|
|
206
|
+
return "gamma";
|
|
207
|
+
if ((40 * (2 * a + ncp)) / (2 * Math.sqrt(a + ncp)) + 750 < 1e-5 * Math.sqrt(b))
|
|
208
|
+
return "w";
|
|
209
|
+
// (shapes above 1e15 are out of the range of the central incomplete beta function: it does not return for
|
|
210
|
+
// a = 1.7e32, b = 6e-35 at a tiny x)
|
|
211
|
+
return mu > MU_MIXTURE || a > 1e15 || b > 1e15 ? "integral" : "mixture";
|
|
212
|
+
}
|
|
213
|
+
/** -log(1 - x) from the pair (x, 1 - x) */
|
|
214
|
+
function minusLog1mx(xd, xcd) {
|
|
215
|
+
return xd[0] <= 0.5 ? -Math.log1p(-(xd[0] + xd[1])) : -(Math.log(xcd[0]) + xcd[1] / xcd[0]);
|
|
216
|
+
}
|
|
217
|
+
/** (1 - x)/x from the pair */
|
|
218
|
+
function oddsInverse(xd, xcd) {
|
|
219
|
+
const r = ddDiv(xcd, xd);
|
|
220
|
+
return r[0] + r[1];
|
|
221
|
+
}
|
|
222
|
+
function odds(xd, xcd) {
|
|
223
|
+
const k = oddsInverse(xd, xcd);
|
|
224
|
+
const lk = k > 1e-300 && k < 1e300 ? Math.log(k) : Math.log(xcd[0]) - Math.log(xd[0]);
|
|
225
|
+
return { k, lk, l1mx: -minusLog1mx(xd, xcd) };
|
|
226
|
+
}
|
|
227
|
+
/** c k, also where k is out of the double range */
|
|
228
|
+
function timesK(c, o) {
|
|
229
|
+
return o.k > 1e-300 && o.k < 1e300 ? c * o.k : Math.exp(Math.log(c) + o.lk);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* log g_b(t), the Gamma(b, 1) density, at t = t0 e^v (v = 0 but for the integrals over log y, where the offset is
|
|
233
|
+
* exact; log t0 given where t0 underflows). For b > 1e4 as
|
|
234
|
+
* b (log(1 + d) - d) - log t + log(b/(2 pi))/2 - 1/(12b) + 1/(360b³), d = t/b - 1,
|
|
235
|
+
* with t - b = (t0 - b) + t0 (e^v - 1) (Stirling's series for log Γ(b), whose next term is below 1e-23): the density's
|
|
236
|
+
* helpers overflow for b or t near 1e300 and break down where t/b underflows, and the rounding of t would carry an
|
|
237
|
+
* error of eps sqrt(b) into the log near the mode. For smaller b the log is dominated by -t or -log Γ(b) where t/b or
|
|
238
|
+
* b/t is beyond 1e300 (or t underflows), and is taken from (b - 1) log t - t - log Γ(b) directly; for a tiny b as
|
|
239
|
+
* log b + (b - 1) log t - t - log Γ(1 + b) (dgamma1 is Infinity at b = 1.9e-199)
|
|
240
|
+
*/
|
|
241
|
+
function gammaDensityLog(t0, b, v = 0, logT0 = Math.log(t0)) {
|
|
242
|
+
const logT = logT0 + v;
|
|
243
|
+
const t = v === 0 ? t0 : Math.exp(logT);
|
|
244
|
+
if (b > 1e4) {
|
|
245
|
+
const d = (t0 - b + t0 * Math.expm1(v)) / b;
|
|
246
|
+
return b * log1pmx(d) - logT + 0.5 * Math.log(b / (2 * Math.PI)) - 1 / (12 * b) + 1 / (360 * b * b * b);
|
|
247
|
+
}
|
|
248
|
+
if (b < 1e-10)
|
|
249
|
+
return Math.log(b) + (b - 1) * logT - t - lgamma1p(b);
|
|
250
|
+
if (!(t > 1e-300) || t > 1e300 * b || b > 1e300 * t)
|
|
251
|
+
return (b - 1) * logT - t - lgamma(b);
|
|
252
|
+
return dgamma1(t, b, 1, true);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* P(b, t) (lower) or Q(b, t), the distribution function of W/2 (its log if log), at t = t0 e^v (as gammaDensityLog).
|
|
256
|
+
* Below t = 1e-300 (where t may have underflowed) from the first term of the series, P(b, t) = t^b / Γ(1 + b) (1 + O(t))
|
|
257
|
+
*/
|
|
258
|
+
function gammaTail(b, t0, lower, log, v = 0, logT0 = Math.log(t0)) {
|
|
259
|
+
const logT = logT0 + v;
|
|
260
|
+
const t = v === 0 ? t0 : Math.exp(logT);
|
|
261
|
+
if (t < 1e-300) {
|
|
262
|
+
const lp = b * logT - lgamma1p(b);
|
|
263
|
+
if (lower)
|
|
264
|
+
return log ? lp : Math.exp(lp);
|
|
265
|
+
// (for a tiny b, P is near 1 and Q = -expm1(lp) small)
|
|
266
|
+
return log ? (lp > -Math.LN2 ? Math.log(-Math.expm1(lp)) : Math.log1p(-Math.exp(lp))) : -Math.expm1(lp);
|
|
267
|
+
}
|
|
268
|
+
// (gammaInc takes 40 ms at b = 1e12, t < b, and 0.1 s at b = 1e37)
|
|
269
|
+
if (b > 1e4)
|
|
270
|
+
return gammaTailNear(b, t0, v, lower, log);
|
|
271
|
+
// Both tails, and the log, from the linear value of the small tail, so that they are consistent (gammaInc's log of
|
|
272
|
+
// a tail can differ from the log of its linear value by 4e-14 relative at a tiny t). The lower tail is the small one
|
|
273
|
+
// below about the median, which is far below b for a tiny b (P(1e-9, 1e-14) = 1 - 3e-8), so it is tried first
|
|
274
|
+
const lowerP = gammaInc(b, t, true, false);
|
|
275
|
+
const small = lowerP <= 0.5;
|
|
276
|
+
const p = small ? lowerP : gammaInc(b, t, false, false);
|
|
277
|
+
if (small !== lower)
|
|
278
|
+
return log ? Math.log1p(-p) : 1 - p;
|
|
279
|
+
if (!log)
|
|
280
|
+
return p;
|
|
281
|
+
return p > 1e-300 ? Math.log(p) : gammaInc(b, t, lower, true);
|
|
282
|
+
}
|
|
283
|
+
/** P[X <= x] (lower) / P[X > x] (its log if log) by the method (not "mixture") */
|
|
284
|
+
function extremeTail(m, a, b, ncp, o, lower, log) {
|
|
285
|
+
if (m === "chisq")
|
|
286
|
+
return pnchisq1(-2 * b * o.l1mx, 2 * a, ncp, lower, log);
|
|
287
|
+
const far = farLeftLog(a, b, ncp, o);
|
|
288
|
+
if (far !== undefined) {
|
|
289
|
+
if (lower)
|
|
290
|
+
return log ? far : 0;
|
|
291
|
+
return log ? -0 : 1;
|
|
292
|
+
}
|
|
293
|
+
const expansion = m === "gamma" ? gammaExpansion(a, b, ncp, o, !lower, log) : m === "w" ? wExpansion(a, b, ncp, o, lower, log) : undefined;
|
|
294
|
+
if (expansion !== undefined)
|
|
295
|
+
return expansion;
|
|
296
|
+
const l = integralTailLog(a, b, ncp, o.lk, lower);
|
|
297
|
+
if (l > -Math.LN2) {
|
|
298
|
+
// The integral is accurate relative to itself: a value near 1 is built from the small value of the other side
|
|
299
|
+
// (unless that integral fails, as it can for a tiny shape, where the integrand is flat towards y = 0)
|
|
300
|
+
const other = Math.exp(integralTailLog(a, b, ncp, o.lk, !lower));
|
|
301
|
+
if (other <= 0.5)
|
|
302
|
+
return log ? Math.log1p(-other) : 1 - other;
|
|
303
|
+
}
|
|
304
|
+
return log ? l : Math.exp(l);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* log P[X <= x] for a λ so large that its leading term -(λ/2)(1 - x) is all of it to the double: undefined otherwise.
|
|
308
|
+
* X <= x is Y <= W x/(1 - x); with log P[Y <= y] = -(sqrt(λ) - sqrt(y))²/2 and log f_W(w) = -w/2 to leading order,
|
|
309
|
+
* the largest term over w is -(λ/2)(1 - x) (at w = λ x (1 - x)), and the rest, from the powers of y and w and the
|
|
310
|
+
* width of the maximum, is of order (a + b + 1) log λ. (The integral cannot place its mode on values that are all
|
|
311
|
+
* -1e295 to the double, and the expansions in the cumulants of Y fail this far out)
|
|
312
|
+
*/
|
|
313
|
+
function farLeftLog(a, b, ncp, o) {
|
|
314
|
+
const mu = ncp / 2;
|
|
315
|
+
if (!(mu > 1e18))
|
|
316
|
+
return undefined;
|
|
317
|
+
// (λ/2)(1 - x): from log(1 - x) where it is near 0, else as (λ/2) k/(1 + k) (k is then below e)
|
|
318
|
+
const lead = o.l1mx > -1 ? mu * Math.exp(o.l1mx) : timesK(mu, o) / (1 + o.k);
|
|
319
|
+
return lead >= 1e17 * (a + b + 1) * (Math.log(mu) + 50) ? -lead : undefined;
|
|
320
|
+
}
|
|
321
|
+
/** log P[X <= x] (lower) / log P[X > x] by integralLog, for log k = log((1 - x)/x) */
|
|
322
|
+
function integralTailLog(a, b, ncp, logK, lower) {
|
|
323
|
+
// X <= x is W >= Y k
|
|
324
|
+
// (towards y = 0 the integrand goes as y^a, or y^(a + b) where P[W < y k] ~ (y k/2)^b / Γ(b + 1))
|
|
325
|
+
const lk = logK - Math.LN2;
|
|
326
|
+
const h = (u0, v) => gammaTail(b, Math.exp(u0 + lk), !lower, true, v, u0 + lk) + u0 + v;
|
|
327
|
+
return integralLog(a, ncp, h, b, lower ? a : a + b);
|
|
328
|
+
}
|
|
329
|
+
/** log f(x) by the method (not "mixture") */
|
|
330
|
+
function extremeDensityLog(m, a, b, ncp, xd, o) {
|
|
331
|
+
const x = xd[0] + xd[1];
|
|
332
|
+
if (m === "chisq") {
|
|
333
|
+
// f(x) = f_Y(2t) 2b/(1 - x), t = -b log(1 - x)
|
|
334
|
+
return dnchisq1(-2 * b * o.l1mx, 2 * a, ncp, true) + Math.log(2 * b) - o.l1mx;
|
|
335
|
+
}
|
|
336
|
+
// f(x) = E[g_b(T) T] / (k x²), g_b the Gamma(b, 1) density (the derivative of E P(b, T) with dk/dx = -1/x²),
|
|
337
|
+
// expanded to second order about t0
|
|
338
|
+
if (m === "gamma") {
|
|
339
|
+
const t0 = timesK(a + ncp / 2, o);
|
|
340
|
+
// v (h² + h' + 2h/t) with h = (b - 1)/t - 1, written as in gammaExpansion (H = b - 1 - t0)
|
|
341
|
+
const H = b - 1 - t0;
|
|
342
|
+
const c = ((a + ncp) / (a + ncp / 2) / (a + ncp / 2)) * (H * H - (b - 1) + 2 * H);
|
|
343
|
+
if (Math.abs(c) < 1e-3)
|
|
344
|
+
return gammaDensityLog(t0, b) + Math.log(a + ncp / 2) + Math.log1p(c / 2) - 2 * Math.log(x);
|
|
345
|
+
}
|
|
346
|
+
// f(x) = E[f_Y(W/k) W] / (k² x²), to zeroth order f_Y(y0) y0 / (k x²) (relative error below 1e-10)
|
|
347
|
+
if (m === "w") {
|
|
348
|
+
const y0 = timesK(2 * b, { k: 1 / o.k, lk: -o.lk, l1mx: NaN });
|
|
349
|
+
return dnchisq1(y0, 2 * a, ncp, true) + Math.log(y0) - o.lk - 2 * Math.log(x);
|
|
350
|
+
}
|
|
351
|
+
// f(x) = ∫ f_Y(y) f_W(y k) y/x² dy
|
|
352
|
+
const lk = o.lk - Math.LN2;
|
|
353
|
+
const h = (u0, v) => gammaDensityLog(Math.exp(u0 + lk), b, v, u0 + lk) - Math.LN2 + 2 * (u0 + v);
|
|
354
|
+
return integralLog(a, ncp, h, b, a + b) - 2 * Math.log(x);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* E P(b, T) (lower) or E Q(b, T) for T = Y k/2 (its log if log), by the expansion about t0 = E T = (a + λ/2) k in the
|
|
358
|
+
* cumulants of T (variance v = (a + λ) k², third and fourth cumulants (2a + 3λ) k³ and 6 (a + 2λ) k⁴, from those of
|
|
359
|
+
* the noncentral χ²: 2(n + 2λ), 8(n + 3λ), 48(n + 4λ) with n = 2a):
|
|
360
|
+
* E P(b, T) = P(t0) + v P''/2 + κ3 P'''/6 + (κ4 + 3v²) P''''/24 + ...,
|
|
361
|
+
* with P' = g, P'' = g h, P''' = g (h² - (b - 1)/t²), P'''' = g (h³ - 3h (b - 1)/t² + 2(b - 1)/t³), h = (b - 1)/t - 1.
|
|
362
|
+
* The terms fall by factors of the relative spread of Y times |d log P/d log t| (below 1e-4 by "gamma"), so the
|
|
363
|
+
* error is below 1e-20. Undefined where the correction is not small (|d log P/d log t| larger in a far tail)
|
|
364
|
+
*/
|
|
365
|
+
function gammaExpansion(a, b, ncp, o, lower, log) {
|
|
366
|
+
const t0 = timesK(a + ncp / 2, o);
|
|
367
|
+
const base = gammaTail(b, t0, lower, log);
|
|
368
|
+
// The choice of the method bounds |d log G/d log t| by 40 sqrt(b) + 750 where G is in the double range; on the log
|
|
369
|
+
// scale G goes beyond, into the tail of G where that is about |t0 - b| (log Q(b, t) is about -t for t >> b)
|
|
370
|
+
const farTail = (lower ? t0 < b : t0 > b) && Math.abs(t0 - b) > 40 * Math.sqrt(b) + 750;
|
|
371
|
+
if (log) {
|
|
372
|
+
// On the log scale the correction is about (v/2) (L'² + L'') (v = (a + λ) k² the variance of T, L = log G), with
|
|
373
|
+
// |L'| at most 1 but far in the tail of G, where log G is nearly linear in t: L' = b/t - 1 for P (t << b),
|
|
374
|
+
// (b - 1)/t - 1 for Q (t >> b). Where that is below 1e-16 of |log G| (its rounding), log G(b, t0) is the answer;
|
|
375
|
+
// far in the tail it is added while the next term, κ3 L'³/6 (κ3 = (2a + 3λ) k³), is below that. (The terms below
|
|
376
|
+
// could not be formed from logs as large as 1e110)
|
|
377
|
+
const c = lower ? b : b - 1;
|
|
378
|
+
const l1 = farTail ? c / t0 - 1 : 1;
|
|
379
|
+
const v = timesK(timesK(a + ncp, o), o);
|
|
380
|
+
if ((v * Math.max(1, l1 * l1)) / 2 <= 1e-16 * Math.abs(base))
|
|
381
|
+
return base;
|
|
382
|
+
const k3 = timesK(timesK(timesK(2 * a + 3 * ncp, o), o), o);
|
|
383
|
+
if (farTail && (k3 * Math.abs(l1 * l1 * l1)) / 6 <= 1e-16 * Math.abs(base))
|
|
384
|
+
return base + (v / 2) * (l1 * l1 - c / t0 / t0);
|
|
385
|
+
}
|
|
386
|
+
// Far enough out (beyond the bound above) the expansion no longer holds, but for the linear value the log above may
|
|
387
|
+
const spreadT = (2 * Math.sqrt(a + ncp)) / (2 * a + ncp);
|
|
388
|
+
if (farTail && !(spreadT * Math.abs(t0 - b) < 1e-4)) {
|
|
389
|
+
const l = log ? undefined : gammaExpansion(a, b, ncp, o, lower, true);
|
|
390
|
+
return l === undefined ? undefined : Math.exp(l);
|
|
391
|
+
}
|
|
392
|
+
// (t0 times the ratio of the density to the tail, d log G/d log t, which stays of order b where t0 underflows and the
|
|
393
|
+
// ratio itself overflows; 0 far in the other tail, where the moments below can overflow)
|
|
394
|
+
const lgt = gammaDensityLog(t0, b) + Math.log(t0);
|
|
395
|
+
const ratio = log ? Math.exp(lgt - base) : base > 0 ? Math.exp(lgt) / base : 0;
|
|
396
|
+
if (!(t0 > 0 && t0 < Infinity) || ratio === 0)
|
|
397
|
+
return base;
|
|
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 (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)
|
|
402
|
+
const A = a + ncp / 2;
|
|
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;
|
|
407
|
+
const H = b - 1 - t0;
|
|
408
|
+
const B = b - 1;
|
|
409
|
+
const series = (w / 2) * H + (u3 / 6) * (H * H - B) + ((u4 + 3 * w * w) / 24) * (H * H * H - 3 * H * B + 2 * B);
|
|
410
|
+
// The correction relative to the tail (P or Q = 1 - P, whose derivatives are those of P negated)
|
|
411
|
+
const rel = (lower ? 1 : -1) * ratio * series;
|
|
412
|
+
if (!(Math.abs(rel) < 1e-3))
|
|
413
|
+
return undefined;
|
|
414
|
+
return log ? base + Math.log1p(rel) : base * (1 + rel);
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* E F(W/k) (lower) or E (1 - F(W/k)) (its log if log) for the method "w": F(y0) plus (2b/k²) f'(y0) (Var W = 4b, the
|
|
418
|
+
* second derivative of F is f'), with f' from a central difference of log f. Undefined where the correction is not
|
|
419
|
+
* small
|
|
420
|
+
*/
|
|
421
|
+
function wExpansion(a, b, ncp, o, lower, log) {
|
|
422
|
+
const y0 = timesK(2 * b, { k: 1 / o.k, lk: -o.lk, l1mx: NaN });
|
|
423
|
+
const base = pnchisq1(y0, 2 * a, ncp, lower, log);
|
|
424
|
+
if (!(y0 > 0 && y0 < Infinity))
|
|
425
|
+
return base;
|
|
426
|
+
const h = 1e-6;
|
|
427
|
+
const dlogf = (dnchisq1(y0 * (1 + h), 2 * a, ncp, true) - dnchisq1(y0 * (1 - h), 2 * a, ncp, true)) / (2 * h * y0);
|
|
428
|
+
const lf = dnchisq1(y0, 2 * a, ncp, true);
|
|
429
|
+
const ratio = log ? Math.exp(lf - base) : base > 0 ? Math.exp(lf) / base : 0;
|
|
430
|
+
const rel = (lower ? 1 : -1) * timesK(y0, { k: 1 / o.k, lk: -o.lk, l1mx: NaN }) * ratio * dlogf;
|
|
431
|
+
if (!(Math.abs(rel) < 1e-3))
|
|
432
|
+
return undefined;
|
|
433
|
+
return log ? base + Math.log1p(rel) : base * (1 + rel);
|
|
434
|
+
}
|
|
435
|
+
/** Ends of the integrals in u = log y: the logs of the smallest and the largest positive doubles */
|
|
436
|
+
const U_MIN = -744;
|
|
437
|
+
const U_MAX = 709.78;
|
|
438
|
+
/**
|
|
439
|
+
* log ∫ exp(L(u)) du over u = log y, L(u) = log f_Y(y) + h(u) (Y ~ χ²(2a, λ)), for an h that makes the integrand
|
|
440
|
+
* unimodal (the log of a distribution function of W at y k, or of its density, plus u or 2u) and that goes as
|
|
441
|
+
* e^(leftSlope u) as y -> 0; h(u0, v) is taken at u0 + v with the offset v exact (as dnchisqLogNear does for f_Y), so
|
|
442
|
+
* that the integrand keeps its accuracy however narrow its peak is in u. The mode is found by a golden-section search,
|
|
443
|
+
* then the integral of exp(L(u) - L(mode)) is taken outwards from it on both sides over intervals of doubling width,
|
|
444
|
+
* each by the adaptive Kronrod rule: upwards until the integrand is below 1e-17 of the sum relative to the rate at
|
|
445
|
+
* which it falls (the secant over the last interval, which overestimates the rest for a log-concave integrand),
|
|
446
|
+
* downwards the same for leftSlope > 1/2, otherwise to the smallest double and the exponential tail beyond. The
|
|
447
|
+
* relative accuracy is that of L(u) - L(mode), about 1e-16 |L(mode)|.
|
|
448
|
+
*/
|
|
449
|
+
function integralLog(a, ncp, h, b, leftSlope) {
|
|
450
|
+
// L at u0 + v, with the density of Y and h taken at the exact offset v from y0 = e^u0 (dnchisqLogNear)
|
|
451
|
+
const near = (u0, v) => {
|
|
452
|
+
const l = dnchisqLogNear(Math.exp(u0), v, 2 * a, ncp) + h(u0, v);
|
|
453
|
+
// (the density of Y is infinite at y = 0 for 2a < 2, where y underflows; the integrand y f_Y(y) is not)
|
|
454
|
+
return Number.isNaN(l) || l === Infinity ? -Infinity : l;
|
|
455
|
+
};
|
|
456
|
+
const L = (u) => near(u, 0);
|
|
457
|
+
// Golden-section search for the mode. Far from the mode of Y (about log(2a + λ)) L can be constant to the double
|
|
458
|
+
// (-λ/2 + O(sqrt(λ y)) for y << λ = 1e22); equal values there send the search towards it
|
|
459
|
+
const uy = Math.log(2 * a + ncp);
|
|
460
|
+
const g = (3 - Math.sqrt(5)) / 2;
|
|
461
|
+
let lo = U_MIN;
|
|
462
|
+
let hi = U_MAX;
|
|
463
|
+
let c = lo + g * (hi - lo);
|
|
464
|
+
let d = hi - g * (hi - lo);
|
|
465
|
+
let fc = L(c);
|
|
466
|
+
let fd = L(d);
|
|
467
|
+
// (where L is beyond 1e18 only L(mode) is wanted, to its rounding of 1e-16 |L|: a u to 1e-8 gives that)
|
|
468
|
+
const tol = () => (Math.abs(fc) > 1e18 ? 1e-8 : 1e-14) * Math.max(1, Math.abs(c));
|
|
469
|
+
for (let i = 0; i < 300 && hi - lo > tol(); i++) {
|
|
470
|
+
if (fc > fd || (fc === fd && d > uy)) {
|
|
471
|
+
hi = d;
|
|
472
|
+
d = c;
|
|
473
|
+
fd = fc;
|
|
474
|
+
c = lo + g * (hi - lo);
|
|
475
|
+
fc = L(c);
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
lo = c;
|
|
479
|
+
c = d;
|
|
480
|
+
fc = fd;
|
|
481
|
+
d = hi - g * (hi - lo);
|
|
482
|
+
fd = L(d);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const [um, lm] = fc >= fd ? [c, fc] : [d, fd];
|
|
486
|
+
// Beyond 1e18 the rounding of L(mode) alone exceeds any log of the integral of exp(L - L(mode)) (the log of a width)
|
|
487
|
+
if (!Number.isFinite(lm) || Math.abs(lm) > 1e18)
|
|
488
|
+
return lm;
|
|
489
|
+
// The integrand at u = mode + t
|
|
490
|
+
const f = (t) => {
|
|
491
|
+
const v = Math.exp(near(um, t) - lm);
|
|
492
|
+
return Number.isFinite(v) ? v : 0;
|
|
493
|
+
};
|
|
494
|
+
// First width: from the curvature at the mode, measured over a step where L falls by about 1 (starting from the
|
|
495
|
+
// spread of log Y or of log W)
|
|
496
|
+
const m = 2 * a + ncp;
|
|
497
|
+
let h0 = Math.min(0.1, (2 * Math.sqrt(a + ncp)) / m, 1 / Math.sqrt(b));
|
|
498
|
+
let fall = 0;
|
|
499
|
+
for (let i = 0; i < 30; i++) {
|
|
500
|
+
fall = 2 * lm - near(um, h0) - near(um, -h0);
|
|
501
|
+
if (fall < 0.01 && h0 < 1)
|
|
502
|
+
h0 *= 10;
|
|
503
|
+
else if (fall > 100)
|
|
504
|
+
h0 /= 10;
|
|
505
|
+
else
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
const w0 = fall > 0 && Number.isFinite(fall) ? h0 / Math.sqrt(fall) : h0;
|
|
509
|
+
let sum = 0;
|
|
510
|
+
// The integrand is known to about eps |L(mode)| (its rounding noise), and costs a density of Y and a gamma function
|
|
511
|
+
const control = quadratureControl(Math.max(1e-15, 4 * Number.EPSILON * Math.abs(lm)), 300);
|
|
512
|
+
// L at the smallest double (computed when needed)
|
|
513
|
+
let lEnd;
|
|
514
|
+
for (const dir of [1, -1]) {
|
|
515
|
+
const end = dir > 0 ? U_MAX - um : um - U_MIN;
|
|
516
|
+
let s0 = 0;
|
|
517
|
+
let w = w0;
|
|
518
|
+
let prev = lm;
|
|
519
|
+
for (let i = 0; i < 200 && s0 < end; i++) {
|
|
520
|
+
const s1 = Math.min(s0 + w, end);
|
|
521
|
+
sum += kronrodAdaptive((t) => f(dir * t), s0, s1, sum, 0, control);
|
|
522
|
+
const l1 = near(um, dir * s1);
|
|
523
|
+
const rate = (prev - l1) / (s1 - s0);
|
|
524
|
+
const rest = rate > 0 ? Math.exp(l1 - lm) / rate : Infinity;
|
|
525
|
+
// For a small leftSlope the integrand can have a second, flat part towards y = 0 (for a = 1e-20 Y has nearly an
|
|
526
|
+
// atom e^(-λ/2) at 0), which the fall of the bulk would hide: there the integral goes on until L follows the
|
|
527
|
+
// power law e^(leftSlope u) down to the smallest double, and the rest is that of the power law
|
|
528
|
+
if ((dir > 0 || leftSlope > 0.5) && rest <= 1e-17 * sum)
|
|
529
|
+
break;
|
|
530
|
+
if (dir < 0 && s1 < end) {
|
|
531
|
+
lEnd ??= near(um, -end);
|
|
532
|
+
// (no flat part where even the power law from the smallest double would be negligible)
|
|
533
|
+
if (rest <= 1e-17 * sum && Math.exp(lEnd - lm) / leftSlope <= 1e-17 * sum)
|
|
534
|
+
break;
|
|
535
|
+
const predicted = l1 - leftSlope * (end - s1);
|
|
536
|
+
if (Number.isFinite(lEnd) && Math.abs(lEnd - predicted) <= 1e-9 * Math.max(1, Math.abs(lEnd))) {
|
|
537
|
+
sum += Math.exp(l1 - lm) / leftSlope;
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
// At the lower end of the range (y below the smallest double) add the rest of the tail, exp(L) falling as
|
|
542
|
+
// e^(leftSlope u) (for a shape of 1e-20 L is constant to the double there, and the rest is most of the integral)
|
|
543
|
+
if (s1 === end && dir < 0)
|
|
544
|
+
sum += Math.exp(l1 - lm) / leftSlope;
|
|
545
|
+
prev = l1;
|
|
546
|
+
s0 = s1;
|
|
547
|
+
w *= 2;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
// (a sum that overflows: the mode was not found, as where L is flat to the double for a tiny shape; -Infinity, the
|
|
551
|
+
// log of the integral from the mode alone being no better)
|
|
552
|
+
return Number.isFinite(sum) ? lm + Math.log(sum) : -Infinity;
|
|
553
|
+
}
|
|
104
554
|
function validBeta(a, b, ncp) {
|
|
105
555
|
return a > 0 && b > 0 && Number.isFinite(a) && Number.isFinite(b) && ncp >= 0 && Number.isFinite(ncp);
|
|
106
556
|
}
|
|
@@ -136,6 +586,8 @@ export function dnbeta1(x, a, b, ncp, log) {
|
|
|
136
586
|
return Infinity;
|
|
137
587
|
return log ? Math.log(a + mu) : a + mu;
|
|
138
588
|
}
|
|
589
|
+
if (negligibleNcp(mu, x * ((a + b) / a)))
|
|
590
|
+
return dbeta1(x, a, b, log);
|
|
139
591
|
return dnbetaCore(x, a, b, ncp, log);
|
|
140
592
|
}
|
|
141
593
|
export function pnbeta1(q, a, b, ncp, lowerTail, logP) {
|
|
@@ -152,6 +604,27 @@ export function pnbeta1(q, a, b, ncp, lowerTail, logP) {
|
|
|
152
604
|
const [xd, xcd] = pair(q);
|
|
153
605
|
return pnbetaRaw(a, b, ncp, xd, xcd, lowerTail, logP);
|
|
154
606
|
}
|
|
607
|
+
/** The larger of the relative spreads of Y and W, 2 sqrt(a + λ)/(2a + λ) and 1/sqrt(b) */
|
|
608
|
+
function relativeSpread(a, b, ncp) {
|
|
609
|
+
return Math.max((2 * Math.sqrt(a + ncp)) / (2 * a + ncp), 1 / Math.sqrt(b));
|
|
610
|
+
}
|
|
611
|
+
/** The distribution function of a quantile search, remembering its values: the searches come back to the same x */
|
|
612
|
+
function memoCdf(f) {
|
|
613
|
+
const cache = new Map();
|
|
614
|
+
return (x, lower, log) => {
|
|
615
|
+
const key = `${x} ${lower} ${log}`;
|
|
616
|
+
let v = cache.get(key);
|
|
617
|
+
if (v === undefined) {
|
|
618
|
+
v = f(x, lower, log);
|
|
619
|
+
cache.set(key, v);
|
|
620
|
+
}
|
|
621
|
+
return v;
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
/** Whether a shape or df is in the range where the central quantile serves as a starting value */
|
|
625
|
+
function inCentralRange(s) {
|
|
626
|
+
return s >= 1e-10 && s <= 1e15;
|
|
627
|
+
}
|
|
155
628
|
export function qnbeta1(p, a, b, ncp, lowerTail, logP) {
|
|
156
629
|
if (Number.isNaN(p) || Number.isNaN(a) || Number.isNaN(b) || Number.isNaN(ncp))
|
|
157
630
|
return NaN;
|
|
@@ -166,10 +639,10 @@ export function qnbeta1(p, a, b, ncp, lowerTail, logP) {
|
|
|
166
639
|
if (ncp === 0)
|
|
167
640
|
return qbeta1(p, a, b, lowerTail, logP);
|
|
168
641
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
169
|
-
const cdfAt = (x, l, log) => {
|
|
642
|
+
const cdfAt = memoCdf((x, l, log) => {
|
|
170
643
|
const [xd, xcd] = pair(x);
|
|
171
644
|
return pnbetaRaw(a, b, ncp, xd, xcd, l, log);
|
|
172
|
-
};
|
|
645
|
+
});
|
|
173
646
|
// If the target is already passed at an end point, the answer is not representable (0 or 1)
|
|
174
647
|
const atMin = cdfAt(Number.MIN_VALUE, lower, true);
|
|
175
648
|
if (lower ? atMin >= logTarget : atMin <= logTarget)
|
|
@@ -177,11 +650,17 @@ export function qnbeta1(p, a, b, ncp, lowerTail, logP) {
|
|
|
177
650
|
const atMax = cdfAt(1 - Number.EPSILON / 2, lower, true);
|
|
178
651
|
if (lower ? atMax < logTarget : atMax > logTarget)
|
|
179
652
|
return 1;
|
|
180
|
-
// Starting value: quantile of the approximation Beta(a + λ/2, b) (rather than a mean-matched central beta)
|
|
181
|
-
|
|
653
|
+
// Starting value: quantile of the approximation Beta(a + λ/2, b) (rather than a mean-matched central beta); its
|
|
654
|
+
// mean where the shapes are out of the central quantile's range
|
|
655
|
+
const central = method(a, b, ncp) === "mixture" && inCentralRange(a) && inCentralRange(b);
|
|
656
|
+
let x0 = central ? qbeta1(logTarget, a + ncp / 2, b, lower, true) : (a + ncp / 2) / (a + ncp / 2 + b);
|
|
182
657
|
if (!(x0 > 0 && x0 < 1))
|
|
183
658
|
x0 = 0.5;
|
|
184
|
-
|
|
659
|
+
const passes = (x) => (lower ? cdfAt(x, true, true) >= logTarget : cdfAt(x, false, true) <= logTarget);
|
|
660
|
+
const narrow = relativeSpread(a, b, ncp) < 1e-10 ? narrowQuantile(x0, 1 - Number.EPSILON / 2, passes) : undefined;
|
|
661
|
+
if (narrow !== undefined)
|
|
662
|
+
return narrow;
|
|
663
|
+
const x = invertContinuous({
|
|
185
664
|
logTarget,
|
|
186
665
|
...(linearTarget !== undefined && { linearTarget }),
|
|
187
666
|
lower,
|
|
@@ -192,12 +671,21 @@ export function qnbeta1(p, a, b, ncp, lowerTail, logP) {
|
|
|
192
671
|
lo: 0,
|
|
193
672
|
hi: 1,
|
|
194
673
|
positive: true,
|
|
674
|
+
maxIter: 60,
|
|
195
675
|
});
|
|
676
|
+
const search = () => searchDoubles(x, Number.MIN_VALUE, 1 - Number.EPSILON / 2, passes);
|
|
677
|
+
return confirmQuantile(x, cdfAt(x, lower, true), logTarget, search, passes);
|
|
196
678
|
}
|
|
197
679
|
// ---- Noncentral F ----
|
|
198
680
|
/** x = m q/(n + m q) and 1 - x = n/(n + m q) in double-double */
|
|
199
681
|
function fPair(q, m, n) {
|
|
200
682
|
const mq = twoProd(m, q);
|
|
683
|
+
if (mq[0] < 1e-290) {
|
|
684
|
+
// m q underflows (a tiny m) where m q/n need not: x = r/(1 + r), 1 - x = 1/(1 + r), r = (m/n) q
|
|
685
|
+
const r = ddMul(ddDiv([m, 0], [n, 0]), [q, 0]);
|
|
686
|
+
const d = ddAdd(r, [1, 0]);
|
|
687
|
+
return [ddDiv(r, d), ddDiv([1, 0], d)];
|
|
688
|
+
}
|
|
201
689
|
if (Number.isFinite(mq[0]) && Number.isFinite(mq[1]) && Math.abs(mq[0]) < 1e290) {
|
|
202
690
|
const d = ddAdd(mq, [n, 0]);
|
|
203
691
|
return [ddDiv(mq, d), ddDiv([n, 0], d)];
|
|
@@ -205,6 +693,15 @@ function fPair(q, m, n) {
|
|
|
205
693
|
const zc = n / m / q;
|
|
206
694
|
return [twoSum(1, -zc), [zc, 0]];
|
|
207
695
|
}
|
|
696
|
+
/** The Odds of x = m q/(n + m q): k = n/(m q), from its logs where it is out of the double range */
|
|
697
|
+
function fOdds(q, m, n, xd, xcd) {
|
|
698
|
+
const o = odds(xd, xcd);
|
|
699
|
+
if (!(o.k < 1e-300))
|
|
700
|
+
return o;
|
|
701
|
+
const k = n / m / q;
|
|
702
|
+
const lk = Math.log(n) - Math.log(m) - Math.log(q);
|
|
703
|
+
return { k, lk, l1mx: lk - Math.log1p(k) };
|
|
704
|
+
}
|
|
208
705
|
function dnfCore(q, m, n, ncp, log) {
|
|
209
706
|
if ((m / n) * q < 1e-306) {
|
|
210
707
|
// First term of the series: f(q) ≈ exp(-λ/2) (m q / n)^a / (B(a, b) q)
|
|
@@ -213,7 +710,7 @@ function dnfCore(q, m, n, ncp, log) {
|
|
|
213
710
|
return log ? l : Math.exp(l);
|
|
214
711
|
}
|
|
215
712
|
const [xd, xcd] = fPair(q, m, n);
|
|
216
|
-
return densitySum(m / 2, n / 2, ncp, xd, xcd, q, log);
|
|
713
|
+
return densitySum(m / 2, n / 2, ncp, xd, xcd, q, log, fOdds(q, m, n, xd, xcd));
|
|
217
714
|
}
|
|
218
715
|
/**
|
|
219
716
|
* q so small that x = m q/(n + m q) underflows: first term of the series
|
|
@@ -224,13 +721,13 @@ function pnfTiny(q, m, n, ncp, lower, log) {
|
|
|
224
721
|
const l = -ncp / 2 + a * (Math.log(m / n) + Math.log(q)) - Math.log(a) - lbeta(a, n / 2);
|
|
225
722
|
if (lower)
|
|
226
723
|
return log ? l : Math.exp(l);
|
|
227
|
-
return log ? -Math.exp(l) : -Math.expm1(l);
|
|
724
|
+
return log ? Math.log1p(-Math.exp(l)) : -Math.expm1(l);
|
|
228
725
|
}
|
|
229
726
|
export function pnfRaw(q, m, n, ncp, lower, log) {
|
|
230
727
|
if ((m / n) * q < 1e-306)
|
|
231
728
|
return pnfTiny(q, m, n, ncp, lower, log);
|
|
232
729
|
const [xd, xcd] = fPair(q, m, n);
|
|
233
|
-
return pnbetaRaw(m / 2, n / 2, ncp, xd, xcd, lower, log);
|
|
730
|
+
return pnbetaRaw(m / 2, n / 2, ncp, xd, xcd, lower, log, fOdds(q, m, n, xd, xcd));
|
|
234
731
|
}
|
|
235
732
|
export function dnf1(x, m, n, ncp, log) {
|
|
236
733
|
if (Number.isNaN(x) || Number.isNaN(m) || Number.isNaN(n) || Number.isNaN(ncp))
|
|
@@ -261,7 +758,8 @@ export function dnf1(x, m, n, ncp, log) {
|
|
|
261
758
|
return log ? -Infinity : 0;
|
|
262
759
|
return log ? -ncp / 2 : Math.exp(-ncp / 2);
|
|
263
760
|
}
|
|
264
|
-
|
|
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))))
|
|
265
763
|
return dfDensity1(x, m, n, log);
|
|
266
764
|
return dnfCore(x, m, n, ncp, log);
|
|
267
765
|
}
|
|
@@ -299,26 +797,31 @@ const LOG_NEGLIGIBLE = -746;
|
|
|
299
797
|
* bound is below the double range.
|
|
300
798
|
*/
|
|
301
799
|
function fTailBound(q, m, n, ncp) {
|
|
302
|
-
|
|
303
|
-
if (!(c > 0 && Number.isFinite(c)))
|
|
304
|
-
return undefined;
|
|
305
|
-
const lower = (s) => -(m / 2) * Math.log1p(2 * s) - (ncp * s) / (1 + 2 * s) - (n / 2) * Math.log1p(-2 * c * s);
|
|
306
|
-
const upper = (s) => -(m / 2) * Math.log1p(-2 * s) + (ncp * s) / (1 - 2 * s) - (n / 2) * Math.log1p(2 * c * s);
|
|
307
|
-
const best = (f, sMax) => {
|
|
308
|
-
let b = Infinity;
|
|
309
|
-
for (let i = 1; i <= 60; i++) {
|
|
310
|
-
const v = Math.min(f(sMax * 2 ** -i), f(sMax * (1 - 2 ** -i)));
|
|
311
|
-
if (v < b)
|
|
312
|
-
b = v;
|
|
313
|
-
}
|
|
314
|
-
return b;
|
|
315
|
-
};
|
|
316
|
-
if (best(lower, 1 / (2 * c)) < LOG_NEGLIGIBLE)
|
|
800
|
+
if (fChernoffLog(q, m, n, ncp, true) < LOG_NEGLIGIBLE)
|
|
317
801
|
return "lower";
|
|
318
|
-
if (
|
|
802
|
+
if (fChernoffLog(q, m, n, ncp, false) < LOG_NEGLIGIBLE)
|
|
319
803
|
return "upper";
|
|
320
804
|
return undefined;
|
|
321
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
|
+
}
|
|
322
825
|
export function qnf1(p, m, n, ncp, lowerTail, logP) {
|
|
323
826
|
if (Number.isNaN(p) || Number.isNaN(m) || Number.isNaN(n) || Number.isNaN(ncp))
|
|
324
827
|
return NaN;
|
|
@@ -337,27 +840,69 @@ export function qnf1(p, m, n, ncp, lowerTail, logP) {
|
|
|
337
840
|
}
|
|
338
841
|
function qnfRaw(p, m, n, ncp, lowerTail, logP) {
|
|
339
842
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
340
|
-
const
|
|
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
|
+
}
|
|
864
|
+
const atMin = cdfAt(Number.MIN_VALUE, lower, true);
|
|
341
865
|
if (lower ? atMin >= logTarget : atMin <= logTarget)
|
|
342
866
|
return 0;
|
|
343
|
-
const atMax =
|
|
867
|
+
const atMax = cdfAt(Number.MAX_VALUE, lower, true);
|
|
344
868
|
if (lower ? atMax < logTarget : atMax > logTarget)
|
|
345
869
|
return Infinity;
|
|
346
|
-
// Starting value: Patnaik's approximation (m + λ)/m F(ν, n), ν = (m + λ)^2/(m + 2λ)
|
|
347
|
-
|
|
348
|
-
|
|
870
|
+
// Starting value: Patnaik's approximation (m + λ)/m F(ν, n), ν = (m + λ)^2/(m + 2λ); (m + λ)/m (the ratio of the
|
|
871
|
+
// means of the χ² variables per degree of freedom) where the parameters are out of the central quantile's range
|
|
872
|
+
const nu = (m + ncp) * ((m + ncp) / (m + 2 * ncp));
|
|
873
|
+
const central = method(m / 2, n / 2, ncp) === "mixture" && inCentralRange(m) && inCentralRange(n);
|
|
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;
|
|
349
884
|
if (!(x0 > 0) || !Number.isFinite(x0))
|
|
350
885
|
x0 = 1;
|
|
351
|
-
|
|
886
|
+
const passes = (x) => (lower ? cdfAt(x, true, true) >= logTarget : cdfAt(x, false, true) <= logTarget);
|
|
887
|
+
const narrow = relativeSpread(m / 2, n / 2, ncp) < 1e-10 ? narrowQuantile(x0, Number.MAX_VALUE, passes) : undefined;
|
|
888
|
+
if (narrow !== undefined)
|
|
889
|
+
return narrow;
|
|
890
|
+
const x = invertContinuous({
|
|
352
891
|
logTarget,
|
|
353
892
|
...(linearTarget !== undefined && { linearTarget }),
|
|
354
893
|
lower,
|
|
355
|
-
cdf: (x, l) =>
|
|
356
|
-
cdfLog: (x, l) =>
|
|
894
|
+
cdf: (x, l) => cdfAt(x, l, false),
|
|
895
|
+
cdfLog: (x, l) => cdfAt(x, l, true),
|
|
357
896
|
pdfLog: (x) => dnfCore(x, m, n, ncp, true),
|
|
358
897
|
x0,
|
|
359
898
|
lo: 0,
|
|
360
899
|
hi: Infinity,
|
|
361
900
|
positive: true,
|
|
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) }),
|
|
362
905
|
});
|
|
906
|
+
const search = () => searchDoubles(x, Number.MIN_VALUE, Number.MAX_VALUE, passes);
|
|
907
|
+
return confirmQuantile(x, cdfAt(x, lower, true), logTarget, search, passes);
|
|
363
908
|
}
|