@ac-kit/math-stats 0.1.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 (156) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/LICENSE +28 -0
  3. package/README.md +91 -0
  4. package/dist/_assert-probability.d.ts +14 -0
  5. package/dist/_assert-probability.d.ts.map +1 -0
  6. package/dist/_assert-probability.js +18 -0
  7. package/dist/_assert-probability.js.map +1 -0
  8. package/dist/_discrete-quantile.d.ts +16 -0
  9. package/dist/_discrete-quantile.d.ts.map +1 -0
  10. package/dist/_discrete-quantile.js +43 -0
  11. package/dist/_discrete-quantile.js.map +1 -0
  12. package/dist/_inverse-transform-sample.d.ts +17 -0
  13. package/dist/_inverse-transform-sample.d.ts.map +1 -0
  14. package/dist/_inverse-transform-sample.js +18 -0
  15. package/dist/_inverse-transform-sample.js.map +1 -0
  16. package/dist/_log-binomial.d.ts +16 -0
  17. package/dist/_log-binomial.d.ts.map +1 -0
  18. package/dist/_log-binomial.js +19 -0
  19. package/dist/_log-binomial.js.map +1 -0
  20. package/dist/average.d.ts +95 -0
  21. package/dist/average.d.ts.map +1 -0
  22. package/dist/average.js +113 -0
  23. package/dist/average.js.map +1 -0
  24. package/dist/beta-distribution.d.ts +15 -0
  25. package/dist/beta-distribution.d.ts.map +1 -0
  26. package/dist/beta-distribution.js +61 -0
  27. package/dist/beta-distribution.js.map +1 -0
  28. package/dist/binomial-distribution.d.ts +18 -0
  29. package/dist/binomial-distribution.d.ts.map +1 -0
  30. package/dist/binomial-distribution.js +74 -0
  31. package/dist/binomial-distribution.js.map +1 -0
  32. package/dist/chi-squared-distribution.d.ts +15 -0
  33. package/dist/chi-squared-distribution.d.ts.map +1 -0
  34. package/dist/chi-squared-distribution.js +25 -0
  35. package/dist/chi-squared-distribution.js.map +1 -0
  36. package/dist/chi-squared-test.d.ts +39 -0
  37. package/dist/chi-squared-test.d.ts.map +1 -0
  38. package/dist/chi-squared-test.js +56 -0
  39. package/dist/chi-squared-test.js.map +1 -0
  40. package/dist/confidence-interval.d.ts +62 -0
  41. package/dist/confidence-interval.d.ts.map +1 -0
  42. package/dist/confidence-interval.js +93 -0
  43. package/dist/confidence-interval.js.map +1 -0
  44. package/dist/dispersion.d.ts +45 -0
  45. package/dist/dispersion.d.ts.map +1 -0
  46. package/dist/dispersion.js +80 -0
  47. package/dist/dispersion.js.map +1 -0
  48. package/dist/distribution.d.ts +61 -0
  49. package/dist/distribution.d.ts.map +1 -0
  50. package/dist/distribution.js +2 -0
  51. package/dist/distribution.js.map +1 -0
  52. package/dist/exponential-distribution.d.ts +15 -0
  53. package/dist/exponential-distribution.d.ts.map +1 -0
  54. package/dist/exponential-distribution.js +33 -0
  55. package/dist/exponential-distribution.js.map +1 -0
  56. package/dist/f-distribution.d.ts +17 -0
  57. package/dist/f-distribution.d.ts.map +1 -0
  58. package/dist/f-distribution.js +88 -0
  59. package/dist/f-distribution.js.map +1 -0
  60. package/dist/gamma-distribution.d.ts +15 -0
  61. package/dist/gamma-distribution.d.ts.map +1 -0
  62. package/dist/gamma-distribution.js +45 -0
  63. package/dist/gamma-distribution.js.map +1 -0
  64. package/dist/geometric-distribution.d.ts +19 -0
  65. package/dist/geometric-distribution.d.ts.map +1 -0
  66. package/dist/geometric-distribution.js +54 -0
  67. package/dist/geometric-distribution.js.map +1 -0
  68. package/dist/hierarchical-cluster.d.ts +35 -0
  69. package/dist/hierarchical-cluster.d.ts.map +1 -0
  70. package/dist/hierarchical-cluster.js +97 -0
  71. package/dist/hierarchical-cluster.js.map +1 -0
  72. package/dist/hypergeometric-distribution.d.ts +18 -0
  73. package/dist/hypergeometric-distribution.d.ts.map +1 -0
  74. package/dist/hypergeometric-distribution.js +92 -0
  75. package/dist/hypergeometric-distribution.js.map +1 -0
  76. package/dist/index.d.ts +34 -0
  77. package/dist/index.d.ts.map +1 -0
  78. package/dist/index.js +34 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/kmeans.d.ts +87 -0
  81. package/dist/kmeans.d.ts.map +1 -0
  82. package/dist/kmeans.js +126 -0
  83. package/dist/kmeans.js.map +1 -0
  84. package/dist/kmedoids.d.ts +27 -0
  85. package/dist/kmedoids.d.ts.map +1 -0
  86. package/dist/kmedoids.js +47 -0
  87. package/dist/kmedoids.js.map +1 -0
  88. package/dist/log-normal-distribution.d.ts +15 -0
  89. package/dist/log-normal-distribution.d.ts.map +1 -0
  90. package/dist/log-normal-distribution.js +44 -0
  91. package/dist/log-normal-distribution.js.map +1 -0
  92. package/dist/negative-binomial-distribution.d.ts +18 -0
  93. package/dist/negative-binomial-distribution.d.ts.map +1 -0
  94. package/dist/negative-binomial-distribution.js +64 -0
  95. package/dist/negative-binomial-distribution.js.map +1 -0
  96. package/dist/normal-distribution.d.ts +16 -0
  97. package/dist/normal-distribution.d.ts.map +1 -0
  98. package/dist/normal-distribution.js +41 -0
  99. package/dist/normal-distribution.js.map +1 -0
  100. package/dist/normal-quantile.d.ts +19 -0
  101. package/dist/normal-quantile.d.ts.map +1 -0
  102. package/dist/normal-quantile.js +115 -0
  103. package/dist/normal-quantile.js.map +1 -0
  104. package/dist/one-way-anova.d.ts +31 -0
  105. package/dist/one-way-anova.d.ts.map +1 -0
  106. package/dist/one-way-anova.js +69 -0
  107. package/dist/one-way-anova.js.map +1 -0
  108. package/dist/outliers.d.ts +20 -0
  109. package/dist/outliers.d.ts.map +1 -0
  110. package/dist/outliers.js +49 -0
  111. package/dist/outliers.js.map +1 -0
  112. package/dist/poisson-distribution.d.ts +15 -0
  113. package/dist/poisson-distribution.d.ts.map +1 -0
  114. package/dist/poisson-distribution.js +44 -0
  115. package/dist/poisson-distribution.js.map +1 -0
  116. package/dist/quantile.d.ts +9 -0
  117. package/dist/quantile.d.ts.map +1 -0
  118. package/dist/quantile.js +23 -0
  119. package/dist/quantile.js.map +1 -0
  120. package/dist/random-effects.d.ts +45 -0
  121. package/dist/random-effects.d.ts.map +1 -0
  122. package/dist/random-effects.js +97 -0
  123. package/dist/random-effects.js.map +1 -0
  124. package/dist/regression.d.ts +12 -0
  125. package/dist/regression.d.ts.map +1 -0
  126. package/dist/regression.js +35 -0
  127. package/dist/regression.js.map +1 -0
  128. package/dist/student-t-distribution.d.ts +16 -0
  129. package/dist/student-t-distribution.d.ts.map +1 -0
  130. package/dist/student-t-distribution.js +54 -0
  131. package/dist/student-t-distribution.js.map +1 -0
  132. package/dist/sum.d.ts +31 -0
  133. package/dist/sum.d.ts.map +1 -0
  134. package/dist/sum.js +30 -0
  135. package/dist/sum.js.map +1 -0
  136. package/dist/t-test.d.ts +52 -0
  137. package/dist/t-test.d.ts.map +1 -0
  138. package/dist/t-test.js +81 -0
  139. package/dist/t-test.js.map +1 -0
  140. package/dist/trend.d.ts +55 -0
  141. package/dist/trend.d.ts.map +1 -0
  142. package/dist/trend.js +98 -0
  143. package/dist/trend.js.map +1 -0
  144. package/dist/uncertainty.d.ts +25 -0
  145. package/dist/uncertainty.d.ts.map +1 -0
  146. package/dist/uncertainty.js +26 -0
  147. package/dist/uncertainty.js.map +1 -0
  148. package/dist/uniform-distribution.d.ts +11 -0
  149. package/dist/uniform-distribution.d.ts.map +1 -0
  150. package/dist/uniform-distribution.js +33 -0
  151. package/dist/uniform-distribution.js.map +1 -0
  152. package/dist/welch-t-statistic.d.ts +24 -0
  153. package/dist/welch-t-statistic.d.ts.map +1 -0
  154. package/dist/welch-t-statistic.js +33 -0
  155. package/dist/welch-t-statistic.js.map +1 -0
  156. package/package.json +66 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to
