@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
|
@@ -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,18 @@ 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
|
-
|
|
90
|
+
// Where a log r is subnormal (a subnormal shape), it has lost digits, and so would the log of the probability -lu
|
|
91
|
+
// taken from it: log a + log log r instead (ppareto(318.7, { a: 1.04e-317, logP: true }) was 3e-11 off; extraDistr
|
|
92
|
+
// gives -Inf there, taking the log after the probability underflowed)
|
|
93
|
+
if (-lu < MIN_NORMAL)
|
|
94
|
+
return Math.log(a) + Math.log(rt[2]);
|
|
95
|
+
return lu < -Math.LN2 ? Math.log1p(-powNeg(q, b, rt, a)) : log1mExp(lu);
|
|
45
96
|
}
|
|
46
97
|
export function qpareto1(p, a, b, lowerTail, logP) {
|
|
47
98
|
if (Number.isNaN(p) || !valid(a, b))
|
|
@@ -52,7 +103,10 @@ export function qpareto1(p, a, b, lowerTail, logP) {
|
|
|
52
103
|
if (a === Infinity)
|
|
53
104
|
return b;
|
|
54
105
|
const lu = lowerTail ? (logP ? log1mExp(p) : Math.log1p(-p)) : logP ? p : Math.log(p);
|
|
55
|
-
|
|
106
|
+
// b exp(-lu / a); where the exponential overflows but b is small the product may not: then in logs
|
|
107
|
+
const t = -lu / a;
|
|
108
|
+
const v = Math.exp(t);
|
|
109
|
+
return v < Infinity ? b * v : Math.exp(Math.log(b) + t);
|
|
56
110
|
}
|
|
57
111
|
/**
|
|
58
112
|
* Density of the Pareto distribution. Equivalent to `extraDistr::dpareto(x, a = 1, b = 1, log = FALSE)` in R.
|
package/dist/distributions/t.js
CHANGED
|
@@ -14,8 +14,8 @@ import { lbeta } from "../special/beta.js";
|
|
|
14
14
|
import { ddAdd, ddDiv, ddDivNum, ddLog, ddMul, ddMulNum, ddNeg, ddSub, twoProd, twoSum } from "../special/dd.js";
|
|
15
15
|
import { betaIncBothDD, betaIncDD } from "../special/incbet.js";
|
|
16
16
|
import { bd0, bd0dd, stirlerr } from "../special/loader.js";
|
|
17
|
-
import { pnorm, qnorm } from "./normal.js";
|
|
18
|
-
import { dnt1, pnt1, qnt1 } from "./nt.js";
|
|
17
|
+
import { dnorm, pnorm, qnorm } from "./normal.js";
|
|
18
|
+
import { HUGE_DF, dnt1, hugeDfRatio, hugeDfTail, hugeDfTailLog, hugeDfTailQuantile, pnt1, qnt1 } from "./nt.js";
|
|
19
19
|
const LN_SQRT_2PI = 0.918938533204672741780329736406;
|
|
20
20
|
/** -log(sqrt(2 pi)) in double-double (computed with Rmpfr) */
|
|
21
21
|
const LN_SQRT_2PI_DD_NEG = [-0.91893853320467278, 3.8782941580672414e-17];
|
|
@@ -68,10 +68,75 @@ export function dtDensity1(x, df, log) {
|
|
|
68
68
|
const lv = -0.5 * x * x - LN_SQRT_2PI;
|
|
69
69
|
return log ? lv : Math.exp(lv);
|
|
70
70
|
}
|
|
71
|
+
if (df > HUGE_DF) {
|
|
72
|
+
if (hugeDfRatio(x, df) < 1e-17)
|
|
73
|
+
return dnorm(x, { log });
|
|
74
|
+
const lv = hugeDfTailLog(x, df);
|
|
75
|
+
return log ? lv : Math.exp(lv);
|
|
76
|
+
}
|
|
77
|
+
if (df < TINY_DF_DENSITY)
|
|
78
|
+
return dtTinyDf(x, df, log);
|
|
71
79
|
return dtCore(x, df, log);
|
|
72
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Below this n the density is sqrt(n) / (2 sqrt(1 + x^2/n)) (dtTinyDf). dtCore loses digits there (1e-14 of the
|
|
83
|
+
* value at n = 1e-80), and for a subnormal n, h = n/2 is rounded (0 at the smallest subnormal: NaN)
|
|
84
|
+
*/
|
|
85
|
+
const TINY_DF_DENSITY = 1e-20;
|
|
86
|
+
/**
|
|
87
|
+
* Density for n < TINY_DF_DENSITY: with Gamma(n/2) = (2/n)(1 + O(n)), f(x) = sqrt(n)/2 (1 + x^2/n)^(-1/2) times
|
|
88
|
+
* (1 + O(n)) and (1 + x^2/n)^(-n/2), whose log, n log(|x|/sqrt(n)) at most, is below 1e-17
|
|
89
|
+
*/
|
|
90
|
+
function dtTinyDf(x, n, log) {
|
|
91
|
+
const sn = Math.sqrt(n);
|
|
92
|
+
const ax = Math.abs(x);
|
|
93
|
+
// u = |x|/sqrt(n) may overflow; beyond 1e150, sqrt(1 + u^2) = u
|
|
94
|
+
const u = ax / sn;
|
|
95
|
+
if (log)
|
|
96
|
+
return Math.log(0.5 * sn) - (u < 1e150 ? 0.5 * Math.log1p(u * u) : Math.log(ax) - Math.log(sn));
|
|
97
|
+
return u < 1e150 ? (0.5 * sn) / Math.sqrt(1 + u * u) : 0.5 * sn * (sn / ax);
|
|
98
|
+
}
|
|
99
|
+
/** Below this n the upper tail is 1/2 - tinyDfDelta (the next terms are under 1e-23 absolute) */
|
|
100
|
+
const TINY_DF = 1e-15;
|
|
101
|
+
/**
|
|
102
|
+
* P[0 < T < t] (t > 0) for n < TINY_DF: (n/2) asinh(t / sqrt(n)). With a = n/2 and y = t^2/(n + t^2),
|
|
103
|
+
* P[|T| < t] = I_y(1/2, a), where 1/B(1/2, a) = a (1 + O(a)) and the integrand u^(-1/2) (1 - u)^(a-1) is
|
|
104
|
+
* u^(-1/2) / (1 - u) (1 + O(a log(1 - u))), whose integral over [0, y] is 2 atanh(sqrt(y)) = 2 asinh(t / sqrt(n)).
|
|
105
|
+
* The relative error is O(n log(t^2/n)), below n^2 log^2(t^2/n) < 1e-23 absolute. The incomplete beta function's
|
|
106
|
+
* series loses digits for so small an a (pt(-5.05, 2.4e-242) was 0.49999999999998995, 0.5 further out)
|
|
107
|
+
*/
|
|
108
|
+
function tinyDfDelta(t, n) {
|
|
109
|
+
const r = t / Math.sqrt(n);
|
|
110
|
+
// asinh(r) = log(2 r) + O(r^-2) where r overflows
|
|
111
|
+
return 0.5 * n * (r < 1e300 ? Math.asinh(r) : Math.LN2 + Math.log(t) - 0.5 * Math.log(n));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* x = n/(n + t^2) and 1 - x = t^2/(n + t^2) in double-double. Where t^2 or n is beyond the range of Veltkamp
|
|
115
|
+
* splitting (the products in ddDiv overflowed to NaN: pt(-3.5e151, 4.3e121)), both are first scaled by the same power
|
|
116
|
+
* of 2, 2^-2m with 2^m >= max(t, sqrt(n)) (exact unless t^2 / n is so small that 1 - x does not matter)
|
|
117
|
+
*/
|
|
118
|
+
function betaArgs(t, n) {
|
|
119
|
+
let tt = t;
|
|
120
|
+
let nn = n;
|
|
121
|
+
if (t > 1e150 || n > 1e290) {
|
|
122
|
+
const f = 2 ** -Math.ceil(Math.log2(Math.max(t, Math.sqrt(n))));
|
|
123
|
+
tt = t * f;
|
|
124
|
+
nn = n * f * f;
|
|
125
|
+
}
|
|
126
|
+
const t2d = twoProd(tt, tt);
|
|
127
|
+
const den = ddAdd(t2d, [nn, 0]);
|
|
128
|
+
return [ddDiv([nn, 0], den), ddDiv(t2d, den)];
|
|
129
|
+
}
|
|
73
130
|
/** Log or value of P[T > t] (t > 0): half of s = P[|T| > t] = I_{n/(n+t^2)}(n/2, 1/2) */
|
|
74
131
|
function upperTailPositive(t, n, log) {
|
|
132
|
+
// (qt's solver can step to t = Infinity, which gave NaN and left it at a wrong point: qt(0.499999999, 1e-10, upper)
|
|
133
|
+
// was 0.59, not 2425.8)
|
|
134
|
+
if (t === Infinity)
|
|
135
|
+
return log ? -Infinity : 0;
|
|
136
|
+
if (n < TINY_DF) {
|
|
137
|
+
const d = tinyDfDelta(t, n);
|
|
138
|
+
return log ? Math.log1p(-2 * d) - Math.LN2 : 0.5 - d;
|
|
139
|
+
}
|
|
75
140
|
const t2 = t * t;
|
|
76
141
|
const x = n / (n + t2);
|
|
77
142
|
if (!(x > 1e-250)) {
|
|
@@ -82,14 +147,14 @@ function upperTailPositive(t, n, log) {
|
|
|
82
147
|
const e = ddAdd(ddMulNum(lx, n / 2), [-Math.log(n / 2) - lbeta(n / 2, 0.5) - Math.LN2, 0]);
|
|
83
148
|
return log ? e[0] + e[1] : Math.exp(e[0]) * (1 + e[1]);
|
|
84
149
|
}
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
const den = ddAdd(t2d, [n, 0]);
|
|
88
|
-
const s = betaIncDD(n / 2, 0.5, ddDiv([n, 0], den), ddDiv(t2d, den), true, log);
|
|
150
|
+
const [x1, y1] = betaArgs(t, n);
|
|
151
|
+
const s = betaIncDD(n / 2, 0.5, x1, y1, true, log);
|
|
89
152
|
return log ? s - Math.LN2 : s / 2;
|
|
90
153
|
}
|
|
91
154
|
/** [log, linear] of upperTailPositive (each value identical to upperTailPositive) */
|
|
92
155
|
function upperTailPositiveBoth(t, n) {
|
|
156
|
+
if (n < TINY_DF || t === Infinity)
|
|
157
|
+
return [upperTailPositive(t, n, true), upperTailPositive(t, n, false)];
|
|
93
158
|
const t2 = t * t;
|
|
94
159
|
const x = n / (n + t2);
|
|
95
160
|
if (!(x > 1e-250)) {
|
|
@@ -97,9 +162,8 @@ function upperTailPositiveBoth(t, n) {
|
|
|
97
162
|
const e = ddAdd(ddMulNum(lx, n / 2), [-Math.log(n / 2) - lbeta(n / 2, 0.5) - Math.LN2, 0]);
|
|
98
163
|
return [e[0] + e[1], Math.exp(e[0]) * (1 + e[1])];
|
|
99
164
|
}
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const s = betaIncBothDD(n / 2, 0.5, ddDiv([n, 0], den), ddDiv(t2d, den), true);
|
|
165
|
+
const [x1, y1] = betaArgs(t, n);
|
|
166
|
+
const s = betaIncBothDD(n / 2, 0.5, x1, y1, true);
|
|
103
167
|
return [s[0] - Math.LN2, s[1] / 2];
|
|
104
168
|
}
|
|
105
169
|
export function pt1(q, df, lowerTail, logP) {
|
|
@@ -113,6 +177,8 @@ export function pt1(q, df, lowerTail, logP) {
|
|
|
113
177
|
return pnorm(q, { lowerTail, logP });
|
|
114
178
|
if (q === 0)
|
|
115
179
|
return logP ? -Math.LN2 : 0.5;
|
|
180
|
+
if (df > HUGE_DF)
|
|
181
|
+
return hugeDfRatio(q, df) < 1e-17 ? pnorm(q, { lowerTail, logP }) : hugeDfTail(q, df, lowerTail, logP);
|
|
116
182
|
// Lower tail for q < 0 = upper tail at |q|. If the requested side is the smaller tail compute it directly, otherwise 1 - (smaller tail)
|
|
117
183
|
const small = q < 0 === lowerTail;
|
|
118
184
|
const a = Math.abs(q);
|
|
@@ -132,18 +198,39 @@ export function qt1(p, df, lowerTail, logP) {
|
|
|
132
198
|
if (!Number.isFinite(df))
|
|
133
199
|
return qnorm(p, { lowerTail, logP });
|
|
134
200
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
201
|
+
if (df > HUGE_DF) {
|
|
202
|
+
// The quantiles of the limits of pt1
|
|
203
|
+
const z = qnorm(p, { lowerTail, logP });
|
|
204
|
+
if (hugeDfRatio(z, df) < 1e-17)
|
|
205
|
+
return z;
|
|
206
|
+
const y = hugeDfTailQuantile(logTarget, df);
|
|
207
|
+
return lower ? -y : y;
|
|
208
|
+
}
|
|
135
209
|
if (logTarget === -Math.LN2 && linearTarget === 0.5)
|
|
136
210
|
return 0;
|
|
211
|
+
if (df < TINY_DF) {
|
|
212
|
+
// Invert P[T > t] = 1/2 - (n/2) asinh(t / sqrt(n)) (tinyDfDelta) for t: Infinity unless p is within about
|
|
213
|
+
// 400 n of 1/2. (Through the general code below lbeta(n/2, 1/2) is Infinity at a subnormal n, and the solver stopped
|
|
214
|
+
// on the flat tail: qt(1e-5, 1e-310, upper) was 4.26, although P[T > t] is 1/2 to the double there)
|
|
215
|
+
const d = linearTarget !== undefined ? 0.5 - linearTarget : -0.5 * Math.expm1(logTarget + Math.LN2);
|
|
216
|
+
const A = (2 * d) / df;
|
|
217
|
+
const y = A < 700 ? Math.sqrt(df) * Math.sinh(A) : Math.exp(A - Math.LN2 + 0.5 * Math.log(df));
|
|
218
|
+
return lower ? -y : y;
|
|
219
|
+
}
|
|
137
220
|
// Smaller tail: if lower, the answer is negative. Solve the upper tail in |t|
|
|
138
221
|
// Starting value: in the deep tail P[T > t] ≈ n^(n/2) t^(-n) / (n B(n/2, 1/2))
|
|
139
222
|
const n = df;
|
|
140
223
|
let x0;
|
|
141
224
|
// log t from the tail asymptotic P[T > t] ≈ n^(n/2) t^(-n) / (n B(n/2, 1/2))
|
|
142
225
|
const lx0 = (0.5 * n * Math.log(n) - Math.log(n) - lbeta(n / 2, 0.5) - logTarget) / n;
|
|
143
|
-
// ±Inf if the answer exceeds the largest double (t is large there and the asymptotic form is accurate)
|
|
144
|
-
|
|
226
|
+
// ±Inf if the answer exceeds the largest double (t is large there and the asymptotic form is accurate); near that
|
|
227
|
+
// decided by the tail at the largest double itself (a fixed 709.78 gave -Infinity for qt(3.048e-196, 0.633), whose
|
|
228
|
+
// answer is -1.79e308)
|
|
229
|
+
if (lx0 > 709.79 || (lx0 > 709.77 && upperTailPositive(Number.MAX_VALUE, n, true) > logTarget))
|
|
145
230
|
return lower ? -Infinity : Infinity;
|
|
146
|
-
|
|
231
|
+
// The asymptotic form needs t^2 >> n; for large n the tail stays normal-like far out (t^2 < n), where it would
|
|
232
|
+
// start Newton near sqrt(n) (qt(1.6e-243, 1e120) converged to -1.13 from there)
|
|
233
|
+
if (logTarget < -30 && 2 * lx0 > Math.log(n) + 1) {
|
|
147
234
|
x0 = Math.exp(lx0);
|
|
148
235
|
}
|
|
149
236
|
else {
|
|
@@ -157,6 +244,9 @@ export function qt1(p, df, lowerTail, logP) {
|
|
|
157
244
|
const g3 = (z * (((3 * z2 + 19) * z2 + 17) * z2 - 15)) / 384;
|
|
158
245
|
const g4 = (z * ((((79 * z2 + 776) * z2 + 1482) * z2 - 1920) * z2 - 945)) / 92160;
|
|
159
246
|
x0 = z + (g1 + (g2 + (g3 + g4 / n) / n) / n) / n;
|
|
247
|
+
// (the terms overflow for a huge z: qt(-1e100, 1e150, log = TRUE) was -1, solved from x0 = 1)
|
|
248
|
+
if (!Number.isFinite(x0))
|
|
249
|
+
x0 = z;
|
|
160
250
|
}
|
|
161
251
|
}
|
|
162
252
|
if (!(x0 > 0) || !Number.isFinite(x0))
|
|
@@ -3,25 +3,69 @@
|
|
|
3
3
|
// Written for rStats.js (no ported code). With w = b - a:
|
|
4
4
|
// f(x) = 2 (x - a) / (w (c - a)) left of c, 2 (b - x) / (w (b - c)) right of it;
|
|
5
5
|
// P[X <= x] = (x - a)^2 / (w (c - a)) left of c, P[X > x] = (b - x)^2 / (w (b - c)) right of it.
|
|
6
|
-
// Each tail is computed from its own end, and the quantile from the tail it falls in.
|
|
6
|
+
// Each tail is computed from its own end, and the quantile from the tail it falls in. Products are formed from ratios
|
|
7
|
+
// of lengths (each at most 1), so that wide and narrow intervals neither overflow nor underflow on the way.
|
|
7
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
9
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
10
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
9
11
|
const valid = (a, b, c) => Number.isFinite(a) && Number.isFinite(b) && a < b && a <= c && c <= b;
|
|
12
|
+
/** 1/2 when b - a overflows (x, a, b and c are all halved then; the probabilities do not change), else 1 */
|
|
13
|
+
const halfIfOverflow = (a, b) => (Number.isFinite(b - a) ? 1 : 0.5);
|
|
10
14
|
export function dtriang1(x, a, b, c, log) {
|
|
11
15
|
if (Number.isNaN(x) || !valid(a, b, c))
|
|
12
16
|
return NaN;
|
|
13
17
|
if (x < a || x > b)
|
|
14
18
|
return d0(log);
|
|
19
|
+
const h = halfIfOverflow(a, b);
|
|
20
|
+
if (h !== 1) {
|
|
21
|
+
const d = dtriang1(x * h, a * h, b * h, c * h, log);
|
|
22
|
+
return log ? d + Math.log(h) : d * h;
|
|
23
|
+
}
|
|
15
24
|
const w = b - a;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
const r = x < c ? (c - x) / (c - a) : (x - c) / (b - c);
|
|
25
|
+
// The density is (2 / w) s with s = (x - a) / (c - a) left of the mode, (b - x) / (b - c) right of it (s <= 1, so
|
|
26
|
+
// nothing over- or underflows on the way unless the density itself does). Near the mode s = 1 - r with r the
|
|
27
|
+
// fraction of the way from the mode to the end on x's side (x - c is exact there)
|
|
28
|
+
const r = x === c ? 0 : x < c ? (c - x) / (c - a) : (x - c) / (b - c);
|
|
21
29
|
if (r < 0.5)
|
|
22
|
-
return log ? Math.log(
|
|
23
|
-
const
|
|
24
|
-
|
|
30
|
+
return log ? Math.LN2 - Math.log(w) + Math.log1p(-r) : (2 * (1 - r)) / w;
|
|
31
|
+
const s = x < c ? (x - a) / (c - a) : (b - x) / (b - c);
|
|
32
|
+
if (s >= MIN_NORMAL)
|
|
33
|
+
return log ? Math.LN2 - Math.log(w) + Math.log(s) : (2 * s) / w;
|
|
34
|
+
// (a subnormal s has lost digits: from the lengths instead; dtriang(3.1e-190, { a: 0, b: 1.24e133, c: 5.58e132,
|
|
35
|
+
// log: true }) was 1% off)
|
|
36
|
+
const l = Math.LN2 - Math.log(w) + (x < c ? Math.log(x - a) - Math.log(c - a) : Math.log(b - x) - Math.log(b - c));
|
|
37
|
+
return log ? l : Math.exp(l);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The tails [lower, upper] at a < q < b (b - a finite), from ratios of lengths that are at most 1, and the log of the
|
|
41
|
+
* near tail (the one from the end on q's side of the mode) as a sum of logs, which stays exact where the tail is
|
|
42
|
+
* subnormal (from the lengths themselves where a ratio is subnormal too: ptriang(3.1e-190, { a: 0, b: 1.24e133, c:
|
|
43
|
+
* 5.58e132, logP: true }) had lost its digits)
|
|
44
|
+
*/
|
|
45
|
+
function tails(q, a, b, c) {
|
|
46
|
+
const w = b - a;
|
|
47
|
+
const lnear = (s, t, d, part) => s >= MIN_NORMAL && t >= MIN_NORMAL ? Math.log(s) + Math.log(t) : 2 * Math.log(d) - Math.log(part) - Math.log(w);
|
|
48
|
+
// the far tail as a sum of positive terms: 1 - (q - a)^2 / (w (c - a)) = (b - c) / w + (c - q)(q - a + c - a) / (w (c - a))
|
|
49
|
+
if (q <= c) {
|
|
50
|
+
const [s, t] = [(q - a) / (c - a), (q - a) / w];
|
|
51
|
+
return [s * t, (b - c) / w + ((c - q) / (c - a)) * (t + (c - a) / w), lnear(s, t, q - a, c - a)];
|
|
52
|
+
}
|
|
53
|
+
const [s, t] = [(b - q) / (b - c), (b - q) / w];
|
|
54
|
+
return [(c - a) / w + ((q - c) / (b - c)) * (t + (b - c) / w), s * t, lnear(s, t, b - q, b - c)];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* log of the far tail (see tails) where it is subnormal, from the logs of its terms: the log of the subnormal sum had
|
|
58
|
+
* lost digits (ptriang(2.28e-257, { a: 0, b: 1.33e63, c: 0, logP: true }) was 25% off in its slope)
|
|
59
|
+
*/
|
|
60
|
+
function logFar(q, a, b, c) {
|
|
61
|
+
const w = b - a;
|
|
62
|
+
const [first, d, part, t, share] = q <= c ? [b - c, c - q, c - a, (q - a) / w, (c - a) / w] : [c - a, q - c, b - c, (b - q) / w, (b - c) / w];
|
|
63
|
+
const l2 = Math.log(d) - Math.log(part) + Math.log(t + share);
|
|
64
|
+
if (!(first > 0))
|
|
65
|
+
return l2;
|
|
66
|
+
const l1 = Math.log(first) - Math.log(w);
|
|
67
|
+
const m = Math.max(l1, l2);
|
|
68
|
+
return m + Math.log(Math.exp(l1 - m) + Math.exp(l2 - m));
|
|
25
69
|
}
|
|
26
70
|
export function ptriang1(q, a, b, c, lowerTail, logP) {
|
|
27
71
|
if (Number.isNaN(q) || !valid(a, b, c))
|
|
@@ -30,23 +74,18 @@ export function ptriang1(q, a, b, c, lowerTail, logP) {
|
|
|
30
74
|
return dt0(lowerTail, logP);
|
|
31
75
|
if (q >= b)
|
|
32
76
|
return dt1(lowerTail, logP);
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (q <= c) {
|
|
38
|
-
lower = ((q - a) * (q - a)) / (w * (c - a));
|
|
39
|
-
upper = (b - c) / w + ((c - q) * (q - a + (c - a))) / (w * (c - a));
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
upper = ((b - q) * (b - q)) / (w * (b - c));
|
|
43
|
-
lower = (c - a) / w + ((q - c) * (b - q + (b - c))) / (w * (b - c));
|
|
44
|
-
}
|
|
45
|
-
const v = lowerTail ? lower : upper;
|
|
77
|
+
const h = halfIfOverflow(a, b);
|
|
78
|
+
const [lower, upper, lnear] = tails(q * h, a * h, b * h, c * h);
|
|
79
|
+
// The sum of positive terms can round an ulp above 1
|
|
80
|
+
const v = Math.min(lowerTail ? lower : upper, 1);
|
|
46
81
|
if (!logP)
|
|
47
82
|
return v;
|
|
48
|
-
// the log of a probability above 1/2 from the other tail
|
|
49
|
-
|
|
83
|
+
// the log of a probability above 1/2 from the other tail; a subnormal near tail from its factors
|
|
84
|
+
if (v > 0.5)
|
|
85
|
+
return Math.log1p(-(lowerTail ? upper : lower));
|
|
86
|
+
if (v >= MIN_NORMAL)
|
|
87
|
+
return Math.log(v);
|
|
88
|
+
return lowerTail === q <= c ? lnear : logFar(q * h, a * h, b * h, c * h);
|
|
50
89
|
}
|
|
51
90
|
export function qtriang1(p, a, b, c, lowerTail, logP) {
|
|
52
91
|
if (Number.isNaN(p) || !valid(a, b, c))
|
|
@@ -54,13 +93,40 @@ export function qtriang1(p, a, b, c, lowerTail, logP) {
|
|
|
54
93
|
const bd = quantileBoundaries(p, a, b, lowerTail, logP);
|
|
55
94
|
if (bd !== undefined)
|
|
56
95
|
return bd;
|
|
96
|
+
const h = halfIfOverflow(a, b);
|
|
97
|
+
if (h !== 1)
|
|
98
|
+
return qtriang1(p, a * h, b * h, c * h, lowerTail, logP) / h;
|
|
57
99
|
const w = b - a;
|
|
58
100
|
const pv = logP ? Math.exp(p) : p;
|
|
101
|
+
// sqrt of the tail on the side of p; exp(p / 2) where exp(p) is subnormal
|
|
102
|
+
const sqrtP = logP && pv < 2 ** -1022 ? Math.exp(p / 2) : Math.sqrt(pv);
|
|
59
103
|
const lower = lowerTail ? pv : logP ? -Math.expm1(p) : 1 - p;
|
|
60
104
|
const upper = lowerTail ? (logP ? -Math.expm1(p) : 1 - p) : pv;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
105
|
+
// Left of the mode x - a = (c - a) sqrt(t) with t = lower w / (c - a), the share of the left part's probability;
|
|
106
|
+
// right of it b - x = (b - c) sqrt(t), t = upper w / (b - c). Near the mode (t > 1/2) the distance from it instead,
|
|
107
|
+
// (1 - sqrt(t)) = (1 - t) / (1 + sqrt(t)) with 1 - t from the other tail (for c = a or c = b that tail itself, so
|
|
108
|
+
// no cancellation near the end). The square roots are taken factor by factor so that no product over- or underflows,
|
|
109
|
+
// and t as the ratio of the probability to the part's share (lower / (c - a) overflowed for a subnormal width, which
|
|
110
|
+
// put x at the mode: qtriang(1.6e-8, { a: 0, b: 1.03e-316, c: 8.7e-317, lowerTail: false }))
|
|
111
|
+
const cl = (c - a) / w;
|
|
112
|
+
const cr = (b - c) / w;
|
|
113
|
+
let x;
|
|
114
|
+
if (cl > 0 && lower <= cl) {
|
|
115
|
+
const t = lower / cl;
|
|
116
|
+
x =
|
|
117
|
+
t <= 0.5
|
|
118
|
+
? a + (lowerTail ? sqrtP : Math.sqrt(lower)) * Math.sqrt(w) * Math.sqrt(c - a)
|
|
119
|
+
: c - ((c - a) * ((upper - cr) / cl)) / (1 + Math.sqrt(t));
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const t = upper / cr;
|
|
123
|
+
x =
|
|
124
|
+
t <= 0.5
|
|
125
|
+
? b - (lowerTail ? Math.sqrt(upper) : sqrtP) * Math.sqrt(w) * Math.sqrt(b - c)
|
|
126
|
+
: c + ((b - c) * ((lower - cl) / cr)) / (1 + Math.sqrt(t));
|
|
127
|
+
}
|
|
128
|
+
// Rounding must not take x out of the support
|
|
129
|
+
return Math.min(Math.max(x, a), b);
|
|
64
130
|
}
|
|
65
131
|
/**
|
|
66
132
|
* Density of the triangular distribution. Equivalent to `extraDistr::dtriang(x, a = -1, b = 1, c = (a + b)/2, log = FALSE)` in R.
|