File size: 4,646 Bytes
05ab4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Generate paper/table_audit_main.tex: main audit results table.

One row per unlearning method; three column blocks:
  behavioral gates -- checkpoints passing FQ > 0.05 and MU >= 90% of
    theta_0, and the best FQ attained on the grid;
  residual access  -- median normalized J-Access, share of checkpoints
    above the gold anchor, share Holm-significant;
  revival prediction -- within-method Spearman rho between pre-attack
    J-Access and excess revival (n=9 each), 95% CI, pooled in the
    bottom row.
Bold marks the least residual access per column.
"""

import json

import numpy as np
from scipy import stats

RES = "/workspace/jspace-unlearning/results"
OUT = "/workspace/jspace-unlearning/paper/table_audit_main.tex"
MU_GATE = 0.9 * 0.5994

grid = json.load(open(f"{RES}/p0a_grid398.json"))
models = grid["models"]
h2 = json.load(open(f"{RES}/h2_ckpt_stats.json"))
j_gold = grid["digest"]["j_gold"]
methods = sorted({v["method"] for v in models.values()})


def fq_fmt(x):
    s = f"{np.log10(max(x, 1e-300)):.1f}"
    if s == "-0.0":
        s = "0.0"
    return f"${s}$"


rows, zs = [], []
for meth in methods:
    sel = [v for v in models.values() if v["method"] == meth]
    pts = [(v["jocc_v2_mean"], v["excess_rate"]) for k, v in h2.items()
           if k.split("_")[0] == meth]
    r, _ = stats.spearmanr(*zip(*pts))
    z, se = np.arctanh(np.clip(r, -0.999, 0.999)), 1 / np.sqrt(len(pts) - 3)
    zs.append((z, se))
    rows.append(dict(
        name=meth,
        npass=sum(1 for v in sel if v["fq"] > 0.05 and v["mu"] >= MU_GATE),
        ntot=len(sel),
        bestfq=max(v["fq"] for v in sel),
        med=np.median([v["omega"] for v in sel]),
        gt=100 * np.mean([v["jocc10"] > j_gold for v in sel]),
        sig=100 * np.mean([v["p_holm"] < 0.05 for v in sel]),
        rho=(r, np.tanh(z - 1.96 * se), np.tanh(z + 1.96 * se)),
    ))

best = {k: min(r[k] for r in rows) for k in ("med", "gt", "sig")}


def cell(r, key, fmt):
    s = fmt.format(r[key])
    return rf"\textbf{{{s}}}" if np.isclose(r[key], best[key]) else s


lines = []
for r in rows:
    rho, lo, hi = r["rho"]
    lines.append(
        f"{r['name']} & {r['npass']}/{r['ntot']} & {fq_fmt(r['bestfq'])} & "
        f"{cell(r, 'med', '{:.2f}')} & {cell(r, 'gt', '{:.0f}\\%')} & "
        f"{cell(r, 'sig', '{:.0f}\\%')} & "
        f"${rho:+.2f}\\;[{lo:+.2f},\\,{hi:+.2f}]$ \\\\")

wz = sum(z / se ** 2 for z, se in zs) / sum(1 / se ** 2 for _, se in zs)
wse = 1 / np.sqrt(sum(1 / se ** 2 for _, se in zs))
allv = list(models.values())
all_pass = sum(1 for v in allv if v["fq"] > 0.05 and v["mu"] >= MU_GATE)
all_row = (
    f"All & {all_pass}/{len(allv)} & --- & "
    f"{np.median([v['omega'] for v in allv]):.2f} & "
    f"{100 * np.mean([v['jocc10'] > j_gold for v in allv]):.0f}\\% & "
    f"{100 * np.mean([v['p_holm'] < 0.05 for v in allv]):.0f}\\% & "
    f"${np.tanh(wz):+.2f}\\;[{np.tanh(wz - 1.96 * wse):+.2f},"
    f"\\,{np.tanh(wz + 1.96 * wse):+.2f}]$ \\\\")

body = "\n".join(lines)
tex = rf"""% Auto-generated by src/make_table_audit.py from
% results/p0a_grid398.json and results/h2_ckpt_stats.json
\begin{{table*}}[t]
\centering
\caption{{\jocc{{}} audit of the OpenUnlearning grid (398 checkpoints,
8 methods, TOFU forget10). \textbf{{Behavioral gates}}: checkpoints
passing Forget Quality $>0.05$ and model utility $\geq 90\%$ of the
original model $\theta_0$, and the best $\log_{{10}}$ Forget Quality
attained ($0$: indistinguishable from $\theta_g$ under the KS test).
\textbf{{Residual access}}: median normalized \jocc{{}} ($0$:
retain-only anchor $\theta_g$, $1$: $\theta_0$), share of checkpoints
with raw access above $\theta_g$, and share significant under grid-wide
Holm correction. \textbf{{Revival prediction}}: within-method Spearman
$\rho$ between pre-attack \jocc{{}} and excess revival after relearning
($n=9$ checkpoints per method; bottom row: inverse-variance pooled).
$\downarrow$: smaller is preferable; least residual access in
\textbf{{bold}}.}}
\label{{tab:audit}}
\setlength{{\tabcolsep}}{{9pt}}
\renewcommand{{\arraystretch}}{{1.12}}
\begin{{tabular}}{{l|cc|ccc|c}}
\toprule[1.5pt]
\multirow{{2}}{{*}}{{Method}} & \multicolumn{{2}}{{c|}}{{Behavioral gates}}
 & \multicolumn{{3}}{{c|}}{{Residual access}}
 & \multicolumn{{1}}{{c}}{{Revival prediction}} \\
\cmidrule(lr){{2-3}}\cmidrule(lr){{4-6}}\cmidrule(lr){{7-7}}
 & pass & best $\log_{{10}}$FQ & med.\ \jocc{{}} $\downarrow$ & $>\theta_g$ $\downarrow$
 & sig.\ $\downarrow$ & $\rho$ [95\% CI] \\
\midrule[1.5pt]
{body}
\midrule[0.8pt]
{all_row}
\bottomrule[1.5pt]
\end{{tabular}}
\end{{table*}}
"""
open(OUT, "w").write(tex)
print("wrote", OUT)