from collections import defaultdict def reliability_table(results: list[tuple[float, bool]], bins: int = 5) -> None: """results: (confidence, was_correct) pairs from a labelled eval set.""" buckets: dict[int, list[bool]] = defaultdict(list) for conf, correct in results: buckets[min(int(conf * bins), bins - 1)].append(correct) print(f"{'bucket':<12}{'n':>6}{'accuracy':>10}") for b in range(bins): hits = buckets.get(b, []) lo, hi = b / bins, (b + 1) / bins acc = f"{sum(hits) / len(hits):.2f}" if hits else "-" print(f"{lo:.1f}-{hi:.1f}".ljust(12) + f"{len(hits):>6}{acc:>10}") def coverage_curve(results: list[tuple[float, bool]], thresholds=(0.5, 0.7, 0.8, 0.9, 0.95)) -> None: """For each threshold: share of items automated, and accuracy on that share.""" total = len(results) print(f"{'threshold':<11}{'coverage':>10}{'sel. accuracy':>15}") for t in thresholds: kept = [ok for conf, ok in results if conf >= t] cov = len(kept) / total acc = sum(kept) / len(kept) if kept else float("nan") print(f"{t:<11}{cov:>10.1%}{acc:>15.1%}")