@numero-uno-jpn/rstats 0.15.1 → 0.15.3

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.ja.md +8 -8
  3. package/README.md +8 -8
  4. package/dist/chunks/{chunk-735NNFAS.js → chunk-3THFTB36.js} +2 -2
  5. package/dist/chunks/chunk-3TSWYE5L.js +2 -0
  6. package/dist/chunks/{chunk-7J4HW6VF.js → chunk-3WPU6LGK.js} +2 -2
  7. package/dist/chunks/{chunk-Z3C3G5XI.js → chunk-5ITR67MB.js} +2 -2
  8. package/dist/chunks/chunk-5NRFU6F6.js +2 -0
  9. package/dist/chunks/{chunk-SAZAEPZC.js → chunk-E2LCSVQV.js} +1 -1
  10. package/dist/chunks/chunk-FCNVTIPJ.js +2 -0
  11. package/dist/chunks/chunk-FKAKALOZ.js +2 -0
  12. package/dist/chunks/{chunk-5OSQ76OO.js → chunk-HD73UKPT.js} +2 -2
  13. package/dist/chunks/{chunk-4ILI2FSB.js → chunk-IUULP3LE.js} +1 -1
  14. package/dist/chunks/{chunk-LY2T6DA6.js → chunk-JQG3YQOC.js} +2 -2
  15. package/dist/chunks/chunk-KT66SB7R.js +2 -0
  16. package/dist/chunks/{chunk-IQOVOHZG.js → chunk-MAUAOQG3.js} +2 -2
  17. package/dist/chunks/{chunk-GEJCBCMP.js → chunk-MDYE7QP2.js} +2 -2
  18. package/dist/chunks/{chunk-5SQFDLNG.js → chunk-NM3Z2UBQ.js} +2 -2
  19. package/dist/chunks/{chunk-5XQZNKHZ.js → chunk-PMJI5QO6.js} +2 -2
  20. package/dist/chunks/chunk-UF7KOTHF.js +3 -0
  21. package/dist/chunks/chunk-UOE57JGT.js +2 -0
  22. package/dist/chunks/{chunk-3QTVXDUM.js → chunk-USPEBXSR.js} +2 -2
  23. package/dist/chunks/{chunk-H4CHJMFU.js → chunk-VLUL2HFW.js} +1 -1
  24. package/dist/chunks/{chunk-VOIJKNL2.js → chunk-ZIX7NYQF.js} +1 -1
  25. package/dist/descriptive.js +2 -2
  26. package/dist/distributions/arcsine.js +48 -4
  27. package/dist/distributions/beta.d.ts +1 -1
  28. package/dist/distributions/beta.js +75 -2
  29. package/dist/distributions/cauchy.js +76 -5
  30. package/dist/distributions/exponential.js +16 -2
  31. package/dist/distributions/f.js +143 -24
  32. package/dist/distributions/gamma.d.ts +13 -2
  33. package/dist/distributions/gamma.js +122 -12
  34. package/dist/distributions/geometric.js +19 -3
  35. package/dist/distributions/invgamma.js +60 -10
  36. package/dist/distributions/kumaraswamy.js +116 -17
  37. package/dist/distributions/laplace.js +5 -2
  38. package/dist/distributions/logistic.js +8 -2
  39. package/dist/distributions/lognormal.js +40 -5
  40. package/dist/distributions/nbeta.js +597 -52
  41. package/dist/distributions/nchisq.d.ts +25 -0
  42. package/dist/distributions/nchisq.js +381 -96
  43. package/dist/distributions/negbinomial.js +26 -6
  44. package/dist/distributions/normal.js +29 -1
  45. package/dist/distributions/nt.d.ts +28 -3
  46. package/dist/distributions/nt.js +401 -63
  47. package/dist/distributions/pareto.js +73 -19
  48. package/dist/distributions/t.js +102 -12
  49. package/dist/distributions/triangular.js +93 -27
  50. package/dist/distributions/tukey.js +110 -50
  51. package/dist/distributions/uniform.js +14 -9
  52. package/dist/distributions/weibull.js +65 -10
  53. package/dist/distributions.js +2 -2
  54. package/dist/internal/ddexp.d.ts +13 -0
  55. package/dist/internal/ddexp.js +55 -3
  56. package/dist/internal/discrete.js +30 -7
  57. package/dist/internal/invert.d.ts +5 -0
  58. package/dist/internal/invert.js +59 -5
  59. package/dist/internal/noncentral.d.ts +41 -0
  60. package/dist/internal/noncentral.js +124 -2
  61. package/dist/internal/poissonMixture.d.ts +7 -2
  62. package/dist/internal/poissonMixture.js +142 -38
  63. package/dist/linalg.js +2 -2
  64. package/dist/mixed.js +2 -2
  65. package/dist/numerics.js +2 -2
  66. package/dist/rstats.js +14 -14
  67. package/dist/special/beta.js +7 -0
  68. package/dist/special/dd.d.ts +1 -1
  69. package/dist/special/dd.js +4 -1
  70. package/dist/special/gamma.js +4 -0
  71. package/dist/special/igam.js +126 -21
  72. package/dist/special/incbet.d.ts +10 -0
  73. package/dist/special/incbet.js +218 -48
  74. package/dist/special/incbetLargeA.js +53 -14
  75. package/dist/special/incbetPrefactor.d.ts +57 -0
  76. package/dist/special/incbetPrefactor.js +103 -0
  77. package/dist/special/incbetTemme.js +15 -3
  78. package/dist/special/loader.js +97 -22
  79. package/dist/special.js +2 -2
  80. package/dist/tests.js +2 -2
  81. package/package.json +1 -1
  82. package/dist/chunks/chunk-5YMP4WAJ.js +0 -2
  83. package/dist/chunks/chunk-BDJPSEA7.js +0 -2
  84. package/dist/chunks/chunk-KO6QQPC6.js +0 -2
  85. package/dist/chunks/chunk-OLPTFKIZ.js +0 -3
  86. package/dist/chunks/chunk-TYFGVCVQ.js +0 -2
  87. package/dist/chunks/chunk-UWBYDIOH.js +0 -2
  88. package/dist/chunks/chunk-UZHY5DWN.js +0 -2
