@ac-kit/math-stats 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +67 -0
  3. package/dist/_assert-probability.d.ts.map +1 -1
  4. package/dist/_assert-probability.js +2 -1
  5. package/dist/_assert-probability.js.map +1 -1
  6. package/dist/_satterthwaite.d.ts +13 -0
  7. package/dist/_satterthwaite.d.ts.map +1 -0
  8. package/dist/_satterthwaite.js +25 -0
  9. package/dist/_satterthwaite.js.map +1 -0
  10. package/dist/adjust-p-values.d.ts +41 -0
  11. package/dist/adjust-p-values.d.ts.map +1 -0
  12. package/dist/adjust-p-values.js +75 -0
  13. package/dist/adjust-p-values.js.map +1 -0
  14. package/dist/ashman-d.d.ts +25 -0
  15. package/dist/ashman-d.d.ts.map +1 -0
  16. package/dist/ashman-d.js +24 -0
  17. package/dist/ashman-d.js.map +1 -0
  18. package/dist/beta-distribution.d.ts.map +1 -1
  19. package/dist/beta-distribution.js +3 -2
  20. package/dist/beta-distribution.js.map +1 -1
  21. package/dist/bimodality-coefficient.d.ts +32 -0
  22. package/dist/bimodality-coefficient.d.ts.map +1 -0
  23. package/dist/bimodality-coefficient.js +63 -0
  24. package/dist/bimodality-coefficient.js.map +1 -0
  25. package/dist/binomial-distribution.d.ts.map +1 -1
  26. package/dist/binomial-distribution.js +2 -1
  27. package/dist/binomial-distribution.js.map +1 -1
  28. package/dist/chi-squared-distribution.d.ts.map +1 -1
  29. package/dist/chi-squared-distribution.js +2 -1
  30. package/dist/chi-squared-distribution.js.map +1 -1
  31. package/dist/chi-squared-test.js +2 -2
  32. package/dist/chi-squared-test.js.map +1 -1
  33. package/dist/combine-independent-estimates.d.ts +53 -0
  34. package/dist/combine-independent-estimates.d.ts.map +1 -0
  35. package/dist/combine-independent-estimates.js +71 -0
  36. package/dist/combine-independent-estimates.js.map +1 -0
  37. package/dist/confidence-interval.d.ts.map +1 -1
  38. package/dist/confidence-interval.js +2 -1
  39. package/dist/confidence-interval.js.map +1 -1
  40. package/dist/exponential-distribution.d.ts.map +1 -1
  41. package/dist/exponential-distribution.js +2 -1
  42. package/dist/exponential-distribution.js.map +1 -1
  43. package/dist/f-distribution.d.ts.map +1 -1
  44. package/dist/f-distribution.js +3 -2
  45. package/dist/f-distribution.js.map +1 -1
  46. package/dist/gamma-distribution.d.ts.map +1 -1
  47. package/dist/gamma-distribution.js +3 -2
  48. package/dist/gamma-distribution.js.map +1 -1
  49. package/dist/geometric-distribution.d.ts.map +1 -1
  50. package/dist/geometric-distribution.js +2 -1
  51. package/dist/geometric-distribution.js.map +1 -1
  52. package/dist/hartigan-dip.d.ts +65 -0
  53. package/dist/hartigan-dip.d.ts.map +1 -0
  54. package/dist/hartigan-dip.js +273 -0
  55. package/dist/hartigan-dip.js.map +1 -0
  56. package/dist/histogram.d.ts +42 -0
  57. package/dist/histogram.d.ts.map +1 -1
  58. package/dist/histogram.js +106 -2
  59. package/dist/histogram.js.map +1 -1
  60. package/dist/hypergeometric-distribution.js +2 -2
  61. package/dist/hypergeometric-distribution.js.map +1 -1
  62. package/dist/index.d.ts +8 -0
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +8 -0
  65. package/dist/index.js.map +1 -1
  66. package/dist/kmeans.d.ts.map +1 -1
  67. package/dist/kmeans.js +1 -1
  68. package/dist/kmeans.js.map +1 -1
  69. package/dist/log-normal-distribution.d.ts.map +1 -1
  70. package/dist/log-normal-distribution.js +2 -1
  71. package/dist/log-normal-distribution.js.map +1 -1
  72. package/dist/negative-binomial-distribution.d.ts.map +1 -1
  73. package/dist/negative-binomial-distribution.js +3 -2
  74. package/dist/negative-binomial-distribution.js.map +1 -1
  75. package/dist/normal-distribution.d.ts.map +1 -1
  76. package/dist/normal-distribution.js +2 -1
  77. package/dist/normal-distribution.js.map +1 -1
  78. package/dist/normal-quantile.d.ts.map +1 -1
  79. package/dist/normal-quantile.js +2 -1
  80. package/dist/normal-quantile.js.map +1 -1
  81. package/dist/poisson-distribution.d.ts.map +1 -1
  82. package/dist/poisson-distribution.js +2 -1
  83. package/dist/poisson-distribution.js.map +1 -1
  84. package/dist/quantile.d.ts +6 -2
  85. package/dist/quantile.d.ts.map +1 -1
  86. package/dist/quantile.js +17 -2
  87. package/dist/quantile.js.map +1 -1
  88. package/dist/random-effects.d.ts +56 -9
  89. package/dist/random-effects.d.ts.map +1 -1
  90. package/dist/random-effects.js +59 -12
  91. package/dist/random-effects.js.map +1 -1
  92. package/dist/regression.d.ts +26 -2
  93. package/dist/regression.d.ts.map +1 -1
  94. package/dist/regression.js +175 -12
  95. package/dist/regression.js.map +1 -1
  96. package/dist/satterthwaite-degrees-of-freedom.d.ts +44 -0
  97. package/dist/satterthwaite-degrees-of-freedom.d.ts.map +1 -0
  98. package/dist/satterthwaite-degrees-of-freedom.js +41 -0
  99. package/dist/satterthwaite-degrees-of-freedom.js.map +1 -0
  100. package/dist/sliding-median.d.ts +1 -2
  101. package/dist/sliding-median.d.ts.map +1 -1
  102. package/dist/sliding-median.js +28 -24
  103. package/dist/sliding-median.js.map +1 -1
  104. package/dist/student-t-distribution.d.ts.map +1 -1
  105. package/dist/student-t-distribution.js +2 -1
  106. package/dist/student-t-distribution.js.map +1 -1
  107. package/dist/two-cluster-split.d.ts +34 -0
  108. package/dist/two-cluster-split.d.ts.map +1 -0
  109. package/dist/two-cluster-split.js +78 -0
  110. package/dist/two-cluster-split.js.map +1 -0
  111. package/dist/variance-of-sample-median.d.ts +40 -0
  112. package/dist/variance-of-sample-median.d.ts.map +1 -0
  113. package/dist/variance-of-sample-median.js +162 -0
  114. package/dist/variance-of-sample-median.js.map +1 -0
  115. package/dist/welch-t-statistic.d.ts.map +1 -1
  116. package/dist/welch-t-statistic.js +7 -3
  117. package/dist/welch-t-statistic.js.map +1 -1
  118. package/package.json +9 -8
