Formula: Confusion Matrix & MCC Systems Engineering Formula
- truePositivesTp: True Positives (TP)
- falsePositivesFp: False Positives (FP)
- falseNegativesFn: False Negatives (FN)
- trueNegativesTn: True Negatives (TN)
Evaluate binary classifiers using F1-Score and Matthews Correlation Coefficient (MCC).
On the default counts it is strong: both error types are small compared with the correct predictions in each class. There is no universal pass mark, though. A value of 0.89 for an easy task may be weaker than 0.5 for a hard one with noisy labels. Compare MCC against a sensible baseline model on the same data, not against a fixed number. A simple rule such as always predicting the larger class scores an MCC of 0, which makes it a clear floor to beat.
Plain accuracy divides all correct predictions by all cases, so the larger class dominates. Balanced accuracy averages recall and specificity, giving each class equal weight whatever its size. On the default matrix the page shows them close together, 96% against 94.7%. On the rare-event example with 20 positives they split to 93.79% against 72.17%, and balanced accuracy is the more honest figure.
A J of 0 means recall plus specificity equals 1, so the model separates the classes no better than random guessing. A negative J means it does worse than chance, which usually signals swapped labels. Setting true negatives to 0 on the default matrix gives a J of minus 0.08 on the page, matching the negative MCC the tool reports for that case.
The harmonic mean is pulled toward the smaller value, so a model cannot hide terrible recall behind excellent precision. In the rare-event example precision is 10% and recall 50%. The ordinary average would be 30%, but F1 is 16.67%. A model only earns a high F1 when both precision and recall are high.
Yes. With the default positives and errors but 0 true negatives, precision stays at 90.32% and F1 at 91.3%, yet MCC is minus 0.0863. The model labels every negative case as positive, so its high precision only reflects how common positives are in that matrix. MCC catches this because it uses all four boxes.
Report both if you can, along with the raw confusion matrix. F1 is familiar and fits tasks where the negative class is huge and uninteresting, such as search or entity extraction. MCC is better for comparing models across datasets with different class balance, because it does not depend on which class is labelled positive. The raw counts let readers work out anything else, and the tool's balanced accuracy and J lines make a useful third summary.
Precision would be 0 divided by 0, which has no value. The engine guards the division and reports 0 instead, and it does the same for MCC when one of the marginal totals is zero. Read that 0 as "not measurable" rather than "completely wrong", and avoid averaging it in with properly measured folds without saying so.
Not directly. It takes one two-by-two matrix. For several classes, treat each class in turn as positive and everything else as negative, read off TP, FP, FN and TN for that class, and run the tool once per class. Average the per-class F1 scores for a macro F1, or pool the counts first for a micro F1.
They add to 1. Specificity is TN over TN plus FP, and the false positive rate is FP over the same total. The page shows specificity of 97.09% for the defaults, so the false positive rate is 2.91%. An ROC curve plots recall against that false positive rate, and Youden's J is the vertical distance from the diagonal at each threshold.
The result lines round to two places, so the default shows 0.89, while the summary sentence keeps four places and gives 0.8872. When two models are close, use the four-place value. With the defaults, doubling false positives gives 0.833 and doubling false negatives gives 0.8432, a gap that the two-place display would shrink to a single hundredth.
The four boxes start filled with 420 true positives, 45 false positives, 35 false negatives and 1500 true negatives. Think of a spam filter that got 420 of its spam flags right, wrongly flagged 45 good messages, and let 35 real spam messages through to the inbox.
From those counts the result reports precision of 90.32%, recall of 92.31% and an F1 score of 91.3%. Below those come the Matthews correlation coefficient, shown as 0.89 (the sentence underneath gives it to four places as 0.8872), then specificity of 97.09%, balanced accuracy of 94.7%, Youden's J of 0.89 and plain accuracy of 96%.
Precision answers one question: of everything the model called positive, how much really was positive? That is 420 divided by 420 plus 45. Recall answers a different one: of everything that really was positive, how much did the model catch? That is 420 divided by 420 plus 35. F1 is the harmonic mean of the two, 2 times P times R over P plus R, which comes out at 0.913.
None of precision, recall or F1 ever touches the 1500 true negatives. MCC, specificity, balanced accuracy and J all do. That single difference explains most of what follows on this page, and it is the reason the tool shows those measures beside F1 instead of leaving them out.
The formula is (TP times TN minus FP times FN) divided by the square root of the four marginal totals multiplied together. With the defaults the top line is 420 times 1500 minus 45 times 35. The first product is 630 thousand and the second is a little under 1.6 thousand, so the numerator is 630 thousand minus that small amount.
The four totals are the predicted positives (TP plus FP), the actual positives (TP plus FN), the actual negatives (TN plus FP) and the predicted negatives (TN plus FN). Multiply all four, take the square root, and the denominator comes out about 12.7% larger than the numerator. Divide and you get 0.8872, which matches the page.
There is a second route that makes a nice cross check. MCC also equals the square root of precision times recall times specificity times negative predictive value, minus the square root of the four matching error rates multiplied together. For the defaults that is the square root of 0.9032 times 0.9231 times 0.9709 times 0.9772, minus a very small second term, and it lands on 0.8872 again.
MCC runs from minus 1 to plus 1. A value of 1 is a perfect classifier, 0 is no better than guessing in proportion to the class sizes, and minus 1 means every prediction is the opposite of the truth. Running the tool with 420 true positives, 1500 true negatives and both error boxes at zero gives exactly 1. Entering 50 in all four boxes gives exactly 0, even though precision, recall and F1 all read 50%.
The result panel lists specificity, balanced accuracy and Youden's J as their own lines, so you can read them off rather than working them out. Each is still a one-line formula, which is handy for checking that the counts went into the right boxes.
Specificity, the true negative rate, is TN divided by TN plus FP: 1500 divided by 1500 plus 45, which the page shows as 97.09%. Balanced accuracy is the plain average of recall and specificity: (92.31% plus 97.09%) divided by 2, shown as 94.7%. Youden's J is recall plus specificity minus 1. The page rounds it to 0.89; to three places it is 0.894.
Balanced accuracy and J carry the same information. J is simply 2 times balanced accuracy minus 1, so a balanced accuracy of 50% is J of 0 and means the classifier has no skill. Plain accuracy for the default is 96%, a bit higher than balanced accuracy, because the larger negative class is also the class the model handles best.
J is the measure people use to pick a cut-off on an ROC curve: the threshold with the largest J sits furthest above the diagonal. If you are tuning a threshold, enter the counts for each candidate threshold and compare the J line. The one with the highest J is the balanced choice; if one kind of error costs more, you may choose a different point on purpose. Because the page rounds J to two places, use balanced accuracy, shown to two decimals of a percent, to separate close candidates.
Try 10 true positives, 90 false positives, 10 false negatives and 1500 true negatives. Only 20 of the cases are real positives, and the model flags 100 cases to find half of them.
The page shows plain accuracy of 93.79%, which sounds excellent. The other lines tell a different story: precision 10%, recall 50%, F1 16.67% and MCC 0.2 (0.2035 in the sentence). Specificity is 94.34%, balanced accuracy is 72.17% and Youden's J is 0.44.
Each number describes the same model from a different angle. Accuracy is propped up by the huge negative class. Balanced accuracy says the model is clearly better than chance at telling the classes apart. F1 and precision say that when it raises an alert, nine times out of ten it is wrong. MCC sits between them and is the only single figure here that is low because of both kinds of error.
Which one you report depends on the cost of each error. For a fraud alert sent to a human reviewer, precision of 10% may mean a lot of wasted reviews. For a screening step that only decides who gets a second, cheaper test, catching half the positives might be the more important fact.
Swap the labels on the default matrix: enter 1500 true positives, 35 false positives, 45 false negatives and 420 true negatives. That is the same model, with the negative class now treated as the class of interest.
F1 jumps from 91.3% to 97.4%, precision becomes 97.72% and recall becomes 97.09%. MCC stays at 0.8872. Balanced accuracy and J also stay put at 94.7% and 0.89, because recall and specificity simply trade places: specificity now reads 92.31%.
This is why many people prefer MCC for comparing models across papers or datasets. F1 rewards you for choosing the larger class as positive. MCC, balanced accuracy and J do not care. If you are reporting F1, say which class was positive.
A second test shows the same blind spot from the other side. Set true negatives to 0 and leave the other boxes at their defaults. F1 does not move from 91.3%, because it never uses TN. MCC falls to minus 0.0863, specificity reads 0%, balanced accuracy drops to 46.15% and J to minus 0.08. A model that labels every negative as positive is not a good model, and F1 alone would never tell you.
Strictly, precision is undefined when the model makes no positive predictions, because TP plus FP is zero. Recall is undefined when there are no actual positives. MCC is undefined when any of the four totals is zero.
The engine does not leave these blank. Undefined ratios come out as 0. With 0 true positives, 0 false positives, 35 false negatives and 1500 true negatives, the result is precision 0%, recall 0%, F1 0% and MCC 0, while specificity reads 100%, balanced accuracy 50%, J 0 and accuracy 97.72%. That matches the convention used in common machine learning libraries, which also report 0 for these cases (usually with a warning).
Be careful how you read that 0. A model that predicts nothing positive has not been measured at all on precision; 0% here means "no evidence", not "always wrong". The balanced accuracy of exactly 50% and J of 0 give the fairer verdict: the model has no skill at finding positives. If you are averaging scores over many folds or classes, a fold with no positive predictions can drag a precision average down unfairly. Note how many such folds there were. A matrix with every box at zero is rejected outright, since there is nothing to measure.
A matrix with one false positive and nothing else in the positive column is different. With 0 true positives, 45 false positives, 35 false negatives and 1500 true negatives the tool reports MCC of minus 0.0258. Every denominator is non-zero there, so that small negative value is a real measurement: the model's positive calls are slightly worse than random.
The scenario rows on this page move true positives alone. At 252 true positives F1 is 86.3% and MCC 0.8372; at 630 true positives F1 rises to 94.03% and MCC to 0.9144. More true positives with the same errors improves everything, as expected.
Doubling the false positives to 90, with everything else at default, lowers precision to 82.35% and F1 to 87.05%. Recall does not change at 92.31%. MCC drops to 0.833.
Doubling the false negatives to 70 instead leaves precision at 90.32% but cuts recall to 85.71%, F1 to 87.96% and MCC to 0.8432. Both changes add 35 to 45 errors, and both cost F1 about three to four points, but they hit different halves of the score.
Changing true negatives alone never moves precision, recall or F1. It only moves MCC, specificity, balanced accuracy, J and accuracy. If someone shows you an F1 score, they have told you nothing about how well the model handles the negative class.
The four boxes expect whole counts from one evaluation set, not percentages or rates. If you have rates, multiply them back up by the class sizes first. Mixing counts from a training set and a test set in one matrix gives numbers that describe neither.
Check the orientation of your source. Some tools put actual classes in rows and predictions in columns, others the reverse, and a false positive in one layout sits where a false negative sits in the other. A quick test: TP plus FN should equal the number of real positives in your data.
The boxes accept only whole numbers of zero or more. A negative count or a fraction such as 12.5 produces an error asking for whole counts, so if you have averaged counts across folds, round them or, better, sum the raw counts across folds before entering them.
For a multi-class problem, build one matrix per class by treating that class as positive and all others as negative, then run each through the tool. Average the F1 or MCC values yourself if you need a macro score.