@numero-uno-jpn/rstats 0.15.2 → 0.16.0
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 +280 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-PWVQVJTP.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +35 -5
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +87 -8
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +11 -1
- package/dist/distributions/f.js +73 -22
- package/dist/distributions/gamma.js +61 -6
- package/dist/distributions/geometric.js +53 -14
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +6 -1
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +31 -7
- package/dist/distributions/logistic.js +11 -2
- package/dist/distributions/lognormal.js +20 -3
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +128 -35
- package/dist/distributions/nchisq.js +85 -14
- package/dist/distributions/negbinomial.js +100 -18
- package/dist/distributions/normal.js +35 -5
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +536 -35
- package/dist/distributions/pareto.js +9 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/t.js +55 -3
- package/dist/distributions/triangular.js +45 -13
- package/dist/distributions/tukey.js +21 -2
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +33 -3
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +23 -1
- package/dist/internal/ddexp.js +86 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +25 -7
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +11 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +15 -2
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +100 -10
- 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/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +179 -33
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-3WQCKGKP.js +0 -2
- package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
- package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-HBMHR5SS.js +0 -2
- package/dist/chunks/chunk-IBPWRQIF.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-LG74QY5U.js +0 -2
- package/dist/chunks/chunk-PEYGUK32.js +0 -2
- package/dist/chunks/chunk-Q4PGYESP.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-SREPSVTT.js +0 -2
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
- package/dist/chunks/chunk-VWJ74UHC.js +0 -2
- package/dist/chunks/chunk-XNPU54NH.js +0 -2
- package/dist/chunks/chunk-XPLDEK6D.js +0 -2
- package/dist/chunks/chunk-YGT4P7EU.js +0 -2
- package/dist/chunks/chunk-YVREYVI2.js +0 -2
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
import { vectorize } from "../internal/vectorize.js";
|
|
7
7
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
8
8
|
import { ddExp, log1mExp } from "../internal/ddexp.js";
|
|
9
|
-
import { ddAdd, ddLog, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
|
|
9
|
+
import { ddAdd, ddDivNum, ddLog, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
|
|
10
10
|
const valid = (a, b) => a > 0 && b > 0 && Number.isFinite(b);
|
|
11
11
|
// a = Inf is taken as the limit (the distribution concentrates at 1), as extraDistr does
|
|
12
12
|
const MIN_NORMAL = 2.2250738585072014e-308;
|
|
13
|
+
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
13
14
|
/**
|
|
14
15
|
* c log(1 - x^a) for 0 < x < 1 where x^a is subnormal: -c x^a (to double precision) from logs, since x^a itself has
|
|
15
16
|
* lost digits
|
|
@@ -127,6 +128,11 @@ export function pkumar1(q, a, b, lowerTail, logP) {
|
|
|
127
128
|
// x^a or 1 - exp(lu) underflows; elsewhere the forms below are accurate
|
|
128
129
|
if (Math.max(b, 1) * xa < 1e-10)
|
|
129
130
|
return Math.log(b) + a * Math.log(q) + Math.log1p((-(b - 1) * xa) / 2);
|
|
131
|
+
// Where b log(1 - x^a) is subnormal (a subnormal b), it has lost digits, and so would the log of the probability
|
|
132
|
+
// -lu taken from it: log b + log(-log(1 - x^a)) instead (pkumar(0.2, 0.42, 1.85e-317, { logP: true }) was 2.5e-10 off;
|
|
133
|
+
// extraDistr gives -Inf there, taking the log after the probability underflowed)
|
|
134
|
+
if (-lu < MIN_NORMAL)
|
|
135
|
+
return Math.log(b) + Math.log(-log1mPow(q, a));
|
|
130
136
|
return lu < -Math.LN2 && !tiny ? Math.log1p(-powOneMinus(q, a, b)) : log1mExp(lu);
|
|
131
137
|
}
|
|
132
138
|
export function qkumar1(p, a, b, lowerTail, logP) {
|
|
@@ -142,13 +148,25 @@ export function qkumar1(p, a, b, lowerTail, logP) {
|
|
|
142
148
|
// w^(1 / a) with w = 1 - exp(lu / b). Where lu / b is subnormal or underflows, w = -lu / b, in logs; for w > 1/2
|
|
143
149
|
// log w = log1p(-exp(lu / b)) keeps the digits of 1 - w
|
|
144
150
|
const t = lu / b;
|
|
145
|
-
|
|
146
|
-
|
|
151
|
+
// (log(-lu) is p itself where a lower tail given as its log is below the normal range: -lu = exp(p) was rounded on
|
|
152
|
+
// the subnormal grid (qkumar(-744.24, 1.13, 0.124, { logP: true }) was 17% off). The exponent, of the order of
|
|
153
|
+
// hundreds, in double-double: its rounding is the relative error of the quantile (3.7e-14 there)
|
|
154
|
+
if (!(t <= -MIN_NORMAL)) {
|
|
155
|
+
const llu = logP && lowerTail && p < LOG_MIN_NORMAL ? p : Math.log(-lu);
|
|
156
|
+
const n = ddSub([llu, 0], ddLog([b, 0]));
|
|
157
|
+
const q = n[0] / a;
|
|
158
|
+
if (!(a < 1e300 && Math.abs(q) < 1e300))
|
|
159
|
+
return Math.exp(q);
|
|
160
|
+
const e = ddExp(ddDivNum(n, a));
|
|
161
|
+
return e[0] + e[1];
|
|
162
|
+
}
|
|
147
163
|
if (t < -Math.LN2)
|
|
148
164
|
return Math.exp(Math.log1p(-Math.exp(t)) / a);
|
|
149
165
|
const w = -Math.expm1(t);
|
|
150
166
|
const r = 1 / a;
|
|
151
|
-
|
|
167
|
+
// (for a below 1e-300, w <= 1/2 here and the quantile w^(1/a) underflows to 0; the correction below was NaN there,
|
|
168
|
+
// from the splitting of a subnormal a or 1 / a overflowing: qkumar(4.8e-52, 9.9e-314, 2.5))
|
|
169
|
+
if (!(a < 1e300) || !(r < 1e300))
|
|
152
170
|
return Math.pow(w, r);
|
|
153
171
|
// w^r corrected for the rounding of r = 1 / a (exactly 1 / a - r = (1 - a r) / a), which log w magnifies
|
|
154
172
|
const [ar, e] = twoProd(a, r);
|
|
@@ -5,24 +5,47 @@
|
|
|
5
5
|
// (and the mirror image above 1/2).
|
|
6
6
|
import { vectorize } from "../internal/vectorize.js";
|
|
7
7
|
import { quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
8
|
-
import { expNeg, locationScale, standardizeDD } from "../internal/ddexp.js";
|
|
9
|
-
const
|
|
10
|
-
|
|
8
|
+
import { expNeg, expNegOver, locationScale, standardizeDD } from "../internal/ddexp.js";
|
|
9
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
10
|
+
const valid = (mu, sigma) => !Number.isNaN(mu) && sigma > 0;
|
|
11
|
+
// an infinite mu is taken as the limit (the distribution moves out to that infinity), as extraDistr does. sigma = Inf is
|
|
12
|
+
// the limit too (review 24: density 0, probability 1/2 at every finite x, as extraDistr; the quantiles -Inf, mu and Inf),
|
|
13
|
+
// except at an infinite x or mu, where it is not defined (NaN, as extraDistr)
|
|
11
14
|
export function dlaplace1(x, mu, sigma, log) {
|
|
12
15
|
if (Number.isNaN(x) || !valid(mu, sigma))
|
|
13
16
|
return NaN;
|
|
14
|
-
if (
|
|
17
|
+
if (sigma === Infinity)
|
|
18
|
+
return Number.isFinite(x) && Number.isFinite(mu) ? (log ? -Infinity : 0) : NaN;
|
|
19
|
+
if (!Number.isFinite(mu)) {
|
|
20
|
+
// x and mu the same infinity: x - mu is Inf - Inf and the limit depends on the path (NaN, as in extraDistr; #338)
|
|
21
|
+
if (x === mu)
|
|
22
|
+
return NaN;
|
|
15
23
|
return log ? -Infinity : 0;
|
|
24
|
+
}
|
|
16
25
|
const z = standardizeDD(x, mu, sigma);
|
|
17
26
|
const a = z[0] < 0 ? [-z[0], -z[1]] : [z[0], z[1]];
|
|
27
|
+
// 2 sigma overflows for sigma above half the largest double: then the factor 1/2 is taken apart
|
|
28
|
+
const twoSigma = 2 * sigma;
|
|
18
29
|
if (log)
|
|
19
|
-
return -a[0] - a[1] - Math.log(
|
|
20
|
-
|
|
30
|
+
return -a[0] - a[1] - (twoSigma < Infinity ? Math.log(twoSigma) : Math.LN2 + Math.log(sigma));
|
|
31
|
+
// (from exp(-|z|) below the normal range the quotient is formed without rounding it there: see expNegOver)
|
|
32
|
+
const e = expNeg(a);
|
|
33
|
+
// (the factor 1/2 on e first, exactly: (e / sigma) / 2 rounded twice onto the subnormals, an ulp off at x = sigma / 2
|
|
34
|
+
// for sigma = 1.447e308; issue #110. Below 2 MIN_NORMAL the quotient is below e^-708 / 1.8e308, 0 either way)
|
|
35
|
+
if (twoSigma === Infinity)
|
|
36
|
+
return e >= 2 * MIN_NORMAL ? e / 2 / sigma : expNegOver(a, sigma) / 2;
|
|
37
|
+
return e >= MIN_NORMAL ? e / twoSigma : expNegOver(a, twoSigma);
|
|
21
38
|
}
|
|
22
39
|
export function plaplace1(q, mu, sigma, lowerTail, logP) {
|
|
23
40
|
if (Number.isNaN(q) || !valid(mu, sigma))
|
|
24
41
|
return NaN;
|
|
42
|
+
if (sigma === Infinity)
|
|
43
|
+
return Number.isFinite(q) && Number.isFinite(mu) ? (logP ? -Math.LN2 : 0.5) : NaN;
|
|
25
44
|
if (!Number.isFinite(mu)) {
|
|
45
|
+
// q and mu the same infinity: the limit is 0 or 1 depending on which goes first (NaN, as in extraDistr, pnorm,
|
|
46
|
+
// plogis and pcauchy; #338)
|
|
47
|
+
if (q === mu)
|
|
48
|
+
return NaN;
|
|
26
49
|
const lower = mu === Infinity ? 0 : 1;
|
|
27
50
|
const v = lowerTail ? lower : 1 - lower;
|
|
28
51
|
return logP ? Math.log(v) : v;
|
|
@@ -35,8 +58,9 @@ export function plaplace1(q, mu, sigma, lowerTail, logP) {
|
|
|
35
58
|
return logP ? Math.log1p(-half) : 1 - half;
|
|
36
59
|
}
|
|
37
60
|
export function qlaplace1(p, mu, sigma, lowerTail, logP) {
|
|
38
|
-
if (Number.isNaN(p) || !valid(mu, sigma))
|
|
61
|
+
if (Number.isNaN(p) || !valid(mu, sigma) || (sigma === Infinity && !Number.isFinite(mu)))
|
|
39
62
|
return NaN;
|
|
63
|
+
// (sigma = Inf: locationScale gives -Inf below p = 1/2, mu at 1/2 and Inf above)
|
|
40
64
|
const bd = quantileBoundaries(p, -Infinity, Infinity, lowerTail, logP);
|
|
41
65
|
if (bd !== undefined)
|
|
42
66
|
return bd;
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// Quantiles compute logit(p) = log(p / (1 - p)) in a cancellation-free form.
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { quantileBoundaries } from "../internal/dpq.js";
|
|
9
|
-
import { expNeg, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
|
|
9
|
+
import { expNeg, expNegOver, locationScale, log1mExp, standardizeDD } from "../internal/ddexp.js";
|
|
10
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
10
11
|
export function dlogis1(x, location, scale, log) {
|
|
11
12
|
if (Number.isNaN(x) || Number.isNaN(location) || Number.isNaN(scale))
|
|
12
13
|
return NaN;
|
|
@@ -19,7 +20,15 @@ export function dlogis1(x, location, scale, log) {
|
|
|
19
20
|
const e = expNeg(a);
|
|
20
21
|
if (log)
|
|
21
22
|
return -a[0] - a[1] - Math.log(scale) - 2 * Math.log1p(e);
|
|
22
|
-
|
|
23
|
+
// Neither e nor the denominator may be rounded on the subnormal grid: from e below the normal range (1 + e is then 1)
|
|
24
|
+
// the quotient by expNegOver, and for a subnormal scale the division by it last (dlogis(-3.5667e-316, { scale:
|
|
25
|
+
// 4.81926e-319 }) was 1.3e-6 off)
|
|
26
|
+
if (e < MIN_NORMAL)
|
|
27
|
+
return expNegOver(a, scale);
|
|
28
|
+
// (the division by scale last also where the denominator overflows: dlogis(0, { scale: 1.28e308 }) was 0 for
|
|
29
|
+
// 1.95e-309; issue #109)
|
|
30
|
+
const d = scale * (1 + e) * (1 + e);
|
|
31
|
+
return scale >= MIN_NORMAL && d < Infinity ? e / d : e / ((1 + e) * (1 + e)) / scale;
|
|
23
32
|
}
|
|
24
33
|
export function plogis1(q, location, scale, lowerTail, logP) {
|
|
25
34
|
if (Number.isNaN(q) || Number.isNaN(location) || Number.isNaN(scale))
|
|
@@ -21,12 +21,25 @@ function zOf(x, meanlog, sdlog) {
|
|
|
21
21
|
if (sdlog === 1)
|
|
22
22
|
return d;
|
|
23
23
|
const hi = d[0] / sdlog;
|
|
24
|
-
|
|
24
|
+
if (!(Number.isFinite(hi) && Math.abs(hi) < 1e300))
|
|
25
|
+
return [hi, 0];
|
|
26
|
+
if (sdlog < 1e300)
|
|
27
|
+
return ddDiv(d, [sdlog, 0]);
|
|
28
|
+
// The splitting of sdlog in the division overflows above 1e300 (plnorm(2, sdlog = 1e302) was NaN): both scaled by
|
|
29
|
+
// 2^-64 (exactly, unless d is so small that z is far below the rounding of the terms it enters)
|
|
30
|
+
const s = 2 ** -64;
|
|
31
|
+
return ddDiv([d[0] * s, d[1] * s], [sdlog * s, 0]);
|
|
25
32
|
}
|
|
26
33
|
/** z^2 / 2 in double-double */
|
|
27
34
|
function halfSquare(z) {
|
|
28
|
-
if (Math.abs(z[0]) > 1e150)
|
|
29
|
-
|
|
35
|
+
if (Math.abs(z[0]) > 1e150) {
|
|
36
|
+
// z times 2^-512 (exact) so that the splitting in ddMul does not overflow, and the square back by 2^1024 / 2 (the
|
|
37
|
+
// low part of z was dropped here: an ulp off where z^2 / 2 is finite; issue #112)
|
|
38
|
+
const t = [z[0] * 2 ** -512, z[1] * 2 ** -512];
|
|
39
|
+
const s = ddMul(t, t);
|
|
40
|
+
const hi = s[0] * 2 ** 1023;
|
|
41
|
+
return Number.isFinite(hi) ? [hi, s[1] * 2 ** 1023] : [hi, 0];
|
|
42
|
+
}
|
|
30
43
|
const s = ddMul(z, z);
|
|
31
44
|
return [s[0] / 2, s[1] / 2];
|
|
32
45
|
}
|
|
@@ -141,6 +154,10 @@ export function qlnorm1(p, meanlog, sdlog, lowerTail, logP) {
|
|
|
141
154
|
}
|
|
142
155
|
// exp(meanlog + sdlog z). Form the exponent in double-double and apply the low-order term to first order
|
|
143
156
|
const zd = refineStdQuantile(z, p, lowerTail, logP);
|
|
157
|
+
// Above sdlog = 1e300 the splitting in the double-double product overflows (qlnorm(1e-30, 0, 9e307) was NaN). The
|
|
158
|
+
// exponent is then beyond the range of exp unless |z| < 1e-297 (p within that of 1/2), where it is close to meanlog
|
|
159
|
+
if (sdlog >= 1e300)
|
|
160
|
+
return Math.exp(meanlog + sdlog * zd[0]);
|
|
144
161
|
const e = ddAdd(ddAdd(mulDD(sdlog, zd[0]), [sdlog * zd[1], 0]), [meanlog, 0]);
|
|
145
162
|
// Beyond the range of exp the result is 0 or Infinity (for a huge exponent e[1] can exceed 1 and flip the sign)
|
|
146
163
|
if (!(Math.abs(e[0]) < 750))
|
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
// The sums of the weights are kept in double-double: q_k is raised to the power x_k, so a rounded sum would give an
|
|
7
7
|
// error of x_k ε (1e-9 at x_k = 1e7).
|
|
8
8
|
// Summing lgamma terms instead loses digits to cancellation for large counts.
|
|
9
|
+
import { lchoose } from "../special/choose.js";
|
|
9
10
|
import { dbinomRawDD } from "../special/loader.js";
|
|
10
11
|
import { ddAdd, ddDiv } from "../special/dd.js";
|
|
12
|
+
import { ldexp } from "../internal/summary.js";
|
|
11
13
|
/**
|
|
12
14
|
* Probability function of the multinomial distribution. Equivalent to R's
|
|
13
15
|
* `dmultinom(x, size = NULL, prob, log = FALSE)`.
|
|
@@ -40,29 +42,63 @@ export function dmultinom(x, prob, options = {}) {
|
|
|
40
42
|
const N = x.reduce((a, b) => a + b, 0);
|
|
41
43
|
if (options.size !== undefined && options.size !== N)
|
|
42
44
|
throw new RangeError("size must equal the sum of x");
|
|
45
|
+
// Suffix sums of the weights (double-double) and the counts
|
|
46
|
+
let w = prob;
|
|
47
|
+
let ps = suffixSums(w);
|
|
48
|
+
// the weights are divided by 2^scale (0 unless their sum overflows)
|
|
49
|
+
let scale = 0;
|
|
50
|
+
if (ps[0][0] === Infinity) {
|
|
51
|
+
// Finite weights whose sum overflows: divided by 2^s >= K, which only the ratios matter for (exact unless a
|
|
52
|
+
// weight becomes subnormal; one that becomes 0 is below 1e-600 of the largest); weights with a sum in range are
|
|
53
|
+
// used as given
|
|
54
|
+
scale = Math.ceil(Math.log2(K));
|
|
55
|
+
w = prob.map((p) => ldexp(p, -scale));
|
|
56
|
+
ps = suffixSums(w);
|
|
57
|
+
}
|
|
58
|
+
const ns = new Array(K + 1).fill(0);
|
|
59
|
+
for (let k = K - 1; k >= 0; k--)
|
|
60
|
+
ns[k] = ns[k + 1] + x[k];
|
|
43
61
|
// A category with probability 0 and a positive count makes the probability 0
|
|
44
62
|
for (let k = 0; k < K; k++)
|
|
45
63
|
if (prob[k] === 0 && x[k] > 0)
|
|
46
64
|
return log ? -Infinity : 0;
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
65
|
+
// A total of the counts that overflows (each count finite) is NaN on both scales, as dnbinom where size + x overflows
|
|
66
|
+
// (reviews 22 and 55) and dhyper's m + n: the integer range beyond 2^53. (The linear value was 0, the log NaN; the
|
|
67
|
+
// true value of dmultinom([1e308, 1e308], [0.5, 0.5]) is 5.64e-155; issue #152)
|
|
68
|
+
if (N === Infinity)
|
|
69
|
+
return NaN;
|
|
70
|
+
// The scaled weights are used only where the sum of the weights from k on overflows: a weight that the scaling takes
|
|
71
|
+
// to 0 (or into the subnormals) is not lost from the later ratios. Where nothing overflows this is ps itself
|
|
72
|
+
const psOrig = scale === 0 ? ps : suffixSums(prob);
|
|
73
|
+
const logDD = (d) => Math.log(d[0]) + d[1] / d[0];
|
|
54
74
|
let logSum = 0;
|
|
55
75
|
let prodLin = 1;
|
|
56
76
|
for (let k = 0; k < K; k++) {
|
|
57
77
|
if (prob[k] === 0)
|
|
58
78
|
continue;
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
79
|
+
const restOrig = psOrig[k + 1];
|
|
80
|
+
if (restOrig[0] === 0)
|
|
61
81
|
break; // the remaining counts all fall here: probability 1
|
|
62
|
-
const
|
|
63
|
-
const
|
|
82
|
+
const scaled = !(psOrig[k][0] < Infinity);
|
|
83
|
+
const sum = scaled ? ps[k] : psOrig[k];
|
|
84
|
+
const sc = scaled ? scale : 0;
|
|
85
|
+
const q = ddDiv([scaled ? w[k] : prob[k], 0], sum);
|
|
86
|
+
const qc = ddDiv(scaled ? ps[k + 1] : restOrig, sum);
|
|
64
87
|
const xk = x[k];
|
|
65
88
|
const nmx = ns[k + 1];
|
|
89
|
+
if (q[0] < 2 ** -1022 || qc[0] < 2 ** -1022) {
|
|
90
|
+
// q or 1 - q below the normal range (dmultinom([1, 0, 0], [5e-324, 1, 1], { log: true }) was -Infinity,
|
|
91
|
+
// -745.13 to the double; R gives -Infinity too): the term from the log of the small one, log(1 - r) = -r (to r²)
|
|
92
|
+
const small = q[0] < 2 ** -1022;
|
|
93
|
+
const lr = (small ? Math.log(prob[k]) : logDD(restOrig)) - sc * Math.LN2 - logDD(sum);
|
|
94
|
+
const [xr, xo] = small ? [xk, nmx] : [nmx, xk];
|
|
95
|
+
const term = lchoose(xk + nmx, xk, { rCompatible: false }) + (xr === 0 ? 0 : xr * lr) - xo * Math.exp(lr);
|
|
96
|
+
if (log)
|
|
97
|
+
logSum += term;
|
|
98
|
+
else
|
|
99
|
+
prodLin *= Math.exp(term);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
66
102
|
if (log)
|
|
67
103
|
logSum += dbinomRawDD(xk, nmx, q, qc, true);
|
|
68
104
|
else
|
|
@@ -70,3 +106,10 @@ export function dmultinom(x, prob, options = {}) {
|
|
|
70
106
|
}
|
|
71
107
|
return log ? logSum : prodLin;
|
|
72
108
|
}
|
|
109
|
+
/** Suffix sums w_k + ... + w_(K-1) in double-double, for k = 0..K (the last is 0) */
|
|
110
|
+
function suffixSums(w) {
|
|
111
|
+
const ps = new Array(w.length + 1).fill([0, 0]);
|
|
112
|
+
for (let k = w.length - 1; k >= 0; k--)
|
|
113
|
+
ps[k] = ddAdd(ps[k + 1], [w[k], 0]);
|
|
114
|
+
return ps;
|
|
115
|
+
}
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
// x and 1 - x are formed as a double-double pair (rounding is amplified by the parameters, as for the central distribution).
|
|
16
16
|
import { dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
17
17
|
import { invertContinuous } from "../internal/invert.js";
|
|
18
|
-
import { kronrodAdaptive, narrowQuantile, quadratureControl, searchDoubles } from "../internal/noncentral.js";
|
|
18
|
+
import { kronrodAdaptive, narrowQuantile, negligibleNcp, quadratureControl, searchDoubles } from "../internal/noncentral.js";
|
|
19
19
|
import { gammaInc } from "../special/igam.js";
|
|
20
20
|
import { mixtureDensity, mixtureTail } from "../internal/poissonMixture.js";
|
|
21
21
|
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddNeg, twoProd, twoSum } from "../special/dd.js";
|
|
@@ -96,8 +96,13 @@ function betaKernelIncLog(s, b, xd, xcd) {
|
|
|
96
96
|
if (!(xd[0] > 0 && xcd[0] > 0))
|
|
97
97
|
return -Infinity;
|
|
98
98
|
const l = ddAdd(ddMulNum(ddLog(xd), s), ddMulNum(ddLog(xcd), b));
|
|
99
|
-
if (s < 1 && b < 1)
|
|
100
|
-
|
|
99
|
+
if (s < 1 && b < 1) {
|
|
100
|
+
// log(1 + s / b), also where s / b overflows for a subnormal b (it was Infinity and the term 0: the j = 0 term of
|
|
101
|
+
// dbeta(0.5, 0.75, 5e-324, ncp = 0.1) was dropped, 3.7 off in the log; issue #114)
|
|
102
|
+
const q = s / b;
|
|
103
|
+
const lq = q < Infinity ? Math.log1p(q) : Math.log(s) - Math.log(b) + Math.log1p(b / s);
|
|
104
|
+
return ddAdd(l, [-(lgamma1p(s) + lgamma1p(b) - lgamma1p(s + b) + lq), 0]);
|
|
105
|
+
}
|
|
101
106
|
return ddAdd(ddAdd(l, ddNeg(lbetaDD(s, b))), [-Math.log(s), 0]);
|
|
102
107
|
}
|
|
103
108
|
/** Form x and 1 - x as a double-double pair, based on the smaller of the two */
|
|
@@ -137,6 +142,9 @@ function leftTailLog(a, b, ncp, xd, xcd) {
|
|
|
137
142
|
return undefined;
|
|
138
143
|
if (mu * rho > 100)
|
|
139
144
|
return rho <= 1e-16 ? -mu + betaIncDD(a, b, xd, xcd, true, true) : undefined;
|
|
145
|
+
// (λ/2 underflows to 0 for a subnormal λ: only the j = 0 term, I_x(a, b), is left)
|
|
146
|
+
if (mu === 0)
|
|
147
|
+
return betaIncDD(a, b, xd, xcd, true, true);
|
|
140
148
|
const lmu = Math.log(mu);
|
|
141
149
|
let max = -Infinity;
|
|
142
150
|
const terms = [];
|
|
@@ -396,11 +404,14 @@ function gammaExpansion(a, b, ncp, o, lower, log) {
|
|
|
396
404
|
if (!(t0 > 0 && t0 < Infinity) || ratio === 0)
|
|
397
405
|
return base;
|
|
398
406
|
// In terms of H = h t0 = b - 1 - t0, B = b - 1 and the cumulants relative to A^n (A = a + λ/2, t0 = A k), which do
|
|
399
|
-
// not underflow or overflow where the cumulants themselves do: v h/2 = (w/2) t0 H, w = (a + λ)/A², and so on
|
|
407
|
+
// not underflow or overflow where the cumulants themselves do: v h/2 = (w/2) t0 H, w = (a + λ)/A², and so on (with a
|
|
408
|
+
// and λ divided by A first: 6 (a + 2λ) overflowed from λ = 1.5e307, and both tails of pf(2.5e300, 1828, 0.165,
|
|
409
|
+
// { ncp: 3.7e307 }) were 0)
|
|
400
410
|
const A = a + ncp / 2;
|
|
401
|
-
const
|
|
402
|
-
const
|
|
403
|
-
const
|
|
411
|
+
const [ra, rl] = [a / A, ncp / A];
|
|
412
|
+
const w = (ra + rl) / A;
|
|
413
|
+
const u3 = (2 * ra + 3 * rl) / A / A;
|
|
414
|
+
const u4 = (6 * (ra + 2 * rl)) / A / A / A;
|
|
404
415
|
const H = b - 1 - t0;
|
|
405
416
|
const B = b - 1;
|
|
406
417
|
const series = (w / 2) * H + (u3 / 6) * (H * H - B) + ((u4 + 3 * w * w) / 24) * (H * H * H - 3 * H * B + 2 * B);
|
|
@@ -581,13 +592,24 @@ export function dnbeta1(x, a, b, ncp, log) {
|
|
|
581
592
|
return log ? -Infinity : 0;
|
|
582
593
|
if (b < 1)
|
|
583
594
|
return Infinity;
|
|
584
|
-
|
|
595
|
+
const s = a + mu;
|
|
596
|
+
if (!log)
|
|
597
|
+
return s;
|
|
598
|
+
// the sum may overflow where its log does not (dnbeta(1, 1.5e308, 1, 1.5e308, log) was Infinity for 710.007; #96)
|
|
599
|
+
return Number.isFinite(s) ? Math.log(s) : Math.log(a / 2 + mu / 2) + Math.LN2;
|
|
585
600
|
}
|
|
601
|
+
if (negligibleNcp(mu, x * ((a + b) / a)))
|
|
602
|
+
return dbeta1(x, a, b, log);
|
|
586
603
|
return dnbetaCore(x, a, b, ncp, log);
|
|
587
604
|
}
|
|
588
605
|
export function pnbeta1(q, a, b, ncp, lowerTail, logP) {
|
|
589
606
|
if (Number.isNaN(q) || Number.isNaN(a) || Number.isNaN(b) || Number.isNaN(ncp))
|
|
590
607
|
return NaN;
|
|
608
|
+
// parameters that define no distribution are checked before the ends of the support, as in the central pbeta (R
|
|
609
|
+
// gives 0 or 1 at q <= 0 or q >= 1 even for a = -1 or ncp = Inf). Shapes of 0 (point masses, the limits R accepts
|
|
610
|
+
// there) still get the ends
|
|
611
|
+
if (a < 0 || b < 0 || ncp < 0 || ncp === Infinity)
|
|
612
|
+
return NaN;
|
|
591
613
|
if (q <= 0)
|
|
592
614
|
return dt0(lowerTail, logP);
|
|
593
615
|
if (q >= 1)
|
|
@@ -685,8 +707,16 @@ function fPair(q, m, n) {
|
|
|
685
707
|
const d = ddAdd(mq, [n, 0]);
|
|
686
708
|
return [ddDiv(mq, d), ddDiv([n, 0], d)];
|
|
687
709
|
}
|
|
710
|
+
// m q is 1e290 or more (or overflows): 1 - x = zc where zc = n/(m q) is below the double-double precision
|
|
688
711
|
const zc = n / m / q;
|
|
689
|
-
|
|
712
|
+
if (zc < 2 ** -106)
|
|
713
|
+
return [twoSum(1, -zc), [zc, 0]];
|
|
714
|
+
// otherwise (n comparable to m q: zc = 1 gave x = 0 for 1/2; issue #158) x = 1/(1 + r), 1 - x = r/(1 + r),
|
|
715
|
+
// r = n/(m q) divided by the larger of m and q first (n/max(m, q) is then within the double range)
|
|
716
|
+
const [big, small] = m >= q ? [m, q] : [q, m];
|
|
717
|
+
const r = ddDiv(ddDiv([n, 0], [big, 0]), [small, 0]);
|
|
718
|
+
const d = ddAdd(r, [1, 0]);
|
|
719
|
+
return [ddDiv([1, 0], d), ddDiv(r, d)];
|
|
690
720
|
}
|
|
691
721
|
/** The Odds of x = m q/(n + m q): k = n/(m q), from its logs where it is out of the double range */
|
|
692
722
|
function fOdds(q, m, n, xd, xcd) {
|
|
@@ -697,11 +727,23 @@ function fOdds(q, m, n, xd, xcd) {
|
|
|
697
727
|
const lk = Math.log(n) - Math.log(m) - Math.log(q);
|
|
698
728
|
return { k, lk, l1mx: lk - Math.log1p(k) };
|
|
699
729
|
}
|
|
730
|
+
/** log(m/n), from the logs where the ratio is below the normal range (m/n underflows for finite positive m, n) */
|
|
731
|
+
function logRatio(m, n) {
|
|
732
|
+
const r = m / n;
|
|
733
|
+
return r >= 2 ** -1022 ? Math.log(r) : Math.log(m) - Math.log(n);
|
|
734
|
+
}
|
|
735
|
+
/** Whether x = m q/(n + m q) is below 1e-306 (the tiny-q series), with m q/n from its log where m/n underflows */
|
|
736
|
+
function tinyFx(q, m, n) {
|
|
737
|
+
const r = m / n;
|
|
738
|
+
if (r >= 2 ** -1022)
|
|
739
|
+
return r * q < 1e-306;
|
|
740
|
+
return logRatio(m, n) + Math.log(q) < Math.log(1e-306);
|
|
741
|
+
}
|
|
700
742
|
function dnfCore(q, m, n, ncp, log) {
|
|
701
|
-
if ((m
|
|
743
|
+
if (tinyFx(q, m, n)) {
|
|
702
744
|
// First term of the series: f(q) ≈ exp(-λ/2) (m q / n)^a / (B(a, b) q)
|
|
703
745
|
const a = m / 2;
|
|
704
|
-
const l = -ncp / 2 + a * (
|
|
746
|
+
const l = -ncp / 2 + a * (logRatio(m, n) + Math.log(q)) - lbeta(a, n / 2) - Math.log(q);
|
|
705
747
|
return log ? l : Math.exp(l);
|
|
706
748
|
}
|
|
707
749
|
const [xd, xcd] = fPair(q, m, n);
|
|
@@ -713,13 +755,13 @@ function dnfCore(q, m, n, ncp, log) {
|
|
|
713
755
|
*/
|
|
714
756
|
function pnfTiny(q, m, n, ncp, lower, log) {
|
|
715
757
|
const a = m / 2;
|
|
716
|
-
const l = -ncp / 2 + a * (
|
|
758
|
+
const l = -ncp / 2 + a * (logRatio(m, n) + Math.log(q)) - Math.log(a) - lbeta(a, n / 2);
|
|
717
759
|
if (lower)
|
|
718
760
|
return log ? l : Math.exp(l);
|
|
719
761
|
return log ? Math.log1p(-Math.exp(l)) : -Math.expm1(l);
|
|
720
762
|
}
|
|
721
763
|
export function pnfRaw(q, m, n, ncp, lower, log) {
|
|
722
|
-
if ((m
|
|
764
|
+
if (tinyFx(q, m, n))
|
|
723
765
|
return pnfTiny(q, m, n, ncp, lower, log);
|
|
724
766
|
const [xd, xcd] = fPair(q, m, n);
|
|
725
767
|
return pnbetaRaw(m / 2, n / 2, ncp, xd, xcd, lower, log, fOdds(q, m, n, xd, xcd));
|
|
@@ -739,13 +781,15 @@ export function dnf1(x, m, n, ncp, log) {
|
|
|
739
781
|
return x === 1 ? Infinity : log ? -Infinity : 0;
|
|
740
782
|
return x === 0 ? NaN : dfDensity1(x, m, n, log);
|
|
741
783
|
}
|
|
742
|
-
|
|
784
|
+
// (df2 so large that χ²_n/n is 1 on the scale of the result, by pnf1's criterion: the noncentral χ² limit, as for
|
|
785
|
+
// df2 = Inf; the tiny-q first term in dnfCore gave df(3, 2, 1e308, ncp = 1) 0.6065 for 0.0956)
|
|
786
|
+
if (n === Infinity || (n > 1e40 && hugeDf2(x, m, n, ncp))) {
|
|
743
787
|
// F = χ²_m(ncp) / m
|
|
744
788
|
const v = dnchisq1(x * m, m, ncp, log);
|
|
745
789
|
return log ? v + Math.log(m) : v * m;
|
|
746
790
|
}
|
|
747
791
|
if (x === Infinity)
|
|
748
|
-
return
|
|
792
|
+
return log ? -Infinity : 0; // the density tends to 0 (R gives NaN at x = Inf when df2 is finite; #493)
|
|
749
793
|
if (x === 0) {
|
|
750
794
|
if (m < 2)
|
|
751
795
|
return Infinity;
|
|
@@ -753,7 +797,8 @@ export function dnf1(x, m, n, ncp, log) {
|
|
|
753
797
|
return log ? -Infinity : 0;
|
|
754
798
|
return log ? -ncp / 2 : Math.exp(-ncp / 2);
|
|
755
799
|
}
|
|
756
|
-
|
|
800
|
+
// (x (a + b)/a of the beta at x = m q/(n + m q), with a = m/2 and b = n/2)
|
|
801
|
+
if (ncp === 0 || negligibleNcp(ncp / 2, (1 + n / m) / (1 + n / (m * x))))
|
|
757
802
|
return dfDensity1(x, m, n, log);
|
|
758
803
|
return dnfCore(x, m, n, ncp, log);
|
|
759
804
|
}
|
|
@@ -772,7 +817,9 @@ export function pnf1(q, m, n, ncp, lowerTail, logP) {
|
|
|
772
817
|
return NaN;
|
|
773
818
|
if (n === Infinity)
|
|
774
819
|
return pnchisq1(q * m, m, ncp, lowerTail, logP);
|
|
775
|
-
if (
|
|
820
|
+
if (n > 1e40 && hugeDf2(q, m, n, ncp))
|
|
821
|
+
return pnchisq1(q * m, m, ncp, lowerTail, logP);
|
|
822
|
+
if (tinyFx(q, m, n))
|
|
776
823
|
return pnfTiny(q, m, n, ncp, lowerTail, logP);
|
|
777
824
|
if (ncp === 0)
|
|
778
825
|
return pf1(q, m, n, lowerTail, logP);
|
|
@@ -781,6 +828,16 @@ export function pnf1(q, m, n, ncp, lowerTail, logP) {
|
|
|
781
828
|
return neg;
|
|
782
829
|
return pnfRaw(q, m, n, ncp, lowerTail, logP);
|
|
783
830
|
}
|
|
831
|
+
/**
|
|
832
|
+
* Whether df2 = n is so large that W = χ²_n/n is 1 on the scale of P[F <= q] (both tails), so that the noncentral χ²
|
|
833
|
+
* limit (df2 = Inf) is the distribution function to the double (also taken for the density in dnf1): the criterion
|
|
834
|
+
* of qnfRaw, with |log p| that of the smaller tail of the limit at q. (The series in x = m q/(n + m q) gave
|
|
835
|
+
* pf(2.9957, 2, 1e308, ncp = 1, upper) -0.817 for 0.13271; R's pnf takes the limit for every df2 > 1e8.)
|
|
836
|
+
*/
|
|
837
|
+
function hugeDf2(q, m, n, ncp) {
|
|
838
|
+
const lp = -Math.min(pnchisq1(q * m, m, ncp, true, true), pnchisq1(q * m, m, ncp, false, true));
|
|
839
|
+
return n > 1e40 * (lp + 1) && n > 1e20 * (m + 2 * lp + 1e3 + 4 * Math.sqrt((m + ncp) * (lp + 1)));
|
|
840
|
+
}
|
|
784
841
|
/** Below this log a probability rounds to 0 in double precision (as in src/distributions/nchisq.ts) */
|
|
785
842
|
const LOG_NEGLIGIBLE = -746;
|
|
786
843
|
/**
|
|
@@ -791,27 +848,31 @@ const LOG_NEGLIGIBLE = -746;
|
|
|
791
848
|
* bound is below the double range.
|
|
792
849
|
*/
|
|
793
850
|
function fTailBound(q, m, n, ncp) {
|
|
794
|
-
|
|
795
|
-
if (!(c > 0 && Number.isFinite(c)))
|
|
796
|
-
return undefined;
|
|
797
|
-
// (c s formed first: 2c overflows for q near the largest double)
|
|
798
|
-
const lower = (s) => -(m / 2) * Math.log1p(2 * s) - (ncp * s) / (1 + 2 * s) - (n / 2) * Math.log1p(-2 * (c * s));
|
|
799
|
-
const upper = (s) => -(m / 2) * Math.log1p(-2 * s) + (ncp * s) / (1 - 2 * s) - (n / 2) * Math.log1p(2 * (c * s));
|
|
800
|
-
const best = (f, sMax) => {
|
|
801
|
-
let b = Infinity;
|
|
802
|
-
for (let i = 1; i <= 60; i++) {
|
|
803
|
-
const v = Math.min(f(sMax * 2 ** -i), f(sMax * (1 - 2 ** -i)));
|
|
804
|
-
if (v < b)
|
|
805
|
-
b = v;
|
|
806
|
-
}
|
|
807
|
-
return b;
|
|
808
|
-
};
|
|
809
|
-
if (best(lower, 1 / (2 * c)) < LOG_NEGLIGIBLE)
|
|
851
|
+
if (fChernoffLog(q, m, n, ncp, true) < LOG_NEGLIGIBLE)
|
|
810
852
|
return "lower";
|
|
811
|
-
if (
|
|
853
|
+
if (fChernoffLog(q, m, n, ncp, false) < LOG_NEGLIGIBLE)
|
|
812
854
|
return "upper";
|
|
813
855
|
return undefined;
|
|
814
856
|
}
|
|
857
|
+
/** The bound of fTailBound on log P[F <= q] (lower) or log P[F >= q], as a log (NaN where q m/n is 0 or overflows) */
|
|
858
|
+
function fChernoffLog(q, m, n, ncp, lower) {
|
|
859
|
+
// (as q (m/n) where q m overflows, as at the largest double)
|
|
860
|
+
const c = q * m < Infinity ? (q * m) / n : q * (m / n);
|
|
861
|
+
if (!(c > 0 && Number.isFinite(c)))
|
|
862
|
+
return NaN;
|
|
863
|
+
// (c s formed first: 2c overflows for q near the largest double)
|
|
864
|
+
const f = lower
|
|
865
|
+
? (s) => -(m / 2) * Math.log1p(2 * s) - (ncp * s) / (1 + 2 * s) - (n / 2) * Math.log1p(-2 * (c * s))
|
|
866
|
+
: (s) => -(m / 2) * Math.log1p(-2 * s) + (ncp * s) / (1 - 2 * s) - (n / 2) * Math.log1p(2 * (c * s));
|
|
867
|
+
const sMax = lower ? 1 / (2 * c) : 0.5;
|
|
868
|
+
let b = Infinity;
|
|
869
|
+
for (let i = 1; i <= 60; i++) {
|
|
870
|
+
const v = Math.min(f(sMax * 2 ** -i), f(sMax * (1 - 2 ** -i)));
|
|
871
|
+
if (v < b)
|
|
872
|
+
b = v;
|
|
873
|
+
}
|
|
874
|
+
return b;
|
|
875
|
+
}
|
|
815
876
|
export function qnf1(p, m, n, ncp, lowerTail, logP) {
|
|
816
877
|
if (Number.isNaN(p) || Number.isNaN(m) || Number.isNaN(n) || Number.isNaN(ncp))
|
|
817
878
|
return NaN;
|
|
@@ -831,6 +892,26 @@ export function qnf1(p, m, n, ncp, lowerTail, logP) {
|
|
|
831
892
|
function qnfRaw(p, m, n, ncp, lowerTail, logP) {
|
|
832
893
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
833
894
|
const cdfAt = memoCdf((x, l, log) => pnfRaw(x, m, n, ncp, l, log));
|
|
895
|
+
// The Chernoff bounds at the smallest and largest doubles (fChernoffLog), which decide the ends where the distribution
|
|
896
|
+
// function there is NaN (out of the range of its methods): where the bound B on log P[F <= MAX] is below the target,
|
|
897
|
+
// no double reaches a lower-tail target, and the upper tail stays above 1 - e^B, above an upper-tail target (at most
|
|
898
|
+
// 1/2) where B < -log 2; likewise at the smallest double. For a subnormal df1 and a huge λ, F lies beyond the doubles
|
|
899
|
+
// but for the point mass e^(-λ/2) at 0 (R does not converge, and the search over NaN took seconds: qf(5.4e-11, 3.7e-313,
|
|
900
|
+
// 1249, { ncp: 1.7e243 }))
|
|
901
|
+
if (fChernoffLog(Number.MAX_VALUE, m, n, ncp, true) < (lower ? logTarget : -Math.LN2))
|
|
902
|
+
return Infinity;
|
|
903
|
+
if (fChernoffLog(Number.MIN_VALUE, m, n, ncp, false) < (lower ? -Math.LN2 : logTarget))
|
|
904
|
+
return 0;
|
|
905
|
+
// A df2 so large that W = χ²_n/n is 1 on the scale of the quantile: the noncentral χ² limit, as for df2 = Inf. log W
|
|
906
|
+
// has a spread s = sqrt(2/n); it moves the quantile of log F by at most about s (sqrt(2 |log p|) + 1) where W alone
|
|
907
|
+
// would make the tail, and by s² L otherwise, L = |d log P/d log x| of the limit, at most of the order of
|
|
908
|
+
// m + 2 |log p| + 2 sqrt((m + λ)(|log p| + 1)) (the power m/2 near 0, the exponential tail, the normal bulk of a large
|
|
909
|
+
// λ); both are below 1e-20 here. (Out of the range of the distribution function, at both a huge df2 and λ, the search
|
|
910
|
+
// took seconds: qf(exp(-385.8), 1.05, 1.1e264, { ncp: 1.7e262 }))
|
|
911
|
+
const lp = -logTarget;
|
|
912
|
+
if (n > 1e40 * (lp + 1) && n > 1e20 * (m + 2 * lp + 1e3 + 4 * Math.sqrt((m + ncp) * (lp + 1)))) {
|
|
913
|
+
return qnchisq1(p, m, ncp, lowerTail, logP) / m;
|
|
914
|
+
}
|
|
834
915
|
const atMin = cdfAt(Number.MIN_VALUE, lower, true);
|
|
835
916
|
if (lower ? atMin >= logTarget : atMin <= logTarget)
|
|
836
917
|
return 0;
|
|
@@ -841,7 +922,16 @@ function qnfRaw(p, m, n, ncp, lowerTail, logP) {
|
|
|
841
922
|
// means of the χ² variables per degree of freedom) where the parameters are out of the central quantile's range
|
|
842
923
|
const nu = (m + ncp) * ((m + ncp) / (m + 2 * ncp));
|
|
843
924
|
const central = method(m / 2, n / 2, ncp) === "mixture" && inCentralRange(m) && inCentralRange(n);
|
|
844
|
-
|
|
925
|
+
// Out of the mixture's range (λ/2 large) χ²(m, λ)/m is (m + λ)/m to within its spread of order 1/sqrt(λ), so F is
|
|
926
|
+
// that times n/χ²_n, the F quantile at df1 = Inf (from (m + λ)/m alone the search took 1 s, through an integral of
|
|
927
|
+
// 0.5 s far in the left tail: qf(3.97e-237, 364.4, 1.476, { ncp: 1.94e11 }))
|
|
928
|
+
const limit = !central && inCentralRange(n) ? qf1(logTarget, Infinity, n, lower, true) : 1;
|
|
929
|
+
let x0 = ((m + ncp) / m) * (central ? qf1(logTarget, nu, n, lower, true) : limit);
|
|
930
|
+
// For a df2 above the central range, the quantile of the χ² limit (df2 = Inf): from (m + λ)/m the search took seconds
|
|
931
|
+
// in the far tails, at 0.1 s per call of the integral (qf(exp(-645), 314, 5.2e16, { ncp: 2393, lowerTail: false }))
|
|
932
|
+
const limitStart = n > 1e15 && inCentralRange(m);
|
|
933
|
+
if (limitStart)
|
|
934
|
+
x0 = qnchisq1(logTarget, m, ncp, lower, true) / m;
|
|
845
935
|
if (!(x0 > 0) || !Number.isFinite(x0))
|
|
846
936
|
x0 = 1;
|
|
847
937
|
const passes = (x) => (lower ? cdfAt(x, true, true) >= logTarget : cdfAt(x, false, true) <= logTarget);
|
|
@@ -860,6 +950,9 @@ function qnfRaw(p, m, n, ncp, lowerTail, logP) {
|
|
|
860
950
|
hi: Infinity,
|
|
861
951
|
positive: true,
|
|
862
952
|
maxIter: 60,
|
|
953
|
+
// (there the distribution function is the integral, accurate to about 1e-13 (see the known violations), at 0.2 s a
|
|
954
|
+
// call: the steps over its noise took seconds (qf(exp(-3.79), 314.2, 5.2e16, { ncp: 2393 })))
|
|
955
|
+
...(limitStart && { residualTolerance: 1e-12 * Math.max(1, -logTarget) }),
|
|
863
956
|
});
|
|
864
957
|
const search = () => searchDoubles(x, Number.MIN_VALUE, Number.MAX_VALUE, passes);
|
|
865
958
|
return confirmQuantile(x, cdfAt(x, lower, true), logTarget, search, passes);
|