File size: 2,262 Bytes
a805048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Paired significance tests between configurations (exact McNemar + bootstrap 95% CI).

Reads results/per_question_results.csv and writes results/significance.json.
"""
import csv
import json
import random
from math import comb
from pathlib import Path

RES = Path(__file__).resolve().parents[1] / "results"


def mcnemar_exact(b, c):
    n = b + c
    if n == 0:
        return 1.0
    k = min(b, c)
    p = sum(comb(n, i) for i in range(k + 1)) / 2 ** n * 2
    return min(1.0, p)


def main():
    rows = list(csv.DictReader(open(RES / "per_question_results.csv")))
    by = {}
    for r in rows:
        by.setdefault(r["config"], {})[r["id"]] = r
    out = {}
    for metric, field, better_low in (("behaviour_accuracy", "behaviour_ok", False),
                                      ("unsupported_answer", "unsupported", True)):
        for split in ("all", "test"):
            for other in ("rerank", "verify", "full"):
                ids = [i for i in by["baseline"] if split == "all" or by["baseline"][i]["split"] == "test"]
                a = [by["baseline"][i][field] == "True" for i in ids]
                b = [by[other][i][field] == "True" for i in ids]
                n01 = sum((not x) and y for x, y in zip(a, b))   # baseline 0, other 1
                n10 = sum(x and (not y) for x, y in zip(a, b))
                rng = random.Random(0)
                diffs = []
                for _ in range(5000):
                    s = [rng.randrange(len(ids)) for _ in ids]
                    diffs.append(sum(b[j] for j in s) / len(s) - sum(a[j] for j in s) / len(s))
                diffs.sort()
                out[f"{metric}|{split}|baseline_vs_{other}"] = {
                    "n": len(ids), "baseline": round(sum(a) / len(a), 4), other: round(sum(b) / len(b), 4),
                    "difference": round(sum(b) / len(b) - sum(a) / len(a), 4),
                    "ci95": [round(diffs[125], 4), round(diffs[4874], 4)],
                    "only_other_true": n01, "only_baseline_true": n10,
                    "mcnemar_exact_p": round(mcnemar_exact(n01, n10), 6),
                }
    (RES / "significance.json").write_text(json.dumps(out, indent=2))
    for k, v in out.items():
        print(k, v)


if __name__ == "__main__":
    main()