@numero-uno-jpn/rstats 0.15.3 → 0.17.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 +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- 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/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -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.js +15 -2
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- 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/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -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 +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- 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 +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- 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 +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -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 +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -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/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -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 +4 -1
- package/dist/special/choose.js +24 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- 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 +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
|
@@ -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 = [];
|
|
@@ -584,7 +592,11 @@ export function dnbeta1(x, a, b, ncp, log) {
|
|
|
584
592
|
return log ? -Infinity : 0;
|
|
585
593
|
if (b < 1)
|
|
586
594
|
return Infinity;
|
|
587
|
-
|
|
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;
|
|
588
600
|
}
|
|
589
601
|
if (negligibleNcp(mu, x * ((a + b) / a)))
|
|
590
602
|
return dbeta1(x, a, b, log);
|
|
@@ -593,6 +605,11 @@ export function dnbeta1(x, a, b, ncp, log) {
|
|
|
593
605
|
export function pnbeta1(q, a, b, ncp, lowerTail, logP) {
|
|
594
606
|
if (Number.isNaN(q) || Number.isNaN(a) || Number.isNaN(b) || Number.isNaN(ncp))
|
|
595
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;
|
|
596
613
|
if (q <= 0)
|
|
597
614
|
return dt0(lowerTail, logP);
|
|
598
615
|
if (q >= 1)
|
|
@@ -690,8 +707,16 @@ function fPair(q, m, n) {
|
|
|
690
707
|
const d = ddAdd(mq, [n, 0]);
|
|
691
708
|
return [ddDiv(mq, d), ddDiv([n, 0], d)];
|
|
692
709
|
}
|
|
710
|
+
// m q is 1e290 or more (or overflows): 1 - x = zc where zc = n/(m q) is below the double-double precision
|
|
693
711
|
const zc = n / m / q;
|
|
694
|
-
|
|
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)];
|
|
695
720
|
}
|
|
696
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 */
|
|
697
722
|
function fOdds(q, m, n, xd, xcd) {
|
|
@@ -702,11 +727,23 @@ function fOdds(q, m, n, xd, xcd) {
|
|
|
702
727
|
const lk = Math.log(n) - Math.log(m) - Math.log(q);
|
|
703
728
|
return { k, lk, l1mx: lk - Math.log1p(k) };
|
|
704
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
|
+
}
|
|
705
742
|
function dnfCore(q, m, n, ncp, log) {
|
|
706
|
-
if ((m
|
|
743
|
+
if (tinyFx(q, m, n)) {
|
|
707
744
|
// First term of the series: f(q) ≈ exp(-λ/2) (m q / n)^a / (B(a, b) q)
|
|
708
745
|
const a = m / 2;
|
|
709
|
-
const l = -ncp / 2 + a * (
|
|
746
|
+
const l = -ncp / 2 + a * (logRatio(m, n) + Math.log(q)) - lbeta(a, n / 2) - Math.log(q);
|
|
710
747
|
return log ? l : Math.exp(l);
|
|
711
748
|
}
|
|
712
749
|
const [xd, xcd] = fPair(q, m, n);
|
|
@@ -718,13 +755,13 @@ function dnfCore(q, m, n, ncp, log) {
|
|
|
718
755
|
*/
|
|
719
756
|
function pnfTiny(q, m, n, ncp, lower, log) {
|
|
720
757
|
const a = m / 2;
|
|
721
|
-
const l = -ncp / 2 + a * (
|
|
758
|
+
const l = -ncp / 2 + a * (logRatio(m, n) + Math.log(q)) - Math.log(a) - lbeta(a, n / 2);
|
|
722
759
|
if (lower)
|
|
723
760
|
return log ? l : Math.exp(l);
|
|
724
761
|
return log ? Math.log1p(-Math.exp(l)) : -Math.expm1(l);
|
|
725
762
|
}
|
|
726
763
|
export function pnfRaw(q, m, n, ncp, lower, log) {
|
|
727
|
-
if ((m
|
|
764
|
+
if (tinyFx(q, m, n))
|
|
728
765
|
return pnfTiny(q, m, n, ncp, lower, log);
|
|
729
766
|
const [xd, xcd] = fPair(q, m, n);
|
|
730
767
|
return pnbetaRaw(m / 2, n / 2, ncp, xd, xcd, lower, log, fOdds(q, m, n, xd, xcd));
|
|
@@ -744,13 +781,15 @@ export function dnf1(x, m, n, ncp, log) {
|
|
|
744
781
|
return x === 1 ? Infinity : log ? -Infinity : 0;
|
|
745
782
|
return x === 0 ? NaN : dfDensity1(x, m, n, log);
|
|
746
783
|
}
|
|
747
|
-
|
|
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))) {
|
|
748
787
|
// F = χ²_m(ncp) / m
|
|
749
788
|
const v = dnchisq1(x * m, m, ncp, log);
|
|
750
789
|
return log ? v + Math.log(m) : v * m;
|
|
751
790
|
}
|
|
752
791
|
if (x === Infinity)
|
|
753
|
-
return
|
|
792
|
+
return log ? -Infinity : 0; // the density tends to 0 (R gives NaN at x = Inf when df2 is finite; #493)
|
|
754
793
|
if (x === 0) {
|
|
755
794
|
if (m < 2)
|
|
756
795
|
return Infinity;
|
|
@@ -778,7 +817,9 @@ export function pnf1(q, m, n, ncp, lowerTail, logP) {
|
|
|
778
817
|
return NaN;
|
|
779
818
|
if (n === Infinity)
|
|
780
819
|
return pnchisq1(q * m, m, ncp, lowerTail, logP);
|
|
781
|
-
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))
|
|
782
823
|
return pnfTiny(q, m, n, ncp, lowerTail, logP);
|
|
783
824
|
if (ncp === 0)
|
|
784
825
|
return pf1(q, m, n, lowerTail, logP);
|
|
@@ -787,6 +828,16 @@ export function pnf1(q, m, n, ncp, lowerTail, logP) {
|
|
|
787
828
|
return neg;
|
|
788
829
|
return pnfRaw(q, m, n, ncp, lowerTail, logP);
|
|
789
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
|
+
}
|
|
790
841
|
/** Below this log a probability rounds to 0 in double precision (as in src/distributions/nchisq.ts) */
|
|
791
842
|
const LOG_NEGLIGIBLE = -746;
|
|
792
843
|
/**
|
|
@@ -79,6 +79,9 @@ function validParams(df, ncp) {
|
|
|
79
79
|
* A value near 1, and its log, are built from the small value of the other side
|
|
80
80
|
*/
|
|
81
81
|
function pnchisqBoth(x, df, ncp, lower) {
|
|
82
|
+
// the end point (the asymptotic forms do not end for x = Infinity: qchisq's bracket reached it; issue #157)
|
|
83
|
+
if (x === Infinity)
|
|
84
|
+
return lower ? { lin: 1, log: 0 } : { lin: 0, log: -Infinity };
|
|
82
85
|
const mu = ncp / 2;
|
|
83
86
|
// The series has too many terms (or gammaInc is slow for its huge shapes): integrate an asymptotic form of the density
|
|
84
87
|
const method = asymptoticMethod(df, besselArg(x, ncp));
|
|
@@ -91,8 +94,12 @@ function pnchisqBoth(x, df, ncp, lower) {
|
|
|
91
94
|
const x2 = 2 * (x / 2);
|
|
92
95
|
const e = Math.expm1((df / 2) * lt);
|
|
93
96
|
const lo = pnchisqBoth(x2, df, ncp, true);
|
|
94
|
-
if (lower)
|
|
95
|
-
|
|
97
|
+
if (lower) {
|
|
98
|
+
// (a factor beyond the doubles, for a large df, times a probability that underflowed to 0 made NaN: from the log;
|
|
99
|
+
// issue #115)
|
|
100
|
+
const l = lo.log + (df / 2) * lt;
|
|
101
|
+
return { lin: 1 + e < Infinity ? lo.lin * (1 + e) : Math.exp(l), log: l };
|
|
102
|
+
}
|
|
96
103
|
const up = pnchisqBoth(x2, df, ncp, false);
|
|
97
104
|
const r = (lo.lin * e) / up.lin;
|
|
98
105
|
return Number.isFinite(r) ? { lin: up.lin - lo.lin * e, log: up.log + Math.log1p(-r) } : up;
|
|
@@ -163,7 +170,19 @@ function dnchisqRaw(x, df, ncp, log) {
|
|
|
163
170
|
if (lt !== undefined) {
|
|
164
171
|
// the density at x2 times (x/x2)^(k/2 - 1) (subnormalHalf)
|
|
165
172
|
const x2 = 2 * (x / 2);
|
|
166
|
-
|
|
173
|
+
// The j = 1 component (power y^a) is not negligible against j = 0 (y^(a - 1)) where a is not large against mu y:
|
|
174
|
+
// their ratio at x2 is rho = a/(mu y2), and the density at x is that at x2 times r^(a - 1) (rho + r)/(rho + 1),
|
|
175
|
+
// r = x/x2. For df = 0 (no j = 0 term, rho = 0) the factor is r^a = 1. Where rho > 2^60 the change is below
|
|
176
|
+
// double precision and the factor is r^(a - 1) alone (issue #147)
|
|
177
|
+
const rho = a / (x2 / 2) / mu;
|
|
178
|
+
const mix = rho < 2 ** 60 ? Math.log1p((x - x2) / x2 / (rho + 1)) : 0;
|
|
179
|
+
if (log)
|
|
180
|
+
return dnchisqRaw(x2, df, ncp, true) + (a - 1) * lt + mix;
|
|
181
|
+
// (where the factor overflows, for a large df, the density at x2 has underflowed to 0: from the log; issue #115)
|
|
182
|
+
const f = Math.exp((a - 1) * lt + mix);
|
|
183
|
+
return f < Infinity
|
|
184
|
+
? dnchisqRaw(x2, df, ncp, false) * f
|
|
185
|
+
: Math.exp(dnchisqRaw(x2, df, ncp, true) + (a - 1) * lt + mix);
|
|
167
186
|
}
|
|
168
187
|
// The ratio of terms w_j g_j is mu y / (j (a + j - 1)) = (z/2)² / (j (a + j - 1)); the peak is at the j where it
|
|
169
188
|
// equals 1, the positive root of j² + (a - 1) j - (z/2)² (written without cancellation for a > 1)
|
|
@@ -630,8 +649,9 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
|
|
|
630
649
|
}
|
|
631
650
|
else
|
|
632
651
|
x0 = c * qgamma1(logTarget, nu / 2, 2, lower, true);
|
|
652
|
+
// (df + ncp overflows where the quantile can lie beyond the doubles; issue #157)
|
|
633
653
|
if (!(x0 > 0) || !Number.isFinite(x0))
|
|
634
|
-
x0 = df + ncp;
|
|
654
|
+
x0 = Math.min(df + ncp, Number.MAX_VALUE);
|
|
635
655
|
const passes = (x) => (lower ? cdf(x, true).log >= logTarget : cdf(x, false).log <= logTarget);
|
|
636
656
|
const narrowSpread = Math.sqrt(2 * (df + 2 * ncp)) < 1e-10 * (df + ncp);
|
|
637
657
|
const narrow = narrowSpread ? narrowQuantile(x0, Number.MAX_VALUE, passes) : undefined;
|
|
@@ -639,10 +659,14 @@ export function qnchisq1(p, df, ncp, lowerTail, logP) {
|
|
|
639
659
|
return narrow;
|
|
640
660
|
// Bracket by doubling the upper end (the distribution function is expensive for very large x)
|
|
641
661
|
let lo = 0;
|
|
642
|
-
|
|
662
|
+
// up to the largest double: not passing the target there, the quantile is beyond it (issue #157: Infinity was
|
|
663
|
+
// reached and the distribution function did not end)
|
|
664
|
+
let hi = Math.min(Math.max(x0, df + ncp, 1), Number.MAX_VALUE);
|
|
643
665
|
for (let i = 0; i < 2000 && !passes(hi); i++) {
|
|
666
|
+
if (hi === Number.MAX_VALUE)
|
|
667
|
+
return Infinity;
|
|
644
668
|
lo = hi;
|
|
645
|
-
hi
|
|
669
|
+
hi = Math.min(hi * 2, Number.MAX_VALUE);
|
|
646
670
|
}
|
|
647
671
|
const x = invertContinuous({
|
|
648
672
|
logTarget,
|
|
@@ -7,29 +7,62 @@
|
|
|
7
7
|
// When mu is given, p = n / (n + mu) and q = mu / (n + mu) are formed in double-double
|
|
8
8
|
// (for n ≫ mu, rounding 1 - p gives q a large relative error). size = Inf is the Poisson distribution.
|
|
9
9
|
// Quantiles use an integer search from a Cornish-Fisher starting value.
|
|
10
|
+
import { logAddExp, logSubExp } from "../internal/ddexp.js";
|
|
10
11
|
import { vectorize } from "../internal/vectorize.js";
|
|
11
12
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
12
13
|
import { discreteQuantile } from "../internal/discrete.js";
|
|
13
|
-
import { nonInt } from "../internal/integer.js";
|
|
14
|
-
import { betaIncDD } from "../special/incbet.js";
|
|
14
|
+
import { countAtMost, nonInt } from "../internal/integer.js";
|
|
15
|
+
import { betaIncDD, betaIncTinyX } from "../special/incbet.js";
|
|
15
16
|
import { prefactorParts } from "../special/incbetPrefactor.js";
|
|
16
17
|
import { dbinomRawDD, dpoisRaw } from "../special/loader.js";
|
|
17
|
-
import { ddAdd, ddDiv, ddMul, twoSum } from "../special/dd.js";
|
|
18
|
+
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, ddSub, twoSum } from "../special/dd.js";
|
|
18
19
|
import { qnorm } from "./normal.js";
|
|
19
20
|
import { qgamma1 } from "./gamma.js";
|
|
20
21
|
import { ppois1, qpois1 } from "./poisson.js";
|
|
21
22
|
import { probPair } from "./geometric.js";
|
|
23
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
24
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
25
|
+
const DNBINOM_KEYS = ["prob", "mu", "log"];
|
|
26
|
+
const PNBINOM_KEYS = ["prob", "mu", "lowerTail", "logP"];
|
|
27
|
+
const QNBINOM_KEYS = ["prob", "mu", "lowerTail", "logP"];
|
|
22
28
|
function fromMu(size, mu) {
|
|
23
29
|
const s = twoSum(size, mu);
|
|
24
|
-
|
|
30
|
+
if (Number.isFinite(s[0]))
|
|
31
|
+
return { size, pd: ddDiv([size, 0], s), qd: ddDiv([mu, 0], s) };
|
|
32
|
+
// size + mu overflows (both near the largest double): from the ratio r of the smaller to the larger, 1 / (1 + r)
|
|
33
|
+
// and r / (1 + r) (p and q were both 0)
|
|
34
|
+
const big = size >= mu;
|
|
35
|
+
const r = big ? mu / size : size / mu;
|
|
36
|
+
const one = ddDiv([1, 0], twoSum(1, r));
|
|
37
|
+
const other = ddDiv([r, 0], twoSum(1, r));
|
|
38
|
+
return big ? { size, pd: one, qd: other } : { size, pd: other, qd: one };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* fromMu for the density: where the smaller of p and q falls below the normal range (0 for a ratio below 2^-1074:
|
|
42
|
+
* dnbinom(1, 1e-200, { mu: 1e200 }) was 0 for 1e-200, dnbinom(1, MAX, { mu: MIN, log: true }) -Inf for -744.44;
|
|
43
|
+
* issue #95), it is kept times 2^1100 (from size 2^550 / ((size + mu) 2^-550), exact powers of 2), a normal number,
|
|
44
|
+
* and dbinomRawDD takes the shift. Otherwise as fromMu
|
|
45
|
+
*/
|
|
46
|
+
function fromMuDensity(size, mu) {
|
|
47
|
+
const par = fromMu(size, mu);
|
|
48
|
+
const pSmall = par.pd[0] < MIN_NORMAL;
|
|
49
|
+
if (!pSmall && !(par.qd[0] < MIN_NORMAL))
|
|
50
|
+
return par;
|
|
51
|
+
const s = twoSum(size, mu);
|
|
52
|
+
const scaled = ddDiv([(pSmall ? size : mu) * 2 ** 550, 0], [s[0] * 2 ** -550, s[1] * 2 ** -550]);
|
|
53
|
+
return pSmall ? { ...par, pd: scaled, shift: [1100, 0] } : { ...par, qd: scaled, shift: [0, 1100] };
|
|
25
54
|
}
|
|
26
55
|
function fromProb(size, prob) {
|
|
27
56
|
return { size, ...probPair(prob) };
|
|
28
57
|
}
|
|
29
58
|
/** P[X = x] (x a nonnegative integer, 0 < size < ∞, 0 < p < 1) */
|
|
30
59
|
function dnbinomRaw(x, par, log) {
|
|
31
|
-
const { size, pd, qd } = par;
|
|
32
|
-
|
|
60
|
+
const { size, pd, qd, shift } = par;
|
|
61
|
+
// size + x (the trials of the binomial density below) overflows: not computed (it gave 0, and NaN with log; the
|
|
62
|
+
// density, 2.8e-155 at size = x = 1e308, would not agree with pnbinom there, as recorded in round 22)
|
|
63
|
+
if (!Number.isFinite(size + x))
|
|
64
|
+
return NaN;
|
|
65
|
+
const b = dbinomRawDD(size, x, pd, qd, log, undefined, shift);
|
|
33
66
|
if (x === 0)
|
|
34
67
|
return b;
|
|
35
68
|
const r = size / (size + x);
|
|
@@ -40,8 +73,48 @@ function dnbinomRaw(x, par, log) {
|
|
|
40
73
|
return log ? b + (Math.log(size) - Math.log(size + x)) : (b / (size + x)) * size;
|
|
41
74
|
}
|
|
42
75
|
const MIN_NORMAL = 2 ** -1022;
|
|
76
|
+
const LN2_DD = [0.6931471805599453, 2.3190468138462996e-17];
|
|
77
|
+
/** A probability kept times 2^k (fromMuDensity): its value (0 below the doubles) and its log in double-double */
|
|
78
|
+
function unshift(v, k) {
|
|
79
|
+
return { value: v[0] * 2 ** -(k / 2) * 2 ** -(k / 2), log: ddSub(ddLog(v), ddMulNum(LN2_DD, k)) };
|
|
80
|
+
}
|
|
43
81
|
/** P[X <= x] (lower) / P[X > x] (!lower) (x a nonnegative integer) */
|
|
44
82
|
function pnbinomRaw(x, par, lower, log) {
|
|
83
|
+
if (par.shift !== undefined) {
|
|
84
|
+
// p or q below the normal range (it was 0, and the incomplete beta took its end: pnbinom(0, MIN, { mu: MAX }) was 0
|
|
85
|
+
// for 1, qnbinom(0.5, ...) Infinity for 0; issue #103): I_p(size, x + 1) by the series for x below DBL_MIN, from
|
|
86
|
+
// the exact log of p (or of q, the other tail); (x + 1) p < 4 there, as the series needs
|
|
87
|
+
const [kp, kq] = par.shift;
|
|
88
|
+
// (the quantile search may ask for x = Infinity when its first step from the largest double overflows)
|
|
89
|
+
if (x === Infinity)
|
|
90
|
+
return lower ? (log ? 0 : 1) : log ? -Infinity : 0;
|
|
91
|
+
const r = kp > 0
|
|
92
|
+
? (({ value, log: lp }) => betaIncTinyX(par.size, x + 1, value, lp, lower, log))(unshift(par.pd, kp))
|
|
93
|
+
: (({ value, log: lq }) => betaIncTinyX(x + 1, par.size, value, lq, !lower, log))(unshift(par.qd, kq));
|
|
94
|
+
return log ? r[0] : r[1];
|
|
95
|
+
}
|
|
96
|
+
if (x + 1 - x !== 1) {
|
|
97
|
+
// Beyond 2^53, x + 1 rounds (to x, or up to x + 2) and I_p(size, x + 1) became I_p(size, x) (pnbinom(1e16, 1e16, 0.5) was 0.5; R too).
|
|
98
|
+
// Without x + 1: I_p(size, x + 1) = I_p(size, x) + dnbinom(x)
|
|
99
|
+
let v;
|
|
100
|
+
let term;
|
|
101
|
+
if (!log) {
|
|
102
|
+
const t = betaIncDD(par.size, x, par.pd, par.qd, lower, false);
|
|
103
|
+
term = dnbinomRaw(x, par, false);
|
|
104
|
+
v = lower ? t + term : t - term;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const lt = betaIncDD(par.size, x, par.pd, par.qd, lower, true);
|
|
108
|
+
term = dnbinomRaw(x, par, true);
|
|
109
|
+
v = lower ? logAddExp(lt, term) : logSubExp(lt, term);
|
|
110
|
+
}
|
|
111
|
+
if (Number.isNaN(term))
|
|
112
|
+
return NaN;
|
|
113
|
+
// The upper tail as a difference: where dnbinom is not resolved against it (both logs near -3.5e48, or the plain
|
|
114
|
+
// difference cancels), one unit of x is below the resolution of the result, and the rounded parameter below serves
|
|
115
|
+
if (lower || (log ? Number.isFinite(v) : v > 0))
|
|
116
|
+
return v;
|
|
117
|
+
}
|
|
45
118
|
const up = upperSeries(x, par);
|
|
46
119
|
if (up !== undefined) {
|
|
47
120
|
// The upper tail (the smaller side) was computed directly by the series
|
|
@@ -95,10 +168,18 @@ function upperSeries(x, par) {
|
|
|
95
168
|
return undefined;
|
|
96
169
|
}
|
|
97
170
|
function quantileRaw(p, par, lowerTail, logP) {
|
|
98
|
-
// Starting value from the Cornish-Fisher expansion
|
|
99
|
-
const
|
|
171
|
+
// Starting value from the Cornish-Fisher expansion (p or q kept times 2^k: scaled back, 0 below the doubles; #103)
|
|
172
|
+
const [kp, kq] = par.shift ?? [0, 0];
|
|
173
|
+
const prob = kp > 0 ? unshift(par.pd, kp).value : par.pd[0] + par.pd[1];
|
|
174
|
+
const qv = kq > 0 ? unshift(par.qd, kq).value : par.qd[0] + par.qd[1];
|
|
175
|
+
if (kp > 0) {
|
|
176
|
+
// X p is nearly gamma (shape = size): y0 = g / p from the logs (p may be 0 here)
|
|
177
|
+
const g = qgamma1(p, par.size, 1, lowerTail, logP);
|
|
178
|
+
const y0 = g > 0 ? Math.min(Math.exp(Math.log(g) - unshift(par.pd, kp).log[0]), Number.MAX_VALUE) : 0;
|
|
179
|
+
return discreteQuantile({ p, lowerTail, logP, cdf: (y, l, lg) => pnbinomRaw(y, par, l, lg), y0, lo: 0, hi: Infinity });
|
|
180
|
+
}
|
|
100
181
|
const Q = 1 / prob;
|
|
101
|
-
const P =
|
|
182
|
+
const P = qv * Q;
|
|
102
183
|
const mu = par.size * P;
|
|
103
184
|
const sigma = Math.sqrt(par.size * P * Q);
|
|
104
185
|
const gamma = (Q + P) / sigma;
|
|
@@ -108,7 +189,7 @@ function quantileRaw(p, par, lowerTail, logP) {
|
|
|
108
189
|
// this is a much closer starting value than Cornish-Fisher, reducing distribution-function evaluations
|
|
109
190
|
// -log q = log(1 + p/q) (taking log of a rounded q loses digits for small p)
|
|
110
191
|
const g = qgamma1(p, par.size, 1, lowerTail, logP);
|
|
111
|
-
y0 = g / Math.log1p(prob /
|
|
192
|
+
y0 = g / Math.log1p(prob / qv);
|
|
112
193
|
// A finite gamma quantile divided by a tiny -log q beyond the doubles: so is the quantile (the distribution
|
|
113
194
|
// function cannot be evaluated out there; qnbinom(2.2e-284, 2.7e37, { prob: 1.9e-279 }) threw a TypeError)
|
|
114
195
|
if (y0 === Infinity && g > 0 && Number.isFinite(g))
|
|
@@ -201,7 +282,7 @@ export function pnbinom1(q, size, prob, lowerTail, logP) {
|
|
|
201
282
|
return dt0(lowerTail, logP);
|
|
202
283
|
if (size === 0 || prob === 1 || !Number.isFinite(q))
|
|
203
284
|
return dt1(lowerTail, logP);
|
|
204
|
-
return pnbinomRaw(
|
|
285
|
+
return pnbinomRaw(countAtMost(q), fromProb(size, prob), lowerTail, logP);
|
|
205
286
|
}
|
|
206
287
|
export function qnbinom1(p, size, prob, lowerTail, logP) {
|
|
207
288
|
if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(prob))
|
|
@@ -242,7 +323,7 @@ export function dnbinomMu1(x, size, mu, log) {
|
|
|
242
323
|
return dpoisRaw(n, mu, log);
|
|
243
324
|
if (mu === 0)
|
|
244
325
|
return n === 0 ? (log ? 0 : 1) : d0(log);
|
|
245
|
-
return dnbinomRaw(n,
|
|
326
|
+
return dnbinomRaw(n, fromMuDensity(size, mu), log);
|
|
246
327
|
}
|
|
247
328
|
export function pnbinomMu1(q, size, mu, lowerTail, logP) {
|
|
248
329
|
if (Number.isNaN(q) || Number.isNaN(size) || Number.isNaN(mu))
|
|
@@ -255,25 +336,26 @@ export function pnbinomMu1(q, size, mu, lowerTail, logP) {
|
|
|
255
336
|
return dt1(lowerTail, logP);
|
|
256
337
|
if (size === Infinity)
|
|
257
338
|
return ppois1(q, mu, lowerTail, logP);
|
|
258
|
-
return pnbinomRaw(
|
|
339
|
+
return pnbinomRaw(countAtMost(q), fromMuDensity(size, mu), lowerTail, logP);
|
|
259
340
|
}
|
|
260
341
|
export function qnbinomMu1(p, size, mu, lowerTail, logP) {
|
|
261
342
|
if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(mu))
|
|
262
343
|
return NaN;
|
|
263
|
-
//
|
|
344
|
+
// A negative size or mu defines no distribution, as in dnbinom and pnbinom (R's qnbinom returns 0 for mu = 0 before
|
|
345
|
+
// checking them); size = Inf is the Poisson distribution, and size = 0 or mu = 0 gives 0 regardless of p
|
|
346
|
+
if (size < 0 || mu < 0)
|
|
347
|
+
return NaN;
|
|
264
348
|
if (size === Infinity)
|
|
265
349
|
return qpois1(p, mu, lowerTail, logP);
|
|
266
350
|
if (size === 0 || mu === 0)
|
|
267
351
|
return 0;
|
|
268
|
-
if (size < 0 || mu < 0)
|
|
269
|
-
return NaN;
|
|
270
352
|
// mu = Inf answers only at the ends (p = 0, 1) and is NaN otherwise (as in R)
|
|
271
353
|
if (mu === Infinity)
|
|
272
354
|
return quantileBoundaries(p, 0, Infinity, lowerTail, logP) ?? NaN;
|
|
273
355
|
const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
|
|
274
356
|
if (b !== undefined)
|
|
275
357
|
return b;
|
|
276
|
-
return quantileRaw(p,
|
|
358
|
+
return quantileRaw(p, fromMuDensity(size, mu), lowerTail, logP);
|
|
277
359
|
}
|
|
278
360
|
/** Accept exactly one of prob and mu (as in R, giving both or neither is an error) */
|
|
279
361
|
function which(options) {
|
|
@@ -307,6 +389,7 @@ function which(options) {
|
|
|
307
389
|
* @category Distributions
|
|
308
390
|
*/
|
|
309
391
|
export function dnbinom(x, size, options) {
|
|
392
|
+
checkOptions(options, DNBINOM_KEYS, "dnbinom"); // generated: option-keys.mjs
|
|
310
393
|
const log = options.log ?? false;
|
|
311
394
|
const w = which(options);
|
|
312
395
|
return w.kind === "prob"
|
|
@@ -334,6 +417,7 @@ export function dnbinom(x, size, options) {
|
|
|
334
417
|
* @category Distributions
|
|
335
418
|
*/
|
|
336
419
|
export function pnbinom(q, size, options) {
|
|
420
|
+
checkOptions(options, PNBINOM_KEYS, "pnbinom"); // generated: option-keys.mjs
|
|
337
421
|
const { lowerTail = true, logP = false } = options;
|
|
338
422
|
const w = which(options);
|
|
339
423
|
return w.kind === "prob"
|
|
@@ -361,6 +445,7 @@ export function pnbinom(q, size, options) {
|
|
|
361
445
|
* @category Distributions
|
|
362
446
|
*/
|
|
363
447
|
export function qnbinom(p, size, options) {
|
|
448
|
+
checkOptions(options, QNBINOM_KEYS, "qnbinom"); // generated: option-keys.mjs
|
|
364
449
|
const { lowerTail = true, logP = false } = options;
|
|
365
450
|
const w = which(options);
|
|
366
451
|
return w.kind === "prob"
|
|
@@ -6,6 +6,12 @@
|
|
|
6
6
|
import { erf, erfcxNonNegative } from "../special/erf.js";
|
|
7
7
|
import { ndtri, ndtriExp } from "../special/ndtri.js";
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
|
+
import { affine, standardizeValue } from "../internal/ddexp.js";
|
|
10
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
11
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
12
|
+
const DNORM_KEYS = ["mean", "sd", "log"];
|
|
13
|
+
const PNORM_KEYS = ["mean", "sd", "lowerTail", "logP"];
|
|
14
|
+
const QNORM_KEYS = ["mean", "sd", "lowerTail", "logP"];
|
|
9
15
|
const SQRT1_2 = Math.SQRT1_2;
|
|
10
16
|
/**
|
|
11
17
|
* exp(-a^2 / 2) (a >= 0) multiplied by scale.
|
|
@@ -41,7 +47,9 @@ function pnormStd(t, logP) {
|
|
|
41
47
|
if (t > -1)
|
|
42
48
|
return Math.log1p(-lowerTailProb(-t));
|
|
43
49
|
const a = -t;
|
|
44
|
-
|
|
50
|
+
// (a / 2) a, not (a a) / 2: the same value, without the overflow of a a above 1.34e154 (pnorm(-1.5e154, { logP: true })
|
|
51
|
+
// was -Infinity for -1.125e308; issue #33)
|
|
52
|
+
return Math.log(0.5 * erfcxNonNegative(a * SQRT1_2)) - (a / 2) * a;
|
|
45
53
|
}
|
|
46
54
|
function pnorm1(q, mean, sd, lowerTail, logP) {
|
|
47
55
|
if (Number.isNaN(q) || Number.isNaN(mean) || Number.isNaN(sd))
|
|
@@ -54,7 +62,8 @@ function pnorm1(q, mean, sd, lowerTail, logP) {
|
|
|
54
62
|
return NaN;
|
|
55
63
|
if (sd === 0)
|
|
56
64
|
return q < mean ? p0 : p1;
|
|
57
|
-
|
|
65
|
+
// (q - mean) / sd, also where q - mean overflows but the standardized value does not
|
|
66
|
+
const x = standardizeValue(q, mean, sd);
|
|
58
67
|
if (!Number.isFinite(x))
|
|
59
68
|
return q < mean ? p0 : p1;
|
|
60
69
|
return pnormStd(lowerTail ? x : -x, logP);
|
|
@@ -85,7 +94,8 @@ function qnorm1(p, mean, sd, lowerTail, logP) {
|
|
|
85
94
|
if (sd === 0)
|
|
86
95
|
return mean;
|
|
87
96
|
const z = logP ? ndtriLogRefined(p) : ndtri(p);
|
|
88
|
-
|
|
97
|
+
// mean + sd z, also where sd z overflows but the quantile does not
|
|
98
|
+
return affine(mean, sd, lowerTail ? z : -z);
|
|
89
99
|
}
|
|
90
100
|
const SQRT_2_OVER_PI = 0.797884560802865355879892119869;
|
|
91
101
|
/**
|
|
@@ -118,7 +128,7 @@ function dnorm1(x, mean, sd, log) {
|
|
|
118
128
|
return NaN; // Inf - Inf
|
|
119
129
|
if (sd === 0)
|
|
120
130
|
return x === mean ? Infinity : log ? -Infinity : 0;
|
|
121
|
-
const z = (x
|
|
131
|
+
const z = standardizeValue(x, mean, sd);
|
|
122
132
|
if (!Number.isFinite(z))
|
|
123
133
|
return log ? -Infinity : 0;
|
|
124
134
|
if (log)
|
|
@@ -171,6 +181,7 @@ function tinySdDensity(a, sd) {
|
|
|
171
181
|
* @category Distributions
|
|
172
182
|
*/
|
|
173
183
|
export function dnorm(x, options = {}) {
|
|
184
|
+
checkOptions(options, DNORM_KEYS, "dnorm"); // generated: option-keys.mjs
|
|
174
185
|
const { mean = 0, sd = 1, log = false } = options;
|
|
175
186
|
return vectorize(x, (v) => dnorm1(v, mean, sd, log));
|
|
176
187
|
}
|
|
@@ -193,6 +204,7 @@ export function dnorm(x, options = {}) {
|
|
|
193
204
|
* @category Distributions
|
|
194
205
|
*/
|
|
195
206
|
export function pnorm(q, options = {}) {
|
|
207
|
+
checkOptions(options, PNORM_KEYS, "pnorm"); // generated: option-keys.mjs
|
|
196
208
|
const { mean = 0, sd = 1, lowerTail = true, logP = false } = options;
|
|
197
209
|
return vectorize(q, (v) => pnorm1(v, mean, sd, lowerTail, logP));
|
|
198
210
|
}
|
|
@@ -213,6 +225,7 @@ export function pnorm(q, options = {}) {
|
|
|
213
225
|
* @category Distributions
|
|
214
226
|
*/
|
|
215
227
|
export function qnorm(p, options = {}) {
|
|
228
|
+
checkOptions(options, QNORM_KEYS, "qnorm"); // generated: option-keys.mjs
|
|
216
229
|
const { mean = 0, sd = 1, lowerTail = true, logP = false } = options;
|
|
217
230
|
return vectorize(p, (v) => qnorm1(v, mean, sd, lowerTail, logP));
|
|
218
231
|
}
|