Download src/evaluate.py from zaaabik/paper_extraction: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/datasets/zaaabik/paper_extraction/resolve/main/src/evaluate.py
- Command line
-
hf download hf://datasets/zaaabik/paper_extraction/src/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/zaaabik/paper_extraction/resolve/main/src/evaluate.py
3.42 kB
| """Evaluation metrics for error prediction. | |
| We report: | |
| - ROC-AUC of wrongness score vs binary `correct/incorrect` label. | |
| - PR-AUC (positive class = wrong). | |
| - AURC (area under risk-coverage). | |
| - Accuracy-Rejection Curve area *above baseline* (the paper's headline metric). | |
| - ECE of the underlying classifier (constant across feature variants, included for context). | |
| """ | |
| from __future__ import annotations | |
| from typing import Dict | |
| import numpy as np | |
| from sklearn.metrics import average_precision_score, roc_auc_score | |
| def accuracy_rejection_curve( | |
| wrongness_scores: np.ndarray, | |
| y_true: np.ndarray, | |
| y_pred: np.ndarray, | |
| step: int = 1, | |
| ) -> Dict[str, np.ndarray]: | |
| """ARC à la Nadeem et al. 2009 — sort by ascending confidence (= descending wrongness), | |
| drop the least confident first, recompute accuracy on the remaining set. | |
| """ | |
| N = len(wrongness_scores) | |
| order = np.argsort(-wrongness_scores) # most-likely-wrong first | |
| r_rates, accs = [0.0], [float((y_pred == y_true).mean())] | |
| for i in range(step, N, step): | |
| keep = order[i:] | |
| if len(keep) == 0: | |
| break | |
| r_rates.append(i / N) | |
| accs.append(float((y_pred[keep] == y_true[keep]).mean())) | |
| return {"rejection_rate": np.asarray(r_rates), "accuracy": np.asarray(accs)} | |
| def arc_auc_above_baseline(arc: Dict[str, np.ndarray]) -> float: | |
| """Trapezoid-integrate (accuracy - baseline_accuracy) wrt rejection_rate.""" | |
| r = arc["rejection_rate"] | |
| a = arc["accuracy"] | |
| baseline = a[0] | |
| return float(np.trapz(np.maximum(a - baseline, 0), r)) | |
| def aurc(wrongness_scores: np.ndarray, y_true: np.ndarray, y_pred: np.ndarray) -> float: | |
| N = len(wrongness_scores) | |
| order = np.argsort(wrongness_scores) # ascending wrongness → most confident first | |
| cum_errors = np.cumsum((y_pred[order] != y_true[order]).astype(np.float64)) | |
| coverage = (np.arange(1, N + 1)) / N | |
| risk = cum_errors / np.arange(1, N + 1) | |
| return float(np.trapz(risk, coverage)) | |
| def expected_calibration_error(probs_max: np.ndarray, correct: np.ndarray, n_bins: int = 15) -> float: | |
| bins = np.linspace(0.0, 1.0, n_bins + 1) | |
| ece = 0.0 | |
| N = len(probs_max) | |
| for i in range(n_bins): | |
| lo, hi = bins[i], bins[i + 1] | |
| mask = (probs_max > lo) & (probs_max <= hi) | |
| if mask.sum() == 0: | |
| continue | |
| acc = correct[mask].mean() | |
| conf = probs_max[mask].mean() | |
| ece += (mask.sum() / N) * abs(acc - conf) | |
| return float(ece) | |
| def evaluate_all( | |
| wrongness_scores: np.ndarray, | |
| y_true: np.ndarray, | |
| y_pred: np.ndarray, | |
| probs_max: np.ndarray, | |
| ) -> Dict[str, float]: | |
| wrong = (y_pred != y_true).astype(int) | |
| metrics: Dict[str, float] = {} | |
| if len(np.unique(wrong)) > 1: | |
| metrics["roc_auc_wrong"] = float(roc_auc_score(wrong, wrongness_scores)) | |
| metrics["pr_auc_wrong"] = float(average_precision_score(wrong, wrongness_scores)) | |
| else: | |
| metrics["roc_auc_wrong"] = float("nan") | |
| metrics["pr_auc_wrong"] = float("nan") | |
| arc = accuracy_rejection_curve(wrongness_scores, y_true, y_pred, step=1) | |
| metrics["arc_auc_above_baseline"] = arc_auc_above_baseline(arc) | |
| metrics["aurc"] = aurc(wrongness_scores, y_true, y_pred) | |
| metrics["ece"] = expected_calibration_error(probs_max, (y_true == y_pred).astype(float)) | |
| metrics["base_accuracy"] = float((y_pred == y_true).mean()) | |
| return metrics | |