@numero-uno-jpn/rstats 0.15.1 → 0.15.2
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 +33 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/chunks/chunk-2CUKVVRO.js +2 -0
- package/dist/chunks/{chunk-3QTVXDUM.js → chunk-3WQCKGKP.js} +2 -2
- package/dist/chunks/{chunk-4ILI2FSB.js → chunk-4Z2XDWKV.js} +1 -1
- package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-AJBLCNZ7.js} +2 -2
- package/dist/chunks/chunk-D4F7GQAY.js +2 -0
- package/dist/chunks/chunk-EBQMGCQZ.js +2 -0
- package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HBMHR5SS.js} +2 -2
- package/dist/chunks/{chunk-7J4HW6VF.js → chunk-IBPWRQIF.js} +2 -2
- package/dist/chunks/chunk-JLAH3X77.js +2 -0
- package/dist/chunks/chunk-LG74QY5U.js +2 -0
- package/dist/chunks/{chunk-735NNFAS.js → chunk-PEYGUK32.js} +2 -2
- package/dist/chunks/{chunk-H4CHJMFU.js → chunk-PWVQVJTP.js} +1 -1
- package/dist/chunks/{chunk-LY2T6DA6.js → chunk-Q4PGYESP.js} +2 -2
- package/dist/chunks/{chunk-OLPTFKIZ.js → chunk-RCAL4VHM.js} +2 -2
- package/dist/chunks/{chunk-IQOVOHZG.js → chunk-SREPSVTT.js} +2 -2
- package/dist/chunks/chunk-UFT662QZ.js +2 -0
- package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-VWJ74UHC.js} +2 -2
- package/dist/chunks/{chunk-SAZAEPZC.js → chunk-XNPU54NH.js} +1 -1
- package/dist/chunks/{chunk-5SQFDLNG.js → chunk-XPLDEK6D.js} +2 -2
- package/dist/chunks/{chunk-VOIJKNL2.js → chunk-YGT4P7EU.js} +1 -1
- package/dist/chunks/{chunk-GEJCBCMP.js → chunk-YVREYVI2.js} +2 -2
- 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 +64 -1
- package/dist/distributions/exponential.js +11 -2
- package/dist/distributions/f.js +104 -21
- package/dist/distributions/gamma.d.ts +13 -2
- package/dist/distributions/gamma.js +91 -12
- package/dist/distributions/geometric.js +16 -3
- package/dist/distributions/invgamma.js +56 -10
- package/dist/distributions/kumaraswamy.js +98 -17
- package/dist/distributions/lognormal.js +28 -4
- package/dist/distributions/nbeta.js +540 -37
- package/dist/distributions/nchisq.d.ts +25 -0
- package/dist/distributions/nchisq.js +330 -92
- package/dist/distributions/negbinomial.js +20 -5
- package/dist/distributions/normal.js +3 -0
- package/dist/distributions/nt.js +263 -51
- package/dist/distributions/pareto.js +68 -19
- package/dist/distributions/t.js +48 -10
- package/dist/distributions/triangular.js +66 -27
- package/dist/distributions/tukey.js +94 -50
- package/dist/distributions/uniform.js +12 -9
- package/dist/distributions/weibull.js +43 -8
- package/dist/distributions.js +2 -2
- package/dist/internal/ddexp.d.ts +5 -0
- package/dist/internal/ddexp.js +28 -3
- package/dist/internal/discrete.js +30 -7
- package/dist/internal/invert.js +49 -0
- package/dist/internal/noncentral.d.ts +34 -0
- package/dist/internal/noncentral.js +115 -2
- package/dist/internal/poissonMixture.d.ts +7 -2
- package/dist/internal/poissonMixture.js +113 -32
- 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/dd.d.ts +1 -1
- package/dist/special/dd.js +4 -1
- package/dist/special/igam.js +93 -15
- package/dist/special/incbet.d.ts +10 -0
- package/dist/special/incbet.js +154 -48
- package/dist/special/incbetLargeA.js +47 -13
- package/dist/special/incbetPrefactor.d.ts +52 -0
- package/dist/special/incbetPrefactor.js +92 -0
- package/dist/special/incbetTemme.js +15 -3
- package/dist/special/loader.js +33 -7
- 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-TYFGVCVQ.js +0 -2
- package/dist/chunks/chunk-UWBYDIOH.js +0 -2
- package/dist/chunks/chunk-UZHY5DWN.js +0 -2
|
@@ -12,6 +12,7 @@ import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
|
12
12
|
import { discreteQuantile } from "../internal/discrete.js";
|
|
13
13
|
import { nonInt } from "../internal/integer.js";
|
|
14
14
|
import { betaIncDD } from "../special/incbet.js";
|
|
15
|
+
import { prefactorParts } from "../special/incbetPrefactor.js";
|
|
15
16
|
import { dbinomRawDD, dpoisRaw } from "../special/loader.js";
|
|
16
17
|
import { ddAdd, ddDiv, ddMul, twoSum } from "../special/dd.js";
|
|
17
18
|
import { qnorm } from "./normal.js";
|
|
@@ -51,7 +52,8 @@ function pnbinomRaw(x, par, lower, log) {
|
|
|
51
52
|
* I_z(A, B) = z^A (1 - z)^B / (A B(A, B)) [1 + Σ_{k>=1} Π_{j<k} (A + B + j) / (A + 1 + j) z^k]
|
|
52
53
|
* (z = q, A = x + 1, B = size). In this region the incomplete beta continued fraction is off by about 3e-14
|
|
53
54
|
* relative near the mean. All terms are positive, so there is no cancellation. The number of terms is about
|
|
54
|
-
* -log(1e-
|
|
55
|
+
* -log(1e-18 p) / -log z (at most about 5e5 for p >= 1e-4). Returns undefined when not usable (too many terms, or the
|
|
56
|
+
* value exceeds 1/2)
|
|
55
57
|
*/
|
|
56
58
|
function upperSeries(x, par) {
|
|
57
59
|
const { size, pd, qd } = par;
|
|
@@ -61,8 +63,13 @@ function upperSeries(x, par) {
|
|
|
61
63
|
return undefined;
|
|
62
64
|
const A = x + 1;
|
|
63
65
|
const B = size;
|
|
64
|
-
// Prefactor z^A (1 - z)^B / (A B(A, B)) = b(A; A + B, z) B / (A + B)
|
|
65
|
-
|
|
66
|
+
// Prefactor z^A (1 - z)^B / (A B(A, B)) = b(A; A + B, z) B / (A + B), or for B < 1 from the small-shape form of
|
|
67
|
+
// incbet.ts (incbetPrefactor.ts): Loader's form loses about ε |log B| / 2 there (5e-14 at size 5.6e-245:
|
|
68
|
+
// pnbinom(1545, 5.6e-245, { mu: 4.6e-241, lowerTail: false }))
|
|
69
|
+
const parts = prefactorParts(A, B, qd, pd);
|
|
70
|
+
const pre = parts !== undefined
|
|
71
|
+
? (parts[0] * (Math.exp(parts[1][0]) * (1 + parts[1][1]))) / A
|
|
72
|
+
: (dbinomRawDD(A, B, qd, pd, false) * B) / (A + B);
|
|
66
73
|
if (!(pre > 1e-290))
|
|
67
74
|
return undefined;
|
|
68
75
|
// Rounding of the term ratios accumulates with the number of terms (1e-12 over tens of thousands), so keep terms and sum in double-double
|
|
@@ -73,7 +80,9 @@ function upperSeries(x, par) {
|
|
|
73
80
|
const ratio = ddDiv(ddAdd(ab, [k, 0]), [A + 1 + k, 0]);
|
|
74
81
|
term = ddMul(ddMul(term, ratio), qd);
|
|
75
82
|
sum = ddAdd(sum, term);
|
|
76
|
-
|
|
83
|
+
// The term ratios rise towards z, so the rest of the series is below term z/(1 - z) < term/p: stopping at
|
|
84
|
+
// term < 1e-18 sum alone left 8e-15 of it out at p = 1.2e-4 (pnbinom(1545, 5.6e-245, { mu: 4.6e-241, lowerTail: false }))
|
|
85
|
+
if (term[0] < 1e-18 * sum[0] * p) {
|
|
77
86
|
const v = pre * (sum[0] + sum[1]);
|
|
78
87
|
return v <= 0.5 ? v : undefined;
|
|
79
88
|
}
|
|
@@ -93,7 +102,12 @@ function quantileRaw(p, par, lowerTail, logP) {
|
|
|
93
102
|
// For small p, X (-log q) is nearly gamma (shape = size). Even for highly skewed distributions (small size)
|
|
94
103
|
// this is a much closer starting value than Cornish-Fisher, reducing distribution-function evaluations
|
|
95
104
|
// -log q = log(1 + p/q) (taking log of a rounded q loses digits for small p)
|
|
96
|
-
|
|
105
|
+
const g = qgamma1(p, par.size, 1, lowerTail, logP);
|
|
106
|
+
y0 = g / Math.log1p(prob / (par.qd[0] + par.qd[1]));
|
|
107
|
+
// A finite gamma quantile divided by a tiny -log q beyond the doubles: so is the quantile (the distribution
|
|
108
|
+
// function cannot be evaluated out there; qnbinom(2.2e-284, 2.7e37, { prob: 1.9e-279 }) threw a TypeError)
|
|
109
|
+
if (y0 === Infinity && g > 0 && Number.isFinite(g))
|
|
110
|
+
return Infinity;
|
|
97
111
|
}
|
|
98
112
|
else {
|
|
99
113
|
const z = qnorm(p, { lowerTail, logP });
|
|
@@ -214,6 +228,7 @@ export function dnbinomMu1(x, size, mu, log) {
|
|
|
214
228
|
if (size === 0) {
|
|
215
229
|
if (n === 0)
|
|
216
230
|
return log ? 0 : 1;
|
|
231
|
+
// NaN at mu = 0 to match R (dnbinom(1, 0, mu = 0) is NaN there), although size = 0 and mu = 0 is the point mass at 0
|
|
217
232
|
return mu === 0 ? NaN : d0(log);
|
|
218
233
|
}
|
|
219
234
|
if (mu === Infinity)
|
|
@@ -16,6 +16,9 @@ const SQRT1_2 = Math.SQRT1_2;
|
|
|
16
16
|
* Where the result is subnormal, rounding happens only in the final multiplication.
|
|
17
17
|
*/
|
|
18
18
|
function scaledExpNegHalfSq(a, scale) {
|
|
19
|
+
// exp(-a^2 / 2) underflows for any finite scale (and a * 16 would overflow near the largest double)
|
|
20
|
+
if (a > 1e10)
|
|
21
|
+
return 0;
|
|
19
22
|
const hi = Math.floor(a * 16) / 16;
|
|
20
23
|
const lo = a - hi;
|
|
21
24
|
const halfHiSq = (hi * hi) / 2;
|
package/dist/distributions/nt.js
CHANGED
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
// When |t| is large and Φ(t S - δ) becomes a steep step relative to the width of the density of w, switch to
|
|
21
21
|
// formula B, obtained by integrating by parts in v = t S - δ (a peak of φ × the incomplete gamma function; see
|
|
22
22
|
// the comment on integrate). The far tail with even larger |t| is extrapolated by the power law P ∝ |t|^(-ν)
|
|
23
|
-
// (farLimit).
|
|
23
|
+
// (farLimit). For a large ν (a > 1e5) the integral is taken by Laplace's method wherever its neglected terms are below
|
|
24
|
+
// the rounding (laplace); for a small ν (a < 1e-5) formula B is used at any t, and a t so near 0 that P is Φ(-δ) to
|
|
25
|
+
// the double is given that value (nearZero). The smaller tail is computed, the larger is 1 - (smaller).
|
|
24
26
|
//
|
|
25
27
|
// For accuracy, the node w and, on top of it, e^(w/2), the exponent a (e^w - 1 - w) and the normal argument
|
|
26
28
|
// t S - δ are formed in double-double (rounding in each is amplified hundreds of times in the tails). Nodes
|
|
@@ -28,10 +30,11 @@
|
|
|
28
30
|
// precision (the fast path).
|
|
29
31
|
import { quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
30
32
|
import { invertContinuous } from "../internal/invert.js";
|
|
31
|
-
import { ddAdd, ddMul, ddMulNum, twoSum } from "../special/dd.js";
|
|
33
|
+
import { ddAdd, ddMul, ddMulNum, twoProd, twoSum } from "../special/dd.js";
|
|
32
34
|
import { erfcxNonNegative } from "../special/erf.js";
|
|
33
35
|
import { gammaInc } from "../special/igam.js";
|
|
34
36
|
import { dpoisRaw, stirlerr } from "../special/loader.js";
|
|
37
|
+
import { lgamma1p } from "../special/gamma.js";
|
|
35
38
|
import { pnorm, qnorm } from "./normal.js";
|
|
36
39
|
const LN_SQRT_2PI = 0.918938533204672741780329736406;
|
|
37
40
|
const SQRT_2_PI = 0.7978845608028654; // sqrt(2/pi)
|
|
@@ -88,6 +91,23 @@ function millsInv(z) {
|
|
|
88
91
|
return SQRT_2_PI / erfcxNonNegative(-z * Math.SQRT1_2);
|
|
89
92
|
return Math.exp(-0.5 * z * z - LN_SQRT_2PI) / pnorm(z);
|
|
90
93
|
}
|
|
94
|
+
/** Below this x = a e^w, P(a, x) = x^a / Γ(1 + a) (1 + O(x)) is taken from log x = log a + w (x may underflow) */
|
|
95
|
+
const X_TINY = 1e-300;
|
|
96
|
+
/** a e^w, also where e^w overflows (a tiny a with |t| tiny takes formula B out to S = |δ|/|t|) */
|
|
97
|
+
function shapeTimesExp(a, w) {
|
|
98
|
+
return w < 700 ? a * Math.exp(w) : Math.exp(Math.log(a) + w);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* log G(a, x) at x = a e^w (G = P if lower, Q otherwise) and log(x^a e^-x / Γ(a + 1)) (the derivative of P in log x).
|
|
102
|
+
* For a tiny a the integrals reach x far below the double range, where P(a, x) is still near 1 (x^a = e^(a log x))
|
|
103
|
+
*/
|
|
104
|
+
function gammaLog(a, w, lower) {
|
|
105
|
+
const x = shapeTimesExp(a, w);
|
|
106
|
+
if (x > X_TINY)
|
|
107
|
+
return [gammaInc(a, x, lower, true), dpoisRaw(a, x, true)];
|
|
108
|
+
const lp = a * (Math.log(a) + w) - lgamma1p(a);
|
|
109
|
+
return [lower ? lp : lp > -Math.LN2 ? Math.log(-Math.expm1(lp)) : Math.log1p(-Math.exp(lp)), lp];
|
|
110
|
+
}
|
|
91
111
|
/** Log of the integrand (double precision, for locating the peak) and its first and second derivatives */
|
|
92
112
|
function logIntegrand(pr, w) {
|
|
93
113
|
const { a, t, delta } = pr;
|
|
@@ -98,10 +118,10 @@ function logIntegrand(pr, w) {
|
|
|
98
118
|
const m = expm1mxNum(w);
|
|
99
119
|
if (pr.kind === "cdfQ" || pr.kind === "cdfP") {
|
|
100
120
|
// log(t S / 2) + log φ(z) + log G(a, x), x = a S^2 = a e^w
|
|
101
|
-
const x = a
|
|
121
|
+
const x = shapeTimesExp(a, w);
|
|
102
122
|
const upperG = pr.kind === "cdfQ";
|
|
103
|
-
const lG =
|
|
104
|
-
const xf = a * Math.exp(
|
|
123
|
+
const [lG, ld] = gammaLog(a, w, !upperG);
|
|
124
|
+
const xf = a * Math.exp(ld - lG); // x f(x) / G
|
|
105
125
|
const sgn = upperG ? -1 : 1;
|
|
106
126
|
const L = Math.log(t / 2) + w / 2 - 0.5 * z * z - LN_SQRT_2PI + lG;
|
|
107
127
|
const d1 = 0.5 - (z * tS) / 2 + sgn * xf;
|
|
@@ -128,8 +148,10 @@ function logIntegrand(pr, w) {
|
|
|
128
148
|
function findPeak(pr) {
|
|
129
149
|
let w = 0;
|
|
130
150
|
let [L, d1, d2] = logIntegrand(pr, w);
|
|
131
|
-
|
|
132
|
-
|
|
151
|
+
// (where L is convex the step is the largest one; for a tiny a the peak of the density can be at w = 800, with L
|
|
152
|
+
// convex most of the way from 0)
|
|
153
|
+
for (let i = 0; i < 400; i++) {
|
|
154
|
+
let step = d2 < 0 ? -d1 / d2 : 4 * Math.sign(d1);
|
|
133
155
|
if (!Number.isFinite(step))
|
|
134
156
|
step = Math.sign(d1) || 0;
|
|
135
157
|
step = Math.max(-4, Math.min(4, step));
|
|
@@ -187,8 +209,10 @@ function integrand(pr, w, log, peak = Infinity) {
|
|
|
187
209
|
return [v, 0];
|
|
188
210
|
}
|
|
189
211
|
else {
|
|
190
|
-
|
|
191
|
-
|
|
212
|
+
// (one exp where C_a e^-E underflows on its own: for a tiny a, C_a is about a, and S can be 1e100)
|
|
213
|
+
const lv = pr.logC - Ed - 0.5 * z * z - LN_SQRT_2PI;
|
|
214
|
+
const v = lv > -700 ? Math.exp(lv) * Sd : Math.exp(lv + wv / 2);
|
|
215
|
+
if ((Ed + Math.abs(z * tS) + z * z + (lv > -700 ? 0 : -lv)) * v <= 4 * peak)
|
|
192
216
|
return [v, 0];
|
|
193
217
|
}
|
|
194
218
|
}
|
|
@@ -198,16 +222,18 @@ function integrand(pr, w, log, peak = Infinity) {
|
|
|
198
222
|
// (t S / 2) φ(t S - δ) G(a, a S^2)
|
|
199
223
|
const zd = ddAdd(ddMulNum(S, t), [-delta, 0]);
|
|
200
224
|
const zz = ddMul(zd, zd);
|
|
201
|
-
const x = a * ew[0] + a * ew[1];
|
|
225
|
+
const x = w[0] < 700 ? a * ew[0] + a * ew[1] : shapeTimesExp(a, w[0] + w[1]);
|
|
202
226
|
const lower = pr.kind === "cdfP";
|
|
203
227
|
if (log) {
|
|
204
|
-
const rest = Math.log(t / 2) + Math.log(S[0]) + S[1] / S[0] - LN_SQRT_2PI +
|
|
228
|
+
const rest = Math.log(t / 2) + Math.log(S[0]) + S[1] / S[0] - LN_SQRT_2PI + gammaLog(a, w[0] + w[1], lower)[0];
|
|
205
229
|
return ddAdd([-zz[0] / 2, -zz[1] / 2], [rest, 0]);
|
|
206
230
|
}
|
|
207
|
-
const g = gammaInc(a, x, lower, false);
|
|
231
|
+
const g = x > X_TINY ? gammaInc(a, x, lower, false) : Math.exp(gammaLog(a, w[0] + w[1], lower)[0]);
|
|
208
232
|
return [(t / 2) * (S[0] + S[1]) * Math.exp(-0.5 * zz[0] - LN_SQRT_2PI) * (1 - zz[1] / 2) * g, 0];
|
|
209
233
|
}
|
|
210
|
-
|
|
234
|
+
// a (e^w - 1 - w); (a tiny a reaches e^w near the overflow threshold, where the splitting in ddMulNum overflows)
|
|
235
|
+
const m = expm1mx(w, ew);
|
|
236
|
+
const E = m[0] < 1e290 ? ddMulNum(m, a) : twoSum(a * m[0], a * m[1]);
|
|
211
237
|
if (!(E[0] < Infinity))
|
|
212
238
|
return [log ? -Infinity : 0, 0];
|
|
213
239
|
const zd = ddAdd(ddMulNum(S, t), [-delta, 0]);
|
|
@@ -225,7 +251,10 @@ function integrand(pr, w, log, peak = Infinity) {
|
|
|
225
251
|
const l = ddAdd(ddAdd([pr.logC - E[0], -E[1]], [-zz[0] / 2, -zz[1] / 2]), [w[0] / 2 - LN_SQRT_2PI, w[1] / 2]);
|
|
226
252
|
return l;
|
|
227
253
|
}
|
|
228
|
-
|
|
254
|
+
const lv = pr.logC - E[0] - 0.5 * zz[0] - LN_SQRT_2PI;
|
|
255
|
+
if (lv < -700)
|
|
256
|
+
return [Math.exp(lv + w[0] / 2) * (1 - E[1] - zz[1] / 2 + w[1] / 2), 0];
|
|
257
|
+
return [Math.exp(lv) * (1 - E[1] - zz[1] / 2) * (S[0] + S[1]), 0];
|
|
229
258
|
}
|
|
230
259
|
/** Trapezoidal sum (step h in z, extending both ways until negligible) */
|
|
231
260
|
function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
|
|
@@ -265,11 +294,22 @@ function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
|
|
|
265
294
|
return sum;
|
|
266
295
|
}
|
|
267
296
|
function integrate(pr, needLog) {
|
|
297
|
+
if (pr.a > A_LAPLACE) {
|
|
298
|
+
const v = laplace(pr);
|
|
299
|
+
if (v !== undefined)
|
|
300
|
+
return v;
|
|
301
|
+
}
|
|
268
302
|
if (pr.kind === "cdf") {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const
|
|
272
|
-
|
|
303
|
+
// (for a tiny a the density of w is spread over w down to about -1/a, and the peak of formula A is not found;
|
|
304
|
+
// formula B is used at any t)
|
|
305
|
+
const pk = pr.a < A_TINY ? undefined : findPeak(pr);
|
|
306
|
+
let formulaB = true;
|
|
307
|
+
if (pk !== undefined) {
|
|
308
|
+
const S = Math.exp(pk.w / 2);
|
|
309
|
+
const widthW = Math.min(1 / Math.sqrt(pr.a * Math.exp(pk.w)), 10);
|
|
310
|
+
formulaB = (Math.abs(pr.t) * S * widthW) / 2 > 2;
|
|
311
|
+
}
|
|
312
|
+
if (formulaB) {
|
|
273
313
|
if (pr.t > 0) {
|
|
274
314
|
const r = integrateCore({ ...pr, kind: "cdfQ" }, needLog);
|
|
275
315
|
return {
|
|
@@ -277,12 +317,161 @@ function integrate(pr, needLog) {
|
|
|
277
317
|
log: needLog ? logAddExp(pnorm(-pr.delta, { logP: true }), r.log) : NaN,
|
|
278
318
|
};
|
|
279
319
|
}
|
|
280
|
-
|
|
320
|
+
if (pr.a >= A_TINY)
|
|
321
|
+
return integrateCore({ ...pr, t: -pr.t, delta: -pr.delta, kind: "cdfP" }, needLog);
|
|
322
|
+
// E[Φ(t S - δ)] = Φ(-δ) - ∫ φ(|t| S + δ) |t| S Q(a, a S²) du: for a tiny a, Q is at most about a |log(a S²)|, and
|
|
323
|
+
// the integral is a small correction (the integral with P(a, a S²), near Φ(-δ), would carry its error in full)
|
|
324
|
+
const r = integrateCore({ ...pr, t: -pr.t, delta: -pr.delta, kind: "cdfQ" }, needLog);
|
|
325
|
+
const l0 = pnorm(-pr.delta, { logP: true });
|
|
326
|
+
return {
|
|
327
|
+
lin: pnorm(-pr.delta) - r.lin,
|
|
328
|
+
log: needLog ? l0 + Math.log1p(-Math.exp(r.log - l0)) : NaN,
|
|
329
|
+
};
|
|
281
330
|
}
|
|
282
331
|
return integrateCore(pr, needLog, pk);
|
|
283
332
|
}
|
|
284
333
|
return integrateCore(pr, needLog);
|
|
285
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* Below this a, formula B is used at any t: the peak of formula A is not found for a below about 1e-15, and from
|
|
337
|
+
* 1e-5 down its error reaches 5e-15 of P, where formula B, a correction of order a log(t²/a) to Φ(-δ), keeps 1e-16
|
|
338
|
+
*/
|
|
339
|
+
const A_TINY = 1e-5;
|
|
340
|
+
/**
|
|
341
|
+
* Above this a (half the degrees of freedom) the integral is taken by Laplace's method where that is accurate (laplace:
|
|
342
|
+
* the far tails from here on, everywhere from about a = 3e8). The width of the density of w is 1/sqrt(a): from about
|
|
343
|
+
* a = 5e7 on the trapezoidal sum loses digits in the far tails (7e-9 of log P at a = 5e7, NaN or Infinity at 1e14), and
|
|
344
|
+
* from 1e29 on it does not resolve the width even near the centre
|
|
345
|
+
*/
|
|
346
|
+
const A_LAPLACE = 1e5;
|
|
347
|
+
/**
|
|
348
|
+
* log Φ(u) and its derivatives: [ℓ1, ℓ2, ℓ3, ℓ4] with ℓ1 = m = φ(u)/Φ(u), ℓ2 = -m (u + m), ℓ3 = -ℓ2 (u + m) - m (1 + ℓ2),
|
|
349
|
+
* ℓ4 = -ℓ3 (u + 2m) - 2 ℓ2 (1 + ℓ2). For u = -z < -3, u + m and 1 + ℓ2 cancel (to 1/z and 1/z²); there they come from
|
|
350
|
+
* the continued fraction m - z = K = 1/(z + K2), K2 = 2/(z + 3/(z + 4/(z + ...))) (the continued fraction of Mills'
|
|
351
|
+
* ratio), as u + m = K and 1 + ℓ2 = 1 - (z + K) K = K (K2 - K) (since K (z + K2) = 1). ℓ3 and ℓ4 still cancel, but
|
|
352
|
+
* they only enter the correction term of laplace
|
|
353
|
+
*/
|
|
354
|
+
function logPhiDerivs(u) {
|
|
355
|
+
let m;
|
|
356
|
+
let r;
|
|
357
|
+
let p2;
|
|
358
|
+
if (u < -3) {
|
|
359
|
+
const z = -u;
|
|
360
|
+
// (to 1e-16 with these terms: the error falls as e^(-2 z sqrt(n)))
|
|
361
|
+
let k2 = 0;
|
|
362
|
+
for (let n = Math.ceil(20 + 600 / (z * z)); n >= 2; n--)
|
|
363
|
+
k2 = n / (z + k2);
|
|
364
|
+
r = 1 / (z + k2);
|
|
365
|
+
m = z + r;
|
|
366
|
+
p2 = r * (k2 - r);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
m = millsInv(u);
|
|
370
|
+
r = u + m;
|
|
371
|
+
p2 = 1 - m * r;
|
|
372
|
+
}
|
|
373
|
+
const l2 = -m * r;
|
|
374
|
+
if (u < -1e3) {
|
|
375
|
+
// ℓ3 and ℓ4 from their asymptotic series (the derivatives of ℓ2 = -(1 - 1/z² + 6/z⁴ - 50/z⁶ + ...)): computed as
|
|
376
|
+
// above they keep an error of eps/z², which v⁴ in laplace would turn into an overflow
|
|
377
|
+
const s = 1 / (u * u);
|
|
378
|
+
return [m, l2, -(2 - 24 * s + 300 * s * s) * s / u, (6 - 120 * s + 2100 * s * s) * s * s];
|
|
379
|
+
}
|
|
380
|
+
const l3 = -l2 * r - m * p2;
|
|
381
|
+
const l4 = -l3 * (u + 2 * m) - 2 * l2 * p2;
|
|
382
|
+
return [m, l2, l3, l4];
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* The integral of integrate (kind "cdf" or "pdf") by Laplace's method in w, for a large a: with L the log of the
|
|
386
|
+
* integrand, w* its maximum and L2 = -L''(w*), L3, L4 the next derivatives there,
|
|
387
|
+
* ∫ e^L dw = e^L(w*) sqrt(2 pi / L2) (1 + L4/(8 L2²) + 5 L3²/(24 L2³) + ...)
|
|
388
|
+
* (the expansion in the moments of a normal variable of variance 1/L2). For the density of w alone (L2 = L3 = L4 = a)
|
|
389
|
+
* the correction is 1/(12a), which cancels stirlerr(a) in C_a. The next terms are of order 1/L2² and q^6 with
|
|
390
|
+
* q = (|v|/2) / (sqrt(L2) max(1, -u)) (v = t S, u = v - δ: the k-th derivative of log Φ(u(w)) is about (v/2)^k ℓk(u),
|
|
391
|
+
* and ℓk(u) about |u|^-k for u << 0); relative to the value they are an error of as much in its log, so they are to be
|
|
392
|
+
* below 1e-17 of |log value| or of 1: near the centre that takes L2 >= 3e8 and q <= 1e-3, far in a tail (log P of -1e6
|
|
393
|
+
* and below) much less. Undefined elsewhere (also where |δ| is of order sqrt(a) with t near δ, where T = (Z + δ)/S is
|
|
394
|
+
* not nearly normal). The derivatives in w follow from v' = v/2:
|
|
395
|
+
* cdf: L = log C_a - a (e^w - 1 - w) + log Φ(u)
|
|
396
|
+
* pdf: L = log C_a - a (e^w - 1 - w) + w/2 - u²/2 - log sqrt(2 pi)
|
|
397
|
+
* The far tails, where w* is far from 0 (e^w* about 2a/t² for |t| >> sqrt(a)), are included: there log Φ(u) is nearly
|
|
398
|
+
* quadratic in v and its derivatives are all of order v², as large as L2.
|
|
399
|
+
*/
|
|
400
|
+
function laplace(pr) {
|
|
401
|
+
const { a, t, delta, kind } = pr;
|
|
402
|
+
const cdf = kind === "cdf";
|
|
403
|
+
const at = (w) => {
|
|
404
|
+
const ew = Math.exp(w);
|
|
405
|
+
const v = t * Math.exp(w / 2);
|
|
406
|
+
const u = Math.abs(w) < 1 ? t - delta + t * Math.expm1(w / 2) : v - delta;
|
|
407
|
+
let f;
|
|
408
|
+
if (cdf) {
|
|
409
|
+
const [l1, l2, l3, l4] = logPhiDerivs(u);
|
|
410
|
+
const v2 = v * v;
|
|
411
|
+
// (ℓk falls as |u|^-k where v is huge: multiplied in this order the terms do not overflow)
|
|
412
|
+
f = [
|
|
413
|
+
(l1 * v) / 2,
|
|
414
|
+
(l2 * v2 + l1 * v) / 4,
|
|
415
|
+
(l3 * v * v2 + 3 * l2 * v2 + l1 * v) / 8,
|
|
416
|
+
(l4 * v2 * v2 + 6 * l3 * v * v2 + 7 * l2 * v2 + l1 * v) / 16,
|
|
417
|
+
];
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
f = [0.5 - (u * v) / 2, -(v * v + u * v) / 4, -(3 * v * v + u * v) / 8, -(7 * v * v + u * v) / 16];
|
|
421
|
+
}
|
|
422
|
+
return { u, v, f, d1: -a * Math.expm1(w) + f[0], L2: a * ew - f[1] };
|
|
423
|
+
};
|
|
424
|
+
// w*: Newton's method on L' inside a bracket (L' > 0 at w = -1400, where v is 0 to the double, and < 0 at 50),
|
|
425
|
+
// bisecting where a step leaves it. Below -1400 e^(w/2) is subnormal
|
|
426
|
+
let lo = -1400;
|
|
427
|
+
let hi = 50;
|
|
428
|
+
let w = 0;
|
|
429
|
+
let p = at(w);
|
|
430
|
+
for (let i = 0; i < 300 && hi - lo > 4e-16 * Math.max(1, Math.abs(w)); i++) {
|
|
431
|
+
// (a Newton step this small leaves an error of L2 step²/2 < 1e-18 in L(w*))
|
|
432
|
+
const step = p.d1 / p.L2;
|
|
433
|
+
if (Math.abs(step) * Math.sqrt(p.L2) < 1e-9)
|
|
434
|
+
break;
|
|
435
|
+
if (p.d1 > 0)
|
|
436
|
+
lo = w;
|
|
437
|
+
else if (p.d1 < 0)
|
|
438
|
+
hi = w;
|
|
439
|
+
else
|
|
440
|
+
break;
|
|
441
|
+
const next = w + step;
|
|
442
|
+
w = next > lo && next < hi ? next : (lo + hi) / 2;
|
|
443
|
+
p = at(w);
|
|
444
|
+
}
|
|
445
|
+
const { u, v, f, L2 } = p;
|
|
446
|
+
const ew = Math.exp(w);
|
|
447
|
+
if (!(w > -1390 && L2 >= 1e5))
|
|
448
|
+
return undefined;
|
|
449
|
+
const r3 = (-a * ew + f[2]) / L2;
|
|
450
|
+
const r4 = (-a * ew + f[3]) / L2;
|
|
451
|
+
const c1 = r4 / L2 / 8 + (5 * r3 * r3) / L2 / 24;
|
|
452
|
+
// log(L2/a), without cancellation near w = 0
|
|
453
|
+
const x = Math.expm1(w) - f[1] / a;
|
|
454
|
+
const lr = Math.abs(x) < 0.5 ? Math.log1p(x) : Math.log(ew - f[1] / a);
|
|
455
|
+
// log C_a + log sqrt(2 pi / L2) + log(1 + c1) - a (e^w - 1 - w); u in double-double where it is formed from t - δ
|
|
456
|
+
const rest = -0.5 * lr - stirlerr(a) + Math.log1p(c1) - a * expm1mxNum(w);
|
|
457
|
+
const ud = Math.abs(w) < 1 ? ddAdd(twoSum(t, -delta), twoProd(t, Math.expm1(w / 2))) : twoSum(v, -delta);
|
|
458
|
+
let value;
|
|
459
|
+
if (cdf) {
|
|
460
|
+
const l = logPhiDD(ud);
|
|
461
|
+
const log = l[0] + (l[1] + rest);
|
|
462
|
+
value = { lin: log < -745 ? 0 : pnorm(ud[0]) * (1 + ud[1] * millsInv(ud[0])) * Math.exp(rest), log };
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
const uu = ddMul(ud, ud);
|
|
466
|
+
const rest2 = rest + w / 2 - LN_SQRT_2PI - uu[1] / 2;
|
|
467
|
+
const hiPart = -uu[0] / 2;
|
|
468
|
+
const log = hiPart + rest2;
|
|
469
|
+
value = { lin: hiPart > -700 ? Math.exp(hiPart) * Math.exp(rest2) : Math.exp(log), log };
|
|
470
|
+
}
|
|
471
|
+
// The neglected terms (in the far tails, where only the log is representable, a smaller L2 is enough)
|
|
472
|
+
const q = cdf ? Math.abs(v) / 2 / (Math.sqrt(L2) * Math.max(1, -u)) : 0;
|
|
473
|
+
return 1 / (L2 * L2) + q ** 6 <= 1e-17 * Math.max(1, Math.abs(value.log)) ? value : undefined;
|
|
474
|
+
}
|
|
286
475
|
function logAddExp(x, y) {
|
|
287
476
|
if (x === -Infinity)
|
|
288
477
|
return y;
|
|
@@ -338,45 +527,63 @@ function farLimit(a, delta) {
|
|
|
338
527
|
}
|
|
339
528
|
/** Log of the far tail (upper if t > 0, lower if t < 0). Extrapolated if |t| > farLimit */
|
|
340
529
|
function farTailLog(t, a, delta) {
|
|
530
|
+
return Math.abs(t) > farLimit(a, delta) ? farTails(t, a, delta, t < 0).log : pntBoth(t, 2 * a, delta, t < 0).log;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* P[T <= t] (lower) / P[T > t] for |t| > T0 = farLimit, from both tails at T0 (same sign): the far tail times
|
|
534
|
+
* (T0/|t|)^(2a), and the near tail grown by what the far tail lost. The near tail is 1 - (far) unless the far tail
|
|
535
|
+
* is still the larger (for a tiny a it falls only as |t|^(-2a), from about Φ(±δ))
|
|
536
|
+
*/
|
|
537
|
+
function farTails(t, a, delta, lower) {
|
|
341
538
|
const T0 = farLimit(a, delta);
|
|
342
539
|
const s = Math.sign(t);
|
|
343
|
-
const
|
|
344
|
-
const
|
|
345
|
-
const
|
|
346
|
-
|
|
540
|
+
const farLower = s < 0;
|
|
541
|
+
const f0 = pntBoth(s * T0, 2 * a, delta, farLower);
|
|
542
|
+
const k = 2 * a * (Math.log(Math.abs(t)) - Math.log(T0));
|
|
543
|
+
const far = { lin: f0.lin > 0 ? f0.lin * Math.exp(-k) : Math.exp(f0.log - k), log: f0.log - k };
|
|
544
|
+
if (lower === farLower)
|
|
545
|
+
return far;
|
|
546
|
+
if (far.lin < 0.5)
|
|
547
|
+
return { lin: 1 - far.lin, log: Math.log1p(-far.lin) };
|
|
548
|
+
const n0 = pntBoth(s * T0, 2 * a, delta, !farLower);
|
|
549
|
+
const gain = -Math.expm1(-k);
|
|
550
|
+
return { lin: n0.lin + f0.lin * gain, log: logAddExp(n0.log, f0.log + Math.log(gain)) };
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Whether |t| is so small that P[T <= t] = Φ(-δ) and P[T > t] = Φ(δ), their values at t = 0, to double precision.
|
|
554
|
+
* For t > 0, P[T <= t] - Φ(-δ) = P[0 < Z + δ <= t S] = ∫ φ(x - δ) Q(a, a x²/t²) dx over x > 0 (t < 0 likewise). Beyond
|
|
555
|
+
* x1 = |t| sqrt(y1/a), y1 = 4a + 2δ² + 800, Q(a, y) < e^(-0.4 y) is far below Φ(∓δ); below x1 the integral is at most
|
|
556
|
+
* x1 φ(δ) (x1 |δ| < 1), at most x1 (|δ| + 2) relative to Φ(∓δ). Formula B would otherwise need S out to |δ|/|t|, where
|
|
557
|
+
* e^w overflows
|
|
558
|
+
*/
|
|
559
|
+
function nearZero(t, a, delta) {
|
|
560
|
+
return Math.abs(t) * Math.sqrt((4 * a + 2 * delta * delta + 800) / a) * (Math.abs(delta) + 2) <= 1e-17;
|
|
347
561
|
}
|
|
348
|
-
/** Linear value and log of P[T <= t] (lower) / P[T > t] (the log
|
|
349
|
-
function pntBoth(t, df, delta, lower) {
|
|
562
|
+
/** Linear value and log of P[T <= t] (lower) / P[T > t] (the log may be NaN unless needLog) */
|
|
563
|
+
function pntBoth(t, df, delta, lower, needLog = true) {
|
|
350
564
|
const a = df / 2;
|
|
351
|
-
if (
|
|
352
|
-
const
|
|
353
|
-
|
|
354
|
-
if (far)
|
|
355
|
-
return { lin: Math.exp(lf), log: lf };
|
|
356
|
-
return { lin: -Math.expm1(lf), log: Math.log1p(-Math.exp(lf)) };
|
|
565
|
+
if (nearZero(t, a, delta)) {
|
|
566
|
+
const d = lower ? -delta : delta;
|
|
567
|
+
return { lin: pnorm(d), log: pnorm(d, { logP: true }) };
|
|
357
568
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
569
|
+
if (Math.abs(t) > farLimit(a, delta))
|
|
570
|
+
return farTails(t, a, delta, lower);
|
|
571
|
+
// The upper tail E[Φ(δ - t S)] is the lower tail with (t, δ) replaced by (-t, -δ). The smaller tail is integrated
|
|
572
|
+
// and the larger is 1 - (smaller), with its log by log1p: integrated itself it can exceed 1 by a few ulps. The lower
|
|
573
|
+
// tail is likely the smaller below the median, about δ (when the guess is wrong the other tail is integrated too)
|
|
574
|
+
const tail = (l) => integrate(l ? problem(a, t, delta, "cdf") : problem(a, -t, -delta, "cdf"), needLog && l === lower);
|
|
575
|
+
let side = t < delta;
|
|
576
|
+
let v = tail(side);
|
|
577
|
+
if (v.lin > 0.5) {
|
|
578
|
+
side = !side;
|
|
579
|
+
v = tail(side);
|
|
580
|
+
}
|
|
581
|
+
if (side === lower)
|
|
582
|
+
return v;
|
|
583
|
+
return { lin: 1 - v.lin, log: Math.log1p(-v.lin) };
|
|
371
584
|
}
|
|
372
585
|
function pntRaw(t, df, delta, lower, log) {
|
|
373
|
-
|
|
374
|
-
const a = df / 2;
|
|
375
|
-
if (Math.abs(t) <= farLimit(a, delta)) {
|
|
376
|
-
return integrate(lower ? problem(a, t, delta, "cdf") : problem(a, -t, -delta, "cdf"), false).lin;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
const v = pntBoth(t, df, delta, lower);
|
|
586
|
+
const v = pntBoth(t, df, delta, lower, log);
|
|
380
587
|
return log ? v.log : v.lin;
|
|
381
588
|
}
|
|
382
589
|
function dntRaw(x, df, delta, log) {
|
|
@@ -475,6 +682,11 @@ export function qnt1(p, df, ncp, lowerTail, logP) {
|
|
|
475
682
|
}
|
|
476
683
|
hi = T0;
|
|
477
684
|
}
|
|
685
|
+
else if (df < 1e-3 && cdfY(Number.MAX_VALUE, true, true) < logTarget) {
|
|
686
|
+
// The near tail grows only as 1 - c |t|^(-2a) (c about Φ(±δ)), so for a small a it can stay below the target up to
|
|
687
|
+
// the largest double (that needs 2a log(1.8e308) < log(2c), a < 5e-4): the quantile is beyond it
|
|
688
|
+
return s * Infinity;
|
|
689
|
+
}
|
|
478
690
|
const y = invertContinuous({
|
|
479
691
|
logTarget,
|
|
480
692
|
...(linearTarget !== undefined && { linearTarget }),
|
|
@@ -3,12 +3,46 @@
|
|
|
3
3
|
// Written for rStats.js (no ported code). For x >= b:
|
|
4
4
|
// f(x) = (a / b) (b / x)^(a+1), P[X > x] = (b / x)^a, q(p) = b (1 - p)^(-1/a)
|
|
5
5
|
// r = x / b is formed in double-double and the powers taken with pow, whose rounding does not grow with a (the low
|
|
6
|
-
// part of r enters to first order).
|
|
6
|
+
// part of r enters to first order). Where a power leaves the normal range, or a is so large that the low part of r no
|
|
7
|
+
// longer enters to first order, the powers are taken from logs instead.
|
|
7
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
9
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
9
|
-
import { log1mExp, standardizeDD } from "../internal/ddexp.js";
|
|
10
|
+
import { log1mExp, logRatioDD, standardizeDD } from "../internal/ddexp.js";
|
|
10
11
|
const valid = (a, b) => a > 0 && b > 0 && Number.isFinite(b);
|
|
11
12
|
// a = Inf is taken as the limit (the distribution concentrates at b), as extraDistr does
|
|
13
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
14
|
+
/**
|
|
15
|
+
* r = x / b (x > b) as [r, e, log r] with e the relative low part of r (r (1 + e) is x / b to double-double accuracy).
|
|
16
|
+
* When r overflows (or x is above 1e308, where the remainder in standardizeDD can overflow) r is Infinity,
|
|
17
|
+
* e is 0 and log r = log x - log b
|
|
18
|
+
*/
|
|
19
|
+
function ratio(x, b) {
|
|
20
|
+
if (x < 1e308) {
|
|
21
|
+
const [r, rl] = standardizeDD(x, 0, b);
|
|
22
|
+
if (r < Infinity) {
|
|
23
|
+
const e = rl / r;
|
|
24
|
+
return [r, e, Math.log(r) + e];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// log x - log b in double-double (the roundings of the two logs near 709 were 1e-12 of the tail at a = 25)
|
|
28
|
+
const lr = logRatioDD(x, b);
|
|
29
|
+
return [Infinity, 0, lr[0] + lr[1]];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* (x / b)^-a from ratio(x, b) = [r, e, lr]. pow is used where its result is a normal double and the low part of r
|
|
33
|
+
* enters to first order (|a e| small); where r overflows, x^-a b^a if both factors are normal; otherwise exp(-a lr)
|
|
34
|
+
* (a power below the normal range, or a so large that a e is not small)
|
|
35
|
+
*/
|
|
36
|
+
function powNeg(x, b, [r, e, lr], a) {
|
|
37
|
+
const ae = a * e;
|
|
38
|
+
if (Math.abs(ae) < 1e-8) {
|
|
39
|
+
const u = r < Infinity ? Math.pow(r, -a) : Math.pow(x, -a);
|
|
40
|
+
const v = r < Infinity ? 1 : Math.pow(b, a);
|
|
41
|
+
if (u >= MIN_NORMAL && v >= MIN_NORMAL && v < Infinity && u * v >= MIN_NORMAL)
|
|
42
|
+
return u * v * (1 - ae);
|
|
43
|
+
}
|
|
44
|
+
return Math.exp(-a * lr);
|
|
45
|
+
}
|
|
12
46
|
export function dpareto1(x, a, b, log) {
|
|
13
47
|
if (Number.isNaN(x) || !valid(a, b))
|
|
14
48
|
return NaN;
|
|
@@ -16,12 +50,29 @@ export function dpareto1(x, a, b, log) {
|
|
|
16
50
|
return d0(log);
|
|
17
51
|
if (a === Infinity)
|
|
18
52
|
return x === b ? Infinity : d0(log);
|
|
19
|
-
const [r,
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
53
|
+
const [r, e, lr] = ratio(x, b);
|
|
54
|
+
const ab = a / b;
|
|
55
|
+
const lab = ab >= MIN_NORMAL && ab < Infinity ? Math.log(ab) : Math.log(a) - Math.log(b);
|
|
56
|
+
const ld = lab - (a + 1) * lr;
|
|
57
|
+
// (a / b) r^-(a+1), unless a / b or the power leaves the normal range (or a e is not small): then in logs. Where
|
|
58
|
+
// r^-a is subnormal but a large a / b brings the density back, r^-a = h^2 with h = r^(-a/2) (exact halving)
|
|
59
|
+
const product = () => {
|
|
60
|
+
if (!(ab < Infinity && Math.abs(a * e) < 1e-8))
|
|
61
|
+
return 0;
|
|
62
|
+
const u = Math.pow(r, -a);
|
|
63
|
+
const h = u >= MIN_NORMAL ? 0 : Math.pow(r, -a / 2);
|
|
64
|
+
return u >= MIN_NORMAL ? (ab * u) / r : h >= MIN_NORMAL ? ((ab * h) / r) * h : 0;
|
|
65
|
+
};
|
|
66
|
+
if (log) {
|
|
67
|
+
// Where log(a / b) and (a + 1) log r (hundreds near the ends of the range) cancel to below half, their roundings
|
|
68
|
+
// would show: the log of the product instead (dpareto(1.1e-263, 1640, 7.8e-264, log) was 1.7e-13 off at -0.88)
|
|
69
|
+
if (!(Math.abs(ld) < 0.5 * Math.abs(lab)))
|
|
70
|
+
return ld;
|
|
71
|
+
const d = product();
|
|
72
|
+
return d >= MIN_NORMAL && d < Infinity ? Math.log(d) + Math.log1p(-(a + 1) * e) : ld;
|
|
73
|
+
}
|
|
74
|
+
const d = product();
|
|
75
|
+
return d >= MIN_NORMAL && d < Infinity ? d * (1 - (a + 1) * e) : Math.exp(ld);
|
|
25
76
|
}
|
|
26
77
|
export function ppareto1(q, a, b, lowerTail, logP) {
|
|
27
78
|
if (Number.isNaN(q) || !valid(a, b))
|
|
@@ -30,18 +81,13 @@ export function ppareto1(q, a, b, lowerTail, logP) {
|
|
|
30
81
|
return dt0(lowerTail, logP);
|
|
31
82
|
if (q === Infinity || a === Infinity)
|
|
32
83
|
return dt1(lowerTail, logP);
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (logP)
|
|
38
|
-
return lu;
|
|
39
|
-
const u = Math.pow(r, -a);
|
|
40
|
-
return u > 0 ? u * (1 - a * e) : Math.exp(lu);
|
|
41
|
-
}
|
|
84
|
+
const rt = ratio(q, b);
|
|
85
|
+
const lu = -a * rt[2]; // log P[X > q]
|
|
86
|
+
if (!lowerTail)
|
|
87
|
+
return logP ? lu : powNeg(q, b, rt, a);
|
|
42
88
|
if (!logP)
|
|
43
89
|
return -Math.expm1(lu);
|
|
44
|
-
return lu < -Math.LN2 ? Math.log1p(-
|
|
90
|
+
return lu < -Math.LN2 ? Math.log1p(-powNeg(q, b, rt, a)) : log1mExp(lu);
|
|
45
91
|
}
|
|
46
92
|
export function qpareto1(p, a, b, lowerTail, logP) {
|
|
47
93
|
if (Number.isNaN(p) || !valid(a, b))
|
|
@@ -52,7 +98,10 @@ export function qpareto1(p, a, b, lowerTail, logP) {
|
|
|
52
98
|
if (a === Infinity)
|
|
53
99
|
return b;
|
|
54
100
|
const lu = lowerTail ? (logP ? log1mExp(p) : Math.log1p(-p)) : logP ? p : Math.log(p);
|
|
55
|
-
|
|
101
|
+
// b exp(-lu / a); where the exponential overflows but b is small the product may not: then in logs
|
|
102
|
+
const t = -lu / a;
|
|
103
|
+
const v = Math.exp(t);
|
|
104
|
+
return v < Infinity ? b * v : Math.exp(Math.log(b) + t);
|
|
56
105
|
}
|
|
57
106
|
/**
|
|
58
107
|
* Density of the Pareto distribution. Equivalent to `extraDistr::dpareto(x, a = 1, b = 1, log = FALSE)` in R.
|