proof-or-claim / scripts /reproduce.py
issaquahd's picture
Initial release (part 2)
611d454 verified
Raw History Blame Contribute Delete
8.63 kB
#!/usr/bin/env python3
"""Re-score every shipped baseline with scripts/score.py and compare with the numbers already reported.
Usage: reproduce.py [--write-results]
Point estimates must match the reported values exactly (tolerance 1e-9). The 95% intervals are
recomputed with this repo's per-metric seeded bootstrap, not the original single shared generator,
so they can differ slightly from the reported intervals; the largest difference is printed.
--write-results regenerates baselines/RESULTS.md from the same scores. Exit 1 on any point mismatch.
"""
import argparse
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from pocb import ROOT, load_jsonl, parse_answer, parse_verdict # noqa: E402
from score import fmt, score_pairwise, score_pointwise # noqa: E402
SYSTEMS = ["gemma4-26b", "qwen3.8-27b", "qwen3-30b-a3b", "clinic-unproven-e0a5bce", "clinic-any-e0a5bce",
"clinic-unproven-2f6fbc0", "clinic-any-2f6fbc0"]
LABELS = {
"gemma4-26b": "gemma4:26b (Q4_K_M, Ollama, think off)",
"qwen3.8-27b": "qwen3.8:27b (Q4_K_M, Ollama, think off)",
"qwen3-30b-a3b": "qwen3:30b-a3b-instruct-2507 (Q4_K_M, Ollama)",
"clinic-unproven-e0a5bce": "Agent Trace Clinic `unproven_completion`, e0a5bce",
"clinic-any-e0a5bce": "Agent Trace Clinic, any high/medium finding, e0a5bce",
"clinic-unproven-2f6fbc0": "Agent Trace Clinic `unproven_completion`, 2f6fbc0 (in-sample fix)",
"clinic-any-2f6fbc0": "Agent Trace Clinic, any high/medium finding, 2f6fbc0 (in-sample fix)",
}
KEYS = ("accuracy", "balanced_accuracy", "false_accept_unproven", "false_reject_proven", "false_accept_impossible",
"length_bias_delta_far", "minimal_edit_pair_both_correct")
PAIR_KEYS = {"acc": "accuracy", "flip": "flip_rate", "both": "both_orders_correct", "first_pos": "chose_position_a"}
TOL = 1e-9
def verdicts_of(name):
return {r["id"]: parse_verdict(r) for r in load_jsonl(ROOT / f"baselines/predictions/{name}.jsonl")}
def pair_answers(name, kind, pairs, verdicts):
f = ROOT / f"baselines/pairwise/{name}__{kind}.jsonl"
if f.exists():
return {(r["pair_id"], r["order"]): parse_answer(r) for r in load_jsonl(f)}
# A rule-based detector has no pairwise mode: it picks the trace it does not flag; a tie is wrong.
out = {}
for p in pairs:
P, U = verdicts.get(p["proven_id"]), verdicts.get(p["unproven_id"])
pick = "P" if (P, U) == ("PROVEN", "UNPROVEN") else ("U" if (P, U) == ("UNPROVEN", "PROVEN") else None)
out[(p["id"], "PU")] = {"P": "A", "U": "B"}.get(pick)
out[(p["id"], "UP")] = {"P": "B", "U": "A"}.get(pick)
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--write-results", action="store_true")
a = ap.parse_args()
reported = json.loads((ROOT / "baselines/reported_numbers.json").read_text())
items = load_jsonl(ROOT / "data/items.jsonl")
hard = load_jsonl(ROOT / "splits/hard.jsonl")
pairs = load_jsonl(ROOT / "data/pairs.jsonl")
pairs_hard = load_jsonl(ROOT / "data/pairs_hard.jsonl")
checked, mismatches, max_ci = 0, [], 0.0
full, hard_all, hard_test, pw, pwh = {}, {}, {}, {}, {}
for s in SYSTEMS:
v = verdicts_of(s)
full[s] = m = score_pointwise(items, v, 2000, 7)
hard_all[s] = score_pointwise(hard, v, 2000, 7)
hard_test[s] = score_pointwise([r for r in hard if r["fold"] == "test"], v, 2000, 7)
ref = reported["pointwise"][s]
pairs_to_check = [(k, m[k], ref[k]) for k in KEYS if k in ref and k in m]
pairs_to_check += [(f"group:{g}", m["by_group_accuracy"][g], ref["by_group_accuracy"][g])
for g in ref["by_group_accuracy"]]
pairs_to_check += [(f"group_n:{g}", [m["by_group_n"][g]], [n]) for g, n in ref["by_group_n"].items()]
for blk, plist, store in (("pairwise", pairs, pw), ("pairwise_hard", pairs_hard, pwh)):
if s in reported[blk]:
kind = "pairs" if blk == "pairwise" else "hardpairs"
store[s] = pm = score_pairwise(plist, pair_answers(s, kind, plist, v), 2000, 7)
pairs_to_check += [(f"{blk}:{rk}", pm[ok], reported[blk][s][rk]) for rk, ok in PAIR_KEYS.items()]
pairs_to_check.append((f"{blk}:n_pairs", [pm["n_pairs"]], [reported[blk][s]["n_pairs"]]))
for name, mine, theirs in pairs_to_check:
checked += 1
if mine[0] is None or theirs[0] is None or abs(mine[0] - theirs[0]) > TOL:
mismatches.append((s, name, mine[0], theirs[0]))
for i in (1, 2):
if len(mine) > i and len(theirs) > i and mine[i] is not None and theirs[i] is not None:
max_ci = max(max_ci, abs(mine[i] - theirs[i]))
print(f"compared {checked} reported point estimates across {len(SYSTEMS)} systems: "
f"{checked - len(mismatches)} exact, {len(mismatches)} mismatched")
for mm in mismatches:
print(" MISMATCH", mm)
print(f"largest 95% interval bound difference vs reported (bootstrap generator differs): {max_ci:.3f}")
if a.write_results:
write_results(full, hard_all, hard_test, pw, pwh)
print("wrote baselines/RESULTS.md")
sys.exit(1 if mismatches else 0)
def write_results(full, hard_all, hard_test, pw, pwh):
L = ["# Baseline results", "",
"Generated by `python3 scripts/reproduce.py --write-results` from the files in `baselines/`. "
"Judges: temperature 0, seed 42, the exact prompts in `scripts/pocb.py`, run locally through Ollama "
"0.33.3 on one Apple Silicon desktop (Q4_K_M weights). Point estimate [95% cluster-bootstrap interval, "
"2000 resamples, seed 7].", "",
"The two `2f6fbc0` rows are a rule-based detector whose fix was designed on these item shapes "
"(in-sample). They show that the hard split can be solved by binding a check to its target; they are "
"not a held-out result.", "",
"## Hard split (`splits/hard.jsonl`, 40 items: 20 wrong_target + 20 verify_after twins)", "",
"Accuracy equals balanced accuracy here (20 PROVEN, 20 UNPROVEN). Answering UNPROVEN to everything scores 0.500.", "",
"| System | Accuracy, all 40 | Accuracy, test fold (30) | False accept on wrong_target | Twin pair both correct | wrong_target accepted, by domain |",
"| --- | --- | --- | --- | --- | --- |"]
for s in SYSTEMS:
h, t = hard_all[s], hard_test[s]
dom = ", ".join(f"{d} {x}" for d, x in h["wrong_target_accept_by_domain"].items())
L.append(f"| {LABELS[s]} | {fmt(h['accuracy'])} | {fmt(t['accuracy'])} | {fmt(h['false_accept_unproven'])} | "
f"{fmt(h['wrong_target_twin_both_correct'])} | {dom} |")
L += ["", "## Full set (`data/items.jsonl`, 317 items: 99 PROVEN, 218 UNPROVEN, 67 clusters)", "",
"| System | Accuracy | Balanced acc. | False accept on UNPROVEN | False reject on PROVEN | False accept on impossible | Minimal-edit pair both correct | wrong_target accuracy |",
"| --- | --- | --- | --- | --- | --- | --- | --- |"]
for s in SYSTEMS:
m = full[s]
L.append(f"| {LABELS[s]} | {fmt(m['accuracy'])} | {fmt(m['balanced_accuracy'])} | {fmt(m['false_accept_unproven'])} | "
f"{fmt(m['false_reject_proven'])} | {fmt(m['false_accept_impossible'])} | "
f"{fmt(m['minimal_edit_pair_both_correct'])} | {fmt(m['by_group_accuracy']['sandbox/wrong_target'])} |")
L += ["", "## Pairwise, order-swapped (seed 42)", "",
"| System | Planned pairs (52): accuracy, flip rate | Hard pairs (40, post hoc): accuracy, flip rate |",
"| --- | --- | --- |"]
for s in SYSTEMS:
if s in pw:
L.append(f"| {LABELS[s]} | {pw[s]['accuracy'][0]:.3f}, {pw[s]['flip_rate'][0]:.3f} | "
f"{pwh[s]['accuracy'][0]:.3f}, {pwh[s]['flip_rate'][0]:.3f} |")
L += ["", "A rule-based detector has no pairwise mode: it picks the trace it does not flag, and a tie is wrong.", "",
"## Accuracy by item group (full set)", ""]
groups = list(full[SYSTEMS[0]]["by_group_n"])
L.append("| Group | n | " + " | ".join(SYSTEMS) + " |")
L.append("| --- | --- | " + " | ".join("---" for _ in SYSTEMS) + " |")
for g in groups:
L.append(f"| {g} | {full[SYSTEMS[0]]['by_group_n'][g]} | " +
" | ".join(f"{full[s]['by_group_accuracy'][g][0]:.2f}" for s in SYSTEMS) + " |")
(ROOT / "baselines/RESULTS.md").write_text("\n".join(L) + "\n")
if __name__ == "__main__":
main()