@numero-uno-jpn/rstats 0.15.3 → 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 +166 -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-VLUL2HFW.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 +24 -4
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +12 -4
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +5 -0
- package/dist/distributions/f.js +32 -17
- package/dist/distributions/gamma.js +27 -3
- package/dist/distributions/geometric.js +49 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +2 -1
- package/dist/distributions/laplace.js +27 -6
- package/dist/distributions/logistic.js +4 -1
- package/dist/distributions/lognormal.js +8 -2
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +94 -17
- package/dist/distributions/normal.js +9 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +4 -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/triangular.js +10 -5
- package/dist/distributions/tukey.js +5 -2
- package/dist/distributions/weibull.js +10 -0
- 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 +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/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 +4 -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 +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.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- 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-ZIX7NYQF.js +0 -2
|
@@ -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,57 @@
|
|
|
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";
|
|
22
23
|
function fromMu(size, mu) {
|
|
23
24
|
const s = twoSum(size, mu);
|
|
24
|
-
|
|
25
|
+
if (Number.isFinite(s[0]))
|
|
26
|
+
return { size, pd: ddDiv([size, 0], s), qd: ddDiv([mu, 0], s) };
|
|
27
|
+
// size + mu overflows (both near the largest double): from the ratio r of the smaller to the larger, 1 / (1 + r)
|
|
28
|
+
// and r / (1 + r) (p and q were both 0)
|
|
29
|
+
const big = size >= mu;
|
|
30
|
+
const r = big ? mu / size : size / mu;
|
|
31
|
+
const one = ddDiv([1, 0], twoSum(1, r));
|
|
32
|
+
const other = ddDiv([r, 0], twoSum(1, r));
|
|
33
|
+
return big ? { size, pd: one, qd: other } : { size, pd: other, qd: one };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* fromMu for the density: where the smaller of p and q falls below the normal range (0 for a ratio below 2^-1074:
|
|
37
|
+
* dnbinom(1, 1e-200, { mu: 1e200 }) was 0 for 1e-200, dnbinom(1, MAX, { mu: MIN, log: true }) -Inf for -744.44;
|
|
38
|
+
* issue #95), it is kept times 2^1100 (from size 2^550 / ((size + mu) 2^-550), exact powers of 2), a normal number,
|
|
39
|
+
* and dbinomRawDD takes the shift. Otherwise as fromMu
|
|
40
|
+
*/
|
|
41
|
+
function fromMuDensity(size, mu) {
|
|
42
|
+
const par = fromMu(size, mu);
|
|
43
|
+
const pSmall = par.pd[0] < MIN_NORMAL;
|
|
44
|
+
if (!pSmall && !(par.qd[0] < MIN_NORMAL))
|
|
45
|
+
return par;
|
|
46
|
+
const s = twoSum(size, mu);
|
|
47
|
+
const scaled = ddDiv([(pSmall ? size : mu) * 2 ** 550, 0], [s[0] * 2 ** -550, s[1] * 2 ** -550]);
|
|
48
|
+
return pSmall ? { ...par, pd: scaled, shift: [1100, 0] } : { ...par, qd: scaled, shift: [0, 1100] };
|
|
25
49
|
}
|
|
26
50
|
function fromProb(size, prob) {
|
|
27
51
|
return { size, ...probPair(prob) };
|
|
28
52
|
}
|
|
29
53
|
/** P[X = x] (x a nonnegative integer, 0 < size < ∞, 0 < p < 1) */
|
|
30
54
|
function dnbinomRaw(x, par, log) {
|
|
31
|
-
const { size, pd, qd } = par;
|
|
32
|
-
|
|
55
|
+
const { size, pd, qd, shift } = par;
|
|
56
|
+
// size + x (the trials of the binomial density below) overflows: not computed (it gave 0, and NaN with log; the
|
|
57
|
+
// density, 2.8e-155 at size = x = 1e308, would not agree with pnbinom there, as recorded in round 22)
|
|
58
|
+
if (!Number.isFinite(size + x))
|
|
59
|
+
return NaN;
|
|
60
|
+
const b = dbinomRawDD(size, x, pd, qd, log, undefined, shift);
|
|
33
61
|
if (x === 0)
|
|
34
62
|
return b;
|
|
35
63
|
const r = size / (size + x);
|
|
@@ -40,8 +68,48 @@ function dnbinomRaw(x, par, log) {
|
|
|
40
68
|
return log ? b + (Math.log(size) - Math.log(size + x)) : (b / (size + x)) * size;
|
|
41
69
|
}
|
|
42
70
|
const MIN_NORMAL = 2 ** -1022;
|
|
71
|
+
const LN2_DD = [0.6931471805599453, 2.3190468138462996e-17];
|
|
72
|
+
/** A probability kept times 2^k (fromMuDensity): its value (0 below the doubles) and its log in double-double */
|
|
73
|
+
function unshift(v, k) {
|
|
74
|
+
return { value: v[0] * 2 ** -(k / 2) * 2 ** -(k / 2), log: ddSub(ddLog(v), ddMulNum(LN2_DD, k)) };
|
|
75
|
+
}
|
|
43
76
|
/** P[X <= x] (lower) / P[X > x] (!lower) (x a nonnegative integer) */
|
|
44
77
|
function pnbinomRaw(x, par, lower, log) {
|
|
78
|
+
if (par.shift !== undefined) {
|
|
79
|
+
// p or q below the normal range (it was 0, and the incomplete beta took its end: pnbinom(0, MIN, { mu: MAX }) was 0
|
|
80
|
+
// for 1, qnbinom(0.5, ...) Infinity for 0; issue #103): I_p(size, x + 1) by the series for x below DBL_MIN, from
|
|
81
|
+
// the exact log of p (or of q, the other tail); (x + 1) p < 4 there, as the series needs
|
|
82
|
+
const [kp, kq] = par.shift;
|
|
83
|
+
// (the quantile search may ask for x = Infinity when its first step from the largest double overflows)
|
|
84
|
+
if (x === Infinity)
|
|
85
|
+
return lower ? (log ? 0 : 1) : log ? -Infinity : 0;
|
|
86
|
+
const r = kp > 0
|
|
87
|
+
? (({ value, log: lp }) => betaIncTinyX(par.size, x + 1, value, lp, lower, log))(unshift(par.pd, kp))
|
|
88
|
+
: (({ value, log: lq }) => betaIncTinyX(x + 1, par.size, value, lq, !lower, log))(unshift(par.qd, kq));
|
|
89
|
+
return log ? r[0] : r[1];
|
|
90
|
+
}
|
|
91
|
+
if (x + 1 - x !== 1) {
|
|
92
|
+
// 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).
|
|
93
|
+
// Without x + 1: I_p(size, x + 1) = I_p(size, x) + dnbinom(x)
|
|
94
|
+
let v;
|
|
95
|
+
let term;
|
|
96
|
+
if (!log) {
|
|
97
|
+
const t = betaIncDD(par.size, x, par.pd, par.qd, lower, false);
|
|
98
|
+
term = dnbinomRaw(x, par, false);
|
|
99
|
+
v = lower ? t + term : t - term;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const lt = betaIncDD(par.size, x, par.pd, par.qd, lower, true);
|
|
103
|
+
term = dnbinomRaw(x, par, true);
|
|
104
|
+
v = lower ? logAddExp(lt, term) : logSubExp(lt, term);
|
|
105
|
+
}
|
|
106
|
+
if (Number.isNaN(term))
|
|
107
|
+
return NaN;
|
|
108
|
+
// The upper tail as a difference: where dnbinom is not resolved against it (both logs near -3.5e48, or the plain
|
|
109
|
+
// difference cancels), one unit of x is below the resolution of the result, and the rounded parameter below serves
|
|
110
|
+
if (lower || (log ? Number.isFinite(v) : v > 0))
|
|
111
|
+
return v;
|
|
112
|
+
}
|
|
45
113
|
const up = upperSeries(x, par);
|
|
46
114
|
if (up !== undefined) {
|
|
47
115
|
// The upper tail (the smaller side) was computed directly by the series
|
|
@@ -95,10 +163,18 @@ function upperSeries(x, par) {
|
|
|
95
163
|
return undefined;
|
|
96
164
|
}
|
|
97
165
|
function quantileRaw(p, par, lowerTail, logP) {
|
|
98
|
-
// Starting value from the Cornish-Fisher expansion
|
|
99
|
-
const
|
|
166
|
+
// Starting value from the Cornish-Fisher expansion (p or q kept times 2^k: scaled back, 0 below the doubles; #103)
|
|
167
|
+
const [kp, kq] = par.shift ?? [0, 0];
|
|
168
|
+
const prob = kp > 0 ? unshift(par.pd, kp).value : par.pd[0] + par.pd[1];
|
|
169
|
+
const qv = kq > 0 ? unshift(par.qd, kq).value : par.qd[0] + par.qd[1];
|
|
170
|
+
if (kp > 0) {
|
|
171
|
+
// X p is nearly gamma (shape = size): y0 = g / p from the logs (p may be 0 here)
|
|
172
|
+
const g = qgamma1(p, par.size, 1, lowerTail, logP);
|
|
173
|
+
const y0 = g > 0 ? Math.min(Math.exp(Math.log(g) - unshift(par.pd, kp).log[0]), Number.MAX_VALUE) : 0;
|
|
174
|
+
return discreteQuantile({ p, lowerTail, logP, cdf: (y, l, lg) => pnbinomRaw(y, par, l, lg), y0, lo: 0, hi: Infinity });
|
|
175
|
+
}
|
|
100
176
|
const Q = 1 / prob;
|
|
101
|
-
const P =
|
|
177
|
+
const P = qv * Q;
|
|
102
178
|
const mu = par.size * P;
|
|
103
179
|
const sigma = Math.sqrt(par.size * P * Q);
|
|
104
180
|
const gamma = (Q + P) / sigma;
|
|
@@ -108,7 +184,7 @@ function quantileRaw(p, par, lowerTail, logP) {
|
|
|
108
184
|
// this is a much closer starting value than Cornish-Fisher, reducing distribution-function evaluations
|
|
109
185
|
// -log q = log(1 + p/q) (taking log of a rounded q loses digits for small p)
|
|
110
186
|
const g = qgamma1(p, par.size, 1, lowerTail, logP);
|
|
111
|
-
y0 = g / Math.log1p(prob /
|
|
187
|
+
y0 = g / Math.log1p(prob / qv);
|
|
112
188
|
// A finite gamma quantile divided by a tiny -log q beyond the doubles: so is the quantile (the distribution
|
|
113
189
|
// function cannot be evaluated out there; qnbinom(2.2e-284, 2.7e37, { prob: 1.9e-279 }) threw a TypeError)
|
|
114
190
|
if (y0 === Infinity && g > 0 && Number.isFinite(g))
|
|
@@ -201,7 +277,7 @@ export function pnbinom1(q, size, prob, lowerTail, logP) {
|
|
|
201
277
|
return dt0(lowerTail, logP);
|
|
202
278
|
if (size === 0 || prob === 1 || !Number.isFinite(q))
|
|
203
279
|
return dt1(lowerTail, logP);
|
|
204
|
-
return pnbinomRaw(
|
|
280
|
+
return pnbinomRaw(countAtMost(q), fromProb(size, prob), lowerTail, logP);
|
|
205
281
|
}
|
|
206
282
|
export function qnbinom1(p, size, prob, lowerTail, logP) {
|
|
207
283
|
if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(prob))
|
|
@@ -242,7 +318,7 @@ export function dnbinomMu1(x, size, mu, log) {
|
|
|
242
318
|
return dpoisRaw(n, mu, log);
|
|
243
319
|
if (mu === 0)
|
|
244
320
|
return n === 0 ? (log ? 0 : 1) : d0(log);
|
|
245
|
-
return dnbinomRaw(n,
|
|
321
|
+
return dnbinomRaw(n, fromMuDensity(size, mu), log);
|
|
246
322
|
}
|
|
247
323
|
export function pnbinomMu1(q, size, mu, lowerTail, logP) {
|
|
248
324
|
if (Number.isNaN(q) || Number.isNaN(size) || Number.isNaN(mu))
|
|
@@ -255,25 +331,26 @@ export function pnbinomMu1(q, size, mu, lowerTail, logP) {
|
|
|
255
331
|
return dt1(lowerTail, logP);
|
|
256
332
|
if (size === Infinity)
|
|
257
333
|
return ppois1(q, mu, lowerTail, logP);
|
|
258
|
-
return pnbinomRaw(
|
|
334
|
+
return pnbinomRaw(countAtMost(q), fromMuDensity(size, mu), lowerTail, logP);
|
|
259
335
|
}
|
|
260
336
|
export function qnbinomMu1(p, size, mu, lowerTail, logP) {
|
|
261
337
|
if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(mu))
|
|
262
338
|
return NaN;
|
|
263
|
-
//
|
|
339
|
+
// A negative size or mu defines no distribution, as in dnbinom and pnbinom (R's qnbinom returns 0 for mu = 0 before
|
|
340
|
+
// checking them); size = Inf is the Poisson distribution, and size = 0 or mu = 0 gives 0 regardless of p
|
|
341
|
+
if (size < 0 || mu < 0)
|
|
342
|
+
return NaN;
|
|
264
343
|
if (size === Infinity)
|
|
265
344
|
return qpois1(p, mu, lowerTail, logP);
|
|
266
345
|
if (size === 0 || mu === 0)
|
|
267
346
|
return 0;
|
|
268
|
-
if (size < 0 || mu < 0)
|
|
269
|
-
return NaN;
|
|
270
347
|
// mu = Inf answers only at the ends (p = 0, 1) and is NaN otherwise (as in R)
|
|
271
348
|
if (mu === Infinity)
|
|
272
349
|
return quantileBoundaries(p, 0, Infinity, lowerTail, logP) ?? NaN;
|
|
273
350
|
const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
|
|
274
351
|
if (b !== undefined)
|
|
275
352
|
return b;
|
|
276
|
-
return quantileRaw(p,
|
|
353
|
+
return quantileRaw(p, fromMuDensity(size, mu), lowerTail, logP);
|
|
277
354
|
}
|
|
278
355
|
/** Accept exactly one of prob and mu (as in R, giving both or neither is an error) */
|
|
279
356
|
function which(options) {
|
|
@@ -6,6 +6,7 @@
|
|
|
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";
|
|
9
10
|
const SQRT1_2 = Math.SQRT1_2;
|
|
10
11
|
/**
|
|
11
12
|
* exp(-a^2 / 2) (a >= 0) multiplied by scale.
|
|
@@ -41,7 +42,9 @@ function pnormStd(t, logP) {
|
|
|
41
42
|
if (t > -1)
|
|
42
43
|
return Math.log1p(-lowerTailProb(-t));
|
|
43
44
|
const a = -t;
|
|
44
|
-
|
|
45
|
+
// (a / 2) a, not (a a) / 2: the same value, without the overflow of a a above 1.34e154 (pnorm(-1.5e154, { logP: true })
|
|
46
|
+
// was -Infinity for -1.125e308; issue #33)
|
|
47
|
+
return Math.log(0.5 * erfcxNonNegative(a * SQRT1_2)) - (a / 2) * a;
|
|
45
48
|
}
|
|
46
49
|
function pnorm1(q, mean, sd, lowerTail, logP) {
|
|
47
50
|
if (Number.isNaN(q) || Number.isNaN(mean) || Number.isNaN(sd))
|
|
@@ -54,7 +57,8 @@ function pnorm1(q, mean, sd, lowerTail, logP) {
|
|
|
54
57
|
return NaN;
|
|
55
58
|
if (sd === 0)
|
|
56
59
|
return q < mean ? p0 : p1;
|
|
57
|
-
|
|
60
|
+
// (q - mean) / sd, also where q - mean overflows but the standardized value does not
|
|
61
|
+
const x = standardizeValue(q, mean, sd);
|
|
58
62
|
if (!Number.isFinite(x))
|
|
59
63
|
return q < mean ? p0 : p1;
|
|
60
64
|
return pnormStd(lowerTail ? x : -x, logP);
|
|
@@ -85,7 +89,8 @@ function qnorm1(p, mean, sd, lowerTail, logP) {
|
|
|
85
89
|
if (sd === 0)
|
|
86
90
|
return mean;
|
|
87
91
|
const z = logP ? ndtriLogRefined(p) : ndtri(p);
|
|
88
|
-
|
|
92
|
+
// mean + sd z, also where sd z overflows but the quantile does not
|
|
93
|
+
return affine(mean, sd, lowerTail ? z : -z);
|
|
89
94
|
}
|
|
90
95
|
const SQRT_2_OVER_PI = 0.797884560802865355879892119869;
|
|
91
96
|
/**
|
|
@@ -118,7 +123,7 @@ function dnorm1(x, mean, sd, log) {
|
|
|
118
123
|
return NaN; // Inf - Inf
|
|
119
124
|
if (sd === 0)
|
|
120
125
|
return x === mean ? Infinity : log ? -Infinity : 0;
|
|
121
|
-
const z = (x
|
|
126
|
+
const z = standardizeValue(x, mean, sd);
|
|
122
127
|
if (!Number.isFinite(z))
|
|
123
128
|
return log ? -Infinity : 0;
|
|
124
129
|
if (log)
|