Build a binary ROC curve without splitting tied scores, then place AUC beside the operating point and cohort counts.
ROC curves: read attainable thresholds and false-positive rates
Define both error denominators
A receiver operating characteristic curve plots true-positive rate against false-positive rate as a score threshold moves. True-positive rate divides captured events by all actual events. False-positive rate divides false alerts by all actual non-events. The latter is not the fraction of alerts that are false. State which label is positive, when the score was produced and when outcomes became final. A hospital or factory may need a low false-positive rate because follow-up capacity is limited, but the curve alone cannot decide the cost of an alert. Precision–recall curves put false alerts in the denominator of precision and often make alert workload easier to see. A confusion matrix makes the counts behind one selected threshold explicit.
Sweep score ties as a single decision
Sort evaluation records by descending score. Start at the no-alert point, false-positive rate zero and true-positive rate zero. Add every record at a distinct score together; equal scores cannot be separated by a threshold that uses the score alone. After each group, divide cumulative true positives by the total positives and cumulative false positives by total negatives. The last point is one, one when every eligible record has a finite score. Retain the actual staircase vertices. A plotting package may remove intermediate collinear points for display, but the operational threshold table must keep enough detail to identify each attainable selection. A score need not be a calibrated probability to produce an ROC curve. Calibration plots test that separate question.
Keep AUC in its place
Trapezoidal ROC AUC summarizes ranking over the full false-positive-rate range. It can be interpreted as the probability that a randomly selected positive outranks a randomly selected negative, with half credit for a tie, under the evaluation population. A high overall AUC may still conceal weak performance in the low-false-positive region where the system can operate. If only that range matters, specify its bounds and normalization before reporting a partial AUC, and show the actual portion of the curve. Compare models on the same held-out records and labels. The ROC axes use rates that do not algebraically contain prevalence, yet precision and the number of false alarms at deployment do depend on the population mix. Print counts and the intended threshold beside AUC.
Test slices and uncertain tails
A curve built from 47 positives can jump by more than two percentage points with each newly captured positive. Likewise, one false alert has a large horizontal effect when few negatives were evaluated. Publish total positives, negatives and score coverage; use uncertainty intervals or unit-level resampling when records are clustered by customer, machine or site. Avoid comparing curves whose outcome windows or eligibility filters differ. Uncertainty displays need to reveal thin support, while shared axes matter when models or sites appear in separate panels. If labels are delayed, do not quietly count pending cases as negatives. A companion table should include threshold, selected count and all four confusion-matrix cells at the chosen operating points.
Worked contract
from math import isfinite
def roc_thresholds(records: list[tuple[str, float, bool]]):
identifiers = [record_id for record_id, _, _ in records]
if len(identifiers) != len(set(identifiers)) or any(not key for key in identifiers):
raise ValueError("record IDs must be unique and nonempty")
if any(not isfinite(score) or type(event) is not bool
for _, score, event in records):
raise ValueError("scores and labels must be valid")
positives = sum(event for _, _, event in records)
negatives = len(records) - positives
if not positives or not negatives:
raise ValueError("ROC needs both positive and negative records")
ranked = sorted(records, key=lambda row: row[1], reverse=True)
points = [(None, 0.0, 0.0, 0)]
true_positives = false_positives = cursor = 0
while cursor < len(ranked):
threshold = ranked[cursor][1]
while cursor < len(ranked) and ranked[cursor][1] == threshold:
true_positives += ranked[cursor][2]
false_positives += not ranked[cursor][2]
cursor += 1
points.append((threshold, false_positives / negatives,
true_positives / positives, cursor))
area = sum((points[index][1] - points[index - 1][1]) *
(points[index][2] + points[index - 1][2]) / 2
for index in range(1, len(points)))
return points, area
quality_scores = [("lot-47", 0.91, True), ("lot-59", 0.64, False),
("lot-83", 0.64, True), ("lot-97", 0.18, False)]
points, area = roc_thresholds(quality_scores)
assert points[0] == (None, 0.0, 0.0, 0)
assert points[2] == (0.64, 0.5, 1.0, 3)
assert points[-1][1:3] == (1.0, 1.0)
assert area == 0.875Performance and operating cost
For N labeled records, sorting takes O(N log N) time, and the grouped threshold sweep plus trapezoid sum take O(N). The sorted records, uniqueness set and output points need O(N) space. This four-record audit proves a tied score creates one shared threshold point and yields area 0.875. It does not estimate uncertainty, calibrate scores or determine a safe operating threshold. Those need an evaluation design and a stated decision cost.
Common Mistakes
- Calling false positives divided by all alerts the false-positive rate.
- Breaking score ties in input order and drawing unattainable thresholds.
- Choosing a model by full AUC when only a narrow false-positive range is usable.
- Comparing AUCs from different label windows or evaluation cohorts.
Validation drill
Score 470 held-out production lots with confirmed outcomes. Mark every tied threshold, report the four confusion-matrix counts at the 47-alert operating point, and inspect the first 0.05 of false-positive rate separately.
Read next
- Precision–recall curves: preserve threshold ties and class prevalence
- Confusion matrices: fix class order, denominators and abstentions
- Probability calibration plots: compare predicted risk with observed frequency
- Uncertainty on charts: show denominators and intervals beside estimates