@@ -1,13 +1,47 @@
1
- import { PreciseSum } from "@ac-kit/core";
2
- import { mean } from "./average.js";
1
+ import { float64ToParts, PreciseSum, ratioToFloat64 } from "@ac-kit/core";
2
+ import { twoProductError, twoSumError } from "@ac-kit/math-scalar";
3
+ // The in-range computation is binary64 arithmetic with no scaling. Within these
4
+ // bounds no sum, product or slope overflows, and nothing underflows: a non-zero
5
+ // value, mean, deviation or rounding error is at least 2⁻⁴³⁶ for fewer than
6
+ // 2³² values, so every product of two is a normal number, and Dekker's
7
+ // products are exact.
8
+ const LOWEST_IN_RANGE_ = 2 ** -300;
9
+ const HIGHEST_IN_RANGE_ = 2 ** 300;
10
+ const ROUNDOFF_SQUARED_ = 2 ** -106;
11
+ const QUARTER_ROUNDOFF_ = 2 ** -55;
12
+ const CORRECTION_ROUNDING_ = 4.02 * 2 ** -53;
3
13
  /**
4
14
  * Slope of the ordinary-least-squares line through paired `xValues` and
5
15
  * `yValues`.
6
16
  *
17
+ * Right for every finite input, whatever its magnitudes. When every non-zero
18
+ * value lies within 2⁻³⁰⁰ to 2³⁰⁰ (about 1e-90 to 1e90), the result is within
19
+ * four units in the last place of the exact slope of the given values: a
20
+ * relative error below 2⁻⁵¹, or less than four times `Number.MIN_VALUE` for a
21
+ * subnormal slope. The means are summed exactly and the deviations corrected
22
+ * for their rounding (the corrected two-pass algorithm), with every deviation
23
+ * and product carried together with its exact rounding error; where a bound on
24
+ * what is still lost cannot show that accuracy — when the products of the
25
+ * deviations cancel to less than about (4n + 11)·2⁻⁵¹ of their magnitudes, 2⁻⁴⁴
26
+ * for twenty values, as for `[0, 0, 1, -1]` against `[2¹⁰⁰, 2¹⁰⁰, 1, -1]` — the
27
+ * slope is computed exactly instead. Past those bounds, where squares and
28
+ * products could overflow, underflow or lose a term next to one a thousand
29
+ * binary orders larger, the slope is always computed exactly from the values as
30
+ * integers and rounded once: it is the binary64 nearest the true slope, even
31
+ * when that is subnormal (`±0` when it underflows) or `±Infinity`, which it is
32
+ * only when the true slope is past `Number.MAX_VALUE`. A fit of a hundred
33
+ * values at those extremes costs about a hundred big-integer products of a few
34
+ * thousand bits.
35
+ *
36
+ * Time complexity: O(n), with O(n) big-integer operations on numbers of up to
37
+ * about 4300 bits when computed exactly. Memory: O(1), or O(n) integers when
38
+ * computed exactly.
39
+ *
7
40
  * @param xValues Independent-variable samples. Not modified.
8
41
  * @param yValues Dependent-variable samples paired by index. Not modified.
9
- * @returns The slope, or `0` for fewer than two values — the same
10
- * underdetermined-input convention as {@link sampleVariance}.
42
+ * @returns The slope; `0` for fewer than two values, or when every `x` is equal
43
+ * — the same underdetermined-input convention as {@link sampleVariance};
44
+ * `NaN` when a value is not finite.
11
45
  * @throws {RangeError} If `xValues` and `yValues` have different lengths.
12
46
  */
