"""verify_article.py checks section 9.1 (Tables 9b, 9c and the sentences quoting them) against sweep_controls.json, sweep_fourth_*.json and sweep_ablation_all.json. Adds verify_controls() and its call; nothing else changes. python -u apply_verify_controls.py """ from __future__ import annotations PATH = "verify_article.py" FUNC = ''' def verify_controls(): """Section 9.1: the controls (PREREG_controls_2026-09-22, sweep_controls.json).""" import math print("\\nSection 9.1 (controls, sweep_controls.json):") R = _load("sweep_controls.json") seen, four = _load("sweep_fourth_seen.json"), _load("sweep_fourth_fourth.json") ap = dict(seen["Aprime"]) ap.update(four["Aprime"]) chf = dict(seen["chandrupatla"]) chf.update(four["chandrupatla"]) sets = ("APS", "CHA", "NEW", "RJL", "APP", "D") def cost(v): return v[0] if v[1] else 200 def tot(r, keep=None): return sum(cost(v) for p, v in r.items() if keep is None or keep(p)) def h2h(a, b, keep=None): ps = [p for p in a if keep is None or keep(p)] w = sum(cost(a[p]) < cost(b[p]) for p in ps) l = sum(cost(a[p]) > cost(b[p]) for p in ps) return w, len(ps) - w - l, l def sign_p(w, l): k, n = min(w, l), w + l return min(1.0, 2.0 * sum(math.comb(n, i) for i in range(k + 1)) / 2.0 ** n) rows = {"K2L": (2235, 1437, 4026, 1495, 1490, 4488, 15171), "K2P": (2682, 2526, 5485, 2117, 1622, 6236, 20668), "K2F": (2693, 2526, 5569, 2126, 1670, 6165, 20749)} check("Table 9b A' row", tuple(tot(ap[s]) for s in sets) + (sum(tot(ap[s]) for s in sets),), (2259, 1074, 3290, 1371, 1382, 3656, 13032)) for k, want in rows.items(): k2 = R["K2"][k] check(f"Table 9b {k} row", tuple(tot(k2[s]) for s in sets) + (sum(tot(k2[s]) for s in sets),), want) for k, wtl, pp in (("K2L", (84, 121, 35), ("1g", "8e-06")), ("K2P", (110, 96, 34), ("2g", "1.5e-10")), ("K2F", (113, 87, 40), ("1g", "3e-09"))): got = h2h(ap["D"], R["K2"][k]["D"]) check(f"Table 9b A' vs {k} on corpus 4", got, wtl) check(f"Table 9b p vs {k}", format(sign_p(got[0], got[2]), "." + pp[0]), pp[1]) check("Table 9b violations", sum(v[2] for k in rows for s in sets for v in R["K2"][k][s].values()), 0) for cls, want in (("D2", (1163, 1739)), ("D3", (924, 1207)), ("D5", (503, 477))): keep = lambda p, c=cls: p.startswith(c) # noqa: E731 check(f"9.1 {cls}: A' vs Lagouanelle", (tot(ap["D"], keep), tot(R["K2"]["K2L"]["D"], keep)), want) level = [(tot(ap["D"], lambda p, c=c: p.startswith(c)), tot(R["K2"]["K2L"]["D"], lambda p, c=c: p.startswith(c))) for c in ("D1", "D4", "D6")] check("9.1 level on the rest (within 1.5%)", all(abs(a - b) <= 0.015 * b for a, b in level), True) ab = _load("sweep_ablation_all.json")["abl-regime"] no_regime_all = sum(tot(ab[s]) for s in sets) check("band and no-detector worse than no regime (six corpora)", all(sum(tot(R["K2"][k][s]) for s in sets) > no_regime_all for k in ("K2P", "K2F")), True) check("band and no-detector worse than no regime (corpus 4)", all(tot(R["K2"][k]["D"]) > tot(ab["D"]) for k in ("K2P", "K2F")), True) t9c = {"CHA": (20, 654, 96, 66, 1068), "NEW": (40, 1229, 358, 337, 1792), "D": (88, 2159, 901, 844, 4151)} nonsimple = lambda m: m >= 2.0 or m <= 2.0 / 3.0 # noqa: E731 wins = 0 for s, want in t9c.items(): k1 = R["K1"][s] keep = lambda p, _m=k1["mstar"]: nonsimple(_m[p]) # noqa: E731 got = (sum(1 for p in k1["mstar"] if keep(p)), tot(ap[s], keep), tot(k1["chandrupatla_g"], keep), tot(k1["brent_g"], keep), tot(chf[s], keep)) check(f"Table 9c {s}", got, want) wins += h2h({p: v for p, v in ap[s].items() if keep(p)}, k1["chandrupatla_g"])[0] check("Table 9c: A' wins none of the 148", wins, 0) check("Table 9c: 2.4 to 6.8 times", (round(2159 / 901, 1), round(654 / 96, 1), min(round(a / c, 1) for _n, a, c, _b, _f in t9c.values()), max(round(a / c, 1) for _n, a, c, _b, _f in t9c.values())), (2.4, 6.8, 2.4, 6.8)) APS_ = "APS" for s, want in ((APS_, (2259, 2640)), ("CHA", (420, 437)), ("NEW", (2061, 1924)), ("D", (1497, 1427))): k1 = R["K1"][s] keep = lambda p, _m=k1["mstar"]: not nonsimple(_m[p]) # noqa: E731 check(f"9.1 rest {s}: A' vs Chandrupatla on g", (tot(ap[s], keep), tot(k1["chandrupatla_g"], keep)), want) ''' src = open(PATH, encoding="utf8").read() call_old = " verify_revision1()\n\n print()" call_new = " verify_revision1()\n verify_controls()\n\n print()" marker = '\n\nif __name__ == "__main__":\n main()\n' assert src.count(call_old) == 1 and src.count(marker) == 1 src = src.replace(call_old, call_new).replace(marker, FUNC + marker) open(PATH, "w", encoding="utf8").write(src) print("added verify_controls")