Spaces:
Running on Zero
Running on Zero
Download scripts/significance.py from leeshal/EvidenceGuard-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/spaces/leeshal/EvidenceGuard-AI/resolve/main/scripts/significance.py
- Command line
-
hf download hf://spaces/leeshal/EvidenceGuard-AI/scripts/significance.py
-
curl -L -o significance.py https://huggingface.co/spaces/leeshal/EvidenceGuard-AI/resolve/main/scripts/significance.py
2.26 kB
| """Paired significance tests between configurations (exact McNemar + bootstrap 95% CI). | |
| Reads results/per_question_results.csv and writes results/significance.json. | |
| """ | |
| import csv | |
| import json | |
| import random | |
| from math import comb | |
| from pathlib import Path | |
| RES = Path(__file__).resolve().parents[1] / "results" | |
| def mcnemar_exact(b, c): | |
| n = b + c | |
| if n == 0: | |
| return 1.0 | |
| k = min(b, c) | |
| p = sum(comb(n, i) for i in range(k + 1)) / 2 ** n * 2 | |
| return min(1.0, p) | |
| def main(): | |
| rows = list(csv.DictReader(open(RES / "per_question_results.csv"))) | |
| by = {} | |
| for r in rows: | |
| by.setdefault(r["config"], {})[r["id"]] = r | |
| out = {} | |
| for metric, field, better_low in (("behaviour_accuracy", "behaviour_ok", False), | |
| ("unsupported_answer", "unsupported", True)): | |
| for split in ("all", "test"): | |
| for other in ("rerank", "verify", "full"): | |
| ids = [i for i in by["baseline"] if split == "all" or by["baseline"][i]["split"] == "test"] | |
| a = [by["baseline"][i][field] == "True" for i in ids] | |
| b = [by[other][i][field] == "True" for i in ids] | |
| n01 = sum((not x) and y for x, y in zip(a, b)) # baseline 0, other 1 | |
| n10 = sum(x and (not y) for x, y in zip(a, b)) | |
| rng = random.Random(0) | |
| diffs = [] | |
| for _ in range(5000): | |
| s = [rng.randrange(len(ids)) for _ in ids] | |
| diffs.append(sum(b[j] for j in s) / len(s) - sum(a[j] for j in s) / len(s)) | |
| diffs.sort() | |
| out[f"{metric}|{split}|baseline_vs_{other}"] = { | |
| "n": len(ids), "baseline": round(sum(a) / len(a), 4), other: round(sum(b) / len(b), 4), | |
| "difference": round(sum(b) / len(b) - sum(a) / len(a), 4), | |
| "ci95": [round(diffs[125], 4), round(diffs[4874], 4)], | |
| "only_other_true": n01, "only_baseline_true": n10, | |
| "mcnemar_exact_p": round(mcnemar_exact(n01, n10), 6), | |
| } | |
| (RES / "significance.json").write_text(json.dumps(out, indent=2)) | |
| for k, v in out.items(): | |
| print(k, v) | |
| if __name__ == "__main__": | |
| main() | |