@ac-kit/math-stats 0.6.0 → 0.7.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 (141) hide show
  1. package/CHANGELOG.md +123 -0
  2. package/README.md +43 -2
  3. package/dist/_digamma.d.ts +10 -0
  4. package/dist/_digamma.d.ts.map +1 -0
  5. package/dist/_digamma.js +38 -0
  6. package/dist/_digamma.js.map +1 -0
  7. package/dist/_dilogarithm.d.ts +14 -0
  8. package/dist/_dilogarithm.d.ts.map +1 -0
  9. package/dist/_dilogarithm.js +77 -0
  10. package/dist/_dilogarithm.js.map +1 -0
  11. package/dist/_incomplete-gamma-shape-derivative.d.ts +17 -0
  12. package/dist/_incomplete-gamma-shape-derivative.d.ts.map +1 -0
  13. package/dist/_incomplete-gamma-shape-derivative.js +90 -0
  14. package/dist/_incomplete-gamma-shape-derivative.js.map +1 -0
  15. package/dist/_logistic.d.ts +14 -0
  16. package/dist/_logistic.d.ts.map +1 -0
  17. package/dist/_logistic.js +29 -0
  18. package/dist/_logistic.js.map +1 -0
  19. package/dist/_normal-tail.d.ts +43 -0
  20. package/dist/_normal-tail.d.ts.map +1 -0
  21. package/dist/_normal-tail.js +124 -0
  22. package/dist/_normal-tail.js.map +1 -0
  23. package/dist/_rank-test.d.ts +71 -0
  24. package/dist/_rank-test.d.ts.map +1 -0
  25. package/dist/_rank-test.js +130 -0
  26. package/dist/_rank-test.js.map +1 -0
  27. package/dist/average.js +2 -2
  28. package/dist/average.js.map +1 -1
  29. package/dist/bootstrap.d.ts +67 -0
  30. package/dist/bootstrap.d.ts.map +1 -0
  31. package/dist/bootstrap.js +154 -0
  32. package/dist/bootstrap.js.map +1 -0
  33. package/dist/censored-logistic-distribution.d.ts +23 -0
  34. package/dist/censored-logistic-distribution.d.ts.map +1 -0
  35. package/dist/censored-logistic-distribution.js +63 -0
  36. package/dist/censored-logistic-distribution.js.map +1 -0
  37. package/dist/censored-shifted-gamma-distribution.d.ts +23 -0
  38. package/dist/censored-shifted-gamma-distribution.d.ts.map +1 -0
  39. package/dist/censored-shifted-gamma-distribution.js +73 -0
  40. package/dist/censored-shifted-gamma-distribution.js.map +1 -0
  41. package/dist/confidence-interval.d.ts +53 -0
  42. package/dist/confidence-interval.d.ts.map +1 -1
  43. package/dist/confidence-interval.js +73 -0
  44. package/dist/confidence-interval.js.map +1 -1
  45. package/dist/correlation.d.ts +41 -0
  46. package/dist/correlation.d.ts.map +1 -1
  47. package/dist/correlation.js +168 -2
  48. package/dist/correlation.js.map +1 -1
  49. package/dist/descriptive.d.ts +129 -0
  50. package/dist/descriptive.d.ts.map +1 -0
  51. package/dist/descriptive.js +289 -0
  52. package/dist/descriptive.js.map +1 -0
  53. package/dist/forecast/_forecast-checks.d.ts +30 -0
  54. package/dist/forecast/_forecast-checks.d.ts.map +1 -0
  55. package/dist/forecast/_forecast-checks.js +119 -0
  56. package/dist/forecast/_forecast-checks.js.map +1 -0
  57. package/dist/forecast/_score-kernels.d.ts +63 -0
  58. package/dist/forecast/_score-kernels.d.ts.map +1 -0
  59. package/dist/forecast/_score-kernels.js +264 -0
  60. package/dist/forecast/_score-kernels.js.map +1 -0
  61. package/dist/forecast/brier-score.d.ts +102 -0
  62. package/dist/forecast/brier-score.d.ts.map +1 -0
  63. package/dist/forecast/brier-score.js +163 -0
  64. package/dist/forecast/brier-score.js.map +1 -0
  65. package/dist/forecast/crps-closed-form.d.ts +64 -0
  66. package/dist/forecast/crps-closed-form.d.ts.map +1 -0
  67. package/dist/forecast/crps-closed-form.js +106 -0
  68. package/dist/forecast/crps-closed-form.js.map +1 -0
  69. package/dist/forecast/crps-ensemble.d.ts +53 -0
  70. package/dist/forecast/crps-ensemble.d.ts.map +1 -0
  71. package/dist/forecast/crps-ensemble.js +121 -0
  72. package/dist/forecast/crps-ensemble.js.map +1 -0
  73. package/dist/forecast/emos.d.ts +158 -0
  74. package/dist/forecast/emos.d.ts.map +1 -0
  75. package/dist/forecast/emos.js +397 -0
  76. package/dist/forecast/emos.js.map +1 -0
  77. package/dist/forecast/exceedance-probability.d.ts +54 -0
  78. package/dist/forecast/exceedance-probability.d.ts.map +1 -0
  79. package/dist/forecast/exceedance-probability.js +125 -0
  80. package/dist/forecast/exceedance-probability.js.map +1 -0
  81. package/dist/forecast/log-score.d.ts +18 -0
  82. package/dist/forecast/log-score.d.ts.map +1 -0
  83. package/dist/forecast/log-score.js +19 -0
  84. package/dist/forecast/log-score.js.map +1 -0
  85. package/dist/forecast/logistic-regression.d.ts +123 -0
  86. package/dist/forecast/logistic-regression.d.ts.map +1 -0
  87. package/dist/forecast/logistic-regression.js +282 -0
  88. package/dist/forecast/logistic-regression.js.map +1 -0
  89. package/dist/forecast/rank-histogram.d.ts +32 -0
  90. package/dist/forecast/rank-histogram.d.ts.map +1 -0
  91. package/dist/forecast/rank-histogram.js +63 -0
  92. package/dist/forecast/rank-histogram.js.map +1 -0
  93. package/dist/forecast/reliability-diagram.d.ts +70 -0
  94. package/dist/forecast/reliability-diagram.d.ts.map +1 -0
  95. package/dist/forecast/reliability-diagram.js +126 -0
  96. package/dist/forecast/reliability-diagram.js.map +1 -0
  97. package/dist/forecast/roc-curve.d.ts +58 -0
  98. package/dist/forecast/roc-curve.d.ts.map +1 -0
  99. package/dist/forecast/roc-curve.js +124 -0
  100. package/dist/forecast/roc-curve.js.map +1 -0
  101. package/dist/index.d.ts +21 -0
  102. package/dist/index.d.ts.map +1 -1
  103. package/dist/index.js +21 -0
  104. package/dist/index.js.map +1 -1
  105. package/dist/mann-whitney-u.d.ts +27 -0
  106. package/dist/mann-whitney-u.d.ts.map +1 -0
  107. package/dist/mann-whitney-u.js +94 -0
  108. package/dist/mann-whitney-u.js.map +1 -0
  109. package/dist/multiple-regression.d.ts +123 -0
  110. package/dist/multiple-regression.d.ts.map +1 -0
  111. package/dist/multiple-regression.js +208 -0
  112. package/dist/multiple-regression.js.map +1 -0
  113. package/dist/principal-components.d.ts +66 -0
  114. package/dist/principal-components.d.ts.map +1 -0
  115. package/dist/principal-components.js +105 -0
  116. package/dist/principal-components.js.map +1 -0
  117. package/dist/quantile.d.ts +97 -9
  118. package/dist/quantile.d.ts.map +1 -1
  119. package/dist/quantile.js +298 -44
  120. package/dist/quantile.js.map +1 -1
  121. package/dist/regression.d.ts +46 -0
  122. package/dist/regression.d.ts.map +1 -1
  123. package/dist/regression.js +99 -1
  124. package/dist/regression.js.map +1 -1
  125. package/dist/sample.d.ts +29 -0
  126. package/dist/sample.d.ts.map +1 -0
  127. package/dist/sample.js +39 -0
  128. package/dist/sample.js.map +1 -0
  129. package/dist/t-digest.d.ts +136 -0
  130. package/dist/t-digest.d.ts.map +1 -0
  131. package/dist/t-digest.js +424 -0
  132. package/dist/t-digest.js.map +1 -0
  133. package/dist/truncated-normal-distribution.d.ts +28 -0
  134. package/dist/truncated-normal-distribution.d.ts.map +1 -0
  135. package/dist/truncated-normal-distribution.js +198 -0
  136. package/dist/truncated-normal-distribution.js.map +1 -0
  137. package/dist/wilcoxon-signed-rank.d.ts +42 -0
  138. package/dist/wilcoxon-signed-rank.d.ts.map +1 -0
  139. package/dist/wilcoxon-signed-rank.js +117 -0
  140. package/dist/wilcoxon-signed-rank.js.map +1 -0
  141. package/package.json +14 -10
