rhscope.metrics¶
Metric primitives used throughout rhscope.
These implementations depend only on numpy and are correctness-matched against
scikit-learn in the test suite. Scikit-learn is an optional dependency, so we
cannot rely on it in core modules - but train_probe and the benchmark
runner both need AUROC, so a lightweight implementation lives here.
- rhscope.metrics.auroc(scores, labels)[source]¶
Compute AUROC via the Mann-Whitney U statistic.
- Parameters:
scores (ndarray | Tensor | list[float] | list[int] | list[bool]) – 1-D sequence of real-valued detector scores.
labels (ndarray | Tensor | list[float] | list[int] | list[bool]) – 1-D sequence of 0/1 labels.
1denotes the positive class (reward hacking); higher scores should correspond to higher likelihood of class 1.
- Returns:
AUROC in
[0, 1]. Returns0.5when every example has the same label (undefined case).- Raises:
ValueError – If inputs are not 1-D or labels are not binary.
- Return type:
- rhscope.metrics.accuracy_at_threshold(scores, labels, threshold)[source]¶
Classification accuracy using
score >= thresholdas the decision rule.The
>=convention matches the ROC-sweep operating point returned byyouden_j_threshold, which picks a threshold at a specific sorted score value - examples at that value should classify as positive, not negative.
- class rhscope.metrics.ThresholdFit(threshold, accuracy, tpr, fpr)[source]¶
Bases:
objectResult of selecting a decision threshold via Youden’s J statistic.
- Variables:
- Parameters:
- rhscope.metrics.youden_j_threshold(scores, labels)[source]¶
Find the threshold maximizing Youden’s J statistic (TPR - FPR).
- Parameters:
- Returns:
ThresholdFitwith the chosen threshold and its operating-point metrics. If either class is empty, returns threshold at the data median with zero TPR/FPR; callers should checkaurocis well-defined separately.- Return type:
- class rhscope.metrics.ConfusionMatrix(tp, fp, tn, fn)[source]¶
Bases:
objectConfusion matrix at a given decision threshold.
- Variables:
- Parameters:
- rhscope.metrics.confusion_matrix(scores, labels, threshold)[source]¶
Build a
ConfusionMatrixfrom scores + labels at a given threshold.The decision rule is
score >= thresholdto matchaccuracy_at_threshold()andyouden_j_threshold().
- rhscope.metrics.precision_recall_curve(scores, labels)[source]¶
Compute the precision-recall curve.
Matches scikit-learn’s
precision_recall_curveconventions:Thresholds are returned in ascending order.
precisionandrecallare aligned with the thresholds; recall consequently decreases from~1(lowest threshold) to the smallest attainable value.A sentinel
(precision=1, recall=0)is appended at the end so the curve closes cleanly at the “nothing is predicted positive” corner.
- Parameters:
- Returns:
(precision, recall, thresholds).precisionandrecallhave lengthlen(thresholds) + 1.- Raises:
ValueError – If inputs are not 1-D or labels are non-binary.
- Return type:
- rhscope.metrics.average_precision(scores, labels)[source]¶
Average precision (PR-AUC) via the step-function sum.
Matches scikit-learn’s
average_precision_score:AP = sum_k (R_{k-1} - R_k) * P_kwhereRis ordered such thatR_{k-1} > R_k(i.e. moving along the curve from high recall toward low recall). Equivalent to-sum(diff(recall) * precision[1:])on the ascending-threshold ordering returned byprecision_recall_curve().- Parameters:
- Returns:
Average precision in
[0, 1]. Returns0.0when there are no positive labels (AP is undefined in that case).- Return type:
- rhscope.metrics.expected_calibration_error(probabilities, labels, *, n_bins=10)[source]¶
Expected Calibration Error with equal-width bins.
- Parameters:
- Returns:
ECE in
[0, 1]. Lower is better.- Raises:
ValueError – If probabilities are outside
[0, 1]or inputs mismatch.- Return type:
- rhscope.metrics.brier_score(probabilities, labels)[source]¶
Brier score: mean squared error between predicted probabilities and labels.
- rhscope.metrics.sigmoid(scores)[source]¶
Numerically stable sigmoid, returning a numpy array.
Useful for converting un-normalized probe scores (DoM dot products, logistic logits) into calibrated-ish probabilities for ECE and Brier computation. Evaluates each branch only on the appropriate half of the input to avoid
overflow encountered in expwarnings on large inputs.