budgetbench / stats /trend.mjs
n3r's picture
BudgetBench v1.0.1
46f490b verified
Raw History Blame Contribute Delete
2.97 kB
#!/usr/bin/env node
// BudgetBench TREND TEST across versions of one system on the SAME frozen split: is a label's rate moving with the
// version, or is a round-to-round change noise? Cochran-Armitage test for trend (versions scored 0, 1, 2, ... in the
// order given), two-sided, normal approximation; plus the chi-square test of homogeneity. Never quote a
// round-to-round difference without it.
// node budgetbench/stats/trend.mjs --label wrong_shown <run v1> <run v2> <run v3> ...
// (--label may be given twice to test the union, e.g. --label wrong_shown --label wrong_offered)
import {loadRun} from "./report.mjs";
const argv = process.argv.slice(2);
const labels = [];
const dirs = [];
for (let i = 0; i < argv.length; i++) { if (argv[i] === "--label") labels.push(argv[++i]); else if (argv[i] !== "--regrade") dirs.push(argv[i]); }
if (dirs.length < 2 || !labels.length) { console.error("usage: trend.mjs --label <label> [--label <label>] <run dir> <run dir> [...]"); process.exit(2); }
const regrade = argv.includes("--regrade");
// standard normal CDF (Abramowitz-Stegun 7.1.26 on erf)
function phi(z) { const t = 1 / (1 + 0.3275911 * Math.abs(z) / Math.SQRT2); const y = 1 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * Math.exp(-z * z / 2); return z >= 0 ? (1 + y) / 2 : (1 - y) / 2; }
// chi-square survival, k degrees of freedom (Wilson-Hilferty)
function chiSurv(x, k) { if (x <= 0) return 1; const z = (Math.cbrt(x / k) - (1 - 2 / (9 * k))) / Math.sqrt(2 / (9 * k)); return 1 - phi(z); }
const runs = [];
for (const d of dirs) runs.push(await loadRun(d, {regrade}));
const shas = new Set(runs.map((r) => r.inputs.cases_sha256));
if (shas.size > 1) console.log("WARNING: the runs are not on the same frozen cases (cases_sha256 differs); a trend across different sets means nothing.");
const ns = runs.map((r) => r.rows.length), xs = runs.map((r) => r.rows.filter((x) => labels.includes(x.label)).length);
const N = ns.reduce((a, b) => a + b, 0), X = xs.reduce((a, b) => a + b, 0), p = X / N;
const t = runs.map((_, i) => i);
const T = xs.reduce((a, x, i) => a + t[i] * (x - ns[i] * p), 0);
const tbar = ns.reduce((a, n, i) => a + n * t[i], 0) / N;
const varT = p * (1 - p) * ns.reduce((a, n, i) => a + n * (t[i] - tbar) ** 2, 0);
const z = varT > 0 ? T / Math.sqrt(varT) : 0;
const pTrend = 2 * (1 - phi(Math.abs(z)));
const chi = ns.reduce((a, n, i) => a + (p > 0 && p < 1 ? (xs[i] - n * p) ** 2 / (n * p * (1 - p)) : 0), 0);
console.log(`label ${labels.join(" + ")}`);
runs.forEach((r, i) => console.log(` ${r.inputs.run_id} ${r.inputs.system.version ?? ""} ${r.inputs.system.variant ?? ""}: ${xs[i]} of ${ns[i]} (${(100 * xs[i] / ns[i]).toFixed(1)}%)`));
console.log(`Cochran-Armitage trend: z = ${z.toFixed(2)}, two-sided p = ${pTrend.toFixed(4)}`);
console.log(`chi-square homogeneity: ${chi.toFixed(2)} on ${runs.length - 1} df, p = ${chiSurv(chi, runs.length - 1).toFixed(4)}`);