"""Inter-rater agreement and robustness of the main contrasts to the second coder. Usage: python agreement.py [results_scored.jsonl] [results_coder2.jsonl] """ import json, random, statistics, sys from collections import defaultdict DIMS = ["intent_inference", "social_appropriateness", "unsupported_assumptions", "semantic_preservation"] P1 = sys.argv[1] if len(sys.argv) > 1 else "results_scored.jsonl" P2 = sys.argv[2] if len(sys.argv) > 2 else "results_coder2.jsonl" FULL = {"request_refusal", "elder_disagreement", "hosting_pressure", "indirect_no", "respect_register"} c1 = {r["id"]: r for r in map(json.loads, open(P1, encoding="utf-8"))} c2 = {r["id"]: r for r in map(json.loads, open(P2, encoding="utf-8"))} assert set(c1) == set(c2) ids = sorted(c1) def wkappa(a, b, k=3): n = len(a) O = [[0] * k for _ in range(k)] for x, y in zip(a, b): O[x][y] += 1 ra = [sum(r) for r in O] cb = [sum(O[i][j] for i in range(k)) for j in range(k)] w = lambda i, j: (i - j) ** 2 / (k - 1) ** 2 obs = sum(w(i, j) * O[i][j] for i in range(k) for j in range(k)) / n exp = sum(w(i, j) * ra[i] * cb[j] for i in range(k) for j in range(k)) / n**2 return 1 - obs / exp if exp else float("nan") def ci(v, n=10000, seed=42): rng = random.Random(seed) m = sorted(statistics.mean(rng.choice(v) for _ in v) for _ in range(n)) return m[int(.025 * n)], m[int(.975 * n)] print("Agreement, coder 1 vs coder 2, n=48, quadratic-weighted Cohen's kappa") for d in DIMS: a = [c1[i][d] for i in ids]; b = [c2[i][d] for i in ids] dist = {v: b.count(v) for v in (0, 1, 2)} print(f" {d:26s} kappa {wkappa(a, b):.2f} exact {sum(x==y for x,y in zip(a,b))/48:.0%} coder 2 used {dist}") a = [c1[i][d] for i in ids for d in DIMS]; b = [c2[i][d] for i in ids for d in DIMS] print(f" {'pooled':26s} kappa {wkappa(a, b):.2f}") t1 = [sum(c1[i][d] for d in DIMS) for i in ids]; t2 = [sum(c2[i][d] for d in DIMS) for i in ids] print(f" totals: correlation {statistics.correlation(t1, t2):.2f}, mean absolute difference {statistics.mean(abs(x-y) for x,y in zip(t1,t2)):.2f}") def run(label, score): p = defaultdict(dict) for i in ids: p[c1[i]["scenario_id"]][c1[i]["condition"]] = score(i) def rep(name, x, y, keep=None): v = [q[x] - q[y] for s, q in p.items() if keep is None or keep(s)] lo, hi = ci(v) print(f" {name:36s} {statistics.mean(v):+.2f} [{lo:+.2f}, {hi:+.2f}] n={len(v)}") print(f"\n{label}") rep("language, context implicit", "roman_urdu_mixed", "implicit_english") rep("language, context explicit", "explicit_context_added", "explicit_english") rep("context, English", "explicit_english", "implicit_english") rep("context, Roman Urdu", "explicit_context_added", "roman_urdu_mixed") v = [(q["explicit_context_added"] - q["roman_urdu_mixed"]) - (q["explicit_english"] - q["implicit_english"]) for q in p.values()] lo, hi = ci(v) print(f" {'interaction':36s} {statistics.mean(v):+.2f} [{lo:+.2f}, {hi:+.2f}] n=12") rep("moderator: whole situation in RU", "roman_urdu_mixed", "implicit_english", lambda s: s in FULL) rep("moderator: RU quote in EN narration", "roman_urdu_mixed", "implicit_english", lambda s: s not in FULL) tot = lambda c, dims: (lambda i: sum(c[i][d] for d in dims)) run("Coder 1, total out of 8", tot(c1, DIMS)) run("Coder 2, total out of 8", tot(c2, DIMS)) run("Mean of both coders, total out of 8", lambda i: (tot(c1, DIMS)(i) + tot(c2, DIMS)(i)) / 2) D3 = [d for d in DIMS if d != "social_appropriateness"] run("Mean of both coders, excluding social appropriateness, total out of 6", lambda i: (tot(c1, D3)(i) + tot(c2, D3)(i)) / 2)