attention-closes-repro / aggregate.py
Frame17's picture
Aggregation: r50 / post-crossover recall with Wilson CIs, per-tier breakdown
be9f654 verified
Raw History Blame Contribute Delete
3.72 kB
"""Aggregate per-turn records from runner.py into Table-1-style cells.
Reports per cell (task x condition):
- r50: recall rate over all fact labels at the turn-50 grouped probe
- r_postcrossover: recall over single-fact probes at turns >= threshold
(>=35 for task1, >=25 for task2, per paper Table 1)
- per-tier recall for task2
- observed crossover turn tau_cross (first turn with Rmin-Gmax >= W under SW)
Each rate gets a Wilson 95% CI (paper Appendix F.3 uses Wilson for
per-cell turn-and-fact recall rates).
"""
import argparse
import json
import math
import sys
from collections import defaultdict
def wilson(k, n, z=1.959964):
if n == 0:
return (float("nan"), float("nan"))
p = k / n
d = 1 + z * z / n
c = p + z * z / (2 * n)
h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n))
return ((c - h) / d, (c + h) / d)
def collect(paths, max_turns=50):
recs = []
for path in paths:
with open(path) as f:
for line in f:
recs.append(json.loads(line))
return recs
def fact_tier(fact_id, cfg):
for tier, ids in cfg.get("tiers", {}).items():
if fact_id in ids:
return int(tier)
return None
def rate(labels):
n = len(labels)
k = sum(labels)
lo, hi = wilson(k, n)
return {"n": n, "k": k, "rate": k / n if n else None, "wilson95": [lo, hi]}
def main():
ap = argparse.ArgumentParser()
ap.add_argument("records", nargs="+", help="runner output JSONL file(s)")
ap.add_argument("--max-turns", type=int, default=50)
args = ap.parse_args()
recs = collect(args.records, args.max_turns)
with open("tasks.json") as f:
cfg = json.load(f)
cells = defaultdict(lambda: {"r50_labels": [], "post_labels": [], "tiers": defaultdict(list),
"tau_cross": [], "probe_traj": defaultdict(list)})
for r in recs:
key = (r["task"], r["condition"])
c = cells[key]
if r["turn"] == args.max_turns and r["recall"]:
for v in r["recall"].values():
c["r50_labels"].append(v)
if r["kind"] == "probe" and r["recall"] and r["turn"] != args.max_turns:
thr = 35 if r["task"] == "task1" else 25
if r["turn"] >= thr:
for fid, v in r["recall"].items():
c["post_labels"].append(v)
tier = fact_tier(fid, cfg[r["task"]])
if tier is not None:
c["tiers"][str(tier)].append(v)
c["probe_traj"][r["turn"]].append(
sum(r["recall"].values()) / len(r["recall"]))
if r["closed_sw"]:
c["tau_cross"].append(r["turn"])
print(json.dumps({
"meta": {"note": "Wilson 95% CIs on fact-level labels at the grouped probe / post-crossover probes"},
"cells": {
f"{t}|{cond}": {
"r50": rate(c["r50_labels"]),
"r_postcrossover": rate(c["post_labels"]),
"tier_recall": {tier: rate(v) for tier, v in sorted(c["tiers"].items())},
"tau_cross_observed": {
"n_episodes": len(c["tau_cross"]),
"min": min(c["tau_cross"]) if c["tau_cross"] else None,
"median": sorted(c["tau_cross"])[len(c["tau_cross"]) // 2] if c["tau_cross"] else None,
"max": max(c["tau_cross"]) if c["tau_cross"] else None,
},
"probe_trajectory": {str(t): sum(v) / len(v) for t, v in sorted(c["probe_traj"].items())},
}
for (t, cond), c in sorted(cells.items())
},
}, indent=2))
if __name__ == "__main__":
sys.exit(main())