7
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8
+
9
+ ## [0.1.0] - 2026-08-24
10
+
11
+ ### Added
12
+
13
+ - `mean`, `geometricMean`, `harmonicMean`, `rootMeanSquare`, `median`, `mode`,
14
+ and `midrange` statistical functions, extracted from `@ac-essentials/util` and
15
+ grouped here as a dedicated math library.
16
+ - Array overloads on all variadic functions. The variadic form still works, but
17
+ it forces the caller to spread and throws `RangeError` past roughly 100 000
18
+ values.
19
+ - `geometricMean` sums logarithms instead of multiplying, so it no longer
20
+ underflows to zero on more than a few hundred values below one.
21
+ - `median` uses selection instead of a full sort, and no longer modifies the
22
+ input array.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # @ac-kit/math-stats
2
+
3
+ Descriptive statistics, probability distributions and statistical inference over
4
+ samples of `number`.
5
+
6
+ ## Scope
7
+
8
+ Summarising a dataset, and reasoning about the process that produced it. The
9
+ input is always a collection of observations; the output is a statistic, a
10
+ distribution parameter, or a model.
11
+
12
+ ## Contents
13
+
14
+ | Symbol | Description |
15
+ | ----------------------- | ---------------------------------------------------------------------- |
16
+ | `mean(values)` | Arithmetic mean |
17
+ | `geometricMean(values)` | `n`-th root of the product; for ratios and growth rates |
18
+ | `harmonicMean(values)` | Reciprocal of the mean of reciprocals; for rates over a fixed distance |
19
+ | `midrange(values)` | `(min + max) / 2` |
20
+
21
+ ## Which mean
22
+
23
+ The three means answer different questions and are not interchangeable:
24
+
25
+ - **Arithmetic** — the value that preserves the _sum_. Use for quantities that
26
+ add: durations, counts, sizes.
27
+ - **Geometric** — the value that preserves the _product_. Use for quantities
28
+ that compound: growth rates, returns, ratios of ratios. The arithmetic mean of
29
+ a +50 % year and a −50 % year is 0 %; the geometric mean correctly reports the
30
+ −13.4 % that actually happened.
31
+ - **Harmonic** — the value that preserves the _sum of reciprocals_. Use for
32
+ rates measured over a fixed quantity: the average speed over equal distances,
33
+ or an F₁ score over precision and recall.
34
+
35
+ They satisfy `harmonic ≤ geometric ≤ arithmetic` for positive values, with
36
+ equality only when every value is identical.
37
+
38
+ ## Distributions
39
+
40
+ Every family is a factory returning a `Distribution` — `density`, `cdf`,
41
+ `survival`, `quantile`, `sample`, `mean`, `variance`.
42
+
43
+ | Continuous | Discrete |
44
+ | --- | --- |
45
+ | `normalDistribution`, `logNormalDistribution` | `binomialDistribution` |
46
+ | `exponentialDistribution`, `uniformDistribution` | `poissonDistribution` |
47
+ | `gammaDistribution`, `chiSquaredDistribution` | `geometricDistribution` |
48
+ | `studentTDistribution`, `betaDistribution` | `negativeBinomialDistribution` |
49
+ | `fDistribution` | `hypergeometricDistribution` |
50
+
51
+ **`survival` is not a convenience over `1 - cdf(x)`.** It is the member the rest
52
+ is built around: a p-value _is_ an upper tail, and `1 - cdf(x)` is exactly `0`
53
+ once `cdf(x)` rounds to 1 — from about four standard deviations out for the
54
+ normal. Every family computes it directly, so a p-value of `1e-64` comes back as
55
+ `1e-64` rather than as zero.
56
+
57
+ `geometricDistribution` counts **failures** before the first success, so its
58
+ support starts at 0 and `geometricDistribution(p)` is exactly
59
+ `negativeBinomialDistribution(1, p)`. Libraries that count trials instead report
60
+ one more throughout.
61
+
62
+ `sample` draws by inverse transform and takes an optional uniform source, so
63
+ passing a seeded generator from `@ac-kit/math-random` makes a draw reproducible.
64
+
65
+ ## Inference
66
+
67
+ | Symbol | Description |
68
+ | --- | --- |
69
+ | `tTest(sample, other, options)` | One-sample, two-sample, paired and pooled |
70
+ | `welchTStatistic(a, b)` | The statistic alone, for callers thresholding on it |
71
+ | `chiSquaredTest(observed, expected, options)` | Pearson's goodness of fit |
72
+ | `oneWayAnova(groups)` | Whether several groups share a common mean |
73
+
74
+ `tTest` reads its form from its arguments rather than from an option: a number
75
+ as the second argument is a hypothesised mean, an array is a second sample.
76
+
77
+ ## Usage
78
+
79
+ ```ts
80
+ import { geometricMean, mean, normalDistribution, tTest } from "@ac-kit/math-stats";
81
+
82
+ mean(1, 2, 3, 4); // 2.5
83
+ geometricMean(1.5, 0.5); // ≈ 0.866 — a 13.4 % compounded loss
84
+
85
+ normalDistribution(0, 1).survival(6); // 9.87e-10, where 1 - cdf(6) is 0
86
+ tTest([5.1, 4.9, 5.3], [5.9, 6.1, 5.8]).pValue; // ≈ 0.0011
87
+ ```
88
+
89
+ See the
90
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/math-stats/)
91
+ for the full reference.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Rejects a probability outside `[0, 1]`, naming the distribution rather than
3
+ * this module.
4
+ *
5
+ * Shared because every family's `quantile` needs the identical check, and a
6
+ * caller reading one message should not find it worded differently from the
7
+ * next.
8
+ *
9
+ * @param routine Name of the calling export, for the message.
10
+ * @param probability The value to check.
11
+ * @throws {RangeError} When `probability` is outside `[0, 1]` or is `NaN`.
12
+ */
13
+ export declare function assertProbability_(routine: string, probability: number): void;
14
+ //# sourceMappingURL=_assert-probability.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_assert-probability.d.ts","sourceRoot":"","sources":["../src/_assert-probability.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAM7E"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Rejects a probability outside `[0, 1]`, naming the distribution rather than
3
+ * this module.
4
+ *
5
+ * Shared because every family's `quantile` needs the identical check, and a
6
+ * caller reading one message should not find it worded differently from the
7
+ * next.
8
+ *
9
+ * @param routine Name of the calling export, for the message.
10
+ * @param probability The value to check.
11
+ * @throws {RangeError} When `probability` is outside `[0, 1]` or is `NaN`.
12
+ */
13
+ export function assertProbability_(routine, probability) {
14
+ if (!(probability >= 0 && probability <= 1)) {
15
+ throw new RangeError(`${routine}: probability must be in [0, 1], got ${probability}`);
16
+ }
17
+ }
18
+ //# sourceMappingURL=_assert-probability.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_assert-probability.js","sourceRoot":"","sources":["../src/_assert-probability.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAe,EAAE,WAAmB;IACtE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CACnB,GAAG,OAAO,wCAAwC,WAAW,EAAE,CAC/D,CAAC;IACH,CAAC;AACF,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The smallest outcome whose cumulative probability reaches `probability`.
3
+ *
4
+ * The generalised inverse a discrete distribution needs, since its `cdf` is a
5
+ * step function and has no ordinary inverse. Binary search over the support, so
6
+ * O(log n) evaluations of `cdf` rather than the O(n) a linear scan from the
7
+ * bottom would cost on a distribution centred far from zero.
8
+ *
9
+ * @param cdf The distribution's cumulative function.
10
+ * @param probability Probability in `[0, 1]`.
11
+ * @param lowerBound Smallest outcome in the support.
12
+ * @param upperBound Largest outcome, or `Infinity` for an unbounded family.
13
+ * @returns The quantile.
14
+ */
15
+ export declare function discreteQuantile_(cdf: (outcome: number) => number, probability: number, lowerBound: number, upperBound: number): number;
16
+ //# sourceMappingURL=_discrete-quantile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_discrete-quantile.d.ts","sourceRoot":"","sources":["../src/_discrete-quantile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,EAChC,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GAChB,MAAM,CA8BR"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The smallest outcome whose cumulative probability reaches `probability`.
3
+ *
4
+ * The generalised inverse a discrete distribution needs, since its `cdf` is a
5
+ * step function and has no ordinary inverse. Binary search over the support, so
6
+ * O(log n) evaluations of `cdf` rather than the O(n) a linear scan from the
7
+ * bottom would cost on a distribution centred far from zero.
8
+ *
9
+ * @param cdf The distribution's cumulative function.
10
+ * @param probability Probability in `[0, 1]`.
11
+ * @param lowerBound Smallest outcome in the support.
12
+ * @param upperBound Largest outcome, or `Infinity` for an unbounded family.
13
+ * @returns The quantile.
14
+ */
15
+ export function discreteQuantile_(cdf, probability, lowerBound, upperBound) {
16
+ if (probability <= 0) {
17
+ return lowerBound;
18
+ }
19
+ if (probability >= 1) {
20
+ return upperBound;
21
+ }
22
+ let lower = lowerBound;
23
+ let upper = upperBound;
24
+ if (!Number.isFinite(upper)) {
25
+ // Double outward until the target is enclosed, so an unbounded support
26
+ // still costs O(log n) rather than needing a guessed ceiling.
27
+ upper = Math.max(1, lowerBound + 1);
28
+ while (cdf(upper) < probability) {
29
+ upper *= 2;
30
+ }
31
+ }
32
+ while (lower < upper) {
33
+ const middle = Math.floor(lower + (upper - lower) / 2);
34
+ if (cdf(middle) < probability) {
35
+ lower = middle + 1;
36
+ }
37
+ else {
38
+ upper = middle;
39
+ }
40
+ }
41
+ return lower;
42
+ }
43
+ //# sourceMappingURL=_discrete-quantile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_discrete-quantile.js","sourceRoot":"","sources":["../src/_discrete-quantile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,iBAAiB,CAChC,GAAgC,EAChC,WAAmB,EACnB,UAAkB,EAClB,UAAkB;IAElB,IAAI,WAAW,IAAI,CAAC,EAAE,CAAC;QACtB,OAAO,UAAU,CAAC;IACnB,CAAC;IACD,IAAI,WAAW,IAAI,CAAC,EAAE,CAAC;QACtB,OAAO,UAAU,CAAC;IACnB,CAAC;IAED,IAAI,KAAK,GAAG,UAAU,CAAC;IACvB,IAAI,KAAK,GAAG,UAAU,CAAC;IAEvB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,uEAAuE;QACvE,8DAA8D;QAC9D,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,GAAG,CAAC,CAAC,CAAC;QACpC,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,WAAW,EAAE,CAAC;YACjC,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;IACF,CAAC;IAED,OAAO,KAAK,GAAG,KAAK,EAAE,CAAC;QACtB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QACvD,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,WAAW,EAAE,CAAC;YAC/B,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC;QACpB,CAAC;aAAM,CAAC;YACP,KAAK,GAAG,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * One draw by inverse transform: feed a uniform variate through the quantile
4
+ * function.
5
+ *
6
+ * Correct for every family with a quantile, continuous or discrete, which is
7
+ * why it is the default rather than a per-family sampler. It is not the fastest
8
+ * route where a specialised method exists — Marsaglia–Tsang for the gamma, say
9
+ * — but those are worth adding only against a measured need, and adding one
10
+ * later changes nothing a caller can see.
11
+ *
12
+ * @param quantile The distribution's quantile function.
13
+ * @param random Uniform source over `[0, 1)`; defaults to `Math.random`.
14
+ * @returns The drawn value.
15
+ */
16
+ export declare function inverseTransformSample_(quantile: (probability: number) => number, random?: RandomFn | null): number;
17
+ //# sourceMappingURL=_inverse-transform-sample.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_inverse-transform-sample.d.ts","sourceRoot":"","sources":["../src/_inverse-transform-sample.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,MAAM,EACzC,MAAM,CAAC,EAAE,QAAQ,GAAG,IAAI,GACtB,MAAM,CAER"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * One draw by inverse transform: feed a uniform variate through the quantile
3
+ * function.
4
+ *
5
+ * Correct for every family with a quantile, continuous or discrete, which is
6
+ * why it is the default rather than a per-family sampler. It is not the fastest
7
+ * route where a specialised method exists — Marsaglia–Tsang for the gamma, say
8
+ * — but those are worth adding only against a measured need, and adding one
9
+ * later changes nothing a caller can see.
10
+ *
11
+ * @param quantile The distribution's quantile function.
12
+ * @param random Uniform source over `[0, 1)`; defaults to `Math.random`.
13
+ * @returns The drawn value.
14
+ */
15
+ export function inverseTransformSample_(quantile, random) {
16
+ return quantile((random ?? Math.random)());
17
+ }
18
+ //# sourceMappingURL=_inverse-transform-sample.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_inverse-transform-sample.js","sourceRoot":"","sources":["../src/_inverse-transform-sample.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,uBAAuB,CACtC,QAAyC,EACzC,MAAwB;IAExB,OAAO,QAAQ,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `ln C(n, k)`, the log of the binomial coefficient.
3
+ *
4
+ * Through log-gammas rather than through `@ac-kit/math-combinatorics`'s exact
5
+ * `binomial`: that returns a `bigint` because the coefficient outgrows binary64
6
+ * — `C(1030, 515)` already does — and every consumer here immediately
7
+ * multiplies it by probabilities small enough to bring the product back into
8
+ * range. Converting the exact value to a `number` first would overflow to
9
+ * `Infinity` on the way, so the log is not a shortcut but the only route.
10
+ *
11
+ * @param n Upper index, a non-negative integer.
12
+ * @param k Lower index, in `[0, n]`.
13
+ * @returns `ln C(n, k)`.
14
+ */
15
+ export declare function logBinomial_(n: number, k: number): number;
16
+ //# sourceMappingURL=_log-binomial.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_log-binomial.d.ts","sourceRoot":"","sources":["../src/_log-binomial.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEzD"}
@@ -0,0 +1,19 @@
1
+ import { logGamma } from "@ac-kit/math-analysis";
2
+ /**
3
+ * `ln C(n, k)`, the log of the binomial coefficient.
4
+ *
5
+ * Through log-gammas rather than through `@ac-kit/math-combinatorics`'s exact
6
+ * `binomial`: that returns a `bigint` because the coefficient outgrows binary64
7
+ * — `C(1030, 515)` already does — and every consumer here immediately
8
+ * multiplies it by probabilities small enough to bring the product back into
9
+ * range. Converting the exact value to a `number` first would overflow to
10
+ * `Infinity` on the way, so the log is not a shortcut but the only route.
11
+ *
12
+ * @param n Upper index, a non-negative integer.
13
+ * @param k Lower index, in `[0, n]`.
14
+ * @returns `ln C(n, k)`.
15
+ */
16
+ export function logBinomial_(n, k) {
17
+ return logGamma(n + 1) - logGamma(k + 1) - logGamma(n - k + 1);
18
+ }
19
+ //# sourceMappingURL=_log-binomial.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_log-binomial.js","sourceRoot":"","sources":["../src/_log-binomial.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,YAAY,CAAC,CAAS,EAAE,CAAS;IAChD,OAAO,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AAChE,CAAC"}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Calculates the arithmetic mean of the given values.
3
+ *
4
+ * The arithmetic mean is the sum of the values divided by the number of values.
5
+ *
6
+ * @param values The values to calculate the mean of, as an array or as separate
7
+ * arguments.
8
+ * @returns The arithmetic mean of the given values.
9
+ * @see https://en.wikipedia.org/wiki/Arithmetic_mean
10
+ */
11
+ export declare function mean(values: readonly number[]): number;
12
+ export declare function mean(...values: number[]): number;
13
+ /**
14
+ * Calculates the geometric mean of the given values.
15
+ *
16
+ * The geometric mean is the nth root of the product of the values, where n is
17
+ * the number of values.
18
+ *
19
+ * @param values The values to calculate the mean of, as an array or as separate
20
+ * arguments.
21
+ * @returns The geometric mean of the given values.
22
+ * @see https://en.wikipedia.org/wiki/Geometric_mean
23
+ */
24
+ export declare function geometricMean(values: readonly number[]): number;
25
+ export declare function geometricMean(...values: number[]): number;
26
+ /**
27
+ * Calculates the harmonic mean of the given values.
28
+ *
29
+ * The harmonic mean is the number of values divided by the sum of the
30
+ * reciprocals of the values.
31
+ *
32
+ * @param values The values to calculate the mean of, as an array or as separate
33
+ * arguments.
34
+ * @returns The harmonic mean of the given values.
35
+ * @see https://en.wikipedia.org/wiki/Harmonic_mean
36
+ */
37
+ export declare function harmonicMean(values: readonly number[]): number;
38
+ export declare function harmonicMean(...values: number[]): number;
39
+ /**
40
+ * Calculates the root mean square (or quadratic mean) of the given values.
41
+ *
42
+ * The quadratic mean is the square root of the arithmetic mean of the squares
43
+ * of the values.
44
+ *
45
+ * @param values The values to calculate the mean of, as an array or as separate
46
+ * arguments.
47
+ * @returns The root mean square of the given values.
48
+ * @see https://en.wikipedia.org/wiki/Root_mean_square
49
+ */
50
+ export declare function rootMeanSquare(values: readonly number[]): number;
51
+ export declare function rootMeanSquare(...values: number[]): number;
52
+ /**
53
+ * Calculates the median of a list of numbers.
54
+ *
55
+ * The median is the middle value when the numbers are sorted in ascending
56
+ * order.
57
+ *
58
+ * If there is an even number of values, the median is the average of the two
59
+ * middle values.
60
+ *
61
+ * The input is never modified.
62
+ *
63
+ * @param values The values to calculate the median of, as an array or as
64
+ * separate arguments.
65
+ * @returns Median of the numbers.
66
+ * @see https://en.wikipedia.org/wiki/Median
67
+ */
68
+ export declare function median(values: readonly number[]): number;
69
+ export declare function median(...values: number[]): number;
70
+ /**
71
+ * Calculates the mode of a list of numbers.
72
+ *
73
+ * The mode is the number that appears most frequently in the list. If multiple
74
+ * numbers have the same highest frequency, all of them are returned.
75
+ *
76
+ * @param values The values to calculate the mode of, as an array or as separate
77
+ * arguments.
78
+ * @returns Mode of the numbers (can be multiple values).
79
+ * @see https://en.wikipedia.org/wiki/Mode_(statistics)
80
+ */
81
+ export declare function mode(values: readonly number[]): number[];
82
+ export declare function mode(...values: number[]): number[];
83
+ /**
84
+ * Calculates the midrange of a list of numbers.
85
+ *
86
+ * The midrange is the average of the minimum and maximum values in the list.
87
+ *
88
+ * @param values The values to calculate the midrange of, as an array or as
89
+ * separate arguments.
90
+ * @returns The midrange of the given values.
91
+ * @see https://en.wikipedia.org/wiki/Mid-range
92
+ */
93
+ export declare function midrange(values: readonly number[]): number;
94
+ export declare function midrange(...values: number[]): number;
95
+ //# sourceMappingURL=average.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"average.d.ts","sourceRoot":"","sources":["../src/average.ts"],"names":[],"mappings":"AAUA;;;;;;;;;GASG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;AACxD,wBAAgB,IAAI,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AAalD;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;AACjE,wBAAgB,aAAa,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AAsB3D;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;AAChE,wBAAgB,YAAY,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AAmB1D;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;AAClE,wBAAgB,cAAc,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AAW5D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;AAC1D,wBAAgB,MAAM,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AA8BpD;;;;;;;;;;GAUG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;AAC1D,wBAAgB,IAAI,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;AA+BpD;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;AAC5D,wBAAgB,QAAQ,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC"}
@@ -0,0 +1,113 @@
1
+ import { quickselect } from "@ac-kit/algo";
2
+ import { compareNaturalAscending, fromVariadicArgs, map, minmax, sumPrecise, } from "@ac-kit/core";
3
+ export function mean(...args) {
4
+ const values = fromVariadicArgs(args);
5
+ const count = values.length;
6
+ if (count === 0) {
7
+ return NaN;
8
+ }
9
+ // Exact: every dispersion function here centres on this mean, so its error
10
+ // would be amplified by the squaring that follows.
11
+ return sumPrecise(values) / count;
12
+ }
13
+ export function geometricMean(...args) {
14
+ const values = fromVariadicArgs(args);
15
+ const count = values.length;
16
+ if (count === 0) {
17
+ return NaN;
18
+ }
19
+ // Sum of logarithms rather than a running product: a few hundred values below
20
+ // one drive the product to zero, and the denormals it passes through on the
21
+ // way are slow as well as wrong.
22
+ const logarithms = [];
23
+ for (let index = 0; index < count; index++) {
24
+ const value = values[index];
25
+ if (value === 0) {
26
+ return 0;
27
+ }
28
+ logarithms.push(Math.log(value));
29
+ }
30
+ return Math.exp(sumPrecise(logarithms) / count);
31
+ }
32
+ export function harmonicMean(...args) {
33
+ const values = fromVariadicArgs(args);
34
+ const count = values.length;
35
+ if (count === 0) {
36
+ return NaN;
37
+ }
38
+ const reciprocals = [];
39
+ for (let index = 0; index < count; index++) {
40
+ const value = values[index];
41
+ if (value === 0) {
42
+ return 0;
43
+ }
44
+ reciprocals.push(1 / value);
45
+ }
46
+ return count / sumPrecise(reciprocals);
47
+ }
48
+ export function rootMeanSquare(...args) {
49
+ const values = fromVariadicArgs(args);
50
+ const count = values.length;
51
+ if (count === 0) {
52
+ return NaN;
53
+ }
54
+ return Math.sqrt(sumPrecise(map(values, (value) => value * value)) / count);
55
+ }
56
+ export function median(...args) {
57
+ const values = fromVariadicArgs(args);
58
+ const count = values.length;
59
+ if (count === 0) {
60
+ return NaN;
61
+ }
62
+ // Selection rather than a full sort: the median needs the element at one
63
+ // index, and partitioning down to it is linear where sorting is not.
64
+ const working = values.slice();
65
+ const middle = count >> 1;
66
+ const upper = quickselect(working, middle, compareNaturalAscending);
67
+ if (count % 2 === 1) {
68
+ return upper;
69
+ }
70
+ // Selecting index `middle` leaves every smaller element on its left, so the
71
+ // other middle value is the largest of that side.
72
+ let lower = -Infinity;
73
+ for (let index = 0; index < middle; index++) {
74
+ const value = working[index];
75
+ if (value > lower) {
76
+ lower = value;
77
+ }
78
+ }
79
+ return (lower + upper) / 2;
80
+ }
81
+ export function mode(...args) {
82
+ const values = fromVariadicArgs(args);
83
+ const count = values.length;
84
+ if (count === 0) {
85
+ return [];
86
+ }
87
+ const frequencyMap = new Map();
88
+ let maxFrequency = 0;
89
+ for (let index = 0; index < count; index++) {
90
+ const value = values[index];
91
+ const frequency = (frequencyMap.get(value) ?? 0) + 1;
92
+ frequencyMap.set(value, frequency);
93
+ if (frequency > maxFrequency) {
94
+ maxFrequency = frequency;
95
+ }
96
+ }
97
+ const modes = [];
98
+ for (const [value, frequency] of frequencyMap.entries()) {
99
+ if (frequency === maxFrequency) {
100
+ modes.push(value);
101
+ }
102
+ }
103
+ return modes;
104
+ }
105
+ export function midrange(...args) {
106
+ const values = fromVariadicArgs(args);
107
+ if (values.length === 0) {
108
+ return NaN;
109
+ }
110
+ const { min, max } = minmax(values);
111
+ return (min + max) / 2;
112
+ }
113
+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,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;QACrD,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAEnC,IAAI,SAAS,GAAG,YAAY,EAAE,CAAC;YAC9B,YAAY,GAAG,SAAS,CAAC;QAC1B,CAAC;IACF,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC;QACzD,IAAI,SAAS,KAAK,YAAY,EAAE,CAAC;YAChC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnB,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC;AAcD,MAAM,UAAU,QAAQ,CAAC,GAAG,IAA0B;IACrD,MAAM,MAAM,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACtC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IACpC,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Distribution } from "./distribution.js";
2
+ /**
3
+ * The beta distribution over `[0, 1]` — the conjugate prior of a proportion,
4
+ * and the reference distribution behind the t and F families.
5
+ *
6
+ * `survival` uses the exact reflection `1 − Iₓ(a, b) = I₁₋ₓ(b, a)` rather than
7
+ * the subtraction it is written as, so the upper tail keeps its digits.
8
+ *
9
+ * @param alpha First shape parameter; must be positive.
10
+ * @param beta Second shape parameter; must be positive.
11
+ * @returns The distribution.
12
+ * @throws {RangeError} When either shape is not positive.
13
+ */
14
+ export declare function betaDistribution(alpha: number, beta: number): Distribution;
15
+ //# sourceMappingURL=beta-distribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"beta-distribution.d.ts","sourceRoot":"","sources":["../src/beta-distribution.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAyD1E"}
@@ -0,0 +1,61 @@
1
+ import { inverseRegularizedIncompleteBeta, logBeta, regularizedIncompleteBeta, } from "@ac-kit/math-analysis";
2
+ import { assertProbability_ } from "./_assert-probability.js";
3
+ import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
4
+ /**
5
+ * The beta distribution over `[0, 1]` — the conjugate prior of a proportion,
6
+ * and the reference distribution behind the t and F families.
7
+ *
8
+ * `survival` uses the exact reflection `1 − Iₓ(a, b) = I₁₋ₓ(b, a)` rather than
9
+ * the subtraction it is written as, so the upper tail keeps its digits.
10
+ *
11
+ * @param alpha First shape parameter; must be positive.
12
+ * @param beta Second shape parameter; must be positive.
13
+ * @returns The distribution.
14
+ * @throws {RangeError} When either shape is not positive.
15
+ */
16
+ export function betaDistribution(alpha, beta) {
17
+ if (!(alpha > 0)) {
18
+ throw new RangeError(`betaDistribution: alpha must be positive, got ${alpha}`);
19
+ }
20
+ if (!(beta > 0)) {
21
+ throw new RangeError(`betaDistribution: beta must be positive, got ${beta}`);
22
+ }
23
+ const logNormaliser = logBeta(alpha, beta);
24
+ const total = alpha + beta;
25
+ /** Where the distribution's mass tips from one side to the other. */
26
+ const crossover = (alpha + 1) / (total + 2);
27
+ const quantile = (probability) => {
28
+ assertProbability_("betaDistribution", probability);
29
+ return inverseRegularizedIncompleteBeta(alpha, beta, probability);
30
+ };
31
+ return {
32
+ density: (x) => {
33
+ if (x < 0 || x > 1) {
34
+ return 0;
35
+ }
36
+ return Math.exp((alpha - 1) * Math.log(x) + (beta - 1) * Math.log1p(-x) - logNormaliser);
37
+ },
38
+ cdf: (x) => x <= 0 ? 0 : x >= 1 ? 1 : regularizedIncompleteBeta(alpha, beta, x),
39
+ survival: (x) => {
40
+ if (x <= 0) {
41
+ return 1;
42
+ }
43
+ if (x >= 1) {
44
+ return 0;
45
+ }
46
+ // Below the crossover the upper tail is the large one, and the exact
47
+ // reflection `I₁₋ₓ(b, a)` cannot express it: `1 - x` rounds to 1 for any
48
+ // `x` under an ulp, which reports the whole tail as 1. Subtracting the
49
+ // small lower tail from 1 is exact there. Above the crossover the roles
50
+ // swap and the reflection is the only form that keeps the digits.
51
+ return x < crossover
52
+ ? 1 - regularizedIncompleteBeta(alpha, beta, x)
53
+ : regularizedIncompleteBeta(beta, alpha, 1 - x);
54
+ },
55
+ quantile,
56
+ sample: (random) => inverseTransformSample_(quantile, random),
57
+ mean: alpha / total,
58
+ variance: (alpha * beta) / (total * total * (total + 1)),
59
+ };
60
+ }
61
+ //# sourceMappingURL=beta-distribution.js.map