Matthews Correlation Coefficient Calculator (MCC)

This free Matthews correlation coefficient calculator takes the four cells of a binary confusion matrix (true positives, true negatives, false positives and false negatives) and returns the MCC, a single score from −1 to +1. It also shows accuracy, precision, recall and F1 next to it, so you can see why MCC is the harder number to fool when one class is rare.

What is the Matthews correlation coefficient?

The Matthews correlation coefficient (MCC) measures how well a classifier’s yes/no predictions agree with the true yes/no labels. It uses all four counts from the confusion matrix:

\[ \mathrm{MCC} = \frac{TP \times TN – FP \times FN}{\sqrt{(TP+FP)(TP+FN)(TN+FP)(TN+FN)}} \]

The numerator rewards correct predictions on both classes and penalises both kinds of error. The denominator scales the result so it always lands between −1 and +1:

  • \( \mathrm{MCC} = +1 \): every prediction is correct.
  • \( \mathrm{MCC} = 0 \): the predictions are no better than random guessing, or the model predicts one class for everything.
  • \( \mathrm{MCC} = -1 \): every prediction is wrong (the model has the labels flipped).

The measure is named after the biochemist Brian Matthews, who used it in 1975 to compare predicted and observed protein secondary structure. Mathematically it is the phi coefficient: the Pearson correlation between two binary variables, the true label and the predicted label. If you have met correlation through a correlation matrix, MCC is the same idea applied to 0s and 1s.

matthews correlation coefficient calculator chart

Matthews correlation coefficient calculator

Enter the four counts from your confusion matrix into the Matthews correlation coefficient calculator and press the button. The defaults are the worked example below.

MCC = —
Working appears here.
Accuracy—
Precision—
Recall—
F1 score—
Specificity—
Total samples—
Actual positives—
Actual negatives—
Example loaded. Press Calculate MCC.

Worked example: a spam filter on 1,000 emails

A spam filter is tested on 1,000 emails, of which 110 are spam. It produces this confusion matrix:

Predicted spamPredicted not spam
Actually spamTP = 90FN = 20
Actually not spamFP = 40TN = 850

Step 1, numerator: \( 90 \times 850 – 40 \times 20 = 76{,}500 – 800 = 75{,}700 \).

Step 2, the four sums: \( TP+FP = 130 \), \( TP+FN = 110 \), \( TN+FP = 890 \), \( TN+FN = 870 \).

Step 3, denominator: \( \sqrt{130 \times 110 \times 890 \times 870} = \sqrt{11{,}072{,}490{,}000} \approx 105{,}225.9 \).

Step 4, divide:

\[ \mathrm{MCC} = \frac{75{,}700}{105{,}225.9} \approx 0.7194 \]

For the same matrix, accuracy is \( 940 / 1000 = 0.94 \) and the F1 score is \( 180 / 240 = 0.75 \). Accuracy looks excellent mainly because 89% of emails are not spam. MCC sits lower because it also counts the 40 good emails wrongly sent to the spam folder against the 850 handled correctly.

The accuracy trap. Now take a data set with 95 positives and 5 negatives, and a lazy model that predicts “positive” every time: TP = 95, FP = 5, TN = 0, FN = 0. Accuracy is 0.95 and F1 is \( 190/195 \approx 0.9744 \), yet the model has learned nothing. The sum \( TN + FN \) is 0, so the denominator is 0 and the Matthews correlation coefficient calculator reports 0: no better than chance. Press “Try the accuracy trap” above to see it.

When to use MCC and when not to

Use MCC when:

  • The classes are unbalanced, as in fraud, disease screening or defect detection. See imbalanced datasets for why plain accuracy misleads there.
  • You care about both classes. MCC is high only if the model does well on positives and negatives.
  • You want one number to compare models or folds in cross-validation, without choosing which class is “positive”. Swapping the class labels leaves MCC unchanged.

Look elsewhere when:

  • The two errors have very different costs. MCC weighs a false positive and a false negative symmetrically. If missing a cancer is far worse than a false alarm, report precision and recall separately, or optimise a cost directly.
  • You want to judge the model’s scores, not one threshold. MCC needs hard 0/1 predictions. To evaluate ranking across every threshold, use the ROC curve and AUC.
  • Negatives are huge and not meaningful, as in some information-retrieval tasks where TN is close to unknowable. F1 ignores TN, which is the point there.

MCC vs F1 score

F1 is the nearest alternative, and both are often recommended for imbalanced problems. The key difference is that F1 never looks at true negatives.

Matthews correlation coefficientF1 score
Formula\( \frac{TP \cdot TN – FP \cdot FN}{\sqrt{(TP+FP)(TP+FN)(TN+FP)(TN+FN)}} \)\( \frac{2\,TP}{2\,TP + FP + FN} \)
Range−1 to +1 (0 = chance)0 to 1 (no fixed chance level)
Uses TN?YesNo
Swap positive and negative classSame valueValue changes
Accuracy trap above00.9744
Worked example above0.71940.75
Best forOverall quality on both classesFocus on one class of interest

Chicco and Jurman (BMC Genomics, 2020) argue for MCC over both F1 and accuracy in binary classification for exactly these reasons. A broader overview of the options is in classification metrics.

Common mistakes

  • Mixing up FP and FN. A false positive is a negative case predicted positive. Swapping FP and FN leaves the numerator the same but changes the denominator, so the MCC changes.
  • Reading MCC as a percentage. An MCC of 0.72 does not mean 72% of predictions are right. It is a correlation.
  • Treating 0 as the worst score. The worst is −1. A clearly negative MCC usually means the labels or the predicted classes are inverted somewhere in the pipeline.
  • Ignoring the 0/0 case. If any of the four sums is 0, the formula divides by zero. scikit-learn returns 0 here, and so does this Matthews correlation coefficient calculator; some write-ups call it undefined. Say which convention you used.
  • Feeding in probabilities. MCC works on predicted classes. Threshold the probabilities first, for example the output of a logistic regression, and remember that MCC changes as you move the threshold.

MCC in Python

scikit-learn has it built in as sklearn.metrics.matthews_corrcoef:

from sklearn.metrics import matthews_corrcoef

y_true = [1, 1, 0, 0, 1, 0]
y_pred = [1, 0, 0, 0, 1, 1]
print(matthews_corrcoef(y_true, y_pred))

Here TP = 2, TN = 2, FP = 1 and FN = 1, so the result is \( (4 – 1)/\sqrt{3 \times 3 \times 3 \times 3} = 3/9 \approx 0.3333 \), the same value the Matthews correlation coefficient calculator above gives for those four counts. The function also accepts more than two classes, using a multiclass generalisation of the same formula.

Frequently asked questions

What is a good MCC score?

There is no universal cut-off. Above 0 means better than chance and 1 is perfect. Compare MCC against a simple baseline model on the same data rather than against a fixed number.

Is MCC the same as the phi coefficient?

Yes. For a 2 × 2 table, MCC and the phi coefficient are the same formula. Both are Pearson’s correlation computed on two binary variables.

Why is MCC better than accuracy on imbalanced data?

Accuracy can be high just by predicting the majority class. MCC uses all four cells, so a model that ignores the minority class scores 0, as the accuracy trap above shows.

Can MCC be used for multi-class problems?

Yes. A generalised version works from the full multi-class confusion matrix, and scikit-learn computes it with the same function. Its minimum can be above −1 when there are more than two classes.

Does it matter which class I call positive?

No. Swapping the roles swaps TP with TN and FP with FN, and the formula gives the same value. Precision, recall and F1 do change.

Related: Cohens Kappa Calculator: Formula and Worked Example

Scroll to Top