@ac-kit/math-stats 0.5.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 (165) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/README.md +86 -6
  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/_histogram-rules.d.ts +15 -0
  12. package/dist/_histogram-rules.d.ts.map +1 -0
  13. package/dist/_histogram-rules.js +208 -0
  14. package/dist/_histogram-rules.js.map +1 -0
  15. package/dist/_incomplete-gamma-shape-derivative.d.ts +17 -0
  16. package/dist/_incomplete-gamma-shape-derivative.d.ts.map +1 -0
  17. package/dist/_incomplete-gamma-shape-derivative.js +90 -0
  18. package/dist/_incomplete-gamma-shape-derivative.js.map +1 -0
  19. package/dist/_logistic.d.ts +14 -0
  20. package/dist/_logistic.d.ts.map +1 -0
  21. package/dist/_logistic.js +29 -0
  22. package/dist/_logistic.js.map +1 -0
  23. package/dist/_normal-tail.d.ts +43 -0
  24. package/dist/_normal-tail.d.ts.map +1 -0
  25. package/dist/_normal-tail.js +124 -0
  26. package/dist/_normal-tail.js.map +1 -0
  27. package/dist/_rank-test.d.ts +71 -0
  28. package/dist/_rank-test.d.ts.map +1 -0
  29. package/dist/_rank-test.js +130 -0
  30. package/dist/_rank-test.js.map +1 -0
  31. package/dist/average.js +2 -2
  32. package/dist/average.js.map +1 -1
  33. package/dist/bootstrap.d.ts +67 -0
  34. package/dist/bootstrap.d.ts.map +1 -0
  35. package/dist/bootstrap.js +154 -0
  36. package/dist/bootstrap.js.map +1 -0
  37. package/dist/censored-logistic-distribution.d.ts +23 -0
  38. package/dist/censored-logistic-distribution.d.ts.map +1 -0
  39. package/dist/censored-logistic-distribution.js +63 -0
  40. package/dist/censored-logistic-distribution.js.map +1 -0
  41. package/dist/censored-shifted-gamma-distribution.d.ts +23 -0
  42. package/dist/censored-shifted-gamma-distribution.d.ts.map +1 -0
  43. package/dist/censored-shifted-gamma-distribution.js +73 -0
  44. package/dist/censored-shifted-gamma-distribution.js.map +1 -0
  45. package/dist/confidence-interval.d.ts +53 -0
  46. package/dist/confidence-interval.d.ts.map +1 -1
  47. package/dist/confidence-interval.js +73 -0
  48. package/dist/confidence-interval.js.map +1 -1
  49. package/dist/correlation.d.ts +41 -0
  50. package/dist/correlation.d.ts.map +1 -1
  51. package/dist/correlation.js +168 -2
  52. package/dist/correlation.js.map +1 -1
  53. package/dist/descriptive.d.ts +129 -0
  54. package/dist/descriptive.d.ts.map +1 -0
  55. package/dist/descriptive.js +289 -0
  56. package/dist/descriptive.js.map +1 -0
  57. package/dist/forecast/_forecast-checks.d.ts +30 -0
  58. package/dist/forecast/_forecast-checks.d.ts.map +1 -0
  59. package/dist/forecast/_forecast-checks.js +119 -0
  60. package/dist/forecast/_forecast-checks.js.map +1 -0
  61. package/dist/forecast/_score-kernels.d.ts +63 -0
  62. package/dist/forecast/_score-kernels.d.ts.map +1 -0
  63. package/dist/forecast/_score-kernels.js +264 -0
  64. package/dist/forecast/_score-kernels.js.map +1 -0
  65. package/dist/forecast/brier-score.d.ts +102 -0
  66. package/dist/forecast/brier-score.d.ts.map +1 -0
  67. package/dist/forecast/brier-score.js +163 -0
  68. package/dist/forecast/brier-score.js.map +1 -0
  69. package/dist/forecast/crps-closed-form.d.ts +64 -0
  70. package/dist/forecast/crps-closed-form.d.ts.map +1 -0
  71. package/dist/forecast/crps-closed-form.js +106 -0
  72. package/dist/forecast/crps-closed-form.js.map +1 -0
  73. package/dist/forecast/crps-ensemble.d.ts +53 -0
  74. package/dist/forecast/crps-ensemble.d.ts.map +1 -0
  75. package/dist/forecast/crps-ensemble.js +121 -0
  76. package/dist/forecast/crps-ensemble.js.map +1 -0
  77. package/dist/forecast/emos.d.ts +158 -0
  78. package/dist/forecast/emos.d.ts.map +1 -0
  79. package/dist/forecast/emos.js +397 -0
  80. package/dist/forecast/emos.js.map +1 -0
  81. package/dist/forecast/exceedance-probability.d.ts +54 -0
  82. package/dist/forecast/exceedance-probability.d.ts.map +1 -0
  83. package/dist/forecast/exceedance-probability.js +125 -0
  84. package/dist/forecast/exceedance-probability.js.map +1 -0
  85. package/dist/forecast/log-score.d.ts +18 -0
  86. package/dist/forecast/log-score.d.ts.map +1 -0
  87. package/dist/forecast/log-score.js +19 -0
  88. package/dist/forecast/log-score.js.map +1 -0
  89. package/dist/forecast/logistic-regression.d.ts +123 -0
  90. package/dist/forecast/logistic-regression.d.ts.map +1 -0
  91. package/dist/forecast/logistic-regression.js +282 -0
  92. package/dist/forecast/logistic-regression.js.map +1 -0
  93. package/dist/forecast/rank-histogram.d.ts +32 -0
  94. package/dist/forecast/rank-histogram.d.ts.map +1 -0
  95. package/dist/forecast/rank-histogram.js +63 -0
  96. package/dist/forecast/rank-histogram.js.map +1 -0
  97. package/dist/forecast/reliability-diagram.d.ts +70 -0
  98. package/dist/forecast/reliability-diagram.d.ts.map +1 -0
  99. package/dist/forecast/reliability-diagram.js +126 -0
  100. package/dist/forecast/reliability-diagram.js.map +1 -0
  101. package/dist/forecast/roc-curve.d.ts +58 -0
  102. package/dist/forecast/roc-curve.d.ts.map +1 -0
  103. package/dist/forecast/roc-curve.js +124 -0
  104. package/dist/forecast/roc-curve.js.map +1 -0
  105. package/dist/hierarchical-cluster.d.ts.map +1 -1
  106. package/dist/hierarchical-cluster.js +10 -5
  107. package/dist/hierarchical-cluster.js.map +1 -1
  108. package/dist/histogram-edges.d.ts +92 -0
  109. package/dist/histogram-edges.d.ts.map +1 -0
  110. package/dist/histogram-edges.js +325 -0
  111. package/dist/histogram-edges.js.map +1 -0
  112. package/dist/histogram.d.ts +55 -21
  113. package/dist/histogram.d.ts.map +1 -1
  114. package/dist/histogram.js +56 -83
  115. package/dist/histogram.js.map +1 -1
  116. package/dist/index.d.ts +23 -0
  117. package/dist/index.d.ts.map +1 -1
  118. package/dist/index.js +23 -0
  119. package/dist/index.js.map +1 -1
  120. package/dist/kmeans.d.ts.map +1 -1
  121. package/dist/kmeans.js +11 -6
  122. package/dist/kmeans.js.map +1 -1
  123. package/dist/kmedoids.js +2 -1
  124. package/dist/kmedoids.js.map +1 -1
  125. package/dist/mann-whitney-u.d.ts +27 -0
  126. package/dist/mann-whitney-u.d.ts.map +1 -0
  127. package/dist/mann-whitney-u.js +94 -0
  128. package/dist/mann-whitney-u.js.map +1 -0
  129. package/dist/multiple-regression.d.ts +123 -0
  130. package/dist/multiple-regression.d.ts.map +1 -0
  131. package/dist/multiple-regression.js +208 -0
  132. package/dist/multiple-regression.js.map +1 -0
  133. package/dist/principal-components.d.ts +66 -0
  134. package/dist/principal-components.d.ts.map +1 -0
  135. package/dist/principal-components.js +105 -0
  136. package/dist/principal-components.js.map +1 -0
  137. package/dist/quantile.d.ts +113 -5
  138. package/dist/quantile.d.ts.map +1 -1
  139. package/dist/quantile.js +318 -19
  140. package/dist/quantile.js.map +1 -1
  141. package/dist/regression.d.ts +46 -0
  142. package/dist/regression.d.ts.map +1 -1
  143. package/dist/regression.js +99 -1
  144. package/dist/regression.js.map +1 -1
  145. package/dist/running-statistics.d.ts +103 -0
  146. package/dist/running-statistics.d.ts.map +1 -0
  147. package/dist/running-statistics.js +283 -0
  148. package/dist/running-statistics.js.map +1 -0
  149. package/dist/sample.d.ts +29 -0
  150. package/dist/sample.d.ts.map +1 -0
  151. package/dist/sample.js +39 -0
  152. package/dist/sample.js.map +1 -0
  153. package/dist/t-digest.d.ts +136 -0
  154. package/dist/t-digest.d.ts.map +1 -0
  155. package/dist/t-digest.js +424 -0
  156. package/dist/t-digest.js.map +1 -0
  157. package/dist/truncated-normal-distribution.d.ts +28 -0
  158. package/dist/truncated-normal-distribution.d.ts.map +1 -0
  159. package/dist/truncated-normal-distribution.js +198 -0
  160. package/dist/truncated-normal-distribution.js.map +1 -0
  161. package/dist/wilcoxon-signed-rank.d.ts +42 -0
  162. package/dist/wilcoxon-signed-rank.d.ts.map +1 -0
  163. package/dist/wilcoxon-signed-rank.js +117 -0
  164. package/dist/wilcoxon-signed-rank.js.map +1 -0
  165. package/package.json +14 -10
