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

TPR=TPTP+FNTPR = \frac{TP}{TP + FN}


πŸ”Ή False Positive Rate (FPR)

FPR=FPFP+TNFPR = \frac{FP}{FP + TN}


🎯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

  1. Compute prediction scores S(xi)S(x_i)
  2. Sort data by score (descending)
  3. Start with threshold ρ=\rho = \infty → all negative
    • TPR = 0, FPR = 0 → point (0,0)
  4. Gradually decrease threshold:
    • Add points to positive class
    • Update TP and FP
  5. End at threshold ρ=∞→ all positive
    • TPR = 1, FPR = 1 → point (1,1)

πŸ“Important Points on ROC Curve



PointMeaning
(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


It is evident from the plot that the AUC for the Logistic Regression ROC curve is higher than that for the KNN ROC curve. Therefore, we can say that logistic regression did a better job of classifying the positive class in the dataset.

πŸ“Š 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

The area under the ROC curve, abbreviated AUC, can be used as a measure of classifier performance. Because the total area of the plot is 1, the AUC lies in the interval [0,1] – the higher the better. The AUC value is essentially the probability that the classifier will rank a random positive test case higher than a random negative test instance.




🧠 Overview

The algorithm describes the steps for:

  • Plotting the ROC curve
  • Computing the Area Under the Curve (AUC)

πŸ”Ή Inputs

  • Testing dataset: DD
  • Classifier: MM

πŸ”Ή Step 1: Compute Scores

For each test point xiDx_i \in D:

  • Compute the score for the positive class (c1)(c_1):
S(xi)S(x_i)

πŸ”Ή Step 2: Sort Data

  • Form pairs (S(xi),yi)(S(x_i), y_i)
  • Sort them in decreasing order of scores

πŸ”Ή Step 3: Initialize Threshold

  • Set threshold:
ρ=\rho = \infty
  • Initially:
    • All points are classified as negative
    • TP=0TP = 0, FP=0FP = 0

πŸ‘‰ Corresponds to point (0,0) in ROC space


πŸ”Ή Step 4: Iterate Over Sorted Data

For each distinct score value:

  • Set threshold:
ρ=S(xi)\rho = S(x_i)
  • Determine predicted positive set:
R1(ρ)={xiD:S(xi)>ρ}R_1(\rho) = \{ x_i \in D : S(x_i) > \rho \}

πŸ”Ή Step 5: Update Counts

For each test point:

  • If yi=c1y_i = c_1(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)
(FPR,TPR)=(FPn2,TPn1)(FPR, TPR) = \left( \frac{FP}{n_2}, \frac{TP}{n_1} \right)

where:

  • n1n_1 = number of positive instances
  • n2n_2 = number of negative instances

πŸ”Ή Step 7: Final Point

  • At the end:
    • All points are classified as positive
    • FN=0FN = 0, TN=0TN = 0

πŸ‘‰ 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:
    • FPprevFP_{prev}
    • TPprevTP_{prev}

πŸ”Ή Define Points

(x1,y1)=(FPprevn2,TPprevn1)(x_1, y_1) = \left( \frac{FP_{prev}}{n_2}, \frac{TP_{prev}}{n_1} \right)
(x2,y2)=(FPn2,TPn1)(x_2, y_2) = \left( \frac{FP}{n_2}, \frac{TP}{n_1} \right)
(x1,0)=(FPprevn2,0)(x_1, 0) = \left( \frac{FP_{prev}}{n_2}, 0 \right)
(x2,0)=(FPn2,0)(x_2, 0) = \left( \frac{FP}{n_2}, 0 \right)

πŸ”Ή Area Calculation

These four points define:

  • A trapezoid if x2>x1x_2 > x_1 and y2>y1y_2 > y_1
  • Otherwise, a rectangle (possibly degenerate)

πŸ”Ή Trapezoid Area Formula

Area=bh,b=x2x1,h=12(y2+y1)\text{Area} = b \cdot h, \quad b = |x_2 - x_1|, \quad h = \frac{1}{2}(y_2 + y_1)


πŸ”Ή 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:

  • n=5n = 5
  • n1=3n_1 = 3 (number of positive instances)
  • n2=2n_2 = 2 (number of negative instances)

πŸ”Ή Data (Score, Class)

(0.9,c1), (0.8,c2), (0.8,c1), (0.8,c1), (0.1,c2)(0.9, c_1),\ (0.8, c_2),\ (0.8, c_1),\ (0.8, c_1),\ (0.1, c_2)

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 (FPR,TPR) values for the classifiers are:

  • A: (0,1)(0, 1)
  • B: (1,1)(1, 1)
  • C: (1,0.5)(1, 0.5)

πŸ‘‰ 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

Area=(0.40)12(0.6+0.6)+(0.60.4)12(0.8+0.8)+(1.00.6)12(1+1)\text{Area} = (0.4 - 0) \cdot \frac{1}{2}(0.6 + 0.6) + (0.6 - 0.4) \cdot \frac{1}{2}(0.8 + 0.8) + (1.0 - 0.6) \cdot \frac{1}{2}(1 + 1)

πŸ”Ή Step 2: Simplification

Area=0.40.6+0.20.8+0.41\text{Area} = 0.4 \cdot 0.6 + 0.2 \cdot 0.8 + 0.4 \cdot 1


πŸ”Ή Step 3: Final Calculation

Area=0.24+0.16+0.4=0.8\text{Area} = 0.24 + 0.16 + 0.4 = 0.8


🎯 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

import matplotlib.pyplot as plt import numpy as np TP = np.array([0,7,18,26,29,29,29]) TN = np.array([25,25,24,20,11,0,0]) FP = np.array([0,0,1,5,14,25,25]) FN = np.array([29,22,11,3,0,0,0]) TPR = TP/(TP+FN) FPR = FP/(FP+TN) plt.plot(FPR,TPR) plt.scatter(FPR,TPR) x = zip(FPR,TPR) for i in x: print(i) area = 0 for i in range(6): area = area + (FPR[i+1]-FPR[i])*((TPR[i]+TPR[i+1])/2) print("Area under the curve=", area)

πŸ”Ή Output

(0.0, 0.0) (0.0, 0.2413793103448276) (0.04, 0.6206896551724138) (0.2, 0.896551724137931) (0.56, 1.0) (1.0, 1.0) (1.0, 1.0) Area under the curve = 0.919999



🎯 Final Result

πŸ‘‰ AUC ≈ 0.92

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming