ROC and AUC
π What is ROC?
πΉ Definition
ROC (Receiver Operating Characteristic) Curve is a graphical tool used to evaluate the performance of a binary classification model across all possible threshold values.
π§ Key Idea
ROC shows how well a model can distinguish between two classes (positive vs negative).
π Axes of ROC Curve
- X-axis → False Positive Rate (FPR)
- Y-axis → True Positive Rate (TPR)
π Important Metrics
πΉ True Positive Rate (TPR) / Recall / Sensitivity
πΉ False Positive Rate (FPR)
π―What Does ROC Curve Represent?
Each point on the ROC curve represents:
- A different threshold (Ο)
- Corresponding TPR and FPR values
πΉ Threshold Concept
- If score ≥ threshold → classify as positive
- If score < threshold → classify as negative
π Varying Threshold
- High threshold → fewer positives → low TPR, low FPR
- Low threshold → more positives → high TPR, high FPR
π How ROC Curve is Constructed
πΉ Step-by-Step
-
Compute prediction scores
- Sort data by score (descending)
-
Start with threshold → all negative
- TPR = 0, FPR = 0 → point (0,0)
-
Gradually decrease threshold:
- Add points to positive class
- Update TP and FP
-
End at threshold
- TPR = 1, FPR = 1 → point (1,1)
πImportant Points on ROC Curve
| Point | Meaning |
|---|---|
| (0,0) | All predicted negative |
| (1,1) | All predicted positive |
| (0,1) | Perfect classifier ✅ |
⭐Ideal Classifier
Best point: (FPR = 0, TPR = 1)
- No false positives
- All positives detected
π Random Classifier
- ROC is a diagonal line
- Points like (0.5, 0.5), (0.9, 0.9)
π Represents random guessing
π Shape of ROC Curve
πΉ Good Model
- Curve bends toward top-left
πΉ Poor Model
- Curve near diagonal
Example Insight
-
Logistic Regression AUC > KNN
π Logistic Regression performs better
Why ROC is Useful
✅ Advantages
- Evaluates model across all thresholds
- Works well with imbalanced datasets
- Compares multiple models easily
π What is AUC?
πΉ Definition
AUC (Area Under the ROC Curve) is a single scalar value that summarizes the performance of a classification model.
It measures how well the model can separate (distinguish) positive and negative classes.
π Mathematical Meaning
- AUC is the area under the ROC curve, which lies between 0 and 1
- It represents the integral of TPR over FPR
π§ Core Interpretation (Most Important)
⭐ Probabilistic Meaning
AUC = Probability that:
A randomly chosen positive instance is ranked higher than a randomly chosen negative instance
✔ Example:
-
AUC = 0.85
π 85% chance model ranks positive higher than negative
πAUC as Measure of Separability
AUC represents the degree of separability
π Meaning
- High AUC → classes are well separated
- Low AUC → classes overlap
π Intuition
Imagine two distributions:
- Positive scores → high values
- Negative scores → low values
π Better model → less overlap → higher AUC
π Range and Interpretation
| AUC Value | Interpretation |
|---|---|
| 1.0 | Perfect classifier |
| 0.9 – 0.99 | Excellent |
| 0.8 – 0.9 | Good |
| 0.7 – 0.8 | Fair |
| 0.5 | Random guessing |
| < 0.5 | Worse than random |
πΉ Important Insight
- AUC = 0.5 → no discrimination (random model)
- AUC = 1 → perfect separation
π Matches your text and standard references
π§ Why AUC is Powerful
✅ 1. Threshold Independent
- Evaluates model across all thresholds (Ο)
- No need to choose a specific cutoff
π This is a major advantage over accuracy
✅ 2. Works for Imbalanced Data
- Unlike accuracy, AUC is not biased by class distribution
✅ 3. Model Comparison
- Higher AUC → better model
- Used to compare classifiers
π Geometric Interpretation
πΉ Area Calculation
-
AUC is computed using:
- Rectangles
- Trapezoids
π Total area = sum of small regions
π― Ideal vs Real Models
πΉ Ideal Classifier
- Point: (0,1)
- AUC = 1
πΉ Random Classifier
- Diagonal line
- AUC = 0.5
πΉ Poor Classifier
- Curve below diagonal
- AUC < 0.5
π Can be improved by flipping predictions
πKey Insight
“A classifier with curve closer to top-left is better”
✔ This directly means:
- Higher TPR
- Lower FPR
- Higher AUC
π§ Intuitive Understanding
π Medical Example
- Positive = disease
- Negative = healthy
π AUC answers:
“How well can the model distinguish diseased vs healthy patients?”
πKey Takeaways
- AUC = area under ROC curve
- Measures classification ability across all thresholds
- Represents probability of correct ranking
- Higher AUC → better separability
- Independent of threshold
- Robust for imbalanced datasets
π― Final Insight
AUC answers the fundamental question:
π “How well does the model rank positive instances higher than negative ones?
π ROC / AUC Algorithm
π§ Overview
The algorithm describes the steps for:
- Plotting the ROC curve
- Computing the Area Under the Curve (AUC)
πΉ Inputs
-
Testing dataset:
-
Classifier:
πΉ Step 1: Compute Scores
For each test point :
- Compute the score for the positive class :
πΉ Step 2: Sort Data
-
Form pairs
- Sort them in decreasing order of scores
πΉ Step 3: Initialize Threshold
- Set threshold:
-
Initially:
- All points are classified as negative
-
,
π Corresponds to point (0,0) in ROC space
πΉ Step 4: Iterate Over Sorted Data
For each distinct score value:
- Set threshold:
- Determine predicted positive set:
πΉ Step 5: Update Counts
For each test point:
-
If (positive class):
- Increment True Positives (TP)
-
Else:
- Increment False Positives (FP)
πΉ Step 6: Compute ROC Point
For each threshold, compute:
- False Positive Rate (FPR)
- True Positive Rate (TPR)
where:
- = number of positive instances
- = number of negative instances
πΉ Step 7: Final Point
-
At the end:
- All points are classified as positive
-
,
π Corresponds to point (1,1) in ROC space
π AUC Computation
πΉ Idea
AUC is computed incrementally as new points are added to the ROC curve.
πΉ Maintain Previous Values
-
Previous values:
πΉ Define Points
πΉ Area Calculation
These four points define:
- A trapezoid if and
- Otherwise, a rectangle (possibly degenerate)
πΉ Trapezoid Area Formula
πΉ Interpretation
- Total AUC = sum of areas of all trapezoids/rectangles
- Represents total area under ROC curve
π― Key Insight
The algorithm constructs the ROC curve point by point and computes AUC as the sum of incremental areas.
π Example
Consider the following sorted scores, along with the true class, for some testing dataset:
- (number of positive instances)
- (number of negative instances)
πΉ Data (Score, Class)
Figure shows the ROC plot, with the shaded region representing the AUC.
We can observe that a trapezoid is obtained whenever there is at least one positive and one negative point with the same score.
The total AUC is 0.833, obtained as the sum of:
- the trapezoidal region on the left (0.333)
- and the rectangular region on the right (0.5)
π Types of Classifiers on ROC Curve
πΉ 1. Random Classifier
- Represented by a diagonal line
- Has no predictive power
- Performs like random guessing
πΉ 2. Perfect Classifier
- Located at the top-left corner (0,1)
- TPR = 1, FPR = 0
- Makes no errors
πΉ 3. Real-World Classifiers
- Fall between random and perfect
- Provide better-than-random predictions
- Goal: stay close to top-left corner
π― Ideal Behavior
A good classifier should:
- Have high True Positive Rate (TPR)
- Have low False Positive Rate (FPR)
- “Hug” the upper-left corner of the ROC plot
π Example Question
Suppose there are three classifiers A, B, and C.
The
-
A:
-
B:
-
C:
π Which classifier can be considered a perfect classifier? Justify your answer.
✅ Answer
✔️ Classifier A (0, 1) is the perfect classifier.
π§ Justification
A perfect classifier has:
- False Positive Rate (FPR) = 0 → No false positives
- True Positive Rate (TPR) = 1 → All positives correctly identified
π Evaluation of Each Classifier
πΉ Classifier A: (0, 1)
- FPR = 0 → No false positives
- TPR = 1 → All positives detected
✅ Perfect classification
πΉ Classifier B: (1, 1)
- FPR = 1 → All negatives misclassified
- TPR = 1 → All positives detected
❌ Not perfect (too many false positives)
πΉ Classifier C: (1, 0.5)
- FPR = 1 → All negatives misclassified
- TPR = 0.5 → Only half positives detected
❌ Poor classifier
π― Final Conclusion
Classifier A is perfect because it achieves:
- Zero false positives
- 100% true positive rate
Example
π AUC Area Calculation
πΉ Step 1: Expression
πΉ Step 2: Simplification
πΉ Step 3: Final Calculation
π― Final Answer
π AUC = 0.8
π Example
Given the following data, construct the ROC curve of the data. Compute the AUC.
πΉ Data Table
| Threshold | TP | TN | FP | FN |
|---|---|---|---|---|
| 1 | 0 | 25 | 0 | 29 |
| 2 | 7 | 25 | 0 | 22 |
| 3 | 18 | 24 | 1 | 11 |
| 4 | 26 | 20 | 5 | 3 |
| 5 | 29 | 11 | 14 | 0 |
| 6 | 29 | 0 | 25 | 0 |
| 7 | 29 | 0 | 25 | 0 |
πΉ Python Code for Plotting ROC and Computing AUC
πΉ Output
π― Final Result
π AUC ≈ 0.92







Comments
Post a Comment