@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
|
@@ -17,12 +17,11 @@
|
|
|
17
17
|
// interpolants in log w (speeds up the outer integral, the quantile search, and repeated calls with the same nmeans)
|
|
18
18
|
// - The outer integral is over v = log(t/a), t = df s^2 / 2 ~ Gamma(a = df/2), with the density in Loader's form
|
|
19
19
|
// (stirlerr and bd0 = a (e^v - 1 - v)). When the result is below 1e-290, it is recomputed with a version that
|
|
20
|
-
// keeps the integrand on the log scale
|
|
20
|
+
// keeps the integrand on the log scale
|
|
21
21
|
// Quantiles solve for the log probability of the smaller tail in log q with Brent's method.
|
|
22
22
|
import { vectorize } from "../internal/vectorize.js";
|
|
23
23
|
import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
24
|
-
import {
|
|
25
|
-
import { lgamma } from "../special/gamma.js";
|
|
24
|
+
import { stirlerr } from "../special/loader.js";
|
|
26
25
|
import { pnorm } from "./normal.js";
|
|
27
26
|
// ---- Clenshaw-Curtis rule (convergence judged from the Chebyshev coefficients) ----
|
|
28
27
|
//
|
|
@@ -33,6 +32,8 @@ import { pnorm } from "./normal.js";
|
|
|
33
32
|
// this test is reliable.
|
|
34
33
|
const CC_MAX = 128;
|
|
35
34
|
const EPS = Number.EPSILON;
|
|
35
|
+
/** Upper bound on the number of intervals of one integral (smooth integrands need a few dozen) */
|
|
36
|
+
const MAX_PIECES = 256;
|
|
36
37
|
/** Table of cos(π m / CC_MAX) (m = 0..2 CC_MAX) */
|
|
37
38
|
const COS_TABLE = Float64Array.from({ length: 2 * CC_MAX + 1 }, (_, m) => Math.cos((Math.PI * m) / CC_MAX));
|
|
38
39
|
/** Weights of the N-point Clenshaw-Curtis rule (interval [-1, 1], nodes cos(jπ/N)) */
|
|
@@ -82,6 +83,7 @@ class CCPiece {
|
|
|
82
83
|
f;
|
|
83
84
|
a;
|
|
84
85
|
b;
|
|
86
|
+
noise;
|
|
85
87
|
c;
|
|
86
88
|
h;
|
|
87
89
|
values = new Float64Array(CC_MAX + 1);
|
|
@@ -89,10 +91,13 @@ class CCPiece {
|
|
|
89
91
|
N = 0;
|
|
90
92
|
integral = 0;
|
|
91
93
|
error = Infinity;
|
|
92
|
-
constructor(f, a, b
|
|
94
|
+
constructor(f, a, b,
|
|
95
|
+
/** Relative noise of the values of f (the error estimate does not go below it) */
|
|
96
|
+
noise) {
|
|
93
97
|
this.f = f;
|
|
94
98
|
this.a = a;
|
|
95
99
|
this.b = b;
|
|
100
|
+
this.noise = noise;
|
|
96
101
|
this.c = 0.5 * (a + b);
|
|
97
102
|
this.h = 0.5 * (b - a);
|
|
98
103
|
}
|
|
@@ -111,19 +116,23 @@ class CCPiece {
|
|
|
111
116
|
const res = chebyshev(v, N);
|
|
112
117
|
this.N = N;
|
|
113
118
|
this.integral = res.integral * this.h;
|
|
114
|
-
// Truncation error is about trailing coefficient × Σ 2/m^2 ≈ trailing × 4/N. Do not go below
|
|
115
|
-
this.error = Math.max((res.tail * 4) / N,
|
|
119
|
+
// Truncation error is about trailing coefficient × Σ 2/m^2 ≈ trailing × 4/N. Do not go below the noise of the values
|
|
120
|
+
this.error = Math.max((res.tail * 4) / N, this.noise * res.scale) * Math.abs(this.h);
|
|
116
121
|
}
|
|
117
122
|
}
|
|
118
123
|
/**
|
|
119
124
|
* Integrate f over the sequence of intervals edges (ascending). First compute each interval with 16 points to
|
|
120
125
|
* estimate the magnitude of the integral, then double the points until each interval's error estimate is at most
|
|
121
|
-
* relTol × |total| / (number of intervals) (bisecting if 128 points are not enough).
|
|
126
|
+
* relTol × |total| / (number of intervals) (bisecting if 128 points are not enough). noise is the relative error of
|
|
127
|
+
* the values of f: an interval whose error estimate is at that level is accepted, since refining it cannot make the
|
|
128
|
+
* estimate smaller (the noise shows up in the trailing coefficients at any width). Without it, an integrand computed
|
|
129
|
+
* as exp(lf - M) with |lf| in the thousands (noise ε |lf|) was bisected down to the round limit at every call. The
|
|
130
|
+
* number of intervals is bounded as well, so that the work stays bounded whatever the integrand.
|
|
122
131
|
*/
|
|
123
|
-
function integrate(f, edges, relTol) {
|
|
132
|
+
function integrate(f, edges, relTol, noise = 4 * EPS) {
|
|
124
133
|
let pieces = [];
|
|
125
134
|
for (let i = 0; i + 1 < edges.length; i++) {
|
|
126
|
-
const p = new CCPiece(f, edges[i], edges[i + 1]);
|
|
135
|
+
const p = new CCPiece(f, edges[i], edges[i + 1], noise);
|
|
127
136
|
p.advance(16);
|
|
128
137
|
pieces.push(p);
|
|
129
138
|
}
|
|
@@ -133,9 +142,10 @@ function integrate(f, edges, relTol) {
|
|
|
133
142
|
total += p.integral;
|
|
134
143
|
const tol = (relTol * Math.abs(total)) / pieces.length;
|
|
135
144
|
let done = true;
|
|
145
|
+
let count = pieces.length;
|
|
136
146
|
const next = [];
|
|
137
147
|
for (const p of pieces) {
|
|
138
|
-
if (p.error <= tol || p.error <=
|
|
148
|
+
if (p.error <= tol || p.error <= noise * Math.abs(p.integral)) {
|
|
139
149
|
next.push(p);
|
|
140
150
|
continue;
|
|
141
151
|
}
|
|
@@ -144,9 +154,13 @@ function integrate(f, edges, relTol) {
|
|
|
144
154
|
p.advance(2 * p.N);
|
|
145
155
|
next.push(p);
|
|
146
156
|
}
|
|
157
|
+
else if (count >= MAX_PIECES) {
|
|
158
|
+
next.push(p);
|
|
159
|
+
}
|
|
147
160
|
else {
|
|
148
|
-
|
|
149
|
-
const
|
|
161
|
+
count++;
|
|
162
|
+
const l = new CCPiece(f, p.a, p.c, noise);
|
|
163
|
+
const r = new CCPiece(f, p.c, p.b, noise);
|
|
150
164
|
l.advance(16);
|
|
151
165
|
r.advance(16);
|
|
152
166
|
next.push(l, r);
|
|
@@ -408,7 +422,8 @@ function logPeakIntegrate(lf, lo, hi) {
|
|
|
408
422
|
const left = end(-1);
|
|
409
423
|
const right = end(1);
|
|
410
424
|
const pts = [...new Set([left, top, right])].sort((x, y) => x - y);
|
|
411
|
-
|
|
425
|
+
// lf is a sum of terms of its own size, so its values carry an absolute error of a few ε |M|
|
|
426
|
+
return M + Math.log(integrate((x) => Math.exp(lf(x) - M), pts, INNER_TOL, 4 * EPS * Math.max(1, Math.abs(M))));
|
|
412
427
|
}
|
|
413
428
|
// ---- Inner integral: the range distribution function W(w) and its complement ----
|
|
414
429
|
// The integrand vanishes as φ(z) outside z. W uses [-10, 10]; 1 - W uses [-10, w + 10], which contains the peak at z ≈ w/2
|
|
@@ -486,6 +501,11 @@ function logIntegrate(lf, edges) {
|
|
|
486
501
|
M = Math.max(M, lf(0.5 * (edges[i] + edges[i + 1])));
|
|
487
502
|
if (M === -Infinity)
|
|
488
503
|
return -Infinity;
|
|
504
|
+
// When the rounding of lf (ε |M|) exceeds 1, exp(lf - M) is noise, and the log of the integral (the log of the peak
|
|
505
|
+
// width, a few dozen at most) is below the rounding of M anyway (huge df with large q: ptukey(2.3e11, 2, 4.8e27,
|
|
506
|
+
// { lowerTail: false, logP: true }) is about -1.3e22)
|
|
507
|
+
if (EPS * Math.abs(M) > 1)
|
|
508
|
+
return M;
|
|
489
509
|
for (let it = 0; it < 4; it++) {
|
|
490
510
|
let seen = M;
|
|
491
511
|
const v = integrate((x) => {
|
|
@@ -493,7 +513,9 @@ function logIntegrate(lf, edges) {
|
|
|
493
513
|
if (l > seen)
|
|
494
514
|
seen = l;
|
|
495
515
|
return Math.exp(l - M);
|
|
496
|
-
}, edges, REL_TOL
|
|
516
|
+
}, edges, REL_TOL,
|
|
517
|
+
// The absolute error of lf (the inner log integral included) is a few ε |M|
|
|
518
|
+
8 * EPS * Math.max(1, Math.abs(M)));
|
|
497
519
|
if (Number.isFinite(v) && seen - M < 300)
|
|
498
520
|
return M + Math.log(v);
|
|
499
521
|
M = seen;
|
|
@@ -656,12 +678,23 @@ function rangeMaxCdf(w, k, r, upper) {
|
|
|
656
678
|
return r === 1 ? Wc : -Math.expm1(r * Math.log1p(-Wc));
|
|
657
679
|
return 1 - Math.min(rangeCdfFast(w, k), 1) ** r;
|
|
658
680
|
}
|
|
681
|
+
/**
|
|
682
|
+
* log G(w) (log(1 - G(w)) if upper). From the interpolants where they apply, and integrating directly only where 1 - W
|
|
683
|
+
* underflows: the outer log integral evaluates this at a few hundred w, and a direct inner integral at each made
|
|
684
|
+
* ptukey(1e-20, 200, 5) and ptukey(2.5e118, 4, 3, { nranges: 2 }) take minutes.
|
|
685
|
+
*/
|
|
659
686
|
function logRangeMaxCdf(w, k, r, upper) {
|
|
660
687
|
if (!upper)
|
|
661
|
-
return r *
|
|
662
|
-
const
|
|
688
|
+
return r * (Math.log(w) < X_HI_W ? logRangeCdfFast(w, k) : Math.log1p(-Math.min(rangeCdfCFast(w, k), 1)));
|
|
689
|
+
const Wc = Math.min(rangeCdfCFast(w, k), 1);
|
|
690
|
+
const lWc = Wc > 1e-290 ? Math.log(Wc) : logRangeCdfC(w, k);
|
|
663
691
|
if (r === 1)
|
|
664
692
|
return lWc;
|
|
693
|
+
// 1 - (1 - Wc)^r = r Wc (1 - (r - 1) Wc/2 + ...): log r + log Wc where Wc is below e^-700 (the form below was
|
|
694
|
+
// -Infinity once Wc underflowed, from q = 54.5 at k = 3, r = 2, and the outer integral over that edge was bisected to
|
|
695
|
+
// its limit at seconds per call: qtukey(1.5e-287, 3, 4.7e10, { nranges: 2, lowerTail: false }))
|
|
696
|
+
if (lWc < -700)
|
|
697
|
+
return Math.log(r) + lWc;
|
|
665
698
|
// 1 - (1 - Wc)^r
|
|
666
699
|
return Math.log(-Math.expm1(r * Math.log1p(-Math.exp(lWc))));
|
|
667
700
|
}
|
|
@@ -671,29 +704,28 @@ function logRangeMaxCdf(w, k, r, upper) {
|
|
|
671
704
|
* Integrate over u, the log of t = df s^2 / 2 ~ Gamma(a = df/2): ∫ a dpois(a; e^u) G(q sqrt(e^u / a)) du
|
|
672
705
|
*/
|
|
673
706
|
function tukeyIntegral(q, k, df, r, upper) {
|
|
674
|
-
if (df === Infinity)
|
|
707
|
+
if (df === Infinity || dfIsInfinite(q, k, df, r))
|
|
675
708
|
return rangeMaxCdf(q, k, r, upper);
|
|
676
709
|
const a = df / 2;
|
|
677
|
-
const
|
|
678
|
-
const dens = gammaDensityAt(a);
|
|
710
|
+
const logDens = logGammaDensityAt(a);
|
|
679
711
|
// Integrate over v = u - log a. For large df the peak width 1/√a is small relative to |u|, and rounding of the
|
|
680
712
|
// u nodes (ulp(log a)) becomes noise in the integrand (1e-14 relative at df = 1e5). v is near 0, so rounding is small
|
|
681
713
|
const f = (v) => {
|
|
682
|
-
const
|
|
683
|
-
return d === 0 ? 0 : d * rangeMaxCdf(q *
|
|
714
|
+
const d = Math.exp(logDens(v));
|
|
715
|
+
return d === 0 ? 0 : d * rangeMaxCdf(q * Math.exp(0.5 * v), k, r, upper);
|
|
684
716
|
};
|
|
685
|
-
return integrate(f, outerEdges(q, k, df, r, upper)
|
|
717
|
+
return integrate(f, outerEdges(q, k, df, r, upper), REL_TOL);
|
|
686
718
|
}
|
|
687
719
|
/**
|
|
688
|
-
*
|
|
689
|
-
* Loader's form a exp(-stirlerr(a) - bd0(a, t)) / sqrt(2π a), with bd0(a, t) = a (e^v - 1 - v)
|
|
690
|
-
*
|
|
691
|
-
*
|
|
692
|
-
*
|
|
720
|
+
* log density of Gamma(a) at v = log(t/a), i.e. log(t f(t)) = log(a dpois(a; t)); then s = sqrt(t/a) = e^(v/2).
|
|
721
|
+
* Loader's form a exp(-stirlerr(a) - bd0(a, t)) / sqrt(2π a), with bd0(a, t) = a (e^v - 1 - v) computed directly
|
|
722
|
+
* from v (e^v - 1 - v by series for |v| < 1/2). Rounding t = a e^v before forming the density would, for large df
|
|
723
|
+
* where the slope of the density in t (a - t) is large, turn the rounding into noise in the integrand and disturb
|
|
724
|
+
* convergence (1e-14 relative at df = 1e5; for df above about 1e30 the whole peak lies within one ulp of log a).
|
|
725
|
+
* The absolute error of the exponent is about ε × bd0.
|
|
693
726
|
*/
|
|
694
|
-
function
|
|
695
|
-
const
|
|
696
|
-
const c0 = Math.sqrt(a / (2 * Math.PI));
|
|
727
|
+
function logGammaDensityAt(a) {
|
|
728
|
+
const c = 0.5 * Math.log(a / (2 * Math.PI)) - stirlerr(a);
|
|
697
729
|
return (v) => {
|
|
698
730
|
let phi;
|
|
699
731
|
if (Math.abs(v) < 0.5) {
|
|
@@ -709,26 +741,30 @@ function gammaDensityAt(a) {
|
|
|
709
741
|
else {
|
|
710
742
|
phi = Math.expm1(v) - v;
|
|
711
743
|
}
|
|
712
|
-
return
|
|
744
|
+
return c - a * phi;
|
|
713
745
|
};
|
|
714
746
|
}
|
|
747
|
+
/**
|
|
748
|
+
* Whether df is large enough that P equals its df = ∞ value to double precision. With log s ~ N(0, 1/(2a)) the
|
|
749
|
+
* relative change of E[G(q s)] from G(q) is about L^2 / (8a), L = d log G / d log q, which is at most about
|
|
750
|
+
* (k-1) r in the lower tail and q^2 in the upper
|
|
751
|
+
*/
|
|
752
|
+
function dfIsInfinite(q, k, df, r) {
|
|
753
|
+
const L = Math.max((k - 1) * r, q * q, 1);
|
|
754
|
+
return L * L < 1e-17 * df;
|
|
755
|
+
}
|
|
715
756
|
function logTukeyIntegral(q, k, df, r, upper) {
|
|
716
|
-
if (df === Infinity)
|
|
757
|
+
if (df === Infinity || dfIsInfinite(q, k, df, r))
|
|
717
758
|
return logRangeMaxCdf(q, k, r, upper);
|
|
718
|
-
const
|
|
719
|
-
const
|
|
720
|
-
const lga1 = lgamma(a + 1);
|
|
759
|
+
const logDens = logGammaDensityAt(df / 2);
|
|
760
|
+
const lq = Math.log(q);
|
|
721
761
|
// Stay on the log scale so that the deep upper tail (extremely large q), where t = e^u underflows, can be computed
|
|
722
|
-
const lf = (
|
|
723
|
-
const t = Math.exp(u);
|
|
724
|
-
const ld = t > 1e-300 ? dpoisRaw(a, t, true) : a * u - t - lga1;
|
|
725
|
-
return la + ld + logRangeMaxCdf(Math.exp(Math.log(q) + 0.5 * (u - la)), k, r, upper);
|
|
726
|
-
};
|
|
762
|
+
const lf = (v) => logDens(v) + logRangeMaxCdf(Math.exp(lq + 0.5 * v), k, r, upper);
|
|
727
763
|
return logIntegrate(lf, outerEdges(q, k, df, r, upper, true));
|
|
728
764
|
}
|
|
729
765
|
/**
|
|
730
|
-
* Endpoints of the intervals for the outer integral
|
|
731
|
-
* The Gamma(a) density concentrates around
|
|
766
|
+
* Endpoints of the intervals for the outer integral, in v = log(t / a).
|
|
767
|
+
* The Gamma(a) density concentrates around v = 0 with width 1/√a; the left tail decays like t^a and the right
|
|
732
768
|
* like e^-t. Multiplying by G(q s) moves the peak: in the lower tail G ≈ (q s)^((k-1) r) as q s → 0, which moves it
|
|
733
769
|
* toward larger t (equivalent to the exponent growing from a to a + (k-1) r / 2); in the upper tail
|
|
734
770
|
* 1 - G ≈ exp(-(q s)^2 / 4), equivalent to the rate growing to 1 + q^2 / (4a), so the peak moves to
|
|
@@ -739,29 +775,30 @@ function outerEdges(q, k, df, r, upper, deep = false) {
|
|
|
739
775
|
const a = df / 2;
|
|
740
776
|
// Peak of Gamma(a) before multiplying by G, and the peak it moves to when G matters in the tail (shape, center)
|
|
741
777
|
let shape = a;
|
|
742
|
-
let center
|
|
778
|
+
let center;
|
|
743
779
|
if (upper) {
|
|
744
780
|
const lq2 = 2 * Math.log(q) - Math.log(4 * a);
|
|
745
|
-
center
|
|
781
|
+
center = -(lq2 > 30 ? lq2 : Math.log1p(Math.exp(lq2)));
|
|
746
782
|
}
|
|
747
783
|
else {
|
|
748
784
|
shape = a + ((k - 1) * r) / 2;
|
|
749
|
-
center = Math.
|
|
785
|
+
center = Math.log1p(((k - 1) * r) / (2 * a));
|
|
750
786
|
}
|
|
751
787
|
// Where the density is about 1e-18 of the peak (decaying like t^shape on the left and e^-t on the right)
|
|
752
788
|
const range = (c, sh) => [
|
|
753
789
|
c - 9 / Math.sqrt(sh) - 41 / sh,
|
|
754
790
|
c + 9 / Math.sqrt(sh) + Math.log1p(41 / sh),
|
|
755
791
|
];
|
|
756
|
-
const [lo1, hi1] = range(
|
|
792
|
+
const [lo1, hi1] = range(0, a);
|
|
757
793
|
const [lo2, hi2] = range(center, shape);
|
|
758
|
-
// The linear version is limited to where e^u does not underflow (beyond that the result is below 1e-290 and is
|
|
759
|
-
|
|
794
|
+
// The linear version is limited to where t = e^u does not underflow (beyond that the result is below 1e-290 and is
|
|
795
|
+
// recomputed with the log version)
|
|
796
|
+
const lo = deep ? Math.min(lo1, lo2) : Math.max(Math.min(lo1, lo2), -740 - Math.log(a));
|
|
760
797
|
const hi = Math.max(hi1, hi2);
|
|
761
798
|
// Split into a central interval containing both peaks (±4 sd) and left and right tail intervals
|
|
762
799
|
const sd1 = 1 / Math.sqrt(a);
|
|
763
800
|
const sd2 = 1 / Math.sqrt(shape);
|
|
764
|
-
const edges = [lo, Math.min(
|
|
801
|
+
const edges = [lo, Math.min(-4 * sd1, center - 4 * sd2), Math.max(4 * sd1, center + 4 * sd2), hi];
|
|
765
802
|
return [...new Set(edges.map((x) => Math.min(Math.max(x, lo), hi)))].sort((x, y) => x - y);
|
|
766
803
|
}
|
|
767
804
|
// ---- Public functions ----
|
|
@@ -774,6 +811,15 @@ function validParams(k, df, r) {
|
|
|
774
811
|
* Calls with the same (q, nmeans, df, nranges) differing only in tail or log use the same integral, so the last result is cached.
|
|
775
812
|
*/
|
|
776
813
|
function ptukeyRaw(q, k, df, r, lowerTail, logP) {
|
|
814
|
+
if (q < TINY_Q) {
|
|
815
|
+
// The lower tail is c q^((k-1) r) (1 + O(q²)) there (W(w) is of the order of w^(k-1) near 0): scaled in logs from
|
|
816
|
+
// q = 2^-1000. The integrals at a subnormal q took 20 to 50 ms a call (the subdivision chasing the rounding of the
|
|
817
|
+
// subnormal arguments), so that qtukey(-726.9, 2, 486.9, { logP: true }), whose answer is 3.5e-316, took 1.2 s
|
|
818
|
+
const lv = ptukeyRaw(TINY_Q, k, df, r, true, true) + (k - 1) * r * Math.log(q * 2 ** 1000);
|
|
819
|
+
if (lowerTail)
|
|
820
|
+
return logP ? lv : Math.exp(lv);
|
|
821
|
+
return logP ? -Math.exp(lv) : -Math.expm1(lv);
|
|
822
|
+
}
|
|
777
823
|
let t;
|
|
778
824
|
if (memo !== undefined && memo.q === q && memo.k === k && memo.df === df && memo.r === r)
|
|
779
825
|
t = memo.tail;
|
|
@@ -786,6 +832,8 @@ function ptukeyRaw(q, k, df, r, lowerTail, logP) {
|
|
|
786
832
|
return logP ? Math.log1p(-t.value) : 1 - t.value;
|
|
787
833
|
}
|
|
788
834
|
let memo;
|
|
835
|
+
/** Below this (2^-1000) the lower tail of ptukeyRaw is scaled from its value here */
|
|
836
|
+
const TINY_Q = 2 ** -1000;
|
|
789
837
|
/**
|
|
790
838
|
* The smaller tail. Which tail is smaller is guessed from the df = ∞ value (one inner integral); if the guess is
|
|
791
839
|
* wrong and the value exceeds 1/2, the other side is computed. preferUpper is the side to compute when there is no
|
|
@@ -867,12 +915,24 @@ function solveMonotone(g, y0, increasing) {
|
|
|
867
915
|
let step = 0.05;
|
|
868
916
|
let b = a + dir * step;
|
|
869
917
|
let fb = g(b);
|
|
918
|
+
// A point past the range of doubles (e^y underflows to 0 or overflows), where g is not finite. Once one is found,
|
|
919
|
+
// steps go halfway to it instead of past it; a zero beyond the doubles ends at the last finite point
|
|
920
|
+
let bad = NaN;
|
|
870
921
|
for (let i = 0; i < 200 && Math.sign(fb) === Math.sign(fa) && fb !== 0; i++) {
|
|
922
|
+
if (Math.abs(bad - b) < 1e-3)
|
|
923
|
+
return b;
|
|
871
924
|
a = b;
|
|
872
925
|
fa = fb;
|
|
873
926
|
step *= 2;
|
|
874
|
-
b = a + dir * step;
|
|
927
|
+
b = (a + dir * step - bad) * dir >= 0 ? 0.5 * (a + bad) : a + dir * step;
|
|
875
928
|
fb = g(b);
|
|
929
|
+
while (!Number.isFinite(fb)) {
|
|
930
|
+
bad = b;
|
|
931
|
+
if (Math.abs(bad - a) < 1e-3)
|
|
932
|
+
return a;
|
|
933
|
+
b = 0.5 * (a + bad);
|
|
934
|
+
fb = g(b);
|
|
935
|
+
}
|
|
876
936
|
}
|
|
877
937
|
if (fb === 0)
|
|
878
938
|
return b;
|
|
@@ -31,6 +31,8 @@ export function punif1(q, min, max, lowerTail, logP) {
|
|
|
31
31
|
const h = halfIfOverflow(min, max);
|
|
32
32
|
const w = max * h - min * h;
|
|
33
33
|
const v = lowerTail ? (q * h - min * h) / w : (max * h - q * h) / w;
|
|
34
|
+
// log(v) even near probability 1, where log1p(-(other tail)) would keep more digits: R does the same
|
|
35
|
+
// (punif(1e-300, lower.tail = FALSE, log.p = TRUE) is 0 there)
|
|
34
36
|
return logP ? Math.log(v) : v;
|
|
35
37
|
}
|
|
36
38
|
export function qunif1(p, min, max, lowerTail, logP) {
|
|
@@ -45,15 +47,18 @@ export function qunif1(p, min, max, lowerTail, logP) {
|
|
|
45
47
|
return min;
|
|
46
48
|
if (b !== undefined)
|
|
47
49
|
return b;
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
// Lower tail: min + p (max - min). Upper tail with p < 1/2: max - p (max - min), which keeps the digits of a small p
|
|
51
|
+
// (1 - p would lose them); otherwise min + (1 - p)(max - min) with 1 - p exact (or -expm1(log p)), as R gives
|
|
52
|
+
const half = halfIfOverflow(min, max) !== 1;
|
|
53
|
+
const at = (q) => (half ? (1 - q) * min + q * max : min + q * (max - min));
|
|
54
|
+
// (exp(p) is rounded on the subnormal grid for a log p below -708, which the range can magnify back into the normal
|
|
55
|
+
// range; kept so, as R does, and as punif takes the log of the subnormal ratio (test/helpers/distributionKnown.ts))
|
|
56
|
+
const pp = logP ? Math.exp(p) : p;
|
|
57
|
+
if (lowerTail)
|
|
58
|
+
return at(pp);
|
|
59
|
+
if (pp < 0.5)
|
|
60
|
+
return half ? pp * min + (1 - pp) * max : max - pp * (max - min);
|
|
61
|
+
return at(logP ? -Math.expm1(p) : 0.5 - p + 0.5);
|
|
57
62
|
}
|
|
58
63
|
/**
|
|
59
64
|
* Density of the uniform distribution. Equivalent to R's `dunif(x, min = 0, max = 1, log = FALSE)`.
|
|
@@ -7,15 +7,35 @@
|
|
|
7
7
|
// R's output (where Inf and NaN appear).
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, quantileBoundaries } from "../internal/dpq.js";
|
|
10
|
-
import { ddExp, expNeg, log1mExp, logOneMinusExpNeg, standardizeDD } from "../internal/ddexp.js";
|
|
11
|
-
import { ddLog, ddMulNum, ddSub } from "../special/dd.js";
|
|
10
|
+
import { ddExp, expNeg, expNegOver, log1mExp, logOneMinusExpNeg, standardizeDD } from "../internal/ddexp.js";
|
|
11
|
+
import { ddDivNum, ddLog, ddMulNum, ddSub } from "../special/dd.js";
|
|
12
12
|
/** 1^y = 1, as with C's pow (JS Math.pow(1, Infinity) is NaN) */
|
|
13
13
|
function cpow(a, b) {
|
|
14
14
|
return a === 1 ? 1 : Math.pow(a, b);
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
17
|
+
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
18
|
+
/**
|
|
19
|
+
* log u (u = x / scale) in double-double. x > 0, finite parameters. When x / scale is subnormal, 0 or overflows,
|
|
20
|
+
* log x - log scale instead; also for x above 1e300, where the exact remainder in standardizeDD overflows
|
|
21
|
+
*/
|
|
17
22
|
function logU(x, scale) {
|
|
18
|
-
|
|
23
|
+
if (scale === 1)
|
|
24
|
+
return ddLog([x, 0]);
|
|
25
|
+
const u = x / scale;
|
|
26
|
+
if (u >= MIN_NORMAL && u < Infinity && x < 1e300)
|
|
27
|
+
return ddLog(standardizeDD(x, 0, scale));
|
|
28
|
+
return ddSub(ddLog([x, 0]), ddLog([scale, 0]));
|
|
29
|
+
}
|
|
30
|
+
/** a s in double-double; only the double product when s is beyond the range of Veltkamp splitting or it overflows */
|
|
31
|
+
function mulLarge(a, s) {
|
|
32
|
+
const p = a[0] * s;
|
|
33
|
+
return s < 1e300 && Number.isFinite(p) ? ddMulNum(a, s) : [p, 0];
|
|
34
|
+
}
|
|
35
|
+
/** log(a / b) for positive finite a, b, also when a / b over- or underflows */
|
|
36
|
+
function logRatio(a, b) {
|
|
37
|
+
const r = a / b;
|
|
38
|
+
return r >= MIN_NORMAL && r < Infinity ? Math.log(r) : Math.log(a) - Math.log(b);
|
|
19
39
|
}
|
|
20
40
|
function allFinite(...v) {
|
|
21
41
|
return v.every(Number.isFinite);
|
|
@@ -41,14 +61,24 @@ export function dweibull1(x, shape, scale, log) {
|
|
|
41
61
|
return log ? -Math.log(scale) : 1 / scale;
|
|
42
62
|
}
|
|
43
63
|
const lu = logU(x, scale);
|
|
44
|
-
const t = ddExp(
|
|
64
|
+
const t = ddExp(mulLarge(lu, shape));
|
|
45
65
|
if (t[0] === Infinity)
|
|
46
66
|
return d0(log);
|
|
47
67
|
// E = (shape - 1) log u - t
|
|
48
|
-
const
|
|
68
|
+
const m = shape === 1 ? [0, 0] : mulLarge(lu, shape - 1);
|
|
69
|
+
// (-Infinity where (shape - 1) log u overflows, which the double-double difference made NaN: dweibull(1.9e-255,
|
|
70
|
+
// 6.6e306, scale 98.6) was NaN)
|
|
71
|
+
const e = m[0] === -Infinity ? m : ddSub(m, t);
|
|
72
|
+
const r = shape / scale;
|
|
49
73
|
if (log)
|
|
50
|
-
return
|
|
51
|
-
|
|
74
|
+
return logRatio(shape, scale) + e[0] + e[1];
|
|
75
|
+
// |log(shape / scale)| < 1455, so the density underflows; for a huge shape e[1] can exceed 1 and flip the sign below
|
|
76
|
+
if (e[0] < -2500)
|
|
77
|
+
return 0;
|
|
78
|
+
if (r >= MIN_NORMAL && r < Infinity && e[0] > -708)
|
|
79
|
+
return r * Math.exp(e[0]) * (1 + e[1]);
|
|
80
|
+
// shape / scale over- or underflows, or exp(E) is subnormal: fold it into the exponent
|
|
81
|
+
return Math.exp(logRatio(shape, scale) + e[0]) * (1 + e[1]);
|
|
52
82
|
}
|
|
53
83
|
export function pweibull1(q, shape, scale, lowerTail, logP) {
|
|
54
84
|
if (Number.isNaN(q) || Number.isNaN(shape) || Number.isNaN(scale))
|
|
@@ -64,7 +94,12 @@ export function pweibull1(q, shape, scale, lowerTail, logP) {
|
|
|
64
94
|
return NaN;
|
|
65
95
|
}
|
|
66
96
|
else {
|
|
67
|
-
|
|
97
|
+
const l = mulLarge(logU(q, scale), shape);
|
|
98
|
+
t = ddExp(l);
|
|
99
|
+
// log(1 - exp(-t)) = log t - t / 2 + ...: below e^-700 it is log t, which stays exact where t is subnormal. Where
|
|
100
|
+
// t underflows to 0 the result is -Infinity, as in R (pweibull(1e-5, 1000, log.p = TRUE) is -Inf)
|
|
101
|
+
if (lowerTail && logP && l[0] < -700)
|
|
102
|
+
return t[0] === 0 ? -Infinity : l[0] + l[1];
|
|
68
103
|
}
|
|
69
104
|
if (!lowerTail)
|
|
70
105
|
return logP ? -t[0] - t[1] : expNeg(t);
|
|
@@ -80,12 +115,32 @@ export function qweibull1(p, shape, scale, lowerTail, logP) {
|
|
|
80
115
|
if (b !== undefined)
|
|
81
116
|
return b;
|
|
82
117
|
// -log P[X > x] = t
|
|
118
|
+
// (an infinite shape or scale is left to the forms below, which give R's NaN for Infinity times 0)
|
|
119
|
+
if (logP && lowerTail && p < LOG_MIN_NORMAL && allFinite(shape, scale)) {
|
|
120
|
+
// -log(1 - P) is P = exp(p) to the double, but rounded on the subnormal grid there: x = scale exp(p / shape), with
|
|
121
|
+
// p / shape in double-double (qweibull(-720.6, 1, { scale: 1.36e257, logP: true }) was 5e-15 off)
|
|
122
|
+
// (in double where the shape or the quotient is beyond the range of Veltkamp splitting)
|
|
123
|
+
const q = shape < 1e300 && Math.abs(p / shape) < 1e300 ? ddDivNum([p, 0], shape) : [p / shape, 0];
|
|
124
|
+
const e = ddExp(q);
|
|
125
|
+
const v = e[0] + e[1];
|
|
126
|
+
if (v >= MIN_NORMAL && v < Infinity)
|
|
127
|
+
return scale * v;
|
|
128
|
+
// (exp(p / shape) itself below the normal range: divided by 1 / scale without rounding it there)
|
|
129
|
+
const r = 1 / scale;
|
|
130
|
+
if (r >= MIN_NORMAL && r < Infinity && q[0] < 0)
|
|
131
|
+
return expNegOver([-q[0], -q[1]], r);
|
|
132
|
+
return Math.exp(Math.log(scale) + q[0]);
|
|
133
|
+
}
|
|
83
134
|
let lu;
|
|
84
135
|
if (logP)
|
|
85
136
|
lu = lowerTail ? log1mExp(p) : p;
|
|
86
137
|
else
|
|
87
138
|
lu = lowerTail ? Math.log1p(-p) : Math.log(p);
|
|
88
|
-
|
|
139
|
+
const v = cpow(-lu, 1 / shape);
|
|
140
|
+
if ((v >= MIN_NORMAL && v < Infinity) || !allFinite(shape, scale) || v === 1)
|
|
141
|
+
return scale * v;
|
|
142
|
+
// (-lu)^(1 / shape) is subnormal, 0 or overflows although scale times it may not be: in logs
|
|
143
|
+
return Math.exp(Math.log(scale) + Math.log(-lu) / shape);
|
|
89
144
|
}
|
|
90
145
|
/**
|
|
91
146
|
* Density of the Weibull distribution. Equivalent to R's `dweibull(x, shape, scale = 1, log = FALSE)`.
|