How is AUC-ROC calculated?
AUC-ROC is calculated by sorting the model's scores, sweeping the decision threshold to get a true positive rate and a false positive rate at each step, and integrating the resulting ROC curve, usually with the trapezoidal rule. It also equals the probability that a randomly chosen positive example gets a higher score than a randomly chosen negative one. On TensorTonic you compute AUC-ROC from scratch and integrate the ROC curve with the trapezoidal rule, with tests that check your output.
From Sums to Areas
Integration is the reverse of differentiation. While derivatives tell us the rate of change, integrals accumulate quantities over an interval. In ML, we use integrals to compute areas under curves like the ROC curve.
Riemann Sums
The area under a curve is approximated by summing rectangles. The height of each rectangle is the function value, and width is .
Left Riemann
Use left endpoint of each interval for height. Underestimates for increasing functions.
Right Riemann
Use right endpoint. Overestimates for increasing functions.
Midpoint Rule
Use midpoint. Generally more accurate than left or right.
Interactive Simulator
Adjust the number of rectangles and watch the Riemann sum converge to the true AUC as n increases.
Riemann Sum Simulator
Results
Why Integrate?
A single accuracy number can be misleading. Integration (AUC) summarizes performance across all specific thresholds.
As , the Riemann sum converges exactly to the AUC.
AUC-ROC: The Area Under the ROC Curve
The ROC (Receiver Operating Characteristic) curve plots True Positive Rate vs False Positive Rate at various classification thresholds. The Area Under this Curve (AUC) is a single number summarizing classifier performance.
AUC = 1.0
Perfect classifier. The curve hugs the top left corner.
AUC = 0.5
Random classifier. The curve is the diagonal line. No discrimination power.
Probabilistic Interpretation
AUC = P(classifier ranks a random positive higher than a random negative). If AUC = 0.8, given a random positive and negative sample, the model correctly ranks them 80% of the time.
Case Study: Bulb Defect Classifier
You train a model to detect defective bulbs. It outputs a probability score from 0 to 1. Let's compute the AUC step by step.
Example model outputs:
Step 1: Vary threshold from 0 to 1
At each threshold τ, predict "defective" if score ≥ τ. This gives different TPR/FPR pairs.
Step 2: Compute TPR and FPR at each threshold
True positives / All actual positives
False positives / All actual negatives
Step 3: Plot the ROC curve
Plot (FPR, TPR) for each threshold. Connect points to form curve from (0,0) to (1,1).
Step 4: Compute AUC using trapezoidal rule
For our example: AUC ≈ 0.94 (excellent discrimination)
Numerical Integration Methods
Trapezoidal Rule
Use trapezoids instead of rectangles. Error:
Simpson's Rule
Fit parabolas to three points. Error:
Monte Carlo Integration
Sample random points and average. Scales well to high dimensions unlike grid methods.
ML Applications
AUC-ROC
Threshold independent evaluation for binary classifiers. Used by sklearn.metrics.roc_auc_score.
AUC-PR
Precision Recall AUC. Better for imbalanced datasets where negatives dominate.
Expected Calibration Error
Integrate the gap between predicted probability and actual frequency. Uses binning (discrete integration).
ELBO in VAEs
The Evidence Lower Bound involves integrals over latent variables. Approximated via Monte Carlo sampling.
Frequently asked questions about integration and AUC
What does an AUC of 0.5 mean?
An AUC of 0.5 means the classifier has no discrimination power, and its ROC curve is the diagonal line of a random classifier. A perfect classifier has an AUC of 1.0, with a curve that hugs the top left corner.
When should you use AUC-PR instead of AUC-ROC?
AUC-PR, the area under the precision-recall curve, is the better choice for imbalanced datasets where negatives dominate. AUC-ROC gives a threshold-independent evaluation for binary classifiers in general.
Practice this
On TensorTonic you implement these metrics from scratch and test them in the browser.
- AUC-ROC from Scratch to sweep thresholds and compute the area yourself.
- Area Under the ROC Curve via Trapezoidal Rule to integrate the curve with the method from this lesson.
- Precision-Recall and Average Precision to compute the related metric for imbalanced data.