@@ -12,6 +12,7 @@ import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
12
12
  import { discreteQuantile } from "../internal/discrete.js";
13
13
  import { nonInt } from "../internal/integer.js";
14
14
  import { betaIncDD } from "../special/incbet.js";
15
+ import { prefactorParts } from "../special/incbetPrefactor.js";
15
16
  import { dbinomRawDD, dpoisRaw } from "../special/loader.js";
16
17
  import { ddAdd, ddDiv, ddMul, twoSum } from "../special/dd.js";
17
18
  import { qnorm } from "./normal.js";
@@ -32,8 +33,13 @@ function dnbinomRaw(x, par, log) {
32
33
  if (x === 0)
33
34
  return b;
34
35
  const r = size / (size + x);
35
- return log ? b + Math.log(r) : b * r;
36
+ if (r >= MIN_NORMAL)
37
+ return log ? b + Math.log(r) : b * r;
38
+ // A subnormal size / (size + x) has lost digits (dnbinom(58, 3.94e-315, { prob: 0.125, log: true }) was 7.5e-12 off,
39
+ // 2.4e-8 at x = 57): log size - log(size + x), and the division by size + x before the product with size
40
+ return log ? b + (Math.log(size) - Math.log(size + x)) : (b / (size + x)) * size;
36
41
  }
42
+ const MIN_NORMAL = 2 ** -1022;
37
43
  /** P[X <= x] (lower) / P[X > x] (!lower) (x a nonnegative integer) */
