<< Back to posts

Sensitivity + Specificity + PPV + TP/FP/TN/FN Formulas

Formulas for sensitivity, specificity, PPV, NPV, TPR, FPR, prevalence, etc.

Posted on August 2, 2022 • Tags: statistics AI models probability

A brief cheat sheet / reference guide containing the definitions, formulas, and explanations of the most commonly used model evaluation metrics for binary classification tasks.

Quickstart

Name Formula Definition
Sensitivity / Recall / Hit Rate / TPR TPTP+FN\frac{TP}{TP + FN} P(y^=1y=1)P(\hat{y} = 1 \mid y = 1)
Specificity / Selectivity / TNR / 1 - FPR TNTN+FP\frac{TN}{TN + FP} P(y^=0y=0)P(\hat{y} = 0 \mid y = 0)
PPV / Precision TPTP+FP\frac{TP}{TP + FP} P(y=1y^=1)P(y = 1 \mid \hat{y} = 1)
NPV TNTN+FN\frac{TN}{TN + FN} P(y=0y^=0)P(y = 0 \mid \hat{y} = 0)
FDR FPTP+FP\frac{FP}{TP + FP} P(y=0y^=1)P(y = 0 \mid \hat{y} = 1)
FPR FPFP+TN\frac{FP}{FP + TN} P(y^=1y=0)P( \hat{y} = 1 \mid y = 0 )

Note: Prevalence impacts PPV/NPV, but does not impact Sensitivity/Specificity/AUROC.

Background

We have a binary classification model MM.

We have a set of inputs XX, and a set of corresponding labels YY.

The goal of our model is to predict the label for a given input.

In other words, given some input xiXx_i \in X, the model makes a binary prediction yi^\hat{y_i} of 0 or 1. Unknown to our model, the input xix_i has an associated ground truth label yiYy_i \in Y.

Ideally, yi=yi^y_i = \hat{y_i}

Example

Let’s say that we want to predict whether a patient has a disease or not.

We develop a binary classification model for this task.

We say that yi=1y_i = 1 if patient ii has the disease, or yi=0y_i = 0 if patient ii does not have the disease.

We say that yi^=1\hat{y_i} = 1 if the model predicts that patient ii has the disease, otherwise yi^=0\hat{y_i} = 0.

Notation

  • xix_i = input example
  • XX = set of all inputs
  • YY = set of all ground truth labels
  • N=X=YN = \lvert X \rvert = \lvert Y \rvert = total number of inputs
  • yi^0,1\hat{y_i} \in {0,1 } = the prediction of our model MM for a specific xix_i
  • yi0,1y_i \in {0,1} = the ground truth for a specific xix_i (i.e. the value we are trying to predict)
  • I(boolean)0,1\mathbb{I}(boolean) \in {0, 1 } = an indicator function. It evaluates to 1 if the boolean expression inside of it is TRUE; otherwise, it evaluates to 0.
  • y=0y = 0 is referred to as a “negative” true outcome
  • y=1y = 1 is referred to as a “positive” true outcome
  • y^=0\hat{y} = 0 is referred to as a “negative” prediction
  • y^=1\hat{y} = 1 is referred to as a “positive” prediction

Counts (TP / TN / FP / FN)

Name Definition Is prediction correct? Interpretation
True Positives (TP) i=1NI(yi=yi^yi=1)\sum_{i = 1}^N \mathbb{I}(y_i = \hat{y_i} \land y_i = 1) Yes Number of times your model predicted yi^=1\hat{y_i} = 1 when yi=1y_i = 1
True Negatives (TN) i=1NI(yi=yi^yi=0)\sum_{i = 1}^N \mathbb{I}(y_i = \hat{y_i} \land y_i = 0) Yes Number of times your model predicted yi^=0\hat{y_i} = 0 when yi=0y_i = 0
False Positives (FP) i=1NI(yiyi^yi=0)\sum_{i = 1}^N \mathbb{I}(y_i \ne \hat{y_i} \land y_i = 0) No Number of times your model predicted yi^=1\hat{y_i} = 1 when yi=0y_i = 0
False Negatives (FN) i=1NI(yiyi^yi=1)\sum_{i = 1}^N \mathbb{I}(y_i \ne \hat{y_i} \land y_i = 1) No Number of times your model predicted that yi^=0\hat{y_i} = 0 when yi=1y_i = 1

Counts (Ground Truth / Predictions)

