@numero-uno-jpn/rstats 0.15.2 → 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 +114 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/chunks/{chunk-PEYGUK32.js → chunk-3THFTB36.js} +2 -2
- package/dist/chunks/chunk-3TSWYE5L.js +2 -0
- package/dist/chunks/{chunk-IBPWRQIF.js → chunk-3WPU6LGK.js} +2 -2
- package/dist/chunks/{chunk-VWJ74UHC.js → chunk-5ITR67MB.js} +2 -2
- package/dist/chunks/chunk-5NRFU6F6.js +2 -0
- package/dist/chunks/{chunk-XNPU54NH.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-HBMHR5SS.js → chunk-HD73UKPT.js} +2 -2
- package/dist/chunks/{chunk-4Z2XDWKV.js → chunk-IUULP3LE.js} +1 -1
- package/dist/chunks/{chunk-Q4PGYESP.js → chunk-JQG3YQOC.js} +2 -2
- package/dist/chunks/{chunk-LG74QY5U.js → chunk-KT66SB7R.js} +1 -1
- package/dist/chunks/{chunk-SREPSVTT.js → chunk-MAUAOQG3.js} +2 -2
- package/dist/chunks/{chunk-YVREYVI2.js → chunk-MDYE7QP2.js} +2 -2
- package/dist/chunks/{chunk-XPLDEK6D.js → chunk-NM3Z2UBQ.js} +2 -2
- package/dist/chunks/{chunk-AJBLCNZ7.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-3WQCKGKP.js → chunk-USPEBXSR.js} +2 -2
- package/dist/chunks/{chunk-PWVQVJTP.js → chunk-VLUL2HFW.js} +1 -1
- package/dist/chunks/{chunk-YGT4P7EU.js → chunk-ZIX7NYQF.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +11 -1
- package/dist/distributions/cauchy.js +76 -5
- package/dist/distributions/exponential.js +6 -1
- package/dist/distributions/f.js +41 -5
- package/dist/distributions/gamma.js +34 -3
- package/dist/distributions/geometric.js +4 -1
- package/dist/distributions/invgamma.js +4 -0
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +5 -2
- package/dist/distributions/logistic.js +8 -2
- package/dist/distributions/lognormal.js +12 -1
- package/dist/distributions/nbeta.js +66 -24
- package/dist/distributions/nchisq.js +58 -11
- package/dist/distributions/negbinomial.js +6 -1
- package/dist/distributions/normal.js +26 -1
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +145 -19
- package/dist/distributions/pareto.js +5 -0
- package/dist/distributions/t.js +55 -3
- package/dist/distributions/triangular.js +35 -8
- package/dist/distributions/tukey.js +16 -0
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +23 -3
- package/dist/distributions.js +2 -2
- package/dist/internal/ddexp.d.ts +8 -0
- package/dist/internal/ddexp.js +27 -0
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +10 -5
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- 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/gamma.js +4 -0
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +70 -6
- package/dist/special/incbetLargeA.js +7 -2
- package/dist/special/incbetPrefactor.d.ts +5 -0
- package/dist/special/incbetPrefactor.js +14 -3
- package/dist/special/loader.js +70 -21
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
package/dist/distributions/nt.js
CHANGED
|
@@ -35,7 +35,8 @@ import { erfcxNonNegative } from "../special/erf.js";
|
|
|
35
35
|
import { gammaInc } from "../special/igam.js";
|
|
36
36
|
import { dpoisRaw, stirlerr } from "../special/loader.js";
|
|
37
37
|
import { lgamma1p } from "../special/gamma.js";
|
|
38
|
-
import {
|
|
38
|
+
import { qgammaStd } from "./gamma.js";
|
|
39
|
+
import { dnorm, pnorm, qnorm } from "./normal.js";
|
|
39
40
|
const LN_SQRT_2PI = 0.918938533204672741780329736406;
|
|
40
41
|
const SQRT_2_PI = 0.7978845608028654; // sqrt(2/pi)
|
|
41
42
|
const LN2_DD = [0.6931471805599453, 2.3190468138462996e-17];
|
|
@@ -103,11 +104,27 @@ function shapeTimesExp(a, w) {
|
|
|
103
104
|
*/
|
|
104
105
|
function gammaLog(a, w, lower) {
|
|
105
106
|
const x = shapeTimesExp(a, w);
|
|
106
|
-
if (x > X_TINY)
|
|
107
|
+
if (x > X_TINY) {
|
|
108
|
+
// Q(a, x) = (a/A_SUB) Q(A_SUB, x) (1 + O(A_SUB log x)) for a < A_SUB: at a subnormal a the log of Q, itself near a,
|
|
109
|
+
// lost digits (7e-5 at a = 1e-320)
|
|
110
|
+
if (!lower && a < A_SUB)
|
|
111
|
+
return [gammaInc(A_SUB, x, false, true) + Math.log(a / A_SUB), dpoisRaw(a, x, true)];
|
|
107
112
|
return [gammaInc(a, x, lower, true), dpoisRaw(a, x, true)];
|
|
108
|
-
|
|
109
|
-
|
|
113
|
+
}
|
|
114
|
+
// log P = a m, m = log x - lgamma1p(a)/a; lgamma1p(a)/a is -γ (1 + O(a)) where lgamma1p(a) would be subnormal
|
|
115
|
+
const m = Math.log(a) + w - (a < A_SUB ? -EULER : lgamma1p(a) / a);
|
|
116
|
+
const lp = a * m;
|
|
117
|
+
if (lower)
|
|
118
|
+
return [lp, lp];
|
|
119
|
+
// log Q = log(-expm1(a m)): log(a) + log(-m) + log1p(lp/2) where a m is so small (it may be subnormal)
|
|
120
|
+
const lq = lp > -1e-10 ? Math.log(a) + Math.log(-m) + Math.log1p(lp / 2) : lp > -Math.LN2 ? Math.log(-Math.expm1(lp)) : Math.log1p(-Math.exp(lp));
|
|
121
|
+
return [lq, lp];
|
|
110
122
|
}
|
|
123
|
+
/** Below this a the upper incomplete gamma function is scaled from Q(A_SUB, x) (gammaLog) */
|
|
124
|
+
const A_SUB = 1e-300;
|
|
125
|
+
const EULER = 0.5772156649015329;
|
|
126
|
+
/** Above this x/(a + 1) the second derivative of log Q(a, x) in w comes from the asymptotic series of Q (logIntegrand) */
|
|
127
|
+
const Q_ASYMPTOTIC = 1e8;
|
|
111
128
|
/** Log of the integrand (double precision, for locating the peak) and its first and second derivatives */
|
|
112
129
|
function logIntegrand(pr, w) {
|
|
113
130
|
const { a, t, delta } = pr;
|
|
@@ -121,11 +138,17 @@ function logIntegrand(pr, w) {
|
|
|
121
138
|
const x = shapeTimesExp(a, w);
|
|
122
139
|
const upperG = pr.kind === "cdfQ";
|
|
123
140
|
const [lG, ld] = gammaLog(a, w, !upperG);
|
|
124
|
-
|
|
141
|
+
// x f(x) / G (with log a inside the exp: for a subnormal a, f(x)/G alone can overflow)
|
|
142
|
+
const xf = Math.exp(Math.log(a) + ld - lG);
|
|
125
143
|
const sgn = upperG ? -1 : 1;
|
|
126
144
|
const L = Math.log(t / 2) + w / 2 - 0.5 * z * z - LN_SQRT_2PI + lG;
|
|
127
145
|
const d1 = 0.5 - (z * tS) / 2 + sgn * xf;
|
|
128
|
-
|
|
146
|
+
// (for Q, the derivative of -xf is xf (x - a - xf), which cancels where x is large: xf = x/(1 + (a-1)/x + ...),
|
|
147
|
+
// from logs near -x, is off by about eps x relative, the difference by eps x² against its value of about -1. From
|
|
148
|
+
// the asymptotic series of Q, x - a - xf = -1 - (a-1)/x up to O(((a+1)/x)²); with the cancelled form the peak
|
|
149
|
+
// search of formula B for a subnormal a and δ = 1e5 stopped short in steps of 1/x, and the log of pt was +Infinity)
|
|
150
|
+
const d2G = upperG && x > Q_ASYMPTOTIC * (a + 1) ? -xf * (1 + (a - 1) / x) : sgn * xf * (a - x) - xf * xf;
|
|
151
|
+
const d2 = -(tS * tS) / 4 - (z * tS) / 4 + d2G;
|
|
129
152
|
return [L, d1, d2];
|
|
130
153
|
}
|
|
131
154
|
let L = pr.logC - a * m;
|
|
@@ -539,7 +562,8 @@ function farTails(t, a, delta, lower) {
|
|
|
539
562
|
const s = Math.sign(t);
|
|
540
563
|
const farLower = s < 0;
|
|
541
564
|
const f0 = pntBoth(s * T0, 2 * a, delta, farLower);
|
|
542
|
-
const
|
|
565
|
+
const r = Math.log(Math.abs(t)) - Math.log(T0);
|
|
566
|
+
const k = 2 * a * r;
|
|
543
567
|
const far = { lin: f0.lin > 0 ? f0.lin * Math.exp(-k) : Math.exp(f0.log - k), log: f0.log - k };
|
|
544
568
|
if (lower === farLower)
|
|
545
569
|
return far;
|
|
@@ -547,7 +571,9 @@ function farTails(t, a, delta, lower) {
|
|
|
547
571
|
return { lin: 1 - far.lin, log: Math.log1p(-far.lin) };
|
|
548
572
|
const n0 = pntBoth(s * T0, 2 * a, delta, !farLower);
|
|
549
573
|
const gain = -Math.expm1(-k);
|
|
550
|
-
|
|
574
|
+
// (log k from log 2a where k is so small that it may be subnormal: for a subnormal a it kept a few bits only)
|
|
575
|
+
const logGain = k < 1e-10 ? Math.log(2 * a) + Math.log(r) - k / 2 : Math.log(gain);
|
|
576
|
+
return { lin: n0.lin + f0.lin * gain, log: logAddExp(n0.log, f0.log + logGain) };
|
|
551
577
|
}
|
|
552
578
|
/**
|
|
553
579
|
* Whether |t| is so small that P[T <= t] = Φ(-δ) and P[T > t] = Φ(δ), their values at t = 0, to double precision.
|
|
@@ -557,7 +583,9 @@ function farTails(t, a, delta, lower) {
|
|
|
557
583
|
* e^w overflows
|
|
558
584
|
*/
|
|
559
585
|
function nearZero(t, a, delta) {
|
|
560
|
-
|
|
586
|
+
// (|t|/sqrt(a) first, and sqrt(y1) as a hypot: y1/a overflowed for a below about 1e-300 with a large δ, and at t = 0
|
|
587
|
+
// the test was NaN, false, so that the integral was taken at t = 0 and gave NaN to qt)
|
|
588
|
+
return (Math.abs(t) / Math.sqrt(a)) * Math.hypot(Math.SQRT2 * delta, Math.sqrt(4 * a + 800)) * (Math.abs(delta) + 2) <= 1e-17;
|
|
561
589
|
}
|
|
562
590
|
/** Linear value and log of P[T <= t] (lower) / P[T > t] (the log may be NaN unless needLog) */
|
|
563
591
|
function pntBoth(t, df, delta, lower, needLog = true) {
|
|
@@ -598,11 +626,71 @@ function dntRaw(x, df, delta, log) {
|
|
|
598
626
|
const v = integrate(problem(a, x, delta, "pdf"), log);
|
|
599
627
|
return log ? v.log : v.lin;
|
|
600
628
|
}
|
|
601
|
-
|
|
602
|
-
|
|
629
|
+
/**
|
|
630
|
+
* Above this df the central and noncentral t are taken from their limits (hugeDf*; shared with t.ts). The t
|
|
631
|
+
* differs from the normal by terms of relative order t²/ν and 1/ν: where y = t²/ν < 1e-17 it is the normal to the
|
|
632
|
+
* double (the absolute error in the log, about t⁴/ν, is below 1e-180 wherever the linear value is above the
|
|
633
|
+
* underflow), and beyond, |t| > 3e91, the log of the far tail and of the density is -(ν/2) log1p(y) up to terms
|
|
634
|
+
* below 1e-179 of it. The general code fails from about 1.3e300 on (NaN where its intermediate terms overflow)
|
|
635
|
+
*/
|
|
636
|
+
export const HUGE_DF = 1e200;
|
|
637
|
+
/** y = t²/ν (t² may overflow on its own) */
|
|
638
|
+
export function hugeDfRatio(t, n) {
|
|
639
|
+
const u = t / Math.sqrt(n);
|
|
640
|
+
return u * u;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Log of the far tail beyond |t| and of the density at t (relative error about eps) for n > HUGE_DF and
|
|
644
|
+
* hugeDfRatio(t, n) >= 1e-17: -(n/2) log1p(t²/n), formed without overflow
|
|
645
|
+
*/
|
|
646
|
+
export function hugeDfTailLog(t, n) {
|
|
647
|
+
const u = Math.abs(t) / Math.sqrt(n);
|
|
648
|
+
// log1p(u²) = 2 log u + log1p(1/u²) where u² is huge (n log u may overflow: the log is then -Infinity, as it is)
|
|
649
|
+
if (u > 1e8)
|
|
650
|
+
return -n * (Math.log(u) + 0.5 * Math.log1p(1 / (u * u)));
|
|
651
|
+
const y = u * u;
|
|
652
|
+
// (n/2) log1p(y) = (t²/2) log1p(y)/y, the ratio by its series for a small y (error y⁴/5)
|
|
653
|
+
const g = y < 1e-5 ? 1 - y * (1 / 2 - y * (1 / 3 - y / 4)) : Math.log1p(y) / y;
|
|
654
|
+
return -0.5 * Math.abs(t) * (Math.abs(t) * g);
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* |t| with hugeDfTailLog(t, n) = logTarget (logTarget < 0), Infinity beyond the largest double: t² = n expm1(-2 logTarget / n).
|
|
658
|
+
* (sqrt(n) is taken apart: through exp(log t) its log, up to 354, would multiply the rounding error)
|
|
659
|
+
*/
|
|
660
|
+
export function hugeDfTailQuantile(logTarget, n) {
|
|
661
|
+
const r = 2 * (-logTarget / n);
|
|
662
|
+
return r < 700 ? Math.sqrt(n) * Math.sqrt(Math.expm1(r)) : Math.sqrt(n) * Math.exp(r / 2);
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* P[T <= t] (lower) / P[T > t] for n > HUGE_DF and hugeDfRatio(t, n) >= 1e-17 from hugeDfTailLog: the tail beyond |t|
|
|
666
|
+
* is its exp (0), the other is 1
|
|
667
|
+
*/
|
|
668
|
+
export function hugeDfTail(t, n, lowerTail, logP) {
|
|
669
|
+
const l = hugeDfTailLog(t, n);
|
|
670
|
+
if (t < 0 === lowerTail)
|
|
671
|
+
return logP ? l : Math.exp(l);
|
|
672
|
+
return logP ? Math.log1p(-Math.exp(l)) : 1 - Math.exp(l);
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Whether the noncentral t at n > HUGE_DF and hugeDfRatio(t, n) >= 1e-17 has the central far tail and density
|
|
676
|
+
* (hugeDfTailLog): δ shifts their log by about δ min(|t|, sqrt(n)), below 1e-19 of it here
|
|
677
|
+
*/
|
|
678
|
+
function hugeDfCentral(t, n, delta) {
|
|
679
|
+
return Math.abs(delta) <= 1e-20 * Math.min(Math.abs(t), Math.sqrt(n));
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* df with a nonzero half: the smallest subnormal, whose half rounds to 0, is taken as twice itself (a = 5e-324). Below
|
|
683
|
+
* about 1e-308 a = df/2 is rounded to the subnormal grid anyway; only the terms of order a beside Φ(∓δ) feel that
|
|
684
|
+
*/
|
|
685
|
+
function halvable(df) {
|
|
686
|
+
return df === Number.MIN_VALUE ? 2 * df : df;
|
|
687
|
+
}
|
|
688
|
+
export function dnt1(x, df0, ncp, log) {
|
|
689
|
+
if (Number.isNaN(x) || Number.isNaN(df0) || Number.isNaN(ncp))
|
|
603
690
|
return NaN;
|
|
604
|
-
if (
|
|
691
|
+
if (df0 <= 0)
|
|
605
692
|
return NaN;
|
|
693
|
+
const df = halvable(df0);
|
|
606
694
|
if (!Number.isFinite(x) || !Number.isFinite(ncp))
|
|
607
695
|
return log ? -Infinity : 0;
|
|
608
696
|
if (df === Infinity) {
|
|
@@ -610,13 +698,22 @@ export function dnt1(x, df, ncp, log) {
|
|
|
610
698
|
const lv = -0.5 * z * z - LN_SQRT_2PI;
|
|
611
699
|
return log ? lv : Math.exp(lv);
|
|
612
700
|
}
|
|
701
|
+
if (df > HUGE_DF) {
|
|
702
|
+
if (hugeDfRatio(x, df) < 1e-17)
|
|
703
|
+
return dnorm(x - ncp, { log });
|
|
704
|
+
if (hugeDfCentral(x, df, ncp)) {
|
|
705
|
+
const lv = hugeDfTailLog(x, df);
|
|
706
|
+
return log ? lv : Math.exp(lv);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
613
709
|
return dntRaw(x, df, ncp, log);
|
|
614
710
|
}
|
|
615
|
-
export function pnt1(q,
|
|
616
|
-
if (Number.isNaN(q) || Number.isNaN(
|
|
711
|
+
export function pnt1(q, df0, ncp, lowerTail, logP) {
|
|
712
|
+
if (Number.isNaN(q) || Number.isNaN(df0) || Number.isNaN(ncp))
|
|
617
713
|
return NaN;
|
|
618
|
-
if (
|
|
714
|
+
if (df0 <= 0)
|
|
619
715
|
return NaN;
|
|
716
|
+
const df = halvable(df0);
|
|
620
717
|
const zero = lowerTail ? (logP ? -Infinity : 0) : logP ? 0 : 1;
|
|
621
718
|
const one = lowerTail ? (logP ? 0 : 1) : logP ? -Infinity : 0;
|
|
622
719
|
if (q === Infinity)
|
|
@@ -632,13 +729,22 @@ export function pnt1(q, df, ncp, lowerTail, logP) {
|
|
|
632
729
|
return pnorm(q - ncp, { lowerTail, logP });
|
|
633
730
|
if (q === 0)
|
|
634
731
|
return pnorm(-ncp, { lowerTail, logP });
|
|
732
|
+
if (df > HUGE_DF) {
|
|
733
|
+
if (hugeDfRatio(q, df) < 1e-17)
|
|
734
|
+
return pnorm(q - ncp, { lowerTail, logP });
|
|
735
|
+
// The far tail beyond |q| (R 4.5.2 gives 0.5 for pt(-4.05e269, 3.586e307, ncp = 1), far in the left tail of
|
|
736
|
+
// what is N(1, 1) to the double; the lower tail is 0 here)
|
|
737
|
+
if (hugeDfCentral(q, df, ncp))
|
|
738
|
+
return hugeDfTail(q, df, lowerTail, logP);
|
|
739
|
+
}
|
|
635
740
|
return pntRaw(q, df, ncp, lowerTail, logP);
|
|
636
741
|
}
|
|
637
|
-
export function qnt1(p,
|
|
638
|
-
if (Number.isNaN(p) || Number.isNaN(
|
|
742
|
+
export function qnt1(p, df0, ncp, lowerTail, logP) {
|
|
743
|
+
if (Number.isNaN(p) || Number.isNaN(df0) || Number.isNaN(ncp))
|
|
639
744
|
return NaN;
|
|
640
|
-
if (
|
|
745
|
+
if (df0 <= 0)
|
|
641
746
|
return NaN;
|
|
747
|
+
const df = halvable(df0);
|
|
642
748
|
const b = quantileBoundaries(p, -Infinity, Infinity, lowerTail, logP);
|
|
643
749
|
if (b !== undefined)
|
|
644
750
|
return b;
|
|
@@ -648,6 +754,15 @@ export function qnt1(p, df, ncp, lowerTail, logP) {
|
|
|
648
754
|
if (!Number.isFinite(ncp))
|
|
649
755
|
return NaN;
|
|
650
756
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
757
|
+
if (df > HUGE_DF) {
|
|
758
|
+
// The quantiles of the limits of pnt1
|
|
759
|
+
const x = ncp + qnorm(p, { lowerTail, logP });
|
|
760
|
+
if (hugeDfRatio(x, df) < 1e-17)
|
|
761
|
+
return x;
|
|
762
|
+
const y = hugeDfTailQuantile(logTarget, df);
|
|
763
|
+
if (hugeDfCentral(y, df, ncp))
|
|
764
|
+
return lower ? -y : y;
|
|
765
|
+
}
|
|
651
766
|
// Starting value: normal approximation (mean δ; t spreads a little in the direction of δ)
|
|
652
767
|
const z = qnorm(logTarget, { lowerTail: lower, logP: true });
|
|
653
768
|
const x0 = ncp + z * Math.sqrt(1 + (ncp * ncp) / (2 * df));
|
|
@@ -687,6 +802,17 @@ export function qnt1(p, df, ncp, lowerTail, logP) {
|
|
|
687
802
|
// the largest double (that needs 2a log(1.8e308) < log(2c), a < 5e-4): the quantile is beyond it
|
|
688
803
|
return s * Infinity;
|
|
689
804
|
}
|
|
805
|
+
// Where the normal start falls on the other side of 0 (a small ν with a large δ, for one): on the side of δ,
|
|
806
|
+
// P[sT <= y] = P[s Z + |δ| <= y S] is about P[S >= |δ|/y] = Q(a, a δ²/y²) when Φ is a step on the scale of S, so y
|
|
807
|
+
// from the gamma quantile. (The start was 1, far above the quantile, from where the first step went to y = 0 and
|
|
808
|
+
// the solver bisected back in log y across the doubly exponential fall of P: 50 ms at df = 1e-200, ncp = 1e4)
|
|
809
|
+
const sideStart = () => {
|
|
810
|
+
if (s * ncp <= 0)
|
|
811
|
+
return 1;
|
|
812
|
+
const g = qgammaStd(logTarget, df / 2, !lowerY, true);
|
|
813
|
+
const y0 = Math.abs(ncp) * Math.sqrt(df / 2 / g);
|
|
814
|
+
return y0 > 0 && Number.isFinite(y0) ? y0 : 1;
|
|
815
|
+
};
|
|
690
816
|
const y = invertContinuous({
|
|
691
817
|
logTarget,
|
|
692
818
|
...(linearTarget !== undefined && { linearTarget }),
|
|
@@ -694,7 +820,7 @@ export function qnt1(p, df, ncp, lowerTail, logP) {
|
|
|
694
820
|
cdf: (y, l) => cdfY(y, l, false),
|
|
695
821
|
cdfLog: (y, l) => cdfY(y, l, true),
|
|
696
822
|
pdfLog: (y) => dntRaw(s * y, df, ncp, true),
|
|
697
|
-
x0: Math.min(Math.abs(x0) > 0 && Number.isFinite(x0) && Math.sign(x0) === s ? Math.abs(x0) :
|
|
823
|
+
x0: Math.min(Math.abs(x0) > 0 && Number.isFinite(x0) && Math.sign(x0) === s ? Math.abs(x0) : sideStart(), hi / 2),
|
|
698
824
|
lo: 0,
|
|
699
825
|
hi,
|
|
700
826
|
positive: true,
|
|
@@ -87,6 +87,11 @@ export function ppareto1(q, a, b, lowerTail, logP) {
|
|
|
87
87
|
return logP ? lu : powNeg(q, b, rt, a);
|
|
88
88
|
if (!logP)
|
|
89
89
|
return -Math.expm1(lu);
|
|
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]);
|
|
90
95
|
return lu < -Math.LN2 ? Math.log1p(-powNeg(q, b, rt, a)) : log1mExp(lu);
|
|
91
96
|
}
|
|
92
97
|
export function qpareto1(p, a, b, lowerTail, logP) {
|
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,8 +68,34 @@ 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
|
+
}
|
|
73
99
|
/** Below this n the upper tail is 1/2 - tinyDfDelta (the next terms are under 1e-23 absolute) */
|
|
74
100
|
const TINY_DF = 1e-15;
|
|
75
101
|
/**
|
|
@@ -103,6 +129,10 @@ function betaArgs(t, n) {
|
|
|
103
129
|
}
|
|
104
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) */
|
|
105
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;
|
|
106
136
|
if (n < TINY_DF) {
|
|
107
137
|
const d = tinyDfDelta(t, n);
|
|
108
138
|
return log ? Math.log1p(-2 * d) - Math.LN2 : 0.5 - d;
|
|
@@ -123,7 +153,7 @@ function upperTailPositive(t, n, log) {
|
|
|
123
153
|
}
|
|
124
154
|
/** [log, linear] of upperTailPositive (each value identical to upperTailPositive) */
|
|
125
155
|
function upperTailPositiveBoth(t, n) {
|
|
126
|
-
if (n < TINY_DF)
|
|
156
|
+
if (n < TINY_DF || t === Infinity)
|
|
127
157
|
return [upperTailPositive(t, n, true), upperTailPositive(t, n, false)];
|
|
128
158
|
const t2 = t * t;
|
|
129
159
|
const x = n / (n + t2);
|
|
@@ -147,6 +177,8 @@ export function pt1(q, df, lowerTail, logP) {
|
|
|
147
177
|
return pnorm(q, { lowerTail, logP });
|
|
148
178
|
if (q === 0)
|
|
149
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);
|
|
150
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)
|
|
151
183
|
const small = q < 0 === lowerTail;
|
|
152
184
|
const a = Math.abs(q);
|
|
@@ -166,8 +198,25 @@ export function qt1(p, df, lowerTail, logP) {
|
|
|
166
198
|
if (!Number.isFinite(df))
|
|
167
199
|
return qnorm(p, { lowerTail, logP });
|
|
168
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
|
+
}
|
|
169
209
|
if (logTarget === -Math.LN2 && linearTarget === 0.5)
|
|
170
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
|
+
}
|
|
171
220
|
// Smaller tail: if lower, the answer is negative. Solve the upper tail in |t|
|
|
172
221
|
// Starting value: in the deep tail P[T > t] ≈ n^(n/2) t^(-n) / (n B(n/2, 1/2))
|
|
173
222
|
const n = df;
|
|
@@ -195,6 +244,9 @@ export function qt1(p, df, lowerTail, logP) {
|
|
|
195
244
|
const g3 = (z * (((3 * z2 + 19) * z2 + 17) * z2 - 15)) / 384;
|
|
196
245
|
const g4 = (z * ((((79 * z2 + 776) * z2 + 1482) * z2 - 1920) * z2 - 945)) / 92160;
|
|
197
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;
|
|
198
250
|
}
|
|
199
251
|
}
|
|
200
252
|
if (!(x0 > 0) || !Number.isFinite(x0))
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// of lengths (each at most 1), so that wide and narrow intervals neither overflow nor underflow on the way.
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
10
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
10
11
|
const valid = (a, b, c) => Number.isFinite(a) && Number.isFinite(b) && a < b && a <= c && c <= b;
|
|
11
12
|
/** 1/2 when b - a overflows (x, a, b and c are all halved then; the probabilities do not change), else 1 */
|
|
12
13
|
const halfIfOverflow = (a, b) => (Number.isFinite(b - a) ? 1 : 0.5);
|
|
@@ -28,21 +29,43 @@ export function dtriang1(x, a, b, c, log) {
|
|
|
28
29
|
if (r < 0.5)
|
|
29
30
|
return log ? Math.LN2 - Math.log(w) + Math.log1p(-r) : (2 * (1 - r)) / w;
|
|
30
31
|
const s = x < c ? (x - a) / (c - a) : (b - x) / (b - c);
|
|
31
|
-
|
|
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);
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
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
|
|
35
|
-
* 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
|
|
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)
|
|
36
44
|
*/
|
|
37
45
|
function tails(q, a, b, c) {
|
|
38
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);
|
|
39
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))
|
|
40
49
|
if (q <= c) {
|
|
41
50
|
const [s, t] = [(q - a) / (c - a), (q - a) / w];
|
|
42
|
-
return [s * t, (b - c) / w + ((c - q) / (c - a)) * (t + (c - a) / w),
|
|
51
|
+
return [s * t, (b - c) / w + ((c - q) / (c - a)) * (t + (c - a) / w), lnear(s, t, q - a, c - a)];
|
|
43
52
|
}
|
|
44
53
|
const [s, t] = [(b - q) / (b - c), (b - q) / w];
|
|
45
|
-
return [(c - a) / w + ((q - c) / (b - c)) * (t + (b - c) / w), s * t,
|
|
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));
|
|
46
69
|
}
|
|
47
70
|
export function ptriang1(q, a, b, c, lowerTail, logP) {
|
|
48
71
|
if (Number.isNaN(q) || !valid(a, b, c))
|
|
@@ -60,7 +83,9 @@ export function ptriang1(q, a, b, c, lowerTail, logP) {
|
|
|
60
83
|
// the log of a probability above 1/2 from the other tail; a subnormal near tail from its factors
|
|
61
84
|
if (v > 0.5)
|
|
62
85
|
return Math.log1p(-(lowerTail ? upper : lower));
|
|
63
|
-
|
|
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);
|
|
64
89
|
}
|
|
65
90
|
export function qtriang1(p, a, b, c, lowerTail, logP) {
|
|
66
91
|
if (Number.isNaN(p) || !valid(a, b, c))
|
|
@@ -80,19 +105,21 @@ export function qtriang1(p, a, b, c, lowerTail, logP) {
|
|
|
80
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;
|
|
81
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,
|
|
82
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
|
|
83
|
-
// no cancellation near the end). The square roots are taken factor by factor so that no product over- or underflows
|
|
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 }))
|
|
84
111
|
const cl = (c - a) / w;
|
|
85
112
|
const cr = (b - c) / w;
|
|
86
113
|
let x;
|
|
87
114
|
if (cl > 0 && lower <= cl) {
|
|
88
|
-
const t =
|
|
115
|
+
const t = lower / cl;
|
|
89
116
|
x =
|
|
90
117
|
t <= 0.5
|
|
91
118
|
? a + (lowerTail ? sqrtP : Math.sqrt(lower)) * Math.sqrt(w) * Math.sqrt(c - a)
|
|
92
119
|
: c - ((c - a) * ((upper - cr) / cl)) / (1 + Math.sqrt(t));
|
|
93
120
|
}
|
|
94
121
|
else {
|
|
95
|
-
const t =
|
|
122
|
+
const t = upper / cr;
|
|
96
123
|
x =
|
|
97
124
|
t <= 0.5
|
|
98
125
|
? b - (lowerTail ? Math.sqrt(upper) : sqrtP) * Math.sqrt(w) * Math.sqrt(b - c)
|
|
@@ -690,6 +690,11 @@ function logRangeMaxCdf(w, k, r, upper) {
|
|
|
690
690
|
const lWc = Wc > 1e-290 ? Math.log(Wc) : logRangeCdfC(w, k);
|
|
691
691
|
if (r === 1)
|
|
692
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;
|
|
693
698
|
// 1 - (1 - Wc)^r
|
|
694
699
|
return Math.log(-Math.expm1(r * Math.log1p(-Math.exp(lWc))));
|
|
695
700
|
}
|
|
@@ -806,6 +811,15 @@ function validParams(k, df, r) {
|
|
|
806
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.
|
|
807
812
|
*/
|
|
808
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
|
+
}
|
|
809
823
|
let t;
|
|
810
824
|
if (memo !== undefined && memo.q === q && memo.k === k && memo.df === df && memo.r === r)
|
|
811
825
|
t = memo.tail;
|
|
@@ -818,6 +832,8 @@ function ptukeyRaw(q, k, df, r, lowerTail, logP) {
|
|
|
818
832
|
return logP ? Math.log1p(-t.value) : 1 - t.value;
|
|
819
833
|
}
|
|
820
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;
|
|
821
837
|
/**
|
|
822
838
|
* The smaller tail. Which tail is smaller is guessed from the df = ∞ value (one inner integral); if the guess is
|
|
823
839
|
* wrong and the value exceeds 1/2, the other side is computed. preferUpper is the side to compute when there is no
|
|
@@ -51,6 +51,8 @@ export function qunif1(p, min, max, lowerTail, logP) {
|
|
|
51
51
|
// (1 - p would lose them); otherwise min + (1 - p)(max - min) with 1 - p exact (or -expm1(log p)), as R gives
|
|
52
52
|
const half = halfIfOverflow(min, max) !== 1;
|
|
53
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))
|
|
54
56
|
const pp = logP ? Math.exp(p) : p;
|
|
55
57
|
if (lowerTail)
|
|
56
58
|
return at(pp);
|
|
@@ -7,13 +7,14 @@
|
|
|
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;
|
|
17
18
|
/**
|
|
18
19
|
* log u (u = x / scale) in double-double. x > 0, finite parameters. When x / scale is subnormal, 0 or overflows,
|
|
19
20
|
* log x - log scale instead; also for x above 1e300, where the exact remainder in standardizeDD overflows
|
|
@@ -64,7 +65,10 @@ export function dweibull1(x, shape, scale, log) {
|
|
|
64
65
|
if (t[0] === Infinity)
|
|
65
66
|
return d0(log);
|
|
66
67
|
// E = (shape - 1) log u - t
|
|
67
|
-
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);
|
|
68
72
|
const r = shape / scale;
|
|
69
73
|
if (log)
|
|
70
74
|
return logRatio(shape, scale) + e[0] + e[1];
|
|
@@ -111,6 +115,22 @@ export function qweibull1(p, shape, scale, lowerTail, logP) {
|
|
|
111
115
|
if (b !== undefined)
|
|
112
116
|
return b;
|
|
113
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
|
+
}
|
|
114
134
|
let lu;
|
|
115
135
|
if (logP)
|
|
116
136
|
lu = lowerTail ? log1mExp(p) : p;
|