38
44
  function pnbinomRaw(x, par, lower, log) {
39
45
  const up = upperSeries(x, par);
@@ -51,7 +57,8 @@ function pnbinomRaw(x, par, lower, log) {
51
57
  * I_z(A, B) = z^A (1 - z)^B / (A B(A, B)) [1 + Σ_{k>=1} Π_{j<k} (A + B + j) / (A + 1 + j) z^k]
52
58
  * (z = q, A = x + 1, B = size). In this region the incomplete beta continued fraction is off by about 3e-14
53
59
  * relative near the mean. All terms are positive, so there is no cancellation. The number of terms is about
54
- * -log(1e-17) / -log z. Returns undefined when not usable (too many terms, or the value exceeds 1/2)
60
+ * -log(1e-18 p) / -log z (at most about 5e5 for p >= 1e-4). Returns undefined when not usable (too many terms, or the
61
+ * value exceeds 1/2)
55
62
  */
56
63
  function upperSeries(x, par) {
57
64
  const { size, pd, qd } = par;
@@ -61,8 +68,13 @@ function upperSeries(x, par) {
61
68
  return undefined;
62
69
  const A = x + 1;
63
70
  const B = size;
64
- // Prefactor z^A (1 - z)^B / (A B(A, B)) = b(A; A + B, z) B / (A + B)
65
- const pre = (dbinomRawDD(A, B, qd, pd, false) * B) / (A + B);
71
+ // Prefactor z^A (1 - z)^B / (A B(A, B)) = b(A; A + B, z) B / (A + B), or for B < 1 from the small-shape form of
72
+ // incbet.ts (incbetPrefactor.ts): Loader's form loses about ε |log B| / 2 there (5e-14 at size 5.6e-245:
73
+ // pnbinom(1545, 5.6e-245, { mu: 4.6e-241, lowerTail: false }))
74
+ const parts = prefactorParts(A, B, qd, pd);
75
+ const pre = parts !== undefined
76
+ ? (parts[0] * (Math.exp(parts[1][0]) * (1 + parts[1][1]))) / A
77
+ : (dbinomRawDD(A, B, qd, pd, false) * B) / (A + B);
66
78
  if (!(pre > 1e-290))
67
79
  return undefined;
68
80
  // Rounding of the term ratios accumulates with the number of terms (1e-12 over tens of thousands), so keep terms and sum in double-double
@@ -73,7 +85,9 @@ function upperSeries(x, par) {
73
85
  const ratio = ddDiv(ddAdd(ab, [k, 0]), [A + 1 + k, 0]);
74
86
  term = ddMul(ddMul(term, ratio), qd);
75
87
  sum = ddAdd(sum, term);
76
- if (term[0] < 1e-18 * sum[0]) {
88
+ // The term ratios rise towards z, so the rest of the series is below term z/(1 - z) < term/p: stopping at
89
+ // term < 1e-18 sum alone left 8e-15 of it out at p = 1.2e-4 (pnbinom(1545, 5.6e-245, { mu: 4.6e-241, lowerTail: false }))
90
+ if (term[0] < 1e-18 * sum[0] * p) {
77
91
  const v = pre * (sum[0] + sum[1]);
78
92
  return v <= 0.5 ? v : undefined;
79
93
  }
@@ -93,7 +107,12 @@ function quantileRaw(p, par, lowerTail, logP) {
93
107
  // For small p, X (-log q) is nearly gamma (shape = size). Even for highly skewed distributions (small size)
94
108
  // this is a much closer starting value than Cornish-Fisher, reducing distribution-function evaluations
95
109
  // -log q = log(1 + p/q) (taking log of a rounded q loses digits for small p)
96
- y0 = qgamma1(p, par.size, 1, lowerTail, logP) / Math.log1p(prob / (par.qd[0] + par.qd[1]));
110
+ const g = qgamma1(p, par.size, 1, lowerTail, logP);
111
+ y0 = g / Math.log1p(prob / (par.qd[0] + par.qd[1]));
112
+ // A finite gamma quantile divided by a tiny -log q beyond the doubles: so is the quantile (the distribution
113
+ // function cannot be evaluated out there; qnbinom(2.2e-284, 2.7e37, { prob: 1.9e-279 }) threw a TypeError)
114
+ if (y0 === Infinity && g > 0 && Number.isFinite(g))
115
+ return Infinity;
97
116
  }
98
117
  else {
99
118
  const z = qnorm(p, { lowerTail, logP });
@@ -214,6 +233,7 @@ export function dnbinomMu1(x, size, mu, log) {
214
233
  if (size === 0) {
215
234
  if (n === 0)
216
235
  return log ? 0 : 1;
236
+ // NaN at mu = 0 to match R (dnbinom(1, 0, mu = 0) is NaN there), although size = 0 and mu = 0 is the point mass at 0
217
237
  return mu === 0 ? NaN : d0(log);
218
238
  }
219
239
  if (mu === Infinity)
@@ -16,6 +16,9 @@ const SQRT1_2 = Math.SQRT1_2;
16
16
  * Where the result is subnormal, rounding happens only in the final multiplication.
17
17
  */
18
18
  function scaledExpNegHalfSq(a, scale) {
19
+ // exp(-a^2 / 2) underflows for any finite scale (and a * 16 would overflow near the largest double)
20
+ if (a > 1e10)
21
+ return 0;
19
22
  const hi = Math.floor(a * 16) / 16;
20
23
  const lo = a - hi;
21
24
  const halfHiSq = (hi * hi) / 2;
@@ -121,7 +124,32 @@ function dnorm1(x, mean, sd, log) {
121
124
  if (log)
122
125
  return -(LN_SQRT_2PI + 0.5 * z * z + Math.log(sd));
123
126
  // Rounding of z^2 is amplified by the exponential in exp(-z^2/2), so split it as in pnorm
124
- return scaledExpNegHalfSq(Math.abs(z), INV_SQRT_2PI / sd);
127
+ const c = INV_SQRT_2PI / sd;
128
+ return c < Infinity ? scaledExpNegHalfSq(Math.abs(z), c) : tinySdDensity(Math.abs(z), sd);
129
+ }
130
+ /**
131
+ * exp(-a^2/2) / (sd sqrt(2π)) for an sd so far below the normal range that 1 / (sd sqrt(2π)) overflows (it was NaN, as
132
+ * Infinity times exp(-a^2/2) = 0: dnorm(4.85e-319, { sd: 1.7e-322 })), with the split of scaledExpNegHalfSq: sd is
133
+ * scaled by 2^600 (exactly) and the factor 2^600 put back where neither the partial product overflows nor its last
134
+ * factor exp(-700) makes it subnormal
135
+ */
136
+ function tinySdDensity(a, sd) {
137
+ if (a > 1e10)
138
+ return 0;
139
+ const hi = Math.floor(a * 16) / 16;
140
+ const lo = a - hi;
141
+ const halfHiSq = (hi * hi) / 2;
142
+ const m = (INV_SQRT_2PI / (sd * 2 ** 600)) * Math.exp((-lo * (a + hi)) / 2);
143
+ if (halfHiSq <= 700)
144
+ return m * Math.exp(-halfHiSq) * 2 ** 600;
145
+ if (halfHiSq > 1400) {
146
+ // (exp(700 - a^2/2) is itself subnormal from a^2/2 = 1408 on, where the density can still be normal: dnorm(-1.59e-321,
147
+ // { sd: 3e-323 }) was 3e-4 off. m exp(1400 - a^2/2) is above e^276 wherever the density is normal, so each partial
148
+ // product below stays normal until the last factor)
149
+ return m * Math.exp(1400 - halfHiSq) * Math.exp(-700) * 2 ** 600 * Math.exp(-700);
150
+ }
151
+ const t = m * Math.exp(700 - halfHiSq);
152
+ return t >= 1 ? t * Math.exp(-700) * 2 ** 600 : t * 2 ** 600 * Math.exp(-700);
125
153
  }
126
154
  /**
127
155
  * Density of the normal distribution. Equivalent to R's `dnorm(x, mean = 0, sd = 1, log = FALSE)`.
@@ -1,3 +1,28 @@
1
- export declare function dnt1(x: number, df: number, ncp: number, log: boolean): number;
2
- export declare function pnt1(q: number, df: number, ncp: number, lowerTail: boolean, logP: boolean): number;
3
- export declare function qnt1(p: number, df: number, ncp: number, lowerTail: boolean, logP: boolean): number;
1
+ /**
2
+ * Above this df the central and noncentral t are taken from their limits (hugeDf*; shared with t.ts). The t
3
+ * differs from the normal by terms of relative order t²/ν and 1/ν: where y = t²/ν < 1e-17 it is the normal to the
4
+ * double (the absolute error in the log, about t⁴/ν, is below 1e-180 wherever the linear value is above the
5
+ * underflow), and beyond, |t| > 3e91, the log of the far tail and of the density is -(ν/2) log1p(y) up to terms
6
+ * below 1e-179 of it. The general code fails from about 1.3e300 on (NaN where its intermediate terms overflow)
7
+ */
8
+ export declare const HUGE_DF = 1e+200;
9
+ /** y = t²/ν (t² may overflow on its own) */
10
+ export declare function hugeDfRatio(t: number, n: number): number;
11
+ /**
12
+ * Log of the far tail beyond |t| and of the density at t (relative error about eps) for n > HUGE_DF and
13
+ * hugeDfRatio(t, n) >= 1e-17: -(n/2) log1p(t²/n), formed without overflow
14
+ */
15
+ export declare function hugeDfTailLog(t: number, n: number): number;
16
+ /**
17
+ * |t| with hugeDfTailLog(t, n) = logTarget (logTarget < 0), Infinity beyond the largest double: t² = n expm1(-2 logTarget / n).
18
+ * (sqrt(n) is taken apart: through exp(log t) its log, up to 354, would multiply the rounding error)
19
+ */
20
+ export declare function hugeDfTailQuantile(logTarget: number, n: number): number;
21
+ /**
22
+ * P[T <= t] (lower) / P[T > t] for n > HUGE_DF and hugeDfRatio(t, n) >= 1e-17 from hugeDfTailLog: the tail beyond |t|
23
+ * is its exp (0), the other is 1
24
+ */
25
+ export declare function hugeDfTail(t: number, n: number, lowerTail: boolean, logP: boolean): number;
26
+ export declare function dnt1(x: number, df0: number, ncp: number, log: boolean): number;
27
+ export declare function pnt1(q: number, df0: number, ncp: number, lowerTail: boolean, logP: boolean): number;
28
+ export declare function qnt1(p: number, df0: number, ncp: number, lowerTail: boolean, logP: boolean): number;