@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
|
@@ -41,8 +41,12 @@ const upperPhi = (x) => pnorm(x, { lowerTail: false });
|
|
|
41
41
|
function upperOrthant(h, k, r) {
|
|
42
42
|
if (r === 1)
|
|
43
43
|
return upperPhi(Math.max(h, k));
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
// r = -1: Y = -X, so P(h < X < -k) (this was Φ(k) − Φ(h); issue #23), from the tails on the side of h for accuracy
|
|
45
|
+
if (r === -1) {
|
|
46
|
+
if (h >= -k)
|
|
47
|
+
return 0;
|
|
48
|
+
return h >= 0 ? upperPhi(h) - upperPhi(-k) : pnorm(-k) - pnorm(h);
|
|
49
|
+
}
|
|
46
50
|
const { x, w } = RULES[Math.abs(r) < 0.3 ? 0 : Math.abs(r) < 0.75 ? 1 : 2];
|
|
47
51
|
const hk = h * k;
|
|
48
52
|
if (Math.abs(r) < 0.925) {
|
|
@@ -124,6 +128,11 @@ export function pbvnorm(lower, upper, rho) {
|
|
|
124
128
|
let [b1, b2] = upper;
|
|
125
129
|
if ([a1, a2, b1, b2].some(Number.isNaN))
|
|
126
130
|
return NaN;
|
|
131
|
+
// A bound beyond ±40 is taken as infinite (before the reflection below, which then decides as for infinite bounds):
|
|
132
|
+
// Φ(-40) is 3.7e-350, far below half the least subnormal, so the probability moves by less than the doubles resolve.
|
|
133
|
+
// Huge finite bounds overflowed h k and h^2 (Inf - Inf, 0 * Inf) and made the rectangle NaN (pbvnorm([-1e200, -1e200],
|
|
134
|
+
// [Inf, Inf], 0.5) for 1; issue #119)
|
|
135
|
+
[a1, a2, b1, b2] = [a1, a2, b1, b2].map((v) => (v > 40 ? Infinity : v < -40 ? -Infinity : v));
|
|
127
136
|
if (!(a1 < b1 && a2 < b2))
|
|
128
137
|
return 0;
|
|
129
138
|
// reflect a rectangle in the lower half into the upper half
|
|
@@ -6,6 +6,12 @@
|
|
|
6
6
|
// -1/tan(π p) for p < 1/4 and -tan(π (1/2 - p)) for 1/4 <= p <= 1/2.
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { locationScale } from "../internal/ddexp.js";
|
|
9
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
10
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
11
|
+
const DCAUCHY_KEYS = ["location", "scale", "log"];
|
|
12
|
+
const PCAUCHY_KEYS = ["location", "scale", "lowerTail", "logP"];
|
|
13
|
+
const QCAUCHY_KEYS = ["location", "scale", "lowerTail", "logP"];
|
|
14
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
9
15
|
const LOG_PI = 1.1447298858494002;
|
|
10
16
|
const MIN_NORMAL = 2 ** -1022;
|
|
11
17
|
/** [f, e] with v = f 2^e, f in [1, 2) up to the rounding of log2 (v > 0 finite; exact, subnormal v included) */
|
|
@@ -60,7 +66,7 @@ export function dcauchy1(x, location, scale, log) {
|
|
|
60
66
|
const y = scale / ah / 2;
|
|
61
67
|
if (log)
|
|
62
68
|
return -(LOG_PI + Math.log(scale)) - 2 * (Math.log(ah) + Math.LN2 - Math.log(scale)) - Math.log1p(y * y);
|
|
63
|
-
return (y
|
|
69
|
+
return overPiScale(y, scale) * (y / (1 + y * y));
|
|
64
70
|
}
|
|
65
71
|
const az = Math.abs(z);
|
|
66
72
|
if (scale < MIN_NORMAL)
|
|
@@ -68,13 +74,19 @@ export function dcauchy1(x, location, scale, log) {
|
|
|
68
74
|
if (az <= 1) {
|
|
69
75
|
if (log)
|
|
70
76
|
return -(LOG_PI + Math.log(scale)) - Math.log1p(z * z);
|
|
71
|
-
|
|
77
|
+
// (π scale overflows above 5.7e307: then 1/π first; the density was 0, R's too; issue #25)
|
|
78
|
+
return Number.isFinite(Math.PI * scale) ? 1 / (Math.PI * scale * (1 + z * z)) : 1 / Math.PI / scale / (1 + z * z);
|
|
72
79
|
}
|
|
73
80
|
// With y = 1/z, f = y^2 / (π scale (1 + y^2))
|
|
74
81
|
const y = 1 / az;
|
|
75
82
|
if (log)
|
|
76
83
|
return -(LOG_PI + Math.log(scale)) - 2 * Math.log(az) - Math.log1p(y * y);
|
|
77
|
-
return (y
|
|
84
|
+
return overPiScale(y, scale) * (y / (1 + y * y));
|
|
85
|
+
}
|
|
86
|
+
/** y / (π scale), with 1/π first where π scale overflows (above 5.7e307 the density was 0, R's too; issue #25) */
|
|
87
|
+
function overPiScale(y, scale) {
|
|
88
|
+
const ps = Math.PI * scale;
|
|
89
|
+
return Number.isFinite(ps) ? y / ps : y / Math.PI / scale;
|
|
78
90
|
}
|
|
79
91
|
/**
|
|
80
92
|
* P[X <= q] (lower) / P[X > q] (!lower). For |z| > 1 the smaller tail atan(1/|z|)/π is computed without forming z,
|
|
@@ -151,7 +163,9 @@ export function qcauchy1(p, location, scale, lowerTail, logP) {
|
|
|
151
163
|
return NaN;
|
|
152
164
|
if (logP ? p > 0 : p < 0 || p > 1)
|
|
153
165
|
return NaN;
|
|
154
|
-
|
|
166
|
+
// scale = Inf gives the limits (-Inf, location, Inf below, at and above the median), as dcauchy and pcauchy (0 and
|
|
167
|
+
// 1/2) and qnorm and qlogis do (R: NaN)
|
|
168
|
+
if (!(scale > 0))
|
|
155
169
|
return scale === 0 ? location : NaN;
|
|
156
170
|
// Standard quantile T for the probability p on that side (x = location + scale T for the lower tail)
|
|
157
171
|
let t;
|
|
@@ -191,6 +205,7 @@ export function qcauchy1(p, location, scale, lowerTail, logP) {
|
|
|
191
205
|
* @category Distributions
|
|
192
206
|
*/
|
|
193
207
|
export function dcauchy(x, options = {}) {
|
|
208
|
+
checkOptions(options, DCAUCHY_KEYS, "dcauchy"); // generated: option-keys.mjs
|
|
194
209
|
const { location = 0, scale = 1, log = false } = options;
|
|
195
210
|
return vectorize(x, (v) => dcauchy1(v, location, scale, log));
|
|
196
211
|
}
|
|
@@ -210,6 +225,7 @@ export function dcauchy(x, options = {}) {
|
|
|
210
225
|
* @category Distributions
|
|
211
226
|
*/
|
|
212
227
|
export function pcauchy(q, options = {}) {
|
|
228
|
+
checkOptions(options, PCAUCHY_KEYS, "pcauchy"); // generated: option-keys.mjs
|
|
213
229
|
const { location = 0, scale = 1, lowerTail = true, logP = false } = options;
|
|
214
230
|
return vectorize(q, (v) => pcauchy1(v, location, scale, lowerTail, logP));
|
|
215
231
|
}
|
|
@@ -229,6 +245,7 @@ export function pcauchy(q, options = {}) {
|
|
|
229
245
|
* @category Distributions
|
|
230
246
|
*/
|
|
231
247
|
export function qcauchy(p, options = {}) {
|
|
248
|
+
checkOptions(options, QCAUCHY_KEYS, "qcauchy"); // generated: option-keys.mjs
|
|
232
249
|
const { location = 0, scale = 1, lowerTail = true, logP = false } = options;
|
|
233
250
|
return vectorize(p, (v) => qcauchy1(v, location, scale, lowerTail, logP));
|
|
234
251
|
}
|
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
import { vectorize } from "../internal/vectorize.js";
|
|
5
5
|
import { dgamma1, pgamma1, qgamma1 } from "./gamma.js";
|
|
6
6
|
import { dnchisq1, pnchisq1, qnchisq1 } from "./nchisq.js";
|
|
7
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
8
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
9
|
+
const DCHISQ_KEYS = ["log", "ncp"];
|
|
10
|
+
const PCHISQ_KEYS = ["lowerTail", "logP", "ncp"];
|
|
11
|
+
const QCHISQ_KEYS = ["lowerTail", "logP", "ncp"];
|
|
12
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
7
13
|
/**
|
|
8
14
|
* Density of the chi-squared distribution. Equivalent to R's `dchisq(x, df, ncp = 0, log = FALSE)`.
|
|
9
15
|
*
|
|
@@ -23,10 +29,13 @@ import { dnchisq1, pnchisq1, qnchisq1 } from "./nchisq.js";
|
|
|
23
29
|
* @category Distributions
|
|
24
30
|
*/
|
|
25
31
|
export function dchisq(x, df, options = {}) {
|
|
32
|
+
checkOptions(options, DCHISQ_KEYS, "dchisq"); // generated: option-keys.mjs
|
|
26
33
|
const { log = false, ncp } = options;
|
|
27
34
|
if (ncp !== undefined)
|
|
28
35
|
return vectorize(x, (v) => dnchisq1(v, df, ncp, log));
|
|
29
|
-
|
|
36
|
+
// (df < 0 is invalid: df / 2 of -5e-324 rounded to -0, the point mass at 0, as in R; issue #26)
|
|
37
|
+
const half = df < 0 ? NaN : df / 2;
|
|
38
|
+
return vectorize(x, (v) => dgamma1(v, half, 2, log));
|
|
30
39
|
}
|
|
31
40
|
/**
|
|
32
41
|
* Distribution function of the chi-squared distribution. Equivalent to R's `pchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)`.
|
|
@@ -47,10 +56,12 @@ export function dchisq(x, df, options = {}) {
|
|
|
47
56
|
* @category Distributions
|
|
48
57
|
*/
|
|
49
58
|
export function pchisq(q, df, options = {}) {
|
|
59
|
+
checkOptions(options, PCHISQ_KEYS, "pchisq"); // generated: option-keys.mjs
|
|
50
60
|
const { lowerTail = true, logP = false, ncp } = options;
|
|
51
61
|
if (ncp !== undefined)
|
|
52
62
|
return vectorize(q, (v) => pnchisq1(v, df, ncp, lowerTail, logP));
|
|
53
|
-
|
|
63
|
+
const half = df < 0 ? NaN : df / 2;
|
|
64
|
+
return vectorize(q, (v) => pgamma1(v, half, 2, lowerTail, logP));
|
|
54
65
|
}
|
|
55
66
|
/**
|
|
56
67
|
* Quantile function of the chi-squared distribution. Equivalent to R's `qchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)`.
|
|
@@ -71,8 +82,10 @@ export function pchisq(q, df, options = {}) {
|
|
|
71
82
|
* @category Distributions
|
|
72
83
|
*/
|
|
73
84
|
export function qchisq(p, df, options = {}) {
|
|
85
|
+
checkOptions(options, QCHISQ_KEYS, "qchisq"); // generated: option-keys.mjs
|
|
74
86
|
const { lowerTail = true, logP = false, ncp } = options;
|
|
75
87
|
if (ncp !== undefined)
|
|
76
88
|
return vectorize(p, (v) => qnchisq1(v, df, ncp, lowerTail, logP));
|
|
77
|
-
|
|
89
|
+
const half = df < 0 ? NaN : df / 2;
|
|
90
|
+
return vectorize(p, (v) => qgamma1(v, half, 2, lowerTail, logP));
|
|
78
91
|
}
|
|
@@ -7,6 +7,12 @@ import { vectorize } from "../internal/vectorize.js";
|
|
|
7
7
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
8
8
|
import { ddExp, expNeg, expNegOver, log1mExp, logOneMinusExpNeg, mulDD } from "../internal/ddexp.js";
|
|
9
9
|
import { ddLog, ddSub } from "../special/dd.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 DEXP_KEYS = ["rate", "log"];
|
|
13
|
+
const PEXP_KEYS = ["rate", "lowerTail", "logP"];
|
|
14
|
+
const QEXP_KEYS = ["rate", "lowerTail", "logP"];
|
|
15
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
10
16
|
const MIN_NORMAL = 2 ** -1022;
|
|
11
17
|
const LOG_MIN_NORMAL = -1022 * Math.LN2;
|
|
12
18
|
export function dexp1(x, rate, log) {
|
|
@@ -50,6 +56,11 @@ export function pexp1(q, rate, lowerTail, logP) {
|
|
|
50
56
|
return NaN;
|
|
51
57
|
if (t[0] === Infinity)
|
|
52
58
|
return dt1(lowerTail, logP);
|
|
59
|
+
// log(1 - exp(-t)) for t = rate q below the normal range: log rate + log q + log((1 - e^-t)/t), the last -t/2 to the
|
|
60
|
+
// double. The rounded subnormal product had lost its digits (pexp(1e-20, { rate: 1e-300, logP: true }) was
|
|
61
|
+
// -736.8272408909739 for log(1e-320) = -736.8272297580946, as R; issue #27)
|
|
62
|
+
if (lowerTail && logP && t[0] < 2 ** -1022 && t[0] > 0)
|
|
63
|
+
return Math.log(rate) + Math.log(q) + Math.log1p(-t[0] / 2);
|
|
53
64
|
return expTail(t, lowerTail, logP);
|
|
54
65
|
}
|
|
55
66
|
export function qexp1(p, rate, lowerTail, logP) {
|
|
@@ -88,6 +99,7 @@ export function qexp1(p, rate, lowerTail, logP) {
|
|
|
88
99
|
* @category Distributions
|
|
89
100
|
*/
|
|
90
101
|
export function dexp(x, options = {}) {
|
|
102
|
+
checkOptions(options, DEXP_KEYS, "dexp"); // generated: option-keys.mjs
|
|
91
103
|
const { rate = 1, log = false } = options;
|
|
92
104
|
return vectorize(x, (v) => dexp1(v, rate, log));
|
|
93
105
|
}
|
|
@@ -107,6 +119,7 @@ export function dexp(x, options = {}) {
|
|
|
107
119
|
* @category Distributions
|
|
108
120
|
*/
|
|
109
121
|
export function pexp(q, options = {}) {
|
|
122
|
+
checkOptions(options, PEXP_KEYS, "pexp"); // generated: option-keys.mjs
|
|
110
123
|
const { rate = 1, lowerTail = true, logP = false } = options;
|
|
111
124
|
return vectorize(q, (v) => pexp1(v, rate, lowerTail, logP));
|
|
112
125
|
}
|
|
@@ -126,6 +139,7 @@ export function pexp(q, options = {}) {
|
|
|
126
139
|
* @category Distributions
|
|
127
140
|
*/
|
|
128
141
|
export function qexp(p, options = {}) {
|
|
142
|
+
checkOptions(options, QEXP_KEYS, "qexp"); // generated: option-keys.mjs
|
|
129
143
|
const { rate = 1, lowerTail = true, logP = false } = options;
|
|
130
144
|
return vectorize(p, (v) => qexp1(v, rate, lowerTail, logP));
|
|
131
145
|
}
|
package/dist/distributions/f.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
//
|
|
3
3
|
// The density uses the relation to the binomial probability. With p = m x/(n + m x) and q = n/(n + m x),
|
|
4
4
|
// m >= 2: f(x) = m q / 2 * dbinom((m-2)/2; (m+n-2)/2, p)
|
|
5
|
-
//
|
|
6
|
-
//
|
|
5
|
+
// (derived by rewriting the F density in terms of Gamma functions); m < 2 (or n < 2) is computed from the log of the
|
|
6
|
+
// density with the small-shape prefactor (fLogDensity).
|
|
7
7
|
// The distribution function is P[F <= x] = I_p(m/2, n/2); quantiles use Newton's method in u = log x.
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, dt1, quantileBoundaries, smallerTail } from "../internal/dpq.js";
|
|
@@ -16,7 +16,14 @@ import { ddAdd, ddDiv, ddLog, ddMul, ddNeg, ddSub, twoProd, twoSum } from "../sp
|
|
|
16
16
|
import { dgamma1, pgamma1, qgamma1 } from "./gamma.js";
|
|
17
17
|
import { dnf1, pnf1, qnf1 } from "./nbeta.js";
|
|
18
18
|
import { betaQuantileGuess } from "./beta.js";
|
|
19
|
-
|
|
19
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
20
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
21
|
+
const DF_KEYS = ["log", "ncp"];
|
|
22
|
+
const PF_KEYS = ["lowerTail", "logP", "ncp"];
|
|
23
|
+
const QF_KEYS = ["lowerTail", "logP", "ncp"];
|
|
24
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
25
|
+
const DBL_MIN = 2.2250738585072014e-308;
|
|
26
|
+
const LOG_DBL_MIN = Math.log(DBL_MIN);
|
|
20
27
|
/**
|
|
21
28
|
* Log density in double-double from log p and log q (p = m x/(n + m x), q = 1 - p = n/(n + m x)), with a = m/2, b = n/2:
|
|
22
29
|
* f(x) = (m/n) p^(a-1) q^(b+1) / B(a, b) (the beta density of p times dp/dx = (m/n) q^2)
|
|
@@ -47,8 +54,17 @@ function fRatios(x, m, n) {
|
|
|
47
54
|
// (pf(1e301, 1.4e-312, 6.72, { lowerTail: false }) was 1, 1.77e-311 expected; qf there did not return)
|
|
48
55
|
const S = 2 ** 600;
|
|
49
56
|
let mx = x > 1e290 ? twoProd(m * S, x / S) : m > 1e290 ? twoProd(m / S, x * S) : twoProd(m, x);
|
|
50
|
-
if (!(Number.isFinite(mx[0]) && Number.isFinite(mx[1]) && Math.abs(mx[0]) < 1e290))
|
|
51
|
-
|
|
57
|
+
if (!(Number.isFinite(mx[0]) && Number.isFinite(mx[1]) && Math.abs(mx[0]) < 1e290)) {
|
|
58
|
+
// m x and n both divided by 2^600 (exactly): a huge m x is not always far above n, and the callers' p ≈ 1 gave
|
|
59
|
+
// pf(1e150, 1e150, 1e308) the wrong tail (lower -Inf for 0 in logs; issue #116). Where m x is still huge or n falls
|
|
60
|
+
// below 1e-290 there, m x is far above n and the callers' p ≈ 1, q ≈ n/(m x) holds
|
|
61
|
+
const mxS = twoProd(Math.max(m, x) / S, Math.min(m, x));
|
|
62
|
+
const nS = n / S;
|
|
63
|
+
if (!(Number.isFinite(mxS[0]) && Number.isFinite(mxS[1]) && Math.abs(mxS[0]) < 1e290) || !(nS >= 1e-290))
|
|
64
|
+
return undefined;
|
|
65
|
+
const dS = ddAdd(mxS, [nS, 0]);
|
|
66
|
+
return [ddDiv(mxS, dS), ddDiv([nS, 0], dS)];
|
|
67
|
+
}
|
|
52
68
|
let nn = n;
|
|
53
69
|
if (x > 0 && mx[0] < 1e-290 && n < 1e127) {
|
|
54
70
|
mx = m < x ? twoProd(m * S, x) : twoProd(m, x * S);
|
|
@@ -83,15 +99,11 @@ function fDensity(x, m, n, log) {
|
|
|
83
99
|
// below underflowed (df(6.6e-112, 3.2e-173, 1868) was -Infinity on the log scale, NaN at x = 4.6e-157)
|
|
84
100
|
if (Math.min(m, n) / 2 < SMALL_SHAPE)
|
|
85
101
|
return fromLog(fLogDensity(m, n, ddLog(pd), ddLog(qd)), log);
|
|
86
|
-
|
|
102
|
+
// (m >= 2 here: SMALL_SHAPE is 1, so df1 < 2 took the branch above; R's form for df1 < 2, whose 2 (m + n) overflowed
|
|
103
|
+
// for df2 near 1e308, was never reached and is gone; issue #32)
|
|
87
104
|
const q = qd[0];
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const v = dbinomRawDD((m - 2) / 2, n / 2, pd, qd, log);
|
|
91
|
-
return log ? Math.log(c) + v : c * v;
|
|
92
|
-
}
|
|
93
|
-
const c = ((m / (n + m * x)) * m * n) / (2 * (m + n) * p);
|
|
94
|
-
const v = dbinomRawDD(m / 2, n / 2, pd, qd, log);
|
|
105
|
+
const c = (m * q) / 2;
|
|
106
|
+
const v = dbinomRawDD((m - 2) / 2, n / 2, pd, qd, log);
|
|
95
107
|
return log ? Math.log(c) + v : c * v;
|
|
96
108
|
}
|
|
97
109
|
export function dfDensity1(x, m, n, log) {
|
|
@@ -110,16 +122,25 @@ export function dfDensity1(x, m, n, log) {
|
|
|
110
122
|
}
|
|
111
123
|
if (!Number.isFinite(m) && !Number.isFinite(n))
|
|
112
124
|
return x === 1 ? Infinity : d0(log);
|
|
125
|
+
// The density tends to 0 as x -> Inf for every df1 and df2 (x^(-df2/2 - 1)). R gives NaN for df1 < 2 and for
|
|
126
|
+
// df1 = Inf with df2 < 2 (Inf / Inf in its forms; #493)
|
|
127
|
+
if (x === Infinity)
|
|
128
|
+
return d0(log);
|
|
113
129
|
if (!Number.isFinite(n))
|
|
114
130
|
return dgamma1(x, m / 2, 2 / m, log); // χ²_m / m
|
|
115
131
|
if (!Number.isFinite(m)) {
|
|
116
132
|
// n / χ²_n: 1/x follows a gamma distribution with shape n/2, scale 2/n
|
|
117
|
-
// At tiny x this is 0 / 0 = NaN (x^2 underflows), as R gives (df(1e-300, Inf, 5) is NaN); the log is finite
|
|
118
133
|
const dens = dgamma1(1 / x, n / 2, 2 / n, log);
|
|
119
|
-
|
|
134
|
+
if (log)
|
|
135
|
+
return dens - 2 * Math.log(x);
|
|
136
|
+
// Where the gamma density or x^2 leaves the normal range, the quotient is 0 / 0 = NaN (df(1e-300, Inf, 5); R the
|
|
137
|
+
// same), or 0 where the density underflows or x^2 overflows (df(1e160, Inf, 1) is 3.99e-241; R 0): from the log
|
|
138
|
+
// (#493). Elsewhere the quotient and its bits are kept
|
|
139
|
+
const x2 = x * x;
|
|
140
|
+
if (dens >= DBL_MIN && dens < Infinity && x2 >= DBL_MIN && x2 < Infinity)
|
|
141
|
+
return dens / x2;
|
|
142
|
+
return Math.exp(dgamma1(1 / x, n / 2, 2 / n, true) - 2 * Math.log(x));
|
|
120
143
|
}
|
|
121
|
-
if (!Number.isFinite(x))
|
|
122
|
-
return m < 2 ? NaN : d0(log); // R gives NaN at x = Inf when df1 < 2
|
|
123
144
|
return fDensity(x, m, n, log);
|
|
124
145
|
}
|
|
125
146
|
/**
|
|
@@ -295,6 +316,7 @@ export function qf1(p, m, n, lowerTail, logP) {
|
|
|
295
316
|
* @category Distributions
|
|
296
317
|
*/
|
|
297
318
|
export function df(x, df1, df2, options = {}) {
|
|
319
|
+
checkOptions(options, DF_KEYS, "df"); // generated: option-keys.mjs
|
|
298
320
|
const { log = false, ncp } = options;
|
|
299
321
|
if (ncp !== undefined)
|
|
300
322
|
return vectorize(x, (v) => dnf1(v, df1, df2, ncp, log));
|
|
@@ -319,6 +341,7 @@ export function df(x, df1, df2, options = {}) {
|
|
|
319
341
|
* @category Distributions
|
|
320
342
|
*/
|
|
321
343
|
export function pf(q, df1, df2, options = {}) {
|
|
344
|
+
checkOptions(options, PF_KEYS, "pf"); // generated: option-keys.mjs
|
|
322
345
|
const { lowerTail = true, logP = false, ncp } = options;
|
|
323
346
|
if (ncp !== undefined)
|
|
324
347
|
return vectorize(q, (v) => pnf1(v, df1, df2, ncp, lowerTail, logP));
|
|
@@ -344,6 +367,7 @@ export function pf(q, df1, df2, options = {}) {
|
|
|
344
367
|
* @category Distributions
|
|
345
368
|
*/
|
|
346
369
|
export function qf(p, df1, df2, options = {}) {
|
|
370
|
+
checkOptions(options, QF_KEYS, "qf"); // generated: option-keys.mjs
|
|
347
371
|
const { lowerTail = true, logP = false, ncp } = options;
|
|
348
372
|
if (ncp !== undefined)
|
|
349
373
|
return vectorize(p, (v) => qnf1(v, df1, df2, ncp, lowerTail, logP));
|
|
@@ -18,11 +18,17 @@ import { dpoisRaw } from "../special/loader.js";
|
|
|
18
18
|
import { ddAdd, ddDivNum, ddLog, ddMulNum, ddSub } from "../special/dd.js";
|
|
19
19
|
import { ddExp, log1mExp, logRatioDD } from "../internal/ddexp.js";
|
|
20
20
|
import { qnorm } from "./normal.js";
|
|
21
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
22
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
23
|
+
const DGAMMA_KEYS = ["rate", "scale", "log"];
|
|
24
|
+
const PGAMMA_KEYS = ["rate", "scale", "lowerTail", "logP"];
|
|
25
|
+
const QGAMMA_KEYS = ["rate", "scale", "lowerTail", "logP"];
|
|
21
26
|
/** Determine scale from rate / scale (as in R, giving both is an error unless scale = 1/rate; also used by rgamma) */
|
|
22
27
|
export function gammaScale(params) {
|
|
23
28
|
const { rate, scale } = params;
|
|
24
29
|
if (rate !== undefined && scale !== undefined) {
|
|
25
|
-
|
|
30
|
+
// (as R: a product that is NaN, from rate = NaN or Inf * 0, is an error too; it passed with >=)
|
|
31
|
+
if (!(Math.abs(rate * scale - 1) < 1e-15)) {
|
|
26
32
|
throw new RangeError("rate and scale can both be given only when scale = 1 / rate");
|
|
27
33
|
}
|
|
28
34
|
return scale;
|
|
@@ -165,8 +171,22 @@ export function pgamma1(q, shape, scale, lowerTail, logP) {
|
|
|
165
171
|
}
|
|
166
172
|
if (x <= 0)
|
|
167
173
|
return dt0(lowerTail, logP);
|
|
174
|
+
if (x === Infinity && !lowerTail && logP && scale < Infinity && overflowLogUpperFinite(q, shape, scale))
|
|
175
|
+
return NaN;
|
|
168
176
|
return gammaInc(shape, x, lowerTail, logP);
|
|
169
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Whether log Q(shape, q / scale) may be finite although q / scale overflows (#368). The other tails are right there
|
|
180
|
+
* (P = 1, Q = 0 and log P = 0 to double precision: q / scale exceeds even the largest shape by far more than its
|
|
181
|
+
* standard deviation), and so is log Q = -Inf where its leading term shape (r - 1 - log r), r = q / (shape scale),
|
|
182
|
+
* overflows. Where that term is finite (a shape of about 1e290 and above) log Q is finite, but gammaInc sees only
|
|
183
|
+
* x = Inf and returns -Inf: such values are NaN, not computed (user decision, 2026-11-03)
|
|
184
|
+
*/
|
|
185
|
+
function overflowLogUpperFinite(q, shape, scale) {
|
|
186
|
+
const ps = shape * scale;
|
|
187
|
+
const r = ps >= MIN_NORMAL ? q / ps : Math.exp(Math.log(q) - Math.log(scale) - Math.log(shape));
|
|
188
|
+
return shape * (r - 1 - Math.log(r)) < Infinity;
|
|
189
|
+
}
|
|
170
190
|
/**
|
|
171
191
|
* log of the standardized gamma quantile when it is below the smallest normal double (else undefined): there
|
|
172
192
|
* P(shape, x) = x^shape / Gamma(shape + 1) to double precision, so log x = (log P + lgamma(shape + 1)) / shape.
|
|
@@ -230,16 +250,25 @@ export function qgammaStd(p, shape, lowerTail, logP, scale = 1) {
|
|
|
230
250
|
hi: Infinity,
|
|
231
251
|
positive: true,
|
|
232
252
|
});
|
|
253
|
+
// The standardized quantile beyond the largest double (#368): the search stops at Number.MAX_VALUE, and x * scale
|
|
254
|
+
// was MAX_VALUE times scale (qgamma(-1e308, 1e308, scale 1e-308, upper, log) 1.797, the true value about 3.146). With
|
|
255
|
+
// scale >= 1 the quantile is Inf; below 1 it may be finite but is not computed here: NaN (user decision, 2026-11-03)
|
|
256
|
+
if (x >= 2 ** 1000) {
|
|
257
|
+
const atMax = gammaInc(shape, Number.MAX_VALUE, lower, true);
|
|
258
|
+
if (lower ? atMax < logTarget : atMax > logTarget)
|
|
259
|
+
return scale >= 1 ? Infinity : NaN;
|
|
260
|
+
}
|
|
233
261
|
return x * scale;
|
|
234
262
|
}
|
|
235
263
|
export function qgamma1(p, shape, scale, lowerTail, logP) {
|
|
236
264
|
if (Number.isNaN(p) || Number.isNaN(shape) || Number.isNaN(scale))
|
|
237
265
|
return NaN;
|
|
266
|
+
// parameters that define no distribution before the ends, as dgamma and pgamma (R gives 0 and Inf at p = 0 and 1)
|
|
267
|
+
if (shape < 0 || scale <= 0)
|
|
268
|
+
return NaN;
|
|
238
269
|
const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
|
|
239
270
|
if (b !== undefined)
|
|
240
271
|
return b;
|
|
241
|
-
if (shape < 0 || scale <= 0)
|
|
242
|
-
return NaN;
|
|
243
272
|
if (shape === 0)
|
|
244
273
|
return 0;
|
|
245
274
|
// Shape Inf: all the mass at +Inf, so every probability strictly between 0 and 1 maps to Inf
|
|
@@ -270,6 +299,7 @@ export function qgamma1(p, shape, scale, lowerTail, logP) {
|
|
|
270
299
|
* @category Distributions
|
|
271
300
|
*/
|
|
272
301
|
export function dgamma(x, shape, options = {}) {
|
|
302
|
+
checkOptions(options, DGAMMA_KEYS, "dgamma"); // generated: option-keys.mjs
|
|
273
303
|
const scale = gammaScale(options);
|
|
274
304
|
const log = options.log ?? false;
|
|
275
305
|
return vectorize(x, (v) => dgamma1(v, shape, scale, log));
|
|
@@ -294,6 +324,7 @@ export function dgamma(x, shape, options = {}) {
|
|
|
294
324
|
* @category Distributions
|
|
295
325
|
*/
|
|
296
326
|
export function pgamma(q, shape, options = {}) {
|
|
327
|
+
checkOptions(options, PGAMMA_KEYS, "pgamma"); // generated: option-keys.mjs
|
|
297
328
|
const scale = gammaScale(options);
|
|
298
329
|
const { lowerTail = true, logP = false } = options;
|
|
299
330
|
return vectorize(q, (v) => pgamma1(v, shape, scale, lowerTail, logP));
|
|
@@ -317,6 +348,7 @@ export function pgamma(q, shape, options = {}) {
|
|
|
317
348
|
* @category Distributions
|
|
318
349
|
*/
|
|
319
350
|
export function qgamma(p, shape, options = {}) {
|
|
351
|
+
checkOptions(options, QGAMMA_KEYS, "qgamma"); // generated: option-keys.mjs
|
|
320
352
|
const scale = gammaScale(options);
|
|
321
353
|
const { lowerTail = true, logP = false } = options;
|
|
322
354
|
return vectorize(p, (v) => qgamma1(v, shape, scale, lowerTail, logP));
|
|
@@ -3,13 +3,20 @@
|
|
|
3
3
|
// P[X = x] = p q^x, P[X > x] = q^(x+1) (q = 1 - p). q^n is computed with Loader's (2000) building blocks
|
|
4
4
|
// (dbinomRawDD in src/special/loader.ts, as the binomial probability of 0 successes and n failures)
|
|
5
5
|
// (the exponent is kept in double-double so that the rounding of n log q is not amplified n times).
|
|
6
|
-
// Quantiles use a closed form.
|
|
6
|
+
// Quantiles use a closed form, with the ratio of logs in double-double.
|
|
7
7
|
import { vectorize } from "../internal/vectorize.js";
|
|
8
8
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
9
9
|
import { log1mExp } from "../internal/ddexp.js";
|
|
10
|
-
import { nonInt } from "../internal/integer.js";
|
|
10
|
+
import { countAtMost, nonInt } from "../internal/integer.js";
|
|
11
|
+
import { FUZZ_LINEAR, FUZZ_LOG } from "../internal/discrete.js";
|
|
11
12
|
import { dbinomRawDD } from "../special/loader.js";
|
|
12
|
-
import { ddLog, ddMul, ddMulNum, twoSum } from "../special/dd.js";
|
|
13
|
+
import { ddAdd, ddDiv, ddLog, ddMul, ddMulNum, twoSum } from "../special/dd.js";
|
|
14
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
15
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
16
|
+
const DGEOM_KEYS = ["log"];
|
|
17
|
+
const PGEOM_KEYS = ["lowerTail", "logP"];
|
|
18
|
+
const QGEOM_KEYS = ["lowerTail", "logP"];
|
|
19
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
13
20
|
/** p and q = 1 - p in double-double (the smaller one is the exact base) */
|
|
14
21
|
export function probPair(prob) {
|
|
15
22
|
return prob <= 0.5
|
|
@@ -48,10 +55,19 @@ export function dgeom1(x, prob, log) {
|
|
|
48
55
|
const qn = qPow(n, pd, qd, log);
|
|
49
56
|
return log ? Math.log(prob) + qn : prob * qn;
|
|
50
57
|
}
|
|
51
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* P[X > x] = q^(x+1) (x a nonnegative integer). Where x + 1 is not exact (beyond 2^53), q^x q: x + 1 rounded to x or
|
|
60
|
+
* x + 2 and the tail was off by a factor q (relative 1e-14 for prob = 1e-14 at x = 2^54; issue #327)
|
|
61
|
+
*/
|
|
52
62
|
function upper(x, prob, log) {
|
|
53
63
|
const { pd, qd } = probPair(prob);
|
|
54
|
-
|
|
64
|
+
if (x + 1 - x === 1)
|
|
65
|
+
return qPow(x + 1, pd, qd, log);
|
|
66
|
+
const qx = qPow(x, pd, qd, log);
|
|
67
|
+
if (!log)
|
|
68
|
+
return qx * (qd[0] + qd[1]);
|
|
69
|
+
const lq = qd[0] === 1 ? [-pd[0], -(pd[0] * pd[0]) / 2] : ddLog(qd);
|
|
70
|
+
return qx + (lq[0] + lq[1]);
|
|
55
71
|
}
|
|
56
72
|
function pgeomRaw(x, prob, lowerTail, logP) {
|
|
57
73
|
if (!lowerTail)
|
|
@@ -71,7 +87,7 @@ export function pgeom1(q, prob, lowerTail, logP) {
|
|
|
71
87
|
return dt0(lowerTail, logP);
|
|
72
88
|
if (!Number.isFinite(q) || prob === 1)
|
|
73
89
|
return dt1(lowerTail, logP);
|
|
74
|
-
return pgeomRaw(
|
|
90
|
+
return pgeomRaw(countAtMost(q), prob, lowerTail, logP);
|
|
75
91
|
}
|
|
76
92
|
export function qgeom1(p, prob, lowerTail, logP) {
|
|
77
93
|
if (Number.isNaN(p) || Number.isNaN(prob))
|
|
@@ -85,14 +101,40 @@ export function qgeom1(p, prob, lowerTail, logP) {
|
|
|
85
101
|
return 0;
|
|
86
102
|
if (b !== undefined)
|
|
87
103
|
return b;
|
|
88
|
-
// Smallest integer y with P[X > y] = q^(y+1) <= (upper probability),
|
|
89
|
-
// (
|
|
104
|
+
// Smallest integer y with P[X > y] = q^(y+1) <= (upper probability) u, in closed form: y + 1 = ceil(log u / log q)
|
|
105
|
+
// (the distribution function is flat, so in an integer search the probability slack would move the answer a lot).
|
|
106
|
+
// R's form in double with a slack of 1e-12 on the ratio took y - 1 for a p up to 1e-12 relative above a jump
|
|
107
|
+
// (qgeom(0.7500000000000866, 0.5) was 1, where P[X <= 1] = 0.75 < p). Here the ratio r is formed in double-double
|
|
108
|
+
// (log u exact from p, 1 - p by twoSum, except from a log lower probability, whose -expm1 is rounded once), and the
|
|
109
|
+
// slack of discreteQuantile on p (8 ε relative, 2 ε on the log scale, so that qgeom(pgeom(y)) = y) is carried over
|
|
110
|
+
// to r only where it is below half a step. Where the rounding of p spans whole steps (p near 1 with a tiny prob:
|
|
111
|
+
// 8 ε there is 1.8e5 steps for qgeom(0.9999999999, 1e-10)), the answer for p as given, as R's
|
|
90
112
|
let lu;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
113
|
+
let dlu; // change of log u for the slack on p
|
|
114
|
+
if (logP) {
|
|
115
|
+
lu = lowerTail ? (p > -Math.LN2 ? ddLog([-Math.expm1(p), 0]) : [log1mExp(p), 0]) : [p, 0];
|
|
116
|
+
// (u = 1 - e^p: d log u / dp = -1 / expm1(-p))
|
|
117
|
+
dlu = FUZZ_LOG * -p * (lowerTail ? 1 / Math.expm1(-p) : 1);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
lu = lowerTail ? ddLog(twoSum(1, -p)) : ddLog([p, 0]);
|
|
121
|
+
dlu = FUZZ_LINEAR * (lowerTail ? p / (1 - p) : 1);
|
|
122
|
+
}
|
|
123
|
+
const { pd, qd } = probPair(prob);
|
|
124
|
+
const lq = qd[0] === 1 ? [-pd[0], -(pd[0] * pd[0]) / 2] : ddLog(qd);
|
|
125
|
+
const r = ddDiv(lu, lq);
|
|
126
|
+
// beyond the doubles (a subnormal prob): Infinity
|
|
127
|
+
if (!Number.isFinite(r[0]))
|
|
128
|
+
return r[0];
|
|
129
|
+
const dr = dlu / -lq[0];
|
|
130
|
+
// an r within 2^-90 relative above an integer is that integer too (a p equal to the distribution function at a jump,
|
|
131
|
+
// such as 0.75 for prob = 1/2, is not moved up by the rounding of the logs)
|
|
132
|
+
const tie = Math.abs(r[0]) * 2 ** -90;
|
|
133
|
+
const s = ddAdd(r, [-(dr < 0.5 ? dr + tie : tie), 0]);
|
|
134
|
+
let n = Math.ceil(s[0]);
|
|
135
|
+
if (s[0] === n && s[1] > 0)
|
|
136
|
+
n += 1;
|
|
137
|
+
return Math.max(0, n - 1);
|
|
96
138
|
}
|
|
97
139
|
/**
|
|
98
140
|
* Probability mass function of the geometric distribution (number of failures before the first success). Equivalent to R's `dgeom(x, prob, log = FALSE)`.
|
|
@@ -111,6 +153,7 @@ export function qgeom1(p, prob, lowerTail, logP) {
|
|
|
111
153
|
* @category Distributions
|
|
112
154
|
*/
|
|
113
155
|
export function dgeom(x, prob, options = {}) {
|
|
156
|
+
checkOptions(options, DGEOM_KEYS, "dgeom"); // generated: option-keys.mjs
|
|
114
157
|
const log = options.log ?? false;
|
|
115
158
|
return vectorize(x, (v) => dgeom1(v, prob, log));
|
|
116
159
|
}
|
|
@@ -131,6 +174,7 @@ export function dgeom(x, prob, options = {}) {
|
|
|
131
174
|
* @category Distributions
|
|
132
175
|
*/
|
|
133
176
|
export function pgeom(q, prob, options = {}) {
|
|
177
|
+
checkOptions(options, PGEOM_KEYS, "pgeom"); // generated: option-keys.mjs
|
|
134
178
|
const { lowerTail = true, logP = false } = options;
|
|
135
179
|
return vectorize(q, (v) => pgeom1(v, prob, lowerTail, logP));
|
|
136
180
|
}
|
|
@@ -151,6 +195,7 @@ export function pgeom(q, prob, options = {}) {
|
|
|
151
195
|
* @category Distributions
|
|
152
196
|
*/
|
|
153
197
|
export function qgeom(p, prob, options = {}) {
|
|
198
|
+
checkOptions(options, QGEOM_KEYS, "qgeom"); // generated: option-keys.mjs
|
|
154
199
|
const { lowerTail = true, logP = false } = options;
|
|
155
200
|
return vectorize(p, (v) => qgeom1(v, prob, lowerTail, logP));
|
|
156
201
|
}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
|
|
2
2
|
import { type DensityOptions, type TailOptions } from "../internal/dpq.js";
|
|
3
3
|
export declare function dhyper1(x: number, m: number, n: number, k: number, log: boolean): number;
|
|
4
|
+
/** Check and round the parameters of phyper / qhyper. undefined if invalid */
|
|
5
|
+
/** m / (m + n) without forming m + n, which may overflow */
|
|
6
|
+
export declare function whiteFraction(m: number, n: number): number;
|
|
4
7
|
export declare function phyper1(q: number, m: number, n: number, k: number, lowerTail: boolean, logP: boolean): number;
|
|
5
|
-
|
|
8
|
+
/** (limitTime false: without the bound on sigma, for rhyper's inversion, which is unchanged) */
|
|
9
|
+
export declare function qhyper1(p: number, m: number, n: number, k: number, lowerTail: boolean, logP: boolean, limitTime?: boolean): number;
|
|
6
10
|
/**
|
|
7
11
|
* Probability mass function of the hypergeometric distribution (number of white balls among `k` drawn without replacement from an urn of `m` white and `n` black balls). Equivalent to R's `dhyper(x, m, n, k, log = FALSE)`.
|
|
8
12
|
*
|