File size: 7,276 Bytes
14a24b9 | 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #!/usr/bin/env python3
"""Analyze format-validity diagnostics from scored per-item files.
Expected layout:
results/
model_a/
mcqa_4way.json
mcqa_4way.per_item.jsonl
mcqa_10way.json
mcqa_10way.per_item.jsonl
open_short_answer.json
open_short_answer.per_item.jsonl
"""
from __future__ import annotations
import argparse
import itertools
import json
import math
import random
from pathlib import Path
from typing import Any
FORMAT_FILES = {
"mcqa4": "mcqa_4way",
"mcqa10": "mcqa_10way",
"open": "open_short_answer",
}
def read_json(path: Path) -> dict[str, Any]:
return json.loads(path.read_text(encoding="utf-8"))
def read_jsonl(path: Path) -> list[dict[str, Any]]:
rows = []
with path.open("r", encoding="utf-8") as f:
for line in f:
if line.strip():
rows.append(json.loads(line))
return rows
def rank(values: dict[str, float]) -> dict[str, float]:
sorted_items = sorted(values.items(), key=lambda x: x[1])
ranks: dict[str, float] = {}
idx = 0
while idx < len(sorted_items):
j = idx
while j + 1 < len(sorted_items) and sorted_items[j + 1][1] == sorted_items[idx][1]:
j += 1
avg_rank = (idx + j) / 2 + 1
for k in range(idx, j + 1):
ranks[sorted_items[k][0]] = avg_rank
idx = j + 1
return ranks
def pearson(xs: list[float], ys: list[float]) -> float:
if len(xs) < 2:
return math.nan
mx = sum(xs) / len(xs)
my = sum(ys) / len(ys)
cov = sum((x - mx) * (y - my) for x, y in zip(xs, ys))
vx = sum((x - mx) ** 2 for x in xs)
vy = sum((y - my) ** 2 for y in ys)
if vx == 0 or vy == 0:
return math.nan
return cov / math.sqrt(vx * vy)
def spearman(a: dict[str, float], b: dict[str, float]) -> float:
common = sorted(set(a) & set(b))
if len(common) < 2:
return math.nan
ra = rank({k: a[k] for k in common})
rb = rank({k: b[k] for k in common})
return pearson([ra[k] for k in common], [rb[k] for k in common])
def load_results(root: Path) -> tuple[dict[str, dict[str, Any]], dict[str, dict[str, list[dict[str, Any]]]]]:
summaries: dict[str, dict[str, Any]] = {}
per_items: dict[str, dict[str, list[dict[str, Any]]]] = {}
for model_dir in sorted(p for p in root.iterdir() if p.is_dir()):
model = model_dir.name
summaries[model] = {}
per_items[model] = {}
for fmt, stem in FORMAT_FILES.items():
summary_path = model_dir / f"{stem}.json"
per_item_path = model_dir / f"{stem}.per_item.jsonl"
if summary_path.exists() and per_item_path.exists():
summaries[model][fmt] = read_json(summary_path)
per_items[model][fmt] = read_jsonl(per_item_path)
return summaries, per_items
def paired_bootstrap_diff_ci(
rows_a: list[dict[str, Any]],
rows_b: list[dict[str, Any]],
seed: int = 42,
n_boot: int = 2000,
) -> tuple[float, float]:
by_a = {row["id"]: row["correct"] for row in rows_a}
by_b = {row["id"]: row["correct"] for row in rows_b}
ids = sorted(set(by_a) & set(by_b))
if not ids:
return (math.nan, math.nan)
rng = random.Random(seed)
diffs = []
n = len(ids)
for _ in range(n_boot):
sample = [ids[rng.randrange(n)] for _ in range(n)]
diff = sum(by_a[i] - by_b[i] for i in sample) / n
diffs.append(diff)
diffs.sort()
return diffs[int(0.025 * n_boot)], diffs[int(0.975 * n_boot)]
def pairwise_distinguishable_ratio(per_items: dict[str, dict[str, list[dict[str, Any]]]], fmt: str) -> dict[str, Any]:
models = sorted(model for model, fmts in per_items.items() if fmt in fmts)
pairs = list(itertools.combinations(models, 2))
distinguishable = 0
details = []
for a, b in pairs:
low, high = paired_bootstrap_diff_ci(per_items[a][fmt], per_items[b][fmt])
is_distinct = int(not (low <= 0 <= high))
distinguishable += is_distinct
details.append({"model_a": a, "model_b": b, "diff_ci95": [low, high], "distinguishable": is_distinct})
return {
"format": fmt,
"num_pairs": len(pairs),
"distinguishable_pairs": distinguishable,
"pairwise_distinguishable_ratio": distinguishable / len(pairs) if pairs else math.nan,
"pairs": details,
}
def all_wrong_rate(per_items: dict[str, dict[str, list[dict[str, Any]]]], fmt: str) -> float:
item_to_scores: dict[str, list[int]] = {}
for fmts in per_items.values():
for row in fmts.get(fmt, []):
item_to_scores.setdefault(row["id"], []).append(row["correct"])
if not item_to_scores:
return math.nan
return sum(1 for vals in item_to_scores.values() if vals and sum(vals) == 0) / len(item_to_scores)
def score_table(summaries: dict[str, dict[str, Any]]) -> list[dict[str, Any]]:
rows = []
for model, fmts in sorted(summaries.items()):
row: dict[str, Any] = {"model": model}
for fmt in ["mcqa4", "mcqa10", "open"]:
if fmt not in fmts:
continue
row[f"{fmt}_accuracy"] = fmts[fmt].get("accuracy")
row[f"{fmt}_ci95"] = fmts[fmt].get("ci95")
if "chance_normalized_accuracy" in fmts[fmt]:
row[f"{fmt}_chance_normalized_accuracy"] = fmts[fmt]["chance_normalized_accuracy"]
if "not_attempted_rate" in fmts[fmt]:
row[f"{fmt}_not_attempted_rate"] = fmts[fmt]["not_attempted_rate"]
rows.append(row)
return rows
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--result-root", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
summaries, per_items = load_results(args.result_root)
table = score_table(summaries)
scores_by_format = {
fmt: {
row["model"]: row[f"{fmt}_accuracy"]
for row in table
if f"{fmt}_accuracy" in row
}
for fmt in ["mcqa4", "mcqa10", "open"]
}
rank_correlations = {
"mcqa4_vs_mcqa10": spearman(scores_by_format["mcqa4"], scores_by_format["mcqa10"]),
"mcqa4_vs_open": spearman(scores_by_format["mcqa4"], scores_by_format["open"]),
"mcqa10_vs_open": spearman(scores_by_format["mcqa10"], scores_by_format["open"]),
}
distinguishability = {
fmt: pairwise_distinguishable_ratio(per_items, fmt)
for fmt in ["mcqa4", "mcqa10", "open"]
}
floor_indicators = {
fmt: {
"all_wrong_item_rate": all_wrong_rate(per_items, fmt),
"pairwise_distinguishable_ratio": distinguishability[fmt]["pairwise_distinguishable_ratio"],
}
for fmt in ["mcqa4", "mcqa10", "open"]
}
result = {
"score_table": table,
"rank_correlations": rank_correlations,
"distinguishability": distinguishability,
"floor_indicators": floor_indicators,
}
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
|