"""Small, dependency-free statistics for the reporting tools (Python 3.9 stdlib only).
Unit of analysis is the ITEM: samples of one item are averaged first, then items are
resampled / paired. Everything here is deterministic (explicit seeds).
McNemar pairing (documented choice): each lane's per-item pass-rate is reduced to a binary
"majority" outcome (rate > 0.5 pass, rate < 0.5 fail); items with an exact 0.5 tie in either
lane are dropped from the McNemar table (they are neither clearly pass nor fail). With one
sample per item this is the classic per-item McNemar.
"""
import math
import random
def mean(xs):
xs = list(xs)
return sum(xs) / len(xs) if xs else None
def median(xs):
xs = sorted(xs)
n = len(xs)
if not n:
return None
return xs[n // 2] if n % 2 else (xs[n // 2 - 1] + xs[n // 2]) / 2
def percentile(sorted_xs, q):
"""Linear-interpolated percentile of an already sorted list, q in [0,1]."""
n = len(sorted_xs)
if n == 1:
return sorted_xs[0]
pos = q * (n - 1)
lo = int(math.floor(pos))
hi = min(lo + 1, n - 1)
return sorted_xs[lo] + (sorted_xs[hi] - sorted_xs[lo]) * (pos - lo)
def bootstrap_ci(values, n_boot=2000, seed=0, alpha=0.05):
"""Percentile bootstrap CI of the mean over items. Returns (lo, hi) or None if empty."""
values = list(values)
n = len(values)
if n == 0:
return None
if n == 1:
return (values[0], values[0])
rng = random.Random(seed)
means = []
for _ in range(n_boot):
s = 0.0
for _ in range(n):
s += values[rng.randrange(n)]
means.append(s / n)
means.sort()
return (percentile(means, alpha / 2), percentile(means, 1 - alpha / 2))
def wilson(k, n, z=1.959964):
"""Wilson score interval for k successes of n. Returns (lo, hi) or None if n == 0."""
if n <= 0:
return None
p = k / n
z2 = z * z
denom = 1 + z2 / n
centre = (p + z2 / (2 * n)) / denom
half = z * math.sqrt(p * (1 - p) / n + z2 / (4 * n * n)) / denom
return (max(0.0, centre - half), min(1.0, centre + half))
def mcnemar_exact(b, c):
"""Exact two-sided McNemar p-value from the discordant counts b (A pass/B fail) and
c (A fail/B pass): two-sided binomial test of min(b,c) ~ Bin(b+c, 0.5)."""
n = b + c
if n == 0:
return 1.0
k = min(b, c)
tail = sum(math.comb(n, i) for i in range(k + 1)) / (2 ** n)
return min(1.0, 2 * tail)
def paired_items(a, b):
"""Item ids present in both dicts (item_id -> value), sorted."""
return sorted(set(a) & set(b))
def paired_delta(a, b, n_boot=2000, seed=0):
"""Delta (a - b) on the intersection of item_ids graded in both lanes.
a, b: dict item_id -> per-item value (None values are ignored).
Returns dict(n, mean_delta, ci_lo, ci_hi, mean_a, mean_b); n == 0 -> deltas None."""
ids = [i for i in paired_items(a, b) if a[i] is not None and b[i] is not None]
if not ids:
return {"n": 0, "mean_delta": None, "ci_lo": None, "ci_hi": None,
"mean_a": None, "mean_b": None}
d = [a[i] - b[i] for i in ids]
ci = bootstrap_ci(d, n_boot=n_boot, seed=seed)
return {"n": len(ids), "mean_delta": mean(d), "ci_lo": ci[0], "ci_hi": ci[1],
"mean_a": mean(a[i] for i in ids), "mean_b": mean(b[i] for i in ids)}
def mcnemar_items(a, b):
"""McNemar over per-item pass-rates (see module docstring for pairing).
Returns dict(b, c, n_both_pass, n_both_fail, n_tied_dropped, n, p).
b = A pass & B fail, c = A fail & B pass."""
nb = nc = both = neither = tied = 0
for i in paired_items(a, b):
x, y = a[i], b[i]
if x is None or y is None:
continue
if x == 0.5 or y == 0.5:
tied += 1
continue
pa, pb = x > 0.5, y > 0.5
if pa and pb:
both += 1
elif pa:
nb += 1
elif pb:
nc += 1
else:
neither += 1
return {"b": nb, "c": nc, "n_both_pass": both, "n_both_fail": neither,
"n_tied_dropped": tied, "n": nb + nc + both + neither, "p": mcnemar_exact(nb, nc)}