@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
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
// beta(1, 1), beta with both shapes below 1, arcsine), the larger of two adjacent maxima of a discrete
|
|
9
9
|
// distribution, and the end point where the density diverges (gamma with shape < 1).
|
|
10
10
|
import { dqagie, dqagse } from "../integrate/quadpack.js";
|
|
11
|
-
import { ddAdd, ddMul, ddSub } from "../special/dd.js";
|
|
12
|
-
import {
|
|
11
|
+
import { ddAdd, ddLog, ddMul, ddMulNum, ddSub, twoProd } from "../special/dd.js";
|
|
12
|
+
import { ddExp } from "../internal/ddexp.js";
|
|
13
|
+
import { gamma as gammaFn, lgamma } from "../special/gamma.js";
|
|
13
14
|
import { lbeta } from "../special/beta.js";
|
|
14
|
-
import { qgamma } from "./gamma.js";
|
|
15
|
+
import { gammaScale, qgamma } from "./gamma.js";
|
|
15
16
|
import { qchisq } from "./chisq.js";
|
|
16
17
|
import { qbeta } from "./beta.js";
|
|
17
18
|
import { qf } from "./f.js";
|
|
@@ -20,14 +21,63 @@ import { qbinom } from "./binomial.js";
|
|
|
20
21
|
import { qpois } from "./poisson.js";
|
|
21
22
|
import { qgeom } from "./geometric.js";
|
|
22
23
|
import { qnbinom } from "./negbinomial.js";
|
|
23
|
-
import { qhyper } from "./hypergeometric.js";
|
|
24
|
+
import { dhyper1, qhyper } from "./hypergeometric.js";
|
|
24
25
|
import { qinvgamma } from "./invgamma.js";
|
|
25
26
|
import { dkumar1, pkumar1, qkumar, qkumar1 } from "./kumaraswamy.js";
|
|
26
27
|
import { qtriang } from "./triangular.js";
|
|
27
|
-
import {
|
|
28
|
+
import { ldexp } from "../internal/summary.js";
|
|
29
|
+
import { midpoint } from "../internal/extremes.js";
|
|
30
|
+
import { splitDouble } from "../linalg/dot.js";
|
|
31
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
32
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
33
|
+
const DIST_SUMMARY_KEYS = {
|
|
34
|
+
arcsine: ["a", "b"],
|
|
35
|
+
beta: ["shape1", "shape2"],
|
|
36
|
+
binom: ["size", "prob"],
|
|
37
|
+
cauchy: ["location", "scale"],
|
|
38
|
+
chisq: ["df"],
|
|
39
|
+
exp: ["rate"],
|
|
40
|
+
f: ["df1", "df2"],
|
|
41
|
+
gamma: ["shape", "rate", "scale"],
|
|
42
|
+
geom: ["prob"],
|
|
43
|
+
hyper: ["m", "n", "k"],
|
|
44
|
+
invgamma: ["alpha", "beta"],
|
|
45
|
+
kumar: ["a", "b"],
|
|
46
|
+
laplace: ["mu", "sigma"],
|
|
47
|
+
lnorm: ["meanlog", "sdlog"],
|
|
48
|
+
logis: ["location", "scale"],
|
|
49
|
+
nbinom: ["size", "prob", "mu"],
|
|
50
|
+
norm: ["mean", "sd"],
|
|
51
|
+
pareto: ["a", "b"],
|
|
52
|
+
pois: ["lambda"],
|
|
53
|
+
t: ["df"],
|
|
54
|
+
triang: ["a", "b", "c"],
|
|
55
|
+
unif: ["min", "max"],
|
|
56
|
+
weibull: ["shape", "scale"],
|
|
57
|
+
};
|
|
28
58
|
const NAN = { mean: NaN, variance: NaN, sd: NaN, median: NaN, mode: NaN, skewness: NaN, kurtosis: NaN };
|
|
29
|
-
|
|
30
|
-
|
|
59
|
+
/** A point mass at 0: no skewness or kurtosis */
|
|
60
|
+
const POINT_AT_0 = { mean: 0, variance: 0, sd: 0, median: 0, mode: 0, skewness: NaN, kurtosis: NaN };
|
|
61
|
+
/**
|
|
62
|
+
* sdDirect: the standard deviation formed without the variance, used where the variance overflowed or lost digits
|
|
63
|
+
* below the smallest normal number (normal(sd = 1e200) has variance Inf and sd 1e200, normal(sd = 1e-200) variance 0)
|
|
64
|
+
*/
|
|
65
|
+
function make(mean, variance, median, mode, skewness, kurtosis, sdDirect) {
|
|
66
|
+
const sd = sdDirect === undefined || (variance >= MIN_NORMAL && variance < Infinity) ? Math.sqrt(variance) : sdDirect;
|
|
67
|
+
return { mean, variance, sd, median, mode, skewness, kurtosis };
|
|
68
|
+
}
|
|
69
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
70
|
+
/**
|
|
71
|
+
* v = f(x_1, ..., x_n), a product of powers x_i^d_i (given as [x_i, d_i]) and factors of moderate size. Where v left
|
|
72
|
+
* the normal range and the x_i are finite and non-zero, f again at x_i 2^-e_i (each in [1, 2)), multiplied back by
|
|
73
|
+
* 2^(sum d_i e_i): the same roundings as f where the result is normal, but no intermediate power overflows or
|
|
74
|
+
* underflows (the variance (max - min)^2 / 12 of unif(0, 2e154) was Infinity for 3.3e307; issue #376)
|
|
75
|
+
*/
|
|
76
|
+
function rescaled(v, f, ...powers) {
|
|
77
|
+
if ((v >= MIN_NORMAL && v < Infinity) || !powers.every(([x]) => Number.isFinite(x) && x !== 0))
|
|
78
|
+
return v;
|
|
79
|
+
const e = powers.map(([x]) => Math.floor(Math.log2(Math.abs(x))));
|
|
80
|
+
return ldexp(f(...powers.map(([x], i) => ldexp(x, -e[i]))), powers.reduce((k, [, d], i) => k + d * e[i], 0));
|
|
31
81
|
}
|
|
32
82
|
/**
|
|
33
83
|
* Variance, skewness and excess kurtosis from the central moments E[(X - mean)^n], integrated numerically from the
|
|
@@ -38,17 +88,110 @@ function make(mean, variance, median, mode, skewness, kurtosis) {
|
|
|
38
88
|
function shape(f, mean, lower, upper) {
|
|
39
89
|
return central((t) => t, f, mean, mean, lower, upper);
|
|
40
90
|
}
|
|
91
|
+
/** Whether a QUADPACK result can be used: it converged, or its error estimate is still within 1e-8 of it */
|
|
92
|
+
function converged(r) {
|
|
93
|
+
return r.ier === 0 || r.abserr <= 1e-8 * Math.abs(r.result);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Variance, skewness and excess kurtosis from the logs l_r of the raw moments E[X^r], r = 1..4, through the ratios
|
|
97
|
+
* rho_r = E[X^r] / E[X]^r taken in logs (so heavy tails, where the integrals of shape() do not converge, neither
|
|
98
|
+
* overflow nor cancel: the leading terms dominate there)
|
|
99
|
+
*/
|
|
100
|
+
function fromLogRawMoments([l1, l2, l3, l4]) {
|
|
101
|
+
const r2 = l2 - 2 * l1;
|
|
102
|
+
const r3 = l3 - 3 * l1;
|
|
103
|
+
const r4 = l4 - 4 * l1;
|
|
104
|
+
const lv = r2 + Math.log(-Math.expm1(-r2)); // log(rho_2 - 1)
|
|
105
|
+
return {
|
|
106
|
+
variance: Math.exp(2 * l1 + lv),
|
|
107
|
+
skewness: Math.exp(r3 - 1.5 * lv) * (1 - 3 * Math.exp(r2 - r3) + 2 * Math.exp(-r3)),
|
|
108
|
+
kurtosis: Math.exp(r4 - 2 * lv) * (1 - 4 * Math.exp(r3 - r4) + 6 * Math.exp(r2 - r4) - 3 * Math.exp(-r4)) - 3,
|
|
109
|
+
ok: true,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
41
112
|
/** shape() over t: x(t) with weight w(t), the integral split at t = split where x = mean */
|
|
42
113
|
function central(x, w, mean, split, lower, upper) {
|
|
43
114
|
const tol = 50 * Number.EPSILON;
|
|
115
|
+
let ok = true;
|
|
44
116
|
const c = [2, 3, 4].map((n) => {
|
|
45
117
|
const g = (t) => (x(t) - mean) ** n * w(t);
|
|
46
|
-
const left = dqagse(g, lower, split, 0, tol, 200)
|
|
47
|
-
const right = upper === Infinity ? dqagie(g, split, 1, 0, tol, 200)
|
|
48
|
-
|
|
118
|
+
const left = dqagse(g, lower, split, 0, tol, 200);
|
|
119
|
+
const right = upper === Infinity ? dqagie(g, split, 1, 0, tol, 200) : dqagse(g, split, upper, 0, tol, 200);
|
|
120
|
+
if (!converged(left) || !converged(right))
|
|
121
|
+
ok = false;
|
|
122
|
+
return left.result + right.result;
|
|
49
123
|
});
|
|
50
124
|
const v = c[0];
|
|
51
|
-
|
|
125
|
+
// (as in expShape: v^1.5 and v^2 underflow where v is small, kumar(1e-220, 1) had skewness and kurtosis Infinity;
|
|
126
|
+
// issue #386)
|
|
127
|
+
const d3 = v ** 1.5;
|
|
128
|
+
const d4 = v * v;
|
|
129
|
+
return {
|
|
130
|
+
variance: v,
|
|
131
|
+
skewness: d3 >= MIN_NORMAL ? c[1] / d3 : c[1] / v / Math.sqrt(v),
|
|
132
|
+
kurtosis: (d4 >= MIN_NORMAL ? c[2] / d4 : c[2] / v / v) - 3,
|
|
133
|
+
ok,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** Shapes above this (Weibull's k, Kumaraswamy's a) go through expShape(); at 100 central() is still within 2e-14 */
|
|
137
|
+
const LARGE_SHAPE = 100;
|
|
138
|
+
/**
|
|
139
|
+
* Moments of X = c exp(Z / s) for a large shape s, through T = s expm1(log(e) / s) = s (X / c - 1), e the base
|
|
140
|
+
* variable (Weibull: e ~ Exp(1), c = 1; Kumaraswamy: e = b W with W ~ Beta(1, b), c = b^(-1/a)), integrated over the
|
|
141
|
+
* pieces. X lies within O(1/s) of c, too narrow for the quadrature in x (and the raw moments Gamma(1 + r/k) round to
|
|
142
|
+
* 1): k = 1e4 gave the skewness -3.03 for -1.139, k = 1e308 sd 0 and NaN (issues #240, #249). T has a spread of
|
|
143
|
+
* order 1 for every shape. Returns E[T], the variance of T and the skewness and kurtosis (those of X), or undefined
|
|
144
|
+
* where an integral did not converge
|
|
145
|
+
*/
|
|
146
|
+
function expShape(s, pieces) {
|
|
147
|
+
const tol = 50 * Number.EPSILON;
|
|
148
|
+
// the integral of h w over all pieces, each split at tSplit(piece); undefined unless every part converged or the
|
|
149
|
+
// error estimates together are within 1e-8 of the sum (a part far smaller than the sum may stop at its round-off)
|
|
150
|
+
const over = (h, tSplit) => {
|
|
151
|
+
let sum = 0;
|
|
152
|
+
let err = 0;
|
|
153
|
+
let all = true;
|
|
154
|
+
pieces.forEach((p, i) => {
|
|
155
|
+
const hp = h(i);
|
|
156
|
+
const h0 = p.mass === undefined ? 0 : hp(0);
|
|
157
|
+
const f = (t) => {
|
|
158
|
+
const v = p.w(t);
|
|
159
|
+
return v === 0 ? 0 : (p.mass === undefined ? hp(t) : hp(t) - h0) * v;
|
|
160
|
+
};
|
|
161
|
+
const t0 = tSplit(p);
|
|
162
|
+
for (const [lower, upper] of [[0, t0], [t0, p.upper]]) {
|
|
163
|
+
if (!(upper > lower))
|
|
164
|
+
continue;
|
|
165
|
+
const r = upper === Infinity ? dqagie(f, lower, 1, 0, tol, 200) : dqagse(f, lower, upper, 0, tol, 200);
|
|
166
|
+
sum += r.result;
|
|
167
|
+
err += r.abserr;
|
|
168
|
+
if (r.ier !== 0)
|
|
169
|
+
all = false;
|
|
170
|
+
}
|
|
171
|
+
if (p.mass !== undefined)
|
|
172
|
+
sum += p.mass * h0;
|
|
173
|
+
});
|
|
174
|
+
return all || err <= 1e-8 * Math.abs(sum) ? sum : undefined;
|
|
175
|
+
};
|
|
176
|
+
const Ts = pieces.map((p) => (t) => s * Math.expm1(p.logE(t) / s));
|
|
177
|
+
const meanT = over((i) => Ts[i], (p) => Math.min(1, p.upper / 2));
|
|
178
|
+
if (meanT === undefined || !(meanT / s > -1))
|
|
179
|
+
return undefined;
|
|
180
|
+
// log e where T = E[T]: each piece's integrals are split there
|
|
181
|
+
const split = s * Math.log1p(meanT / s);
|
|
182
|
+
const [c2, c3, c4] = [2, 3, 4].map((n) => over((i) => (t) => (Ts[i](t) - meanT) ** n, (p) => Math.min(Math.max(p.tAt(split), 0), p.upper)));
|
|
183
|
+
if (c2 === undefined || c3 === undefined || c4 === undefined)
|
|
184
|
+
return undefined;
|
|
185
|
+
// (c2^1.5 and c2^2 underflow for b = 1e-300, where the skewness is -1.7e150)
|
|
186
|
+
const d3 = c2 ** 1.5;
|
|
187
|
+
const d4 = c2 * c2;
|
|
188
|
+
return {
|
|
189
|
+
variance: c2,
|
|
190
|
+
skewness: d3 >= MIN_NORMAL ? c3 / d3 : c3 / c2 / Math.sqrt(c2),
|
|
191
|
+
kurtosis: (d4 >= MIN_NORMAL ? c4 / d4 : c4 / c2 / c2) - 3,
|
|
192
|
+
ok: true,
|
|
193
|
+
meanT,
|
|
194
|
+
};
|
|
52
195
|
}
|
|
53
196
|
/** floor(a b) of the exact product (a b rounded to double may cross an integer) */
|
|
54
197
|
function floorProduct(a, b) {
|
|
@@ -61,11 +204,25 @@ function productAtMost(a, b, c) {
|
|
|
61
204
|
const d = ddSub(ddMul(a, [b, 0]), [c, 0]);
|
|
62
205
|
return d[0] + d[1] <= 0;
|
|
63
206
|
}
|
|
207
|
+
/** x r <= (r - 1) mu exactly, for integers x >= 0 and r and a finite mu >= 0 (mu = m 2^e in BigInt) */
|
|
208
|
+
function exactModeTest(x, r, mu) {
|
|
209
|
+
const [m, e] = splitDouble(mu);
|
|
210
|
+
const R = BigInt(r);
|
|
211
|
+
const lhs = BigInt(x) * R;
|
|
212
|
+
const rhs = (R - 1n) * m;
|
|
213
|
+
return e >= 0 ? lhs <= rhs << BigInt(e) : lhs << BigInt(-e) <= rhs;
|
|
214
|
+
}
|
|
64
215
|
const has = (o, k) => o[k] !== undefined;
|
|
216
|
+
/** ab / (s^2 (s + 1)), from the ratios a / s and b / s when the products overflow or underflow (huge shapes) */
|
|
217
|
+
function betaVariance(a, b, s) {
|
|
218
|
+
const v = (a * b) / (s * s * (s + 1));
|
|
219
|
+
return v > 0 && v < Infinity ? v : ((a / s) * (b / s)) / (s + 1);
|
|
220
|
+
}
|
|
65
221
|
/**
|
|
66
222
|
* Mean, variance, standard deviation, median, mode, skewness and excess kurtosis of a distribution, given by the
|
|
67
223
|
* name of its d/p/q/r functions and their parameters (with the same names and defaults; `gamma` takes `rate` or
|
|
68
|
-
* `scale`, `nbinom` takes `prob` or `mu`). Invalid parameters give NaN throughout.
|
|
224
|
+
* `scale`, `nbinom` takes `prob` or `mu`). Invalid parameters give NaN throughout. The distributions are the central
|
|
225
|
+
* ones: `ncp` of `chisq`, `beta`, `t` and `f` is not taken (an unknown key, a RangeError).
|
|
69
226
|
* Not an R function. R has no function for these summaries; the R references are closed forms checked against
|
|
70
227
|
* numerical moments.
|
|
71
228
|
*
|
|
@@ -78,13 +235,15 @@ const has = (o, k) => o[k] !== undefined;
|
|
|
78
235
|
* @category Distributions
|
|
79
236
|
*/
|
|
80
237
|
export function distSummary(distribution, params = {}) {
|
|
238
|
+
if (Object.hasOwn(DIST_SUMMARY_KEYS, distribution))
|
|
239
|
+
checkOptions(params, DIST_SUMMARY_KEYS[distribution] ?? [], "distSummary"); // generated: option-keys.mjs
|
|
81
240
|
const P = params;
|
|
82
241
|
switch (distribution) {
|
|
83
242
|
case "norm": {
|
|
84
243
|
const { mean = 0, sd = 1 } = P;
|
|
85
244
|
if (!(Number.isFinite(mean) && sd > 0 && Number.isFinite(sd)))
|
|
86
245
|
return NAN;
|
|
87
|
-
return make(mean, sd * sd, mean, mean, 0, 0);
|
|
246
|
+
return make(mean, sd * sd, mean, mean, 0, 0, sd);
|
|
88
247
|
}
|
|
89
248
|
case "lnorm": {
|
|
90
249
|
const { meanlog = 0, sdlog = 1 } = P;
|
|
@@ -93,35 +252,67 @@ export function distSummary(distribution, params = {}) {
|
|
|
93
252
|
const s2 = sdlog * sdlog;
|
|
94
253
|
const em1 = Math.expm1(s2);
|
|
95
254
|
const w = Math.exp(s2);
|
|
96
|
-
|
|
255
|
+
// sqrt(em1): sdlog itself where s2 is below 1e-280 (em1 / s2 is 1 to double precision; s2 underflowed to 0 or onto
|
|
256
|
+
// the subnormals and took the sd and skewness with it: 0 for sdlog = 1e-200; issue #120)
|
|
257
|
+
const rootEm1 = s2 < 1e-280 ? sdlog : Math.sqrt(em1);
|
|
258
|
+
const expHalf = Math.exp(meanlog + s2 / 2);
|
|
259
|
+
const variance = em1 * Math.exp(2 * meanlog + s2);
|
|
260
|
+
const result = make(expHalf, variance, Math.exp(meanlog), Math.exp(meanlog - s2), (w + 2) * rootEm1,
|
|
97
261
|
// w^4 + 2 w^3 + 3 w^2 - 6 with w = 1 + em1, expanded in em1 (the w form cancels for small sdlog)
|
|
98
|
-
em1 * (16 + em1 * (15 + em1 * (6 + em1))));
|
|
262
|
+
em1 * (16 + em1 * (15 + em1 * (6 + em1))), rootEm1 * expHalf);
|
|
263
|
+
if (variance >= MIN_NORMAL && variance < Infinity)
|
|
264
|
+
return result;
|
|
265
|
+
// The variance (and with it maybe the sd) out of the normal range: em1 or the exponential factor overflowed or
|
|
266
|
+
// underflowed although the product may be representable (meanlog 400, sdlog 1e-200 gave 0 * Inf = NaN; meanlog
|
|
267
|
+
// -500, sdlog 27 an infinite variance and sd for e^458 and e^229). Rebuilt from log sd = meanlog + s2 / 2 +
|
|
268
|
+
// log(em1) / 2 in double-double; the sd as before where both of its factors were accurate (issue #120)
|
|
269
|
+
const s2dd = twoProd(sdlog, sdlog);
|
|
270
|
+
const logEm1 = s2 < 1e-280 ? ddMulNum(ddLog([sdlog, 0]), 2)
|
|
271
|
+
: s2 <= 1 ? ddLog([em1, 0])
|
|
272
|
+
: ddAdd(s2dd, [Math.log(-Math.expm1(-s2)), 0]);
|
|
273
|
+
const half = ddAdd(s2dd, logEm1);
|
|
274
|
+
const logSd = ddAdd([meanlog, 0], [half[0] / 2, half[1] / 2]);
|
|
275
|
+
const keepSd = Number.isFinite(rootEm1) && expHalf >= MIN_NORMAL && expHalf < Infinity;
|
|
276
|
+
return {
|
|
277
|
+
...result,
|
|
278
|
+
variance: ddExp([2 * logSd[0], 2 * logSd[1]])[0],
|
|
279
|
+
sd: keepSd ? result.sd : ddExp(logSd)[0],
|
|
280
|
+
};
|
|
99
281
|
}
|
|
100
282
|
case "unif": {
|
|
101
283
|
const { min = 0, max = 1 } = P;
|
|
102
284
|
if (!(Number.isFinite(min) && Number.isFinite(max) && min < max))
|
|
103
285
|
return NAN;
|
|
104
|
-
const m = min + (max - min) / 2;
|
|
105
|
-
|
|
286
|
+
const m = Number.isFinite(max - min) ? min + (max - min) / 2 : midpoint(min, max);
|
|
287
|
+
const w = max - min;
|
|
288
|
+
return make(m, rescaled(w ** 2 / 12, (x) => x ** 2 / 12, [w, 2]), m, NaN, 0, -1.2, (max / 2 - min / 2) * (2 / Math.sqrt(12)));
|
|
106
289
|
}
|
|
107
290
|
case "exp": {
|
|
108
291
|
const { rate = 1 } = P;
|
|
109
292
|
if (!(rate > 0 && Number.isFinite(rate)))
|
|
110
293
|
return NAN;
|
|
111
|
-
return make(1 / rate, 1 / (rate * rate), Math.LN2 / rate, 0, 2, 6);
|
|
294
|
+
return make(1 / rate, rescaled(1 / (rate * rate), (x) => 1 / (x * x), [rate, -2]), Math.LN2 / rate, 0, 2, 6, 1 / rate);
|
|
112
295
|
}
|
|
113
296
|
case "gamma": {
|
|
114
297
|
const k = P.shape ?? NaN;
|
|
115
|
-
|
|
298
|
+
// (rate and scale as dgamma: both only with scale = 1 / rate, otherwise invalid; the scale was taken and the rate
|
|
299
|
+
// ignored; issue #245)
|
|
300
|
+
let scale;
|
|
301
|
+
try {
|
|
302
|
+
scale = gammaScale({ ...(has(P, "rate") && { rate: P.rate }), ...(has(P, "scale") && { scale: P.scale }) });
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
return NAN;
|
|
306
|
+
}
|
|
116
307
|
if (!(k > 0 && Number.isFinite(k) && scale > 0 && Number.isFinite(scale)))
|
|
117
308
|
return NAN;
|
|
118
|
-
return make(k * scale, k * scale * scale, qgamma(0.5, k, { scale }), k >= 1 ? (k - 1) * scale : 0, 2 / Math.sqrt(k), 6 / k);
|
|
309
|
+
return make(k * scale, k * scale * scale, qgamma(0.5, k, { scale }), k >= 1 ? (k - 1) * scale : 0, 2 / Math.sqrt(k), 6 / k, Math.sqrt(k) * scale);
|
|
119
310
|
}
|
|
120
311
|
case "chisq": {
|
|
121
312
|
const df = P.df ?? NaN;
|
|
122
313
|
if (!(df > 0 && Number.isFinite(df)))
|
|
123
314
|
return NAN;
|
|
124
|
-
return make(df, 2 * df, qchisq(0.5, df), Math.max(df - 2, 0), Math.sqrt(8 / df), 12 / df);
|
|
315
|
+
return make(df, 2 * df, qchisq(0.5, df), Math.max(df - 2, 0), Math.sqrt(8 / df), 12 / df, Math.SQRT2 * Math.sqrt(df));
|
|
125
316
|
}
|
|
126
317
|
case "beta": {
|
|
127
318
|
const a = P.shape1 ?? NaN;
|
|
@@ -131,12 +322,40 @@ export function distSummary(distribution, params = {}) {
|
|
|
131
322
|
const s = a + b;
|
|
132
323
|
let mode;
|
|
133
324
|
if (a > 1 && b > 1)
|
|
134
|
-
mode = (a - 1) / (s - 2);
|
|
325
|
+
mode = Number.isFinite(s) ? (a - 1) / (s - 2) : (a - 1) / 2 / ((a - 1) / 2 + (b - 1) / 2);
|
|
135
326
|
else if ((a === 1 && b === 1) || (a < 1 && b < 1))
|
|
136
327
|
mode = NaN;
|
|
137
328
|
else
|
|
138
329
|
mode = a <= 1 && b >= 1 ? 0 : 1;
|
|
139
|
-
|
|
330
|
+
const median = qbeta(0.5, a, b);
|
|
331
|
+
if (!Number.isFinite(s)) {
|
|
332
|
+
// a + b overflows: from the halved shapes, p = a / (a + b), q = b / (a + b) and h = (a + b) / 2, with s + 1, s + 2
|
|
333
|
+
// and s + 3 equal to s (beta(1e308, 1e308) had mean 0, mode 0 and variance 0)
|
|
334
|
+
const h = a / 2 + b / 2;
|
|
335
|
+
const pa = a / 2 / h;
|
|
336
|
+
const pb = b / 2 / h;
|
|
337
|
+
return make(pa, (pa * pb) / h / 2, median, mode, (2 * (pb - pa)) / (Math.sqrt(pa) * Math.sqrt(pb) * Math.SQRT2 * Math.sqrt(h)), (3 * ((pa - pb) ** 2 / (pa * pb) - 1)) / h, (Math.sqrt(pa) * Math.sqrt(pb)) / Math.sqrt(h) / Math.SQRT2);
|
|
338
|
+
}
|
|
339
|
+
const ab = a * b;
|
|
340
|
+
// Where a b overflows or underflows (shapes beyond 1e154 or below 1e-154), the moments in p = a / s and q = b / s
|
|
341
|
+
// (numerators and denominators divided by s^2; beta(1e200, 1e200) had kurtosis NaN)
|
|
342
|
+
const inRange = ab >= MIN_NORMAL && ab < Infinity;
|
|
343
|
+
const pa = a / s;
|
|
344
|
+
const pb = b / s;
|
|
345
|
+
// The closed forms also where a b is in range but their numerator or denominator is not (unbalanced or large
|
|
346
|
+
// shapes: (a - b)^2 and a b (s + 2)(s + 3) overflow, beta(1e-3, 1e155) had kurtosis NaN for 6000 and
|
|
347
|
+
// beta(1e100, 1e100) 0 for -3e-100; issue #385)
|
|
348
|
+
const direct = (num, den) => inRange && (num === 0 || Math.abs(num) >= MIN_NORMAL) && Math.abs(num) < Infinity && den >= MIN_NORMAL && den < Infinity
|
|
349
|
+
? num / den
|
|
350
|
+
: undefined;
|
|
351
|
+
return make(pa, betaVariance(a, b, s), median, mode, direct(2 * (b - a) * Math.sqrt(s + 1), (s + 2) * Math.sqrt(ab)) ??
|
|
352
|
+
(2 * (pb - pa) * Math.sqrt(s + 1)) / ((s + 2) * Math.sqrt(pa * pb)), direct(6 * ((a - b) ** 2 * (s + 1) - ab * (s + 2)), ab * (s + 2) * (s + 3)) ??
|
|
353
|
+
(6 * ((pa - pb) ** 2 * ((s + 1) / (s + 2)) - pa * pb)) / (pa * pb * (s + 3)),
|
|
354
|
+
// (where a / s or b / s is below the normal range, the square roots taken before the ratios: beta(1e-308, 1e154)
|
|
355
|
+
// had sd 0 for 1e-308; issue #241)
|
|
356
|
+
pa >= MIN_NORMAL && pb >= MIN_NORMAL
|
|
357
|
+
? (Math.sqrt(pa) * Math.sqrt(pb)) / Math.sqrt(s + 1)
|
|
358
|
+
: ((Math.sqrt(a) / Math.sqrt(s)) * (Math.sqrt(b) / Math.sqrt(s))) / Math.sqrt(s + 1));
|
|
140
359
|
}
|
|
141
360
|
case "t": {
|
|
142
361
|
const df = P.df ?? NaN;
|
|
@@ -152,12 +371,55 @@ export function distSummary(distribution, params = {}) {
|
|
|
152
371
|
if (!(d1 > 0 && d2 > 0 && Number.isFinite(d1) && Number.isFinite(d2)))
|
|
153
372
|
return NAN;
|
|
154
373
|
const mean = d2 > 2 ? d2 / (d2 - 2) : NaN;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
374
|
+
// Each closed form where its numerator and denominator are normal numbers (the same bits as before); otherwise
|
|
375
|
+
// from ratios that stay in range, with t = d1 + d2 - 2 halved (df1 = df2 = 1e200 gave variance NaN, sd 0 and
|
|
376
|
+
// skewness 0; 1e308 NaN throughout; issue #235)
|
|
377
|
+
const inRange = (x) => x >= MIN_NORMAL && x < Infinity;
|
|
378
|
+
const t = d1 + d2 - 2;
|
|
379
|
+
const th = d1 / 2 + d2 / 2 - 1; // t / 2
|
|
380
|
+
const r = d2 / (d2 - 2);
|
|
381
|
+
const u = (d2 - 2) / d1; // t / d1 - 1
|
|
382
|
+
const w = (d2 - 2) / (d2 - 4);
|
|
383
|
+
let variance = d2 > 4 ? NaN : d2 > 2 ? Infinity : NaN;
|
|
384
|
+
let sd = variance;
|
|
385
|
+
if (d2 > 4) {
|
|
386
|
+
const num = 2 * d2 * d2 * t;
|
|
387
|
+
const den = d1 * (d2 - 2) ** 2 * (d2 - 4);
|
|
388
|
+
// 2 r^2 (t / d1) / (d2 - 4) with t / d1 = 1 + u; where u overflows, 2 r^2 (1 / (d2 - 4) + w / d1)
|
|
389
|
+
variance = inRange(num) && inRange(den) ? num / den : Number.isFinite(u) ? (2 * r * r * (1 + u)) / (d2 - 4) : 2 * r * r * (1 / (d2 - 4) + w / d1);
|
|
390
|
+
// (the standard deviation from the square roots of the factors, where the variance overflows)
|
|
391
|
+
const sNum = Math.SQRT2 * d2 * Math.sqrt(t);
|
|
392
|
+
const sDen = Math.sqrt(d1) * (d2 - 2) * Math.sqrt(d2 - 4);
|
|
393
|
+
sd =
|
|
394
|
+
inRange(sNum) && inRange(sDen)
|
|
395
|
+
? sNum / sDen
|
|
396
|
+
: Number.isFinite(u)
|
|
397
|
+
? Math.SQRT2 * r * (Math.sqrt(1 + u) / Math.sqrt(d2 - 4))
|
|
398
|
+
: Math.SQRT2 * r * (Math.sqrt(w) / Math.sqrt(d1));
|
|
399
|
+
}
|
|
400
|
+
let skewness = NaN;
|
|
401
|
+
if (d2 > 6) {
|
|
402
|
+
const num = (2 * d1 + d2 - 2) * Math.sqrt(8 * (d2 - 4));
|
|
403
|
+
const den = (d2 - 6) * Math.sqrt(d1 * t);
|
|
404
|
+
// (d1 + t) / sqrt(d1 t) = sqrt(d1 / t) + sqrt(t / d1)
|
|
405
|
+
const sq1 = Math.sqrt(d1);
|
|
406
|
+
const sqt = Math.sqrt(th) * Math.SQRT2;
|
|
407
|
+
const x = (Math.sqrt(8) * Math.sqrt(d2 - 4)) / (d2 - 6);
|
|
408
|
+
skewness = inRange(num) && inRange(den) ? num / den : (sq1 / sqt) * x + (sqt * x) / sq1;
|
|
409
|
+
}
|
|
410
|
+
let kurtosis = NaN;
|
|
411
|
+
if (d2 > 8) {
|
|
412
|
+
const num = 12 * (d1 * (5 * d2 - 22) * t + (d2 - 4) * (d2 - 2) ** 2);
|
|
413
|
+
const den = d1 * (d2 - 6) * (d2 - 8) * t;
|
|
414
|
+
// 12 ((5 d2 - 22) / ((d2 - 6)(d2 - 8)) + (d2 - 4)(d2 - 2)^2 / (d1 (d2 - 6)(d2 - 8) t))
|
|
415
|
+
kurtosis =
|
|
416
|
+
inRange(num) && inRange(den)
|
|
417
|
+
? num / den
|
|
418
|
+
: 12 *
|
|
419
|
+
(((5 - 22 / d2) * (d2 / (d2 - 6))) / (d2 - 8) +
|
|
420
|
+
((d2 - 4) / (d2 - 6)) * ((d2 - 2) / (d2 - 8)) * ((d2 / 2 - 1) / th / d1));
|
|
421
|
+
}
|
|
422
|
+
return make(mean, variance, qf(0.5, d1, d2), d1 > 2 ? ((d1 - 2) / d1) * (d2 / (d2 + 2)) : 0, skewness, kurtosis, sd);
|
|
161
423
|
}
|
|
162
424
|
case "cauchy": {
|
|
163
425
|
const { location = 0, scale = 1 } = P;
|
|
@@ -171,14 +433,24 @@ export function distSummary(distribution, params = {}) {
|
|
|
171
433
|
if (!(k > 0 && Number.isFinite(k) && lam > 0 && Number.isFinite(lam)))
|
|
172
434
|
return NAN;
|
|
173
435
|
const mean = gammaFn(1 + 1 / k);
|
|
174
|
-
|
|
175
|
-
|
|
436
|
+
// X^k ~ Exp(1)
|
|
437
|
+
const big = k > LARGE_SHAPE ? expShape(k, [{ logE: Math.log, w: (e) => Math.exp(-e), upper: Infinity, tAt: Math.exp }]) : undefined;
|
|
438
|
+
if (big !== undefined) {
|
|
439
|
+
const sd = lam * (Math.sqrt(big.variance) / k);
|
|
440
|
+
const median = qweibull(0.5, k, { scale: lam });
|
|
441
|
+
return make(lam * mean, sd * sd, median, lam * Math.pow((k - 1) / k, 1 / k), big.skewness, big.kurtosis, sd);
|
|
442
|
+
}
|
|
443
|
+
// E[X^r] = Gamma(1 + r/k) for scale 1: used when the integrals do not converge (small shapes, heavy tails)
|
|
444
|
+
const si = shape((x) => dweibull1(x, k, 1, false), mean, 0, Infinity);
|
|
445
|
+
const s = si.ok ? si : fromLogRawMoments([1, 2, 3, 4].map((r) => lgamma(1 + r / k)));
|
|
446
|
+
return make(lam * mean, rescaled(lam * lam * s.variance, (x) => x * x * s.variance, [lam, 2]), qweibull(0.5, k, { scale: lam }), k > 1 ? lam * Math.pow((k - 1) / k, 1 / k) : 0, s.skewness, s.kurtosis, lam * Math.sqrt(s.variance));
|
|
176
447
|
}
|
|
177
448
|
case "logis": {
|
|
178
449
|
const { location = 0, scale = 1 } = P;
|
|
179
450
|
if (!(Number.isFinite(location) && scale > 0 && Number.isFinite(scale)))
|
|
180
451
|
return NAN;
|
|
181
|
-
|
|
452
|
+
const v = (scale * scale * Math.PI * Math.PI) / 3;
|
|
453
|
+
return make(location, rescaled(v, (x) => (x * x * Math.PI * Math.PI) / 3, [scale, 2]), location, location, 0, 1.2, (scale * Math.PI) / Math.sqrt(3));
|
|
182
454
|
}
|
|
183
455
|
case "binom": {
|
|
184
456
|
const n = P.size ?? NaN;
|
|
@@ -186,11 +458,16 @@ export function distSummary(distribution, params = {}) {
|
|
|
186
458
|
if (!(Number.isInteger(n) && n >= 0 && p >= 0 && p <= 1))
|
|
187
459
|
return NAN;
|
|
188
460
|
const q = 1 - p;
|
|
461
|
+
// (a point mass: no skewness or kurtosis)
|
|
462
|
+
const point = n === 0 || p === 0 || p === 1;
|
|
189
463
|
// P(x) >= P(x - 1) exactly when x <= (n + 1) p
|
|
190
|
-
return make(n * p, n * p * q, qbinom(0.5, n, p), Math.min(n, floorProduct(n + 1, p)), (q - p) / Math.sqrt(n * p * q), (1 - 6 * p * q) / (n * p * q));
|
|
464
|
+
return make(n * p, n * p * q, qbinom(0.5, n, p), Math.min(n, floorProduct(n + 1, p)), point ? NaN : (q - p) / Math.sqrt(n * p * q), point ? NaN : (1 - 6 * p * q) / (n * p * q));
|
|
191
465
|
}
|
|
192
466
|
case "pois": {
|
|
193
467
|
const lambda = P.lambda ?? NaN;
|
|
468
|
+
// (lambda = 0 is the point mass at 0, as in dpois, ppois and qpois; issue #379)
|
|
469
|
+
if (lambda === 0)
|
|
470
|
+
return POINT_AT_0;
|
|
194
471
|
if (!(lambda > 0 && Number.isFinite(lambda)))
|
|
195
472
|
return NAN;
|
|
196
473
|
return make(lambda, lambda, qpois(0.5, lambda), Math.floor(lambda), 1 / Math.sqrt(lambda), 1 / lambda);
|
|
@@ -200,28 +477,67 @@ export function distSummary(distribution, params = {}) {
|
|
|
200
477
|
if (!(p > 0 && p <= 1))
|
|
201
478
|
return NAN;
|
|
202
479
|
const q = 1 - p;
|
|
203
|
-
|
|
480
|
+
// (p = 1 is a point mass at 0: no skewness or kurtosis)
|
|
481
|
+
return make(q / p, q / (p * p), qgeom(0.5, p), 0, p === 1 ? NaN : (2 - p) / Math.sqrt(q), p === 1 ? NaN : 6 + (p * p) / q, Math.sqrt(q) / p);
|
|
204
482
|
}
|
|
205
483
|
case "nbinom": {
|
|
206
484
|
const r = P.size ?? NaN;
|
|
485
|
+
// (size = 0 is the point mass at 0, as in dnbinom, pnbinom and qnbinom, with a valid prob or mu; before p = size /
|
|
486
|
+
// (size + mu), which is 0 or 0/0 there; issue #379)
|
|
487
|
+
if (r === 0)
|
|
488
|
+
return (has(P, "mu") ? P.mu >= 0 && P.mu < Infinity : P.prob > 0 && P.prob <= 1) ? POINT_AT_0 : NAN;
|
|
207
489
|
if (!(r > 0 && Number.isFinite(r)))
|
|
208
490
|
return NAN;
|
|
209
|
-
|
|
491
|
+
// (where size + mu overflows, from the halved parameters: size = mu = 1e308 gave p = 0 and NaN throughout; issue #236)
|
|
492
|
+
const p = has(P, "mu") ? (Number.isFinite(r + P.mu) ? r / (r + P.mu) : r / 2 / (r / 2 + P.mu / 2)) : (P.prob ?? NaN);
|
|
210
493
|
if (!(p > 0 && p <= 1))
|
|
211
494
|
return NAN;
|
|
212
495
|
const q = 1 - p;
|
|
213
496
|
const opts = has(P, "mu") ? { mu: P.mu } : { prob: p };
|
|
214
497
|
// P(x) >= P(x - 1) exactly when x p <= (r - 1)(1 - p), i.e. (x + r - 1) p <= r - 1 (with mu: x r <= (r - 1) mu)
|
|
215
|
-
|
|
498
|
+
// Beyond 2^53, r - 1 rounds to r (an integer mu passed as its own mode, floor(mu - mu / r) = mu - 1) and
|
|
499
|
+
// (r - 1) mu can overflow, the bound Infinity passing every step (size 4.3e305, mu 1.8e11 did not end): there
|
|
500
|
+
// the test in exact integers, as r is one (issue #236 follow-up 3)
|
|
501
|
+
const ok = (x) => has(P, "mu")
|
|
502
|
+
? r > 2 ** 53
|
|
503
|
+
? exactModeTest(x, r, P.mu)
|
|
504
|
+
: productAtMost([x, 0], r, (r - 1) * P.mu)
|
|
505
|
+
: productAtMost(ddAdd([r - 1, 0], [x, 0]), p, r - 1);
|
|
216
506
|
let mode = 0;
|
|
217
507
|
if (r > 1) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
508
|
+
const guess = Math.floor(((r - 1) * q) / p);
|
|
509
|
+
// With mu much smaller than size, q = 1 - p loses its digits (0 where p rounds to 1) and the guess is far below
|
|
510
|
+
// the mode, which the search then walked one by one (size 1e100, mu 1e30 did not end; issue #236 follow-up):
|
|
511
|
+
// there the guess from (r - 1) / r mu. The search ends at the same mode from either start.
|
|
512
|
+
const near = has(P, "mu") && guess < 2 ** 53 ? Math.floor(((r - 1) / r) * P.mu) : guess;
|
|
513
|
+
// Beyond 2^53 the integers are not all doubles (mode + 1 = mode, and the search did not end): the rounded value
|
|
514
|
+
if (!(guess < 2 ** 53))
|
|
515
|
+
mode = guess;
|
|
516
|
+
else if (!(near < 2 ** 53))
|
|
517
|
+
mode = near;
|
|
518
|
+
else {
|
|
519
|
+
mode = Math.max(0, near - 1);
|
|
520
|
+
while (ok(mode + 1))
|
|
521
|
+
mode++;
|
|
522
|
+
while (mode > 0 && !ok(mode))
|
|
523
|
+
mode--;
|
|
524
|
+
}
|
|
223
525
|
}
|
|
224
|
-
|
|
526
|
+
const mu = P.mu;
|
|
527
|
+
if (mu !== undefined && mu > 0 && mu / r < 2 ** -26) {
|
|
528
|
+
// With mu far below size, q = 1 - p keeps only the digits of p's rounding (size 1e10, mu 1 gave a mean 8e-8
|
|
529
|
+
// off; size 1e100, mu 1e30 a mean 0 and NaN skewness): the moments directly in mu and t = mu / size, as
|
|
530
|
+
// mu (1 + t), (1 + 2t) / sqrt(mu (1 + t)) and 6 / size + 1 / (mu (1 + t)) (issue #236 follow-up 2). The
|
|
531
|
+
// products of the closed forms overflow or underflow (size 1e308 with mu 1), these do not.
|
|
532
|
+
const t = mu / r;
|
|
533
|
+
const variance = mu + mu * t;
|
|
534
|
+
// (sqrt(mu) sqrt(1 + t) where the variance is subnormal)
|
|
535
|
+
const sd = variance >= MIN_NORMAL ? Math.sqrt(variance) : Math.sqrt(mu) * Math.sqrt(1 + t);
|
|
536
|
+
return make(mu, variance, qnbinom(0.5, r, opts), mode, (1 + 2 * t) / sd, 6 / r + 1 / variance, sd);
|
|
537
|
+
}
|
|
538
|
+
return make((r * q) / p, rescaled((r * q) / (p * p), (x, y) => (x * q) / (y * y), [r, 1], [p, -2]), qnbinom(0.5, r, opts), mode,
|
|
539
|
+
// (p = 1 is a point mass at 0: no skewness or kurtosis)
|
|
540
|
+
p === 1 ? NaN : (2 - p) / Math.sqrt(r * q), p === 1 ? NaN : 6 / r + (p * p) / (r * q), (Math.sqrt(r) * Math.sqrt(q)) / p);
|
|
225
541
|
}
|
|
226
542
|
case "hyper": {
|
|
227
543
|
const m = P.m ?? NaN;
|
|
@@ -230,63 +546,167 @@ export function distSummary(distribution, params = {}) {
|
|
|
230
546
|
if (!([m, n, k].every((v) => Number.isInteger(v) && v >= 0) && k <= m + n))
|
|
231
547
|
return NAN;
|
|
232
548
|
const N = m + n;
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
549
|
+
// Where (k + 1)(m + 1) overflows, the floor of the exact quotient in integers, rounded to a double (beyond 2^53,
|
|
550
|
+
// as the nbinom mode); k m / N and the variance from the ratio m / N where the products overflow (m = n = k =
|
|
551
|
+
// 1e200 gave mean, variance and mode Infinity; issue #237)
|
|
552
|
+
const moProduct = (k + 1) * (m + 1);
|
|
553
|
+
const mo = Number.isFinite(moProduct)
|
|
554
|
+
? Math.floor(moProduct / (N + 2))
|
|
555
|
+
: Number(((BigInt(k) + 1n) * (BigInt(m) + 1n)) / (BigInt(m) + BigInt(n) + 2n));
|
|
556
|
+
// a point mass (k = 0, m = 0, n = 0 or k = N) has variance 0 and the mean k m / N, 0 for N = 0 (the formulas gave
|
|
557
|
+
// 0/0 for N = 0 and N = 1; issue #19)
|
|
558
|
+
const point = k === 0 || m === 0 || n === 0 || k === N;
|
|
559
|
+
const mean = N === 0 ? 0 : Number.isFinite(k * m) ? (k * m) / N : k * (m / N);
|
|
560
|
+
const vPlain = ((mean * (n / N)) * (N - k)) / (N - 1);
|
|
561
|
+
const variance = point ? 0 : Number.isFinite(vPlain) ? vPlain : mean * (n / N) * ((N - k) / (N - 1));
|
|
562
|
+
let skewness;
|
|
563
|
+
let kurt;
|
|
564
|
+
if (k === 0 || m === 0 || n === 0 || k === N) {
|
|
565
|
+
// a point mass: no skewness or kurtosis
|
|
566
|
+
skewness = NaN;
|
|
567
|
+
kurt = NaN;
|
|
568
|
+
}
|
|
569
|
+
else if (N <= 3) {
|
|
570
|
+
// the closed forms divide by N - 2 and N - 3: the central moments over the (at most two-point) support
|
|
571
|
+
const c = [2, 3, 4].map((r) => {
|
|
572
|
+
let s = 0;
|
|
573
|
+
for (let x = Math.max(0, k - n); x <= Math.min(k, m); x++)
|
|
574
|
+
s += dhyper1(x, m, n, k, false) * (x - mean) ** r;
|
|
575
|
+
return s;
|
|
576
|
+
});
|
|
577
|
+
skewness = c[1] / c[0] ** 1.5;
|
|
578
|
+
kurt = c[2] / (c[0] * c[0]) - 3;
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
skewness = ((N - 2 * m) * Math.sqrt(N - 1) * (N - 2 * k)) / (Math.sqrt(k * m * n * (N - k)) * (N - 2));
|
|
582
|
+
const num = (N - 1) * N * N * (N * (N + 1) - 6 * m * n - 6 * k * (N - k)) + 6 * k * m * n * (N - k) * (5 * N - 6);
|
|
583
|
+
const den = k * m * n * (N - k) * (N - 2) * (N - 3);
|
|
584
|
+
if (Number.isFinite(num) && Number.isFinite(den))
|
|
585
|
+
kurt = num / den;
|
|
586
|
+
else {
|
|
587
|
+
// the products overflow: numerator and denominator divided by N^5, in p = m / N, q = n / N, f = k / N and
|
|
588
|
+
// g = (N - k) / N (m = n = k = 1e150 gave NaN for about -1e-150; issue #244)
|
|
589
|
+
const pq = (m / N) * (n / N);
|
|
590
|
+
const fg = (k / N) * ((N - k) / N);
|
|
591
|
+
const a = 1 + 1 / N - 6 * pq - 6 * fg;
|
|
592
|
+
kurt = ((1 - 1 / N) * a + 6 * pq * fg * (5 - 6 / N)) / (k * pq * ((N - k) / N) * ((1 - 2 / N) * (1 - 3 / N)));
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return make(mean, variance, qhyper(0.5, m, n, k), mo, skewness, kurt);
|
|
237
596
|
}
|
|
238
597
|
case "invgamma": {
|
|
239
598
|
const a = P.alpha ?? NaN;
|
|
240
599
|
const b = P.beta ?? 1;
|
|
241
600
|
if (!(a > 0 && Number.isFinite(a) && b > 0 && Number.isFinite(b)))
|
|
242
601
|
return NAN;
|
|
243
|
-
|
|
602
|
+
// Where b^2, (a - 1)^2 (a - 2), (a - 1) sqrt(a - 2) or (a - 3)(a - 4) leave the range, from r = b / (a - 1) and
|
|
603
|
+
// ratios of a (alpha = beta = 1e308 gave variance NaN, sd 0 and kurtosis NaN; issue #238)
|
|
604
|
+
const r = b / (a - 1);
|
|
605
|
+
const b2 = b * b;
|
|
606
|
+
const vDen = (a - 1) ** 2 * (a - 2);
|
|
607
|
+
const sdDen = (a - 1) * Math.sqrt(a - 2);
|
|
608
|
+
const kurt = (30 * a - 66) / ((a - 3) * (a - 4));
|
|
609
|
+
return make(a > 1 ? r : Infinity, a > 2 ? (b2 >= MIN_NORMAL && b2 < Infinity && vDen < Infinity ? b2 / vDen : r * (r / (a - 2))) : a > 1 ? Infinity : NaN, qinvgamma(0.5, a, { beta: b }), b / (a + 1), a > 3 ? (4 * Math.sqrt(a - 2)) / (a - 3) : NaN, a > 4 ? (kurt > 0 && kurt < Infinity ? kurt : ((30 - 66 / a) / (a - 3)) * (a / (a - 4))) : NaN, a > 2 ? (sdDen < Infinity ? b / sdDen : r / Math.sqrt(a - 2)) : undefined);
|
|
244
610
|
}
|
|
245
611
|
case "kumar": {
|
|
246
612
|
const a = P.a ?? NaN;
|
|
247
613
|
const b = P.b ?? NaN;
|
|
248
614
|
if (!(a > 0 && b > 0 && Number.isFinite(a) && Number.isFinite(b)))
|
|
249
615
|
return NAN;
|
|
250
|
-
//
|
|
251
|
-
|
|
616
|
+
// X = c (b W)^(1/a) with c = b^(-1/a), W = X^a ~ Beta(1, b): for a > 100, the moments in T = a (X / c - 1) over
|
|
617
|
+
// e = b W, density (1 - e/b)^(b - 1) on (0, b). b >= 1: one piece in e (beyond 1500 the density is below
|
|
618
|
+
// e^-1500). b < 1: the density diverges at e = b, so (b/2, b) is taken in r = b - e, the density
|
|
619
|
+
// (r/b)^(b - 1) by its total 2^-b and the integral of the differences from r = 0 (in e alone, a = 150, b = 0.1
|
|
620
|
+
// lost 3e-10 of the kurtosis)
|
|
621
|
+
const inE = (upper) => ({
|
|
622
|
+
logE: Math.log,
|
|
623
|
+
w: (e) => (e >= b ? 0 : Math.exp((b - 1) * Math.log1p(-e / b))),
|
|
624
|
+
upper,
|
|
625
|
+
tAt: Math.exp,
|
|
626
|
+
});
|
|
627
|
+
const nearB = {
|
|
628
|
+
logE: (r) => Math.log(b - r),
|
|
629
|
+
w: (r) => Math.exp((b - 1) * Math.log(r / b)),
|
|
630
|
+
upper: b / 2,
|
|
631
|
+
tAt: (le) => b - Math.exp(le),
|
|
632
|
+
mass: Math.pow(0.5, b),
|
|
633
|
+
};
|
|
634
|
+
const big = a > LARGE_SHAPE ? expShape(a, b >= 1 ? [inE(Math.min(b, 1500))] : [inE(b / 2), nearB]) : undefined;
|
|
635
|
+
const c = big === undefined ? NaN : Math.exp(-Math.log(b) / a);
|
|
636
|
+
// mean b B(1 + 1/a, b); for a > 100, c (1 + E[T] / a) (b B(1 + 1/a, b) was 1e-14 off at a = b = 1e100)
|
|
637
|
+
const mean = big === undefined ? b * Math.exp(lbeta(1 + 1 / a, b)) : c * (1 + big.meanT / a);
|
|
252
638
|
// over the density, or over u = F(x) when the density diverges at 1 (b < 1: there the quantile is smooth)
|
|
253
|
-
const
|
|
254
|
-
?
|
|
255
|
-
:
|
|
639
|
+
const si = big !== undefined
|
|
640
|
+
? big
|
|
641
|
+
: b < 1
|
|
642
|
+
? central((u) => qkumar1(u, a, b, true, false), () => 1, mean, pkumar1(mean, a, b, true, false), 0, 1)
|
|
643
|
+
: shape((x) => dkumar1(x, a, b, false), mean, 0, 1);
|
|
644
|
+
// E[X^r] = b B(1 + r/a, b): used when the integrals do not converge
|
|
645
|
+
const s = si.ok ? si : fromLogRawMoments([1, 2, 3, 4].map((r) => Math.log(b) + lbeta(1 + r / a, b)));
|
|
646
|
+
// the sd in T's units (the variance of X underflows for a = 1e308: sd 1.28e-308)
|
|
647
|
+
const sd = big === undefined ? undefined : c * (Math.sqrt(big.variance) / a);
|
|
256
648
|
let mode;
|
|
257
|
-
|
|
258
|
-
|
|
649
|
+
// ((a - 1) / (a b - 1))^(1/a); where a b overflows (a b - 1 is a b there), as ((a - 1) / a)^(1/a) b^(-1/a)
|
|
650
|
+
// (a = b = 1e200 gave 0 for 1; issue #239)
|
|
651
|
+
if (a >= 1 && b >= 1) {
|
|
652
|
+
if (a === 1 && b === 1)
|
|
653
|
+
mode = NaN;
|
|
654
|
+
else
|
|
655
|
+
mode = Number.isFinite(a * b) ? Math.pow((a - 1) / (a * b - 1), 1 / a) : Math.pow((a - 1) / a, 1 / a) * Math.pow(b, -1 / a);
|
|
656
|
+
}
|
|
259
657
|
else if (a < 1 && b < 1)
|
|
260
658
|
mode = NaN;
|
|
261
659
|
else
|
|
262
660
|
mode = a < 1 ? 0 : 1;
|
|
263
|
-
return make(mean, s.variance, qkumar(0.5, a, b), mode, s.skewness, s.kurtosis);
|
|
661
|
+
return make(mean, sd === undefined ? s.variance : sd * sd, qkumar(0.5, a, b), mode, s.skewness, s.kurtosis, sd);
|
|
264
662
|
}
|
|
265
663
|
case "pareto": {
|
|
266
664
|
const { a = 1, b = 1 } = P;
|
|
267
665
|
if (!(a > 0 && Number.isFinite(a) && b > 0 && Number.isFinite(b)))
|
|
268
666
|
return NAN;
|
|
269
|
-
|
|
667
|
+
// Each moment from the closed form, or where an intermediate product overflows (a b, b² a, a³, 2 (1 + a), b √a:
|
|
668
|
+
// a = b = 1e308 gave mean Inf, variance NaN, kurtosis NaN; issue #34) from ratios in which the powers cancel
|
|
669
|
+
const orElse = (v, alt) => (Number.isFinite(v) ? v : alt());
|
|
670
|
+
const r = b / (a - 1);
|
|
671
|
+
return make(a > 1 ? orElse((a * b) / (a - 1), () => b * (a / (a - 1))) : Infinity, a > 2 ? orElse((b * b * a) / ((a - 1) ** 2 * (a - 2)), () => r * r * (a / (a - 2))) : a > 1 ? Infinity : NaN, b * Math.pow(2, 1 / a), b, a > 3 ? orElse(((2 * (1 + a)) / (a - 3)) * Math.sqrt((a - 2) / a), () => 2 * ((1 + a) / (a - 3)) * Math.sqrt((a - 2) / a)) : NaN, a > 4
|
|
672
|
+
? orElse((6 * (a ** 3 + a * a - 6 * a - 2)) / (a * (a - 3) * (a - 4)), () => (6 * (1 + 1 / a - 6 / (a * a) - 2 / a ** 3)) / ((1 - 3 / a) * (1 - 4 / a)))
|
|
673
|
+
: NaN, a > 2 ? orElse((b * Math.sqrt(a)) / ((a - 1) * Math.sqrt(a - 2)), () => r * Math.sqrt(a / (a - 2))) : undefined);
|
|
270
674
|
}
|
|
271
675
|
case "triang": {
|
|
272
676
|
const { a = -1, b = 1 } = P;
|
|
273
|
-
const c = P.c ?? (a
|
|
677
|
+
const c = P.c ?? midpoint(a, b);
|
|
274
678
|
if (!(Number.isFinite(a) && Number.isFinite(b) && a < b && a <= c && c <= b))
|
|
275
679
|
return NAN;
|
|
276
|
-
|
|
277
|
-
|
|
680
|
+
// q = a² + b² + c² - ab - ac - bc as (b - a)(b - c) + (c - a)², whose terms are all non-negative: the
|
|
681
|
+
// expanded form cancels when the location is large against the width ((1e10, 1e10 + 1, 1e10 + 0.25) gave the
|
|
682
|
+
// variance 910 for 0.045) and overflows to Inf - Inf near the top of the range (issue #243). The skewness's
|
|
683
|
+
// numerator (a + b - 2c)(2a - b - c)(a - 2b + c) is (w - v)(u + v)(u + w) in the same differences
|
|
684
|
+
const e = Math.floor(Math.log2(Math.max(Math.abs(a), Math.abs(b), Math.abs(c))));
|
|
685
|
+
// from the parameters divided by 2^e where b - a overflows, the powers of the parameters' scale could
|
|
686
|
+
// overflow or underflow (the largest outside [2^-300, 2^300]) or those of the width could underflow (below
|
|
687
|
+
// 2^-290: q^1.5 fell onto the subnormals); otherwise the products are in range
|
|
688
|
+
const scaled = !Number.isFinite(b - a) || e < -300 || e >= 300 || b - a < 2 ** -290;
|
|
689
|
+
const [x, y, z] = scaled ? [a, b, c].map((v) => ldexp(v, -e)) : [a, b, c];
|
|
690
|
+
const u = y - x;
|
|
691
|
+
const v = z - x;
|
|
692
|
+
const w = y - z;
|
|
693
|
+
const q = u * w + v * v;
|
|
694
|
+
return make(Number.isFinite(a + b + c) ? (a + b + c) / 3 : a / 3 + b / 3 + c / 3, scaled ? ldexp(q / 18, 2 * e) : q / 18, qtriang(0.5, { a, b, c }), c, (Math.SQRT2 * (w - v) * (u + v) * (u + w)) / (5 * q ** 1.5), -0.6, scaled ? ldexp(Math.sqrt(q / 18), e) : Math.sqrt(q / 18));
|
|
278
695
|
}
|
|
279
696
|
case "laplace": {
|
|
280
697
|
const { mu = 0, sigma = 1 } = P;
|
|
281
698
|
if (!(Number.isFinite(mu) && sigma > 0 && Number.isFinite(sigma)))
|
|
282
699
|
return NAN;
|
|
283
|
-
return make(mu, 2 * sigma * sigma, mu, mu, 0, 3);
|
|
700
|
+
return make(mu, 2 * sigma * sigma, mu, mu, 0, 3, Math.SQRT2 * sigma);
|
|
284
701
|
}
|
|
285
702
|
case "arcsine": {
|
|
286
703
|
const { a = 0, b = 1 } = P;
|
|
287
704
|
if (!(Number.isFinite(a) && Number.isFinite(b) && a < b))
|
|
288
705
|
return NAN;
|
|
289
|
-
|
|
706
|
+
// symmetric: the median is the mean (qarcsine(0.5) rounds to 1e-16 of the width); b - a may overflow
|
|
707
|
+
const mean = Number.isFinite(b - a) ? a + (b - a) / 2 : midpoint(a, b);
|
|
708
|
+
const w = b - a;
|
|
709
|
+
return make(mean, rescaled(w ** 2 / 8, (x) => x ** 2 / 8, [w, 2]), mean, NaN, 0, -1.5, (b / 2 - a / 2) / Math.SQRT2);
|
|
290
710
|
}
|
|
291
711
|
}
|
|
292
712
|
throw new RangeError(`unknown distribution '${String(distribution)}'`);
|