@numero-uno-jpn/rstats 0.0.0-stage → 0.5.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.
Files changed (159) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +21 -0
  3. package/README.ja.md +181 -0
  4. package/README.md +172 -2
  5. package/THIRD_PARTY_NOTICES.md +297 -0
  6. package/dist/distributions/beta.d.ts +62 -0
  7. package/dist/distributions/beta.js +246 -0
  8. package/dist/distributions/binomial.d.ts +41 -0
  9. package/dist/distributions/binomial.js +136 -0
  10. package/dist/distributions/cauchy.d.ts +34 -0
  11. package/dist/distributions/cauchy.js +139 -0
  12. package/dist/distributions/chisq.d.ts +42 -0
  13. package/dist/distributions/chisq.js +60 -0
  14. package/dist/distributions/exponential.d.ts +34 -0
  15. package/dist/distributions/exponential.js +93 -0
  16. package/dist/distributions/f.d.ts +47 -0
  17. package/dist/distributions/f.js +214 -0
  18. package/dist/distributions/gamma.d.ts +61 -0
  19. package/dist/distributions/gamma.js +188 -0
  20. package/dist/distributions/geometric.d.ts +38 -0
  21. package/dist/distributions/geometric.js +116 -0
  22. package/dist/distributions/hypergeometric.d.ts +38 -0
  23. package/dist/distributions/hypergeometric.js +234 -0
  24. package/dist/distributions/logistic.d.ts +36 -0
  25. package/dist/distributions/logistic.js +94 -0
  26. package/dist/distributions/lognormal.d.ts +39 -0
  27. package/dist/distributions/lognormal.js +158 -0
  28. package/dist/distributions/nbeta.d.ts +7 -0
  29. package/dist/distributions/nbeta.js +312 -0
  30. package/dist/distributions/nchisq.d.ts +3 -0
  31. package/dist/distributions/nchisq.js +217 -0
  32. package/dist/distributions/negbinomial.d.ts +54 -0
  33. package/dist/distributions/negbinomial.js +325 -0
  34. package/dist/distributions/normal.d.ts +66 -0
  35. package/dist/distributions/normal.js +184 -0
  36. package/dist/distributions/nt.d.ts +3 -0
  37. package/dist/distributions/nt.js +491 -0
  38. package/dist/distributions/poisson.d.ts +41 -0
  39. package/dist/distributions/poisson.js +104 -0
  40. package/dist/distributions/signrank.d.ts +38 -0
  41. package/dist/distributions/signrank.js +216 -0
  42. package/dist/distributions/t.d.ts +49 -0
  43. package/dist/distributions/t.js +235 -0
  44. package/dist/distributions/tukey.d.ts +43 -0
  45. package/dist/distributions/tukey.js +985 -0
  46. package/dist/distributions/uniform.d.ts +36 -0
  47. package/dist/distributions/uniform.js +90 -0
  48. package/dist/distributions/weibull.d.ts +37 -0
  49. package/dist/distributions/weibull.js +125 -0
  50. package/dist/distributions/wilcox.d.ts +47 -0
  51. package/dist/distributions/wilcox.js +270 -0
  52. package/dist/htest/chisqTest.d.ts +55 -0
  53. package/dist/htest/chisqTest.js +160 -0
  54. package/dist/htest/cor.d.ts +81 -0
  55. package/dist/htest/cor.js +436 -0
  56. package/dist/htest/fisherTest.d.ts +48 -0
  57. package/dist/htest/fisherTest.js +468 -0
  58. package/dist/htest/pAdjust.d.ts +34 -0
  59. package/dist/htest/pAdjust.js +147 -0
  60. package/dist/htest/tTest.d.ts +57 -0
  61. package/dist/htest/tTest.js +153 -0
  62. package/dist/htest/types.d.ts +49 -0
  63. package/dist/htest/types.js +20 -0
  64. package/dist/htest/wilcoxTest.d.ts +61 -0
  65. package/dist/htest/wilcoxTest.js +471 -0
  66. package/dist/index.d.ts +50 -0
  67. package/dist/index.js +50 -0
  68. package/dist/internal/ddexp.d.ts +19 -0
  69. package/dist/internal/ddexp.js +94 -0
  70. package/dist/internal/discrete.d.ts +14 -0
  71. package/dist/internal/discrete.js +85 -0
  72. package/dist/internal/dpq.d.ts +43 -0
  73. package/dist/internal/dpq.js +54 -0
  74. package/dist/internal/integer.d.ts +4 -0
  75. package/dist/internal/integer.js +17 -0
  76. package/dist/internal/invert.d.ts +36 -0
  77. package/dist/internal/invert.js +124 -0
  78. package/dist/internal/noncentral.d.ts +12 -0
  79. package/dist/internal/noncentral.js +2 -0
  80. package/dist/internal/poissonMixture.d.ts +31 -0
  81. package/dist/internal/poissonMixture.js +401 -0
  82. package/dist/internal/summary.d.ts +21 -0
  83. package/dist/internal/summary.js +125 -0
  84. package/dist/internal/uniroot.d.ts +11 -0
  85. package/dist/internal/uniroot.js +75 -0
  86. package/dist/internal/vectorize.d.ts +12 -0
  87. package/dist/internal/vectorize.js +7 -0
  88. package/dist/linalg/basic.d.ts +95 -0
  89. package/dist/linalg/basic.js +152 -0
  90. package/dist/linalg/chol.d.ts +83 -0
  91. package/dist/linalg/chol.js +200 -0
  92. package/dist/linalg/eigen.d.ts +52 -0
  93. package/dist/linalg/eigen.js +753 -0
  94. package/dist/linalg/kappa.d.ts +51 -0
  95. package/dist/linalg/kappa.js +199 -0
  96. package/dist/linalg/matrix.d.ts +29 -0
  97. package/dist/linalg/matrix.js +65 -0
  98. package/dist/linalg/norm.d.ts +32 -0
  99. package/dist/linalg/norm.js +92 -0
  100. package/dist/linalg/qr.d.ts +264 -0
  101. package/dist/linalg/qr.js +494 -0
  102. package/dist/linalg/solve.d.ts +123 -0
  103. package/dist/linalg/solve.js +380 -0
  104. package/dist/linalg/svd.d.ts +38 -0
  105. package/dist/linalg/svd.js +275 -0
  106. package/dist/random/as241.d.ts +2 -0
  107. package/dist/random/as241.js +58 -0
  108. package/dist/random/beta.d.ts +14 -0
  109. package/dist/random/beta.js +117 -0
  110. package/dist/random/binomial.d.ts +25 -0
  111. package/dist/random/binomial.js +224 -0
  112. package/dist/random/common.d.ts +4 -0
  113. package/dist/random/common.js +35 -0
  114. package/dist/random/continuous.d.ts +91 -0
  115. package/dist/random/continuous.js +138 -0
  116. package/dist/random/gamma.d.ts +52 -0
  117. package/dist/random/gamma.js +217 -0
  118. package/dist/random/hyper.d.ts +12 -0
  119. package/dist/random/hyper.js +240 -0
  120. package/dist/random/mt.d.ts +16 -0
  121. package/dist/random/mt.js +97 -0
  122. package/dist/random/poisson.d.ts +33 -0
  123. package/dist/random/poisson.js +204 -0
  124. package/dist/random/rank.d.ts +19 -0
  125. package/dist/random/rank.js +62 -0
  126. package/dist/random/rng.d.ts +46 -0
  127. package/dist/random/rng.js +148 -0
  128. package/dist/random/sample.d.ts +45 -0
  129. package/dist/random/sample.js +255 -0
  130. package/dist/rstats.js +2 -0
  131. package/dist/special/beta.d.ts +30 -0
  132. package/dist/special/beta.js +152 -0
  133. package/dist/special/constants.d.ts +16 -0
  134. package/dist/special/constants.js +19 -0
  135. package/dist/special/dd.d.ts +29 -0
  136. package/dist/special/dd.js +693 -0
  137. package/dist/special/erf.d.ts +9 -0
  138. package/dist/special/erf.js +85 -0
  139. package/dist/special/gamma.d.ts +32 -0
  140. package/dist/special/gamma.js +270 -0
  141. package/dist/special/igam.d.ts +11 -0
  142. package/dist/special/igam.js +291 -0
  143. package/dist/special/igamAsympCoeff.d.ts +4 -0
  144. package/dist/special/igamAsympCoeff.js +233 -0
  145. package/dist/special/igami.d.ts +2 -0
  146. package/dist/special/igami.js +135 -0
  147. package/dist/special/incbet.d.ts +25 -0
  148. package/dist/special/incbet.js +326 -0
  149. package/dist/special/lgammaDiff.d.ts +2 -0
  150. package/dist/special/lgammaDiff.js +34 -0
  151. package/dist/special/loader.d.ts +43 -0
  152. package/dist/special/loader.js +456 -0
  153. package/dist/special/log1pmx.d.ts +2 -0
  154. package/dist/special/log1pmx.js +20 -0
  155. package/dist/special/ndtri.d.ts +4 -0
  156. package/dist/special/ndtri.js +82 -0
  157. package/dist/special/polevl.d.ts +4 -0
  158. package/dist/special/polevl.js +17 -0
  159. package/package.json +68 -4
