"""Per-question analysis of the four completed 256-question cells (parent, forward MLP transplant, reverse, donor). uv run --no-project --with zstandard python code/four_cell_question_analysis.py A. Consistency: does the MLP update move the same questions on both backgrounds? Correlation between the forward saving (parent - forward), the reverse cost (reverse - donor) and the donor's own saving (parent - donor). B. Caps: how much of each saving comes from questions whose parent trace hit the 32,768-token ceiling. C. Scaling: saving by quartile of the parent's own length (Table 4). D. Sources: saving by source stratum. Tokens are sampled tokens (native + forced final answer). Intervals are paired question-bootstrap percentiles, resampled within source (10,000 draws, seed 20261130). Writes outputs/four_cell_question_analysis.json, which equals stats/audit/four_cell_question_analysis.json. """ import json, os, random, statistics, sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from common import RECORDS, ROOT, iter_jsonl # noqa: E402 N = str(RECORDS) ARMS = { "parent": ("confirm_baseline", "base"), "forward": ("confirm_full_mlp_forward", "base_mlp_all"), "reverse": ("confirm_full_mlp_reverse", "concise_restore_mlp_all"), "donor": ("confirm_concise", "concise"), } def load(arm): d, stem = ARMS[arm] rows = {} if True: for r in iter_jsonl(os.path.join(N, d, f"{stem}.jsonl")): rows[r["id"]] = { "area": r.get("area") or r.get("set"), "tok": r["generated_tokens"], "think": len(r.get("thinking_ids") or []), "capped": r.get("native_finish_reason") == "length", "natural": bool(r.get("natural_completion")), } with open(os.path.join(N, d, "scored", f"{stem}.scored.jsonl")) as f: for line in f: s = json.loads(line) if s["id"] in rows: rows[s["id"]]["final_ok"] = bool(s.get("final_correct", s.get("correct_final", s.get("correct")))) return rows data = {a: load(a) for a in ARMS} ids = sorted(set.intersection(*(set(v) for v in data.values()))) assert len(ids) == 256, len(ids) area = {i: data["parent"][i]["area"] for i in ids} T = {a: {i: data[a][i]["tok"] for i in ids} for a in ARMS} fwd = {i: T["parent"][i] - T["forward"][i] for i in ids} rev = {i: T["reverse"][i] - T["donor"][i] for i in ids} don = {i: T["parent"][i] - T["donor"][i] for i in ids} def rank(v): o = sorted(range(len(v)), key=lambda k: v[k]); r = [0.0] * len(v); k = 0 while k < len(o): m = k while m + 1 < len(o) and v[o[m + 1]] == v[o[k]]: m += 1 for q in range(k, m + 1): r[o[q]] = (k + m) / 2 k = m + 1 return r def pearson(x, y): mx, my = statistics.fmean(x), statistics.fmean(y) sx = sum((a - mx) ** 2 for a in x) ** 0.5; sy = sum((b - my) ** 2 for b in y) ** 0.5 return sum((a - mx) * (b - my) for a, b in zip(x, y)) / (sx * sy) def spearman(x, y): return pearson(rank(x), rank(y)) strata = {} for i in ids: strata.setdefault(area[i], []).append(i) rng = random.Random(20261130) draws = [[rng.choice(v) for s, v in sorted(strata.items()) for _ in v] for _ in range(10000)] def ci(stat): vals = sorted(stat(d) for d in draws) return stat(ids), vals[249], vals[9749] out = {} def corr(a, b): return ci(lambda s: spearman([a[i] for i in s], [b[i] for i in s])) out["A_spearman_forward_vs_reverse"] = corr(fwd, rev) out["A_spearman_forward_vs_donor_saving"] = corr(fwd, don) out["A_spearman_reverse_vs_donor_saving"] = corr(rev, don) out["A_sign_agreement_forward_reverse"] = sum((fwd[i] > 0) == (rev[i] > 0) for i in ids) / len(ids) capped_parent = [i for i in ids if data["parent"][i]["capped"]] out["B_parent_capped_questions"] = len(capped_parent) for name, eff in (("forward", fwd), ("reverse", rev), ("donor", don)): tot = sum(eff[i] for i in ids) out[f"B_{name}_share_of_saving_from_parent_capped"] = ci(lambda s, eff=eff: sum(eff[i] for i in s if data["parent"][i]["capped"]) / max(1, sum(eff[i] for i in s))) both_nat = [i for i in ids if data["parent"][i]["natural"] and data[{"forward": "forward", "reverse": "reverse", "donor": "donor"}[name]][i]["natural"]] out[f"B_{name}_mean_effect_both_finished_naturally"] = (statistics.fmean(eff[i] for i in both_nat), len(both_nat)) out["B_capped_counts"] = {a: sum(data[a][i]["capped"] for i in ids) for a in ARMS} order = sorted(ids, key=lambda i: T["parent"][i]) quart = [order[k * 64:(k + 1) * 64] for k in range(4)] out["C_by_parent_length_quartile"] = [ {"parent_mean": round(statistics.fmean(T["parent"][i] for i in q)), "forward_saving_pct": round(100 * sum(fwd[i] for i in q) / sum(T["parent"][i] for i in q), 1), "donor_saving_pct": round(100 * sum(don[i] for i in q) / sum(T["parent"][i] for i in q), 1), "forward_share_of_donor": round(100 * sum(fwd[i] for i in q) / max(1, sum(don[i] for i in q)), 1), "reverse_share_of_donor": round(100 * sum(rev[i] for i in q) / max(1, sum(don[i] for i in q)), 1)} for q in quart] out["D_by_source"] = {s: {"n": len(v), "forward_share_of_donor": round(100 * sum(fwd[i] for i in v) / max(1, sum(don[i] for i in v)), 1), "reverse_share_of_donor": round(100 * sum(rev[i] for i in v) / max(1, sum(don[i] for i in v)), 1), "donor_saving_pct": round(100 * sum(don[i] for i in v) / sum(T["parent"][i] for i in v), 1)} for s, v in sorted(strata.items())} (ROOT / "outputs").mkdir(exist_ok=True) json.dump(out, open(ROOT / "outputs/four_cell_question_analysis.json", "w"), indent=1, default=str) for k, v in out.items(): print(k, ":", v)