I wanted to provide a more complete answer to address both F and MCC, and to provide a well reasoned alternative definition to avoid undefined-ness in both cases.
When $F_\beta$ is undefined
In one sense, the F-measure is truly undefined when precision or recall are 0, which happens when there are zero true positives:
F-measure (originally introduced in MUC-4, 1992):
$$
F_\beta = \frac{(\beta^2 + 1.0) \times P \times R}{\beta^2 \times P + R}
$$
where $P = \frac{TP}{TP + FP}$ and $R = \frac{TP}{TP + FN}$. Here, when $TP = 0$, precision and recall are 0, and $F_\beta = \frac{0}{0}$, which is undefined.
However, as far as $F_1$, when precision and recall are given equal weight ($\beta = 1$), you can rewrite without computing precision and recall, and avoid some undefined bounds:
$$
F_1 = \frac{2 \times TP}{2 \times TP + FP + FN}
$$
In this case, $F_1 = 0$ when $TP = 0$ AND $(FP \neq 0$ OR $FN \neq 0)$.
BUT, $F_1$ is still undefined when TP, FP, and FN are all 0!
Handling undefined-ness
Some say those are cases when $F_1$ is an unnecessary test. Perhaps so, if you're just running it once. I have found the following alternative useful when computing many $F_1$ scores on models that may include undefined cases, and I want to compare them all fairly.
Thanks to https://stats.stackexchange.com/a/305905/240083, I found https://github.com/dice-group/gerbil/wiki/Precision,-Recall-and-F1-measure, which provides a nice solution that avoids the divide by 0 issue when TP, FP, and FN are all 0:
In some rare cases, the calculation of Precision or Recall can cause a division by 0. Regarding the precision, this can happen if there are no results inside the answer of an annotator and, thus, the true as well as the false positives are 0. For these special cases, we have defined that if the true positives, false positives and false negatives are all 0, the precision, recall and F1-measure are 1. This might occur in cases in which the gold standard contains a document without any annotations and the annotator (correctly) returns no annotations. If true positives are 0 and one of the two other counters is larger than 0, the precision, recall and F1-measure are 0.
I think this applied well to any binary classification problem. For example, if the predicted classes are all 0, and the true classes are indeed all 0, then $F$ should capture a perfect classification.
MCC's undefined-ness
This idea extends well to MCC too, albeit more elaborately. MCC ranges from $[-1, 1]$, where 1 is perfect classification, 0 is arbitrarily guessing, and -1 is a perfectly incorrect classification.
$$
MCC = \frac{(TP \times TN - FP \times FN)}{\sqrt{(TP+FP)(TP+FN)(TN+FP)(TN+FN)}}
$$
MCC is undefined in four cases:
- if $TP = 0$ AND $FP = 0$, then $MCC = \frac{0}{0}$ = undefined
- if $TP = 0$ AND $FN = 0$, then $MCC = \frac{0}{0}$ = undefined
- if $TN = 0$ AND $FP = 0$, then $MCC = \frac{0}{0}$ = undefined
- if $TN = 0$ AND $FN = 0$, then $MCC = \frac{0}{0}$ = undefined
A proposed alternative; for each of the four cases above, there are three valid subcases to consider (when MCC = 1, 0, or -1):
- if $TP = 0$ AND $FP = 0$, then the classifier predicted all negatives, and we need to check $FN$ and $TN$:
- if $FN = 0$ AND $TN \neq 0$, then the predicted and true classes are all negative, and the classifier is perfectly correct, so $MCC = 1$.
- if $FN \neq 0$ AND $TN \neq 0$, then the true classes are mixed and the classifier is arbitrarily wrong, so $MCC = 0$.
- if $FN \neq 0$ AND $TN = 0$, then the true classes are all positive, so the classifier is perfectly wrong and $MCC = -1$.
- if $TP = 0$ AND $FN = 0$, then the true classes are all negative, so we need to check both $FP$ and $TN$:
- if $FP = 0$ AND $TN \neq 0$, then the classifier is guessing all negatives, so the classifier is perfectly correct and $MCC = 1$.
- if $FP \neq 0$ AND $TN \neq 0$, then the guesses are mixed and the classifier is arbitrarily wrong, so $MCC = 0$.
- if $FP \neq 0$ AND $TN = 0$, then the classifier is always guessing positive, so the classifier is always wrong, so $MCC = -1$.
- if $TN = 0$ AND $FP = 0$, then the true classes are all positive and we need to check $TP$ and $FN$:
- if $TP \neq 0$ AND $FN = 0$, then the classifier is guessing all positives, so $MCC = 1$.
- if $TP \neq 0$ AND $FN \neq 0$, then the guesses are mixed and the classifier is arbitrarily guessing and $MCC = 0$.
- if $TP = 0$ AND $FN \neq 0$, then the classifier is only guessing negatives and it is perfectly wrong, so $MCC = -1$.
- if $TN = 0$ AND $FN = 0$, then the classifier is always guessing positives, so we need to check $TP$ and $FP$:
- if $TP \neq 0$ AND $FP = 0$, then the true classes are also all positive and $MCC = 1$
- if $TP \neq 0$ AND $FP \neq 0$, then the true classes are mixed and the classifier is arbitrarily wrong, so $MCC = 0$.
- if $TP = 0$ AND $FP \neq 0$, then the true classes are all negative and the classifier is perfectly wrong, so $MCC = -1$.