Lightning-AI / torchmetrics

Torchmetrics - Machine learning metrics for distributed, scalable PyTorch applications.

Home Page:https://lightning.ai/docs/torchmetrics/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Specificity if TN + FN = 0

lukazso opened this issue · comments

The specificity is defined as $\text{Specificity} = \frac{\text{TN}}{\text{TN} + \text{FP}}$. As far as I see, in case $\text{FP} = 0$, the specificity should be 1.

The docs say that in case $\text{FP} + \text{TP} = 0$, the metric is not defined (see reference below). To put this into words, this means that if the dataset does not contain any positive samples, the metric is not defined and it will return 0.

Is this really intended? It might be a weird edge case. But from the metric definition, I see no argument why the metric should not be defined in case there are no positive samples in the data. In case $\text{FP} + \text{TP} = 0$, I think the metric should return 1, not 0.

torchmetrics version: 1.3.2

class Specificity(_ClassificationTaskWrapper):
r"""Compute `Specificity`_.
.. math:: \text{Specificity} = \frac{\text{TN}}{\text{TN} + \text{FP}}
Where :math:`\text{TN}` and :math:`\text{FP}` represent the number of true negatives and false positives
respectively. The metric is only proper defined when :math:`\text{TP} + \text{FP} \neq 0`. If this case is
encountered for any class/label, the metric for that class/label will be set to 0 and the overall metric may
therefore be affected in turn.

I just saw that this might be just a documentation issue. The classes BinarySpecificity, MulticlassSpecificity, and Multilabelspecificity all have the correct description:

class BinarySpecificity(BinaryStatScores):
r"""Compute `Specificity`_ for binary tasks.
.. math:: \text{Specificity} = \frac{\text{TN}}{\text{TN} + \text{FP}}
Where :math:`\text{TN}` and :math:`\text{FP}` represent the number of true negatives and false positives
respectively. The metric is only proper defined when :math:`\text{TN} + \text{FP} \neq 0`. If this case is
encountered a score of 0 is returned.

Also, the implementation seems to follow this definition.