K-CONF / artykul_before_the_answer /make_appendix.py
JakCwi's picture
Add study report, results, K-EXT validation, paper 'Before the Answer, Not After' and the TES correction package
30dc39a
Raw History Blame Contribute Delete
3.68 kB
import json
A = '../WYNIKI/ANALIZA_KONCOWA/'; V = '../WALIDACJA_KEXT/'
R = json.load(open(A + 'final_kconf2/results_k.json')); PM = R['per_model']; fmt = R['formats']
reg = json.load(open(A + 'models_k.json'))['models']; T1 = sorted([k for k, v in reg.items() if v['tier'] == 1], key=lambda k: (fmt[k], k))
d2 = json.load(open('analysis_v2.json')); val = json.load(open(V + 'walidacja_kext.json')); fam = json.load(open('familiarity_test.json'))
nm = lambda k: reg[k]['id'].split('/')[-1]
ci = lambda t: f"{t[0]:+.3f} [{t[1]:+.3f}, {t[2]:+.3f}]"
o = ['### Table A1. K-CONF-2, answerable vs unanswerable (AUROC)\n',
'Columns marked OOF are out-of-fold logistic AUROCs (5 folds grouped by triplet; the direction of each signal is learned on the training folds) on all A and U items of the by-design triplets. Single-signal OOF values therefore differ slightly (up to about 0.005) from the fixed-direction AUROCs in the companion report, and a reversed signal appears above 0.5: K in Granite-3.3-8B-Instruct has a fixed-direction AUROC of 0.246 (reversed, as in Section 4.1) and an OOF AUROC of 0.754. The column "K, fixed direction" gives the pre-registered value. z(K) − z(entropy) is label-free and has a fixed direction.\n',
'| Model | Format | Items | K, fixed direction | Entropy (OOF) | K (OOF) | P(True) (OOF) | z(K) − z(entropy) | K + entropy (OOF) | K + entropy + P(True) (OOF) | Probe (supervised) |', '|---|---|---|---|---|---|---|---|---|---|---|']
for k in T1:
a = d2['M'][k]['AU']['auc']
o.append(f"| {nm(k)} | {fmt[k]} | {d2['M'][k]['AU']['n']} | {PM[k]['A_vs_U']['auc']['K']['auc']:.3f} | {a['E']:.3f} | {a['K']:.3f} | {a['PT']:.3f} | {a['labelfree']:.3f} | {a['K+E']:.3f} | {a['K+E+PT']:.3f} | {PM[k]['A_vs_U']['probe']['auc_probe_fixed_layer_P']:.3f} |")
o += ['\n### Table A2. Held-out validation on PopQA (plain prompts; out-of-fold AUROC; 95% bootstrap CI)\n',
'| Model | Entropy | K | P(True) | K + entropy | Δ vs entropy | Δ vs K | Δ from adding K to entropy + P(True) | Wrong answer: Δ from adding K to entropy + P(True) |', '|---|---|---|---|---|---|---|---|---|']
for k in T1:
p = val[k]['PQ-AB']; e = val[k]['PQ-ERR']
o.append(f"| {nm(k)} | {p['E']:.3f} | {p['K']:.3f} | {p['PT']:.3f} | {p['K+E']:.3f} | {ci(p['d:K+E-E'])} | {ci(p['d:K+E-K'])} | {ci(p['d:K+E+PT-E+PT'])} | {ci(e['d:K+E+PT-E+PT']) if 'd:K+E+PT-E+PT' in e else 'insufficient events'} |")
o += ['\n### Table A3. Pre-registered semantic-entropy subset (999 items, plain prompts): gain from adding K to entropy + semantic entropy + P(True)\n',
'| Model | Query level: −SE | Query level: Δ [95% CI] | Answer level: SE | Answer level: Δ [95% CI] |', '|---|---|---|---|---|']
for k in T1:
a = d2['S'][k]['AU']; e = d2['S'][k]['ERR']
o.append(f"| {nm(k)} | {a['auc']['SE']:.3f} | {ci(a['gain K over E+SE+PT'])} | {e['auc']['SE']:.3f} | {ci(e['gain K over E+SE+PT'])} |")
fp = json.load(open('familiarity_paired.json'))
o += ['\n### Table A4. Within-triplet comparison: rare subject in an answerable frame (B) versus famous subject in an unanswerable frame (U-knownentity); AUROC of ranking B higher\n',
'| Model | Format | All pairs: K [95% CI] | All: entropy | Same-predicate frames: K | Same-predicate: entropy | Private/attribute frames: K | Private/attribute: entropy |', '|---|---|---|---|---|---|---|---|']
for k in T1:
a, c, t = fp[k]['all'], fp[k]['crosskind'], fp[k]['attr']
o.append(f"| {nm(k)} | {fmt[k]} | {a['K']:.3f} [{a['ci'][0]:.3f}, {a['ci'][1]:.3f}] | {a['E']:.3f} | {c['K']:.3f} | {c['E']:.3f} | {t['K']:.3f} | {t['E']:.3f} |")
open('appendix_tables.md', 'w').write('\n'.join(o) + '\n'); print(len(o))