Name Definition Formula Interpretation
Ground Truth Positives (PP) i=1NI(yi=1)\sum_{i = 1}^N \mathbb{I}(y_i = 1) TP+FNTP + FN Number of examples in your dataset where yi=1y_i = 1.
Ground Truth Negatives (NN) i=1NI(yi=0)\sum_{i = 1}^N \mathbb{I}(y_i = 0) FP+TNFP + TN Number of examples in your dataset where yi=0y_i = 0
Predicted Positives (P^\hat{P}) i=1NI(yi^=1)\sum_{i = 1}^N \mathbb{I}(\hat{y_i} = 1) TP+FPTP + FP Number of times the model predicted yi^=1\hat{y_i} = 1
Predicted Negatives (N^\hat{N}) i=1NI(yi^=0)\sum_{i = 1}^N \mathbb{I}(\hat{y_i} = 0) TN+FNTN + FN Number of times the model predicted yi^=0\hat{y_i} = 0
Prevalence (pp) 1Ni=1NI(yi=1)\frac{1}{N} \sum_{i = 1}^N \mathbb{I}(y_i = 1) PP+N=TP+FNTP+FN+TN+FP\frac{P}{P + N} = \frac{TP + FN}{TP + FN + TN + FP} Proportion of examples in your dataset where yi=1y_i = 1

Sensitivity / Specificity

Name Definition Formula #1 Formula #2 Formula #3 Interpretation
Sensitivity / Recall / Hit Rate / True Positive Rate (TPR) P(y^=1y=1)P(\hat{y} = 1 \mid y = 1) TPTP+FN\frac{TP}{TP + FN} TPP\frac{TP}{P} 1FNR1 - FNR 5% sensitivity => 5% of positive patients will test positive.
Specificity / Selectivity / True Negative Rate (TNR) P(y^=0y=0)P(\hat{y} = 0 \mid y = 0) TNFP+TN\frac{TN}{FP + TN} TNN\frac{TN}{N} 1FPR1 - FPR 5% specificity => 5% of negative patients will test negative.
False Positive Rate (FPR) P(y^=1y=0)P(\hat{y} = 1 \mid y = 0) FPFP+TN\frac{FP}{FP + TN} FPN\frac{FP}{N} 1TNR1 - TNR 5% FPR => 5% of patients that are negative will test positive

Predictive Values

Name Definition Formula #1 Formula #2 Formula #3 Interpretation
Precision / Positive Predictive Value (PPV) P(y=1y^=1)P(y = 1 \mid \hat{y} = 1) TPTP+FP\frac{TP}{TP + FP} TPP^\frac{TP}{\hat{P}} 1FDR1 - FDR 5% PPV => 5% of patients that test positive are actually positive
Negative Predictive Value (NPV) P(y=0y^=0)P(y = 0 \mid \hat{y} = 0) TNTN+FN\frac{TN}{TN + FN} TNN^\frac{TN}{\hat{N}} 1FOR1 - FOR 5% NPV => 5% of patients that test negative are actually negative
False Discovery Rate (FDR) P(y=0y^=1)P(y = 0 \mid \hat{y} = 1) FPTP+FP\frac{FP}{TP + FP} FPP^\frac{FP}{\hat{P}} 1PPV1 - PPV 5% FDR => 5% of patients that test positive are actually negative

Interpretation

**Sensitivity: **Ability to detect disease if a person has it. (Source)

Specificity: Ability to exclude people without disease. (Source)

PPV: How likely does someone with a positive result have the disease? (Source)

NPV: How likely does someone with a negative result not have the disease? (Source)

When to Use

From Geeky Medics:

Test with high specificity => rule in an outcome when prediction is positive

  • If high specificity, then P(y^=0y=0)1P(\hat{y} = 0 \mid y = 0) \approx 1
  • Thus, getting a result of y^=1\hat{y} = 1 probably means that y=1y = 1
  • Since if y=0y = 0 then we would have gotten y^=0\hat{y} = 0

Test with high sensitivity => rule out an outcome when prediction is negative

  • If high sensitivity, then P(y^=1y=1)1P(\hat{y} = 1 \mid y = 1) \approx 1
  • Thus, getting a result of y^=0\hat{y} = 0 probably means that y=0y = 0
  • Since if y=1y = 1 then we would have gotten y^=1\hat{y} = 1

Prevalence

  • Sensitivity and specificity are not impacted by prevalence
    • They are metrics of the test, no population
  • PPV and NPV are impacted by prevalence
    • Higher prevalence -> Higher PPV
    • At low prevalence -> We expect NPV > PPV

References