@@ -0,0 +1,136 @@
1
+ /**
2
+ * The scale function `k(q)` that sizes the centroids of a {@link TDigest}
3
+ * (Dunning and Ertl 2019, §2.1), with `Z(n) = 4 ln(n/δ) + 24` (`+ 21` for k₃):
4
+ *
5
+ * - `"k1"` — `δ/(2π) · asin(2q − 1)`: centroid sizes `∝ √(q(1 − q))`, a bounded
6
+ * count; the error in the extreme tails is then about `1/δ²` absolute.
7
+ * - `"k2"` — `δ/Z · ln(q/(1 − q))`: sizes `∝ q(1 − q)`, a **relative** error in
8
+ * the tails, extremes kept as single values (the default, as in the reference
9
+ * implementation).
10
+ * - `"k3"` — `δ/Z · ln(2q)` below the median, mirrored above: like k₂ with larger
11
+ * centroids in the middle.
12
+ */
13
+ export type TDigestScale = "k1" | "k2" | "k3";
14
+ /** Options of {@link TDigest}. */
15
+ export type TDigestOptions = {
16
+ /**
17
+ * The compression `δ`, at least `10`: the digest keeps fewer than about `δ`
18
+ * centroids, and its quantile error shrinks as `1/δ`. Defaults to `100`,
19
+ * about 1.5 kB with the buffer and rank errors near `1e−3` in the middle.
20
+ */
21
+ readonly compression?: number;
22
+ /** Defaults to `"k2"`. */
23
+ readonly scale?: TDigestScale;
24
+ };
25
+ /** Centroids exported by {@link TDigest.centroids}, ascending by mean. */
26
+ export type TDigestCentroids = {
27
+ readonly means: Float64Array;
28
+ readonly weights: Float64Array;
29
+ };
30
+ /**
31
+ * A t-digest (Dunning and Ertl, "Computing extremely accurate quantiles using
32
+ * t-digests", 2019): a streaming summary of a distribution that estimates any
33
+ * quantile or cumulative probability from a fixed few hundred numbers, most
34
+ * accurate in the tails, and that **merges**: digests of parts of the data
35
+ * (shards, time windows, workers) combine into the digest of the whole.
36
+ *
37
+ * The merging variant: values gather in a buffer, and when it fills the buffer
38
+ * and the centroids are merged in one pass that lets a centroid grow only while
39
+ * it spans at most one unit of the scale function `k` ({@link TDigestScale}),
40
+ * so centroids are small near `q = 0` and `q = 1` and large in the middle.
41
+ * Passes alternate direction, which removes the bias of always merging the same
42
+ * way. The minimum and maximum are kept exactly.
43
+ *
44
+ * Estimates come from the piecewise-linear cumulative weight through `(min,
45
+ * 0)`, each centroid's mean at the weight below it plus half its own, and
46
+ * `(max, n)`: {@link quantile} is its inverse and {@link cdf} the function
47
+ * itself, so the two agree. While every value is still its own centroid (up to
48
+ * about `δ / 3` values with the default scale), {@link quantile} is the exact
49
+ * Hazen (type 5) sample quantile.
50
+ *
51
+ * `add` is amortised O(log δ); a compression O(δ log δ). Memory O(δ), allocated
52
+ * by the constructor.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const digest = new TDigest();
57
+ * for (const latency of latencies) digest.add(latency);
58
+ * digest.quantile(0.99); // the p99 latency, to a fraction of a percent
59
+ * ```;
60
+ */
61
+ export declare class TDigest {
62
+ /** The compression `δ`. */
63
+ readonly compression: number;
64
+ /** The scale function. */
65
+ readonly scale: TDigestScale;
66
+ private readonly capacity_;
67
+ private readonly bufferCapacity_;
68
+ private readonly means_;
69
+ private readonly weights_;
70
+ private readonly bufferMeans_;
71
+ private readonly bufferWeights_;
72
+ private readonly order_;
73
+ private readonly scratchMeans_;
74
+ private readonly scratchWeights_;
75
+ private centroids_;
76
+ private buffered_;
77
+ private total_;
78
+ private bufferedWeight_;
79
+ private min_;
80
+ private max_;
81
+ private reversed_;
82
+ /**
83
+ * @throws {RangeError} When `compression` is not a finite number ≥ 10, or
84
+ * `scale` is not a {@link TDigestScale}.
85
+ */
86
+ constructor(options?: TDigestOptions);
87
+ /**
88
+ * Rebuilds a digest from exported {@link centroids} (and its extremes), to
89
+ * restore or merge one that was serialised.
90
+ *
91
+ * @throws {RangeError} When the arrays differ in length, a mean is not finite
92
+ * or outside `[min, max]`, a weight is not finite and positive, or
93
+ * `compression` is invalid.
94
+ */
95
+ static fromCentroids(centroids: TDigestCentroids, min: number, max: number, options?: TDigestOptions): TDigest;
96
+ /** The total weight added: the count of values when every weight is 1. */
97
+ get count(): number;
98
+ /** The smallest value added, `NaN` when empty. */
99
+ get min(): number;
100
+ /** The largest value added, `NaN` when empty. */
101
+ get max(): number;
102
+ /**
103
+ * Adds `value` with `weight` (a count of occurrences, or any positive
104
+ * importance).
105
+ *
106
+ * @throws {RangeError} When `value` is not finite or `weight` is not finite
107
+ * and positive; the digest is left unchanged.
108
+ */
109
+ add(value: number, weight?: number): void;
110
+ /**
111
+ * Merges `other` into this digest, which then summarises both; `other` is not
112
+ * changed. Merging digests of parts gives about the accuracy of one digest of
113
+ * the whole.
114
+ */
115
+ merge(other: TDigest): void;
116
+ /** Empties the digest, keeping its storage. */
117
+ reset(): void;
118
+ /** A copy of the centroids, ascending by mean, for serialisation. */
119
+ centroids(): TDigestCentroids;
120
+ /**
121
+ * The estimated `probability` quantile: `min` at `0`, `max` at `1`; `NaN`
122
+ * when empty.
123
+ *
124
+ * @throws {RangeError} When `probability` is outside `[0, 1]`.
125
+ */
126
+ quantile(probability: number): number;
127
+ /**
128
+ * The estimated cumulative probability `P(X ≤ x)`: `0` below `min`, `1` above
129
+ * `max`, the weight below plus half the weight of the centroids whose mean is
130
+ * exactly `x` (a mid-rank); `NaN` when empty or for a `NaN` `x`.
131
+ */
132
+ cdf(x: number): number;
133
+ /** Merges the buffer into the centroids, if anything is buffered. */
134
+ private compress_;
135
+ }
136
+ //# sourceMappingURL=t-digest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"t-digest.d.ts","sourceRoot":"","sources":["../src/t-digest.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAE9C,kCAAkC;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,EAAE,gBAAgB,EAC3B,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,cAAc,GACtB,OAAO,CAsBT;IAED,0EAA0E;IAC1E,IAAI,KAAK,IAAI,MAAM,CAElB;IAED,kDAAkD;IAClD,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED,iDAAiD;IACjD,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED;;;;;;OAMG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,IAAI,CAwBnC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAY1B;IAED,+CAA+C;IAC/C,KAAK,IAAI,IAAI,CAQZ;IAED,qEAAqE;IACrE,SAAS,IAAI,gBAAgB,CAM5B;IAED;;;;;OAKG;IACH,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAyCpC;IAED;;;;OAIG;IACH,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAoDrB;IAED,qEAAqE;IACrE,OAAO,CAAC,SAAS;CA+FjB"}
@@ -0,0 +1,424 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ /**
3
+ * A t-digest (Dunning and Ertl, "Computing extremely accurate quantiles using
4
+ * t-digests", 2019): a streaming summary of a distribution that estimates any
5
+ * quantile or cumulative probability from a fixed few hundred numbers, most
6
+ * accurate in the tails, and that **merges**: digests of parts of the data
7
+ * (shards, time windows, workers) combine into the digest of the whole.
8
+ *
9
+ * The merging variant: values gather in a buffer, and when it fills the buffer
10
+ * and the centroids are merged in one pass that lets a centroid grow only while
11
+ * it spans at most one unit of the scale function `k` ({@link TDigestScale}),
12
+ * so centroids are small near `q = 0` and `q = 1` and large in the middle.
13
+ * Passes alternate direction, which removes the bias of always merging the same
14
+ * way. The minimum and maximum are kept exactly.
15
+ *
16
+ * Estimates come from the piecewise-linear cumulative weight through `(min,
17
+ * 0)`, each centroid's mean at the weight below it plus half its own, and
18
+ * `(max, n)`: {@link quantile} is its inverse and {@link cdf} the function
19
+ * itself, so the two agree. While every value is still its own centroid (up to
20
+ * about `δ / 3` values with the default scale), {@link quantile} is the exact
21
+ * Hazen (type 5) sample quantile.
22
+ *
23
+ * `add` is amortised O(log δ); a compression O(δ log δ). Memory O(δ), allocated
24
+ * by the constructor.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const digest = new TDigest();
29
+ * for (const latency of latencies) digest.add(latency);
30
+ * digest.quantile(0.99); // the p99 latency, to a fraction of a percent
31
+ * ```;
32
+ */
33
+ export class TDigest {
34
+ /** The compression `δ`. */
35
+ compression;
36
+ /** The scale function. */
37
+ scale;
38
+ capacity_;
39
+ bufferCapacity_;
40
+ means_;
41
+ weights_;
42
+ bufferMeans_;
43
+ bufferWeights_;
44
+ order_;
45
+ scratchMeans_;
46
+ scratchWeights_;
47
+ centroids_ = 0;
48
+ buffered_ = 0;
49
+ total_ = 0;
50
+ bufferedWeight_ = 0;
51
+ min_ = Number.POSITIVE_INFINITY;
52
+ max_ = Number.NEGATIVE_INFINITY;
53
+ reversed_ = false;
54
+ /**
55
+ * @throws {RangeError} When `compression` is not a finite number ≥ 10, or
56
+ * `scale` is not a {@link TDigestScale}.
57
+ */
58
+ constructor(options) {
59
+ const compression = options?.compression ?? 100;
60
+ if (!(compression >= 10 && compression < Number.POSITIVE_INFINITY)) {
61
+ throw new RangeError(`TDigest: compression must be a finite number of at least 10, got ${describeValue(compression)}`);
62
+ }
63
+ const scale = options?.scale ?? "k2";
64
+ if (scale !== "k1" && scale !== "k2" && scale !== "k3") {
65
+ throw new RangeError(`TDigest: scale must be "k1", "k2" or "k3", got ${describeValue(scale)}`);
66
+ }
67
+ this.compression = compression;
68
+ this.scale = scale;
69
+ // Each pair of neighbouring centroids spans more than one unit of k,
70
+ // whose range is δ/2 for k₁ and below δ/2 for k₂ and k₃ (as Z grows
71
+ // with ln n): about δ centroids, twice that a safe bound.
72
+ this.capacity_ = 2 * Math.ceil(compression) + 8;
73
+ this.bufferCapacity_ = 5 * Math.ceil(compression);
74
+ this.means_ = new Float64Array(this.capacity_);
75
+ this.weights_ = new Float64Array(this.capacity_);
76
+ this.bufferMeans_ = new Float64Array(this.bufferCapacity_);
77
+ this.bufferWeights_ = new Float64Array(this.bufferCapacity_);
78
+ this.order_ = new Int32Array(this.bufferCapacity_);
79
+ const merged = this.capacity_ + this.bufferCapacity_;
80
+ this.scratchMeans_ = new Float64Array(merged);
81
+ this.scratchWeights_ = new Float64Array(merged);
82
+ }
83
+ /**
84
+ * Rebuilds a digest from exported {@link centroids} (and its extremes), to
85
+ * restore or merge one that was serialised.
86
+ *
87
+ * @throws {RangeError} When the arrays differ in length, a mean is not finite
88
+ * or outside `[min, max]`, a weight is not finite and positive, or
89
+ * `compression` is invalid.
90
+ */
91
+ static fromCentroids(centroids, min, max, options) {
92
+ const { means, weights } = centroids;
93
+ if (means.length !== weights.length) {
94
+ throw new RangeError("TDigest.fromCentroids: means and weights must have the same length");
95
+ }
96
+ const digest = new TDigest(options);
97
+ for (let index = 0; index < means.length; index++) {
98
+ const mean = means[index];
99
+ if (!(mean >= min && mean <= max)) {
100
+ throw new RangeError(`TDigest.fromCentroids: mean ${describeValue(mean)} lies outside [${min}, ${max}]`);
101
+ }
102
+ digest.add(mean, weights[index]);
103
+ }
104
+ if (means.length > 0) {
105
+ digest.min_ = min;
106
+ digest.max_ = max;
107
+ }
108
+ return digest;
109
+ }
110
+ /** The total weight added: the count of values when every weight is 1. */
111
+ get count() {
112
+ return this.total_ + this.bufferedWeight_;
113
+ }
114
+ /** The smallest value added, `NaN` when empty. */
115
+ get min() {
116
+ return this.count === 0 ? Number.NaN : this.min_;
117
+ }
118
+ /** The largest value added, `NaN` when empty. */
119
+ get max() {
120
+ return this.count === 0 ? Number.NaN : this.max_;
121
+ }
122
+ /**
123
+ * Adds `value` with `weight` (a count of occurrences, or any positive
124
+ * importance).
125
+ *
126
+ * @throws {RangeError} When `value` is not finite or `weight` is not finite
127
+ * and positive; the digest is left unchanged.
128
+ */
129
+ add(value, weight = 1) {
130
+ if (value - value !== 0) {
131
+ throw new RangeError(`TDigest.add: value must be finite, got ${describeValue(value)}`);
132
+ }
133
+ if (!(weight > 0 && weight < Number.POSITIVE_INFINITY)) {
134
+ throw new RangeError(`TDigest.add: weight must be finite and positive, got ${describeValue(weight)}`);
135
+ }
136
+ if (this.buffered_ === this.bufferCapacity_) {
137
+ this.compress_();
138
+ }
139
+ this.bufferMeans_[this.buffered_] = value;
140
+ this.bufferWeights_[this.buffered_] = weight;
141
+ this.buffered_++;
142
+ this.bufferedWeight_ += weight;
143
+ if (value < this.min_) {
144
+ this.min_ = value;
145
+ }
146
+ if (value > this.max_) {
147
+ this.max_ = value;
148
+ }
149
+ }
150
+ /**
151
+ * Merges `other` into this digest, which then summarises both; `other` is not
152
+ * changed. Merging digests of parts gives about the accuracy of one digest of
153
+ * the whole.
154
+ */
155
+ merge(other) {
156
+ if (other === this) {
157
+ throw new RangeError("TDigest.merge: cannot merge a digest into itself");
158
+ }
159
+ other.compress_();
160
+ for (let index = 0; index < other.centroids_; index++) {
161
+ this.add(other.means_[index], other.weights_[index]);
162
+ }
163
+ if (other.count > 0) {
164
+ this.min_ = Math.min(this.min_, other.min_);
165
+ this.max_ = Math.max(this.max_, other.max_);
166
+ }
167
+ }
168
+ /** Empties the digest, keeping its storage. */
169
+ reset() {
170
+ this.centroids_ = 0;
171
+ this.buffered_ = 0;
172
+ this.total_ = 0;
173
+ this.bufferedWeight_ = 0;
174
+ this.min_ = Number.POSITIVE_INFINITY;
175
+ this.max_ = Number.NEGATIVE_INFINITY;
176
+ this.reversed_ = false;
177
+ }
178
+ /** A copy of the centroids, ascending by mean, for serialisation. */
179
+ centroids() {
180
+ this.compress_();
181
+ return {
182
+ means: this.means_.slice(0, this.centroids_),
183
+ weights: this.weights_.slice(0, this.centroids_),
184
+ };
185
+ }
186
+ /**
187
+ * The estimated `probability` quantile: `min` at `0`, `max` at `1`; `NaN`
188
+ * when empty.
189
+ *
190
+ * @throws {RangeError} When `probability` is outside `[0, 1]`.
191
+ */
192
+ quantile(probability) {
193
+ if (!(probability >= 0 && probability <= 1)) {
194
+ throw new RangeError(`TDigest.quantile: probability must be in [0, 1], got ${describeValue(probability)}`);
195
+ }
196
+ this.compress_();
197
+ const count = this.centroids_;
198
+ if (count === 0) {
199
+ return Number.NaN;
200
+ }
201
+ const means = this.means_;
202
+ const weights = this.weights_;
203
+ const target = probability * this.total_;
204
+ // Knots: (min, 0), (mᵢ, Σⱼ<ᵢ wⱼ + wᵢ/2), (max, total).
205
+ let previousX = this.min_;
206
+ let previousY = 0;
207
+ let below = 0;
208
+ for (let index = 0; index <= count; index++) {
209
+ let x;
210
+ let y;
211
+ if (index < count) {
212
+ const weight = weights[index];
213
+ x = means[index];
214
+ y = below + weight / 2;
215
+ below += weight;
216
+ }
217
+ else {
218
+ x = this.max_;
219
+ y = this.total_;
220
+ }
221
+ if (target <= y) {
222
+ if (y === previousY) {
223
+ return previousX;
224
+ }
225
+ const fraction = (target - previousY) / (y - previousY);
226
+ return clamp_(previousX + fraction * (x - previousX), previousX, x);
227
+ }
228
+ previousX = x;
229
+ previousY = y;
230
+ }
231
+ return this.max_;
232
+ }
233
+ /**
234
+ * The estimated cumulative probability `P(X ≤ x)`: `0` below `min`, `1` above
235
+ * `max`, the weight below plus half the weight of the centroids whose mean is
236
+ * exactly `x` (a mid-rank); `NaN` when empty or for a `NaN` `x`.
237
+ */
238
+ cdf(x) {
239
+ this.compress_();
240
+ const count = this.centroids_;
241
+ if (count === 0 || x !== x) {
242
+ return Number.NaN;
243
+ }
244
+ if (x < this.min_) {
245
+ return 0;
246
+ }
247
+ if (x > this.max_) {
248
+ return 1;
249
+ }
250
+ const means = this.means_;
251
+ const weights = this.weights_;
252
+ const total = this.total_;
253
+ let previousX = this.min_;
254
+ let previousY = 0;
255
+ let below = 0;
256
+ // The range of the centroid knots at exactly x, when there are some.
257
+ let lowest = Number.NaN;
258
+ let highest = Number.NaN;
259
+ for (let index = 0; index <= count; index++) {
260
+ let knotX;
261
+ let knotY;
262
+ if (index < count) {
263
+ const weight = weights[index];
264
+ knotX = means[index];
265
+ knotY = below + weight / 2;
266
+ below += weight;
267
+ }
268
+ else {
269
+ knotX = this.max_;
270
+ knotY = total;
271
+ }
272
+ if (knotX === x && index < count) {
273
+ if (lowest !== lowest) {
274
+ lowest = knotY;
275
+ }
276
+ highest = knotY;
277
+ }
278
+ else if (knotX >= x) {
279
+ if (lowest === lowest) {
280
+ break;
281
+ }
282
+ if (knotX === previousX) {
283
+ return knotY / total;
284
+ }
285
+ const fraction = (x - previousX) / (knotX - previousX);
286
+ return (previousY + fraction * (knotY - previousY)) / total;
287
+ }
288
+ previousX = knotX;
289
+ previousY = knotY;
290
+ }
291
+ return (lowest + highest) / 2 / total;
292
+ }
293
+ /** Merges the buffer into the centroids, if anything is buffered. */
294
+ compress_() {
295
+ const buffered = this.buffered_;
296
+ if (buffered === 0) {
297
+ return;
298
+ }
299
+ const order = this.order_;
300
+ const bufferMeans = this.bufferMeans_;
301
+ const bufferWeights = this.bufferWeights_;
302
+ for (let index = 0; index < buffered; index++) {
303
+ order[index] = index;
304
+ }
305
+ order
306
+ .subarray(0, buffered)
307
+ .sort((left, right) => bufferMeans[left] - bufferMeans[right]);
308
+ // Merge the sorted buffer with the sorted centroids into the scratch.
309
+ const means = this.means_;
310
+ const weights = this.weights_;
311
+ const scratchMeans = this.scratchMeans_;
312
+ const scratchWeights = this.scratchWeights_;
313
+ const centroids = this.centroids_;
314
+ let left = 0;
315
+ let right = 0;
316
+ let output = 0;
317
+ while (left < centroids || right < buffered) {
318
+ if (right === buffered ||
319
+ (left < centroids && means[left] <= bufferMeans[order[right]])) {
320
+ scratchMeans[output] = means[left];
321
+ scratchWeights[output] = weights[left];
322
+ left++;
323
+ }
324
+ else {
325
+ scratchMeans[output] = bufferMeans[order[right]];
326
+ scratchWeights[output] = bufferWeights[order[right]];
327
+ right++;
328
+ }
329
+ output++;
330
+ }
331
+ const total = this.total_ + this.bufferedWeight_;
332
+ this.total_ = total;
333
+ this.bufferedWeight_ = 0;
334
+ this.buffered_ = 0;
335
+ const reversed = this.reversed_;
336
+ this.reversed_ = !reversed;
337
+ // One pass in k-space, from the low end or (every other time) the high
338
+ // end; every scale is symmetric about q = ½, so the high-end pass is
339
+ // the same rule on the mirrored order.
340
+ const scale = this.scale;
341
+ const normalizer = kNormalizer_(scale, this.compression, total);
342
+ const lastSlot = this.capacity_ - 1;
343
+ let kept = 0;
344
+ let weightSoFar = 0;
345
+ let limit = total * kInverse_(scale, k_(scale, 0, normalizer) + 1, normalizer);
346
+ const step = reversed ? -1 : 1;
347
+ let source = reversed ? output - 1 : 0;
348
+ let currentMean = scratchMeans[source];
349
+ let currentWeight = scratchWeights[source];
350
+ for (let count = 1; count < output; count++) {
351
+ source += step;
352
+ const mean = scratchMeans[source];
353
+ const weight = scratchWeights[source];
354
+ // The capacity guard only binds for weights spanning hundreds of
355
+ // orders of magnitude, whose k range the bound does not cover.
356
+ if (weightSoFar + currentWeight + weight <= limit || kept === lastSlot) {
357
+ currentWeight += weight;
358
+ currentMean += (weight / currentWeight) * (mean - currentMean);
359
+ }
360
+ else {
361
+ means[kept] = currentMean;
362
+ weights[kept] = currentWeight;
363
+ kept++;
364
+ weightSoFar += currentWeight;
365
+ limit =
366
+ total *
367
+ kInverse_(scale, k_(scale, weightSoFar / total, normalizer) + 1, normalizer);
368
+ currentMean = mean;
369
+ currentWeight = weight;
370
+ }
371
+ }
372
+ means[kept] = currentMean;
373
+ weights[kept] = currentWeight;
374
+ kept++;
375
+ if (reversed) {
376
+ means.subarray(0, kept).reverse();
377
+ weights.subarray(0, kept).reverse();
378
+ }
379
+ this.centroids_ = kept;
380
+ }
381
+ }
382
+ function k_(scale, quantile, normalizer) {
383
+ switch (scale) {
384
+ case "k1": {
385
+ return normalizer * Math.asin(2 * quantile - 1);
386
+ }
387
+ case "k2": {
388
+ return normalizer * Math.log(quantile / (1 - quantile));
389
+ }
390
+ case "k3": {
391
+ return quantile <= 0.5
392
+ ? normalizer * Math.log(2 * quantile)
393
+ : -normalizer * Math.log(2 * (1 - quantile));
394
+ }
395
+ }
396
+ }
397
+ function kInverse_(scale, k, normalizer) {
398
+ const scaled = k / normalizer;
399
+ switch (scale) {
400
+ case "k1": {
401
+ return scaled >= Math.PI / 2 ? 1 : (Math.sin(scaled) + 1) / 2;
402
+ }
403
+ case "k2": {
404
+ return 1 / (1 + Math.exp(-scaled));
405
+ }
406
+ case "k3": {
407
+ return scaled <= 0 ? Math.exp(scaled) / 2 : 1 - Math.exp(-scaled) / 2;
408
+ }
409
+ }
410
+ }
411
+ /** `δ/(2π)` for k₁; `δ / Z(n)` for k₂ and k₃, Dunning's size normaliser. */
412
+ function kNormalizer_(scale, compression, total) {
413
+ if (scale === "k1") {
414
+ return compression / (2 * Math.PI);
415
+ }
416
+ // Below δ values the digest is all singletons anyway: the logarithm is
417
+ // floored at zero so Z stays positive.
418
+ const growth = 4 * Math.log(Math.max(total / compression, 1));
419
+ return compression / (growth + (scale === "k2" ? 24 : 21));
420
+ }
421
+ function clamp_(value, low, high) {
422
+ return value < low ? low : value > high ? high : value;
423
+ }
424
+ //# sourceMappingURL=t-digest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"t-digest.js","sourceRoot":"","sources":["../src/t-digest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAmC7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,OAAO,OAAO;IACnB,2BAA2B;IAClB,WAAW,CAAS;IAE7B,0BAA0B;IACjB,KAAK,CAAe;IAEZ,SAAS,CAAS;IAClB,eAAe,CAAS;IACxB,MAAM,CAAe;IACrB,QAAQ,CAAe;IACvB,YAAY,CAAe;IAC3B,cAAc,CAAe;IAC7B,MAAM,CAAa;IACnB,aAAa,CAAe;IAC5B,eAAe,CAAe;I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AE9D,OAAO,WAAW,GAAG,CAAC,MAAM,GAAG,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED,SAAS,MAAM,CAAC,KAAa,EAAE,GAAW,EAAE,IAAY;IACvD,OAAO,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;AACxD,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { Distribution } from "./distribution.js";
2
+ /**
3
+ * The normal distribution of `mean` and `standardDeviation` restricted to
4
+ * `[lower, upper]` and renormalised: the law of wind speed and other bounded
5
+ * quantities in ensemble post-processing (Thorarinsdottir and Gneiting 2010).
6
+ *
7
+ * The bounds may lie anywhere, even hundreds of standard deviations out in a
8
+ * tail, where `Φ(upper) − Φ(lower)` is far below the smallest double: an
9
+ * interval clear of the centre is reflected into the upper tail and every
10
+ * probability taken as a ratio of Mills' ratios, which never underflow; one
11
+ * holding the centre uses `erfc` on each side. `cdf` and `survival` are each
12
+ * computed directly, so both tails keep their relative precision away from the
13
+ * bounds (within about `1e-8` standard deviations of a bound, they are exact to
14
+ * `ε` of the total only). The quantile of a tail interval is Newton's method on
15
+ * the logarithm of `Φᶜ`, which is concave and so converges monotonically.
16
+ * `mean` and `variance` are the exact moments; the variance of a one-sided tail
17
+ * interval comes from Laplace's continued fraction without cancellation.
18
+ *
19
+ * @param mean Location of the parent normal, finite.
20
+ * @param standardDeviation Scale of the parent normal; positive and finite.
21
+ * @param lower Lower bound, `-Infinity` (the default) for none.
22
+ * @param upper Upper bound, above `lower`; `Infinity` (the default) for none.
23
+ * @returns The distribution; the parent normal when both bounds are infinite.
24
+ * @throws {RangeError} When `mean` is not finite, `standardDeviation` is not
25
+ * positive and finite, a bound is `NaN`, or `lower ≥ upper`.
26
+ */
27
+ export declare function truncatedNormalDistribution(mean: number, standardDeviation: number, lower?: number, upper?: number): Distribution;
28
+ //# sourceMappingURL=truncated-normal-distribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncated-normal-distribution.d.ts","sourceRoot":"","sources":["../src/truncated-normal-distribution.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AA4JtD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,2BAA2B,CAC1C,IAAI,EAAE,MAAM,EACZ,iBAAiB,EAAE,MAAM,EACzB,KAAK,SAA2B,EAChC,KAAK,SAA2B,GAC9B,YAAY,CAkEd"}