File size: 2,050 Bytes
830e33a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Logit-ensemble of two ARC runs (same items, same options): accuracy as a function of
the mixing weight on model A, plus paired stats against each endpoint."""
import json, math, sys

def load(p):
    d = json.load(open(p))
    if isinstance(d, list):  # legacy format: per-item correctness only
        raise SystemExit(f"{p} has no per-option logprobs; rerun with --dump-lp")
    return d

def acc(lp, gold):
    return sum(int(max(range(len(s)), key=lambda j: s[j]) == g) for s, g in zip(lp, gold)) / len(gold)

def mcnemar(a, b):
    n01 = sum(1 for x, y in zip(a, b) if x and not y)
    n10 = sum(1 for x, y in zip(a, b) if not x and y)
    m = n01 + n10
    p = min(1.0, 2 * sum(math.comb(m, i) for i in range(0, min(n01, n10) + 1)) / 2 ** m) if m else 1.0
    return n01, n10, p

A = load(sys.argv[1]); B = load(sys.argv[2])
lab_a = sys.argv[3] if len(sys.argv) > 3 else "A"
lab_b = sys.argv[4] if len(sys.argv) > 4 else "B"
assert A["gold"] == B["gold"] and len(A["lp"]) == len(B["lp"]), "different item sets"
gold = A["gold"]
print(f"{'w(' + lab_a + ')':>16}  acc     vs {lab_a:<14} vs {lab_b}")
best = None
for w in [0, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 1.0]:
    mix = [[w * x + (1 - w) * y for x, y in zip(sa, sb)] for sa, sb in zip(A["lp"], B["lp"])]
    per = [int(max(range(len(s)), key=lambda j: s[j]) == g) for s, g in zip(mix, gold)]
    a_per = [int(max(range(len(s)), key=lambda j: s[j]) == g) for s, g in zip(A["lp"], gold)]
    b_per = [int(max(range(len(s)), key=lambda j: s[j]) == g) for s, g in zip(B["lp"], gold)]
    n01, n10, p = mcnemar(b_per, per)
    n01b, n10b, pb = mcnemar(a_per, per)
    print(f"{w:16.2f}  {sum(per)/len(per)*100:5.1f}%  {sum(per)/len(per)*100 - sum(a_per)/len(a_per)*100:+5.1f}pp (p={pb:.3f})  "
          f"{sum(per)/len(per)*100 - sum(b_per)/len(b_per)*100:+5.1f}pp (p={p:.3f})")
    if best is None or sum(per) > best[1]:
        best = (w, sum(per), p)
print(f"best w({lab_a})={best[0]}  acc={best[1]/len(gold)*100:.1f}%  p_vs_B={best[2]:.3f}")