Upload code/analyze.py with huggingface_hub
Browse files- code/analyze.py +197 -55
code/analyze.py
CHANGED
|
@@ -98,10 +98,18 @@ set4 = load("set4_goldfish.jsonl")
|
|
| 98 |
rows4 = []
|
| 99 |
for r in set4:
|
| 100 |
rg = r["rungs"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
m1 = {k: v for k, v in rg.items() if k.startswith("M1")}
|
| 102 |
-
best = min(m1, key=lambda k: m1[k]["
|
| 103 |
-
d0 = rg["M0_naive_avg"]["
|
| 104 |
-
d1 = m1[best]["
|
| 105 |
row = {"set": "SET4_goldfish", "substrate": "goldfish-125M", "pair": f"eng-{r['lang']}",
|
| 106 |
"lang": r["lang"], "floor_eng": r["floor_eng"], "floor_x": r["floor_x"],
|
| 107 |
"dfloor_M0": d0, "dfloor_M1best": d1, "M1best": best,
|
|
@@ -139,74 +147,142 @@ PRED_KEYS = ["p_weight_cosine", "p_weight_cosine_bn", "p_d_raw", "p_qmd_perm", "
|
|
| 139 |
"p_qmd_act_perm", "p_qmd_act_procrustes", "p_qmd_act_ot", "p_task_vector_cosine"]
|
| 140 |
|
| 141 |
pred_rows, roc_store = [], {}
|
|
|
|
|
|
|
| 142 |
for size in sorted({r["size"] for r in rows1}):
|
| 143 |
sub = [r for r in rows1 if r["size"] == size]
|
| 144 |
if len(sub) < 8:
|
| 145 |
continue
|
| 146 |
-
y_cont = np.array([r["rescue_frac"] for r in sub], float)
|
| 147 |
-
med = np.nanmedian(y_cont)
|
| 148 |
-
y = (y_cont > med).astype(int)
|
| 149 |
seeds = sorted({r["a"] for r in sub} | {r["b"] for r in sub})
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
te = np.array([(r["a"] == s or r["b"] == s) for r in sub])
|
| 160 |
-
tr = ~te
|
| 161 |
-
if tr.sum() < 4 or te.sum() < 1: continue
|
| 162 |
-
sgn = np.sign(spearman(x[tr], y_cont[tr])) or 1.0
|
| 163 |
-
oof[te] = sgn * x[te]
|
| 164 |
-
a_oof = auroc(oof, y)
|
| 165 |
-
a_in = auroc(np.sign(spearman(x, y_cont) or 1.0) * x, y)
|
| 166 |
-
# SEED-CLUSTER PERMUTATION NULL: permute the seed identities and re-map each pair's outcome
|
| 167 |
-
# to the outcome of the permuted pair; the predictor vector is untouched.
|
| 168 |
-
pair_ix = {(r["a"], r["b"]): i for i, r in enumerate(sub)}
|
| 169 |
-
null = []
|
| 170 |
for _ in range(2000):
|
| 171 |
pi = rng.permutation(seeds)
|
| 172 |
-
m = {
|
| 173 |
-
idx = []
|
| 174 |
for r in sub:
|
| 175 |
u, v = sorted((m[r["a"]], m[r["b"]]))
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
"perm_null_mean": float(null.mean()) if len(null) else float("nan"),
|
| 184 |
-
"
|
| 185 |
-
|
| 186 |
-
# multivariate, held out by seed
|
| 187 |
-
X = np.array([[r.get(k, np.nan) for k in PRED_KEYS] for r in sub], float)
|
| 188 |
-
good = np.isfinite(X).all(0) & (np.nanstd(X, 0) > 0)
|
| 189 |
-
Xg = X[:, good]
|
| 190 |
-
mu, sd = Xg.mean(0), Xg.std(0) + 1e-12
|
| 191 |
-
Xg = (Xg - mu) / sd
|
| 192 |
-
oof = np.full(len(sub), np.nan)
|
| 193 |
-
for s in seeds:
|
| 194 |
-
te = np.array([(r["a"] == s or r["b"] == s) for r in sub]); tr = ~te
|
| 195 |
-
if tr.sum() < 4: continue
|
| 196 |
-
w, b = ridge(Xg[tr], y_cont[tr], lam=2.0)
|
| 197 |
-
oof[te] = Xg[te] @ w + b
|
| 198 |
-
pred_rows.append({"set": "SET1", "substrate": f"pythia-{size}", "n_pairs": len(sub),
|
| 199 |
-
"predictor": "MULTIVARIATE_ridge_all", "spearman_rescue": spearman(oof, y_cont),
|
| 200 |
-
"auroc_in_sample": float("nan"), "auroc_heldout_by_seed": auroc(oof, y),
|
| 201 |
-
"perm_null_mean": float("nan"), "perm_null_p": float("nan")})
|
| 202 |
|
| 203 |
if pred_rows:
|
| 204 |
-
|
| 205 |
-
q = bh(ps)
|
| 206 |
for r, qq in zip(pred_rows, q):
|
| 207 |
r["bh_q"] = float(qq) if np.isfinite(qq) else ""
|
| 208 |
to_csv(pred_rows, f"{R}/predictor_auroc.csv")
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
# SET 4: leave-one-language-out, n=4 -> report Spearman only, flagged as underpowered
|
| 211 |
pred4 = []
|
| 212 |
if len(rows4) >= 3:
|
|
@@ -290,3 +366,69 @@ if rows4:
|
|
| 290 |
fig.tight_layout(); fig.savefig(f"{F}/set4_dfloor.png", bbox_inches="tight"); plt.close(fig)
|
| 291 |
|
| 292 |
print("figures + csvs written")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
rows4 = []
|
| 99 |
for r in set4:
|
| 100 |
rg = r["rungs"]
|
| 101 |
+
# Re-reference the X-language floor to the X parent's OWN tokenizer. The English parent's
|
| 102 |
+
# nats/byte on X text is degenerate wherever the English tokenizer UNK-s the script (46% of
|
| 103 |
+
# Greek tokens), so min(parents) was picking up an artifact rather than a floor.
|
| 104 |
+
px = r["parents"]["x_on_x"]["nats_per_byte"]
|
| 105 |
+
for _k, _v in rg.items():
|
| 106 |
+
_v["delta_floor_x"] = _v["x"]["nats_per_byte"] - px
|
| 107 |
+
_v["delta_floor_mean"] = 0.5 * (_v["delta_floor_eng"] + _v["delta_floor_x"])
|
| 108 |
+
r["floor_x"] = px
|
| 109 |
m1 = {k: v for k, v in rg.items() if k.startswith("M1")}
|
| 110 |
+
best = min(m1, key=lambda k: m1[k]["delta_floor_eng"]) if m1 else None
|
| 111 |
+
d0 = rg["M0_naive_avg"]["delta_floor_eng"]
|
| 112 |
+
d1 = m1[best]["delta_floor_eng"] if best else float("nan")
|
| 113 |
row = {"set": "SET4_goldfish", "substrate": "goldfish-125M", "pair": f"eng-{r['lang']}",
|
| 114 |
"lang": r["lang"], "floor_eng": r["floor_eng"], "floor_x": r["floor_x"],
|
| 115 |
"dfloor_M0": d0, "dfloor_M1best": d1, "M1best": best,
|
|
|
|
| 147 |
"p_qmd_act_perm", "p_qmd_act_procrustes", "p_qmd_act_ot", "p_task_vector_cosine"]
|
| 148 |
|
| 149 |
pred_rows, roc_store = [], {}
|
| 150 |
+
OUTCOMES = [("rescue_frac", "fraction of the naive Δfloor that the best M1 rung removes", +1),
|
| 151 |
+
("dfloor_M1best", "Δfloor of the best M1 rung (how good the ALIGNED merge actually is)", -1)]
|
| 152 |
for size in sorted({r["size"] for r in rows1}):
|
| 153 |
sub = [r for r in rows1 if r["size"] == size]
|
| 154 |
if len(sub) < 8:
|
| 155 |
continue
|
|
|
|
|
|
|
|
|
|
| 156 |
seeds = sorted({r["a"] for r in sub} | {r["b"] for r in sub})
|
| 157 |
+
pair_ix = {(r["a"], r["b"]): i for i, r in enumerate(sub)}
|
| 158 |
+
complete = len(sub) == len(seeds) * (len(seeds) - 1) // 2
|
| 159 |
+
for oname, odesc, osign in OUTCOMES:
|
| 160 |
+
y_cont = osign * np.array([r[oname] for r in sub], float)
|
| 161 |
+
med = np.nanmedian(y_cont)
|
| 162 |
+
y = (y_cont > med).astype(int)
|
| 163 |
+
rng = np.random.default_rng(0)
|
| 164 |
+
# pre-draw the seed-cluster permutations ONCE per outcome so every predictor sees the same null
|
| 165 |
+
perms = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
for _ in range(2000):
|
| 167 |
pi = rng.permutation(seeds)
|
| 168 |
+
m = {sd: pi[i] for i, sd in enumerate(seeds)}
|
| 169 |
+
idx, ok = [], True
|
| 170 |
for r in sub:
|
| 171 |
u, v = sorted((m[r["a"]], m[r["b"]]))
|
| 172 |
+
if (u, v) not in pair_ix:
|
| 173 |
+
ok = False; break
|
| 174 |
+
idx.append(pair_ix[(u, v)])
|
| 175 |
+
if ok:
|
| 176 |
+
perms.append(np.asarray(idx))
|
| 177 |
+
for pk in PRED_KEYS:
|
| 178 |
+
x = np.array([r.get(pk, np.nan) for r in sub], float)
|
| 179 |
+
if np.isfinite(x).sum() < 8 or np.nanstd(x) == 0:
|
| 180 |
+
continue
|
| 181 |
+
# HELD OUT BY SEED: fold k = every pair touching seed k, fitted on pairs touching neither,
|
| 182 |
+
# so the predictor's SIGN never sees the held-out pairs.
|
| 183 |
+
oof = np.full(len(sub), np.nan)
|
| 184 |
+
for sd_ in seeds:
|
| 185 |
+
te = np.array([(r["a"] == sd_ or r["b"] == sd_) for r in sub]); tr = ~te
|
| 186 |
+
if tr.sum() < 4 or te.sum() < 1: continue
|
| 187 |
+
sgn = np.sign(spearman(x[tr], y_cont[tr])) or 1.0
|
| 188 |
+
oof[te] = sgn * x[te]
|
| 189 |
+
a_oof = auroc(oof, y)
|
| 190 |
+
a_in = auroc(np.sign(spearman(x, y_cont) or 1.0) * x, y)
|
| 191 |
+
null = np.array([auroc(oof, y[ix]) for ix in perms]) if len(perms) >= 200 else np.array([])
|
| 192 |
+
null = null[np.isfinite(null)]
|
| 193 |
+
pval = float((np.sum(null >= a_oof) + 1) / (len(null) + 1)) if len(null) else float("nan")
|
| 194 |
+
pred_rows.append({"set": "SET1", "substrate": f"pythia-{size}", "outcome": oname,
|
| 195 |
+
"n_pairs": len(sub), "predictor": pk[2:],
|
| 196 |
+
"spearman_rescue": spearman(x, y_cont),
|
| 197 |
+
"auroc_in_sample": a_in, "auroc_heldout_by_seed": a_oof,
|
| 198 |
+
"perm_null_mean": float(null.mean()) if len(null) else float("nan"),
|
| 199 |
+
"n_null_draws": int(len(null)), "pairs_complete": int(complete),
|
| 200 |
+
"perm_null_p": pval})
|
| 201 |
+
if oname == "rescue_frac":
|
| 202 |
+
roc_store[(size, pk)] = (oof, y)
|
| 203 |
+
# multivariate, held out by seed
|
| 204 |
+
X = np.array([[r.get(k, np.nan) for k in PRED_KEYS] for r in sub], float)
|
| 205 |
+
good = np.isfinite(X).all(0) & (np.nanstd(X, 0) > 0)
|
| 206 |
+
Xg = X[:, good]
|
| 207 |
+
Xg = (Xg - Xg.mean(0)) / (Xg.std(0) + 1e-12)
|
| 208 |
+
oof = np.full(len(sub), np.nan)
|
| 209 |
+
for sd_ in seeds:
|
| 210 |
+
te = np.array([(r["a"] == sd_ or r["b"] == sd_) for r in sub]); tr = ~te
|
| 211 |
+
if tr.sum() < 4: continue
|
| 212 |
+
w, b = ridge(Xg[tr], y_cont[tr], lam=2.0)
|
| 213 |
+
oof[te] = Xg[te] @ w + b
|
| 214 |
+
a_oof = auroc(oof, y)
|
| 215 |
+
null = np.array([auroc(oof, y[ix]) for ix in perms]) if len(perms) >= 200 else np.array([])
|
| 216 |
+
null = null[np.isfinite(null)]
|
| 217 |
+
pred_rows.append({"set": "SET1", "substrate": f"pythia-{size}", "outcome": oname,
|
| 218 |
+
"n_pairs": len(sub), "predictor": "MULTIVARIATE_ridge_all",
|
| 219 |
+
"spearman_rescue": spearman(oof, y_cont), "auroc_in_sample": float("nan"),
|
| 220 |
+
"auroc_heldout_by_seed": a_oof,
|
| 221 |
"perm_null_mean": float(null.mean()) if len(null) else float("nan"),
|
| 222 |
+
"n_null_draws": int(len(null)), "pairs_complete": int(complete),
|
| 223 |
+
"perm_null_p": float((np.sum(null >= a_oof) + 1) / (len(null) + 1)) if len(null) else float("nan")})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
if pred_rows:
|
| 226 |
+
q = bh([r["perm_null_p"] for r in pred_rows])
|
|
|
|
| 227 |
for r, qq in zip(pred_rows, q):
|
| 228 |
r["bh_q"] = float(qq) if np.isfinite(qq) else ""
|
| 229 |
to_csv(pred_rows, f"{R}/predictor_auroc.csv")
|
| 230 |
|
| 231 |
+
# ---------------- P0-2b: does the predictor transfer ACROSS substrates (leave-one-size-out)?
|
| 232 |
+
xfer = []
|
| 233 |
+
szs_all = sorted({r["size"] for r in rows1 if len([q for q in rows1 if q["size"] == r["size"]]) >= 8})
|
| 234 |
+
if len(szs_all) >= 3:
|
| 235 |
+
pool = [r for r in rows1 if r["size"] in szs_all]
|
| 236 |
+
for oname, osign in (("rescue_frac", +1), ("dfloor_M1best", -1)):
|
| 237 |
+
Y = osign * np.array([r[oname] for r in pool], float)
|
| 238 |
+
SZ = np.array([r["size"] for r in pool])
|
| 239 |
+
X = np.array([[r.get(k, np.nan) for k in PRED_KEYS] for r in pool], float)
|
| 240 |
+
good = np.isfinite(X).all(0) & (np.nanstd(X, 0) > 0)
|
| 241 |
+
Xg = X[:, good].copy()
|
| 242 |
+
# standardise WITHIN size: the raw scales differ across substrates, and a predictor that only
|
| 243 |
+
# works because it encodes "which size is this" is not a transferring predictor.
|
| 244 |
+
for sz in szs_all:
|
| 245 |
+
m = SZ == sz
|
| 246 |
+
Xg[m] = (Xg[m] - Xg[m].mean(0)) / (Xg[m].std(0) + 1e-12)
|
| 247 |
+
oof = np.full(len(pool), np.nan)
|
| 248 |
+
for sz in szs_all:
|
| 249 |
+
te = SZ == sz; tr = ~te
|
| 250 |
+
w, b = ridge(Xg[tr], Y[tr], lam=2.0)
|
| 251 |
+
oof[te] = Xg[te] @ w + b
|
| 252 |
+
rng = np.random.default_rng(1)
|
| 253 |
+
for sz in szs_all:
|
| 254 |
+
te = SZ == sz
|
| 255 |
+
y = (Y[te] > np.median(Y[te])).astype(int)
|
| 256 |
+
a = auroc(oof[te], y)
|
| 257 |
+
null = np.array([auroc(oof[te], y[rng.permutation(len(y))]) for _ in range(2000)])
|
| 258 |
+
null = null[np.isfinite(null)]
|
| 259 |
+
xfer.append({"outcome": oname, "held_out_substrate": f"pythia-{sz}", "n": int(te.sum()),
|
| 260 |
+
"auroc_transfer": a, "null_mean": float(null.mean()),
|
| 261 |
+
"perm_p": float((np.sum(null >= a) + 1) / (len(null) + 1))})
|
| 262 |
+
# univariate transfer of the single most-cited predictor family
|
| 263 |
+
for pk in ("p_coord_share_bnd_perm", "p_qmd_act_perm", "p_cka_mean", "p_weight_cosine"):
|
| 264 |
+
if pk not in PRED_KEYS: continue
|
| 265 |
+
j = PRED_KEYS.index(pk)
|
| 266 |
+
if not good[j]: continue
|
| 267 |
+
col = np.where(good)[0].tolist().index(j)
|
| 268 |
+
for sz in szs_all:
|
| 269 |
+
te = SZ == sz; tr = ~te
|
| 270 |
+
sgn = np.sign(spearman(Xg[tr, col], Y[tr])) or 1.0
|
| 271 |
+
y = (Y[te] > np.median(Y[te])).astype(int)
|
| 272 |
+
a = auroc(sgn * Xg[te, col], y)
|
| 273 |
+
null = np.array([auroc(sgn * Xg[te, col], y[rng.permutation(len(y))]) for _ in range(1000)])
|
| 274 |
+
null = null[np.isfinite(null)]
|
| 275 |
+
xfer.append({"outcome": oname, "held_out_substrate": f"pythia-{sz}", "n": int(te.sum()),
|
| 276 |
+
"predictor": pk[2:], "auroc_transfer": a,
|
| 277 |
+
"null_mean": float(null.mean()),
|
| 278 |
+
"perm_p": float((np.sum(null >= a) + 1) / (len(null) + 1))})
|
| 279 |
+
for r in xfer:
|
| 280 |
+
r.setdefault("predictor", "MULTIVARIATE_ridge_all")
|
| 281 |
+
qq = bh([r["perm_p"] for r in xfer])
|
| 282 |
+
for r, q in zip(xfer, qq):
|
| 283 |
+
r["bh_q"] = float(q)
|
| 284 |
+
to_csv(xfer, f"{R}/predictor_transfer_across_size.csv")
|
| 285 |
+
|
| 286 |
# SET 4: leave-one-language-out, n=4 -> report Spearman only, flagged as underpowered
|
| 287 |
pred4 = []
|
| 288 |
if len(rows4) >= 3:
|
|
|
|
| 366 |
fig.tight_layout(); fig.savefig(f"{F}/set4_dfloor.png", bbox_inches="tight"); plt.close(fig)
|
| 367 |
|
| 368 |
print("figures + csvs written")
|
| 369 |
+
|
| 370 |
+
# ------------------------------------------------------------------ 5. BLiMP dissociation
|
| 371 |
+
blimp = load("blimp_*.jsonl")
|
| 372 |
+
if blimp:
|
| 373 |
+
brows = []
|
| 374 |
+
for b in blimp:
|
| 375 |
+
m1 = {k: v for k, v in b["rungs"].items() if k.startswith("M1")}
|
| 376 |
+
brows.append({"size": b["size"], "pair": tuple(b["pair"]),
|
| 377 |
+
"ceiling": b["ceiling"], "parent_mean": float(np.mean(list(b["parent_acc"].values()))),
|
| 378 |
+
"M0": b["rungs"]["M0_naive_avg"]["blimp_acc"],
|
| 379 |
+
"M1best": max(v["blimp_acc"] for v in m1.values()),
|
| 380 |
+
**{f"acc_{k}": v["blimp_acc"] for k, v in b["rungs"].items()}})
|
| 381 |
+
to_csv(brows, f"{R}/blimp_pairs.csv")
|
| 382 |
+
s1 = {(r["size"], (r["a"], r["b"])): r for r in rows1}
|
| 383 |
+
sizes_b = sorted({b["size"] for b in brows}, key=lambda x: int(x[:-1]))
|
| 384 |
+
fig, axes = plt.subplots(1, 2, figsize=(9, 3.8))
|
| 385 |
+
for sz in sizes_b:
|
| 386 |
+
sub = [b for b in brows if b["size"] == sz]
|
| 387 |
+
xs, ys = [], []
|
| 388 |
+
for b in sub:
|
| 389 |
+
k = (sz, b["pair"])
|
| 390 |
+
if k in s1 and np.isfinite(s1[k]["rescue_nats"]):
|
| 391 |
+
xs.append(s1[k]["rescue_nats"]); ys.append(b["M1best"] - b["M0"])
|
| 392 |
+
if xs:
|
| 393 |
+
axes[0].scatter(xs, ys, s=20, alpha=.75, label=f"pythia-{sz} (n={len(xs)})")
|
| 394 |
+
axes[0].axhline(0, color="k", lw=.7)
|
| 395 |
+
axes[0].set_xlabel("likelihood rescue from alignment (nats/token removed)")
|
| 396 |
+
axes[0].set_ylabel("accuracy rescue (BLiMP, M1best − M0)")
|
| 397 |
+
axes[0].set_title("Rescue in nats does NOT buy rescue in accuracy", fontsize=9)
|
| 398 |
+
axes[0].legend(fontsize=7, frameon=False)
|
| 399 |
+
lab, vals = [], []
|
| 400 |
+
for sz in sizes_b:
|
| 401 |
+
sub = [b for b in brows if b["size"] == sz]
|
| 402 |
+
lab.append(f"pythia-{sz}\n(n={len(sub)})")
|
| 403 |
+
vals.append([np.mean([b["parent_mean"] for b in sub]), np.mean([b["M0"] for b in sub]),
|
| 404 |
+
np.mean([b["acc_M1_perm_avg"] for b in sub]), np.mean([b["acc_M1_orth_avg"] for b in sub])])
|
| 405 |
+
vals = np.array(vals)
|
| 406 |
+
w = 0.2
|
| 407 |
+
for i, nm in enumerate(["parents", "M0 naive", "M1 permutation", "M1 Procrustes"]):
|
| 408 |
+
axes[1].bar(np.arange(len(lab)) + i * w, vals[:, i], width=w, label=nm)
|
| 409 |
+
axes[1].axhline(0.5, color="k", ls="--", lw=.8)
|
| 410 |
+
axes[1].text(0.02, 0.505, "chance", fontsize=7, transform=axes[1].get_yaxis_transform())
|
| 411 |
+
axes[1].set_xticks(np.arange(len(lab)) + 1.5 * w); axes[1].set_xticklabels(lab, fontsize=7)
|
| 412 |
+
axes[1].set_ylim(0.45, None); axes[1].set_ylabel("BLiMP accuracy")
|
| 413 |
+
axes[1].legend(fontsize=7, frameon=False)
|
| 414 |
+
axes[1].set_title("Parents vs merges", fontsize=9)
|
| 415 |
+
fig.suptitle("SET 1 · likelihood recovery vs grammatical competence", fontsize=10)
|
| 416 |
+
fig.tight_layout(); fig.savefig(f"{F}/set1_blimp_dissociation.png", bbox_inches="tight"); plt.close(fig)
|
| 417 |
+
|
| 418 |
+
# ------------------------------------------------------------------ 6. scale trend
|
| 419 |
+
if rows1:
|
| 420 |
+
szs = sorted({r["size"] for r in rows1}, key=lambda s: int(s[:-1]))
|
| 421 |
+
P = {"14m": 14, "31m": 31, "70m": 70, "160m": 160, "410m": 410}
|
| 422 |
+
x = [P[s] for s in szs]
|
| 423 |
+
naive = [np.mean([r["dfloor_M0_naive_avg"] for r in rows1 if r["size"] == s]) for s in szs]
|
| 424 |
+
resc = [np.mean([1 - min(r["dfloor_M1_perm_avg"], r["dfloor_M1_orth_avg"]) / r["dfloor_M0_naive_avg"]
|
| 425 |
+
for r in rows1 if r["size"] == s]) * 100 for s in szs]
|
| 426 |
+
fig, ax = plt.subplots(figsize=(4.6, 3.6))
|
| 427 |
+
ax.plot(x, naive, "o-", color="#c0392b", label="naive merge Δfloor (nats/token)")
|
| 428 |
+
ax.set_xscale("log"); ax.set_xticks(x); ax.set_xticklabels(szs)
|
| 429 |
+
ax.set_xlabel("PolyPythia size"); ax.set_ylabel("naive Δfloor (nats/token)", color="#c0392b")
|
| 430 |
+
ax2 = ax.twinx(); ax2.plot(x, resc, "s--", color="#2471a3", label="rescue by alignment (%)")
|
| 431 |
+
ax2.set_ylabel("% of naive Δfloor removed by alignment", color="#2471a3"); ax2.grid(False)
|
| 432 |
+
ax.set_title("Both the obstruction AND alignment's purchase\nshrink with scale", fontsize=9)
|
| 433 |
+
fig.tight_layout(); fig.savefig(f"{F}/set1_scale_trend.png", bbox_inches="tight"); plt.close(fig)
|
| 434 |
+
print("extra figures written")
|