@numero-uno-jpn/rstats 0.15.3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +166 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-VLUL2HFW.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +24 -4
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +12 -4
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +5 -0
- package/dist/distributions/f.js +32 -17
- package/dist/distributions/gamma.js +27 -3
- package/dist/distributions/geometric.js +49 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +2 -1
- package/dist/distributions/laplace.js +27 -6
- package/dist/distributions/logistic.js +4 -1
- package/dist/distributions/lognormal.js +8 -2
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +94 -17
- package/dist/distributions/normal.js +9 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +4 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/triangular.js +10 -5
- package/dist/distributions/tukey.js +5 -2
- package/dist/distributions/weibull.js +10 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/distributions/nt.js
CHANGED
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
// the rounding (laplace); for a small ν (a < 1e-5) formula B is used at any t, and a t so near 0 that P is Φ(-δ) to
|
|
25
25
|
// the double is given that value (nearZero). The smaller tail is computed, the larger is 1 - (smaller).
|
|
26
26
|
//
|
|
27
|
+
// Far tails of a large δ (a log beyond about 1e9, or |δ| > 2^32), where the logs of the integrand in double precision
|
|
28
|
+
// no longer place its peak: Laplace's method with the derivatives scaled and t S - δ in double-double (laplaceWide),
|
|
29
|
+
// the trapezoidal sum with the logs in double-double where the expansion is not accurate enough, the integral of the
|
|
30
|
+
// ratio to Φ(-δ) where the peak is broad (integrateShifted), and for t >> sqrt(ν) an expansion over Z (zTail).
|
|
31
|
+
//
|
|
27
32
|
// For accuracy, the node w and, on top of it, e^(w/2), the exponent a (e^w - 1 - w) and the normal argument
|
|
28
33
|
// t S - δ are formed in double-double (rounding in each is amplified hundreds of times in the tails). Nodes
|
|
29
34
|
// where the rounding effect is estimated to be at most eps times the peak value are computed in double
|
|
@@ -154,14 +159,27 @@ function logIntegrand(pr, w) {
|
|
|
154
159
|
let L = pr.logC - a * m;
|
|
155
160
|
let d1 = -a * Math.expm1(w);
|
|
156
161
|
let d2 = -a * ew;
|
|
162
|
+
if (pr.shift !== undefined && w > 700) {
|
|
163
|
+
// (a e^w from its log, where e^w overflows: for a subnormal a the peak of integrateShifted is near w = 723)
|
|
164
|
+
const aew = Math.exp(Math.log(a) + w);
|
|
165
|
+
L = pr.logC - (aew - a * (1 + w));
|
|
166
|
+
d1 = -(aew - a);
|
|
167
|
+
d2 = -aew;
|
|
168
|
+
}
|
|
157
169
|
if (pr.kind === "cdf") {
|
|
158
170
|
const r = millsInv(z);
|
|
159
|
-
|
|
171
|
+
if (pr.complement) {
|
|
172
|
+
// log |ρ - 1| and its derivative (the second is left to bracketedPeak)
|
|
173
|
+
const lr = logPhiRatio(tS, delta);
|
|
174
|
+
// (e^lr / (e^lr - 1) is 1 where both overflow)
|
|
175
|
+
return [L + logAbsExpm1(lr), d1 + (tS / 2) * r * (lr > 40 ? 1 : Math.exp(lr) / Math.expm1(lr)), NaN];
|
|
176
|
+
}
|
|
177
|
+
L += pr.shift === undefined ? pnorm(z, { logP: true }) : logPhiRatio(tS, delta);
|
|
160
178
|
d1 += (tS / 2) * r;
|
|
161
179
|
d2 += (tS / 4) * r - (tS / 2) * (tS / 2) * r * (z + r);
|
|
162
180
|
}
|
|
163
181
|
else {
|
|
164
|
-
L += w / 2 - 0.5 * z * z - LN_SQRT_2PI;
|
|
182
|
+
L += pr.shift === undefined ? w / 2 - 0.5 * z * z - LN_SQRT_2PI : w / 2 + tS * (delta - tS / 2);
|
|
165
183
|
d1 += 0.5 - (z * tS) / 2;
|
|
166
184
|
d2 += -(tS * tS) / 4 - (z * tS) / 4;
|
|
167
185
|
}
|
|
@@ -207,8 +225,9 @@ function logPhiDD(zd) {
|
|
|
207
225
|
const corr = zd[1] * millsInv(z);
|
|
208
226
|
if (z > -5)
|
|
209
227
|
return twoSum(pnorm(z, { logP: true }), corr);
|
|
210
|
-
|
|
211
|
-
|
|
228
|
+
// (z²/2 as z (z/2), the same to the bit: z² alone overflows from |z| = 1.34e154, z²/2 from 1.9e154)
|
|
229
|
+
const zh = ddMul(zd, [zd[0] / 2, zd[1] / 2]);
|
|
230
|
+
return ddAdd([-zh[0], -zh[1]], [Math.log(0.5 * erfcxNonNegative(-z * Math.SQRT1_2)), 0]);
|
|
212
231
|
}
|
|
213
232
|
/**
|
|
214
233
|
* Value of the integrand at node w (double-double). If log, return the log in double-double
|
|
@@ -254,11 +273,23 @@ function integrand(pr, w, log, peak = Infinity) {
|
|
|
254
273
|
const g = x > X_TINY ? gammaInc(a, x, lower, false) : Math.exp(gammaLog(a, w[0] + w[1], lower)[0]);
|
|
255
274
|
return [(t / 2) * (S[0] + S[1]) * Math.exp(-0.5 * zz[0] - LN_SQRT_2PI) * (1 - zz[1] / 2) * g, 0];
|
|
256
275
|
}
|
|
257
|
-
// a (e^w - 1 - w); (a tiny a reaches e^w near the overflow threshold, where the splitting in ddMulNum overflows
|
|
276
|
+
// a (e^w - 1 - w); (a tiny a reaches e^w near the overflow threshold, where the splitting in ddMulNum overflows;
|
|
277
|
+
// in integrateShifted beyond it, from the log of a e^w)
|
|
258
278
|
const m = expm1mx(w, ew);
|
|
259
|
-
const E =
|
|
279
|
+
const E = pr.shift !== undefined && w[0] > 700
|
|
280
|
+
? twoSum(Math.exp(Math.log(a) + w[0] + w[1]), -a * (1 + w[0]))
|
|
281
|
+
: m[0] < 1e290
|
|
282
|
+
? ddMulNum(m, a)
|
|
283
|
+
: twoSum(a * m[0], a * m[1]);
|
|
260
284
|
if (!(E[0] < Infinity))
|
|
261
285
|
return [log ? -Infinity : 0, 0];
|
|
286
|
+
if (pr.shift !== undefined) {
|
|
287
|
+
// (integrateShifted: the log less log Φ(-δ) or log φ(δ), of a moderate size)
|
|
288
|
+
const y = ddMulNum(S, t)[0];
|
|
289
|
+
const lr = pr.kind === "cdf" ? logPhiRatio(y, delta) : y * (delta - y / 2);
|
|
290
|
+
const l = ddAdd([pr.logC - E[0], -E[1]], [pr.complement ? logAbsExpm1(lr) : lr, 0]);
|
|
291
|
+
return pr.kind === "cdf" ? l : ddAdd(l, [w[0] / 2, w[1] / 2]);
|
|
292
|
+
}
|
|
262
293
|
const zd = ddAdd(ddMulNum(S, t), [-delta, 0]);
|
|
263
294
|
const z = zd[0];
|
|
264
295
|
if (pr.kind === "cdf") {
|
|
@@ -280,7 +311,7 @@ function integrand(pr, w, log, peak = Infinity) {
|
|
|
280
311
|
return [Math.exp(lv) * (1 - E[1] - zz[1] / 2) * (S[0] + S[1]), 0];
|
|
281
312
|
}
|
|
282
313
|
/** Trapezoidal sum (step h in z, extending both ways until negligible) */
|
|
283
|
-
function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
|
|
314
|
+
function trapezoid(pr, w0, sigma, h, log, offset, odd, peak, offsetLow) {
|
|
284
315
|
// If odd, only the odd-indexed nodes (the points added by halving the step)
|
|
285
316
|
let sum = 0;
|
|
286
317
|
const node = (z) => {
|
|
@@ -289,6 +320,8 @@ function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
|
|
|
289
320
|
const d = (sigma / 2) * (z - Math.expm1(-z));
|
|
290
321
|
const v = integrand(pr, twoSum(w0, d), log, peak);
|
|
291
322
|
const jac = (sigma / 2) * (1 + ez);
|
|
323
|
+
if (offsetLow !== undefined)
|
|
324
|
+
return Math.exp(v[0] - offset + (v[1] - offsetLow)) * jac;
|
|
292
325
|
return log ? Math.exp(v[0] - offset) * (1 + v[1]) * jac : v[0] * jac;
|
|
293
326
|
};
|
|
294
327
|
const start = odd ? 1 : 0;
|
|
@@ -317,11 +350,31 @@ function trapezoid(pr, w0, sigma, h, log, offset, odd, peak) {
|
|
|
317
350
|
return sum;
|
|
318
351
|
}
|
|
319
352
|
function integrate(pr, needLog) {
|
|
353
|
+
// Far tails of a large δ (LOG_LARGE, D_LARGE): over z for a large t (zTail; the density is symmetric under
|
|
354
|
+
// (t, δ) -> (-t, -δ), and for the distribution function a negative δ is the upper tail), otherwise laplaceWide below
|
|
355
|
+
const large = farLogEstimate(pr) > LOG_LARGE || Math.abs(pr.delta) > D_LARGE;
|
|
356
|
+
if (large && pr.t * pr.delta > 0 && Math.abs(pr.t) / Math.sqrt(2 * pr.a) >= T_Z) {
|
|
357
|
+
return zTail(pr.a, Math.abs(pr.t), Math.abs(pr.delta), pr.kind === "pdf" ? "pdf" : pr.delta > 0 ? "lower" : "upper");
|
|
358
|
+
}
|
|
320
359
|
if (pr.a > A_LAPLACE) {
|
|
321
360
|
const v = laplace(pr);
|
|
322
361
|
if (v !== undefined)
|
|
323
362
|
return v;
|
|
324
363
|
}
|
|
364
|
+
// (for the distribution function a negative δ here is the upper tail near 1, whose logs are of a moderate size)
|
|
365
|
+
if (large && !(pr.kind === "cdf" && pr.delta < 0)) {
|
|
366
|
+
const sharp = { w: NaN, sigma: NaN };
|
|
367
|
+
const v = laplaceWide(pr, sharp);
|
|
368
|
+
if (v !== undefined)
|
|
369
|
+
return v;
|
|
370
|
+
// A sharp peak (L'' <= -1e5) where the expansion is not accurate enough (u at the peak is of order -10: log Φ(u)
|
|
371
|
+
// is not yet nearly quadratic): its log there is at most about 1e17 (beyond, the expansion's error is below the
|
|
372
|
+
// rounding), so the trapezoidal sum with the logs of the nodes and of the peak in double-double resolves it. A
|
|
373
|
+
// broad peak: integrateShifted
|
|
374
|
+
if (!Number.isNaN(sharp.w))
|
|
375
|
+
return integrateCore(pr, true, { w: sharp.w, sigma: sharp.sigma, L: NaN }, true);
|
|
376
|
+
return integrateShifted(pr);
|
|
377
|
+
}
|
|
325
378
|
if (pr.kind === "cdf") {
|
|
326
379
|
// (for a tiny a the density of w is spread over w down to about -1/a, and the peak of formula A is not found;
|
|
327
380
|
// formula B is used at any t)
|
|
@@ -355,6 +408,20 @@ function integrate(pr, needLog) {
|
|
|
355
408
|
}
|
|
356
409
|
return integrateCore(pr, needLog);
|
|
357
410
|
}
|
|
411
|
+
/**
|
|
412
|
+
* -log of the far tail (cdf, δ > 0 and t below it) or of the density far from the centre, roughly: the minimum over
|
|
413
|
+
* S of (t S - δ)²/2 + a S², δ²/2 / (1 + t²/(2a)) for t δ > 0, and δ²/2 otherwise (within a factor of about 2 where
|
|
414
|
+
* it is large). The peak of the integrand itself (findPeak) is no guide: where Φ(t S - δ) is a steep step, formula A's
|
|
415
|
+
* peak is not found, and its value can be that of a node far in the left tail of Φ
|
|
416
|
+
*/
|
|
417
|
+
function farLogEstimate(pr) {
|
|
418
|
+
const { a, t, delta, kind } = pr;
|
|
419
|
+
if (kind === "cdf" && !(delta > 0))
|
|
420
|
+
return 0;
|
|
421
|
+
const d = Math.abs(delta);
|
|
422
|
+
const h = t * delta > 0 ? d / Math.hypot(1, t / Math.sqrt(2 * a)) : d;
|
|
423
|
+
return (h * h) / 2;
|
|
424
|
+
}
|
|
358
425
|
/**
|
|
359
426
|
* Below this a, formula B is used at any t: the peak of formula A is not found for a below about 1e-15, and from
|
|
360
427
|
* 1e-5 down its error reaches 5e-15 of P, where formula B, a correction of order a log(t²/a) to Φ(-δ), keeps 1e-16
|
|
@@ -469,6 +536,9 @@ function laplace(pr) {
|
|
|
469
536
|
const ew = Math.exp(w);
|
|
470
537
|
if (!(w > -1390 && L2 >= 1e5))
|
|
471
538
|
return undefined;
|
|
539
|
+
// (the peak beyond the bracket, for a large δ: pt(1e9, 1e6, ncp 1e20) was -3.9e38, its log is -5e27; laplaceWide)
|
|
540
|
+
if (p.d1 > 0 && 50 - w < 1e-9)
|
|
541
|
+
return undefined;
|
|
472
542
|
const r3 = (-a * ew + f[2]) / L2;
|
|
473
543
|
const r4 = (-a * ew + f[3]) / L2;
|
|
474
544
|
const c1 = r4 / L2 / 8 + (5 * r3 * r3) / L2 / 24;
|
|
@@ -495,6 +565,282 @@ function laplace(pr) {
|
|
|
495
565
|
const q = cdf ? Math.abs(v) / 2 / (Math.sqrt(L2) * Math.max(1, -u)) : 0;
|
|
496
566
|
return 1 / (L2 * L2) + q ** 6 <= 1e-17 * Math.max(1, Math.abs(value.log)) ? value : undefined;
|
|
497
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* laplace where the log of the integrand is beyond LOG_LARGE (a large δ, at any a): there the peak can lie beyond
|
|
570
|
+
* w = 50 (S* is about t δ/(t² + 2a) for t > 0), and v = t S, v² and a e^w can overflow while the answer is finite. The
|
|
571
|
+
* derivatives d1, L2 and f are formed in units of 2^(2k), with 2^k about max(|v|, sqrt(a e^w)) / 2^400, and u from v
|
|
572
|
+
* in double-double (where t² >> a, u = v - δ is a small part of δ)
|
|
573
|
+
*/
|
|
574
|
+
function laplaceWide(pr, sharp) {
|
|
575
|
+
const { a, t, delta, kind } = pr;
|
|
576
|
+
const cdf = kind === "cdf";
|
|
577
|
+
const la = Math.log(a);
|
|
578
|
+
const at = (w) => {
|
|
579
|
+
const lg2 = Math.max(Math.log2(Math.abs(t)) + w / 2 / Math.LN2, (la + w) / 2 / Math.LN2);
|
|
580
|
+
const k = Math.max(0, Math.ceil(lg2) - 400);
|
|
581
|
+
// (beyond the double range of v the derivatives are not formed: the peak is below there)
|
|
582
|
+
if (lg2 > 1100) {
|
|
583
|
+
const f = [NaN, NaN, NaN, NaN];
|
|
584
|
+
return { u: NaN, v: NaN, vs: NaN, sk: 1, k, f, d1: NaN, L2: NaN, aew: NaN };
|
|
585
|
+
}
|
|
586
|
+
const sk = 2 ** -k;
|
|
587
|
+
const vs = t * Math.exp(w / 2 - k * Math.LN2);
|
|
588
|
+
const v = vs / sk;
|
|
589
|
+
const u = Math.abs(w) < 1 ? t - delta + t * Math.expm1(w / 2) : v - delta;
|
|
590
|
+
// a e^w 2^-2k (from its log where e^w overflows: a subnormal a puts the peak near w = 735)
|
|
591
|
+
const direct = k === 0 && w < 700;
|
|
592
|
+
const aew = direct ? a * Math.exp(w) : Math.exp(la + w - 2 * k * Math.LN2);
|
|
593
|
+
const v2 = vs * vs;
|
|
594
|
+
let f;
|
|
595
|
+
if (cdf) {
|
|
596
|
+
const [l1, l2, l3, l4] = logPhiDerivs(u);
|
|
597
|
+
const l1v = l1 * sk * vs;
|
|
598
|
+
const l3v = (l3 / sk) * vs;
|
|
599
|
+
const l4v2 = (((l4 / sk) * vs) / sk) * vs;
|
|
600
|
+
f = [
|
|
601
|
+
l1v / 2,
|
|
602
|
+
(l2 * v2 + l1v) / 4,
|
|
603
|
+
(l3v * v2 + 3 * l2 * v2 + l1v) / 8,
|
|
604
|
+
(l4v2 * v2 + 6 * l3v * v2 + 7 * l2 * v2 + l1v) / 16,
|
|
605
|
+
];
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
const uv = u * sk * vs;
|
|
609
|
+
f = [0.5 * sk * sk - uv / 2, -(v2 + uv) / 4, -(3 * v2 + uv) / 8, -(7 * v2 + uv) / 16];
|
|
610
|
+
}
|
|
611
|
+
const d1 = (direct ? -a * Math.expm1(w) : -(aew - a * sk * sk)) + f[0];
|
|
612
|
+
return { u, v, vs, sk, k, f, d1, L2: aew - f[1], aew };
|
|
613
|
+
};
|
|
614
|
+
// w*: as in laplace, over w up to 1418 (e^(w/2) up to the overflow threshold), from the estimate of the peak; a d1
|
|
615
|
+
// that is not formed (beyond the double range) moves the upper end down. Where L' is about -a e^w Newton's steps are
|
|
616
|
+
// about 1 long, so a step that does not halve the one before last is replaced by bisection (as in rtsafe)
|
|
617
|
+
let lo = -1400;
|
|
618
|
+
let hi = 1418;
|
|
619
|
+
let w = 0;
|
|
620
|
+
if (t > 0 && delta > 0) {
|
|
621
|
+
const w0 = 2 * Math.log(delta / (t + (2 * a) / t));
|
|
622
|
+
if (w0 > lo && w0 < hi)
|
|
623
|
+
w = w0;
|
|
624
|
+
}
|
|
625
|
+
let p = at(w);
|
|
626
|
+
let dx = hi - lo;
|
|
627
|
+
let dxOld = dx;
|
|
628
|
+
for (let i = 0; i < 400 && hi - lo > 4e-16 * Math.max(1, Math.abs(w)); i++) {
|
|
629
|
+
if (Number.isNaN(p.d1) || Number.isNaN(p.L2)) {
|
|
630
|
+
hi = w;
|
|
631
|
+
w = (lo + hi) / 2;
|
|
632
|
+
p = at(w);
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
const step = p.d1 / p.L2;
|
|
636
|
+
if ((Math.abs(step) * Math.sqrt(p.L2)) / p.sk < 1e-9)
|
|
637
|
+
break;
|
|
638
|
+
if (p.d1 > 0)
|
|
639
|
+
lo = w;
|
|
640
|
+
else if (p.d1 < 0)
|
|
641
|
+
hi = w;
|
|
642
|
+
else
|
|
643
|
+
break;
|
|
644
|
+
const next = w + step;
|
|
645
|
+
if (next > lo && next < hi && Math.abs(2 * step) <= Math.abs(dxOld)) {
|
|
646
|
+
dxOld = dx;
|
|
647
|
+
dx = step;
|
|
648
|
+
w = next;
|
|
649
|
+
}
|
|
650
|
+
else {
|
|
651
|
+
dxOld = dx;
|
|
652
|
+
dx = (hi - lo) / 2;
|
|
653
|
+
w = lo + dx;
|
|
654
|
+
}
|
|
655
|
+
p = at(w);
|
|
656
|
+
}
|
|
657
|
+
const { u, vs, sk, k, f, L2, aew } = p;
|
|
658
|
+
if (!(w > -1390 && (k > 0 || L2 >= 1e5)) || !Number.isFinite(L2))
|
|
659
|
+
return undefined;
|
|
660
|
+
// (the peak, for the trapezoidal sum where the expansion is not accurate enough)
|
|
661
|
+
sharp.w = w;
|
|
662
|
+
sharp.sigma = Math.min(sk / Math.sqrt(L2), 10);
|
|
663
|
+
const r3 = (-aew + f[2]) / L2;
|
|
664
|
+
const r4 = (-aew + f[3]) / L2;
|
|
665
|
+
const c1 = ((r4 / L2 / 8 + (5 * r3 * r3) / L2 / 24) * sk) * sk;
|
|
666
|
+
// log(L2/a) (L2 here in units of 2^(2k)), without cancellation near w = 0
|
|
667
|
+
let lr;
|
|
668
|
+
if (k === 0 && w < 700) {
|
|
669
|
+
const x = Math.expm1(w) - f[1] / a;
|
|
670
|
+
lr = Math.abs(x) < 0.5 ? Math.log1p(x) : Math.log(Math.exp(w) - f[1] / a);
|
|
671
|
+
}
|
|
672
|
+
else
|
|
673
|
+
lr = Math.log(L2) + 2 * k * Math.LN2 - la;
|
|
674
|
+
// a (e^w - 1 - w), with a e^w from its log beyond the overflow of e^w (it may still overflow: the log is then -Inf)
|
|
675
|
+
const aE = w > 700 ? Math.exp(la + w) - a * (1 + w) : a * expm1mxNum(w);
|
|
676
|
+
const rest = -0.5 * lr - stirlerr(a) + Math.log1p(c1) - aE;
|
|
677
|
+
const ud = Math.abs(w) < 1
|
|
678
|
+
? ddAdd(twoSum(t, -delta), twoProd(t, Math.expm1(w / 2)))
|
|
679
|
+
: ddAdd(ddMulNum(ddExp([w / 2, 0]), t), [-delta, 0]);
|
|
680
|
+
let value;
|
|
681
|
+
if (cdf) {
|
|
682
|
+
const l = logPhiDD(ud);
|
|
683
|
+
const log = l[0] + (l[1] + rest);
|
|
684
|
+
value = { lin: log < -745 ? 0 : pnorm(ud[0]) * (1 + ud[1] * millsInv(ud[0])) * Math.exp(rest), log };
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
// (u²/2 as u (u/2): u² alone overflows from |u| = 1.34e154, u²/2 from 1.9e154)
|
|
688
|
+
const uh = ddMul(ud, [ud[0] / 2, ud[1] / 2]);
|
|
689
|
+
const log = -uh[0] + (rest + w / 2 - LN_SQRT_2PI - uh[1]);
|
|
690
|
+
value = { lin: log < -745 ? 0 : Math.exp(log), log };
|
|
691
|
+
}
|
|
692
|
+
const q = cdf ? Math.abs(vs) / 2 / (Math.sqrt(L2) * Math.max(1, -u)) : 0;
|
|
693
|
+
const inv = (sk * sk) / L2;
|
|
694
|
+
return inv * inv + q ** 6 <= 1e-17 * Math.max(1, Math.abs(value.log)) ? value : undefined;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Beyond this far log estimate (farLogEstimate), the double-precision log of the integrand has an absolute error (eps
|
|
698
|
+
* times it) that no longer locates the peak of the trapezoidal sum (the sum overflowed or vanished from an estimate of
|
|
699
|
+
* about 5e11 on: pt(5, 3, ncp 1e7) gave a log of +Infinity, pt(1, 10, ncp 1e9) NaN). There the integral is taken by
|
|
700
|
+
* zTail, laplaceWide or, where the peak is broad, with Φ(-δ) (φ(δ) for the density) taken out (integrateShifted)
|
|
701
|
+
*/
|
|
702
|
+
const LOG_LARGE = 2 ** 30;
|
|
703
|
+
/**
|
|
704
|
+
* Beyond this |δ| the logs of the integrand reach δ²/2 at some nodes even where the answer is moderate (the old
|
|
705
|
+
* integral gave pt(1.6e15, 0.5, ncp 1e20) a log of +Infinity and pt(7.9e17, 1e5, ncp 1e20) -δ²/2). There the far log
|
|
706
|
+
* estimate is below LOG_LARGE only for t/sqrt(2a) above 2^16, where zTail applies
|
|
707
|
+
*/
|
|
708
|
+
const D_LARGE = 2 ** 32;
|
|
709
|
+
/**
|
|
710
|
+
* From this t/sqrt(2a) on (t δ > 0, beyond LOG_LARGE) the integrals are taken over z = Z (zTail). In w the peak narrows
|
|
711
|
+
* as t grows: placing it on the nearest double leaves an error of about L'' ulp(w)²/2, w² eps t²/(8a) relative to the
|
|
712
|
+
* log (180 eps at t/sqrt(2a) = 2^26, w = 27, where pt(t, 3, ncp 1e14) stepped by 0.1 in 1e12); here it is below
|
|
713
|
+
* eps/500 for any w. The terms zTail leaves out are about 64 a⁴/t⁶ in the log, far below eps |log| from here on
|
|
714
|
+
*/
|
|
715
|
+
const T_Z = 2 ** 12;
|
|
716
|
+
/**
|
|
717
|
+
* The tails and the density for t/sqrt(2a) >= T_Z, t δ > 0 (here both positive), over z = Z (formula B):
|
|
718
|
+
* P[T <= t] = Φ(-δ) + ∫ φ(z) Q(a, x(z)) dz, P[T > t] = ∫ φ(z) P(a, x(z)) dz,
|
|
719
|
+
* f(t) = 2/(t Γ(a)) ∫ φ(z) x(z)^a e^(-x(z)) dz,
|
|
720
|
+
* x(z) = a ((δ + z)/t)² = x0 (1 + 2z/δ + z²/δ²), x0 = a (δ/t)² (the integrals over z > -δ). With g = log Q, log P or
|
|
721
|
+
* a log x - x, and its derivatives in log x, g1 = x g', g2 = x² g'' (these do not overflow for a tiny a, where g' is
|
|
722
|
+
* about 1/(x log(1/x))), the integral is e^g(x0) E[e^(b Z + k Z²/2)] = e^g(x0) exp(b²/(2(1 - k))) / sqrt(1 - k),
|
|
723
|
+
* b = 2 g1/δ, k = (4 g2 + 2 g1)/δ² (g to second order in z). The terms left out are about 64 a⁴/t⁶ in the log, and
|
|
724
|
+
* z below -δ (cut off) lies some δ beyond the peak at z = b (|b| is at most about 2a δ/t², below δ 2^-23)
|
|
725
|
+
*/
|
|
726
|
+
function zTail(a, t, delta, kind) {
|
|
727
|
+
const y = delta / t;
|
|
728
|
+
const x0 = a * y * y;
|
|
729
|
+
if (!Number.isFinite(x0)) {
|
|
730
|
+
// (x0 beyond the double range: Q and the density are 0 to the double, P is 1)
|
|
731
|
+
const log = kind === "upper" ? 0 : kind === "lower" ? pnorm(-delta, { logP: true }) : -Infinity;
|
|
732
|
+
return { lin: Math.exp(log), log };
|
|
733
|
+
}
|
|
734
|
+
let base;
|
|
735
|
+
let g1;
|
|
736
|
+
let g2;
|
|
737
|
+
if (kind === "pdf") {
|
|
738
|
+
base = Math.LN2 - lgamma1p(a) + Math.log(a) - Math.log(t) + a * Math.log(x0) - x0;
|
|
739
|
+
g1 = a - x0;
|
|
740
|
+
g2 = -a;
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
const upper = kind === "upper";
|
|
744
|
+
base = gammaInc(a, x0, upper, true);
|
|
745
|
+
if (!upper && x0 > 1e4 * (a + 1)) {
|
|
746
|
+
// Q = h S, h = x^(a-1) e^-x / Γ(a), S = Σ (a-1)...(a-k)/x^k: x d log Q/dx = -x/S, and x² d²log Q/dx² = x² S'/S²
|
|
747
|
+
// (from the logs of h and Q, which are both about -x, the derivative would keep an error of eps x)
|
|
748
|
+
let term = 1;
|
|
749
|
+
let sum = 1;
|
|
750
|
+
let dsum = 0;
|
|
751
|
+
for (let j = 1; j < 20; j++) {
|
|
752
|
+
term *= (a - j) / x0;
|
|
753
|
+
sum += term;
|
|
754
|
+
dsum -= j * term;
|
|
755
|
+
if (Math.abs(term) < 1e-20)
|
|
756
|
+
break;
|
|
757
|
+
}
|
|
758
|
+
g1 = -x0 / sum;
|
|
759
|
+
g2 = (dsum * x0) / (sum * sum);
|
|
760
|
+
}
|
|
761
|
+
else {
|
|
762
|
+
// x d log G/dx = ∓x^a e^-x / (Γ(a) G) for G = Q, P, and x² d²log G/dx² = g1 (a - 1 - x - g1)
|
|
763
|
+
const h = Math.exp(dpoisRaw(a, x0, true) + Math.log(a) - base);
|
|
764
|
+
g1 = upper ? h : -h;
|
|
765
|
+
g2 = g1 * (a - 1 - x0 - g1);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
const b = (2 * g1) / delta;
|
|
769
|
+
const k = (4 * g2 + 2 * g1) / delta / delta;
|
|
770
|
+
const logI = base + (b * b) / (2 * (1 - k)) - 0.5 * Math.log1p(-k);
|
|
771
|
+
const log = kind === "lower" ? logAddExp(pnorm(-delta, { logP: true }), logI) : logI;
|
|
772
|
+
return { lin: log < -745 ? 0 : Math.exp(log), log };
|
|
773
|
+
}
|
|
774
|
+
/** log |e^x - 1| (x beyond 40: x, to 1e-17) */
|
|
775
|
+
function logAbsExpm1(x) {
|
|
776
|
+
return x > 40 ? x : Math.log(Math.abs(Math.expm1(x)));
|
|
777
|
+
}
|
|
778
|
+
/** log Φ(y - δ) - log Φ(-δ), where both are far in the left tail as the difference of the exponents and erfcx */
|
|
779
|
+
function logPhiRatio(y, delta) {
|
|
780
|
+
const z1 = y - delta;
|
|
781
|
+
if (z1 < -5 && delta > 5) {
|
|
782
|
+
return y * (delta - y / 2) + Math.log(erfcxNonNegative(-z1 * Math.SQRT1_2) / erfcxNonNegative(delta * Math.SQRT1_2));
|
|
783
|
+
}
|
|
784
|
+
return pnorm(z1, { logP: true }) - pnorm(-delta, { logP: true });
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* The integral beyond LOG_LARGE where Laplace's method does not apply (a broad peak: t S small beside δ), as Φ(-δ) times
|
|
788
|
+
* E[Φ(t S - δ)/Φ(-δ)] (the density: φ(δ) times E[S φ(t S - δ)/φ(δ)]): the log of the ratio, log Φ(t S - δ) - log Φ(-δ)
|
|
789
|
+
* or t S (δ - t S/2), is of a moderate size there, and the trapezoidal sum of formula A finds its peak
|
|
790
|
+
*/
|
|
791
|
+
function integrateShifted(pr) {
|
|
792
|
+
const shift = pr.kind === "cdf" ? pnorm(-pr.delta, { logP: true }) : -(pr.delta * (pr.delta / 2)) - LN_SQRT_2PI;
|
|
793
|
+
if (shift === -Infinity)
|
|
794
|
+
return { lin: 0, log: -Infinity };
|
|
795
|
+
const { a, t, delta } = pr;
|
|
796
|
+
// log λ, λ = |t δ|: for t δ < 0 the ratio is about e^(-λ S) (with S for the density), and the peak, at S = (2a + m)/λ
|
|
797
|
+
// (m = 0, 1 for the distribution function and the density), falls out of the range of w (S below e^-700) from
|
|
798
|
+
// λ = e^690 on. There the integral is the Laplace transform of the density of S near 0, 2 a^a/Γ(a) S^(2a-1):
|
|
799
|
+
// E[S^m e^(-λ S)] = 2 a^a Γ(2a + m) / (Γ(a) λ^(2a + m)), to relative order (2a + m)/λ (also beside 1/sqrt(a), the scale
|
|
800
|
+
// on which the density of S departs from its power, and the departure of the ratio from e^(-λ S), of order m/δ²)
|
|
801
|
+
const lLam = Math.log(Math.abs(t)) + Math.log(Math.abs(delta));
|
|
802
|
+
if (t * delta < 0 && lLam > 690) {
|
|
803
|
+
const p = pr.kind === "cdf" ? 2 * a : 2 * a + 1;
|
|
804
|
+
const log = shift + Math.LN2 + a * Math.log(a) - (lgamma1p(a) - Math.log(a)) + (lgamma1p(p) - Math.log(p)) - p * lLam;
|
|
805
|
+
return { lin: log < -745 ? 0 : Math.exp(log), log };
|
|
806
|
+
}
|
|
807
|
+
if (!(pr.kind === "cdf" && a < A_TINY)) {
|
|
808
|
+
const sp = { ...pr, shift };
|
|
809
|
+
return integrateCore(sp, true, bracketedPeak(sp));
|
|
810
|
+
}
|
|
811
|
+
// For a tiny a most of the probability of S lies near 0, spread over w down to about -1/a, where ρ is 1: the integral
|
|
812
|
+
// is 1 ∓ E[|ρ - 1|] (t < 0, t > 0), whose integrand peaks where t S δ is of order 1
|
|
813
|
+
const sp = { ...pr, shift, complement: true };
|
|
814
|
+
const lc = integrateCore(sp, true, bracketedPeak(sp)).log;
|
|
815
|
+
const log = shift + (t < 0 ? Math.log1p(-Math.exp(lc)) : logAddExp(0, lc));
|
|
816
|
+
return { lin: log < -745 ? 0 : Math.exp(log), log };
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* The peak of the log integrand by bisection on the sign of L' (the peak of integrateShifted is broad, so 1e-12 in w is
|
|
820
|
+
* ample). findPeak's Newton steps stall where L falls steeply over the range: for a large negative t S, L' is about
|
|
821
|
+
* t S δ / 2 at w = 0, and its steps of 1e-14 were taken for convergence
|
|
822
|
+
*/
|
|
823
|
+
function bracketedPeak(pr) {
|
|
824
|
+
let lo = -1400;
|
|
825
|
+
let hi = 1418;
|
|
826
|
+
for (let i = 0; i < 200 && hi - lo > 1e-12 * Math.max(1, Math.abs(lo)); i++) {
|
|
827
|
+
const m = (lo + hi) / 2;
|
|
828
|
+
const d1 = logIntegrand(pr, m)[1];
|
|
829
|
+
// (a d1 that is not formed lies beyond the double range on that side)
|
|
830
|
+
if (d1 > 0 || (Number.isNaN(d1) && m < 0))
|
|
831
|
+
lo = m;
|
|
832
|
+
else
|
|
833
|
+
hi = m;
|
|
834
|
+
}
|
|
835
|
+
const w = (lo + hi) / 2;
|
|
836
|
+
let [L, , d2] = logIntegrand(pr, w);
|
|
837
|
+
if (Number.isNaN(d2)) {
|
|
838
|
+
// (L'' from the differences of L')
|
|
839
|
+
const h = 1e-4;
|
|
840
|
+
d2 = (logIntegrand(pr, w + h)[1] - logIntegrand(pr, w - h)[1]) / (2 * h);
|
|
841
|
+
}
|
|
842
|
+
return { w, sigma: d2 < 0 ? Math.min(1 / Math.sqrt(-d2), 10) : 1, L };
|
|
843
|
+
}
|
|
498
844
|
function logAddExp(x, y) {
|
|
499
845
|
if (x === -Infinity)
|
|
500
846
|
return y;
|
|
@@ -503,24 +849,26 @@ function logAddExp(x, y) {
|
|
|
503
849
|
return x > y ? x + Math.log1p(Math.exp(y - x)) : y + Math.log1p(Math.exp(x - y));
|
|
504
850
|
}
|
|
505
851
|
/** Linear value and log of the integral (the log may be NaN unless needLog) */
|
|
506
|
-
function integrateCore(pr, needLog, peak) {
|
|
852
|
+
function integrateCore(pr, needLog, peak, ddOffset = false) {
|
|
507
853
|
const { w, sigma, L } = peak ?? findPeak(pr);
|
|
508
854
|
// With a peak value this small the linear answer underflows to 0 (even after multiplying by the width)
|
|
509
855
|
if (!needLog && L < -800)
|
|
510
856
|
return { lin: 0, log: NaN };
|
|
511
857
|
// If summing linearly would overflow or underflow, subtract the peak value L before summing
|
|
512
|
-
const useLog = L < -650 || L > 650;
|
|
513
|
-
|
|
858
|
+
const useLog = L < -650 || L > 650 || pr.shift !== undefined || ddOffset;
|
|
859
|
+
// (with ddOffset, the log of the peak in double-double: subtracted from the nodes' in double-double)
|
|
860
|
+
const off = ddOffset ? integrand(pr, [w, 0], true) : [useLog ? L : 0, 0];
|
|
861
|
+
const offset = off[0];
|
|
514
862
|
// Peak value (approximate) used by the fast-path test
|
|
515
863
|
const peakVal = useLog ? 0 : Math.exp(L);
|
|
516
864
|
// Convergence test: halving the step roughly squares the error, so if the difference is below this the finer
|
|
517
865
|
// value is accurate enough. In deep tails (summed on the log scale) the node values themselves carry an error
|
|
518
866
|
// of about eps |L|, so stop there
|
|
519
|
-
const tol = useLog ? Math.max(1e-15, 2e-16 * Math.abs(L)) : 1e-15;
|
|
867
|
+
const tol = useLog && !ddOffset ? Math.max(1e-15, 2e-16 * Math.abs(L)) : 1e-15;
|
|
520
868
|
let h = 0.5;
|
|
521
|
-
let s = trapezoid(pr, w, sigma, h, useLog, offset, false, peakVal) * h;
|
|
869
|
+
let s = trapezoid(pr, w, sigma, h, useLog, offset, false, peakVal, ddOffset ? off[1] : undefined) * h;
|
|
522
870
|
for (let level = 0; level < 6; level++) {
|
|
523
|
-
const odd = trapezoid(pr, w, sigma, h / 2, useLog, offset, true, peakVal) * (h / 2);
|
|
871
|
+
const odd = trapezoid(pr, w, sigma, h / 2, useLog, offset, true, peakVal, ddOffset ? off[1] : undefined) * (h / 2);
|
|
524
872
|
const s2 = s / 2 + odd;
|
|
525
873
|
h /= 2;
|
|
526
874
|
const conv = Math.abs(s2 - s) <= tol * Math.abs(s2);
|
|
@@ -530,6 +878,15 @@ function integrateCore(pr, needLog, peak) {
|
|
|
530
878
|
}
|
|
531
879
|
if (!useLog)
|
|
532
880
|
return { lin: s, log: Math.log(s) };
|
|
881
|
+
if (pr.shift !== undefined) {
|
|
882
|
+
// (only beyond LOG_LARGE, where the linear value is 0; with complement, E|ρ - 1| itself, without the shift)
|
|
883
|
+
const log = (pr.complement ? 0 : pr.shift) + offset + Math.log(s);
|
|
884
|
+
return { lin: log < -745 ? 0 : Math.exp(log), log };
|
|
885
|
+
}
|
|
886
|
+
if (ddOffset) {
|
|
887
|
+
const log = offset + (off[1] + Math.log(s));
|
|
888
|
+
return { lin: log < -745 ? 0 : Math.exp(log), log };
|
|
889
|
+
}
|
|
533
890
|
// exp(offset) s: take exp of offset first and multiply (exp(offset + log s) would have an error |offset| times larger)
|
|
534
891
|
const lin = offset > -700 ? Math.exp(offset) * s : Math.exp(offset + 700) * s * Math.exp(-700);
|
|
535
892
|
return { lin, log: offset + Math.log(s) };
|
|
@@ -585,7 +942,16 @@ function farTails(t, a, delta, lower) {
|
|
|
585
942
|
function nearZero(t, a, delta) {
|
|
586
943
|
// (|t|/sqrt(a) first, and sqrt(y1) as a hypot: y1/a overflowed for a below about 1e-300 with a large δ, and at t = 0
|
|
587
944
|
// the test was NaN, false, so that the integral was taken at t = 0 and gave NaN to qt)
|
|
588
|
-
|
|
945
|
+
const r = (Math.abs(t) / Math.sqrt(a)) * Math.hypot(Math.SQRT2 * delta, Math.sqrt(4 * a + 800)) * (Math.abs(delta) + 2);
|
|
946
|
+
if (Number.isFinite(r))
|
|
947
|
+
return r <= 1e-17;
|
|
948
|
+
// (a factor overflowed, or |t|/sqrt(a) underflowed against it, at δ near the overflow threshold: by logs)
|
|
949
|
+
const l = Math.log(Math.abs(t)) -
|
|
950
|
+
0.5 * Math.log(a) +
|
|
951
|
+
0.5 * Math.LN2 +
|
|
952
|
+
Math.log(Math.hypot(delta, Math.sqrt(2 * a + 400))) +
|
|
953
|
+
Math.log(Math.abs(delta) + 2);
|
|
954
|
+
return l <= Math.log(1e-17);
|
|
589
955
|
}
|
|
590
956
|
/** Linear value and log of P[T <= t] (lower) / P[T > t] (the log may be NaN unless needLog) */
|
|
591
957
|
function pntBoth(t, df, delta, lower, needLog = true) {
|
|
@@ -750,9 +1116,10 @@ export function qnt1(p, df0, ncp, lowerTail, logP) {
|
|
|
750
1116
|
return b;
|
|
751
1117
|
if (df === Infinity)
|
|
752
1118
|
return ncp + qnorm(p, { lowerTail, logP });
|
|
753
|
-
// ncp = ±Inf
|
|
1119
|
+
// ncp = ±Inf puts all the probability at ±∞ (pt is 0 or 1 at every finite t, in R too), so every quantile inside
|
|
1120
|
+
// (0, 1) is ±∞, as already for df = Inf above (R gives NaN for a finite df)
|
|
754
1121
|
if (!Number.isFinite(ncp))
|
|
755
|
-
return
|
|
1122
|
+
return ncp;
|
|
756
1123
|
const { lower, logTarget, linearTarget } = smallerTail(p, lowerTail, logP);
|
|
757
1124
|
if (df > HUGE_DF) {
|
|
758
1125
|
// The quantiles of the limits of pnt1
|
|
@@ -786,6 +1153,14 @@ export function qnt1(p, df0, ncp, lowerTail, logP) {
|
|
|
786
1153
|
}
|
|
787
1154
|
return log ? last.log : last.lin;
|
|
788
1155
|
};
|
|
1156
|
+
// Where the normal start is beyond 1e300 or not finite (ncp near the largest double, or a huge ncp^2 / df), the
|
|
1157
|
+
// quantile can lie beyond the largest double for any df: if the target is not reached there, it is ±Infinity. (The
|
|
1158
|
+
// solver broke down outside the doubles and returned 1 for qt(0.9, 2, ncp = 1e308); issue #136)
|
|
1159
|
+
if (!(Math.abs(x0) <= 1e300)) {
|
|
1160
|
+
const lMax = cdfY(Number.MAX_VALUE, lowerY, true);
|
|
1161
|
+
if (lowerY ? lMax < logTarget : lMax > logTarget)
|
|
1162
|
+
return s * Infinity;
|
|
1163
|
+
}
|
|
789
1164
|
// In the far tail, if the target is smaller than the probability at |t| = T0, solve directly with the power law
|
|
790
1165
|
const T0 = farLimit(df / 2, ncp);
|
|
791
1166
|
let hi = Infinity;
|
|
@@ -793,7 +1168,7 @@ export function qnt1(p, df0, ncp, lowerTail, logP) {
|
|
|
793
1168
|
const lT0 = farTailLog(s * T0, df / 2, ncp);
|
|
794
1169
|
if (logTarget < lT0) {
|
|
795
1170
|
const ly = Math.log(T0) + (lT0 - logTarget) / df;
|
|
796
|
-
return
|
|
1171
|
+
return s * Math.exp(ly); // (Infinity where the quantile is beyond the largest double)
|
|
797
1172
|
}
|
|
798
1173
|
hi = T0;
|
|
799
1174
|
}
|
|
@@ -77,9 +77,12 @@ export function dpareto1(x, a, b, log) {
|
|
|
77
77
|
export function ppareto1(q, a, b, lowerTail, logP) {
|
|
78
78
|
if (Number.isNaN(q) || !valid(a, b))
|
|
79
79
|
return NaN;
|
|
80
|
+
// a = Infinity: the point mass at b, counted at q = b (as qpareto gives b and pnorm(0, 0, 0) 1)
|
|
81
|
+
if (a === Infinity)
|
|
82
|
+
return q < b ? dt0(lowerTail, logP) : dt1(lowerTail, logP);
|
|
80
83
|
if (q <= b)
|
|
81
84
|
return dt0(lowerTail, logP);
|
|
82
|
-
if (q === Infinity
|
|
85
|
+
if (q === Infinity)
|
|
83
86
|
return dt1(lowerTail, logP);
|
|
84
87
|
const rt = ratio(q, b);
|
|
85
88
|
const lu = -a * rt[2]; // log P[X > q]
|
|
@@ -7,8 +7,9 @@ import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
|
7
7
|
import { discreteQuantile } from "../internal/discrete.js";
|
|
8
8
|
import { gammaInc } from "../special/igam.js";
|
|
9
9
|
import { dpoisRaw } from "../special/loader.js";
|
|
10
|
+
import { logAddExp, logSubExp } from "../internal/ddexp.js";
|
|
10
11
|
import { qnorm } from "./normal.js";
|
|
11
|
-
import { nonInt } from "../internal/integer.js";
|
|
12
|
+
import { countAtMost, nonInt } from "../internal/integer.js";
|
|
12
13
|
/** Whether x is not an integer (relative tolerance 1e-9 as in R; see src/internal/integer.ts) */
|
|
13
14
|
export const nonInteger = nonInt;
|
|
14
15
|
export function dpois1(x, lambda, log) {
|
|
@@ -33,7 +34,30 @@ export function ppois1(q, lambda, lowerTail, logP) {
|
|
|
33
34
|
return dt1(lowerTail, logP);
|
|
34
35
|
if (!Number.isFinite(q))
|
|
35
36
|
return dt1(lowerTail, logP);
|
|
36
|
-
const x =
|
|
37
|
+
const x = countAtMost(q);
|
|
38
|
+
if (x + 1 - x === 1)
|
|
39
|
+
return gammaInc(x + 1, lambda, !lowerTail, logP);
|
|
40
|
+
// Beyond 2^53, x + 1 rounds (to x, or up to x + 2) and Q(x + 1, lambda) became Q(x, lambda): ppois(1e16, 1e16) was 0.4999999987, below
|
|
41
|
+
// 1/2 (R: 0.5000000027), and qpois(0.5, 1e16) 2 above the median 1e16. Without x + 1:
|
|
42
|
+
// P[X <= x] = Q(x, lambda) + dpois(x, lambda), P[X > x] = P(x, lambda) - dpois(x, lambda)
|
|
43
|
+
let v;
|
|
44
|
+
let term;
|
|
45
|
+
if (!logP) {
|
|
46
|
+
const t = gammaInc(x, lambda, !lowerTail, false);
|
|
47
|
+
term = dpoisRaw(x, lambda, false);
|
|
48
|
+
v = lowerTail ? t + term : t - term;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const lt = gammaInc(x, lambda, !lowerTail, true);
|
|
52
|
+
term = dpoisRaw(x, lambda, true);
|
|
53
|
+
v = lowerTail ? logAddExp(lt, term) : logSubExp(lt, term);
|
|
54
|
+
}
|
|
55
|
+
if (Number.isNaN(term))
|
|
56
|
+
return NaN;
|
|
57
|
+
// The upper tail as a difference: where dpois is not resolved against it (deep tails, where both logs round to the
|
|
58
|
+
// same value), one unit of x is below the resolution of the result, and the rounded x + 1 serves (round 55)
|
|
59
|
+
if (lowerTail || (logP ? Number.isFinite(v) : v > 0))
|
|
60
|
+
return v;
|
|
37
61
|
return gammaInc(x + 1, lambda, !lowerTail, logP);
|
|
38
62
|
}
|
|
39
63
|
export function qpois1(p, lambda, lowerTail, logP) {
|
|
@@ -54,7 +78,8 @@ export function qpois1(p, lambda, lowerTail, logP) {
|
|
|
54
78
|
p,
|
|
55
79
|
lowerTail,
|
|
56
80
|
logP,
|
|
57
|
-
|
|
81
|
+
// (through ppois1, which also holds beyond 2^53 where y + 1 rounds to y)
|
|
82
|
+
cdf: (y, l, lg) => ppois1(y, lambda, l, lg),
|
|
58
83
|
y0: Number.isFinite(y0) ? y0 : lambda,
|
|
59
84
|
lo: 0,
|
|
60
85
|
hi: Infinity,
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { vectorize } from "../internal/vectorize.js";
|
|
9
9
|
import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
|
|
10
10
|
import { discreteQuantile } from "../internal/discrete.js";
|
|
11
|
-
import { nonInt, roundHalfEven } from "../internal/integer.js";
|
|
11
|
+
import { countAtMost, nonInt, roundHalfEven } from "../internal/integer.js";
|
|
12
12
|
import { qnorm } from "./normal.js";
|
|
13
13
|
/** Threshold at which counts are multiplied by 2^-SCALE_STEP to keep them from growing too large */
|
|
14
14
|
const SCALE_LIMIT = 2 ** 900;
|
|
@@ -152,7 +152,9 @@ function signrankN(n) {
|
|
|
152
152
|
if (!Number.isFinite(n))
|
|
153
153
|
return NaN;
|
|
154
154
|
n = roundHalfEven(n);
|
|
155
|
-
|
|
155
|
+
// The statistic ranges over 0..n(n + 1)/2: beyond 2^53 its values are not all doubles (NaN, as wilcox), and the
|
|
156
|
+
// rounded n(n + 1)/2 was taken as the end of the support. n(n + 1)/2 <= 2^53 exactly when n < 2^27
|
|
157
|
+
return n <= 0 || n >= 2 ** 27 ? NaN : n;
|
|
156
158
|
}
|
|
157
159
|
export function dsignrank1(x, n, log) {
|
|
158
160
|
if (Number.isNaN(x) || Number.isNaN(n))
|
|
@@ -193,8 +195,9 @@ export function psignrank1(q, n, lowerTail, logP) {
|
|
|
193
195
|
n = signrankN(n);
|
|
194
196
|
if (Number.isNaN(n))
|
|
195
197
|
return NaN;
|
|
196
|
-
// R rounds q + 1e-7 to the nearest integer (psignrank(3.6, 5) is P[V <= 4])
|
|
197
|
-
|
|
198
|
+
// R rounds q + 1e-7 to the nearest integer (psignrank(3.6, 5) is P[V <= 4], although V <= 3.6 is V <= 3). The
|
|
199
|
+
// largest count not above q, as the other discrete distribution functions (#145, #146)
|
|
200
|
+
const x = countAtMost(q);
|
|
198
201
|
const total = (n * (n + 1)) / 2;
|
|
199
202
|
if (x < 0)
|
|
200
203
|
return dt0(lowerTail, logP);
|