@@ -0,0 +1,63 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { assertProbability_ } from "./_assert-probability.js";
3
+ import { fermiDirac1_ } from "./_dilogarithm.js";
4
+ import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
5
+ import { logistic_, logisticDensity_, softplus_ } from "./_logistic.js";
6
+ /**
7
+ * The logistic distribution of `location` and `scale` censored below at
8
+ * `lower`: `Y = max(lower, X)`, every draw of `X` under `lower` recorded as
9
+ * `lower` itself. The law of precipitation and other quantities with a point
10
+ * mass at a bound in ensemble post-processing (Messner, Mayr and Zeileis
11
+ * 2016).
12
+ *
13
+ * It is a mixed distribution: `cdf` jumps by `P(X ≤ lower)` at `lower`, and
14
+ * `density(lower)` returns that point mass — the density with respect to
15
+ * Lebesgue measure plus a unit atom at the bound, the likelihood of censored
16
+ * regression, so `−ln density(y)` is the log score at every `y`. `quantile(p)`
17
+ * is `lower` for every `p` up to the mass. `mean` and `variance` are exact, the
18
+ * second moment through the Fermi–Dirac integral `F₁` (a dilogarithm).
19
+ *
20
+ * @param location Location of the parent logistic, finite.
21
+ * @param scale Scale of the parent logistic; positive and finite.
22
+ * @param lower The censoring point, finite; `0` by default.
23
+ * @returns The distribution.
24
+ * @throws {RangeError} When a parameter is out of its range.
25
+ */
26
+ export function censoredLogisticDistribution(location, scale, lower = 0) {
27
+ if (!Number.isFinite(location) || !Number.isFinite(lower)) {
28
+ throw new RangeError(`censoredLogisticDistribution: location and lower must be finite, got ${describeValue(location)} and ${describeValue(lower)}`);
29
+ }
30
+ if (!(scale > 0 && scale < Number.POSITIVE_INFINITY)) {
31
+ throw new RangeError(`censoredLogisticDistribution: scale must be positive and finite, got ${describeValue(scale)}`);
32
+ }
33
+ const bound = (lower - location) / scale;
34
+ const pointMass = logistic_(bound);
35
+ // E(Y − lower) = s·softplus(−a), E(Y − lower)² = 2s²·F₁(−a).
36
+ const excess = scale * softplus_(-bound);
37
+ const secondMoment = 2 * scale * scale * fermiDirac1_(-bound);
38
+ const quantile = (probability) => {
39
+ assertProbability_("censoredLogisticDistribution", probability);
40
+ if (probability <= pointMass) {
41
+ return lower;
42
+ }
43
+ return (location + scale * (Math.log(probability) - Math.log1p(-probability)));
44
+ };
45
+ return {
46
+ density: (x) => {
47
+ if (x < lower) {
48
+ return 0;
49
+ }
50
+ if (x === lower) {
51
+ return pointMass;
52
+ }
53
+ return logisticDensity_((x - location) / scale) / scale;
54
+ },
55
+ cdf: (x) => (x < lower ? 0 : logistic_((x - location) / scale)),
56
+ survival: (x) => (x < lower ? 1 : logistic_((location - x) / scale)),
57
+ quantile,
58
+ sample: (random) => inverseTransformSample_(quantile, random),
59
+ mean: lower + excess,
60
+ variance: Math.max(secondMoment - excess * excess, 0),
61
+ };
62
+ }
63
+ //# sourceMappingURL=censored-logistic-distribution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"censored-logistic-distribution.js","sourceRoot":"","sources":["../src/censored-logistic-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGxE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,4BAA4B,CAC3C,QAAgB,EAChB,KAAa,EACb,KAAK,GAAG,CAAC;IAET,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3D,MAAM,IAAI,UAAU,CACnB,wEAAwE,aAAa,CAAC,QAAQ,CAAC,QAAQ,aAAa,CAAC,KAAK,CAAC,EAAE,CAC7H,CAAC;IACH,CAAC;IACD,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,MAAM,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACtD,MAAM,IAAI,UAAU,CACnB,wEAAwE,aAAa,CAAC,KAAK,CAAC,EAAE,CAC9F,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAG,CAAC,KAAK,GAAG,QAAQ,CAAC,GAAG,KAAK,CAAC;IACzC,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;IACnC,6DAA6D;IAC7D,MAAM,MAAM,GAAG,KAAK,GAAG,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;IACzC,MAAM,YAAY,GAAG,CAAC,GAAG,KAAK,GAAG,KAAK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AAC,GAAG,CAAC,YAAY,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;KACrD,CAAC;AACH,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { Distribution } from "./distribution.js";
2
+ /**
3
+ * The censored shifted gamma distribution (Scheuerer and Hamill 2015): a gamma
4
+ * variable `X` of `shape` and `scale`, shifted left by `shift` and censored at
5
+ * `lower`, `Y = lower + max(0, X − shift)`. The shift gives a point mass `P(X ≤
6
+ * shift)` at the bound while the shape stays free, which a plain gamma cannot:
7
+ * the law of precipitation amounts in ensemble post-processing.
8
+ *
9
+ * A mixed distribution: `cdf` jumps by the point mass at `lower`, and
10
+ * `density(lower)` returns it — the density with respect to Lebesgue measure
11
+ * plus a unit atom at the bound, so `−ln density(y)` is the log score at every
12
+ * `y`. `quantile(p)` is `lower` for every `p` up to the mass. `mean` and
13
+ * `variance` are exact, from the partial moments `E[Xʲ; X > shift]`.
14
+ *
15
+ * @param shape The gamma shape `k`; positive and finite.
16
+ * @param scale The gamma scale `θ`; positive and finite.
17
+ * @param shift The shift `δ`; finite and non-negative (`0`: a gamma, no mass).
18
+ * @param lower The censoring point, finite; `0` by default.
19
+ * @returns The distribution.
20
+ * @throws {RangeError} When a parameter is out of its range.
21
+ */
22
+ export declare function censoredShiftedGammaDistribution(shape: number, scale: number, shift: number, lower?: number): Distribution;
23
+ //# sourceMappingURL=censored-shifted-gamma-distribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"censored-shifted-gamma-distribution.d.ts","sourceRoot":"","sources":["../src/censored-shifted-gamma-distribution.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGtD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gCAAgC,CAC/C,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,SAAI,GACP,YAAY,CA6Dd"}
@@ -0,0 +1,73 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { regularizedIncompleteGammaUpper } from "@ac-kit/math-analysis";
3
+ import { assertProbability_ } from "./_assert-probability.js";
4
+ import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
5
+ import { gammaDistribution } from "./gamma-distribution.js";
6
+ /**
7
+ * The censored shifted gamma distribution (Scheuerer and Hamill 2015): a gamma
8
+ * variable `X` of `shape` and `scale`, shifted left by `shift` and censored at
9
+ * `lower`, `Y = lower + max(0, X − shift)`. The shift gives a point mass `P(X ≤
10
+ * shift)` at the bound while the shape stays free, which a plain gamma cannot:
11
+ * the law of precipitation amounts in ensemble post-processing.
12
+ *
13
+ * A mixed distribution: `cdf` jumps by the point mass at `lower`, and
14
+ * `density(lower)` returns it — the density with respect to Lebesgue measure
15
+ * plus a unit atom at the bound, so `−ln density(y)` is the log score at every
16
+ * `y`. `quantile(p)` is `lower` for every `p` up to the mass. `mean` and
17
+ * `variance` are exact, from the partial moments `E[Xʲ; X > shift]`.
18
+ *
19
+ * @param shape The gamma shape `k`; positive and finite.
20
+ * @param scale The gamma scale `θ`; positive and finite.
21
+ * @param shift The shift `δ`; finite and non-negative (`0`: a gamma, no mass).
22
+ * @param lower The censoring point, finite; `0` by default.
23
+ * @returns The distribution.
24
+ * @throws {RangeError} When a parameter is out of its range.
25
+ */
26
+ export function censoredShiftedGammaDistribution(shape, scale, shift, lower = 0) {
27
+ if (!(shape > 0 && shape < Number.POSITIVE_INFINITY) ||
28
+ !(scale > 0 && scale < Number.POSITIVE_INFINITY)) {
29
+ throw new RangeError(`censoredShiftedGammaDistribution: shape and scale must be positive and finite, got ${describeValue(shape)} and ${describeValue(scale)}`);
30
+ }
31
+ if (!(shift >= 0 && shift < Number.POSITIVE_INFINITY) ||
32
+ !Number.isFinite(lower)) {
33
+ throw new RangeError(`censoredShiftedGammaDistribution: shift must be finite and non-negative and lower finite, got ${describeValue(shift)} and ${describeValue(lower)}`);
34
+ }
35
+ const parent = gammaDistribution(shape, 1 / scale);
36
+ const pointMass = parent.cdf(shift);
37
+ const ratio = shift / scale;
38
+ // Partial moments E[Xʲ; X > δ] = θʲ·k(k+1)…(k+j−1)·Q(k + j, δ/θ).
39
+ const upper0 = regularizedIncompleteGammaUpper(shape, ratio);
40
+ const upper1 = shape * scale * regularizedIncompleteGammaUpper(shape + 1, ratio);
41
+ const upper2 = shape *
42
+ (shape + 1) *
43
+ scale *
44
+ scale *
45
+ regularizedIncompleteGammaUpper(shape + 2, ratio);
46
+ const excess = upper1 - shift * upper0;
47
+ const secondMoment = upper2 - 2 * shift * upper1 + shift * shift * upper0;
48
+ const quantile = (probability) => {
49
+ assertProbability_("censoredShiftedGammaDistribution", probability);
50
+ if (probability <= pointMass) {
51
+ return lower;
52
+ }
53
+ return lower + parent.quantile(probability) - shift;
54
+ };
55
+ return {
56
+ density: (x) => {
57
+ if (x < lower) {
58
+ return 0;
59
+ }
60
+ if (x === lower && shift > 0) {
61
+ return pointMass;
62
+ }
63
+ return parent.density(x - lower + shift);
64
+ },
65
+ cdf: (x) => (x < lower ? 0 : parent.cdf(x - lower + shift)),
66
+ survival: (x) => (x < lower ? 1 : parent.survival(x - lower + shift)),
67
+ quantile,
68
+ sample: (random) => inverseTransformSample_(quantile, random),
69
+ mean: lower + excess,
70
+ variance: Math.max(secondMoment - excess * excess, 0),
71
+ };
72
+ }
73
+ //# sourceMappingURL=censored-shifted-gamma-distribution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"censored-shifted-gamma-distribution.js","sourceRoot":"","sources":["../src/censored-shifted-gamma-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,+BAA+B,EAAE,MAAM,uBAAuB,CAAC;AAExE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,OAAO,EAAE,iBAAiB,EA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AM,CAAC;QAC7D,IAAI,EAAE,KAAK,GAAG,MAAM;QACpB,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,YAAY,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;KACrD,CAAC;AACH,CAAC"}
@@ -1,3 +1,5 @@
1
+ import { type Callable } from "@ac-kit/core";
2
+ import { type BootstrapOptions } from "./bootstrap.js";
1
3
  /** An interval estimate around a point estimate, at a confidence level. */
