@ac-kit/math-stats 0.4.0 → 0.6.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 (135) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/README.md +106 -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/_histogram-rules.d.ts +15 -0
  7. package/dist/_histogram-rules.d.ts.map +1 -0
  8. package/dist/_histogram-rules.js +208 -0
  9. package/dist/_histogram-rules.js.map +1 -0
  10. package/dist/_satterthwaite.d.ts +13 -0
  11. package/dist/_satterthwaite.d.ts.map +1 -0
  12. package/dist/_satterthwaite.js +25 -0
  13. package/dist/_satterthwaite.js.map +1 -0
  14. package/dist/adjust-p-values.d.ts +41 -0
  15. package/dist/adjust-p-values.d.ts.map +1 -0
  16. package/dist/adjust-p-values.js +75 -0
  17. package/dist/adjust-p-values.js.map +1 -0
  18. package/dist/ashman-d.d.ts +25 -0
  19. package/dist/ashman-d.d.ts.map +1 -0
  20. package/dist/ashman-d.js +24 -0
  21. package/dist/ashman-d.js.map +1 -0
  22. package/dist/beta-distribution.d.ts.map +1 -1
  23. package/dist/beta-distribution.js +3 -2
  24. package/dist/beta-distribution.js.map +1 -1
  25. package/dist/bimodality-coefficient.d.ts +32 -0
  26. package/dist/bimodality-coefficient.d.ts.map +1 -0
  27. package/dist/bimodality-coefficient.js +63 -0
  28. package/dist/bimodality-coefficient.js.map +1 -0
  29. package/dist/binomial-distribution.d.ts.map +1 -1
  30. package/dist/binomial-distribution.js +2 -1
  31. package/dist/binomial-distribution.js.map +1 -1
  32. package/dist/chi-squared-distribution.d.ts.map +1 -1
  33. package/dist/chi-squared-distribution.js +2 -1
  34. package/dist/chi-squared-distribution.js.map +1 -1
  35. package/dist/chi-squared-test.js +2 -2
  36. package/dist/chi-squared-test.js.map +1 -1
  37. package/dist/combine-independent-estimates.d.ts +53 -0
  38. package/dist/combine-independent-estimates.d.ts.map +1 -0
  39. package/dist/combine-independent-estimates.js +71 -0
  40. package/dist/combine-independent-estimates.js.map +1 -0
  41. package/dist/confidence-interval.d.ts.map +1 -1
  42. package/dist/confidence-interval.js +2 -1
  43. package/dist/confidence-interval.js.map +1 -1
  44. package/dist/exponential-distribution.d.ts.map +1 -1
  45. package/dist/exponential-distribution.js +2 -1
  46. package/dist/exponential-distribution.js.map +1 -1
  47. package/dist/f-distribution.d.ts.map +1 -1
  48. package/dist/f-distribution.js +3 -2
  49. package/dist/f-distribution.js.map +1 -1
  50. package/dist/gamma-distribution.d.ts.map +1 -1
  51. package/dist/gamma-distribution.js +3 -2
  52. package/dist/gamma-distribution.js.map +1 -1
  53. package/dist/geometric-distribution.d.ts.map +1 -1
  54. package/dist/geometric-distribution.js +2 -1
  55. package/dist/geometric-distribution.js.map +1 -1
  56. package/dist/hartigan-dip.d.ts +65 -0
  57. package/dist/hartigan-dip.d.ts.map +1 -0
  58. package/dist/hartigan-dip.js +273 -0
  59. package/dist/hartigan-dip.js.map +1 -0
  60. package/dist/hierarchical-cluster.d.ts.map +1 -1
  61. package/dist/hierarchical-cluster.js +10 -5
  62. package/dist/hierarchical-cluster.js.map +1 -1
  63. package/dist/histogram-edges.d.ts +92 -0
  64. package/dist/histogram-edges.d.ts.map +1 -0
  65. package/dist/histogram-edges.js +325 -0
  66. package/dist/histogram-edges.js.map +1 -0
  67. package/dist/histogram.d.ts +80 -4
  68. package/dist/histogram.d.ts.map +1 -1
  69. package/dist/histogram.js +86 -9
  70. package/dist/histogram.js.map +1 -1
  71. package/dist/hypergeometric-distribution.js +2 -2
  72. package/dist/hypergeometric-distribution.js.map +1 -1
  73. package/dist/index.d.ts +10 -0
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +10 -0
  76. package/dist/index.js.map +1 -1
  77. package/dist/kmeans.d.ts.map +1 -1
  78. package/dist/kmeans.js +12 -7
  79. package/dist/kmeans.js.map +1 -1
  80. package/dist/kmedoids.js +2 -1
  81. package/dist/kmedoids.js.map +1 -1
  82. package/dist/log-normal-distribution.d.ts.map +1 -1
  83. package/dist/log-normal-distribution.js +2 -1
  84. package/dist/log-normal-distribution.js.map +1 -1
  85. package/dist/negative-binomial-distribution.d.ts.map +1 -1
  86. package/dist/negative-binomial-distribution.js +3 -2
  87. package/dist/negative-binomial-distribution.js.map +1 -1
  88. package/dist/normal-distribution.d.ts.map +1 -1
  89. package/dist/normal-distribution.js +2 -1
  90. package/dist/normal-distribution.js.map +1 -1
  91. package/dist/normal-quantile.d.ts.map +1 -1
  92. package/dist/normal-quantile.js +2 -1
  93. package/dist/normal-quantile.js.map +1 -1
  94. package/dist/poisson-distribution.d.ts.map +1 -1
  95. package/dist/poisson-distribution.js +2 -1
  96. package/dist/poisson-distribution.js.map +1 -1
  97. package/dist/quantile.d.ts +26 -2
  98. package/dist/quantile.d.ts.map +1 -1
  99. package/dist/quantile.js +69 -9
  100. package/dist/quantile.js.map +1 -1
  101. package/dist/random-effects.d.ts +56 -9
  102. package/dist/random-effects.d.ts.map +1 -1
  103. package/dist/random-effects.js +59 -12
  104. package/dist/random-effects.js.map +1 -1
  105. package/dist/regression.d.ts +26 -2
  106. package/dist/regression.d.ts.map +1 -1
  107. package/dist/regression.js +175 -12
  108. package/dist/regression.js.map +1 -1
  109. package/dist/running-statistics.d.ts +103 -0
  110. package/dist/running-statistics.d.ts.map +1 -0
  111. package/dist/running-statistics.js +283 -0
  112. package/dist/running-statistics.js.map +1 -0
  113. package/dist/satterthwaite-degrees-of-freedom.d.ts +44 -0
  114. package/dist/satterthwaite-degrees-of-freedom.d.ts.map +1 -0
  115. package/dist/satterthwaite-degrees-of-freedom.js +41 -0
  116. package/dist/satterthwaite-degrees-of-freedom.js.map +1 -0
  117. package/dist/sliding-median.d.ts +1 -2
  118. package/dist/sliding-median.d.ts.map +1 -1
  119. package/dist/sliding-median.js +28 -24
  120. package/dist/sliding-median.js.map +1 -1
  121. package/dist/student-t-distribution.d.ts.map +1 -1
  122. package/dist/student-t-distribution.js +2 -1
  123. package/dist/student-t-distribution.js.map +1 -1
  124. package/dist/two-cluster-split.d.ts +34 -0
  125. package/dist/two-cluster-split.d.ts.map +1 -0
  126. package/dist/two-cluster-split.js +78 -0
  127. package/dist/two-cluster-split.js.map +1 -0
  128. package/dist/variance-of-sample-median.d.ts +40 -0
  129. package/dist/variance-of-sample-median.d.ts.map +1 -0
  130. package/dist/variance-of-sample-median.js +162 -0
  131. package/dist/variance-of-sample-median.js.map +1 -0
  132. package/dist/welch-t-statistic.d.ts.map +1 -1
  133. package/dist/welch-t-statistic.js +7 -3
  134. package/dist/welch-t-statistic.js.map +1 -1
  135. package/package.json +9 -8