13
47
  export function linearRegressionSlope(xValues, yValues) {
@@ -18,18 +52,147 @@ export function linearRegressionSlope(xValues, yValues) {
18
52
  if (count < 2) {
19
53
  return 0;
20
54
  }
21
- const meanX = mean(xValues);
22
- const meanY = mean(yValues);
23
- // Exact: the deviations straddle their means by construction, so a naive
24
- // running total can cancel away every significant bit of either accumulator.
55
+ let isInRange = true;
56
+ for (let index = 0; index < count; index++) {
57
+ const magnitudeX = Math.abs(xValues[index]);
58
+ const magnitudeY = Math.abs(yValues[index]);
59
+ if (!(magnitudeX <= Number.MAX_VALUE && magnitudeY <= Number.MAX_VALUE)) {
60
+ return Number.NaN;
61
+ }
62
+ if (magnitudeX > HIGHEST_IN_RANGE_ ||
63
+ magnitudeY > HIGHEST_IN_RANGE_ ||
64
+ (magnitudeX !== 0 && magnitudeX < LOWEST_IN_RANGE_) ||
65
+ (magnitudeY !== 0 && magnitudeY < LOWEST_IN_RANGE_)) {
66
+ isInRange = false;
67
+ }
68
+ }
69
+ return isInRange
70
+ ? slopeInRange_(xValues, yValues, count)
71
+ : slopeExact_(xValues, yValues, count);
72
+ }
73
+ function slopeInRange_(xValues, yValues, count) {
74
+ // Exact, then rounded once: the deviations straddle their means by
75
+ // construction, so a naive running total can cancel away every significant
76
+ // bit of either accumulator.
77
+ const sumX = new PreciseSum();
78
+ const sumY = new PreciseSum();
79
+ for (let index = 0; index < count; index++) {
80
+ sumX.add(xValues[index]);
81
+ sumY.add(yValues[index]);
82
+ }
83
+ const meanX = sumX.value / count;
84
+ const meanY = sumY.value / count;
85
+ // The means are rounded, so the deviations do not sum to exactly zero: the
86
+ // corrected two-pass algorithm (Chan, Golub and LeVeque) takes the leftover
87
+ // `Σ(x − meanX) · Σ(y − meanY) / n` back out. Each leftover is the exact sum
88
+ // less `n · mean`, that product split exactly, so it is rounded only once.
89
+ const offsetX = count * meanX;
90
+ sumX.add(-offsetX);
91
+ sumX.add(-twoProductError(count, meanX, offsetX));
92
+ const driftX = sumX.value;
93
+ const offsetY = count * meanY;
94
+ sumY.add(-offsetY);
95
+ sumY.add(-twoProductError(count, meanY, offsetY));
96
+ const driftY = sumY.value;
97
+ // Each deviation is carried with its exact rounding error, and each product
98
+ // of deviations with its own: the products are summed exactly, the terms of
99
+ // first order in those errors in plain arithmetic beside them, and only the
100
+ // product of two errors is dropped.
25
101
  const covariance = new PreciseSum();
26
102
  const variance = new PreciseSum();
103
+ let covarianceTail = 0;
104
+ let varianceTail = 0;
105
+ let covarianceSize = 0;
106
+ let varianceSize = 0;
27
107
  for (let index = 0; index < count; index++) {
28
- const deltaX = xValues[index] - meanX;
29
- covariance.add(deltaX * (yValues[index] - meanY));
30
- variance.add(deltaX * deltaX);
108
+ const x = xValues[index];
109
+ const y = yValues[index];
110
+ const deltaX = x - meanX;
111
+ const deltaY = y - meanY;
112
+ const errorX = twoSumError(x, -meanX, deltaX);
113
+ const errorY = twoSumError(y, -meanY, deltaY);
114
+ const productXY = deltaX * deltaY;
115
+ const productXX = deltaX * deltaX;
116
+ covariance.add(productXY);
117
+ variance.add(productXX);
118
+ covarianceTail +=
119
+ twoProductError(deltaX, deltaY, productXY) +
120
+ (deltaX * errorY + errorX * deltaY);
121
+ varianceTail +=
122
+ twoProductError(deltaX, deltaX, productXX) + 2 * (deltaX * errorX);
123
+ covarianceSize += Math.abs(productXY);
124
+ varianceSize += productXX;
31
125
  }
126
+ const correctionXY = (driftX / count) * driftY;
127
+ const correctionXX = (driftX / count) * driftX;
128
+ covariance.add(covarianceTail);
129
+ covariance.add(-correctionXY);
130
+ variance.add(varianceTail);
131
+ variance.add(-correctionXX);
132
+ const product = covariance.value;
32
133
  const spread = variance.value;
33
- return spread === 0 ? 0 : covariance.value / spread;
134
+ // Beyond the last rounding of each total, `u·|total|` with u = 2⁻⁵³, what is
135
+ // left is the first-order tail's rounding, the dropped products of two
136
+ // errors and the tail's plain summation, at most (3.05n + 5)·u²·Σ|products|,
137
+ // and the correction's four roundings, 4.01u·|correction|; rounded up to
138
+ // the constants below. Once that is at most u/4 of each total, both totals
139
+ // are within 1.25u of the true ones and the slope within 3.6u < 2⁻⁵¹;
140
+ // otherwise the bits that decide it may be gone, and it is computed exactly.
141
+ const growth = (4 * count + 11) * ROUNDOFF_SQUARED_;
142
+ if (growth * covarianceSize + CORRECTION_ROUNDING_ * Math.abs(correctionXY) >
143
+ QUARTER_ROUNDOFF_ * Math.abs(product) ||
144
+ growth * varianceSize + CORRECTION_ROUNDING_ * correctionXX >
145
+ QUARTER_ROUNDOFF_ * spread) {
146
+ return slopeExact_(xValues, yValues, count);
147
+ }
148
+ if (!(spread > 0) || product === 0) {
149
+ return 0;
150
+ }
151
+ return product / spread;
152
+ }
153
+ // Every value is `integer × 2^exponent`, so with the smallest exponent of each
154
+ // variable as the unit, the sums below are big-integer arithmetic with no
155
+ // rounding, and the slope is the ratio of two integers:
156
+ // (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²), times the ratio of the two units.
157
+ function slopeExact_(xValues, yValues, count) {
158
+ const [integersX, unitX] = asIntegers_(xValues);
159
+ const [integersY, unitY] = asIntegers_(yValues);
160
+ let sumX = 0n;
161
+ let sumY = 0n;
162
+ let sumXY = 0n;
163
+ let sumXX = 0n;
164
+ for (let index = 0; index < count; index++) {
165
+ const x = integersX[index];
166
+ const y = integersY[index];
167
+ sumX += x;
168
+ sumY += y;
169
+ sumXY += x * y;
170
+ sumXX += x * x;
171
+ }
172
+ const size = BigInt(count);
173
+ const denominator = size * sumXX - sumX * sumX;
174
+ const numerator = size * sumXY - sumX * sumY;
175
+ if (denominator === 0n || numerator === 0n) {
176
+ return 0;
177
+ }
178
+ return ratioToFloat64(numerator, denominator, unitY - unitX);
179
+ }
180
+ // The values as integers of one common unit `2^exponent`, and that exponent:
181
+ // the smallest of the non-zero values, so no integer is wider than it must be.
182
+ function asIntegers_(values) {
183
+ const parts = values.map((value) => float64ToParts(value));
184
+ let unit = Number.POSITIVE_INFINITY;
185
+ for (const { significand, exponent } of parts) {
186
+ if (significand !== 0n && exponent < unit) {
187
+ unit = exponent;
188
+ }
189
+ }
190
+ if (unit === Number.POSITIVE_INFINITY) {
191
+ return [parts.map(() => 0n), 0];
192
+ }
193
+ return [
194
+ parts.map(({ significand, exponent }) => significand === 0n ? 0n : significand << BigInt(exponent - unit)),
195
+ unit,
196
+ ];
34
197
  }
35
198
  //# sourceMappingURL=regression.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"regression.js","sourceRoot":"","sources":["../src/regression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;;;;;;;;;GASG;AACH,MAAM,UAAU,qBAAqB,CACpC,OAA0B,EAC1B,OAA0B;IAE1B,IAAI,OAAO,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC;QACvC,MAAM,IAAI,UAAU,CAAC,+CAA+C,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IAC7B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACf,OAAO,CAAC,CAAC;IACV,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5B,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO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iB,CAAC;IACpC,KAAK,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,IAAI,KAAK,EAAE,CAAC;QAC/C,IAAI,WAAW,KAAK,EAAE,IAAI,QAAQ,GAAG,IAAI,EAAE,CAAC;YAC3C,IAAI,GAAG,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;IACD,IAAI,IAAI,KAAK,MAAM,CAAC,iBAAiB,EAAE,CAAC;QACvC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACjC,CAAC;IAED,OAAO;QACN,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,EAAE,QAAQ,EAAE,EAAE,EAAE,CACvC,WAAW,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,IAAI,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,CAChE;QACD,IAAI;KACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * One term of a sum of independent variance estimates: the term's estimated
3
+ * variance and the degrees of freedom it was estimated with.
4
+ */
5
+ export type VarianceComponent = {
6
+ /**
7
+ * The term's contribution to the summed variance — for a mean, the sample
8
+ * variance divided by the sample size, not the sample variance itself. Finite
9
+ * and non-negative.
10
+ */
11
+ readonly variance: number;
12
+ /**
13
+ * Degrees of freedom of that estimate, `n − 1` for a sample variance.
14
+ * Positive; need not be an integer. `Infinity` marks a variance known
15
+ * exactly, which adds to the sum without adding uncertainty.
16
+ */
17
+ readonly degreesOfFreedom: number;
18
+ };
19
+ /**
20
+ * Welch–Satterthwaite effective degrees of freedom of a sum of independent
21
+ * variance estimates: `ν = (Σ Vᵢ)² / Σ (Vᵢ² / νᵢ)`.
22
+ *
23
+ * The sum of estimated variances is not exactly a scaled chi-squared variable;
24
+ * this is the degrees of freedom of the chi-squared it is approximated by, so a
25
+ * statistic divided by `√Σ Vᵢ` can be referred to Student's t with `ν`. It is
26
+ * what Welch's unequal-variance t-test uses (with two components), and what an
27
+ * unpaired difference of two pooled estimates uses. Over the components with a
28
+ * non-zero variance, the result lies between the smallest `νᵢ` and `Σ νᵢ`.
29
+ * Variances anywhere in the double range, up to `Number.MAX_VALUE`, give a
30
+ * finite, accurate result.
31
+ *
32
+ * O(n) in the number of components.
33
+ *
34
+ * @param components The terms of the sum, at least one. Not modified.
35
+ * @returns The effective degrees of freedom; `Infinity` when every term with a
36
+ * non-zero variance is known exactly; `NaN` when every variance is zero,
37
+ * since a sum with no spread says nothing about how its spread was
38
+ * estimated.
39
+ * @throws {RangeError} When `components` is empty, when a variance is negative,
40
+ * `NaN` or infinite, or when a degrees of freedom is not positive (or is
41
+ * `NaN`).
42
+ */
43
+ export declare function satterthwaiteDegreesOfFreedom(components: readonly VarianceComponent[]): number;
44
+ //# sourceMappingURL=satterthwaite-degrees-of-freedom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"satterthwaite-degrees-of-freedom.d.ts","sourceRoot":"","sources":["../src/satterthwaite-degrees-of-freedom.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;;OAIG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,6BAA6B,CAC5C,UAAU,EAAE,SAAS,iBAAiB,EAAE,GACtC,MAAM,CAsBR"}
@@ -0,0 +1,41 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { satterthwaite_ } from "./_satterthwaite.js";
3
+ /**
4
+ * Welch–Satterthwaite effective degrees of freedom of a sum of independent
5
+ * variance estimates: `ν = (Σ Vᵢ)² / Σ (Vᵢ² / νᵢ)`.
6
+ *
7
+ * The sum of estimated variances is not exactly a scaled chi-squared variable;
8
+ * this is the degrees of freedom of the chi-squared it is approximated by, so a
9
+ * statistic divided by `√Σ Vᵢ` can be referred to Student's t with `ν`. It is
10
+ * what Welch's unequal-variance t-test uses (with two components), and what an
11
+ * unpaired difference of two pooled estimates uses. Over the components with a
12
+ * non-zero variance, the result lies between the smallest `νᵢ` and `Σ νᵢ`.
13
+ * Variances anywhere in the double range, up to `Number.MAX_VALUE`, give a
14
+ * finite, accurate result.
15
+ *
16
+ * O(n) in the number of components.
17
+ *
18
+ * @param components The terms of the sum, at least one. Not modified.
19
+ * @returns The effective degrees of freedom; `Infinity` when every term with a
20
+ * non-zero variance is known exactly; `NaN` when every variance is zero,
21
+ * since a sum with no spread says nothing about how its spread was
22
+ * estimated.
23
+ * @throws {RangeError} When `components` is empty, when a variance is negative,
24
+ * `NaN` or infinite, or when a degrees of freedom is not positive (or is
25
+ * `NaN`).
26
+ */
27
+ export function satterthwaiteDegreesOfFreedom(components) {
28
+ if (components.length === 0) {
29
+ throw new RangeError("satterthwaiteDegreesOfFreedom: at least one component is required");
30
+ }
31
+ for (const { variance, degreesOfFreedom } of components) {
32
+ if (!(variance >= 0 && variance < Number.POSITIVE_INFINITY)) {
33
+ throw new RangeError(`satterthwaiteDegreesOfFreedom: every variance must be finite and non-negative, got ${describeValue(variance)}`);
34
+ }
35
+ if (!(degreesOfFreedom > 0)) {
36
+ throw new RangeError(`satterthwaiteDegreesOfFreedom: every degrees of freedom must be positive, got ${describeValue(degreesOfFreedom)}`);
37
+ }
38
+ }
39
+ return satterthwaite_(components);
40
+ }
41
+ //# sourceMappingURL=satterthwaite-degrees-of-freedom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"satterthwaite-degrees-of-freedom.js","sourceRoot":"","sources":["../src/satterthwaite-degrees-of-freedom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAsBrD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,6BAA6B,CAC5C,UAAwC;IAExC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CACnB,mEAAmE,CACnE,CAAC;IACH,CAAC;IAED,KAAK,MAAM,EAAE,QAAQ,EAAE,gBAAgB,EAAE,IAAI,UAAU,EAAE,CAAC;QACzD,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,QAAQ,GAAG,MAAM,CAAC,iBAAiB,CAAC,EAAE,CAAC;YAC7D,MAAM,IAAI,UAAU,CACnB,sFAAsF,aAAa,CAAC,QAAQ,CAAC,EAAE,CAC/G,CAAC;QACH,CAAC;QAED,IAAI,CAAC,CAAC,gBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,UAAU,CACnB,iFAAiF,aAAa,CAAC,gBAAgB,CAAC,EAAE,CAClH,CAAC;QACH,CAAC;IACF,CAAC;IAED,OAAO,cAAc,CAAC,UAAU,CAAC,CAAC;AACnC,CAAC"}
@@ -14,10 +14,9 @@
14
14
  export declare class SlidingMedian {
15
15
  readonly windowLength: number;
16
16
  private readonly arrival_;
17
+ private readonly arrivalValues_;
17
18
  private readonly sorted_;
18
19
  private readonly probe_;
19
- private count_;
20
- private oldest_;
21
20
  /** @throws {RangeError} If `windowLength` is not a positive integer. */
22
21
  constructor(windowLength: number);
23
22
  /** Number of values currently held, at most {@link windowLength}. */
@@ -1 +1 @@
1
- {"version":3,"file":"sliding-median.d.ts","sourceRoot":"","sources":["../src/sliding-median.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;IACzB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAC9C,OAAO,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E,MAAM,GAAG,IAAI,CAwBlD;IAED;;;;;;;OAOG;IACH,MAAM,IAAI,MAAM,CAUf;IAED;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG3B;IAED;;;;;;OAMG;IACH,UAAU,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAUlE;IAED,2BAA2B;IAC3B,KAAK,IAAI,IAAI,CAEZ;IAED,6EAA6E;IAC7E,OAAO,CAAC,WAAW;CAgBnB"}
@@ -1,3 +1,5 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { TypedColumnRing } from "@ac-kit/data";
1
3
  /**
2
4
  * Exact median of the last `windowLength` values pushed.
3
5
  *
@@ -13,23 +15,27 @@
13
15
  */
14
16
  export class SlidingMedian {
15
17
  windowLength;
18
+ // The values in arrival order: one column, `windowLength` rows.
16
19
  arrival_;
20
+ arrivalValues_;
17
21
  sorted_;
18
22
  probe_ = new Float64Array(1);
19
- count_ = 0;
20
- oldest_ = 0;
21
23
  /** @throws {RangeError} If `windowLength` is not a positive integer. */
22
24
  constructor(windowLength) {
23
25
  if (!Number.isInteger(windowLength) || windowLength < 1) {
24
- throw new RangeError(`windowLength must be a positive integer, got ${windowLength}`);
26
+ throw new RangeError(`windowLength must be a positive integer, got ${describeValue(windowLength)}`);
25
27
  }
26
28
  this.windowLength = windowLength;
27
- this.arrival_ = new Float64Array(windowLength);
29
+ this.arrival_ = new TypedColumnRing(Float64Array, {
30
+ columnCount: 1,
31
+ capacity: windowLength,
32
+ });
33
+ this.arrivalValues_ = this.arrival_.column(0);
28
34
  this.sorted_ = new Float64Array(windowLength);
29
35
  }
30
36
  /** Number of values currently held, at most {@link windowLength}. */
31
37
  get length() {
32
- return this.count_;
38
+ return this.arrival_.length;
33
39
  }
34
40
  /**
35
41
  * Adds `value`, dropping the oldest one once the window is full.
@@ -56,22 +62,20 @@ export class SlidingMedian {
56
62
  }
57
63
  const sorted = this.sorted_;
58
64
  const probe = this.probe_;
59
- if (this.count_ === this.windowLength) {
60
- probe[0] = this.arrival_[this.oldest_];
61
- const from = this.lowerBound_(this.count_);
62
- sorted.copyWithin(from, from + 1, this.count_);
63
- this.count_--;
64
- this.arrival_[this.oldest_] = value;
65
- this.oldest_ = (this.oldest_ + 1) % this.windowLength;
66
- }
67
- else {
68
- this.arrival_[(this.oldest_ + this.count_) % this.windowLength] = value;
65
+ const arrival = this.arrival_;
66
+ let held = arrival.length;
67
+ if (arrival.isFull) {
68
+ probe[0] = this.arrivalValues_[arrival.slotAt(0)];
69
+ const from = this.lowerBound_(held);
70
+ sorted.copyWithin(from, from + 1, held);
71
+ held--;
69
72
  }
73
+ // Once full, the new row takes the slot of the oldest.
74
+ this.arrivalValues_[arrival.pushSlot()] = value;
70
75
  probe[0] = value;
71
- const at = this.lowerBound_(this.count_);
72
- sorted.copyWithin(at + 1, at, this.count_);
76
+ const at = this.lowerBound_(held);
77
+ sorted.copyWithin(at + 1, at, held);
73
78
  sorted[at] = value;
74
- this.count_++;
75
79
  }
76
80
  /**
77
81
  * The median of the values held: the middle one, or the mean of the two
@@ -82,7 +86,7 @@ export class SlidingMedian {
82
86
  * {@link selectInto} instead.
83
87
  */
84
88
  median() {
85
- const count = this.count_;
89
+ const count = this.arrival_.length;
86
90
  if (count === 0) {
87
91
  return Number.NaN;
88
92
  }
@@ -111,15 +115,15 @@ export class SlidingMedian {
111
115
  */
112
116
  selectInto(output, index, rank) {
113
117
  assertIndex_(output, index);
114
- if (!Number.isInteger(rank) || rank < 0 || rank >= this.count_) {
115
- throw new RangeError(`rank must be an integer in [0, ${this.count_}), got ${rank}`);
118
+ const count = this.arrival_.length;
119
+ if (!Number.isInteger(rank) || rank < 0 || rank >= count) {
120
+ throw new RangeError(`rank must be an integer in [0, ${count}), got ${describeValue(rank)}`);
116
121
  }
117
122
  output[index] = this.sorted_[rank];
118
123
  }
119
124
  /** Forgets every value. */
120
125
  reset() {
121
- this.count_ = 0;
122
- this.oldest_ = 0;
126
+ this.arrival_.clear();
123
127
  }
124
128
  /** First index in the sorted prefix whose value is not below `probe_[0]`. */
125
129
  lowerBound_(count) {
@@ -141,7 +145,7 @@ export class SlidingMedian {
141
145
  }
142
146
  function assertIndex_(array, index) {
143
147
  if (!Number.isInteger(index) || index < 0 || index >= array.length) {
144
- throw new RangeError(`index must be an integer in [0, ${array.length}), got ${index}`);
148
+ throw new RangeError(`index must be an integer in [0, ${array.length}), got ${describeValue(index)}`);
145
149
  }
146
150
  }
147
151
  //# sourceMappingURL=sliding-median.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sliding-median.js","sourceRoot":"","sources":["../src/sliding-median.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,aAAa;IAChB,YAAY,CAAS;IAEb,QAAQ,CAAe;IACvB,OAAO,CAAe;IACtB,MAAM,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IACtC,MAAM,GAAG,CAAC,CAAC;IACX,OAAO,GAAG,CAAC,CAAC;IAEpB,wEAAwE;IACxE,YAAY,YAAoB;QAC/B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,IAAI,YAAY,GAAG,CAAC,EAAE,CAAC;YACzD,MAAM,IAAI,UAAU,CACnB,gDAAgD,YAAY,EAAE,CAC9D,CAAC;QACH,CAAC;QAED,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;QAC/C,IAAI,CAAC,OAAO,GAAG,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;IAC/C,CAAC;IAED,qEAAqE;IACrE,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,KAAa;QACjB,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,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IAAI,EAAE,CAAC;YACnB,MAAM,MAAM,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;YACjC,IAAI,MAAM,CAAC,MAAM,CAAE,GAAG,KAAK,EAAE,CAAC;gBAC7B,GAAG,GAAG,MAAM,GAAG,CAAC,CAAC;YAClB,CAAC;iBAAM,CAAC;gBACP,IAAI,GAAG,MAAM,CAAC;YACf,CAAC;QACF,CAAC;QAED,OAAO,GAAG,CAAC;IACZ,CAAC;CACD;AAED,SAAS,YAAY,CAAC,KAAmB,EAAE,KAAa;IACvD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;QACpE,MAAM,IAAI,UAAU,CACnB,mCAAmC,KAAK,CAAC,MAAM,UAAU,aAAa,CAAC,KAAK,CAAC,EAAE,CAC/E,CAAC;IACH,CAAC;AACF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"student-t-distribution.d.ts","sourceRoot":"","sources":["../src/student-t-distribution.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,gBAAgB,EAAE,MAAM,GAAG,YAAY,CAuD3E"}
1
+ {"version":3,"file":"student-t-distribution.d.ts","sourceRoot":"","sources":["../src/student-t-distribution.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,gBAAgB,EAAE,MAAM,GAAG,YAAY,CAuD3E"}
@@ -1,3 +1,4 @@
1
+ import { describeValue } from "@ac-kit/core";
1
2
  import { inverseRegularizedIncompleteBeta, logGamma, regularizedIncompleteBeta, } from "@ac-kit/math-analysis";
2
3
  import { assertProbability_ } from "./_assert-probability.js";
3
4
  import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
@@ -16,7 +17,7 @@ import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
16
17
  */
17
18
  export function studentTDistribution(degreesOfFreedom) {
18
19
  if (!(degreesOfFreedom > 0)) {
19
- throw new RangeError(`studentTDistribution: degreesOfFreedom must be positive, got ${degreesOfFreedom}`);
20
+ throw new RangeError(`studentTDistribution: degreesOfFreedom must be positive, got ${describeValue(degreesOfFreedom)}`);
20
21
  }
21
22
  const half = degreesOfFreedom / 2;
22
23
  const logNormaliser = logGamma(half + 0.5) -
@@ -1 +1 @@
1
- {"version":3,"file":"student-t-distribution.js","sourceRoot":"","sources":["../src/student-t-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gCAAgC,EAChC,QAAQ,EACR,yBAAyB,GACzB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAGzE;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,oBAAoB,CAAC,gBAAwB;IAC5D,IAAI,CAAC,CAAC,gBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CACnB,gEAAgE,gBAAgB,EAAE,CAClF,CAAC;IACH,CAAC;IAED,MAAM,IAAI,GAAG,gBAAgB,GAAG,CAAC,CAAC;IAClC,MAAM,aAAa,GAClB,QAAQ,CAAC,IAAI,GAAG,GAAG,CAAC;QACpB,QAAQ,CAAC,IAAI,CAAC;QACd,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,gBAAgB,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC;IAE5C,6EAA6E;IAC7E,MAAM,SAAS,GAAG,CAAC,CAAS,EAAE,EAAE,CAC/B,GAAG;QACH,yBAAyB,CACxB,IAAI,EACJ,GAAG,EACH,gBAAgB,GAAG,CAAC,gBAAgB,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AE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QAC3D,QAAQ;QACR,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC7D,IAAI,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG;QAC3C,QAAQ,EACP,gBAAgB,GAAG,CAAC;YACnB,CAAC,CAAC,gBAAgB,GAAG,CAAC,gBAAgB,GAAG,CAAC,CAAC;YAC3C,CAAC,CAAC,gBAAgB,GAAG,CAAC;gBACrB,CAAC,CAAC,MAAM,CAAC,iBAAiB;gBAC1B,CAAC,CAAC,MAAM,CAAC,GAAG;KACf,CAAC;AACH,CAAC"}
@@ -0,0 +1,34 @@
1
+ /** The two groups of a sample split by {@link twoClusterSplit}. */
2
+ export type TwoClusterSplit = {
3
+ /** Values in the low group: the first `size` of the sorted sample. */
4
+ readonly size: number;
5
+ readonly lowMean: number;
6
+ readonly highMean: number;
7
+ /** Population variances (divided by the group size). */
8
+ readonly lowVariance: number;
9
+ readonly highVariance: number;
10
+ };
11
+ /**
12
+ * Splits a sorted sample into a low and a high group so that the variance
13
+ * within the groups is smallest — the criterion of Otsu's thresholding, which
14
+ * in one dimension is exact: every cut is tried over prefix sums.
15
+ *
16
+ * Unlike `kmeans` it is deterministic (no seeding), exact rather than a local
17
+ * optimum, and allocates one array of `n + 1` numbers instead of index lists.
18
+ * Equal values are never separated, so a sample with `d` distinct values has `d -
19
+ * 1` candidate cuts.
20
+ *
21
+ * Time O(n), memory O(n).
22
+ *
23
+ * Source: N. Otsu, "A threshold selection method from gray-level histograms",
24
+ * IEEE Transactions on Systems, Man, and Cybernetics 9(1), 1979, pp. 62–66.
25
+ *
26
+ * @example
27
+ * twoClusterSplit([1, 1, 1, 9, 9]); // size 3, lowMean 1, highMean 9
28
+ *
29
+ * @param sorted - The sample in ascending order. Not modified, not re-checked.
30
+ * @returns The best split, or `null` when no cut exists (fewer than two values,
31
+ * or all equal).
32
+ */
33
+ export declare function twoClusterSplit(sorted: readonly number[]): TwoClusterSplit | null;
34
+ //# sourceMappingURL=two-cluster-split.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"two-cluster-split.d.ts","sourceRoot":"","sources":["../src/two-cluster-split.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,MAAM,eAAe,GAAG;IAC7B,sEAAsE;IACtE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,wDAAwD;IACxD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,eAAe,CAC9B,MAAM,EAAE,SAAS,MAAM,EAAE,GACvB,eAAe,GAAG,IAAI,CAiDxB"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Splits a sorted sample into a low and a high group so that the variance
3
+ * within the groups is smallest — the criterion of Otsu's thresholding, which
4
+ * in one dimension is exact: every cut is tried over prefix sums.
5
+ *
6
+ * Unlike `kmeans` it is deterministic (no seeding), exact rather than a local
7
+ * optimum, and allocates one array of `n + 1` numbers instead of index lists.
8
+ * Equal values are never separated, so a sample with `d` distinct values has `d -
9
+ * 1` candidate cuts.
10
+ *
11
+ * Time O(n), memory O(n).
12
+ *
13
+ * Source: N. Otsu, "A threshold selection method from gray-level histograms",
14
+ * IEEE Transactions on Systems, Man, and Cybernetics 9(1), 1979, pp. 62–66.
15
+ *
16
+ * @example
17
+ * twoClusterSplit([1, 1, 1, 9, 9]); // size 3, lowMean 1, highMean 9
18
+ *
19
+ * @param sorted - The sample in ascending order. Not modified, not re-checked.
20
+ * @returns The best split, or `null` when no cut exists (fewer than two values,
21
+ * or all equal).
22
+ */
23
+ export function twoClusterSplit(sorted) {
24
+ const count = sorted.length;
25
+ if (count < 2) {
26
+ return null;
27
+ }
28
+ // Centred on the middle value, so the sums used to choose the cut keep their
29
+ // precision for values far from zero.
30
+ const centre = sorted[count >> 1];
31
+ const sums = new Float64Array(count + 1);
32
+ for (let index = 0; index < count; index++) {
33
+ sums[index + 1] = sums[index] + (sorted[index] - centre);
34
+ }
35
+ let bestSize = 0;
36
+ let bestBetween = -1;
37
+ for (let size = 1; size < count; size++) {
38
+ if (sorted[size - 1] === sorted[size]) {
39
+ continue;
40
+ }
41
+ const highSize = count - size;
42
+ const lowMean = sums[size] / size;
43
+ const highMean = (sums[count] - sums[size]) / highSize;
44
+ const between = size * highSize * (highMean - lowMean) ** 2;
45
+ if (between > bestBetween) {
46
+ bestBetween = between;
47
+ bestSize = size;
48
+ }
49
+ }
50
+ if (bestSize === 0) {
51
+ return null;
52
+ }
53
+ // The variances come from each group's own deviations: the difference of two
54
+ // large sums of squares would lose them for groups far from the centre.
55
+ const low = groupMoments_(sorted, 0, bestSize);
56
+ const high = groupMoments_(sorted, bestSize, count);
57
+ return {
58
+ size: bestSize,
59
+ lowMean: low.mean,
60
+ highMean: high.mean,
61
+ lowVariance: low.variance,
62
+ highVariance: high.variance,
63
+ };
64
+ }
65
+ function groupMoments_(sorted, from, to) {
66
+ const first = sorted[from];
67
+ let offset = 0;
68
+ for (let index = from; index < to; index++) {
69
+ offset += sorted[index] - first;
70
+ }
71
+ const mean = first + offset / (to - from);
72
+ let squares = 0;
73
+ for (let index = from; index < to; index++) {
74
+ squares += (sorted[index] - mean) ** 2;
75
+ }
76
+ return { mean, variance: squares / (to - from) };
77
+ }
78
+ //# sourceMappingURL=two-cluster-split.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"two-cluster-split.js","sour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