Confusion Matrix Formulas
Every formula, in one table, with a live calculator so you can check your own numbers against them.
The Complete Formula Reference
The Four Building Blocks
Every formula below reduces to arithmetic on four counts: TP (true positive), TN (true negative), FP (false positive), and FN (false negative). Each one is what a model predicted, checked against what was actually true. If those four aren't familiar yet, the full walkthrough on the main calculator covers them with a worked example first.
All 22 Formulas
| Metric | Formula | Plain English |
|---|---|---|
| Core Metrics | ||
| Accuracy | (TP+TN) / Total | Share of all predictions that were correct |
| Sensitivity (Recall, TPR) | TP / (TP+FN) | Of actual positives, how many were caught |
| Specificity (TNR) | TN / (TN+FP) | Of actual negatives, how many were cleared |
| Precision (PPV) | TP / (TP+FP) | Of positive calls, how many were right |
| Negative Predictive Value | TN / (TN+FN) | Of negative calls, how many were right |
| Prevalence | (TP+FN) / Total | How common the positive class actually is |
| Composite Scores | ||
| F1-Score | 2·P·R / (P+R) | Harmonic mean of precision and recall |
| Balanced Accuracy | (Sens+Spec) / 2 | Honest accuracy when classes are imbalanced |
| Matthews Correlation (MCC) | (TP·TN−FP·FN) / √(...) | Correlation between predictions and truth, −1 to 1 |
| Cohen's Kappa | (p₀−pₑ) / (1−pₑ) | Agreement with the truth beyond chance |
| Youden's J | Sens+Spec−1 | 0 is useless, 1 is flawless |
| Markedness | PPV+NPV−1 | How trustworthy both predictions are, together |
| Jaccard Index (IoU, CSI) | TP / (TP+FP+FN) | Overlap of predicted and actual positives, ignoring TN |
| Fowlkes–Mallows | √(P·R) | Geometric mean of precision and recall |
| Rate Complements | ||
| False Positive Rate | FP / (TN+FP) | False-alarm rate on actual negatives |
| False Negative Rate | FN / (TP+FN) | Miss rate on actual positives |
| Error Rate | (FP+FN) / Total | 1 − Accuracy |
| False Discovery Rate | FP / (TP+FP) | 1 − Precision |
| False Omission Rate | FN / (TN+FN) | 1 − NPV |
| Diagnostic Ratios | ||
| Positive Likelihood Ratio | TPR / FPR | How much a positive result shifts the odds up |
| Negative Likelihood Ratio | FNR / TNR | How much a negative result shifts the odds down |
| Diagnostic Odds Ratio | LR+ / LR− | Overall discriminative power in one number |
Worked Example: A Diagnostic Test
A diagnostic test screens 500 patients. 50 actually have the condition. The test correctly flags 45 of them (TP=45, FN=5 missed) and correctly clears 423 of the 450 who don't have it, at the cost of 27 false alarms (FP=27, TN=423):
| Test positive | Test negative | |
|---|---|---|
| Actually has it | TP = 45 | FN = 5 |
| Actually doesn't | FP = 27 | TN = 423 |
| Metric | Calculation | Result |
|---|---|---|
| Accuracy | (45+423) / 500 | 93.6% |
| Sensitivity | 45 / 50 | 90% |
| Specificity | 423 / 450 | 94% |
| Precision | 45 / 72 | 62.5% |
| NPV | 423 / 428 | 98.83% |
| F1-Score | 2(.625)(.90) / (.625+.90) | 73.77% |
| MCC | (TP·TN−FP·FN) / √(...) | 0.72 |
| Positive Likelihood Ratio | 90% / 6% | 15.0 |
Accuracy (93.6%) and sensitivity (90%) both look reassuring, but precision is only 62.5%. Of everyone the test flags positive, more than a third don't actually have the condition. That gap is prevalence at work (only 10% of this population actually has it), and it's exactly the kind of thing a single accuracy number hides. See the Prevalence and likelihood ratio definitions for how that connects to real-world test interpretation, or plug in your own counts above.
Frequently Asked Questions
Why isn't there just one confusion matrix formula?
Because "correct" depends on what a wrong answer costs. Accuracy alone hides which kind of mistake a model is making. A spam filter that misses spam and a spam filter that blocks real mail are both "wrong" but not equally wrong. Different formulas isolate different failure modes on purpose, so you can pick the one that matches what actually matters for your problem.
What's the difference between a rate and a ratio in this table?
A rate (Accuracy, Sensitivity, FPR, Error Rate, ...) is a proportion between 0 and 1, computed as one count divided by a sum that includes it. A ratio like LR+ or the Diagnostic Odds Ratio divides two rates against each other, so it isn't bounded the same way. LR+ of 15 simply means a positive result is 15 times more likely from a true case than a false alarm, with no upper limit.
Which formula should I use to compare two models?
It depends on your data and what an error costs. Balanced Accuracy or MCC are reasonable defaults on imbalanced data since both account for all four confusion matrix cells at once. If false positives and false negatives cost differently, compare on whichever single metric captures that cost (Precision, Recall, or a weighted F-beta) instead of a general-purpose one.
Do these formulas work for multi-class problems?
As written, no. They assume a 2×2 matrix. For 3+ classes each formula is computed per class (treating that class as "positive" and everything else as "negative"), then usually averaged (macro-average treats every class equally; weighted-average accounts for class size). The Multi-Class tab in the calculator above does this automatically.