package/dist/quantile.js CHANGED
@@ -1,23 +1,83 @@
1
1
  /**
2
2
  * Linear-interpolated quantile of an already-sorted sample.
3
3
  *
4
- * @param sorted Values in ascending order. Not modified, not re-checked.
4
+ * Exact at the sample's values and free of overflow between them, even when two
5
+ * neighbours are further apart than `Number.MAX_VALUE`.
6
+ *
7
+ * @param sorted Values in ascending order within the range, any array-like (a
8
+ * typed array included). Not modified, not re-checked.
5
9
  * @param fraction Quantile to compute, in `[0, 1]`.
10
+ * @param from First position of the sample in `sorted`: one group's slice of a
11
+ * scratch buffer reads in place, without a `subarray` view. Default `0`.
12
+ * @param to End position, excluded. Default `sorted.length`.
6
13
  * @returns The quantile, or `NaN` for an empty sample.
14
+ * @throws {RangeError} When `from` and `to` are not integers with `0 ≤ from ≤
15
+ * to ≤ sorted.length`.
7
16
  */
8
- export function quantile(sorted, fraction) {
9
- if (sorted.length === 0) {
10
- return Number.NaN;
17
+ export function quantile(sorted, fraction, from = 0, to = sorted.length) {
18
+ assertRange_("quantile", sorted, from, to);
19
+ write_(RESULT_, 0, sorted, fraction, from, to);
20
+ return RESULT_[0];
21
+ }
22
+ /** Where {@link quantile} reads its result back. */
23
+ const RESULT_ = new Float64Array(1);
24
+ function assertRange_(name, sorted, from, to) {
25
+ if (!Number.isInteger(from) ||
26
+ !Number.isInteger(to) ||
27
+ from < 0 ||
28
+ from > to ||
29
+ to > sorted.length) {
30
+ throw new RangeError(`${name}: [${from}, ${to}) is not a range of [0, ${sorted.length})`);
11
31
  }
12
- if (sorted.length === 1) {
13
- return sorted[0];
32
+ }
33
+ /**
34
+ * Writes {@link quantile} to `target[index]`, which allocates nothing: a double
35
+ * returned by a call the engine does not inline is boxed, one written to an
36
+ * array is not. One sorted scratch buffer can so fill an output column group by
37
+ * group.
38
+ *
39
+ * @param target A `Float64Array`, or a `number[]` of doubles.
40
+ * @param index The position written.
41
+ * @param sorted As for {@link quantile}.
42
+ * @param fraction As for {@link quantile}.
43
+ * @param from As for {@link quantile}.
44
+ * @param to As for {@link quantile}.
45
+ * @throws {RangeError} As {@link quantile}.
46
+ */
47
+ export function writeQuantile(target, index, sorted, fraction, from = 0, to = sorted.length) {
48
+ assertRange_("writeQuantile", sorted, from, to);
49
+ write_(target, index, sorted, fraction, from, to);
50
+ }
51
+ function write_(target, index, sorted, fraction, from, to) {
52
+ const length = to - from;
53
+ if (length === 0) {
54
+ target[index] = Number.NaN;
55
+ return;
14
56
  }
15
- const position = (sorted.length - 1) * fraction;
57
+ if (length === 1) {
58
+ target[index] = sorted[from];
59
+ return;
60
+ }
61
+ const position = (length - 1) * fraction;
16
62
  const lower = Math.floor(position);
17
63
  const upper = Math.ceil(position);
18
64
  if (lower === upper) {
19
- return sorted[lower];
65
+ target[index] = sorted[from + lower];
66
+ return;
67
+ }
68
+ const lowerValue = sorted[from + lower];
69
+ const upperValue = sorted[from + upper];
70
+ const weight = position - lower;
71
+ const difference = upperValue - lowerValue;
72
+ if (difference === Infinity &&
73
+ lowerValue !== -Infinity &&
74
+ upperValue !== Infinity) {
75
+ // Finite neighbours further apart than the largest double: their halves,
76
+ // exact at that magnitude, interpolate without overflow.
77
+ target[index] =
78
+ 2 * (lowerValue / 2 + (upperValue / 2 - lowerValue / 2) * weight);
79
+ return;
20
80
  }
21
- return (sorted[lower] + (sorted[upper] - sorted[lower]) * (position - lower));
81
+ target[index] = lowerValue + difference * weight;
22
82
  }
23
83
  //# sourceMappingURL=quantile.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"quantile.js","sourceRoot":"","sources":["../src/quantile.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAyB,EAAE,QAAgB;IACnE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC,CAAC,CAAE,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAClC,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;QACrB,OAAO,MAAM,CAAC,KAAK,CAAE,CAAC;IACvB,CAAC;IACD,OAAO,CACN,MAAM,CAAC,KAAK,CAAE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAE,GAAG,MAAM,CAAC,KAAK,CAAE,CAAC,GAAG,CAAC,QAAQ,GAAG,KAAK,CAAC,CACvE,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"quantile.js","sourceRoot":"","sources":["../src/quantile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,QAAQ,CACvB,MAAyB,EACzB,QAAgB,EAChB,IAAI,GAAG,CAAC,EACR,EAAE,GAAW,MAAM,CAAC,MAAM;IAE1B,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IA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,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;QAClB,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,CAAE,CAAC;QAC9B,OAAO;IACR,CAAC;IAED,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC;IACzC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAClC,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;QACrB,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,IAAI,GAAG,KAAK,CAAE,CAAC;QACtC,OAAO;IACR,CAAC;IACD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,GAAG,KAAK,CAAE,CAAC;IACzC,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,GAAG,KAAK,CAAE,CAAC;IACzC,MAAM,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;IAChC,MAAM,UAAU,GAAG,UAAU,GAAG,UAAU,CAAC;IAC3C,IACC,UAAU,KAAK,QAAQ;QACvB,UAAU,KAAK,CAAC,QAAQ;QACxB,UAAU,KAAK,QAAQ,EACtB,CAAC;QACF,yEAAyE;QACzE,yDAAyD;QACzD,MAAM,CAAC,KAAK,CAAC;YACZ,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,GAAG,UAAU,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;QACnE,OAAO;IACR,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,CAAC;AAClD,CAAC"}
@@ -5,9 +5,46 @@ export type RandomEffectsGroup = {
5
5
  /** Variance **of the estimate**, not of the underlying samples. */
6
6
  readonly variance: number;
7
7
  };
8
+ /**
9
+ * How {@link poolRandomEffects} turns the weighted average into an interval.
10
+ * Both estimate τ² by DerSimonian–Laird and share the point estimate; they
11
+ * differ in the estimate's variance and the reference distribution.
12
+ *
13
+ * - `"dersimonian-laird"` — variance `1 / Σw`, normal quantile. Under-covers with
14
+ * few groups (a nominal 95 % interval from two groups holds the true value
15
+ * far less often), because it ignores that τ² itself was estimated.
16
+ * - `"hartung-knapp"` — Hartung–Knapp–Sidik–Jonkman: variance `max(q, 1) / Σw`,
17
+ * where `q = Σ w (y − μ̂)² / (k − 1)` is the observed scatter of the groups
18
+ * around the pooled estimate, with Student's t on `k − 1` degrees of freedom.
19
+ * Unlike DerSimonian–Laird's, its interval does not under-cover with few
20
+ * groups (IntHout, Ioannidis and Borm 2014); the truncation at one keeps it
21
+ * never narrower than DerSimonian–Laird's (Röver, Knapp and Friede 2015),
22
+ * which makes it conservative when the groups agree closely and with two or
23
+ * three groups. Because `q` follows the observed scatter, an underestimated
24
+ * within-group variance cannot make the interval too narrow. Needs at least
25
+ * two groups.
26
+ */
27
+ export type RandomEffectsMethod = "dersimonian-laird" | "hartung-knapp";
28
+ /** Options of {@link poolRandomEffects}. */
29
+ export type PoolRandomEffectsOptions = {
30
+ /** The interval method. Default `"dersimonian-laird"`. */
31
+ readonly method?: RandomEffectsMethod;
32
+ };
8
33
  export type PooledEstimate = {
9
34
  readonly estimate: number;
10
35
  readonly interval: ConfidenceInterval;
36
+ /**
37
+ * Standard error of {@link PooledEstimate.estimate} under the chosen method;
38
+ * the interval is `estimate ± quantile · standardError`.
39
+ */
40
+ readonly standardError: number;
41
+ /**
42
+ * Degrees of freedom of the reference distribution the interval uses: `k − 1`
43
+ * for Hartung–Knapp's Student's t, `Infinity` for DerSimonian–Laird's normal.
44
+ * A test of the estimate against a value refers `(estimate − value) /
45
+ * standardError` to that distribution.
46
+ */
47
+ readonly degreesOfFreedom: number;
11
48
  /** Between-group variance component, τ². Zero when groups agree. */
12
49
  readonly betweenVariance: number;
13
50
  /**
@@ -20,8 +57,9 @@ export type PooledEstimate = {
20
57
  readonly groups: number;
21
58
  };
22
59
  /**
23
- * Pool estimates from several groups under a random-effects model, by the
24
- * DerSimonian–Laird method.
60
+ * Pool estimates from several groups under a random-effects model, with the
61
+ * between-group variance τ² estimated by the DerSimonian–Laird method and the
62
+ * interval by the method `options` names.
25
63
  *
26
64
  * Use this when each group is one draw from a population of contexts rather
27
65
  * than a repeat of the same measurement: separate processes, machines or
@@ -29,17 +67,26 @@ export type PooledEstimate = {
29
67
  * it did not exist and report an interval far too narrow; this estimates that
30
68
  * spread as τ² and widens the interval by it.
31
69
  *
32
- * With one group there is no between-group spread to estimate, and the result
33
- * is that group's own estimate and interval.
70
+ * With few groups, prefer `{ method: "hartung-knapp" }`: the default
71
+ * DerSimonian–Laird interval treats the estimated τ² as known and is too narrow
72
+ * below about ten groups (see {@link RandomEffectsMethod}).
73
+ *
74
+ * With one group there is no between-group spread to estimate: the
75
+ * DerSimonian–Laird result is that group's own estimate and interval, and
76
+ * Hartung–Knapp, which has no degrees of freedom left, refuses.
34
77
  *
35
- * O(k) in the number of groups.
78
+ * O(k) in the number of groups, plus one t-quantile for Hartung–Knapp.
36
79
  *
37
80
  * @param groups One entry per group. Not modified.
38
81
  * @param level Confidence level in `(0, 1)`. Default `0.95`.
39
- * @returns The pooled estimate, its interval, and how much the groups
82
+ * @param options The interval method.
83
+ * @returns The pooled estimate, its interval and standard error, the degrees of
84
+ * freedom of the interval's reference distribution, and how much the groups
40
85
  * disagreed.
41
- * @throws {RangeError} When `groups` is empty, when any variance is not a
42
- * positive finite number, or when `level` is outside `(0, 1)`.
86
+ * @throws {RangeError} When `groups` is empty, when any estimate is not finite,
87
+ * when any variance is not a positive finite number, when `level` is outside
88
+ * `(0, 1)`, when `method` is unknown, or when Hartung–Knapp is given a single
89
+ * group.
43
90
  */
44
- export declare function poolRandomEffects(groups: readonly RandomEffectsGroup[], level?: number): PooledEstimate;
91
+ export declare function poolRandomEffects(groups: readonly RandomEffectsGroup[], level?: number, options?: PoolRandomEffectsOptions): PooledEstimate;
45
92
  //# sourceMappingURL=random-effects.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"random-effects.d.ts","sourceRoot":"","sources":["../src/random-effects.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAG9D,8DAA8D;AAC9D,MAAM,MAAM,kBAAkB,GAAG;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,mEAAmE;IACnE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC,oEAAoE;IACpE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC;;;;;OAKG;IACH,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,SAAS,kBAAkB,EAAE,EACrC,KAAK,SAAO,GACV,cAAc,CA8ChB"}
1
+ {"version":3,"file":"random-effects.d.ts","sourceRoot":"","sources":["../src/random-effects.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAI9D,8DAA8D;AAC9D,MAAM,MAAM,kBAAkB,GAAG;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,mEAAmE;IACnE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,GAAG,eAAe,CAAC;AAExE,4CAA4C;AAC5C,MAAM,MAAM,wBAAwB,GAAG;IACtC,0DAA0D;IAC1D,QAAQ,CAAC,MAAM,CAAC,EAAE,mBAAmB,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC;;;OAGG;IACH,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B;;;;;OAKG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,oEAAoE;IACpE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC;;;;;OAKG;IACH,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,SAAS,kBAAkB,EAAE,EACrC,KAAK,SAAO,EACZ,OAAO,GAAE,wBAA6B,GACpC,cAAc,CAkGhB"}
@@ -1,8 +1,10 @@
1
- import { map, sumPrecise } from "@ac-kit/core";
1
+ import { describeValue, map, sumPrecise } from "@ac-kit/core";
2
2
  import { normalQuantile } from "./normal-quantile.js";
3
+ import { studentTDistribution } from "./student-t-distribution.js";
3
4
  /**
4
- * Pool estimates from several groups under a random-effects model, by the
5
- * DerSimonian–Laird method.
5
+ * Pool estimates from several groups under a random-effects model, with the
6
+ * between-group variance τ² estimated by the DerSimonian–Laird method and the
7
+ * interval by the method `options` names.
6
8
  *
7
9
  * Use this when each group is one draw from a population of contexts rather
8
10
  * than a repeat of the same measurement: separate processes, machines or
@@ -10,26 +12,45 @@ import { normalQuantile } from "./normal-quantile.js";
10
12
  * it did not exist and report an interval far too narrow; this estimates that
11
13
  * spread as τ² and widens the interval by it.
12
14
  *
13
- * With one group there is no between-group spread to estimate, and the result
14
- * is that group's own estimate and interval.
15
+ * With few groups, prefer `{ method: "hartung-knapp" }`: the default
16
+ * DerSimonian–Laird interval treats the estimated τ² as known and is too narrow
17
+ * below about ten groups (see {@link RandomEffectsMethod}).
15
18
  *
16
- * O(k) in the number of groups.
19
+ * With one group there is no between-group spread to estimate: the
20
+ * DerSimonian–Laird result is that group's own estimate and interval, and
21
+ * Hartung–Knapp, which has no degrees of freedom left, refuses.
22
+ *
23
+ * O(k) in the number of groups, plus one t-quantile for Hartung–Knapp.
17
24
  *
18
25
  * @param groups One entry per group. Not modified.
19
26
  * @param level Confidence level in `(0, 1)`. Default `0.95`.
20
- * @returns The pooled estimate, its interval, and how much the groups
27
+ * @param options The interval method.
28
+ * @returns The pooled estimate, its interval and standard error, the degrees of
29
+ * freedom of the interval's reference distribution, and how much the groups
21
30
  * disagreed.
22
- * @throws {RangeError} When `groups` is empty, when any variance is not a
23
- * positive finite number, or when `level` is outside `(0, 1)`.
31
+ * @throws {RangeError} When `groups` is empty, when any estimate is not finite,
32
+ * when any variance is not a positive finite number, when `level` is outside
33
+ * `(0, 1)`, when `method` is unknown, or when Hartung–Knapp is given a single
34
+ * group.
24
35
  */
25
- export function poolRandomEffects(groups, level = 0.95) {
36
+ export function poolRandomEffects(groups, level = 0.95, options = {}) {
37
+ const method = options.method ?? "dersimonian-laird";
26
38
  if (groups.length === 0) {
27
39
  throw new RangeError("poolRandomEffects: at least one group is required");
28
40
  }
29
- if (level <= 0 || level >= 1) {
41
+ if (!(level > 0 && level < 1)) {
30
42
  throw new RangeError("poolRandomEffects: level must be in (0, 1)");
31
43
  }
44
+ if (method !== "dersimonian-laird" && method !== "hartung-knapp") {
45
+ throw new RangeError(`poolRandomEffects: unknown method ${describeValue(method)}`);
46
+ }
47
+ if (method === "hartung-knapp" && groups.length < 2) {
48
+ throw new RangeError("poolRandomEffects: hartung-knapp needs at least two groups");
49
+ }
32
50
  for (const group of groups) {
51
+ if (!Number.isFinite(group.estimate)) {
52
+ throw new RangeError("poolRandomEffects: every group estimate must be finite");
53
+ }
33
54
  // A zero-variance group would claim infinite precision and take the whole
34
55
  // weight. It never means that: it means the estimate was quantised by a
35
56
  // coarse clock, and the caller has to decide what to do about it.
@@ -44,7 +65,31 @@ export function poolRandomEffects(groups, level = 0.95) {
44
65
  const weightSum = sumPrecise(weights);
45
66
  const weightedSum = sumPrecise(map(groups, (group, index) => weights[index] * group.estimate));
46
67
  const estimate = weightedSum / weightSum;
47
- const halfWidth = normalQuantile((1 + level) / 2) / Math.sqrt(weightSum);
68
+ let standardError;
69
+ let degreesOfFreedom;
70
+ let criticalValue;
71
+ // The lower tail, negated: `(1 + level) / 2` rounds to 1 for a level within
72
+ // an ulp of 1, where the quantile is infinite, while `(1 − level) / 2` is
73
+ // exact.
74
+ const tailProbability = (1 - level) / 2;
75
+ if (method === "hartung-knapp") {
76
+ degreesOfFreedom = groups.length - 1;
77
+ const scatter = sumPrecise(map(groups, (group, index) => {
78
+ const deviation = group.estimate - estimate;
79
+ return weights[index] * deviation * deviation;
80
+ })) / degreesOfFreedom;
81
+ // √max(q, 1) is exactly 1 when truncated, so the standard error is then
82
+ // bit-for-bit DerSimonian–Laird's.
83
+ standardError = Math.sqrt(Math.max(scatter, 1)) / Math.sqrt(weightSum);
84
+ criticalValue =
85
+ -studentTDistribution(degreesOfFreedom).quantile(tailProbability);
86
+ }
87
+ else {
88
+ degreesOfFreedom = Number.POSITIVE_INFINITY;
89
+ standardError = 1 / Math.sqrt(weightSum);
90
+ criticalValue = -normalQuantile(tailProbability);
91
+ }
92
+ const halfWidth = criticalValue * standardError;
48
93
  return {
49
94
  estimate,
50
95
  interval: {
@@ -54,6 +99,8 @@ export function poolRandomEffects(groups, level = 0.95) {
54
99
  halfWidth,
55
100
  level,
56
101
  },
102
+ standardError,
103
+ degreesOfFreedom,
57
104
  betweenVariance,
58
105
  heterogeneity: heterogeneity_(groups),
59
106
  groups: groups.length,
@@ -1 +1 @@
1
- {"version":3,"file":"random-effects.js","sourceRoot":"","sources":["../src/random-effects.ts"],"names":[],"mappings":"AAAA,OAAO,EA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Slope of the ordinary-least-squares line through paired `xValues` and
3
3
  * `yValues`.
4
4
  *
5
+ * Right for every finite input, whatever its magnitudes. When every non-zero
6
+ * value lies within 2⁻³⁰⁰ to 2³⁰⁰ (about 1e-90 to 1e90), the result is within
7
+ * four units in the last place of the exact slope of the given values: a
8
+ * relative error below 2⁻⁵¹, or less than four times `Number.MIN_VALUE` for a
9
+ * subnormal slope. The means are summed exactly and the deviations corrected
10
+ * for their rounding (the corrected two-pass algorithm), with every deviation
11
+ * and product carried together with its exact rounding error; where a bound on
12
+ * what is still lost cannot show that accuracy — when the products of the
13
+ * deviations cancel to less than about (4n + 11)·2⁻⁵¹ of their magnitudes, 2⁻⁴⁴
14
+ * for twenty values, as for `[0, 0, 1, -1]` against `[2¹⁰⁰, 2¹⁰⁰, 1, -1]` — the
15
+ * slope is computed exactly instead. Past those bounds, where squares and
16
+ * products could overflow, underflow or lose a term next to one a thousand
17
+ * binary orders larger, the slope is always computed exactly from the values as
18
+ * integers and rounded once: it is the binary64 nearest the true slope, even
19
+ * when that is subnormal (`±0` when it underflows) or `±Infinity`, which it is
20
+ * only when the true slope is past `Number.MAX_VALUE`. A fit of a hundred
21
+ * values at those extremes costs about a hundred big-integer products of a few
22
+ * thousand bits.
23
+ *
24
+ * Time complexity: O(n), with O(n) big-integer operations on numbers of up to
25
+ * about 4300 bits when computed exactly. Memory: O(1), or O(n) integers when
26
+ * computed exactly.
27
+ *
5
28
  * @param xValues Independent-variable samples. Not modified.
6
29
  * @param yValues Dependent-variable samples paired by index. Not modified.
7
- * @returns The slope, or `0` for fewer than two values — the same
8
- * underdetermined-input convention as {@link sampleVariance}.
30
+ * @returns The slope; `0` for fewer than two values, or when every `x` is equal
31
+ * — the same underdetermined-input convention as {@link sampleVariance};
32
+ * `NaN` when a value is not finite.
9
33
  * @throws {RangeError} If `xValues` and `yValues` have different lengths.
10
34
  */
11
35
  export declare function linearRegressionSlope(xValues: readonly number[], yValues: readonly number[]): number;
@@ -1 +1 @@
1
- {"version":3,"file":"regression.d.ts","sourceRoot":"","sources":["../src/regression.ts"],"names":[],"mappings":"AAIA;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CA0BR"}
1
+ {"version":3,"file":"regression.d.ts","sourceRoot":"","sources":["../src/regression.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CA8BR"}
@@ -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,M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