# Educational purpose :Calculation of sensitivity and specificity from confusion matrix for this example

Consider a case where the number of labelled data as 0 = 1400 and labelled as 1 =100. The data labelled as 0 denote normal operating conditions and data labelled as 1 denote abnormal. 0 is no event and 1 is an event.

Assuming the following confusion matrix is obtained for the binary classification in Matlab's confusionmatrix() function using SVM learner

cmMatrix =
predicted 0  predicted 1
truth 0    1100 (TN)       300 (FN)
truth 1    30  (TN)      70 (TP)

cmMatrix = [1100,300;30,70];
acc_0  = 100*(cmMatrix(1,1))/sum(cmMatrix(1,:));
acc_1  = 100*(cmMatrix(2,2))/sum(cmMatrix(2,:));


will give acc_0 = 78.5714 and acc_1 = 70

The confusion matrix is read as out of 1400 normal events, 1100 are correctly identified as normal and 300 are incorrectly identified as abnormal. Then, out of 100 abnormal events, 70 are correctly detected as abnormal whereas 30 are incorrectly detected as abnormal. I want to calculate the sensitivity and specificity with respect t class 1 since that is of primary interest in abnormal event detection. This is how I did

Sensitivity for class 1= TP/(TP+FN) = 70/(70+300) = 0.1892
Specificity for class 0= TN/(TN+FP) = 1100/(1100+30) = 0.9735


where TP with respect to class 1 = 70 FN with respect to class 1 = 300

which means that 18.92% the model will correctly identify abnormal events (with labels 1) and 3% of abnormal events will be incorrectly detected as normal events.

• Sensitivity would refer to the test's ability to correctly detect abnormal events. Is this calculation correct. Did I do any mistake in the calculation?

You're correct. If a classifier is a "shoebox" and does nothing, then for $n=100$ objects (5 of which are 1 and 95 are 0), the accuracy is 95% since everything is classified as zero and only 5/100 are incorrectly classified. However, sens/spec will not be 95%. Have you tried logistic regression or using linear regression with $y=+1$ for class 1 objects and $y=-1$ for objects in class 0 (assign objects to class 1 if predicted $y>0$)?
Let $a$ be the true positives ($TP$), $b$ the false positives ($FP$), $c$ the false negatives ($FN$), and $d$ the true negative ($TN$). Under these definitions, sensitivity is the proportion of diseased subjects who have a positive test, defined as $$sensitivity=\frac{a}{a+c}=\frac{TP}{TP+FN}.$$ On the other hand, the specificity is the proportion of disease-free subjects with a negative test $$specificity=\frac{d}{d+b}=\frac{TN}{TN+FP}.$$