2
4
  export type ConfidenceInterval = {
3
5
  /** The point estimate the interval is about. */
@@ -59,4 +61,55 @@ export declare function meanConfidenceInterval(values: readonly number[], level?
59
61
  * 1)`.
60
62
  */
61
63
  export declare function medianConfidenceInterval(sorted: readonly number[], level?: number): ConfidenceInterval;
64
+ /**
65
+ * The estimators {@link confidenceInterval} computes an interval for: the sample
66
+ * `"mean"`, the `"median"`, or any statistic of a sample.
67
+ */
68
+ export type ConfidenceIntervalStatistic = "mean" | "median" | Callable<[readonly number[]], number>;
69
+ /**
70
+ * How {@link confidenceInterval} builds the interval:
71
+ *
72
+ * - `"t"` — Student's `mean ± t · s/√n` (the mean only; its default).
73
+ * - `"normal"` — `mean ± z · s/√n`, {@link meanConfidenceInterval} (the mean
74
+ * only).
75
+ * - `"order-statistic"` — the distribution-free order statistics of
76
+ * {@link medianConfidenceInterval} (the median only; its default).
77
+ * - `"bootstrap"` — {@link bootstrap}, for any statistic (the default for a
78
+ * statistic function).
79
+ */
80
+ export type ConfidenceIntervalMethod = "t" | "normal" | "order-statistic" | "bootstrap";
81
+ /** Options of {@link confidenceInterval}. */
82
+ export type ConfidenceIntervalOptions = {
83
+ /** Defaults to `"mean"`. */
84
+ readonly statistic?: ConfidenceIntervalStatistic;
85
+ /** Defaults to the statistic's default (see {@link ConfidenceIntervalMethod}). */
86
+ readonly method?: ConfidenceIntervalMethod;
87
+ /** Resamples, method and random source of the `"bootstrap"` method. */
88
+ readonly bootstrap?: Omit<BootstrapOptions, "level">;
89
+ };
90
+ /**
91
+ * A confidence interval at `level` for a statistic of `values`, by the method
92
+ * that suits it: Student's t for the mean, order statistics for the median, the
93
+ * bootstrap (BCa by default) for any statistic and as the
94
+ * no-distributional-assumption choice for the mean and the median too.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * confidenceInterval(timings); // Student's t interval for the mean
99
+ * confidenceInterval(timings, 0.99, { statistic: "median" });
100
+ * confidenceInterval(timings, 0.95, {
101
+ * statistic: (sample) => trimmedMean(sample, 0.1),
102
+ * bootstrap: { random: mulberry32(1) },
103
+ * });
104
+ * ```;
105
+ *
106
+ * @param values The sample, in any order. Not modified.
107
+ * @param level Confidence level in `(0, 1)`. Default `0.95`.
108
+ * @param options The statistic and the method.
109
+ * @returns The interval.
110
+ * @throws {RangeError} When the sample is too small for the method (two values,
111
+ * one for the order statistics), `level` is outside `(0, 1)`, or the method
112
+ * does not apply to the statistic.
113
+ */
114
+ export declare function confidenceInterval(values: readonly number[], level?: number, options?: ConfidenceIntervalOptions): ConfidenceInterval;
62
115
  //# sourceMappingURL=confidence-interval.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"confidence-interval.d.ts","sourceRoot":"","sources":["../src/confidence-interval.ts"],"names":[],"mappings":"AAOA,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;;;;;;;;;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,4BAA4B;IAC5B,QAAQ,CAAC,SAAS,CAAC,EAAE,2BAA2B,CAAC;IAEjD,kFAAkF;IAClF,QAAQ,CAAC,MAAM,CAAC,EAAE,wBAAwB,CAAC;IAE3C,uEAAuE;IACvE,QAAQ,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC;CACrD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,KAAK,SAAO,EACZ,OAAO,CAAC,EAAE,yBAAyB,GACjC,kBAAkB,CAsDpB"}
@@ -1,8 +1,10 @@
1
1
  import { describeValue } from "@ac-kit/core";
2
2
  import { mean } from "./average.js";
3
+ import { bootstrap } from "./bootstrap.js";
3
4
  import { sampleStandardDeviation } from "./dispersion.js";
4
5
  import { normalQuantile } from "./normal-quantile.js";
5
6
  import { quantile } from "./quantile.js";
7
+ import { studentTDistribution } from "./student-t-distribution.js";
6
8
  /**
7
9
  * Normal-approximation confidence interval for the mean of a sample: `mean ± z
8
10
  * · s / √n`, with `z` the `(1 + level) / 2` standard normal quantile and `s`
@@ -86,6 +88,77 @@ export function medianConfidenceInterval(sorted, level = 0.95) {
86
88
  level,
87
89
  };
88
90
  }
91
+ /**
92
+ * A confidence interval at `level` for a statistic of `values`, by the method
93
+ * that suits it: Student's t for the mean, order statistics for the median, the
94
+ * bootstrap (BCa by default) for any statistic and as the
95
+ * no-distributional-assumption choice for the mean and the median too.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * confidenceInterval(timings); // Student's t interval for the mean
100
+ * confidenceInterval(timings, 0.99, { statistic: "median" });
101
+ * confidenceInterval(timings, 0.95, {
102
+ * statistic: (sample) => trimmedMean(sample, 0.1),
103
+ * bootstrap: { random: mulberry32(1) },
104
+ * });
105
+ * ```;
106
+ *
107
+ * @param values The sample, in any order. Not modified.
108
+ * @param level Confidence level in `(0, 1)`. Default `0.95`.
109
+ * @param options The statistic and the method.
110
+ * @returns The interval.
111
+ * @throws {RangeError} When the sample is too small for the method (two values,
112
+ * one for the order statistics), `level` is outside `(0, 1)`, or the method
113
+ * does not apply to the statistic.
114
+ */
115
+ export function confidenceInterval(values, level = 0.95, options) {
116
+ checkConfidenceLevel(level);
117
+ const statistic = options?.statistic ?? "mean";
118
+ const method = options?.method ??
119
+ (statistic === "mean"
120
+ ? "t"
121
+ : statistic === "median"
122
+ ? "order-statistic"
123
+ : "bootstrap");
124
+ if (method === "bootstrap") {
125
+ const estimator = statistic === "mean"
126
+ ? meanOf_
127
+ : statistic === "median"
128
+ ? medianOf_
129
+ : statistic;
130
+ return bootstrap(values, estimator, { ...options?.bootstrap, level });
131
+ }
132
+ if (statistic === "mean" && method === "normal") {
133
+ return meanConfidenceInterval(values, level);
134
+ }
135
+ if (statistic === "mean" && method === "t") {
136
+ if (values.length < 2) {
137
+ throw new RangeError("confidenceInterval: the sample must have at least two values");
138
+ }
139
+ const estimate = mean(values);
140
+ const standardError = sampleStandardDeviation(values) / Math.sqrt(values.length);
141
+ const halfWidth = studentTDistribution(values.length - 1).quantile((1 + level) / 2) *
142
+ standardError;
143
+ return {
144
+ estimate,
145
+ lower: estimate - halfWidth,
146
+ upper: estimate + halfWidth,
147
+ halfWidth,
148
+ level,
149
+ };
150
+ }
151
+ if (statistic === "median" && method === "order-statistic") {
152
+ return medianConfidenceInterval([...values].sort((left, right) => left - right), level);
153
+ }
154
+ throw new RangeError(`confidenceInterval: the "${method}" method does not apply to ${typeof statistic === "string" ? `the ${statistic}` : "a statistic function"}`);
155
+ }
156
+ function meanOf_(values) {
157
+ return mean(values);
158
+ }
159
+ function medianOf_(values) {
160
+ return quantile([...values].sort((left, right) => left - right), 0.5);
161
+ }
89
162
  function checkConfidenceLevel(level) {
90
163
  if (!(level > 0 && level < 1)) {
91
164
  throw new RangeError(`the confidence level must be in (0, 1), got ${describeValue(level)}`);
@@ -1 +1 @@
1
- {"version":3,"file":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CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,GAAG,KAAK,CAAC,EAC/C,KAAK,CACL,CAAC;IACH,CAAC;IAED,MAAM,IAAI,UAAU,CACnB,4BAA4B,MAAM,8BAA8B,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,SAAS,EAAE,CAAC,CAAC,CAAC,sBAAsB,EAAE,CAC7I,CAAC;AACH,CAAC;AAED,SAAS,OAAO,CAAC,MAAyB;IACzC,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,SAAS,CAAC,MAAyB;IAC3C,OAAO,QAAQ,CACd,CAAC,GAAG,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,GAAG,KAAK,CAAC,EAC/C,GAAG,CACH,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,aAAa,CAAC,KAAK,CAAC,EAAE,CACrE,CAAC;IACH,CAAC;AACF,CAAC"}
@@ -11,4 +11,45 @@
11
11
  * @throws {RangeError} If `xValues` and `yValues` have different lengths.
12
12
  */
13
13
  export declare function pearsonCorrelation(xValues: readonly number[], yValues: readonly number[]): number;
14
+ /**
15
+ * Spearman's rank correlation coefficient ρ of paired `xValues` and `yValues`:
16
+ * Pearson's coefficient of their ranks, so it measures how nearly a monotone
17
+ * function — not only a line — relates them, from -1 through 0 to
18
+ *
19
+ * 1. Tied values take the average of the ranks they span (`rankBy` from
20
+ * `@ac-kit/algo` with `ties: "average"`), which is SciPy's `spearmanr` and
21
+ * R's `cor(method = "spearman")`; the `1 − 6Σd²/(n(n² − 1))` shortcut is
22
+ * exact only without ties and is not used.
23
+ *
24
+ * Time O(n log n), memory O(n).
25
+ *
26
+ * @param xValues Samples. Not modified.
27
+ * @param yValues Samples paired by index. Not modified.
28
+ * @returns The coefficient; `0` for fewer than two pairs or when either side is
29
+ * constant, as {@link pearsonCorrelation}; `NaN` when a value is `NaN`.
30
+ * @throws {RangeError} If `xValues` and `yValues` have different lengths.
31
+ */
32
+ export declare function spearmanCorrelation(xValues: readonly number[], yValues: readonly number[]): number;
33
+ /**
34
+ * Kendall's rank correlation coefficient τ_b of paired `xValues` and `yValues`:
35
+ * the excess of concordant over discordant pairs, `(n_c − n_d) / √((n₀ − n₁)(n₀
36
+ * − n₂))`, with `n₀ = n(n − 1)/2` and `n₁`, `n₂` the pairs tied in `x` and in
37
+ * `y`. The tie correction makes it reach ±1 for a monotone relation with ties
38
+ * (where τ_a does not); it is SciPy's `kendalltau` default and R's `cor(method
39
+ * = "kendall")`.
40
+ *
41
+ * Knight's algorithm: the pairs sorted by `x` then `y`, then a merge sort by
42
+ * `y` counting the exchanges, which are the discordant pairs. Values are
43
+ * compared through their dense ranks (`rankBy` from `@ac-kit/algo`), so `-0`
44
+ * ties `0`.
45
+ *
46
+ * Time O(n log n), memory O(n).
47
+ *
48
+ * @param xValues Samples. Not modified.
49
+ * @param yValues Samples paired by index. Not modified.
50
+ * @returns The coefficient; `0` for fewer than two pairs or when either side is
51
+ * constant, as {@link pearsonCorrelation}; `NaN` when a value is `NaN`.
52
+ * @throws {RangeError} If `xValues` and `yValues` have different lengths.
53
+ */
54
+ export declare function kendallTau(xValues: readonly number[], yValues: readonly number[]): number;
14
55
  //# sourceMappingURL=correlation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"correlation.d.ts","sourceRoot":"","sources":["../src/correlation.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CAuBR"}
1
+ {"version":3,"file":"correlation.d.ts","sourceRoot":"","sources":["../src/correlation.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CACjC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CASR;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAClC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CAcR;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,UAAU,CACzB,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CA8ER"}
@@ -1,3 +1,4 @@
1
+ import { rankBy } from "@ac-kit/algo";
1
2
  import { PreciseSum } from "@ac-kit/core";
2
3
  import { mean } from "./average.js";
3
4
  /**
@@ -19,8 +20,134 @@ export function pearsonCorrelation(xValues, yValues) {
19
20
  if (xValues.length < 2) {
20
21
  return 0;
21
22
  }
22
- const meanX = mean(xValues);
23
- const meanY = mean(yValues);
23
+ return correlationAboutMeans_(xValues, yValues, mean(xValues), mean(yValues));
24
+ }
25
+ /**
26
+ * Spearman's rank correlation coefficient ρ of paired `xValues` and `yValues`:
27
+ * Pearson's coefficient of their ranks, so it measures how nearly a monotone
28
+ * function — not only a line — relates them, from -1 through 0 to
29
+ *
30
+ * 1. Tied values take the average of the ranks they span (`rankBy` from
31
+ * `@ac-kit/algo` with `ties: "average"`), which is SciPy's `spearmanr` and
32
+ * R's `cor(method = "spearman")`; the `1 − 6Σd²/(n(n² − 1))` shortcut is
33
+ * exact only without ties and is not used.
34
+ *
35
+ * Time O(n log n), memory O(n).
36
+ *
37
+ * @param xValues Samples. Not modified.
38
+ * @param yValues Samples paired by index. Not modified.
39
+ * @returns The coefficient; `0` for fewer than two pairs or when either side is
40
+ * constant, as {@link pearsonCorrelation}; `NaN` when a value is `NaN`.
41
+ * @throws {RangeError} If `xValues` and `yValues` have different lengths.
42
+ */
43
+ export function spearmanCorrelation(xValues, yValues) {
44
+ if (xValues.length !== yValues.length) {
45
+ throw new RangeError("xValues and yValues must have the same length");
46
+ }
47
+ const count = xValues.length;
48
+ if (count < 2) {
49
+ return 0;
50
+ }
51
+ const ranksX = rankBy(xValues, identity_);
52
+ const ranksY = rankBy(yValues, identity_);
53
+ // Average ranks always sum to n(n + 1)/2, so their mean is exact.
54
+ const meanRank = (count + 1) / 2;
55
+ return correlationAboutMeans_(ranksX, ranksY, meanRank, meanRank);
56
+ }
57
+ /**
58
+ * Kendall's rank correlation coefficient τ_b of paired `xValues` and `yValues`:
59
+ * the excess of concordant over discordant pairs, `(n_c − n_d) / √((n₀ − n₁)(n₀
60
+ * − n₂))`, with `n₀ = n(n − 1)/2` and `n₁`, `n₂` the pairs tied in `x` and in
61
+ * `y`. The tie correction makes it reach ±1 for a monotone relation with ties
62
+ * (where τ_a does not); it is SciPy's `kendalltau` default and R's `cor(method
63
+ * = "kendall")`.
64
+ *
65
+ * Knight's algorithm: the pairs sorted by `x` then `y`, then a merge sort by
66
+ * `y` counting the exchanges, which are the discordant pairs. Values are
67
+ * compared through their dense ranks (`rankBy` from `@ac-kit/algo`), so `-0`
68
+ * ties `0`.
69
+ *
70
+ * Time O(n log n), memory O(n).
71
+ *
72
+ * @param xValues Samples. Not modified.
73
+ * @param yValues Samples paired by index. Not modified.
74
+ * @returns The coefficient; `0` for fewer than two pairs or when either side is
75
+ * constant, as {@link pearsonCorrelation}; `NaN` when a value is `NaN`.
76
+ * @throws {RangeError} If `xValues` and `yValues` have different lengths.
77
+ */
78
+ export function kendallTau(xValues, yValues) {
79
+ if (xValues.length !== yValues.length) {
80
+ throw new RangeError("xValues and yValues must have the same length");
81
+ }
82
+ const count = xValues.length;
83
+ if (count < 2) {
84
+ return 0;
85
+ }
86
+ const ranksX = rankBy(xValues, identity_, { ties: "dense" });
87
+ const ranksY = rankBy(yValues, identity_, { ties: "dense" });
88
+ for (let index = 0; index < count; index++) {
89
+ if (ranksX[index] !== ranksX[index] || ranksY[index] !== ranksY[index]) {
90
+ return Number.NaN;
91
+ }
92
+ }
93
+ const order = new Int32Array(count);
94
+ for (let index = 0; index < count; index++) {
95
+ order[index] = index;
96
+ }
97
+ order.sort((left, right) => ranksX[left] - ranksX[right] || ranksY[left] - ranksY[right]);
98
+ let tiedX = 0;
99
+ let tiedBoth = 0;
100
+ let runX = 1;
101
+ let runBoth = 1;
102
+ for (let position = 1; position < count; position++) {
103
+ const previous = order[position - 1];
104
+ const current = order[position];
105
+ if (ranksX[previous] === ranksX[current]) {
106
+ runX++;
107
+ if (ranksY[previous] === ranksY[current]) {
108
+ runBoth++;
109
+ }
110
+ else {
111
+ tiedBoth += (runBoth * (runBoth - 1)) / 2;
112
+ runBoth = 1;
113
+ }
114
+ }
115
+ else {
116
+ tiedX += (runX * (runX - 1)) / 2;
117
+ tiedBoth += (runBoth * (runBoth - 1)) / 2;
118
+ runX = 1;
119
+ runBoth = 1;
120
+ }
121
+ }
122
+ tiedX += (runX * (runX - 1)) / 2;
123
+ tiedBoth += (runBoth * (runBoth - 1)) / 2;
124
+ const discordant = mergeSortCountingExchanges_(order, new Int32Array(count), ranksY);
125
+ let tiedY = 0;
126
+ let runY = 1;
127
+ for (let position = 1; position < count; position++) {
128
+ if (ranksY[order[position - 1]] === ranksY[order[position]]) {
129
+ runY++;
130
+ }
131
+ else {
132
+ tiedY += (runY * (runY - 1)) / 2;
133
+ runY = 1;
134
+ }
135
+ }
136
+ tiedY += (runY * (runY - 1)) / 2;
137
+ const pairs = (count * (count - 1)) / 2;
138
+ const untiedX = pairs - tiedX;
139
+ const untiedY = pairs - tiedY;
140
+ if (untiedX === 0 || untiedY === 0) {
141
+ return 0;
142
+ }
143
+ // n_c − n_d = n₀ − n₁ − n₂ + n₃ − 2·n_d, every term an exact integer.
144
+ const excess = pairs - tiedX - tiedY + tiedBoth - 2 * discordant;
145
+ return excess / Math.sqrt(untiedX) / Math.sqrt(untiedY);
146
+ }
147
+ function identity_(value) {
148
+ return value;
149
+ }
150
+ function correlationAboutMeans_(xValues, yValues, meanX, meanY) {
24
151
  const covariance = new PreciseSum();
25
152
  const varianceX = new PreciseSum();
26
153
  const varianceY = new PreciseSum();
@@ -34,4 +161,43 @@ export function pearsonCorrelation(xValues, yValues) {
34
161
  const spread = Math.sqrt(varianceX.value * varianceY.value);
35
162
  return spread === 0 ? 0 : covariance.value / spread;
36
163
  }
164
+ // Bottom-up stable merge sort of `order` by `keys`, returning the number of
165
+ // pairs it exchanges: those whose key decreases along the input order.
166
+ function mergeSortCountingExchanges_(order, scratch, keys) {
167
+ const count = order.length;
168
+ let source = order;
169
+ let target = scratch;
170
+ let exchanges = 0;
171
+ for (let width = 1; width < count; width *= 2) {
172
+ for (let low = 0; low < count; low += 2 * width) {
173
+ const middle = Math.min(low + width, count);
174
+ const high = Math.min(low + 2 * width, count);
175
+ let left = low;
176
+ let right = middle;
177
+ let output = low;
178
+ while (left < middle && right < high) {
179
+ if (keys[source[right]] < keys[source[left]]) {
180
+ exchanges += middle - left;
181
+ target[output++] = source[right++];
182
+ }
183
+ else {
184
+ target[output++] = source[left++];
185
+ }
186
+ }
187
+ while (left < middle) {
188
+ target[output++] = source[left++];
189
+ }
190
+ while (right < high) {
191
+ target[output++] = source[right++];
192
+ }
193
+ }
194
+ const swap = source;
195
+ source = target;
196
+ target = swap;
197
+ }
198
+ if (source !== order) {
199
+ order.set(source);
200
+ }
201
+ return exchanges;
202
+ }
37
203
  //# sourceMappingURL=correlation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"correlation.js","sourceRoot":"","sources":["../src/correlation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CACjC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