@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"beta-distribution.js","sourceRoot":"","sources":["../src/beta-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gCAAgC,EAChC,OAAO,EACP,yBAAyB,GACzB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAGzE;;;;;;;;;;;GAWG;AACH,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,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,yBAAyB,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACpE,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE;YACf,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;gBACZ,OAAO,CAAC,CAAC;YACV,CAAC;YACD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;gBACZ,OAAO,CAAC,CAAC;YACV,CAAC;YAED,qEAAqE;YACrE,yEAAyE;YACzE,uEAAuE;YACvE,wEAAwE;YACxE,kEAAkE;YAClE,OAAO,CAAC,GAAG,SAAS;gBACnB,CAAC,CAAC,CAAC,GAAG,yBAAyB,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC/C,CAAC,CAAC,yBAAyB,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAClD,CAAC;QACD,QAAQ;QACR,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC7D,IAAI,EAAE,KAAK,GAAG,KAAK;QACnB,QAAQ,EAAE,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;KACxD,CAAC;AACH,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { Distribution } from "./distribution.js";
2
+ /**
3
+ * The binomial distribution — the number of successes in `trials` independent
4
+ * attempts, each succeeding with probability `probability`.
5
+ *
6
+ * Both tails come from the regularized incomplete beta, which is an exact
7
+ * identity rather than an approximation: `P(X ≤ k) = I₁₋ₚ(n − k, k + 1)`. That
8
+ * costs one continued fraction instead of a sum of `k` terms, and keeps the
9
+ * upper tail accurate where summing from the bottom and subtracting would not.
10
+ *
11
+ * @param trials Number of attempts; a non-negative integer.
12
+ * @param probability Success probability of one attempt, in `[0, 1]`.
13
+ * @returns The distribution.
14
+ * @throws {RangeError} When `trials` is not a non-negative integer, or
15
+ * `probability` is outside `[0, 1]`.
16
+ */
17
+ export declare function binomialDistribution(trials: number, probability: number): Distribution;
18
+ //# sourceMappingURL=binomial-distribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binomial-distribution.d.ts","sourceRoot":"","sources":["../src/binomial-distribution.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GACjB,YAAY,CAuEd"}
@@ -0,0 +1,74 @@
1
+ import { regularizedIncompleteBeta } from "@ac-kit/math-analysis";
2
+ import { assertProbability_ } from "./_assert-probability.js";
3
+ import { discreteQuantile_ } from "./_discrete-quantile.js";
4
+ import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
5
+ import { logBinomial_ } from "./_log-binomial.js";
6
+ /**
7
+ * The binomial distribution — the number of successes in `trials` independent
8
+ * attempts, each succeeding with probability `probability`.
9
+ *
10
+ * Both tails come from the regularized incomplete beta, which is an exact
11
+ * identity rather than an approximation: `P(X ≤ k) = I₁₋ₚ(n − k, k + 1)`. That
12
+ * costs one continued fraction instead of a sum of `k` terms, and keeps the
13
+ * upper tail accurate where summing from the bottom and subtracting would not.
14
+ *
15
+ * @param trials Number of attempts; a non-negative integer.
16
+ * @param probability Success probability of one attempt, in `[0, 1]`.
17
+ * @returns The distribution.
18
+ * @throws {RangeError} When `trials` is not a non-negative integer, or
19
+ * `probability` is outside `[0, 1]`.
20
+ */
21
+ export function binomialDistribution(trials, probability) {
22
+ if (!Number.isInteger(trials) || trials < 0) {
23
+ throw new RangeError(`binomialDistribution: trials must be a non-negative integer, got ${trials}`);
24
+ }
25
+ assertProbability_("binomialDistribution", probability);
26
+ const cdf = (x) => {
27
+ const successes = Math.floor(x);
28
+ if (successes < 0) {
29
+ return 0;
30
+ }
31
+ if (successes >= trials) {
32
+ return 1;
33
+ }
34
+ return regularizedIncompleteBeta(trials - successes, successes + 1, 1 - probability);
35
+ };
36
+ const quantile = (target) => {
37
+ assertProbability_("binomialDistribution", target);
38
+ return discreteQuantile_(cdf, target, 0, trials);
39
+ };
40
+ return {
41
+ density: (x) => {
42
+ if (!Number.isInteger(x) || x < 0 || x > trials) {
43
+ return 0;
44
+ }
45
+ // `0 ** 0` is 1, but `0 * Math.log(0)` is NaN, so the degenerate
46
+ // probabilities are answered before any logarithm is taken.
47
+ if (probability === 0) {
48
+ return x === 0 ? 1 : 0;
49
+ }
50
+ if (probability === 1) {
51
+ return x === trials ? 1 : 0;
52
+ }
53
+ return Math.exp(logBinomial_(trials, x) +
54
+ x * Math.log(probability) +
55
+ (trials - x) * Math.log1p(-probability));
56
+ },
57
+ cdf,
58
+ survival: (x) => {
59
+ const successes = Math.floor(x);
60
+ if (successes < 0) {
61
+ return 1;
62
+ }
63
+ if (successes >= trials) {
64
+ return 0;
65
+ }
66
+ return regularizedIncompleteBeta(successes + 1, trials - successes, probability);
67
+ },
68
+ quantile,
69
+ sample: (random) => inverseTransformSample_(quantile, random),
70
+ mean: trials * probability,
71
+ variance: trials * probability * (1 - probability),
72
+ };
73
+ }
74
+ //# sourceMappingURL=binomial-distribution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binomial-distribution.js","sourceRoot":"","sources":["../src/binomial-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAElE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,oBAAoB,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AAE,CAAC;gBACvB,OAAO,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC7B,CAAC;YAED,OAAO,IAAI,CAAC,GAAG,CACd,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC;gBACtB,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC;gBACzB,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,CACxC,CAAC;QACH,CAAC;QACD,GAAG;QACH,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE;YACf,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAChC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;gBACnB,OAAO,CAAC,CAAC;YACV,CAAC;YACD,IAAI,SAAS,IAAI,MAAM,EAAE,CAAC;gBACzB,OAAO,CAAC,CAAC;YACV,CAAC;YAED,OAAO,yBAAyB,CAC/B,SAAS,GAAG,CAAC,EACb,MAAM,GAAG,SAAS,EAClB,WAAW,CACX,CAAC;QACH,CAAC;QACD,QAAQ;QACR,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC7D,IAAI,EAAE,MAAM,GAAG,WAAW;QAC1B,QAAQ,EAAE,MAAM,GAAG,WAAW,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC;KAClD,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { Distribution } from "./distribution.js";
2
+ /**
3
+ * The chi-squared distribution — the sum of `degreesOfFreedom` squared standard
4
+ * normals, and the reference distribution of the goodness-of-fit statistic.
5
+ *
6
+ * It is exactly `gammaDistribution(k / 2, 1 / 2)`, so it delegates rather than
7
+ * restating the incomplete gamma a second time; only the moments are named
8
+ * directly, where the closed form is simpler than the delegation.
9
+ *
10
+ * @param degreesOfFreedom Positive; need not be an integer.
11
+ * @returns The distribution.
12
+ * @throws {RangeError} When `degreesOfFreedom` is not positive.
13
+ */
14
+ export declare function chiSquaredDistribution(degreesOfFreedom: number): Distribution;
15
+ //# sourceMappingURL=chi-squared-distribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chi-squared-distribution.d.ts","sourceRoot":"","sources":["../src/chi-squared-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGtD;;;;;;;;;;;GAWG;AACH,wBAAgB,sBAAsB,CAAC,gBAAgB,EAAE,MAAM,GAAG,YAAY,CAc7E"}
@@ -0,0 +1,25 @@
1
+ import { gammaDistribution } from "./gamma-distribution.js";
2
+ /**
3
+ * The chi-squared distribution — the sum of `degreesOfFreedom` squared standard
4
+ * normals, and the reference distribution of the goodness-of-fit statistic.
5
+ *
6
+ * It is exactly `gammaDistribution(k / 2, 1 / 2)`, so it delegates rather than
7
+ * restating the incomplete gamma a second time; only the moments are named
8
+ * directly, where the closed form is simpler than the delegation.
9
+ *
10
+ * @param degreesOfFreedom Positive; need not be an integer.
11
+ * @returns The distribution.
12
+ * @throws {RangeError} When `degreesOfFreedom` is not positive.
13
+ */
14
+ export function chiSquaredDistribution(degreesOfFreedom) {
15
+ if (!(degreesOfFreedom > 0)) {
16
+ throw new RangeError(`chiSquaredDistribution: degreesOfFreedom must be positive, got ${degreesOfFreedom}`);
17
+ }
18
+ const underlying = gammaDistribution(degreesOfFreedom / 2, 0.5);
19
+ return {
20
+ ...underlying,
21
+ mean: degreesOfFreedom,
22
+ variance: 2 * degreesOfFreedom,
23
+ };
24
+ }
25
+ //# sourceMappingURL=chi-squared-distribution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chi-squared-distribution.js","sourceRoot":"","sources":["../src/chi-squared-distribution.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,sBAAsB,CAAC,gBAAwB;IAC9D,IAAI,CAAC,CAAC,gBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CACnB,kEAAkE,gBAAgB,EAAE,CACpF,CAAC;IACH,CAAC;IAED,MAAM,UAAU,GAAG,iBAAiB,CAAC,gBAAgB,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;IAEhE,OAAO;QACN,GAAG,UAAU;QACb,IAAI,EAAE,gBAAgB;QACtB,QAAQ,EAAE,CAAC,GAAG,gBAAgB;KAC9B,CAAC;AACH,CAAC"}
@@ -0,0 +1,39 @@
1
+ /** How many parameters the expected counts were fitted from. */
2
+ export type ChiSquaredTestOptions = {
3
+ /**
4
+ * Parameters estimated from the data itself, each of which costs one degree
5
+ * of freedom.
6
+ *
7
+ * Defaults to 0, which is right when the expected counts come from a
8
+ * hypothesis stated in advance. Fitting even one parameter and leaving this
9
+ * at 0 overstates the degrees of freedom and understates the p-value.
10
+ */
11
+ fittedParameters?: number;
12
+ };
13
+ /** A chi-squared test's statistic and the evidence against the null. */
14
+ export type ChiSquaredTestResult = {
15
+ /** Pearson's `Σ (observed − expected)² / expected`. */
16
+ statistic: number;
17
+ /** `categories − 1 − fittedParameters`. */
18
+ degreesOfFreedom: number;
19
+ /** Probability of a statistic at least this large under the null. */
20
+ pValue: number;
21
+ };
22
+ /**
23
+ * Pearson's chi-squared test, comparing observed counts against expected ones.
24
+ *
25
+ * The p-value is the chi-squared upper tail computed directly, so a result
26
+ * significant at `1e-30` reports that rather than zero.
27
+ *
28
+ * @param observed Counts actually seen. Not modified.
29
+ * @param expected Counts the null hypothesis predicts, in the same order. Every
30
+ * one must be positive — a zero expected count makes the statistic undefined,
31
+ * not infinite.
32
+ * @param options How many parameters were fitted from the data.
33
+ * @returns The statistic, its degrees of freedom and the p-value.
34
+ * @throws {RangeError} When the inputs differ in length, hold fewer than two
35
+ * categories, contain a non-positive expected count, disagree on the total
36
+ * count, or leave no degrees of freedom.
37
+ */
38
+ export declare function chiSquaredTest(observed: readonly number[], expected: readonly number[], options?: ChiSquaredTestOptions): ChiSquaredTestResult;
39
+ //# sourceMappingURL=chi-squared-test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chi-squared-test.d.ts","sourceRoot":"","sources":["../src/chi-squared-test.ts"],"names":[],"mappings":"AAIA,gEAAgE;AAChE,MAAM,MAAM,qBAAqB,GAAG;IACnC;;;;;;;OAOG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,wEAAwE;AACxE,MAAM,MAAM,oBAAoB,GAAG;IAClC,uDAAuD;IACvD,SAAS,EAAE,MAAM,CAAC;IAElB,2CAA2C;IAC3C,gBAAgB,EAAE,MAAM,CAAC;IAEzB,qEAAqE;IACrE,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAC7B,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,OAAO,CAAC,EAAE,qBAAqB,GAC7B,oBAAoB,CAqDtB"}
@@ -0,0 +1,56 @@
1
+ import { isCloseRelative, PreciseSum } from "@ac-kit/core";
2
+ import { chiSquaredDistribution } from "./chi-squared-distribution.js";
3
+ /**
4
+ * Pearson's chi-squared test, comparing observed counts against expected ones.
5
+ *
6
+ * The p-value is the chi-squared upper tail computed directly, so a result
7
+ * significant at `1e-30` reports that rather than zero.
8
+ *
9
+ * @param observed Counts actually seen. Not modified.
10
+ * @param expected Counts the null hypothesis predicts, in the same order. Every
11
+ * one must be positive — a zero expected count makes the statistic undefined,
12
+ * not infinite.
13
+ * @param options How many parameters were fitted from the data.
14
+ * @returns The statistic, its degrees of freedom and the p-value.
15
+ * @throws {RangeError} When the inputs differ in length, hold fewer than two
16
+ * categories, contain a non-positive expected count, disagree on the total
17
+ * count, or leave no degrees of freedom.
18
+ */
19
+ export function chiSquaredTest(observed, expected, options) {
20
+ if (observed.length !== expected.length) {
21
+ throw new RangeError("chiSquaredTest: observed and expected must have the same length");
22
+ }
23
+ if (observed.length < 2) {
24
+ throw new RangeError("chiSquaredTest: need at least two categories");
25
+ }
26
+ const degreesOfFreedom = observed.length - 1 - (options?.fittedParameters ?? 0);
27
+ if (degreesOfFreedom < 1) {
28
+ throw new RangeError(`chiSquaredTest: no degrees of freedom left, got ${degreesOfFreedom}`);
29
+ }
30
+ const total = new PreciseSum();
31
+ const observedTotal = new PreciseSum();
32
+ const expectedTotal = new PreciseSum();
33
+ for (const [index, count] of observed.entries()) {
34
+ const predicted = expected[index];
35
+ if (!(predicted > 0)) {
36
+ throw new RangeError(`chiSquaredTest: expected counts must be positive, got ${predicted} at index ${index}`);
37
+ }
38
+ observedTotal.add(count);
39
+ expectedTotal.add(predicted);
40
+ const deviation = count - predicted;
41
+ total.add((deviation * deviation) / predicted);
42
+ }
43
+ // A distribution of counts over a different total is not a null hypothesis
44
+ // this statistic measures against — the result would be a number, but not one
45
+ // the chi-squared distribution describes.
46
+ if (!isCloseRelative(observedTotal.value, expectedTotal.value, 1e-8)) {
47
+ throw new RangeError(`chiSquaredTest: expected counts must sum to the observed total, got ${expectedTotal.value} against ${observedTotal.value}`);
48
+ }
49
+ const statistic = total.value;
50
+ return {
51
+ statistic,
52
+ degreesOfFreedom,
53
+ pValue: chiSquaredDistribution(degreesOfFreedom).survival(statistic),
54
+ };
55
+ }
56
+ //# sourceMappingURL=chi-squared-test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chi-squared-test.js","sourceRoot":"","sources":["../src/chi-squared-test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AA2BvE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,cAAc,CAC7B,QAA2B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0C;IAC1C,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC,KAAK,EAAE,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,CAAC;QACtE,MAAM,IAAI,UAAU,CACnB,uEAAuE,aAAa,CAAC,KAAK,YAAY,aAAa,CAAC,KAAK,EAAE,CAC3H,CAAC;IACH,CAAC;IAED,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC;IAE9B,OAAO;QACN,SAAS;QACT,gBAAgB;QAChB,MAAM,EAAE,sBAAsB,CAAC,gBAAgB,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC;KACpE,CAAC;AACH,CAAC"}
@@ -0,0 +1,62 @@
1
+ /** An interval estimate around a point estimate, at a confidence level. */
2
+ export type ConfidenceInterval = {
3
+ /** The point estimate the interval is about. */
4
+ readonly estimate: number;
5
+ /** Lower bound of the interval. */
6
+ readonly lower: number;
7
+ /** Upper bound of the interval. */
8
+ readonly upper: number;
9
+ /**
10
+ * `(upper - lower) / 2`. Only equals the distance from
11
+ * {@link ConfidenceInterval.estimate} to either bound when the interval is
12
+ * symmetric about it.
13
+ */
14
+ readonly halfWidth: number;
15
+ /** The confidence level the interval was computed at, e.g. `0.95`. */
16
+ readonly level: number;
17
+ };
18
+ /**
19
+ * Normal-approximation confidence interval for the mean of a sample: `mean ± z
20
+ * · s / √n`, with `z` the `(1 + level) / 2` standard normal quantile and `s`
21
+ * the sample standard deviation.
22
+ *
23
+ * The approximation assumes the sampling distribution of the mean is close to
24
+ * normal — fine for a symmetric or mildly skewed sample of reasonable size,
25
+ * optimistic for the heavy right tail timing measurements usually have. See
26
+ * {@link medianConfidenceInterval} for a distribution-free alternative.
27
+ *
28
+ * O(n).
29
+ *
30
+ * @param values The sample. Not modified.
31
+ * @param level Confidence level in `(0, 1)`. Default `0.95`.
32
+ * @returns The interval, centered on the sample mean.
33
+ * @throws {RangeError} When the sample has fewer than two values, or `level` is
34
+ * outside `(0, 1)`.
35
+ */
36
+ export declare function meanConfidenceInterval(values: readonly number[], level?: number): ConfidenceInterval;
37
+ /**
38
+ * Distribution-free confidence interval for the median of an already-sorted
39
+ * sample, bounded by the pair of order statistics whose ranks bracket the
40
+ * median with at least the requested confidence (normal approximation to the
41
+ * binomial rank bounds).
42
+ *
43
+ * Unlike {@link meanConfidenceInterval}, no distributional assumption is made:
44
+ * both bounds are actual sample values, which is what makes this robust to the
45
+ * heavy right tail benchmark timings always have. The bounds are not generally
46
+ * symmetric about the estimate, so `estimate ± halfWidth` does not reproduce
47
+ * them.
48
+ *
49
+ * A sample can be too small for the level to be reachable at all — the widest
50
+ * possible interval spans only `1 - 2^-(n-1)` — in which case the whole sample
51
+ * is returned as the interval. At `n = 1` that is a single point.
52
+ *
53
+ * O(1) — the input is already sorted.
54
+ *
55
+ * @param sorted The sample, in ascending order. Not modified, not re-checked.
56
+ * @param level Confidence level in `(0, 1)`. Default `0.95`.
57
+ * @returns The interval, estimating the sample median.
58
+ * @throws {RangeError} When the sample is empty, or `level` is outside `(0,
59
+ * 1)`.
60
+ */
61
+ export declare function medianConfidenceInterval(sorted: readonly number[], level?: number): ConfidenceInterval;
62
+ //# sourceMappingURL=confidence-interval.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"confidence-interval.d.ts","sourceRoot":"","sources":["../src/confidence-interval.ts"],"names":[],"mappings":"AAKA,2EAA2E;AAC3E,MAAM,MAAM,kBAAkB,GAAG;IAChC,gDAAgD;IAChD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B,mCAAmC;IACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,mCAAmC;IACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;;;OAIG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,sEAAsE;IACtE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,sBAAsB,CACrC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,KAAK,SAAO,GACV,kBAAkB,CAoBpB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,KAAK,SAAO,GACV,kBAAkB,CA6BpB"}
@@ -0,0 +1,93 @@
1
+ import { mean } from "./average.js";
2
+ import { sampleStandardDeviation } from "./dispersion.js";
3
+ import { normalQuantile } from "./normal-quantile.js";
4
+ import { quantile } from "./quantile.js";
5
+ /**
6
+ * Normal-approximation confidence interval for the mean of a sample: `mean ± z
7
+ * · s / √n`, with `z` the `(1 + level) / 2` standard normal quantile and `s`
8
+ * the sample standard deviation.
9
+ *
10
+ * The approximation assumes the sampling distribution of the mean is close to
11
+ * normal — fine for a symmetric or mildly skewed sample of reasonable size,
12
+ * optimistic for the heavy right tail timing measurements usually have. See
13
+ * {@link medianConfidenceInterval} for a distribution-free alternative.
14
+ *
15
+ * O(n).
16
+ *
17
+ * @param values The sample. Not modified.
18
+ * @param level Confidence level in `(0, 1)`. Default `0.95`.
19
+ * @returns The interval, centered on the sample mean.
20
+ * @throws {RangeError} When the sample has fewer than two values, or `level` is
21
+ * outside `(0, 1)`.
22
+ */
23
+ export function meanConfidenceInterval(values, level = 0.95) {
24
+ if (values.length < 2) {
25
+ throw new RangeError("meanConfidenceInterval: the sample must have at least two values");
26
+ }
27
+ checkConfidenceLevel(level);
28
+ const estimate = mean(values);
29
+ const standardError = sampleStandardDeviation(values) / Math.sqrt(values.length);
30
+ const halfWidth = normalQuantile((1 + level) / 2) * standardError;
31
+ return {
32
+ estimate,
33
+ lower: estimate - halfWidth,
34
+ upper: estimate + halfWidth,
35
+ halfWidth,
36
+ level,
37
+ };
38
+ }
39
+ /**
40
+ * Distribution-free confidence interval for the median of an already-sorted
41
+ * sample, bounded by the pair of order statistics whose ranks bracket the
42
+ * median with at least the requested confidence (normal approximation to the
43
+ * binomial rank bounds).
44
+ *
45
+ * Unlike {@link meanConfidenceInterval}, no distributional assumption is made:
46
+ * both bounds are actual sample values, which is what makes this robust to the
47
+ * heavy right tail benchmark timings always have. The bounds are not generally
48
+ * symmetric about the estimate, so `estimate ± halfWidth` does not reproduce
49
+ * them.
50
+ *
51
+ * A sample can be too small for the level to be reachable at all — the widest
52
+ * possible interval spans only `1 - 2^-(n-1)` — in which case the whole sample
53
+ * is returned as the interval. At `n = 1` that is a single point.
54
+ *
55
+ * O(1) — the input is already sorted.
56
+ *
57
+ * @param sorted The sample, in ascending order. Not modified, not re-checked.
58
+ * @param level Confidence level in `(0, 1)`. Default `0.95`.
59
+ * @returns The interval, estimating the sample median.
60
+ * @throws {RangeError} When the sample is empty, or `level` is outside `(0,
61
+ * 1)`.
62
+ */
63
+ export function medianConfidenceInterval(sorted, level = 0.95) {
64
+ if (sorted.length === 0) {
65
+ throw new RangeError("medianConfidenceInterval: the sample must not be empty");
66
+ }
67
+ checkConfidenceLevel(level);
68
+ const count = sorted.length;
69
+ const estimate = quantile(sorted, 0.5);
70
+ // The count of observations below the true median is Binomial(n, 1/2),
71
+ // approximated as normal with mean n/2 and standard deviation √n/2. The
72
+ // upper rank is the lower rank's mirror rather than the same expression
73
+ // with the sign flipped: rounding each end independently loses the
74
+ // symmetry the procedure has, and truncates the interval by one rank.
75
+ const spread = (normalQuantile((1 + level) / 2) * Math.sqrt(count)) / 2;
76
+ const lowerRank = Math.max(1, Math.floor(count / 2 - spread));
77
+ const upperRank = Math.min(count, count + 1 - lowerRank);
78
+ const lower = sorted[lowerRank - 1];
79
+ const upper = sorted[upperRank - 1];
80
+ return {
81
+ estimate,
82
+ lower,
83
+ upper,
84
+ halfWidth: (upper - lower) / 2,
85
+ level,
86
+ };
87
+ }
88
+ function checkConfidenceLevel(level) {
89
+ if (!(level > 0 && level < 1)) {
90
+ throw new RangeError(`the confidence level must be in (0, 1), got ${level}`);
91
+ }
92
+ }
93
+ //# sourceMappingURL=confidence-interval.js.map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AK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAEvC,uEAAuE;IACvE,wEAAwE;IACxE,wEAAwE;IACxE,mEAAmE;IACnE,sEAAsE;IACtE,MAAM,MAAM,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC;IACxE,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IAC9D,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC;IACzD,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,GAAG,CAAC,CAAE,CAAC;IACrC,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,GAAG,CAAC,CAAE,CAAC;IAErC,OAAO;QACN,QAAQ;QACR,KAAK;QACL,KAAK;QACL,SAAS,EAAE,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC;QAC9B,KAAK;KACL,CAAC;AACH,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAa;IAC1C,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,UAAU,CACnB,+CAA+C,KAAK,EAAE,CACtD,CAAC;IACH,CAAC;AACF,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Variance of a sample, with Bessel's correction.
3
+ *
4
+ * Use this when the values are a sample drawn from a larger process, which is
5
+ * the usual case for measurements. See {@link populationVariance} when the
6
+ * values are the entire population.
7
+ *
8
+ * @param values The sample. Not modified.
9
+ * @returns The variance, or `0` for a sample of fewer than two values.
10
+ */
11
+ export declare function sampleVariance(values: readonly number[]): number;
12
+ /**
13
+ * Variance of a complete population.
14
+ *
15
+ * @param values The population. Not modified.
16
+ * @returns The variance, or `NaN` for an empty population.
17
+ */
18
+ export declare function populationVariance(values: readonly number[]): number;
19
+ /**
20
+ * Standard deviation of a sample, with Bessel's correction.
21
+ *
22
+ * @param values The sample. Not modified.
23
+ * @returns The standard deviation.
24
+ */
25
+ export declare function sampleStandardDeviation(values: readonly number[]): number;
26
+ /**
27
+ * Standard deviation of a complete population.
28
+ *
29
+ * @param values The population. Not modified.
30
+ * @returns The standard deviation.
31
+ */
32
+ export declare function populationStandardDeviation(values: readonly number[]): number;
33
+ /**
34
+ * Median absolute deviation — the median of the absolute deviations from the
35
+ * median.
36
+ *
37
+ * A dispersion measure that a handful of extreme values cannot inflate, unlike
38
+ * the standard deviation.
39
+ *
40
+ * @param values The sample. Not modified.
41
+ * @param center Precomputed median, when the caller already has one.
42
+ * @returns The median absolute deviation, or `NaN` for an empty sample.
43
+ */
44
+ export declare function medianAbsoluteDeviation(values: readonly number[], center?: number): number;
45
+ //# sourceMappingURL=dispersion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dispersion.d.ts","sourceRoot":"","sources":["../src/dispersion.ts"],"names":[],"mappings":"AAKA;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAOhE;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAOpE;AAUD;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAE7E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CACtC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,MAAM,CAAC,EAAE,MAAM,GACb,MAAM,CAWR"}
@@ -0,0 +1,80 @@
1
+ import { map, sumPrecise } from "@ac-kit/core";
2
+ import { mean } from "./average.js";
3
+ import { quantile } from "./quantile.js";
4
+ /**
5
+ * Variance of a sample, with Bessel's correction.
6
+ *
7
+ * Use this when the values are a sample drawn from a larger process, which is
8
+ * the usual case for measurements. See {@link populationVariance} when the
9
+ * values are the entire population.
10
+ *
11
+ * @param values The sample. Not modified.
12
+ * @returns The variance, or `0` for a sample of fewer than two values.
13
+ */
14
+ export function sampleVariance(values) {
15
+ const count = values.length;
16
+ if (count < 2) {
17
+ return 0;
18
+ }
19
+ return sumPrecise(squaredDeviations_(values)) / (count - 1);
20
+ }
21
+ /**
22
+ * Variance of a complete population.
23
+ *
24
+ * @param values The population. Not modified.
25
+ * @returns The variance, or `NaN` for an empty population.
26
+ */
27
+ export function populationVariance(values) {
28
+ const count = values.length;
29
+ if (count === 0) {
30
+ return Number.NaN;
31
+ }
32
+ return sumPrecise(squaredDeviations_(values)) / count;
33
+ }
34
+ // Exact summation is not optional here: deviations straddle zero, so a naive
35
+ // running total can cancel away every significant bit of a small variance.
36
+ function squaredDeviations_(values) {
37
+ const average = mean(values);
38
+ return map(values, (value) => (value - average) ** 2);
39
+ }
40
+ /**
41
+ * Standard deviation of a sample, with Bessel's correction.
42
+ *
43
+ * @param values The sample. Not modified.
44
+ * @returns The standard deviation.
45
+ */
46
+ export function sampleStandardDeviation(values) {
47
+ return Math.sqrt(sampleVariance(values));
48
+ }
49
+ /**
50
+ * Standard deviation of a complete population.
51
+ *
52
+ * @param values The population. Not modified.
53
+ * @returns The standard deviation.
54
+ */
55
+ export function populationStandardDeviation(values) {
56
+ return Math.sqrt(populationVariance(values));
57
+ }
58
+ /**
59
+ * Median absolute deviation — the median of the absolute deviations from the
60
+ * median.
61
+ *
62
+ * A dispersion measure that a handful of extreme values cannot inflate, unlike
63
+ * the standard deviation.
64
+ *
65
+ * @param values The sample. Not modified.
66
+ * @param center Precomputed median, when the caller already has one.
67
+ * @returns The median absolute deviation, or `NaN` for an empty sample.
68
+ */
69
+ export function medianAbsoluteDeviation(values, center) {
70
+ if (values.length === 0) {
71
+ return Number.NaN;
72
+ }
73
+ const sorted = [...values].sort((left, right) => left - right);
74
+ const middle = center ?? quantile(sorted, 0.5);
75
+ const deviations = sorted
76
+ .map((value) => Math.abs(value - middle))
77
+ .sort((left, right) => left - right);
78
+ return quantile(deviations, 0.5);
79
+ }
80
+ //# sourceMappingURL=dispersion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dispersion.js","sourceRoot":"","sources":["../src/dispersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,MAAyB;IACvD,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACf,OAAO,CAAC,CAAC;IACV,CAAC;IAED,OAAO,UAAU,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAyB;IAC3D,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACjB,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,OAAO,UAAU,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,CAAC;AACvD,CAAC;AAED,6EAA6E;AAC7E,2EAA2E;AAC3E,SAAS,k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
3
+ * A probability distribution over the reals, fixed at construction.
4
+ *
5
+ * Every member is resolved from the parameters once, so the shape is a plain
6
+ * object rather than a class: nothing here mutates after the constructing call
7
+ * returns, and `mean` and `variance` are values rather than methods because
8
+ * they are properties of the parameters, not computations over an argument.
9
+ *
10
+ * Discrete families implement the same shape — `density` is then a probability
11
+ * mass, and `quantile` the generalised inverse (the smallest outcome whose
12
+ * cumulative probability reaches the argument).
13
+ */
14
+ export type Distribution = {
15
+ /**
16
+ * Probability density at `x`, or probability mass for a discrete family.
17
+ *
18
+ * @param x Point of evaluation.
19
+ * @returns The density or mass; `0` outside the support.
20
+ */
21
+ density: (x: number) => number;
22
+ /**
23
+ * Probability of drawing a value at or below `x`.
24
+ *
25
+ * @param x Point of evaluation.
26
+ * @returns `P(X ≤ x)`, in `[0, 1]`.
27
+ */
28
+ cdf: (x: number) => number;
29
+ /**
30
+ * Probability of drawing a value above `x`.
31
+ *
32
+ * Not a convenience over `1 - cdf(x)`, and the reason this member exists at
33
+ * all: a tail of `1e-20` is exactly `0` once computed that way, and a tail is
34
+ * precisely what a p-value is. Each family computes it directly.
35
+ *
36
+ * @param x Point of evaluation.
37
+ * @returns `P(X > x)`, in `[0, 1]`.
38
+ */
39
+ survival: (x: number) => number;
40
+ /**
41
+ * The value at which the cumulative probability reaches `probability`.
42
+ *
43
+ * @param probability Probability in `[0, 1]`.
44
+ * @returns The quantile.
45
+ * @throws {RangeError} When `probability` is outside `[0, 1]`.
46
+ */
47
+ quantile: (probability: number) => number;
48
+ /**
49
+ * One draw from the distribution.
50
+ *
51
+ * @param random Uniform source over `[0, 1)`. Defaults to `Math.random`; pass
52
+ * a seeded generator from `@ac-kit/math-random` for a reproducible draw.
53
+ * @returns The drawn value.
54
+ */
55
+ sample: (random?: RandomFn | null) => number;
56
+ /** Expected value, or `NaN` where the family has none. */
57
+ mean: number;
58
+ /** Variance, or `NaN` where the family has none. */
59
+ variance: number;
60
+ };
61
+ //# sourceMappingURL=distribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"distribution.d.ts","sourceRoot":"","sources":["../src/distribution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,YAAY,GAAG;IAC1B;;;;;OAKG;IACH,OAAO,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAE/B;;;;;OAKG;IACH,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAE3B;;;;;;;;;OASG;IACH,QAAQ,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAEhC;;;;;;OAMG;IACH,QAAQ,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,MAAM,CAAC;IAE1C;;;;;;OAMG;IACH,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,IAAI,KAAK,MAAM,CAAC;IAE7C,0DAA0D;IAC1D,IAAI,EAAE,MAAM,CAAC;IAEb,oDAAoD;IACpD,QAAQ,EAAE,MAAM,CAAC;CACjB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=distribution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"distribution.js","sourceRoot":"","sources":["../src/distribution.ts"],"names":[],"mappings":""}