@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
|
@@ -5,25 +5,80 @@
|
|
|
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, ddDivNum, 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
|
+
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
14
|
+
/**
|
|
15
|
+
* c log(1 - x^a) for 0 < x < 1 where x^a is subnormal: -c x^a (to double precision) from logs, since x^a itself has
|
|
16
|
+
* lost digits
|
|
17
|
+
*/
|
|
18
|
+
const scaledTinyPow = (x, a, c) => -Math.sign(c) * Math.exp(Math.log(Math.abs(c)) + a * Math.log(x));
|
|
19
|
+
/**
|
|
20
|
+
* Whether a log x (x near 1, a tiny) is subnormal or 0 although 1 - x^a > 0: -expm1 of it has lost digits
|
|
21
|
+
* (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
|
|
22
|
+
* factors
|
|
23
|
+
*/
|
|
24
|
+
const subnormalALogX = (x, a) => x < 1 && -(a * Math.log(x)) < MIN_NORMAL;
|
|
11
25
|
/** log(1 - x^a) for 0 <= x <= 1 */
|
|
12
26
|
function log1mPow(x, a) {
|
|
13
27
|
const xa = Math.pow(x, a);
|
|
14
|
-
|
|
28
|
+
if (xa < 0.5)
|
|
29
|
+
return Math.log1p(-xa);
|
|
30
|
+
return subnormalALogX(x, a) ? Math.log(a) + Math.log(-Math.log(x)) : Math.log(-Math.expm1(a * Math.log(x)));
|
|
15
31
|
}
|
|
16
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* (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
|
|
34
|
+
* correction is not small (e above about 1e8 magnifies the rounding of 1 - x^a), exp(e log1p(-x^a)) instead
|
|
35
|
+
*/
|
|
17
36
|
function powOneMinus(x, a, e) {
|
|
18
37
|
const xa = Math.pow(x, a);
|
|
19
|
-
if (xa >= 0.5)
|
|
20
|
-
|
|
38
|
+
if (xa >= 0.5) {
|
|
39
|
+
if (!subnormalALogX(x, a))
|
|
40
|
+
return Math.pow(-Math.expm1(a * Math.log(x)), e);
|
|
41
|
+
// (-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
|
|
42
|
+
// factor leaves the range
|
|
43
|
+
const v = Math.pow(a * 2 ** 600 * -Math.log(x), e) * Math.pow(2, -600 * e);
|
|
44
|
+
return v >= MIN_NORMAL && v < Infinity ? v : Math.exp(e * (Math.log(a) + Math.log(-Math.log(x))));
|
|
45
|
+
}
|
|
21
46
|
const hi = 1 - xa;
|
|
22
47
|
const lo = 1 - hi - xa; // exact remainder (Fast2Sum)
|
|
23
|
-
|
|
48
|
+
const c = (e * lo) / hi;
|
|
49
|
+
return Math.abs(c) < 1e-8 ? Math.pow(hi, e) * (1 + c) : Math.exp(e * Math.log1p(-xa));
|
|
24
50
|
}
|
|
25
51
|
/** c log(v), 0 when c is 0 */
|
|
26
52
|
const xlog = (c, v) => (c === 0 ? 0 : c * Math.log(v));
|
|
53
|
+
/**
|
|
54
|
+
* 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
|
|
55
|
+
* that cancel to a log density near 0, where their roundings would show (1.6e-13 at dkumar(0.305, 486, 3.8e247)), so
|
|
56
|
+
* 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
|
|
57
|
+
* 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
|
|
58
|
+
* x = 0 or 1, a subnormal x^a, and shapes beyond the range of Veltkamp splitting
|
|
59
|
+
*/
|
|
60
|
+
function logDensity(x, a, b, lb, xa) {
|
|
61
|
+
if (x === 0 || !(a < 1e300) || !(b < 1e300))
|
|
62
|
+
return Math.log(a) + Math.log(b) + xlog(a - 1, x) + lb;
|
|
63
|
+
const lx = ddLog([x, 0]);
|
|
64
|
+
let s = ddAdd(ddLog([a, 0]), ddLog([b, 0]));
|
|
65
|
+
if (a !== 1)
|
|
66
|
+
s = ddAdd(s, ddMul(lx, twoSum(a, -1)));
|
|
67
|
+
if (b === 1 || x === 1 || xa < MIN_NORMAL) {
|
|
68
|
+
if (!Number.isFinite(lb))
|
|
69
|
+
return s[0] + lb;
|
|
70
|
+
const t = ddAdd(s, [lb, 0]);
|
|
71
|
+
return t[0] + t[1];
|
|
72
|
+
}
|
|
73
|
+
// (log a + log(-log x) where a log x is subnormal, see subnormalALogX)
|
|
74
|
+
const l1 = xa <= 1e-10
|
|
75
|
+
? [Math.log1p(-xa), 0]
|
|
76
|
+
: subnormalALogX(x, a)
|
|
77
|
+
? ddAdd(ddLog([a, 0]), ddLog([-lx[0], -lx[1]]))
|
|
78
|
+
: ddLog(ddSub([1, 0], ddExp(ddMul(lx, [a, 0]))));
|
|
79
|
+
const t = ddAdd(s, ddMul(l1, twoSum(b, -1)));
|
|
80
|
+
return t[0] + t[1];
|
|
81
|
+
}
|
|
27
82
|
export function dkumar1(x, a, b, log) {
|
|
28
83
|
if (Number.isNaN(x) || !valid(a, b))
|
|
29
84
|
return NaN;
|
|
@@ -31,15 +86,27 @@ export function dkumar1(x, a, b, log) {
|
|
|
31
86
|
return d0(log);
|
|
32
87
|
if (a === Infinity)
|
|
33
88
|
return x === 1 ? Infinity : d0(log);
|
|
89
|
+
const xa = Math.pow(x, a);
|
|
90
|
+
const tiny = x > 0 && xa < MIN_NORMAL;
|
|
34
91
|
const l1 = x === 1 ? -Infinity : log1mPow(x, a);
|
|
35
|
-
|
|
92
|
+
// (b - 1) log(1 - x^a)
|
|
93
|
+
const lb = b === 1 ? 0 : tiny ? scaledTinyPow(x, a, b - 1) : (b - 1) * l1;
|
|
94
|
+
const ld = logDensity(x, a, b, lb, xa);
|
|
36
95
|
if (log)
|
|
37
96
|
return ld;
|
|
38
97
|
// the product keeps the relative accuracy that exp(ld) loses for large |ld|; exp(ld) where it under- or overflows
|
|
98
|
+
// or where x^(a-1) is subnormal (a large b can bring the density back into the normal range)
|
|
39
99
|
// x^(a-1) as x^a / x (a - 1 itself may round, which x^(a-1) magnifies by |log x|)
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
100
|
+
const xm = a === 1 ? 1 : xa > 1e-290 ? xa / x : Math.pow(x, a - 1);
|
|
101
|
+
if (x > 0 && xm < MIN_NORMAL)
|
|
102
|
+
return Math.exp(ld);
|
|
103
|
+
// (also where a factor or partial product is below the normal range: a b = 3.3e-315 for tiny shapes, which xm and
|
|
104
|
+
// the power brought back to 1e99 with 6e-10 relative error; the power (1 - x^a)^(b-1) subnormal)
|
|
105
|
+
const ab = a * b;
|
|
106
|
+
const abx = ab * xm;
|
|
107
|
+
const pw = b === 1 || x === 0 ? 1 : tiny ? Math.exp(lb) : powOneMinus(x, a, b - 1);
|
|
108
|
+
const d = abx * pw;
|
|
109
|
+
return ab >= MIN_NORMAL && abx >= MIN_NORMAL && pw >= MIN_NORMAL && d > MIN_NORMAL && d < Infinity ? d : Math.exp(ld);
|
|
43
110
|
}
|
|
44
111
|
export function pkumar1(q, a, b, lowerTail, logP) {
|
|
45
112
|
if (Number.isNaN(q) || !valid(a, b))
|
|
@@ -50,16 +117,23 @@ export function pkumar1(q, a, b, lowerTail, logP) {
|
|
|
50
117
|
return dt1(lowerTail, logP);
|
|
51
118
|
if (a === Infinity)
|
|
52
119
|
return dt0(lowerTail, logP);
|
|
53
|
-
const
|
|
120
|
+
const xa = Math.pow(q, a);
|
|
121
|
+
const tiny = xa < MIN_NORMAL;
|
|
122
|
+
const lu = tiny ? scaledTinyPow(q, a, b) : b * log1mPow(q, a); // log P[X > q]
|
|
54
123
|
if (!lowerTail)
|
|
55
|
-
return logP ? lu : powOneMinus(q, a, b);
|
|
124
|
+
return logP ? lu : tiny ? Math.exp(lu) : powOneMinus(q, a, b);
|
|
56
125
|
if (!logP)
|
|
57
126
|
return -Math.expm1(lu);
|
|
58
|
-
// for
|
|
59
|
-
|
|
60
|
-
if (xa < 1e-
|
|
127
|
+
// 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
|
|
128
|
+
// x^a or 1 - exp(lu) underflows; elsewhere the forms below are accurate
|
|
129
|
+
if (Math.max(b, 1) * xa < 1e-10)
|
|
61
130
|
return Math.log(b) + a * Math.log(q) + Math.log1p((-(b - 1) * xa) / 2);
|
|
62
|
-
|
|
131
|
+
// Where b log(1 - x^a) is subnormal (a subnormal b), it has lost digits, and so would the log of the probability
|
|
132
|
+
// -lu taken from it: log b + log(-log(1 - x^a)) instead (pkumar(0.2, 0.42, 1.85e-317, { logP: true }) was 2.5e-10 off;
|
|
133
|
+
// extraDistr gives -Inf there, taking the log after the probability underflowed)
|
|
134
|
+
if (-lu < MIN_NORMAL)
|
|
135
|
+
return Math.log(b) + Math.log(-log1mPow(q, a));
|
|
136
|
+
return lu < -Math.LN2 && !tiny ? Math.log1p(-powOneMinus(q, a, b)) : log1mExp(lu);
|
|
63
137
|
}
|
|
64
138
|
export function qkumar1(p, a, b, lowerTail, logP) {
|
|
65
139
|
if (Number.isNaN(p) || !valid(a, b))
|
|
@@ -71,7 +145,32 @@ export function qkumar1(p, a, b, lowerTail, logP) {
|
|
|
71
145
|
return 1;
|
|
72
146
|
// log of the upper probability
|
|
73
147
|
const lu = lowerTail ? (logP ? log1mExp(p) : Math.log1p(-p)) : logP ? p : Math.log(p);
|
|
74
|
-
|
|
148
|
+
// w^(1 / a) with w = 1 - exp(lu / b). Where lu / b is subnormal or underflows, w = -lu / b, in logs; for w > 1/2
|
|
149
|
+
// log w = log1p(-exp(lu / b)) keeps the digits of 1 - w
|
|
150
|
+
const t = lu / b;
|
|
151
|
+
// (log(-lu) is p itself where a lower tail given as its log is below the normal range: -lu = exp(p) was rounded on
|
|
152
|
+
// the subnormal grid (qkumar(-744.24, 1.13, 0.124, { logP: true }) was 17% off). The exponent, of the order of
|
|
153
|
+
// hundreds, in double-double: its rounding is the relative error of the quantile (3.7e-14 there)
|
|
154
|
+
if (!(t <= -MIN_NORMAL)) {
|
|
155
|
+
const llu = logP && lowerTail && p < LOG_MIN_NORMAL ? p : Math.log(-lu);
|
|
156
|
+
const n = ddSub([llu, 0], ddLog([b, 0]));
|
|
157
|
+
const q = n[0] / a;
|
|
158
|
+
if (!(a < 1e300 && Math.abs(q) < 1e300))
|
|
159
|
+
return Math.exp(q);
|
|
160
|
+
const e = ddExp(ddDivNum(n, a));
|
|
161
|
+
return e[0] + e[1];
|
|
162
|
+
}
|
|
163
|
+
if (t < -Math.LN2)
|
|
164
|
+
return Math.exp(Math.log1p(-Math.exp(t)) / a);
|
|
165
|
+
const w = -Math.expm1(t);
|
|
166
|
+
const r = 1 / a;
|
|
167
|
+
// (for a below 1e-300, w <= 1/2 here and the quantile w^(1/a) underflows to 0; the correction below was NaN there,
|
|
168
|
+
// from the splitting of a subnormal a or 1 / a overflowing: qkumar(4.8e-52, 9.9e-314, 2.5))
|
|
169
|
+
if (!(a < 1e300) || !(r < 1e300))
|
|
170
|
+
return Math.pow(w, r);
|
|
171
|
+
// w^r corrected for the rounding of r = 1 / a (exactly 1 / a - r = (1 - a r) / a), which log w magnifies
|
|
172
|
+
const [ar, e] = twoProd(a, r);
|
|
173
|
+
return Math.pow(w, r) * (1 + (Math.log(w) * (1 - ar - e)) / a);
|
|
75
174
|
}
|
|
76
175
|
/**
|
|
77
176
|
* Density of the Kumaraswamy distribution. Equivalent to `extraDistr::dkumar(x, a, b, log = FALSE)` in R.
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
// (and the mirror image above 1/2).
|
|
6
6
|
import { vectorize } from "../internal/vectorize.js";
|
|
7
7
|
import { quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
8
|
-
import { expNeg, locationScale, standardizeDD } from "../internal/ddexp.js";
|
|
8
|
+
import { expNeg, expNegOver, locationScale, standardizeDD } from "../internal/ddexp.js";
|
|
9
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
9
10
|
const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0 && Number.isFinite(sigma);
|
|
10
11
|
// an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does
|
|
11
12
|
export function dlaplace1(x, mu, sigma, log) {
|
|
@@ -17,7 +18,9 @@ export function dlaplace1(x, mu, sigma, log) {
|
|
|
17
18
|
const a = z[0] < 0 ? [-z[0], -z[1]] : [z[0], z[1]];
|
|
18
19
|
if (log)
|
|
19
20
|
return -a[0] - a[1] - Math.log(2 * sigma);
|
|
20
|
-
|
|
21
|
+
// (from exp(-|z|) below the normal range the quotient is formed without rounding it there: see expNegOver)
|
|
22
|
+
const e = expNeg(a);
|
|
23
|
+
return e >= MIN_NORMAL ? e / (2 * sigma) : expNegOver(a, 2 * sigma);
|
|
21
24
|
}
|
|
22
25
|
export function plaplace1(q, mu, sigma, lowerTail, logP) {
|
|
23
26
|
if (Number.isNaN(q) || !valid(mu, sigma))
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// Quantiles compute logit(p) = log(p / (1 - p)) in a cancellation-free form.
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { quantileBoundaries } from "../internal/dpq.js";
|
|
9
|
-
import { expNeg, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
|
|
9
|
+
import { expNeg, expNegOver, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
|
|
10
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
10
11
|
export function dlogis1(x, location, scale, log) {
|
|
11
12
|
if (Number.isNaN(x) || Number.isNaN(location) || Number.isNaN(scale))
|
|
12
13
|
return NaN;
|
|
@@ -19,7 +20,12 @@ export function dlogis1(x, location, scale, log) {
|
|
|
19
20
|
const e = expNeg(a);
|
|
20
21
|
if (log)
|
|
21
22
|
return -a[0] - a[1] - Math.log(scale) - 2 * Math.log1p(e);
|
|
22
|
-
|
|
23
|
+
// Neither e nor the denominator may be rounded on the subnormal grid: from e below the normal range (1 + e is then 1)
|
|
24
|
+
// the quotient by expNegOver, and for a subnormal scale the division by it last (dlogis(-3.5667e-316, { scale:
|
|
25
|
+
// 4.81926e-319 }) was 1.3e-6 off)
|
|
26
|
+
if (e < MIN_NORMAL)
|
|
27
|
+
return expNegOver(a, scale);
|
|
28
|
+
return scale >= MIN_NORMAL ? e / (scale * (1 + e) * (1 + e)) : e / ((1 + e) * (1 + e)) / scale;
|
|
23
29
|
}
|
|
24
30
|
export function plogis1(q, location, scale, lowerTail, logP) {
|
|
25
31
|
if (Number.isNaN(q) || Number.isNaN(location) || Number.isNaN(scale))
|
|
@@ -6,19 +6,29 @@
|
|
|
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]);
|
|
18
21
|
if (sdlog === 1)
|
|
19
22
|
return d;
|
|
20
23
|
const hi = d[0] / sdlog;
|
|
21
|
-
|
|
24
|
+
if (!(Number.isFinite(hi) && Math.abs(hi) < 1e300))
|
|
25
|
+
return [hi, 0];
|
|
26
|
+
if (sdlog < 1e300)
|
|
27
|
+
return ddDiv(d, [sdlog, 0]);
|
|
28
|
+
// The splitting of sdlog in the division overflows above 1e300 (plnorm(2, sdlog = 1e302) was NaN): both scaled by
|
|
29
|
+
// 2^-64 (exactly, unless d is so small that z is far below the rounding of the terms it enters)
|
|
30
|
+
const s = 2 ** -64;
|
|
31
|
+
return ddDiv([d[0] * s, d[1] * s], [sdlog * s, 0]);
|
|
22
32
|
}
|
|
23
33
|
/** z^2 / 2 in double-double */
|
|
24
34
|
function halfSquare(z) {
|
|
@@ -94,9 +104,27 @@ export function dlnorm1(x, meanlog, sdlog, log) {
|
|
|
94
104
|
}
|
|
95
105
|
const z = zOf(x, meanlog, sdlog);
|
|
96
106
|
const h = halfSquare(z);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
107
|
+
// 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
|
|
108
|
+
// density near 0, where their roundings would show (dlnorm(1.1e-294, -0.21, 18.5, log) was 1.3e-13 off)
|
|
109
|
+
const exponent = () => ddAdd(ddAdd(h, LN_SQRT_2PI_DD), ddAdd(ddLog([x, 0]), ddLog([sdlog, 0])));
|
|
110
|
+
if (log) {
|
|
111
|
+
if (!(h[0] < 1e300))
|
|
112
|
+
return -(LN_SQRT_2PI + h[0] + h[1] + Math.log(sdlog) + Math.log(x));
|
|
113
|
+
const e = exponent();
|
|
114
|
+
return -(e[0] + e[1]);
|
|
115
|
+
}
|
|
116
|
+
const eh = expNeg(h);
|
|
117
|
+
// 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
|
|
118
|
+
// (fixture: dlnorm(1e-100, -100, 3) is 0, 5.6e-311 exactly), kept to match
|
|
119
|
+
if (eh === 0)
|
|
120
|
+
return 0;
|
|
121
|
+
if (eh < MIN_NORMAL) {
|
|
122
|
+
// 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
|
|
123
|
+
// the subnormal factor)
|
|
124
|
+
const e = exponent();
|
|
125
|
+
return e[0] > 746 ? 0 : ddExp([-e[0], -e[1]])[0];
|
|
126
|
+
}
|
|
127
|
+
return (INV_SQRT_2PI * eh) / sdlog / x;
|
|
100
128
|
}
|
|
101
129
|
export function plnorm1(q, meanlog, sdlog, lowerTail, logP) {
|
|
102
130
|
if (Number.isNaN(q) || Number.isNaN(meanlog) || Number.isNaN(sdlog))
|
|
@@ -120,7 +148,14 @@ export function qlnorm1(p, meanlog, sdlog, lowerTail, logP) {
|
|
|
120
148
|
}
|
|
121
149
|
// exp(meanlog + sdlog z). Form the exponent in double-double and apply the low-order term to first order
|
|
122
150
|
const zd = refineStdQuantile(z, p, lowerTail, logP);
|
|
151
|
+
// Above sdlog = 1e300 the splitting in the double-double product overflows (qlnorm(1e-30, 0, 9e307) was NaN). The
|
|
152
|
+
// exponent is then beyond the range of exp unless |z| < 1e-297 (p within that of 1/2), where it is close to meanlog
|
|
153
|
+
if (sdlog >= 1e300)
|
|
154
|
+
return Math.exp(meanlog + sdlog * zd[0]);
|
|
123
155
|
const e = ddAdd(ddAdd(mulDD(sdlog, zd[0]), [sdlog * zd[1], 0]), [meanlog, 0]);
|
|
156
|
+
// Beyond the range of exp the result is 0 or Infinity (for a huge exponent e[1] can exceed 1 and flip the sign)
|
|
157
|
+
if (!(Math.abs(e[0]) < 750))
|
|
158
|
+
return Math.exp(e[0]);
|
|
124
159
|
return Math.exp(e[0]) * (1 + e[1]);
|
|
125
160
|
}
|
|
126
161
|
/**
|