@seanhogg/builderforce-memory 2026.10.72 → 2026.10.74
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.
- package/dist/knowledge/index.d.ts +1 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/model.d.ts +17 -0
- package/dist/knowledge/model.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/index.d.ts +4 -1
- package/dist/knowledge/pack/sections/index.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/index.js +7 -3
- package/dist/knowledge/pack/sections/index.js.map +1 -1
- package/dist/knowledge/pack/sections/rows.d.ts +9 -0
- package/dist/knowledge/pack/sections/rows.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/rows.js +46 -1
- package/dist/knowledge/pack/sections/rows.js.map +1 -1
- package/dist/knowledge/reviewFinish.d.ts.map +1 -1
- package/dist/knowledge/reviewFinish.js +4 -1
- package/dist/knowledge/reviewFinish.js.map +1 -1
- package/dist/knowledge/reviewModel.d.ts +6 -1
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.js +5 -0
- package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
- package/dist/stats/distributions.d.ts +6 -0
- package/dist/stats/distributions.d.ts.map +1 -1
- package/dist/stats/distributions.js +10 -0
- package/dist/stats/distributions.js.map +1 -1
- package/dist/stats/index.d.ts +1 -1
- package/dist/stats/index.d.ts.map +1 -1
- package/dist/stats/tests.d.ts +50 -7
- package/dist/stats/tests.d.ts.map +1 -1
- package/dist/stats/tests.js +241 -64
- package/dist/stats/tests.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/regression.scopeMeasured.test.ts +121 -0
- package/src/contract/regression.wilcoxon.test.ts +180 -0
- package/src/contract/types.contract.test.ts +7 -0
- package/src/knowledge/index.ts +1 -1
- package/src/knowledge/model.ts +18 -0
- package/src/knowledge/pack/pack.testkit.test-util.ts +4 -1
- package/src/knowledge/pack/sections/index.ts +7 -3
- package/src/knowledge/pack/sections/rows.ts +39 -1
- package/src/knowledge/reviewFinish.ts +4 -1
- package/src/knowledge/reviewModel.ts +6 -1
- package/src/knowledge/stores/localSnapshotStore.ts +4 -0
- package/src/stats/distributions.ts +10 -0
- package/src/stats/index.ts +1 -1
- package/src/stats/tests.ts +238 -64
package/src/stats/tests.ts
CHANGED
|
@@ -1,120 +1,294 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Statistical tests for the stats module (PRD 41 Part D / D13): Wilcoxon signed-rank
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* (exact per n, normal approximation for large n) with the paired Hodges–Lehmann
|
|
4
|
+
* estimator, a seeded paired bootstrap CI (mean or Hodges–Lehmann), and the sign-test
|
|
5
|
+
* SPRT. All deterministic under their seeds.
|
|
5
6
|
*/
|
|
6
7
|
|
|
8
|
+
import { regularizedUpperIncompleteGamma } from "./distributions.js";
|
|
7
9
|
import { createRng, type Rng } from "./prng.js";
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
// Bound once at module scope for the hot loops: a bare `Math.x` is a global lookup on every call, which inside a vm
|
|
12
|
+
// context (the jest runner) costs ~1 µs each (the same reason as distributions.ts).
|
|
13
|
+
const { floor, max, min } = Math;
|
|
14
|
+
|
|
15
|
+
/** Up to this many nonzero differences the p-value is EXACT; above it, the normal approximation. */
|
|
16
|
+
export const WILCOXON_EXACT_MAX_N = 50;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Upper standard-normal tail P(Z ≥ z) for z ≥ 0: ½·erfc(z/√2) = ½·Q(½, z²/2), with Q taken
|
|
20
|
+
* directly (not 1 − P) so a far tail stays a tiny positive number instead of underflowing to 0.
|
|
21
|
+
*/
|
|
22
|
+
function normalUpperTail(z: number): number {
|
|
23
|
+
if (z <= 0) return 0.5;
|
|
24
|
+
return 0.5 * regularizedUpperIncompleteGamma(0.5, (z * z) / 2);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The paired differences `after − before` that are finite, and how many were not. */
|
|
28
|
+
function finiteDifferences(before: readonly number[], after: readonly number[]): { diffs: number[]; nonFinite: number } {
|
|
29
|
+
const diffs: number[] = [];
|
|
30
|
+
let nonFinite = 0;
|
|
31
|
+
const n = Math.min(before.length, after.length);
|
|
32
|
+
for (let i = 0; i < n; i++) {
|
|
33
|
+
const d = after[i]! - before[i]!;
|
|
34
|
+
if (Number.isFinite(d)) diffs.push(d);
|
|
35
|
+
else nonFinite++;
|
|
36
|
+
}
|
|
37
|
+
return { diffs, nonFinite };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Exact two-sided p-value: the permutation distribution of W+ given THESE ranks (mid-ranks
|
|
42
|
+
* for ties), each sign ± with probability ½. Ranks are doubled so mid-ranks are integers,
|
|
43
|
+
* and the distribution is built by DP over subset sums for this n — never a table built for
|
|
44
|
+
* another n. Probabilities (halved at each step) rather than counts, so no overflow.
|
|
45
|
+
*/
|
|
46
|
+
export function exactSignedRankPValue(doubledRanks: readonly number[], doubledWPlus: number): number {
|
|
47
|
+
const total = doubledRanks.reduce((a, b) => a + b, 0);
|
|
48
|
+
let dp = new Float64Array(total + 1);
|
|
14
49
|
dp[0] = 1;
|
|
15
|
-
for (
|
|
16
|
-
|
|
50
|
+
for (const r of doubledRanks) {
|
|
51
|
+
const next = new Float64Array(total + 1);
|
|
52
|
+
for (let s = 0; s <= total; s++) {
|
|
53
|
+
const p = dp[s]!;
|
|
54
|
+
if (p === 0) continue;
|
|
55
|
+
next[s] = next[s]! + p / 2;
|
|
56
|
+
next[s + r] = next[s + r]! + p / 2;
|
|
57
|
+
}
|
|
58
|
+
dp = next;
|
|
17
59
|
}
|
|
18
|
-
|
|
19
|
-
let
|
|
60
|
+
let lower = 0; // P(W+ ≤ observed)
|
|
61
|
+
let upper = 0; // P(W+ ≥ observed)
|
|
20
62
|
for (let s = 0; s <= total; s++) {
|
|
21
|
-
|
|
22
|
-
|
|
63
|
+
if (s <= doubledWPlus) lower += dp[s]!;
|
|
64
|
+
if (s >= doubledWPlus) upper += dp[s]!;
|
|
65
|
+
}
|
|
66
|
+
return Math.min(1, 2 * Math.min(lower, upper));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Normal approximation for large n: mean n(n+1)/4, variance n(n+1)(2n+1)/24 reduced by
|
|
71
|
+
* Σ(t³ − t)/48 over tie groups, continuity correction ½, two-sided normal tail.
|
|
72
|
+
*/
|
|
73
|
+
export function normalSignedRankPValue(n: number, wPlus: number, tieGroups: readonly number[]): number {
|
|
74
|
+
const mean = (n * (n + 1)) / 4;
|
|
75
|
+
const tieCorrection = tieGroups.reduce((acc, t) => acc + (t * t * t - t), 0) / 48;
|
|
76
|
+
const variance = (n * (n + 1) * (2 * n + 1)) / 24 - tieCorrection;
|
|
77
|
+
if (!(variance > 0)) return 1;
|
|
78
|
+
const z = Math.max(0, Math.abs(wPlus - mean) - 0.5) / Math.sqrt(variance);
|
|
79
|
+
return Math.min(1, 2 * normalUpperTail(z));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Walsh averages (sᵢ + sⱼ)/2, i ≤ j, of an ASCENDING array, that are ≤ x: one O(n) two-pointer sweep. */
|
|
83
|
+
function walshCountAtMost(s: readonly number[], x: number): number {
|
|
84
|
+
const n = s.length;
|
|
85
|
+
let count = 0;
|
|
86
|
+
let j = n - 1;
|
|
87
|
+
for (let i = 0; i < n; i++) {
|
|
88
|
+
while (j >= i && (s[i]! + s[j]!) / 2 > x) j--;
|
|
89
|
+
if (j < i) break;
|
|
90
|
+
count += j - i + 1;
|
|
91
|
+
}
|
|
92
|
+
return count;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Bisection steps per round before a stuck range (a cluster of equal averages) is stepped past. */
|
|
96
|
+
const WALSH_BISECTION_MAX_STEPS = 128;
|
|
97
|
+
|
|
98
|
+
/** The smallest Walsh average of an ASCENDING array strictly above `x` (Infinity if none): O(n). */
|
|
99
|
+
function walshNextAbove(s: readonly number[], x: number): number {
|
|
100
|
+
const n = s.length;
|
|
101
|
+
let best = Infinity;
|
|
102
|
+
let j = n - 1; // last j with avg ≤ x for the current i
|
|
103
|
+
for (let i = 0; i < n; i++) {
|
|
104
|
+
while (j >= i && (s[i]! + s[j]!) / 2 > x) j--;
|
|
105
|
+
const first = max(i, j + 1);
|
|
106
|
+
if (first < n) best = min(best, (s[i]! + s[first]!) / 2);
|
|
107
|
+
}
|
|
108
|
+
return best;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The largest Walsh average of an ASCENDING array at most `x` (−Infinity if none): O(n). */
|
|
112
|
+
function walshLargestAtMost(s: readonly number[], x: number): number {
|
|
113
|
+
const n = s.length;
|
|
114
|
+
let best = -Infinity;
|
|
115
|
+
let j = n - 1;
|
|
116
|
+
for (let i = 0; i < n; i++) {
|
|
117
|
+
while (j >= i && (s[i]! + s[j]!) / 2 > x) j--;
|
|
118
|
+
if (j < i) break;
|
|
119
|
+
const v = (s[i]! + s[j]!) / 2;
|
|
120
|
+
if (v > best) best = v;
|
|
121
|
+
}
|
|
122
|
+
return best;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The kLow-th and kHigh-th smallest (0-based; kHigh ∈ {kLow, kLow + 1}) Walsh averages of
|
|
127
|
+
* an ASCENDING array, EXACTLY, without materialising the n(n+1)/2 averages — an exact
|
|
128
|
+
* value-space selection (same result as Monahan's algorithm), O(n log n) for the sort plus
|
|
129
|
+
* O(n) per bisection step:
|
|
130
|
+
* - bisect on the VALUE with an O(n) two-pointer count until at most n averages lie in
|
|
131
|
+
* (lo, hi] around both ranks, then enumerate just those and select;
|
|
132
|
+
* - when a bisection point falls between the two ranks, the kLow-th is the largest
|
|
133
|
+
* average ≤ it and the kHigh-th the smallest above it;
|
|
134
|
+
* - a range that will not shrink (a cluster of EQUAL averages) is stepped past one
|
|
135
|
+
* distinct value at a time.
|
|
136
|
+
* Each candidate is the same expression (sᵢ + sⱼ)/2 as a full sort, so the doubles match it.
|
|
137
|
+
*/
|
|
138
|
+
function kthWalshAverages(s: readonly number[], kLow: number, kHigh: number): [number, number] {
|
|
139
|
+
const n = s.length;
|
|
140
|
+
let lo = s[0]!; // the smallest average
|
|
141
|
+
let loCount = walshCountAtMost(s, lo);
|
|
142
|
+
if (loCount > kHigh) return [lo, lo];
|
|
143
|
+
if (loCount > kLow) return [lo, walshNextAbove(s, lo)]; // kHigh = kLow + 1 = loCount
|
|
144
|
+
let hi = s[n - 1]!; // the largest average (its count, m, exceeds every rank)
|
|
145
|
+
let hiCount = walshCountAtMost(s, hi);
|
|
146
|
+
// Invariant: loCount ≤ kLow and hiCount > kHigh.
|
|
147
|
+
for (;;) {
|
|
148
|
+
for (let step = 0; step < WALSH_BISECTION_MAX_STEPS && hiCount - loCount > n; step++) {
|
|
149
|
+
const mid = lo + (hi - lo) / 2;
|
|
150
|
+
if (mid <= lo || mid >= hi) break;
|
|
151
|
+
const c = walshCountAtMost(s, mid);
|
|
152
|
+
if (c > kHigh) { hi = mid; hiCount = c; }
|
|
153
|
+
else if (c <= kLow) { lo = mid; loCount = c; }
|
|
154
|
+
else return [walshLargestAtMost(s, mid), walshNextAbove(s, mid)]; // c = kHigh = kLow + 1
|
|
155
|
+
}
|
|
156
|
+
if (hiCount - loCount <= n) break;
|
|
157
|
+
// Stuck on a cluster of equal averages: everything in (lo, next] equals `next`.
|
|
158
|
+
const next = walshNextAbove(s, lo);
|
|
159
|
+
const c = walshCountAtMost(s, next);
|
|
160
|
+
if (c > kHigh) return [next, next];
|
|
161
|
+
if (c > kLow) return [next, walshNextAbove(s, next)];
|
|
162
|
+
lo = next;
|
|
163
|
+
loCount = c;
|
|
23
164
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return Math.min(1, Math.exp(-0.5 * z * z) * 0.5 * 2); // two-sided via |z|
|
|
165
|
+
// Both ranks lie in (lo, hi]: enumerate those averages (at most n) and select.
|
|
166
|
+
const candidates: number[] = [];
|
|
167
|
+
let jLo = n - 1; // last j with avg ≤ lo (for the current i)
|
|
168
|
+
let jHi = n - 1; // last j with avg ≤ hi
|
|
169
|
+
for (let i = 0; i < n; i++) {
|
|
170
|
+
while (jHi >= i && (s[i]! + s[jHi]!) / 2 > hi) jHi--;
|
|
171
|
+
while (jLo >= i && (s[i]! + s[jLo]!) / 2 > lo) jLo--;
|
|
172
|
+
if (jHi < i) break;
|
|
173
|
+
for (let j = max(i, jLo + 1); j <= jHi; j++) candidates.push((s[i]! + s[j]!) / 2);
|
|
34
174
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
175
|
+
candidates.sort((a, b) => a - b);
|
|
176
|
+
return [candidates[kLow - loCount] ?? hi, candidates[kHigh - loCount] ?? hi];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Median of the Walsh averages (dᵢ + dⱼ)/2, i ≤ j, over ALL the given differences (zeros
|
|
181
|
+
* included), in O(n log n): one sort, then exact selection of the middle one or two
|
|
182
|
+
* ({@link kthWalshAverages}).
|
|
183
|
+
*/
|
|
184
|
+
function hodgesLehmannOf(diffs: readonly number[]): number {
|
|
185
|
+
const n = diffs.length;
|
|
186
|
+
if (n === 0) return 0;
|
|
187
|
+
const s = [...diffs].sort((a, b) => a - b);
|
|
188
|
+
const m = (n * (n + 1)) / 2;
|
|
189
|
+
if (m % 2 === 1) return kthWalshAverages(s, (m - 1) / 2, (m - 1) / 2)[0];
|
|
190
|
+
const [a, b] = kthWalshAverages(s, m / 2 - 1, m / 2);
|
|
191
|
+
return (a + b) / 2;
|
|
41
192
|
}
|
|
42
193
|
|
|
43
194
|
export interface WilcoxonResult {
|
|
195
|
+
/** Nonzero differences — the ones the test ranks (zeros are dropped: Wilcoxon's rule). */
|
|
44
196
|
n: number;
|
|
197
|
+
/** Sum of the (mid-)ranks of the positive differences. */
|
|
45
198
|
wPlus: number;
|
|
199
|
+
/** Two-sided. Exact for n ≤ {@link WILCOXON_EXACT_MAX_N}, normal approximation (continuity + tie corrected) above. */
|
|
46
200
|
pValue: number;
|
|
47
|
-
/**
|
|
201
|
+
/**
|
|
202
|
+
* Paired Hodges–Lehmann estimator: median of the Walsh averages of ALL finite paired
|
|
203
|
+
* differences, zeros included (Hollander & Wolfe). R's `wilcox.test(conf.int = TRUE)` drops
|
|
204
|
+
* zeros for its estimate too; this does not, because dropping them biases the estimate away
|
|
205
|
+
* from 0.
|
|
206
|
+
*/
|
|
48
207
|
hodgesLehmann: number;
|
|
208
|
+
/**
|
|
209
|
+
* Pairs whose difference was not a finite number (NaN or ±Infinity, e.g. a missing or
|
|
210
|
+
* failed measurement). They are DROPPED from the test and the estimate, never ranked;
|
|
211
|
+
* a caller that cannot tolerate missing pairs should check this is 0.
|
|
212
|
+
*/
|
|
213
|
+
nonFinite: number;
|
|
49
214
|
}
|
|
50
215
|
|
|
51
216
|
/**
|
|
52
|
-
* Wilcoxon signed-rank test
|
|
53
|
-
*
|
|
217
|
+
* Wilcoxon signed-rank test of `after − before`, plus the paired Hodges–Lehmann location
|
|
218
|
+
* estimator.
|
|
219
|
+
*
|
|
220
|
+
* - Zeros: Wilcoxon's rule — zero differences are dropped before ranking (`n` counts the
|
|
221
|
+
* rest). They still count toward the Hodges–Lehmann estimate, which would otherwise be
|
|
222
|
+
* biased away from 0.
|
|
223
|
+
* - Ties: tied |differences| share their mid-rank. The exact p-value conditions on those
|
|
224
|
+
* ranks; the normal approximation applies the tie variance correction.
|
|
225
|
+
* - Non-finite differences (NaN, ±Infinity) are dropped and counted in `nonFinite`.
|
|
54
226
|
*/
|
|
55
227
|
export function wilcoxonSignedRank(before: readonly number[], after: readonly number[]): WilcoxonResult {
|
|
56
|
-
const diffs:
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
const d = after[i]! - before[i]!;
|
|
60
|
-
if (d !== 0) diffs.push(d);
|
|
61
|
-
}
|
|
228
|
+
const { diffs: all, nonFinite } = finiteDifferences(before, after);
|
|
229
|
+
const hodgesLehmann = hodgesLehmannOf(all);
|
|
230
|
+
const diffs = all.filter((d) => d !== 0);
|
|
62
231
|
const n = diffs.length;
|
|
63
|
-
if (n === 0) return { n: 0, wPlus: 0, pValue: 1, hodgesLehmann
|
|
232
|
+
if (n === 0) return { n: 0, wPlus: 0, pValue: 1, hodgesLehmann, nonFinite };
|
|
64
233
|
const abs = diffs.map((d) => Math.abs(d));
|
|
65
234
|
const order = abs.map((_, i) => i).sort((p, q) => abs[p]! - abs[q]!);
|
|
66
235
|
const ranks = new Array<number>(n).fill(0);
|
|
236
|
+
const tieGroups: number[] = [];
|
|
67
237
|
let i = 0;
|
|
68
238
|
while (i < n) {
|
|
69
239
|
let j = i;
|
|
70
240
|
while (j + 1 < n && abs[order[j + 1]!] === abs[order[i]!]) j++;
|
|
71
241
|
const avg = (i + j) / 2 + 1;
|
|
72
242
|
for (let k = i; k <= j; k++) ranks[order[k]!] = avg;
|
|
243
|
+
if (j > i) tieGroups.push(j - i + 1);
|
|
73
244
|
i = j + 1;
|
|
74
245
|
}
|
|
75
246
|
let wPlus = 0;
|
|
76
247
|
for (let k = 0; k < n; k++) if (diffs[k]! > 0) wPlus += ranks[k]!;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
walsh.sort((a, b) => a - b);
|
|
83
|
-
const hl =
|
|
84
|
-
walsh.length % 2 === 1
|
|
85
|
-
? walsh[(walsh.length - 1) / 2]!
|
|
86
|
-
: (walsh[walsh.length / 2 - 1]! + walsh[walsh.length / 2]!) / 2;
|
|
87
|
-
return { n, wPlus, pValue: wilcoxonPValue(wPlus, n), hodgesLehmann: hl };
|
|
248
|
+
const pValue =
|
|
249
|
+
n <= WILCOXON_EXACT_MAX_N
|
|
250
|
+
? exactSignedRankPValue(ranks.map((r) => Math.round(2 * r)), Math.round(2 * wPlus))
|
|
251
|
+
: normalSignedRankPValue(n, wPlus, tieGroups);
|
|
252
|
+
return { n, wPlus, pValue, hodgesLehmann, nonFinite };
|
|
88
253
|
}
|
|
89
254
|
|
|
255
|
+
/** The statistic a paired bootstrap resamples. */
|
|
256
|
+
export type PairedStatistic = "mean" | "hodges-lehmann";
|
|
257
|
+
|
|
90
258
|
/**
|
|
91
|
-
* Paired bootstrap confidence interval for
|
|
92
|
-
*
|
|
259
|
+
* Paired bootstrap percentile confidence interval for a location statistic of
|
|
260
|
+
* `after − before` — the mean difference (default) or the paired Hodges–Lehmann estimate
|
|
261
|
+
* (PRD 41 C6.2) — resampling PAIRS so the pairing is preserved. Deterministic: seeded
|
|
262
|
+
* (`seed`, default 1) with a fixed resample count (`iterations`, default 2000). The
|
|
263
|
+
* Hodges–Lehmann statistic costs O(n log n) per resample (exact selection, no n² list).
|
|
264
|
+
* Pairs with a non-finite difference are dropped, as in {@link wilcoxonSignedRank}.
|
|
93
265
|
*/
|
|
94
266
|
export function pairedBootstrapCi(
|
|
95
267
|
before: readonly number[],
|
|
96
268
|
after: readonly number[],
|
|
97
|
-
options: { iterations?: number; confidence?: number; seed?: number } = {},
|
|
269
|
+
options: { iterations?: number; confidence?: number; seed?: number; statistic?: PairedStatistic } = {},
|
|
98
270
|
): { low: number; high: number; estimate: number } {
|
|
99
271
|
const iterations = options.iterations ?? 2000;
|
|
100
272
|
const confidence = options.confidence ?? 0.95;
|
|
273
|
+
const stat = options.statistic === "hodges-lehmann"
|
|
274
|
+
? hodgesLehmannOf
|
|
275
|
+
: (d: readonly number[]) => d.reduce((a, b) => a + b, 0) / Math.max(d.length, 1);
|
|
101
276
|
const rng: Rng = createRng(options.seed ?? 1);
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
for (let i = 0; i < n; i++) diffs.push(after[i]! - before[i]!);
|
|
105
|
-
const estimate = diffs.reduce((a, b) => a + b, 0) / Math.max(diffs.length, 1);
|
|
277
|
+
const { diffs } = finiteDifferences(before, after);
|
|
278
|
+
const n = diffs.length;
|
|
106
279
|
if (n === 0) return { low: 0, high: 0, estimate: 0 };
|
|
107
|
-
const
|
|
280
|
+
const estimate = stat(diffs);
|
|
281
|
+
const values: number[] = [];
|
|
282
|
+
const sample = new Array<number>(n);
|
|
108
283
|
for (let b = 0; b < iterations; b++) {
|
|
109
|
-
let
|
|
110
|
-
|
|
111
|
-
means.push(s / n);
|
|
284
|
+
for (let i = 0; i < n; i++) sample[i] = diffs[floor(rng() * n)]!;
|
|
285
|
+
values.push(stat(sample));
|
|
112
286
|
}
|
|
113
|
-
|
|
287
|
+
values.sort((a, b) => a - b);
|
|
114
288
|
const alpha = 1 - confidence;
|
|
115
289
|
return {
|
|
116
|
-
low:
|
|
117
|
-
high:
|
|
290
|
+
low: values[Math.floor((alpha / 2) * iterations)] ?? estimate,
|
|
291
|
+
high: values[Math.floor((1 - alpha / 2) * iterations) - 1] ?? estimate,
|
|
118
292
|
estimate,
|
|
119
293
|
};
|
|
120
294
|
}
|