"""Turn the raw per-example results into the claim-by-claim comparison tables. Every arm is evaluated on the SAME examples, so score differences are tested with McNemar's exact paired test on the discordant pairs rather than with two independent proportions -- at our sample sizes the paired test is the only one with any power, and using the unpaired s.e. would let us "fail to reject" everything and call that a result. """ import json, glob, os, math from itertools import zip_longest OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "outputs") def load(name): p = os.path.join(OUT, name) return json.load(open(p)) if os.path.exists(p) else None def load_merged(*names): """Concatenate per-example results from runs over disjoint problem slices. Claim 4 was bought in two halves (problems 0..31, then 32..63) so that extending n=32 -> n=64 cost one increment rather than a full re-run. The halves are disjoint by construction (verified on task_id), so per-example vectors concatenate and the score is recomputed over the union. """ parts = [load(n) for n in names] parts = [p for p in parts if p] if not parts: return None if len(parts) == 1: return parts[0] m = dict(parts[0]) for key in ("per_example_score", "per_example_steps", "responses", "budgets", "ic_sizes", "n_stable"): m[key] = [v for p in parts for v in p.get(key, [])] m["n_examples"] = len(m["per_example_score"]) m["score"] = 100.0 * sum(m["per_example_score"]) / m["n_examples"] m["mean_steps"] = sum(m["per_example_steps"]) / len(m["per_example_steps"]) m["speedup_vs_uniform"] = m["config"]["steps"] / m["mean_steps"] m["fallback_steps"] = sum(p.get("fallback_steps", 0) for p in parts) m["merged_from"] = list(names) return m def wilson(k, n, z=1.96): """Wilson score interval -- behaves sanely at small n and near 0/1.""" if n == 0: return (0.0, 0.0) p = k / n d = 1 + z * z / n c = (p + z * z / (2 * n)) / d h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d return (100 * max(0, c - h), 100 * min(1, c + h)) def mcnemar_exact(a, b): """Exact two-sided McNemar on paired 0/1 vectors. Returns (b01, b10, p).""" b01 = sum(1 for x, y in zip(a, b) if x == 0 and y == 1) # base wrong, new right b10 = sum(1 for x, y in zip(a, b) if x == 1 and y == 0) # base right, new wrong n = b01 + b10 if n == 0: return b01, b10, 1.0 k = min(b01, b10) # two-sided exact binomial test at p=0.5 tail = sum(math.comb(n, i) for i in range(0, k + 1)) / (2 ** n) return b01, b10, min(1.0, 2 * tail) def compare(task, base_file, arms, paper): base = load(base_file) if not base: print(f" [{task}] baseline missing ({base_file})") return [] bs = base["per_example_score"] n = len(bs) lo, hi = wilson(sum(bs), n) rows = [] print(f"\n{'='*88}\n{task} (n={n})\n{'='*88}") print(f"{'arm':<22} {'paper':>7} {'repro':>7} {'95% CI':>16} {'steps':>8} " f"{'speedup':>8} {'delta':>7} {'McNemar p':>10}") print(f"{'baseline':<22} {paper['baseline']:>7.2f} {base['score']:>7.2f} " f"{f'[{lo:.1f},{hi:.1f}]':>16} {base['mean_steps']:>8.1f} " f"{base['speedup_vs_uniform']:>7.2f}x {'-':>7} {'-':>10}") rows.append(dict(task=task, arm="baseline", paper=paper["baseline"], repro=base["score"], ci=[lo, hi], steps=base["mean_steps"], speedup=base["speedup_vs_uniform"], n=n)) for label, fname in arms: r = load(fname) if not r: print(f"{label:<22} {'-':>7} {'MISSING':>7}") continue rs = r["per_example_score"] # Arms may cover different numbers of examples (only some were extended # to n=64). Always compare on the COMMON prefix: a delta between a # 64-example baseline and a 32-example arm is a different problem set, # not an effect. m = min(len(bs), len(rs)) b01, b10, p = mcnemar_exact(bs[:m], rs[:m]) base_m = 100.0 * sum(bs[:m]) / m arm_m = 100.0 * sum(rs[:m]) / m lo2, hi2 = wilson(sum(rs[:m]), m) pv = paper.get(label, float("nan")) note = "" if m == len(bs) else f" [vs baseline on the same n={m}: {base_m:.2f}]" print(f"{label:<22} {pv:>7.2f} {arm_m:>7.2f} " f"{f'[{lo2:.1f},{hi2:.1f}]':>16} {r['mean_steps']:>8.1f} " f"{r['speedup_vs_uniform']:>7.2f}x {arm_m-base_m:>+7.2f} " f"{p:>10.3f} (win {b01} / lose {b10}, n={m}){note}") rows.append(dict(task=task, arm=label, paper=pv, repro=arm_m, ci=[lo2, hi2], steps=r["mean_steps"], speedup=r["speedup_vs_uniform"], delta=arm_m - base_m, baseline_same_n=base_m, mcnemar_p=p, wins=b01, losses=b10, n=m)) return rows all_rows = [] all_rows += compare("Trip Plan (Claim 3)", "c3_trip_baseline.json", [("CCD", "c3_trip_ccd.json"), ("CCD-DS", "c3_trip_ccd_ds.json"), ("CCD-DS V=16 (repaired)", "c3_trip_ccd_ds_V16.json"), ("CCD V=16", "c3_trip_ccd_V16.json")], {"baseline": 15.10, "CCD": 16.93, "CCD-DS": 19.01}) # Claim 4: merge the two disjoint halves (0..31 and 32..63) -> n=64. # BOTH arms must have BOTH halves, or we would compare a 64-example baseline # against a 32-example CCD -- different problem sets, so the score delta would be # meaningless even though the paired test silently truncates to the common prefix. import json as _json _ext_ready = all(os.path.exists(os.path.join(OUT, f"c4ext_he_{k}.json")) for k in ("baseline", "ccd")) if _ext_ready: for stem in ("baseline", "ccd"): m = load_merged(f"c4_he_{stem}.json", f"c4ext_he_{stem}.json") if m and m.get("merged_from"): _json.dump(m, open(os.path.join(OUT, f"c4merged_he_{stem}.json"), "w")) _b, _c = "c4merged_he_baseline.json", "c4merged_he_ccd.json" print("\n[Claim 4] using MERGED n=64 (problems 0..63; both arms complete)") else: _b, _c = "c4_he_baseline.json", "c4_he_ccd.json" print("\n[Claim 4] extension incomplete -> reporting n=32 (problems 0..31) only") all_rows += compare("HumanEval (Claim 4)", _b, [("CCD", _c), ("CCD-DS", "c4_he_ccd_ds.json"), ("CCD-DS V=12 (repaired)", "c4_he_ccd_ds_V12.json")], {"baseline": 52.66, "CCD": 57.31, "CCD-DS": 56.71}) # ---- Claim 5: buffer ablation print(f"\n{'='*88}\nBuffer ablation, Trip City=3 (Claim 5)\n{'='*88}") b = load("c5_abl_baseline.json") or load("c5_abl_baseline_n60.json") if b: print(f"baseline: score={b['score']:.1f} steps={b['mean_steps']:.1f} " f"n={b['n_examples']} (paper: 58%, 256 steps)") print(f"\n{'axis':>16} {'score':>7} {'steps':>8} {'k=256/steps':>12} " f"{'predicted k':>12} {'n':>4}") abl = [] for V in [1, 2, 4, 8, 16]: r = load(f"c5_abl_V{V}.json") if r: k = 256.0 / r["mean_steps"] print(f"{'V=' + str(V) + ' (d=3)':>16} {r['score']:>7.1f} {r['mean_steps']:>8.1f} " f"{k:>12.2f} {max(1.0, V/4.0):>12.2f} {r['n_examples']:>4}") abl.append(dict(axis="V", val=V, score=r["score"], steps=r["mean_steps"], k=k, pred_k=max(1.0, V / 4.0))) for d in [1, 2, 3, 5]: r = load(f"c5_abl_d{d}.json") or (load("c5_abl_V4.json") if d == 3 else None) if r: k = 256.0 / r["mean_steps"] print(f"{'d=' + str(d) + ' (V=4)':>16} {r['score']:>7.1f} {r['mean_steps']:>8.1f} " f"{k:>12.2f} {max(1.0, 4.0/(d+1)):>12.2f} {r['n_examples']:>4}") abl.append(dict(axis="d", val=d, score=r["score"], steps=r["mean_steps"], k=k, pred_k=max(1.0, 4.0 / (d + 1)))) # ---- Claim 6: temperature print(f"\n{'='*88}\nTemperature robustness, HumanEval (Claim 6)\n{'='*88}") print(f"{'temp':>6} {'baseline':>9} {'CCD-DS':>9} {'delta':>7} {'paper gain':>11} {'n':>4}") paper_gain = {"0.0": 9.8, "0.1": 7.7, "0.4": 1.5, "0.7": 9.0, "1.0": 2.0} temps = [] for t in ["0.0", "0.1", "0.4", "0.7", "1.0"]: rb, rc = load(f"c6_he_baseline_t{t}.json"), load(f"c6_he_ccd_ds_t{t}.json") if rb and rc: print(f"{t:>6} {rb['score']:>9.2f} {rc['score']:>9.2f} " f"{rc['score']-rb['score']:>+7.2f} {paper_gain[t]:>10.1f}% {rb['n_examples']:>4}") temps.append(dict(temp=float(t), baseline=rb["score"], ccd_ds=rc["score"], delta=rc["score"] - rb["score"], paper_gain=paper_gain[t])) os.makedirs(OUT, exist_ok=True) json.dump(dict(main=all_rows, ablation=abl, temperature=temps), open(os.path.join(OUT, "analysis.json"), "w"), indent=1) print(f"\nwrote {os.path.join(OUT, 'analysis.json')}")