dsh-eval 0.3.0 → 0.4.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.
- package/README.md +8 -2
- package/README.zh.md +75 -26
- package/lib/benchmark.js +125 -0
- package/lib/command.js +109 -0
- package/lib/compare.js +194 -0
- package/lib/import.js +318 -0
- package/lib/index.js +2 -1
- package/lib/judge.js +165 -0
- package/lib/metrics.js +196 -0
- package/lib/report.js +126 -0
- package/lib/runner.js +355 -0
- package/lib/stats.js +112 -0
- package/lib/trace.js +136 -0
- package/lib/types/command.d.ts +1 -0
- package/lib/types/compare.d.ts +9 -6
- package/lib/types/index.d.ts +1 -1
- package/lib/types/runner.d.ts +12 -1
- package/lib/types/stats.d.ts +61 -0
- package/lib/types/types.d.ts +2 -0
- package/lib/types.js +8 -0
- package/package.json +29 -22
- package/src/benchmark.ts +2 -0
- package/src/command.ts +20 -5
- package/src/compare.ts +38 -17
- package/src/index.ts +2 -1
- package/src/runner.ts +31 -4
- package/src/stats.ts +147 -0
- package/src/types.ts +2 -0
- package/LICENSE +0 -21
package/src/stats.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paired-comparison statistics for cross-run reports: an exact two-sided sign
|
|
3
|
+
* test over paired outcomes, and a seeded bootstrap confidence interval for a
|
|
4
|
+
* paired mean delta.
|
|
5
|
+
*
|
|
6
|
+
* Both are distribution-free on purpose. A benchmark run yields few paired
|
|
7
|
+
* trials (single digits to low tens), the per-trial deltas are integers with
|
|
8
|
+
* long tails, and the question is "on the same case, did B beat A" — which a
|
|
9
|
+
* paired sign test answers exactly, without a normality or symmetry assumption
|
|
10
|
+
* the data cannot support. Ties carry no sign and drop out of the test, as the
|
|
11
|
+
* sign test requires.
|
|
12
|
+
*
|
|
13
|
+
* The bootstrap resamples the observed deltas with replacement, so its interval
|
|
14
|
+
* needs no shape assumption either, and it is seeded so that re-running
|
|
15
|
+
* `compare` on the same reports prints a byte-identical table.
|
|
16
|
+
*
|
|
17
|
+
* @module dsh-eval/stats
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Outcome of an exact two-sided sign test. */
|
|
21
|
+
export interface SignTestOutcome {
|
|
22
|
+
/** Discordant pairs (`wins + losses`); ties drop out of the test. */
|
|
23
|
+
discordant: number
|
|
24
|
+
/** Exact two-sided p-value; 1 when every pair tied. */
|
|
25
|
+
pValue: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A paired mean delta with its bootstrap interval. */
|
|
29
|
+
export interface PairedDelta {
|
|
30
|
+
/** Mean per-pair delta (`b - a`). */
|
|
31
|
+
mean: number
|
|
32
|
+
/** Lower interval bound, or null when fewer than two pairs are available. */
|
|
33
|
+
lower: number | null
|
|
34
|
+
/** Upper interval bound, or null when fewer than two pairs are available. */
|
|
35
|
+
upper: number | null
|
|
36
|
+
/** Pairs contributing to the mean. */
|
|
37
|
+
pairs: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Knobs for {@link pairedDelta}; defaults keep reports reproducible. */
|
|
41
|
+
export interface BootstrapOptions {
|
|
42
|
+
/** Interval confidence, default `0.95`. */
|
|
43
|
+
confidence?: number
|
|
44
|
+
/** Resample count, default `2000`. */
|
|
45
|
+
resamples?: number
|
|
46
|
+
/** PRNG seed, default `0`. */
|
|
47
|
+
seed?: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Log of the binomial coefficient, accumulated rather than factorized so large
|
|
52
|
+
* pair counts stay finite.
|
|
53
|
+
* @param n - trials.
|
|
54
|
+
* @param k - successes, at most n.
|
|
55
|
+
* @returns `ln C(n, k)`.
|
|
56
|
+
*/
|
|
57
|
+
function logChoose(n: number, k: number): number {
|
|
58
|
+
let value = 0
|
|
59
|
+
for (let index = 0; index < k; index++) {
|
|
60
|
+
value += Math.log(n - index) - Math.log(index + 1)
|
|
61
|
+
}
|
|
62
|
+
return value
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Exact two-sided sign test. Under the null hypothesis a discordant pair is a
|
|
67
|
+
* win or a loss with equal probability, so the win count is Binomial(n, 1/2)
|
|
68
|
+
* and the p-value doubles the smaller tail.
|
|
69
|
+
* @param wins - pairs where B beat A.
|
|
70
|
+
* @param losses - pairs where A beat B.
|
|
71
|
+
* @returns the discordant count and the exact p-value in `[0, 1]`.
|
|
72
|
+
*/
|
|
73
|
+
export function signTest(wins: number, losses: number): SignTestOutcome {
|
|
74
|
+
const discordant = wins + losses
|
|
75
|
+
if (discordant === 0) return { discordant, pValue: 1 }
|
|
76
|
+
const tail = Math.min(wins, losses)
|
|
77
|
+
const halfPow = discordant * Math.log(2)
|
|
78
|
+
let cumulative = 0
|
|
79
|
+
for (let index = 0; index <= tail; index++) {
|
|
80
|
+
cumulative += Math.exp(logChoose(discordant, index) - halfPow)
|
|
81
|
+
}
|
|
82
|
+
return { discordant, pValue: Math.min(1, 2 * cumulative) }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Small deterministic PRNG (mulberry32): identical seeds must produce identical
|
|
87
|
+
* intervals, otherwise `compare` output would drift between runs.
|
|
88
|
+
* @param seed - any integer.
|
|
89
|
+
* @returns a generator of floats in `[0, 1)`.
|
|
90
|
+
*/
|
|
91
|
+
function prng(seed: number): () => number {
|
|
92
|
+
let state = seed >>> 0
|
|
93
|
+
return () => {
|
|
94
|
+
state = (state + 0x6d2b79f5) >>> 0
|
|
95
|
+
let value = state
|
|
96
|
+
value = Math.imul(value ^ (value >>> 15), value | 1)
|
|
97
|
+
value ^= value + Math.imul(value ^ (value >>> 7), value | 61)
|
|
98
|
+
return ((value ^ (value >>> 14)) >>> 0) / 4294967296
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Percentile of a sorted sample, linearly interpolated between neighbours.
|
|
104
|
+
* @param sorted - ascending values, non-empty.
|
|
105
|
+
* @param q - quantile in `[0, 1]`.
|
|
106
|
+
* @returns the interpolated value.
|
|
107
|
+
*/
|
|
108
|
+
function quantile(sorted: readonly number[], q: number): number {
|
|
109
|
+
const position = (sorted.length - 1) * q
|
|
110
|
+
const low = Math.floor(position)
|
|
111
|
+
const weight = position - low
|
|
112
|
+
return sorted[low]! * (1 - weight) + sorted[Math.ceil(position)]! * weight
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Mean paired delta with a percentile bootstrap interval.
|
|
117
|
+
* @param deltas - per-pair differences (`b - a`).
|
|
118
|
+
* @param options - confidence, resample count, and PRNG seed.
|
|
119
|
+
* @returns the mean and interval, or null when `deltas` is empty.
|
|
120
|
+
*/
|
|
121
|
+
export function pairedDelta(
|
|
122
|
+
deltas: readonly number[],
|
|
123
|
+
options: BootstrapOptions = {},
|
|
124
|
+
): PairedDelta | null {
|
|
125
|
+
if (deltas.length === 0) return null
|
|
126
|
+
const mean = deltas.reduce((total, value) => total + value, 0) / deltas.length
|
|
127
|
+
if (deltas.length === 1) return { mean, lower: null, upper: null, pairs: 1 }
|
|
128
|
+
const confidence = options.confidence ?? 0.95
|
|
129
|
+
const resamples = options.resamples ?? 2000
|
|
130
|
+
const random = prng(options.seed ?? 0)
|
|
131
|
+
const means: number[] = []
|
|
132
|
+
for (let resample = 0; resample < resamples; resample++) {
|
|
133
|
+
let total = 0
|
|
134
|
+
for (let draw = 0; draw < deltas.length; draw++) {
|
|
135
|
+
total += deltas[Math.floor(random() * deltas.length)]!
|
|
136
|
+
}
|
|
137
|
+
means.push(total / deltas.length)
|
|
138
|
+
}
|
|
139
|
+
means.sort((a, b) => a - b)
|
|
140
|
+
const alpha = (1 - confidence) / 2
|
|
141
|
+
return {
|
|
142
|
+
mean,
|
|
143
|
+
lower: quantile(means, alpha),
|
|
144
|
+
upper: quantile(means, 1 - alpha),
|
|
145
|
+
pairs: deltas.length,
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -74,6 +74,8 @@ export interface Benchmark {
|
|
|
74
74
|
command: readonly string[]
|
|
75
75
|
/** Trials per case. */
|
|
76
76
|
trials: number
|
|
77
|
+
/** Case x trial executions allowed in flight at once; defaults to 1 (serial). */
|
|
78
|
+
concurrency: number
|
|
77
79
|
/** Per-trial timeout in milliseconds. */
|
|
78
80
|
timeoutMs: number
|
|
79
81
|
/** Reserved deterministic seed for future paired comparisons. */
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 hccccc01333
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|