@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
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
// = dpois(shape; y) * shape / x (shape < 1)
|
|
6
6
|
// The distribution function uses the incomplete gamma function (src/special/igam.ts); quantiles use Newton's
|
|
7
7
|
// method (src/internal/invert.ts) from the DiDonato & Morris starting value.
|
|
8
|
+
// Where y = x/scale is below the smallest normal double (subnormal, or 0 although x > 0), the leading terms
|
|
9
|
+
// f(x) = y^shape / (Gamma(shape) x), P[X <= x] = y^shape / Gamma(shape + 1)
|
|
10
|
+
// are exact to double precision (the next terms are O(y) relative), and are taken in logs with log y = log x - log scale.
|
|
8
11
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
12
|
import { d0, dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
10
13
|
import { invertContinuous } from "../internal/invert.js";
|
|
@@ -12,6 +15,8 @@ import { gammaInc, gammaIncBoth } from "../special/igam.js";
|
|
|
12
15
|
import { findInverseGamma } from "../special/igami.js";
|
|
13
16
|
import { lgamma, lgamma1p } from "../special/gamma.js";
|
|
14
17
|
import { dpoisRaw } from "../special/loader.js";
|
|
18
|
+
import { ddAdd, ddDivNum, ddLog, ddMulNum, ddSub } from "../special/dd.js";
|
|
19
|
+
import { ddExp, log1mExp, logRatioDD } from "../internal/ddexp.js";
|
|
15
20
|
import { qnorm } from "./normal.js";
|
|
16
21
|
/** Determine scale from rate / scale (as in R, giving both is an error unless scale = 1/rate; also used by rgamma) */
|
|
17
22
|
export function gammaScale(params) {
|
|
@@ -28,12 +33,38 @@ export function gammaScale(params) {
|
|
|
28
33
|
return 1 / rate;
|
|
29
34
|
return 1;
|
|
30
35
|
}
|
|
36
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
37
|
+
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
38
|
+
/** c t in double-double; only the double product where c is beyond the range of Veltkamp splitting */
|
|
39
|
+
const mulBig = (t, c) => (c < 1e300 ? ddMulNum(t, c) : [t[0] * c, 0]);
|
|
40
|
+
/** exp of a double-double; 0 below the subnormal range (where the low part could carry -0 or NaN into ddExp's result) */
|
|
41
|
+
const expOf = (l) => (l[0] < -746 ? 0 : ddExp(l)[0]);
|
|
42
|
+
/**
|
|
43
|
+
* log P[X <= x] where y = x / scale is below the smallest normal double (x > 0, finite scale): shape log y -
|
|
44
|
+
* lgamma(shape + 1), with log y from log x - log scale (y itself has lost digits in the division, or is 0)
|
|
45
|
+
*/
|
|
46
|
+
function logLowerTiny(x, shape, scale) {
|
|
47
|
+
return ddAdd(mulBig(logRatioDD(x, scale), shape), [-lgamma1p(shape), 0]);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether the log density l = lp + (log of a divisor) has cancelled to below half of |lp|: the roundings of the two
|
|
51
|
+
* logs (eps times hundreds near the ends of the range) are then large against l (dgamma(1.2e-289, 40.4,
|
|
52
|
+
* scale 1.5e-292, log) was 1e-13 off at -0.38)
|
|
53
|
+
*/
|
|
54
|
+
const cancels = (l, lp) => Math.abs(l) < 0.5 * Math.abs(lp);
|
|
55
|
+
/** log(f / d) from the linear factor f where f and the quotient are normal (relative accuracy kept), else l */
|
|
56
|
+
function logOfLinear(f, d, l) {
|
|
57
|
+
if (!(f >= MIN_NORMAL && f < Infinity))
|
|
58
|
+
return l;
|
|
59
|
+
const v = f / d;
|
|
60
|
+
return v >= MIN_NORMAL && v < Infinity ? Math.log(v) : l;
|
|
61
|
+
}
|
|
31
62
|
/** Log density of the standardized (scale = 1) gamma distribution. x > 0, shape > 0 */
|
|
32
63
|
export function dgammaStdLog(x, shape) {
|
|
33
64
|
if (shape < 1) {
|
|
34
65
|
const pr = dpoisRaw(shape, x, true);
|
|
35
66
|
const r = shape / x;
|
|
36
|
-
return pr + (
|
|
67
|
+
return pr + (r >= MIN_NORMAL && r < Infinity ? Math.log(r) : Math.log(shape) - Math.log(x));
|
|
37
68
|
}
|
|
38
69
|
return dpoisRaw(shape - 1, x, true);
|
|
39
70
|
}
|
|
@@ -53,16 +84,32 @@ export function dgamma1(x, shape, scale, log) {
|
|
|
53
84
|
return d0(log);
|
|
54
85
|
return log ? -Math.log(scale) : 1 / scale;
|
|
55
86
|
}
|
|
87
|
+
if (x / scale < MIN_NORMAL && scale < Infinity && shape < Infinity) {
|
|
88
|
+
// log f = shape log y + log(shape) - lgamma(shape + 1) - log x (e^-y is 1)
|
|
89
|
+
const l = ddAdd(ddSub(logLowerTiny(x, shape, scale), ddLog([x, 0])), ddLog([shape, 0]));
|
|
90
|
+
return log ? l[0] + l[1] : expOf(l);
|
|
91
|
+
}
|
|
56
92
|
if (shape < 1) {
|
|
57
93
|
const pr = dpoisRaw(shape, x / scale, log);
|
|
58
94
|
if (log) {
|
|
95
|
+
// log(shape / x), from the logs where the quotient over- or underflows or is subnormal (it has lost digits:
|
|
96
|
+
// dgamma(8.8e214, 7.4e-105, rate 6.3e-268, log) was off by 4e-6)
|
|
59
97
|
const r = shape / x;
|
|
60
|
-
|
|
98
|
+
const l = pr + (r >= MIN_NORMAL && r < Infinity ? Math.log(r) : Math.log(shape) - Math.log(x));
|
|
99
|
+
return cancels(l, pr) ? logOfLinear(dpoisRaw(shape, x / scale, false) * shape, x, l) : l;
|
|
61
100
|
}
|
|
62
|
-
|
|
101
|
+
const ps = pr * shape;
|
|
102
|
+
return ps >= MIN_NORMAL || x >= 1 ? ps / x : Math.exp(dgamma1(x, shape, scale, true));
|
|
63
103
|
}
|
|
64
104
|
const pr = dpoisRaw(shape - 1, x / scale, log);
|
|
65
|
-
|
|
105
|
+
if (log) {
|
|
106
|
+
const l = pr - Math.log(scale);
|
|
107
|
+
return cancels(l, pr) ? logOfLinear(dpoisRaw(shape - 1, x / scale, false), scale, l) : l;
|
|
108
|
+
}
|
|
109
|
+
// Where the Poisson factor (or its product with shape above) is below the normal range, a small scale or x can
|
|
110
|
+
// bring the density back into it with the factor's lost digits (dgamma(6.8e-291, 6.37, scale 1.6e-231) was 0.4%
|
|
111
|
+
// off): exp of the log density instead
|
|
112
|
+
return pr >= MIN_NORMAL || scale >= 1 ? pr / scale : Math.exp(dgamma1(x, shape, scale, true));
|
|
66
113
|
}
|
|
67
114
|
export function pgamma1(q, shape, scale, lowerTail, logP) {
|
|
68
115
|
if (Number.isNaN(q) || Number.isNaN(shape) || Number.isNaN(scale))
|
|
@@ -78,17 +125,48 @@ export function pgamma1(q, shape, scale, lowerTail, logP) {
|
|
|
78
125
|
// returned for every finite x (docs/differences-from-r.md)
|
|
79
126
|
if (shape === Infinity)
|
|
80
127
|
return x === Infinity ? dt1(lowerTail, logP) : dt0(lowerTail, logP);
|
|
128
|
+
if (q <= 0)
|
|
129
|
+
return dt0(lowerTail, logP);
|
|
130
|
+
if (x < MIN_NORMAL && scale < Infinity) {
|
|
131
|
+
const l = logLowerTiny(q, shape, scale);
|
|
132
|
+
const lp = l[0] + l[1];
|
|
133
|
+
if (lowerTail)
|
|
134
|
+
return logP ? lp : expOf(l);
|
|
135
|
+
// 1 - P with P from the double-double log where P < 1/2 (exp of the rounded log loses eps |log P| relative)
|
|
136
|
+
if (lp > -Math.LN2)
|
|
137
|
+
return logP ? Math.log(-Math.expm1(lp)) : -Math.expm1(lp);
|
|
138
|
+
const e = expOf(l);
|
|
139
|
+
return logP ? Math.log1p(-e) : 1 - e;
|
|
140
|
+
}
|
|
81
141
|
if (x <= 0)
|
|
82
142
|
return dt0(lowerTail, logP);
|
|
83
143
|
return gammaInc(shape, x, lowerTail, logP);
|
|
84
144
|
}
|
|
85
|
-
/**
|
|
86
|
-
|
|
145
|
+
/**
|
|
146
|
+
* log of the standardized gamma quantile when it is below the smallest normal double (else undefined): there
|
|
147
|
+
* P(shape, x) = x^shape / Gamma(shape + 1) to double precision, so log x = (log P + lgamma(shape + 1)) / shape.
|
|
148
|
+
* lower, logTarget and linearTarget as from smallerTail. log P from the linear target in double-double where there is
|
|
149
|
+
* one (log P rounded would lose eps |log P| / shape relative in x)
|
|
150
|
+
*/
|
|
151
|
+
export function logTinyGammaQuantile(logTarget, lower, shape, linearTarget) {
|
|
152
|
+
const lp = lower ? logTarget : linearTarget !== undefined ? Math.log1p(-linearTarget) : log1mExp(logTarget);
|
|
153
|
+
const lg = lgamma1p(shape);
|
|
154
|
+
if (!(lp < shape * LOG_MIN_NORMAL - lg))
|
|
155
|
+
return undefined;
|
|
156
|
+
const s = ddAdd(lower && linearTarget !== undefined ? ddLog([linearTarget, 0]) : [lp, 0], [lg, 0]);
|
|
157
|
+
// (in double where the shape or the quotient is beyond the range of Veltkamp splitting; exp of it is 0 or 1 then)
|
|
158
|
+
const q = s[0] / shape;
|
|
159
|
+
return shape < 1e300 && Math.abs(q) < 1e300 ? ddDivNum(s, shape) : [q, 0];
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Quantile of the standardized gamma distribution times scale (default 1). 0 < p < 1 (range checks are the caller's
|
|
163
|
+
* job). Where the standardized quantile is below the smallest normal double the scaling is done in logs
|
|
164
|
+
*/
|
|
165
|
+
export function qgammaStd(p, shape, lowerTail, logP, scale = 1) {
|
|
87
166
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return 0;
|
|
167
|
+
const tiny = logTinyGammaQuantile(logTarget, lower, shape, linearTarget);
|
|
168
|
+
if (tiny !== undefined)
|
|
169
|
+
return expOf(scale === 1 ? tiny : ddAdd(tiny, ddLog([scale, 0])));
|
|
92
170
|
let x0;
|
|
93
171
|
if (shape > 1 && logTarget <= -700) {
|
|
94
172
|
// Deep tail: Wilson-Hilferty approximation x = a (1 - 1/(9a) + z / sqrt(9a))^3
|
|
@@ -111,7 +189,7 @@ export function qgammaStd(p, shape, lowerTail, logP) {
|
|
|
111
189
|
for (let i = 0; i < 3; i++)
|
|
112
190
|
x0 = -logTarget - lgamma(shape) + (shape - 1) * Math.log(x0);
|
|
113
191
|
}
|
|
114
|
-
|
|
192
|
+
const x = invertContinuous({
|
|
115
193
|
logTarget,
|
|
116
194
|
...(linearTarget !== undefined && { linearTarget }),
|
|
117
195
|
lower,
|
|
@@ -125,6 +203,7 @@ export function qgammaStd(p, shape, lowerTail, logP) {
|
|
|
125
203
|
hi: Infinity,
|
|
126
204
|
positive: true,
|
|
127
205
|
});
|
|
206
|
+
return x * scale;
|
|
128
207
|
}
|
|
129
208
|
export function qgamma1(p, shape, scale, lowerTail, logP) {
|
|
130
209
|
if (Number.isNaN(p) || Number.isNaN(shape) || Number.isNaN(scale))
|
|
@@ -139,7 +218,7 @@ export function qgamma1(p, shape, scale, lowerTail, logP) {
|
|
|
139
218
|
// Shape Inf: all the mass at +Inf, so every probability strictly between 0 and 1 maps to Inf
|
|
140
219
|
if (shape === Infinity)
|
|
141
220
|
return Infinity;
|
|
142
|
-
return qgammaStd(p, shape, lowerTail, logP
|
|
221
|
+
return qgammaStd(p, shape, lowerTail, logP, scale);
|
|
143
222
|
}
|
|
144
223
|
/**
|
|
145
224
|
* Density of the gamma distribution. Equivalent to R's `dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE)`.
|
|
@@ -9,13 +9,26 @@ import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
|
9
9
|
import { log1mExp } from "../internal/ddexp.js";
|
|
10
10
|
import { nonInt } from "../internal/integer.js";
|
|
11
11
|
import { dbinomRawDD } from "../special/loader.js";
|
|
12
|
-
import { twoSum } from "../special/dd.js";
|
|
12
|
+
import { ddLog, ddMul, ddMulNum, twoSum } from "../special/dd.js";
|
|
13
13
|
/** p and q = 1 - p in double-double (the smaller one is the exact base) */
|
|
14
14
|
export function probPair(prob) {
|
|
15
15
|
return prob <= 0.5
|
|
16
16
|
? { pd: [prob, 0], qd: twoSum(1, -prob) }
|
|
17
17
|
: { pd: twoSum(1, -(1 - prob)), qd: [1 - prob, 0] };
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* q^n (its log if log) for an integer n >= 0 by Loader's form, except from n = 1e290 on: there its double-double
|
|
21
|
+
* products overflow in the splitting of n (pgeom(8.7e299, 1.1e-300) was NaN, 0.616 expected), and n log q is formed in
|
|
22
|
+
* double-double with n scaled down by 2^64 and log q up by 2^64 (both exact)
|
|
23
|
+
*/
|
|
24
|
+
function qPow(n, pd, qd, log) {
|
|
25
|
+
if (n < 1e290)
|
|
26
|
+
return dbinomRawDD(0, n, pd, qd, log);
|
|
27
|
+
const l = ddMul([n / 2 ** 64, 0], ddMulNum(ddLog(qd), 2 ** 64));
|
|
28
|
+
if (!(l[0] > -Infinity))
|
|
29
|
+
return log ? -Infinity : 0;
|
|
30
|
+
return log ? l[0] + l[1] : Math.exp(l[0]) * (1 + l[1]);
|
|
31
|
+
}
|
|
19
32
|
function badProb(prob) {
|
|
20
33
|
return !(prob > 0) || prob > 1;
|
|
21
34
|
}
|
|
@@ -29,13 +42,13 @@ export function dgeom1(x, prob, log) {
|
|
|
29
42
|
const n = Math.round(x);
|
|
30
43
|
const { pd, qd } = probPair(prob);
|
|
31
44
|
// q^n
|
|
32
|
-
const qn =
|
|
45
|
+
const qn = qPow(n, pd, qd, log);
|
|
33
46
|
return log ? Math.log(prob) + qn : prob * qn;
|
|
34
47
|
}
|
|
35
48
|
/** P[X > x] = q^(x+1) (x a nonnegative integer) */
|
|
36
49
|
function upper(x, prob, log) {
|
|
37
50
|
const { pd, qd } = probPair(prob);
|
|
38
|
-
return
|
|
51
|
+
return qPow(x + 1, pd, qd, log);
|
|
39
52
|
}
|
|
40
53
|
function pgeomRaw(x, prob, lowerTail, logP) {
|
|
41
54
|
if (!lowerTail)
|
|
@@ -3,13 +3,21 @@
|
|
|
3
3
|
// Written for rStats.js (no ported code). X = 1/Y with Y ~ Gamma(alpha, rate = beta), so with z = beta / x
|
|
4
4
|
// f(x) = f_Gamma(z; alpha, 1) z / x, P[X <= x] = P[Z >= z], q(p) = beta / q_Gamma(1 - p; alpha, 1)
|
|
5
5
|
// which keeps the gamma functions' accuracy in both tails. z is formed in double-double and its low part enters to
|
|
6
|
-
// first order (the density and the tails are steep in z when alpha is large).
|
|
6
|
+
// first order (the density and the tails are steep in z when alpha is large). Above alpha = 1e15 it is left out: near
|
|
7
|
+
// the centre the low part (up to eps z / 2, z about alpha) is no longer small against the width sqrt(alpha) over which
|
|
8
|
+
// the tails change, so a first-order term is not valid (it gave probabilities of 1e16); the functions are then those
|
|
9
|
+
// at the rounded z (relative errors of order eps sqrt(alpha), the condition of the problem itself).
|
|
7
10
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
|
-
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
9
|
-
import { standardizeDD } from "../internal/ddexp.js";
|
|
10
|
-
import {
|
|
11
|
+
import { d0, dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
12
|
+
import { ddExp, logRatioDD, standardizeDD } from "../internal/ddexp.js";
|
|
13
|
+
import { ddAdd, ddLog, ddMulNum, ddSub } from "../special/dd.js";
|
|
14
|
+
import { lgamma } from "../special/gamma.js";
|
|
15
|
+
import { dgamma1, logTinyGammaQuantile, pgamma1, qgamma1 } from "./gamma.js";
|
|
11
16
|
const valid = (alpha, beta) => alpha > 0 && beta > 0 && Number.isFinite(beta);
|
|
12
17
|
// alpha = Inf is taken as the limit (the distribution concentrates at 0), as extraDistr does
|
|
18
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
19
|
+
/** Largest alpha for which the low part of z enters to first order (see the top of the file) */
|
|
20
|
+
const MAX_ALPHA_CORRECTED = 1e15;
|
|
13
21
|
export function dinvgamma1(x, alpha, beta, log) {
|
|
14
22
|
if (Number.isNaN(x) || !valid(alpha, beta))
|
|
15
23
|
return NaN;
|
|
@@ -20,11 +28,36 @@ export function dinvgamma1(x, alpha, beta, log) {
|
|
|
20
28
|
const [z, zl] = standardizeDD(beta, 0, x);
|
|
21
29
|
if (z === Infinity)
|
|
22
30
|
return d0(log);
|
|
31
|
+
if (z < MIN_NORMAL) {
|
|
32
|
+
// beta / x subnormal or 0: in logs, log f = alpha log(beta / x) - lgamma(alpha) - log x (e^-z is 1), with
|
|
33
|
+
// log(beta / x) from log beta - log x
|
|
34
|
+
const lr = logRatioDD(beta, x);
|
|
35
|
+
const l = ddAdd(ddSub(alpha < 1e300 ? ddMulNum(lr, alpha) : [lr[0] * alpha, 0], ddLog([x, 0])), [-lgamma(alpha), 0]);
|
|
36
|
+
return log ? l[0] + l[1] : l[0] < -746 ? 0 : ddExp(l)[0];
|
|
37
|
+
}
|
|
23
38
|
// d/dz log(f_Gamma(z) z) = alpha / z - 1
|
|
24
|
-
const c = Number.isFinite(zl) ?
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
39
|
+
const c = Number.isFinite(zl) && alpha <= MAX_ALPHA_CORRECTED ? alpha * (zl / z) - zl : 0;
|
|
40
|
+
// f_Gamma(z) z / x from normal factors (undefined where f_Gamma(z) or f_Gamma(z) z is below the normal range, where z
|
|
41
|
+
// or 1 / x can bring the density back)
|
|
42
|
+
const linear = () => {
|
|
43
|
+
const g = dgamma1(z, alpha, 1, false);
|
|
44
|
+
const dz = g * z;
|
|
45
|
+
return g >= MIN_NORMAL && dz >= MIN_NORMAL ? dz / x : undefined;
|
|
46
|
+
};
|
|
47
|
+
if (log) {
|
|
48
|
+
const lg = dgamma1(z, alpha, 1, true);
|
|
49
|
+
const ld = lg + Math.log(z) - Math.log(x) + c;
|
|
50
|
+
if (!(Math.abs(ld) < 0.5 * Math.abs(lg)))
|
|
51
|
+
return ld;
|
|
52
|
+
// log f_Gamma and log(z / x) (hundreds near the ends of the range) cancelled to below half, so their roundings
|
|
53
|
+
// would show: the log of the linear value instead (dinvgamma(9.7e-286, 91.7, beta 2.5e-287, log) was 1.2e-13 off)
|
|
54
|
+
const v = linear();
|
|
55
|
+
return v !== undefined && v >= MIN_NORMAL && v < Infinity ? Math.log(v) + c : ld;
|
|
56
|
+
}
|
|
57
|
+
const v = linear();
|
|
58
|
+
if (v === undefined)
|
|
59
|
+
return Math.exp(dgamma1(z, alpha, 1, true) + Math.log(z) - Math.log(x) + c);
|
|
60
|
+
return v * (1 + c);
|
|
28
61
|
}
|
|
29
62
|
export function pinvgamma1(q, alpha, beta, lowerTail, logP) {
|
|
30
63
|
if (Number.isNaN(q) || !valid(alpha, beta))
|
|
@@ -34,14 +67,22 @@ export function pinvgamma1(q, alpha, beta, lowerTail, logP) {
|
|
|
34
67
|
if (q === Infinity || alpha === Infinity)
|
|
35
68
|
return dt1(lowerTail, logP);
|
|
36
69
|
const [z, zl] = standardizeDD(beta, 0, q);
|
|
70
|
+
if (z < MIN_NORMAL) {
|
|
71
|
+
// beta / x subnormal or 0: P[Z >= z] is the upper tail at beta of the gamma distribution with scale x, which
|
|
72
|
+
// pgamma takes in logs there
|
|
73
|
+
return pgamma1(beta, alpha, q, !lowerTail, logP);
|
|
74
|
+
}
|
|
37
75
|
const v = pgamma1(z, alpha, 1, !lowerTail, logP);
|
|
38
|
-
if (!(zl !== 0 && Number.isFinite(zl) && Number.isFinite(z)))
|
|
76
|
+
if (!(zl !== 0 && Number.isFinite(zl) && Number.isFinite(z) && alpha <= MAX_ALPHA_CORRECTED))
|
|
39
77
|
return v;
|
|
40
78
|
// P[Z <= z] grows by f(z) zl, P[Z > z] falls by it
|
|
41
79
|
const sign = lowerTail ? -1 : 1;
|
|
42
80
|
if (!logP)
|
|
43
81
|
return v + sign * dgamma1(z, alpha, 1, false) * zl;
|
|
44
|
-
// log P + f(z) zl / P, with f / P from the logs (P may underflow)
|
|
82
|
+
// log P + f(z) zl / P, with f / P from the logs (P may underflow). Beyond |log P| = 1e8 the difference of the logs
|
|
83
|
+
// has lost its digits (eps |log P| absolute) and the correction is below the rounding of log P anyway
|
|
84
|
+
if (!(Math.abs(v) < 1e8))
|
|
85
|
+
return v;
|
|
45
86
|
const c = sign * Math.exp(dgamma1(z, alpha, 1, true) - v) * zl;
|
|
46
87
|
return Number.isFinite(c) ? v + c : v;
|
|
47
88
|
}
|
|
@@ -53,6 +94,11 @@ export function qinvgamma1(p, alpha, beta, lowerTail, logP) {
|
|
|
53
94
|
return b;
|
|
54
95
|
if (alpha === Infinity)
|
|
55
96
|
return 0;
|
|
97
|
+
// A gamma quantile below the smallest normal double has lost digits (or is 0): beta / it from the log
|
|
98
|
+
const { lower, logTarget, linearTarget } = smallerTail(p, !lowerTail, logP);
|
|
99
|
+
const tiny = logTinyGammaQuantile(logTarget, lower, alpha, linearTarget);
|
|
100
|
+
if (tiny !== undefined)
|
|
101
|
+
return Math.log(beta) - tiny[0] > 710 ? Infinity : ddExp(ddSub(ddLog([beta, 0]), tiny))[0];
|
|
56
102
|
return beta / qgamma1(p, alpha, 1, !lowerTail, logP);
|
|
57
103
|
}
|
|
58
104
|
/**
|
|
@@ -5,25 +5,79 @@
|
|
|
5
5
|
// 1 - x^a is formed as -expm1(a log x) when x^a is near 1, and the tails as exp / -expm1 of b log(1 - x^a).
|
|
6
6
|
import { vectorize } from "../internal/vectorize.js";
|
|
7
7
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
8
|
-
import { log1mExp } from "../internal/ddexp.js";
|
|
8
|
+
import { ddExp, log1mExp } from "../internal/ddexp.js";
|
|
9
|
+
import { ddAdd, ddLog, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
|
|
9
10
|
const valid = (a, b) => a > 0 && b > 0 && Number.isFinite(b);
|
|
10
11
|
// a = Inf is taken as the limit (the distribution concentrates at 1), as extraDistr does
|
|
12
|
+
const MIN_NORMAL = 2.2250738585072014e-308;
|
|
13
|
+
/**
|
|
14
|
+
* c log(1 - x^a) for 0 < x < 1 where x^a is subnormal: -c x^a (to double precision) from logs, since x^a itself has
|
|
15
|
+
* lost digits
|
|
16
|
+
*/
|
|
17
|
+
const scaledTinyPow = (x, a, c) => -Math.sign(c) * Math.exp(Math.log(Math.abs(c)) + a * Math.log(x));
|
|
18
|
+
/**
|
|
19
|
+
* Whether a log x (x near 1, a tiny) is subnormal or 0 although 1 - x^a > 0: -expm1 of it has lost digits
|
|
20
|
+
* (dkumar(1 - 1.1e-16, 2.6e-300, 0.29) was 6e-8 off). 1 - x^a = -a log x to double precision there, taken from the
|
|
21
|
+
* factors
|
|
22
|
+
*/
|
|
23
|
+
const subnormalALogX = (x, a) => x < 1 && -(a * Math.log(x)) < MIN_NORMAL;
|
|
11
24
|
/** log(1 - x^a) for 0 <= x <= 1 */
|
|
12
25
|
function log1mPow(x, a) {
|
|
13
26
|
const xa = Math.pow(x, a);
|
|
14
|
-
|
|
27
|
+
if (xa < 0.5)
|
|
28
|
+
return Math.log1p(-xa);
|
|
29
|
+
return subnormalALogX(x, a) ? Math.log(a) + Math.log(-Math.log(x)) : Math.log(-Math.expm1(a * Math.log(x)));
|
|
15
30
|
}
|
|
16
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* (1 - x^a)^e for 0 < x < 1: pow of 1 - x^a with the rounding of the subtraction corrected to first order. Where the
|
|
33
|
+
* correction is not small (e above about 1e8 magnifies the rounding of 1 - x^a), exp(e log1p(-x^a)) instead
|
|
34
|
+
*/
|
|
17
35
|
function powOneMinus(x, a, e) {
|
|
18
36
|
const xa = Math.pow(x, a);
|
|
19
|
-
if (xa >= 0.5)
|
|
20
|
-
|
|
37
|
+
if (xa >= 0.5) {
|
|
38
|
+
if (!subnormalALogX(x, a))
|
|
39
|
+
return Math.pow(-Math.expm1(a * Math.log(x)), e);
|
|
40
|
+
// (-a log x)^e = ((a 2^600) (-log x))^e 2^(-600 e), the base made normal by an exact scaling; from the logs where a
|
|
41
|
+
// factor leaves the range
|
|
42
|
+
const v = Math.pow(a * 2 ** 600 * -Math.log(x), e) * Math.pow(2, -600 * e);
|
|
43
|
+
return v >= MIN_NORMAL && v < Infinity ? v : Math.exp(e * (Math.log(a) + Math.log(-Math.log(x))));
|
|
44
|
+
}
|
|
21
45
|
const hi = 1 - xa;
|
|
22
46
|
const lo = 1 - hi - xa; // exact remainder (Fast2Sum)
|
|
23
|
-
|
|
47
|
+
const c = (e * lo) / hi;
|
|
48
|
+
return Math.abs(c) < 1e-8 ? Math.pow(hi, e) * (1 + c) : Math.exp(e * Math.log1p(-xa));
|
|
24
49
|
}
|
|
25
50
|
/** c log(v), 0 when c is 0 */
|
|
26
51
|
const xlog = (c, v) => (c === 0 ? 0 : c * Math.log(v));
|
|
52
|
+
/**
|
|
53
|
+
* log a + log b + (a - 1) log x + (b - 1) log(1 - x^a), given lb = the last term in double. The terms can be hundreds
|
|
54
|
+
* that cancel to a log density near 0, where their roundings would show (1.6e-13 at dkumar(0.305, 486, 3.8e247)), so
|
|
55
|
+
* they are summed in double-double, with a - 1 and b - 1 exact and log(1 - x^a) from x^a = exp(a log x) in
|
|
56
|
+
* double-double where x^a > 1e-10 (below, log1p(-x^a) in double is accurate to its last bit). lb is used as is for
|
|
57
|
+
* x = 0 or 1, a subnormal x^a, and shapes beyond the range of Veltkamp splitting
|
|
58
|
+
*/
|
|
59
|
+
function logDensity(x, a, b, lb, xa) {
|
|
60
|
+
if (x === 0 || !(a < 1e300) || !(b < 1e300))
|
|
61
|
+
return Math.log(a) + Math.log(b) + xlog(a - 1, x) + lb;
|
|
62
|
+
const lx = ddLog([x, 0]);
|
|
63
|
+
let s = ddAdd(ddLog([a, 0]), ddLog([b, 0]));
|
|
64
|
+
if (a !== 1)
|
|
65
|
+
s = ddAdd(s, ddMul(lx, twoSum(a, -1)));
|
|
66
|
+
if (b === 1 || x === 1 || xa < MIN_NORMAL) {
|
|
67
|
+
if (!Number.isFinite(lb))
|
|
68
|
+
return s[0] + lb;
|
|
69
|
+
const t = ddAdd(s, [lb, 0]);
|
|
70
|
+
return t[0] + t[1];
|
|
71
|
+
}
|
|
72
|
+
// (log a + log(-log x) where a log x is subnormal, see subnormalALogX)
|
|
73
|
+
const l1 = xa <= 1e-10
|
|
74
|
+
? [Math.log1p(-xa), 0]
|
|
75
|
+
: subnormalALogX(x, a)
|
|
76
|
+
? ddAdd(ddLog([a, 0]), ddLog([-lx[0], -lx[1]]))
|
|
77
|
+
: ddLog(ddSub([1, 0], ddExp(ddMul(lx, [a, 0]))));
|
|
78
|
+
const t = ddAdd(s, ddMul(l1, twoSum(b, -1)));
|
|
79
|
+
return t[0] + t[1];
|
|
80
|
+
}
|
|
27
81
|
export function dkumar1(x, a, b, log) {
|
|
28
82
|
if (Number.isNaN(x) || !valid(a, b))
|
|
29
83
|
return NaN;
|
|
@@ -31,15 +85,27 @@ export function dkumar1(x, a, b, log) {
|
|
|
31
85
|
return d0(log);
|
|
32
86
|
if (a === Infinity)
|
|
33
87
|
return x === 1 ? Infinity : d0(log);
|
|
88
|
+
const xa = Math.pow(x, a);
|
|
89
|
+
const tiny = x > 0 && xa < MIN_NORMAL;
|
|
34
90
|
const l1 = x === 1 ? -Infinity : log1mPow(x, a);
|
|
35
|
-
|
|
91
|
+
// (b - 1) log(1 - x^a)
|
|
92
|
+
const lb = b === 1 ? 0 : tiny ? scaledTinyPow(x, a, b - 1) : (b - 1) * l1;
|
|
93
|
+
const ld = logDensity(x, a, b, lb, xa);
|
|
36
94
|
if (log)
|
|
37
95
|
return ld;
|
|
38
96
|
// the product keeps the relative accuracy that exp(ld) loses for large |ld|; exp(ld) where it under- or overflows
|
|
97
|
+
// or where x^(a-1) is subnormal (a large b can bring the density back into the normal range)
|
|
39
98
|
// x^(a-1) as x^a / x (a - 1 itself may round, which x^(a-1) magnifies by |log x|)
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
99
|
+
const xm = a === 1 ? 1 : xa > 1e-290 ? xa / x : Math.pow(x, a - 1);
|
|
100
|
+
if (x > 0 && xm < MIN_NORMAL)
|
|
101
|
+
return Math.exp(ld);
|
|
102
|
+
// (also where a factor or partial product is below the normal range: a b = 3.3e-315 for tiny shapes, which xm and
|
|
103
|
+
// the power brought back to 1e99 with 6e-10 relative error; the power (1 - x^a)^(b-1) subnormal)
|
|
104
|
+
const ab = a * b;
|
|
105
|
+
const abx = ab * xm;
|
|
106
|
+
const pw = b === 1 || x === 0 ? 1 : tiny ? Math.exp(lb) : powOneMinus(x, a, b - 1);
|
|
107
|
+
const d = abx * pw;
|
|
108
|
+
return ab >= MIN_NORMAL && abx >= MIN_NORMAL && pw >= MIN_NORMAL && d > MIN_NORMAL && d < Infinity ? d : Math.exp(ld);
|
|
43
109
|
}
|
|
44
110
|
export function pkumar1(q, a, b, lowerTail, logP) {
|
|
45
111
|
if (Number.isNaN(q) || !valid(a, b))
|
|
@@ -50,16 +116,18 @@ export function pkumar1(q, a, b, lowerTail, logP) {
|
|
|
50
116
|
return dt1(lowerTail, logP);
|
|
51
117
|
if (a === Infinity)
|
|
52
118
|
return dt0(lowerTail, logP);
|
|
53
|
-
const
|
|
119
|
+
const xa = Math.pow(q, a);
|
|
120
|
+
const tiny = xa < MIN_NORMAL;
|
|
121
|
+
const lu = tiny ? scaledTinyPow(q, a, b) : b * log1mPow(q, a); // log P[X > q]
|
|
54
122
|
if (!lowerTail)
|
|
55
|
-
return logP ? lu : powOneMinus(q, a, b);
|
|
123
|
+
return logP ? lu : tiny ? Math.exp(lu) : powOneMinus(q, a, b);
|
|
56
124
|
if (!logP)
|
|
57
125
|
return -Math.expm1(lu);
|
|
58
|
-
// for
|
|
59
|
-
|
|
60
|
-
if (xa < 1e-
|
|
126
|
+
// for small x^a and b x^a, P[X <= q] = b x^a (1 - (b - 1) x^a / 2 + O(max(1, b)^2 x^2a)) whose log survives where
|
|
127
|
+
// x^a or 1 - exp(lu) underflows; elsewhere the forms below are accurate
|
|
128
|
+
if (Math.max(b, 1) * xa < 1e-10)
|
|
61
129
|
return Math.log(b) + a * Math.log(q) + Math.log1p((-(b - 1) * xa) / 2);
|
|
62
|
-
return lu < -Math.LN2 ? Math.log1p(-powOneMinus(q, a, b)) : log1mExp(lu);
|
|
130
|
+
return lu < -Math.LN2 && !tiny ? Math.log1p(-powOneMinus(q, a, b)) : log1mExp(lu);
|
|
63
131
|
}
|
|
64
132
|
export function qkumar1(p, a, b, lowerTail, logP) {
|
|
65
133
|
if (Number.isNaN(p) || !valid(a, b))
|
|
@@ -71,7 +139,20 @@ export function qkumar1(p, a, b, lowerTail, logP) {
|
|
|
71
139
|
return 1;
|
|
72
140
|
// log of the upper probability
|
|
73
141
|
const lu = lowerTail ? (logP ? log1mExp(p) : Math.log1p(-p)) : logP ? p : Math.log(p);
|
|
74
|
-
|
|
142
|
+
// w^(1 / a) with w = 1 - exp(lu / b). Where lu / b is subnormal or underflows, w = -lu / b, in logs; for w > 1/2
|
|
143
|
+
// log w = log1p(-exp(lu / b)) keeps the digits of 1 - w
|
|
144
|
+
const t = lu / b;
|
|
145
|
+
if (!(t <= -MIN_NORMAL))
|
|
146
|
+
return Math.exp((Math.log(-lu) - Math.log(b)) / a);
|
|
147
|
+
if (t < -Math.LN2)
|
|
148
|
+
return Math.exp(Math.log1p(-Math.exp(t)) / a);
|
|
149
|
+
const w = -Math.expm1(t);
|
|
150
|
+
const r = 1 / a;
|
|
151
|
+
if (!(a < 1e300))
|
|
152
|
+
return Math.pow(w, r);
|
|
153
|
+
// w^r corrected for the rounding of r = 1 / a (exactly 1 / a - r = (1 - a r) / a), which log w magnifies
|
|
154
|
+
const [ar, e] = twoProd(a, r);
|
|
155
|
+
return Math.pow(w, r) * (1 + (Math.log(w) * (1 - ar - e)) / a);
|
|
75
156
|
}
|
|
76
157
|
/**
|
|
77
158
|
* Density of the Kumaraswamy distribution. Equivalent to `extraDistr::dkumar(x, a, b, log = FALSE)` in R.
|
|
@@ -6,12 +6,15 @@
|
|
|
6
6
|
// Tail probabilities use Φ(-a) = erfcx(a/√2) exp(-a^2/2) / 2 (xsf's erfcx, src/special/erf.ts).
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { d0, dt0 } from "../internal/dpq.js";
|
|
9
|
-
import { expNeg, mulDD } from "../internal/ddexp.js";
|
|
9
|
+
import { ddExp, expNeg, mulDD } from "../internal/ddexp.js";
|
|
10
10
|
import { ddAdd, ddDiv, ddLog, ddMul, ddSub, twoSum } from "../special/dd.js";
|
|
11
11
|
import { erfcxNonNegative } from "../special/erf.js";
|
|
12
12
|
import { pnorm, qnorm } from "./normal.js";
|
|
13
13
|
const LN_SQRT_2PI = 0.918938533204672741780329736406;
|
|
14
14
|
const INV_SQRT_2PI = 0.398942280401432677939946059934;
|
|
15
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
16
|
+
/** log(sqrt(2 pi)) in double-double */
|
|
17
|
+
const LN_SQRT_2PI_DD = [0.91893853320467278, -3.8782941580672414e-17];
|
|
15
18
|
/** z = (log x - meanlog) / sdlog (x > 0, finite parameters, sdlog > 0) */
|
|
16
19
|
function zOf(x, meanlog, sdlog) {
|
|
17
20
|
const d = ddSub(ddLog([x, 0]), [meanlog, 0]);
|
|
@@ -94,9 +97,27 @@ export function dlnorm1(x, meanlog, sdlog, log) {
|
|
|
94
97
|
}
|
|
95
98
|
const z = zOf(x, meanlog, sdlog);
|
|
96
99
|
const h = halfSquare(z);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
+
// The exponent z^2/2 + log(sqrt(2 pi) sdlog x) in double-double: its terms (log x up to 745) can cancel to a log
|
|
101
|
+
// density near 0, where their roundings would show (dlnorm(1.1e-294, -0.21, 18.5, log) was 1.3e-13 off)
|
|
102
|
+
const exponent = () => ddAdd(ddAdd(h, LN_SQRT_2PI_DD), ddAdd(ddLog([x, 0]), ddLog([sdlog, 0])));
|
|
103
|
+
if (log) {
|
|
104
|
+
if (!(h[0] < 1e300))
|
|
105
|
+
return -(LN_SQRT_2PI + h[0] + h[1] + Math.log(sdlog) + Math.log(x));
|
|
106
|
+
const e = exponent();
|
|
107
|
+
return -(e[0] + e[1]);
|
|
108
|
+
}
|
|
109
|
+
const eh = expNeg(h);
|
|
110
|
+
// Where exp(-z^2/2) underflows to 0 the density is 0 even if 1 / x would bring it back into range: R computes it so
|
|
111
|
+
// (fixture: dlnorm(1e-100, -100, 3) is 0, 5.6e-311 exactly), kept to match
|
|
112
|
+
if (eh === 0)
|
|
113
|
+
return 0;
|
|
114
|
+
if (eh < MIN_NORMAL) {
|
|
115
|
+
// exp(-z^2/2) subnormal: exp of the whole exponent instead (dlnorm(2e-105, 1.53, 6.36) had lost 2.8e-7 relative in
|
|
116
|
+
// the subnormal factor)
|
|
117
|
+
const e = exponent();
|
|
118
|
+
return e[0] > 746 ? 0 : ddExp([-e[0], -e[1]])[0];
|
|
119
|
+
}
|
|
120
|
+
return (INV_SQRT_2PI * eh) / sdlog / x;
|
|
100
121
|
}
|
|
101
122
|
export function plnorm1(q, meanlog, sdlog, lowerTail, logP) {
|
|
102
123
|
if (Number.isNaN(q) || Number.isNaN(meanlog) || Number.isNaN(sdlog))
|
|
@@ -121,6 +142,9 @@ export function qlnorm1(p, meanlog, sdlog, lowerTail, logP) {
|
|
|
121
142
|
// exp(meanlog + sdlog z). Form the exponent in double-double and apply the low-order term to first order
|
|
122
143
|
const zd = refineStdQuantile(z, p, lowerTail, logP);
|
|
123
144
|
const e = ddAdd(ddAdd(mulDD(sdlog, zd[0]), [sdlog * zd[1], 0]), [meanlog, 0]);
|
|
145
|
+
// Beyond the range of exp the result is 0 or Infinity (for a huge exponent e[1] can exceed 1 and flip the sign)
|
|
146
|
+
if (!(Math.abs(e[0]) < 750))
|
|
147
|
+
return Math.exp(e[0]);
|
|
124
148
|
return Math.exp(e[0]) * (1 + e[1]);
|
|
125
149
|
}
|
|
126
150
|
/**
|