@@ -0,0 +1,136 @@
1
+ // Binomial distribution dbinom / pbinom / qbinom.
2
+ //
3
+ // Probabilities use Loader's (2000) dbinomRaw; the distribution function is
4
+ // P[X <= x] = 1 - I_p(x + 1, n - x) (regularized incomplete beta function)
5
+ // Quantiles use an integer search from a Cornish-Fisher starting value.
6
+ import { vectorize } from "../internal/vectorize.js";
7
+ import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
8
+ import { discreteQuantile } from "../internal/discrete.js";
9
+ import { betaInc } from "../special/incbet.js";
10
+ import { dbinomRawDD } from "../special/loader.js";
11
+ import { twoSum } from "../special/dd.js";
12
+ import { qnorm } from "./normal.js";
13
+ import { nonInteger } from "./poisson.js";
14
+ function badParams(size, prob) {
15
+ return nonInteger(size) || size < 0 || !Number.isFinite(size) || prob < 0 || prob > 1;
16
+ }
17
+ export function dbinom1(x, size, prob, log) {
18
+ if (Number.isNaN(x) || Number.isNaN(size) || Number.isNaN(prob))
19
+ return NaN;
20
+ if (badParams(size, prob))
21
+ return NaN;
22
+ if (nonInteger(x) || x < 0 || !Number.isFinite(x))
23
+ return d0(log);
24
+ // q = 1 - prob is formed exactly in double-double (rounding in q would be amplified n - x times)
25
+ const xr = Math.round(x);
26
+ const n = Math.round(size);
27
+ if (xr > n)
28
+ return d0(log);
29
+ const pd = prob <= 0.5 ? [prob, 0] : twoSum(1, -(1 - prob));
30
+ const qd = prob <= 0.5 ? twoSum(1, -prob) : [1 - prob, 0];
31
+ return dbinomRawDD(xr, n - xr, pd, qd, log);
32
+ }
33
+ /** P[X <= x] (lower) / P[X > x] (!lower). x an integer, 0 <= x < n */
34
+ function pbinomRaw(x, n, prob, lower, log) {
35
+ // P[X <= x] = 1 - I_p(x + 1, n - x)
36
+ return betaInc(x + 1, n - x, prob, 1 - prob, !lower, log);
37
+ }
38
+ export function pbinom1(q, size, prob, lowerTail, logP) {
39
+ if (Number.isNaN(q) || Number.isNaN(size) || Number.isNaN(prob))
40
+ return NaN;
41
+ if (badParams(size, prob))
42
+ return NaN;
43
+ const n = Math.round(size);
44
+ if (q < 0)
45
+ return dt0(lowerTail, logP);
46
+ const x = Math.floor(q + 1e-7);
47
+ if (n <= x)
48
+ return dt1(lowerTail, logP);
49
+ if (prob === 0)
50
+ return dt1(lowerTail, logP);
51
+ if (prob === 1)
52
+ return dt0(lowerTail, logP);
53
+ return pbinomRaw(x, n, prob, lowerTail, logP);
54
+ }
55
+ export function qbinom1(p, size, prob, lowerTail, logP) {
56
+ if (Number.isNaN(p) || Number.isNaN(size) || Number.isNaN(prob))
57
+ return NaN;
58
+ if (!Number.isFinite(size) || !Number.isFinite(prob) || prob < 0 || prob > 1 || size < 0)
59
+ return NaN;
60
+ // R returns the endpoints (p = 0, 1) first even for non-integer size. The upper end is size rounded half to even
61
+ const b = quantileBoundaries(p, 0, roundHalfEven(size), lowerTail, logP);
62
+ if (b !== undefined)
63
+ return b;
64
+ if (badParams(size, prob))
65
+ return NaN;
66
+ const n = Math.round(size);
67
+ if (prob === 0 || n === 0)
68
+ return 0;
69
+ if (prob === 1)
70
+ return n;
71
+ const q = 1 - prob;
72
+ const mu = n * prob;
73
+ const sigma = Math.sqrt(n * prob * q);
74
+ const gamma = (q - prob) / sigma;
75
+ const z = qnorm(p, { lowerTail, logP });
76
+ const y0 = mu + sigma * (z + (gamma * (z * z - 1)) / 6);
77
+ return discreteQuantile({
78
+ p,
79
+ lowerTail,
80
+ logP,
81
+ cdf: (y, l, lg) => (y >= n ? dt1(l, lg) : pbinomRaw(y, n, prob, l, lg)),
82
+ y0: Number.isFinite(y0) ? y0 : mu,
83
+ lo: 0,
84
+ hi: n,
85
+ });
86
+ }
87
+ /**
88
+ * Probability mass function of the binomial distribution. Equivalent to R's `dbinom(x, size, prob, log = FALSE)`.
89
+ *
90
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
91
+ * @param size - Number of trials.
92
+ * @param prob - Probability of success on each trial.
93
+ * @param options - `log` (see {@link DensityOptions}).
94
+ * @returns The probability (or log probability).
95
+ */
96
+ export function dbinom(x, size, prob, options = {}) {
97
+ const log = options.log ?? false;
98
+ return vectorize(x, (v) => dbinom1(v, size, prob, log));
99
+ }
100
+ /**
101
+ * Distribution function of the binomial distribution. Equivalent to R's `pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE)`.
102
+ *
103
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
104
+ * @param size - Number of trials.
105
+ * @param prob - Probability of success on each trial.
106
+ * @param options - `lowerTail` and `logP` (see {@link TailOptions}).
107
+ * @returns The probability (or log probability).
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * pbinom(3, 10, 0.5); // 0.171875
112
+ * pbinom([3, 5], 10, 0.5); // [0.171875, 0.623046875]
113
+ * ```
114
+ */
115
+ export function pbinom(q, size, prob, options = {}) {
116
+ const { lowerTail = true, logP = false } = options;
117
+ return vectorize(q, (v) => pbinom1(v, size, prob, lowerTail, logP));
118
+ }
119
+ /**
120
+ * Quantile function of the binomial distribution. Equivalent to R's `qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE)`.
121
+ *
122
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
123
+ * @param size - Number of trials.
124
+ * @param prob - Probability of success on each trial.
125
+ * @param options - `lowerTail` and `logP` (see {@link TailOptions}).
126
+ * @returns The quantile(s).
127
+ */
128
+ export function qbinom(p, size, prob, options = {}) {
129
+ const { lowerTail = true, logP = false } = options;
130
+ return vectorize(p, (v) => qbinom1(v, size, prob, lowerTail, logP));
131
+ }
132
+ /** Round half to even (0.5 goes to the nearest even number; same as R's nearbyint) */
133
+ function roundHalfEven(x) {
134
+ const r = Math.round(x);
135
+ return Math.abs(x % 1) === 0.5 && r % 2 !== 0 ? r - 1 : r;
136
+ }
@@ -0,0 +1,34 @@
1
+ import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
2
+ import { type DensityOptions, type TailOptions } from "../internal/dpq.js";
3
+ import type { LocationScaleParams } from "./logistic.js";
4
+ export declare function dcauchy1(x: number, location: number, scale: number, log: boolean): number;
5
+ /**
6
+ * P[X <= q] (lower) / P[X > q] (!lower). For |z| > 1 the smaller tail atan(1/|z|)/π is computed without forming z,
7
+ * using 1/|z| = scale / |q - location| (so the log value survives even if z overflows)
8
+ */
9
+ export declare function pcauchy1(q: number, location: number, scale: number, lowerTail: boolean, logP: boolean): number;
10
+ export declare function qcauchy1(p: number, location: number, scale: number, lowerTail: boolean, logP: boolean): number;
11
+ /**
12
+ * Density of the Cauchy distribution. Equivalent to R's `dcauchy(x, location = 0, scale = 1, log = FALSE)`.
13
+ *
14
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
15
+ * @param options - `location` and `scale` (see {@link LocationScaleParams}) and `log` (see {@link DensityOptions}).
16
+ * @returns The density (or log density).
17
+ */
18
+ export declare function dcauchy<T extends Vectorizable>(x: T, options?: LocationScaleParams & DensityOptions): VectorResult<T>;
19
+ /**
20
+ * Distribution function of the Cauchy distribution. Equivalent to R's `pcauchy(q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE)`.
21
+ *
22
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
23
+ * @param options - `location` and `scale` (see {@link LocationScaleParams}), `lowerTail` and `logP` (see {@link TailOptions}).
24
+ * @returns The probability (or log probability).
25
+ */
26
+ export declare function pcauchy<T extends Vectorizable>(q: T, options?: LocationScaleParams & TailOptions): VectorResult<T>;
27
+ /**
28
+ * Quantile function of the Cauchy distribution. Equivalent to R's `qcauchy(p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE)`.
29
+ *
30
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
31
+ * @param options - `location` and `scale` (see {@link LocationScaleParams}), `lowerTail` and `logP` (see {@link TailOptions}).
32
+ * @returns The quantile(s).
33
+ */
34
+ export declare function qcauchy<T extends Vectorizable>(p: T, options?: LocationScaleParams & TailOptions): VectorResult<T>;
@@ -0,0 +1,139 @@
1
+ // Cauchy distribution dcauchy / pcauchy / qcauchy.
2
+ //
3
+ // With z = (x - location) / scale, f(x) = 1 / (π scale (1 + z^2)) and
4
+ // P[X <= x] = 1/2 + atan(z)/π. For |z| > 1 the smaller tail is computed directly as atan(1/|z|)/π,
5
+ // avoiding subtraction from 1/2 and overflow of z^2. Quantiles compute tan(π (p - 1/2)) as
6
+ // -1/tan(π p) for p < 1/4 and -tan(π (1/2 - p)) for 1/4 <= p <= 1/2.
7
+ import { vectorize } from "../internal/vectorize.js";
8
+ import { locationScale } from "../internal/ddexp.js";
9
+ const LOG_PI = 1.1447298858494002;
10
+ export function dcauchy1(x, location, scale, log) {
11
+ if (Number.isNaN(x) || Number.isNaN(location) || Number.isNaN(scale))
12
+ return NaN;
13
+ if (!(scale > 0))
14
+ return NaN;
15
+ const z = (x - location) / scale;
16
+ if (Number.isNaN(z))
17
+ return NaN;
18
+ const az = Math.abs(z);
19
+ if (az <= 1) {
20
+ if (log)
21
+ return -(LOG_PI + Math.log(scale)) - Math.log1p(z * z);
22
+ return 1 / (Math.PI * scale * (1 + z * z));
23
+ }
24
+ // With y = 1/z, f = y^2 / (π scale (1 + y^2))
25
+ const y = 1 / az;
26
+ if (log)
27
+ return -(LOG_PI + Math.log(scale)) - 2 * Math.log(az) - Math.log1p(y * y);
28
+ return (y / (Math.PI * scale)) * (y / (1 + y * y));
29
+ }
30
+ /**
31
+ * P[X <= q] (lower) / P[X > q] (!lower). For |z| > 1 the smaller tail atan(1/|z|)/π is computed without forming z,
32
+ * using 1/|z| = scale / |q - location| (so the log value survives even if z overflows)
33
+ */
34
+ export function pcauchy1(q, location, scale, lowerTail, logP) {
35
+ if (Number.isNaN(q) || Number.isNaN(location) || Number.isNaN(scale))
36
+ return NaN;
37
+ if (!(scale > 0))
38
+ return NaN;
39
+ const d = q - location;
40
+ if (Number.isNaN(d))
41
+ return NaN;
42
+ if (!(Math.abs(d) > scale)) {
43
+ const z = d / scale;
44
+ const v = 0.5 + Math.atan(lowerTail ? z : -z) / Math.PI;
45
+ return logP ? Math.log(v) : v;
46
+ }
47
+ // Probability of the smaller side v = atan(w)/π (w = 1/|z|)
48
+ const w = scale / Math.abs(d);
49
+ const v = Math.atan(w) / Math.PI;
50
+ const small = lowerTail === d < 0;
51
+ if (small) {
52
+ if (!logP)
53
+ return v;
54
+ // atan(w) = w (1 - w^2/3 + ...). For small w use log w directly (survives even if w underflows)
55
+ return w < 1e-150 ? Math.log(scale) - Math.log(Math.abs(d)) - LOG_PI : Math.log(v);
56
+ }
57
+ return logP ? Math.log1p(-v) : 1 - v;
58
+ }
59
+ const TINY = 1e-200;
60
+ const TINY_LOG = -460;
61
+ /**
62
+ * scale |T| = scale / tan(π s) ≈ scale / (π s) when the probability on that side is tiny.
63
+ * When π s is subnormal, 1 / (π s) overflows and loses accuracy, so change the order of division.
64
+ * With logP (s is a log), multiply by e^-s split into two halves (taking log and exp back would amplify the rounding of the exponent)
65
+ */
66
+ function tinyTail(scale, s, logP) {
67
+ if (!logP)
68
+ return scale / s / Math.PI;
69
+ const h = Math.exp(-s / 2);
70
+ return h * (scale / Math.PI) * h;
71
+ }
72
+ /** Quantile tan(π (s - 1/2)) of the standard Cauchy for lower probability s (0 < s <= 1/2) */
73
+ function quantileSmall(s) {
74
+ if (s <= 0.25)
75
+ return -1 / Math.tan(Math.PI * s);
76
+ return -Math.tan(Math.PI * (0.5 - s));
77
+ }
78
+ export function qcauchy1(p, location, scale, lowerTail, logP) {
79
+ if (Number.isNaN(p) || Number.isNaN(location) || Number.isNaN(scale))
80
+ return NaN;
81
+ if (logP ? p > 0 : p < 0 || p > 1)
82
+ return NaN;
83
+ if (!(scale > 0) || !Number.isFinite(scale))
84
+ return scale === 0 ? location : NaN;
85
+ // Standard quantile T for the probability p on that side (x = location + scale T for the lower tail)
86
+ let t;
87
+ if (logP) {
88
+ if (p === 0)
89
+ t = Infinity;
90
+ else if (p === -Infinity)
91
+ t = -Infinity;
92
+ else if (p < TINY_LOG)
93
+ return location + (lowerTail ? -1 : 1) * tinyTail(scale, p, true);
94
+ else
95
+ t = p < -Math.LN2 ? quantileSmall(Math.exp(p)) : -quantileSmall(-Math.expm1(p));
96
+ }
97
+ else if (p === 0)
98
+ t = -Infinity;
99
+ else if (p === 1)
100
+ t = Infinity;
101
+ else if (p < TINY)
102
+ return location + (lowerTail ? -1 : 1) * tinyTail(scale, p, false);
103
+ else
104
+ t = p <= 0.5 ? quantileSmall(p) : -quantileSmall(1 - p);
105
+ return locationScale(location, scale, lowerTail ? t : -t);
106
+ }
107
+ /**
108
+ * Density of the Cauchy distribution. Equivalent to R's `dcauchy(x, location = 0, scale = 1, log = FALSE)`.
109
+ *
110
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
111
+ * @param options - `location` and `scale` (see {@link LocationScaleParams}) and `log` (see {@link DensityOptions}).
112
+ * @returns The density (or log density).
113
+ */
114
+ export function dcauchy(x, options = {}) {
115
+ const { location = 0, scale = 1, log = false } = options;
116
+ return vectorize(x, (v) => dcauchy1(v, location, scale, log));
117
+ }
118
+ /**
119
+ * Distribution function of the Cauchy distribution. Equivalent to R's `pcauchy(q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE)`.
120
+ *
121
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
122
+ * @param options - `location` and `scale` (see {@link LocationScaleParams}), `lowerTail` and `logP` (see {@link TailOptions}).
123
+ * @returns The probability (or log probability).
124
+ */
125
+ export function pcauchy(q, options = {}) {
126
+ const { location = 0, scale = 1, lowerTail = true, logP = false } = options;
127
+ return vectorize(q, (v) => pcauchy1(v, location, scale, lowerTail, logP));
128
+ }
129
+ /**
130
+ * Quantile function of the Cauchy distribution. Equivalent to R's `qcauchy(p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE)`.
131
+ *
132
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
133
+ * @param options - `location` and `scale` (see {@link LocationScaleParams}), `lowerTail` and `logP` (see {@link TailOptions}).
134
+ * @returns The quantile(s).
135
+ */
136
+ export function qcauchy(p, options = {}) {
137
+ const { location = 0, scale = 1, lowerTail = true, logP = false } = options;
138
+ return vectorize(p, (v) => qcauchy1(v, location, scale, lowerTail, logP));
139
+ }
@@ -0,0 +1,42 @@
1
+ import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
2
+ import type { DensityOptions, TailOptions } from "../internal/dpq.js";
3
+ import type { NoncentralOption } from "../internal/noncentral.js";
4
+ /**
5
+ * Density of the chi-squared distribution. Equivalent to R's `dchisq(x, df, ncp = 0, log = FALSE)`.
6
+ *
7
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral chi-squared distribution.
8
+ *
9
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
10
+ * @param df - Degrees of freedom.
11
+ * @param options - `ncp` (see {@link NoncentralOption}) and `log` (see {@link DensityOptions}).
12
+ * @returns The density (or log density).
13
+ */
14
+ export declare function dchisq<T extends Vectorizable>(x: T, df: number, options?: DensityOptions & NoncentralOption): VectorResult<T>;
15
+ /**
16
+ * Distribution function of the chi-squared distribution. Equivalent to R's `pchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)`.
17
+ *
18
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral chi-squared distribution.
19
+ *
20
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
21
+ * @param df - Degrees of freedom.
22
+ * @param options - `ncp` (see {@link NoncentralOption}), `lowerTail` and `logP` (see {@link TailOptions}).
23
+ * @returns The probability (or log probability).
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * pchisq(3.84, 1, { lowerTail: false }); // 0.05004352124870517
28
+ * pchisq(5, 3, { ncp: 2 }); // 0.5934051800831556
29
+ * ```
30
+ */
31
+ export declare function pchisq<T extends Vectorizable>(q: T, df: number, options?: TailOptions & NoncentralOption): VectorResult<T>;
32
+ /**
33
+ * Quantile function of the chi-squared distribution. Equivalent to R's `qchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)`.
34
+ *
35
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral chi-squared distribution.
36
+ *
37
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
38
+ * @param df - Degrees of freedom.
39
+ * @param options - `ncp` (see {@link NoncentralOption}), `lowerTail` and `logP` (see {@link TailOptions}).
40
+ * @returns The quantile(s).
41
+ */
42
+ export declare function qchisq<T extends Vectorizable>(p: T, df: number, options?: TailOptions & NoncentralOption): VectorResult<T>;
@@ -0,0 +1,60 @@
1
+ // χ² distribution dchisq / pchisq / qchisq, computed as the gamma distribution with shape = df/2, scale = 2.
2
+ // Passing ncp gives the noncentral χ² (src/distributions/nchisq.ts). As in R, when ncp is omitted the
3
+ // central distribution is used.
4
+ import { vectorize } from "../internal/vectorize.js";
5
+ import { dgamma1, pgamma1, qgamma1 } from "./gamma.js";
6
+ import { dnchisq1, pnchisq1, qnchisq1 } from "./nchisq.js";
7
+ /**
8
+ * Density of the chi-squared distribution. Equivalent to R's `dchisq(x, df, ncp = 0, log = FALSE)`.
9
+ *
10
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral chi-squared distribution.
11
+ *
12
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
13
+ * @param df - Degrees of freedom.
14
+ * @param options - `ncp` (see {@link NoncentralOption}) and `log` (see {@link DensityOptions}).
15
+ * @returns The density (or log density).
16
+ */
17
+ export function dchisq(x, df, options = {}) {
18
+ const { log = false, ncp } = options;
19
+ if (ncp !== undefined)
20
+ return vectorize(x, (v) => dnchisq1(v, df, ncp, log));
21
+ return vectorize(x, (v) => dgamma1(v, df / 2, 2, log));
22
+ }
23
+ /**
24
+ * Distribution function of the chi-squared distribution. Equivalent to R's `pchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)`.
25
+ *
26
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral chi-squared distribution.
27
+ *
28
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
29
+ * @param df - Degrees of freedom.
30
+ * @param options - `ncp` (see {@link NoncentralOption}), `lowerTail` and `logP` (see {@link TailOptions}).
31
+ * @returns The probability (or log probability).
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * pchisq(3.84, 1, { lowerTail: false }); // 0.05004352124870517
36
+ * pchisq(5, 3, { ncp: 2 }); // 0.5934051800831556
37
+ * ```
38
+ */
39
+ export function pchisq(q, df, options = {}) {
40
+ const { lowerTail = true, logP = false, ncp } = options;
41
+ if (ncp !== undefined)
42
+ return vectorize(q, (v) => pnchisq1(v, df, ncp, lowerTail, logP));
43
+ return vectorize(q, (v) => pgamma1(v, df / 2, 2, lowerTail, logP));
44
+ }
45
+ /**
46
+ * Quantile function of the chi-squared distribution. Equivalent to R's `qchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)`.
47
+ *
48
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral chi-squared distribution.
49
+ *
50
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
51
+ * @param df - Degrees of freedom.
52
+ * @param options - `ncp` (see {@link NoncentralOption}), `lowerTail` and `logP` (see {@link TailOptions}).
53
+ * @returns The quantile(s).
54
+ */
55
+ export function qchisq(p, df, options = {}) {
56
+ const { lowerTail = true, logP = false, ncp } = options;
57
+ if (ncp !== undefined)
58
+ return vectorize(p, (v) => qnchisq1(v, df, ncp, lowerTail, logP));
59
+ return vectorize(p, (v) => qgamma1(v, df / 2, 2, lowerTail, logP));
60
+ }
@@ -0,0 +1,34 @@
1
+ import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
2
+ import { type DensityOptions, type TailOptions } from "../internal/dpq.js";
3
+ /** Parameters of the exponential distribution. */
4
+ export interface ExponentialParams {
5
+ /** Rate. Default `1`. */
6
+ rate?: number;
7
+ }
8
+ export declare function dexp1(x: number, rate: number, log: boolean): number;
9
+ export declare function pexp1(q: number, rate: number, lowerTail: boolean, logP: boolean): number;
10
+ export declare function qexp1(p: number, rate: number, lowerTail: boolean, logP: boolean): number;
11
+ /**
12
+ * Density of the exponential distribution. Equivalent to R's `dexp(x, rate = 1, log = FALSE)`.
13
+ *
14
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
15
+ * @param options - `rate` (see {@link ExponentialParams}) and `log` (see {@link DensityOptions}).
16
+ * @returns The density (or log density).
17
+ */
18
+ export declare function dexp<T extends Vectorizable>(x: T, options?: ExponentialParams & DensityOptions): VectorResult<T>;
19
+ /**
20
+ * Distribution function of the exponential distribution. Equivalent to R's `pexp(q, rate = 1, lower.tail = TRUE, log.p = FALSE)`.
21
+ *
22
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
23
+ * @param options - `rate` (see {@link ExponentialParams}), `lowerTail` and `logP` (see {@link TailOptions}).
24
+ * @returns The probability (or log probability).
25
+ */
26
+ export declare function pexp<T extends Vectorizable>(q: T, options?: ExponentialParams & TailOptions): VectorResult<T>;
27
+ /**
28
+ * Quantile function of the exponential distribution. Equivalent to R's `qexp(p, rate = 1, lower.tail = TRUE, log.p = FALSE)`.
29
+ *
30
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
31
+ * @param options - `rate` (see {@link ExponentialParams}), `lowerTail` and `logP` (see {@link TailOptions}).
32
+ * @returns The quantile(s).
33
+ */
34
+ export declare function qexp<T extends Vectorizable>(p: T, options?: ExponentialParams & TailOptions): VectorResult<T>;
@@ -0,0 +1,93 @@
1
+ // Exponential distribution dexp / pexp / qexp.
2
+ //
3
+ // f(x) = rate exp(-rate x), P[X > x] = exp(-rate x). rate x is formed in double-double
4
+ // (the relative error of exp(-t) equals the absolute error of t, and at t = 700 the rounding of the product alone gives 6e-14).
5
+ // Argument handling (rate = 0, Inf) is matched to R's output.
6
+ import { vectorize } from "../internal/vectorize.js";
7
+ import { d0, dt0, dt1, quantileBoundaries } from "../internal/dpq.js";
8
+ import { expNeg, log1mExp, logOneMinusExpNeg, mulDD } from "../internal/ddexp.js";
9
+ export function dexp1(x, rate, log) {
10
+ if (Number.isNaN(x) || Number.isNaN(rate))
11
+ return NaN;
12
+ if (rate < 0 || rate === Infinity)
13
+ return NaN;
14
+ if (x < 0)
15
+ return d0(log);
16
+ const t = mulDD(rate, x);
17
+ if (Number.isNaN(t[0]))
18
+ return NaN;
19
+ if (log)
20
+ return Math.log(rate) - t[0] - t[1];
21
+ return rate * expNeg(t);
22
+ }
23
+ /** Log or linear value of P[X > x], according to the tail direction */
24
+ function expTail(t, lowerTail, logP) {
25
+ if (!lowerTail)
26
+ return logP ? -t[0] - t[1] : expNeg(t);
27
+ // Lower tail 1 - exp(-t)
28
+ const s = t[0] + t[1];
29
+ return logP ? logOneMinusExpNeg(t) : -Math.expm1(-s);
30
+ }
31
+ export function pexp1(q, rate, lowerTail, logP) {
32
+ if (Number.isNaN(q) || Number.isNaN(rate))
33
+ return NaN;
34
+ if (rate < 0)
35
+ return NaN;
36
+ if (q <= 0)
37
+ return dt0(lowerTail, logP);
38
+ const t = mulDD(rate, q);
39
+ if (Number.isNaN(t[0]))
40
+ return NaN;
41
+ if (t[0] === Infinity)
42
+ return dt1(lowerTail, logP);
43
+ return expTail(t, lowerTail, logP);
44
+ }
45
+ export function qexp1(p, rate, lowerTail, logP) {
46
+ if (Number.isNaN(p) || Number.isNaN(rate))
47
+ return NaN;
48
+ if (rate < 0)
49
+ return NaN;
50
+ const b = quantileBoundaries(p, 0, Infinity, lowerTail, logP);
51
+ if (Number.isNaN(b) || b === 0)
52
+ return b;
53
+ // Log of the upper probability, log P[X > x] = -rate x
54
+ let lu;
55
+ if (logP)
56
+ lu = lowerTail ? log1mExp(p) : p;
57
+ else
58
+ lu = lowerTail ? Math.log1p(-p) : Math.log(p);
59
+ return -lu / rate;
60
+ }
61
+ /**
62
+ * Density of the exponential distribution. Equivalent to R's `dexp(x, rate = 1, log = FALSE)`.
63
+ *
64
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
65
+ * @param options - `rate` (see {@link ExponentialParams}) and `log` (see {@link DensityOptions}).
66
+ * @returns The density (or log density).
67
+ */
68
+ export function dexp(x, options = {}) {
69
+ const { rate = 1, log = false } = options;
70
+ return vectorize(x, (v) => dexp1(v, rate, log));
71
+ }
72
+ /**
73
+ * Distribution function of the exponential distribution. Equivalent to R's `pexp(q, rate = 1, lower.tail = TRUE, log.p = FALSE)`.
74
+ *
75
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
76
+ * @param options - `rate` (see {@link ExponentialParams}), `lowerTail` and `logP` (see {@link TailOptions}).
77
+ * @returns The probability (or log probability).
78
+ */
79
+ export function pexp(q, options = {}) {
80
+ const { rate = 1, lowerTail = true, logP = false } = options;
81
+ return vectorize(q, (v) => pexp1(v, rate, lowerTail, logP));
82
+ }
83
+ /**
84
+ * Quantile function of the exponential distribution. Equivalent to R's `qexp(p, rate = 1, lower.tail = TRUE, log.p = FALSE)`.
85
+ *
86
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
87
+ * @param options - `rate` (see {@link ExponentialParams}), `lowerTail` and `logP` (see {@link TailOptions}).
88
+ * @returns The quantile(s).
89
+ */
90
+ export function qexp(p, options = {}) {
91
+ const { rate = 1, lowerTail = true, logP = false } = options;
92
+ return vectorize(p, (v) => qexp1(v, rate, lowerTail, logP));
93
+ }
@@ -0,0 +1,47 @@
1
+ import { type Vectorizable, type VectorResult } from "../internal/vectorize.js";
2
+ import { type DensityOptions, type TailOptions } from "../internal/dpq.js";
3
+ import type { NoncentralOption } from "../internal/noncentral.js";
4
+ export declare function dfDensity1(x: number, m: number, n: number, log: boolean): number;
5
+ export declare function pf1(q: number, m: number, n: number, lowerTail: boolean, logP: boolean): number;
6
+ export declare function qf1(p: number, m: number, n: number, lowerTail: boolean, logP: boolean): number;
7
+ /**
8
+ * Density of the F distribution. Equivalent to R's `df(x, df1, df2, ncp, log = FALSE)`.
9
+ *
10
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral F distribution.
11
+ *
12
+ * @param x - Value(s) at which to evaluate: a number, `number[]` or `Float64Array`. The result has the same shape.
13
+ * @param df1 - Numerator degrees of freedom.
14
+ * @param df2 - Denominator degrees of freedom.
15
+ * @param options - `ncp` (see {@link NoncentralOption}) and `log` (see {@link DensityOptions}).
16
+ * @returns The density (or log density).
17
+ */
18
+ export declare function df<T extends Vectorizable>(x: T, df1: number, df2: number, options?: DensityOptions & NoncentralOption): VectorResult<T>;
19
+ /**
20
+ * Distribution function of the F distribution. Equivalent to R's `pf(q, df1, df2, ncp, lower.tail = TRUE, log.p = FALSE)`.
21
+ *
22
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral F distribution.
23
+ *
24
+ * @param q - Quantile(s): a number, `number[]` or `Float64Array`. The result has the same shape.
25
+ * @param df1 - Numerator degrees of freedom.
26
+ * @param df2 - Denominator degrees of freedom.
27
+ * @param options - `ncp` (see {@link NoncentralOption}), `lowerTail` and `logP` (see {@link TailOptions}).
28
+ * @returns The probability (or log probability).
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * pf(4.1, 2, 20, { lowerTail: false }); // 0.03219651558603902
33
+ * ```
34
+ */
35
+ export declare function pf<T extends Vectorizable>(q: T, df1: number, df2: number, options?: TailOptions & NoncentralOption): VectorResult<T>;
36
+ /**
37
+ * Quantile function of the F distribution. Equivalent to R's `qf(p, df1, df2, ncp, lower.tail = TRUE, log.p = FALSE)`.
38
+ *
39
+ * If `ncp` is omitted the central distribution is used; otherwise the noncentral F distribution.
40
+ *
41
+ * @param p - Probability (or log probability with `logP`): a number, `number[]` or `Float64Array`. The result has the same shape.
42
+ * @param df1 - Numerator degrees of freedom.
43
+ * @param df2 - Denominator degrees of freedom.
44
+ * @param options - `ncp` (see {@link NoncentralOption}), `lowerTail` and `logP` (see {@link TailOptions}).
45
+ * @returns The quantile(s).
46
+ */
47
+ export declare function qf<T extends Vectorizable>(p: T, df1: number, df2: number, options?: TailOptions & NoncentralOption): VectorResult<T>;