← Math
Math

Precision, recall, and the confusion matrix

Is the fraud model 99% accurate, or is it useless?

Judge a classifier by the mistakes and workload its threshold creates.

Outcome

Build a confusion matrix at a threshold, name the denominators of precision, recall and accuracy, and choose a threshold for a recall target within review capacity.

Terminology (6)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Confusion matrix
A 2 × 2 table of true positives, false positives, false negatives and true negatives at one threshold.
Precision
True positives divided by everything flagged: how many flags are right.
Recall Also sensitivity
True positives divided by every real positive: how much of the problem the flags catch.
Accuracy
Correct decisions divided by all items. On rare classes it is high even for a system that flags nothing.
Threshold
The score at or above which an item is flagged. Raising it cannot increase recall or the number of flags; precision must be measured.
Review capacity
How many flags the people downstream can handle in a period.

Why this matters

Fraud models, spam filters, alerting rules, code scanners and search ranking all flag some items and pass the rest. A single score such as accuracy hides the two failures that cost money: flags that waste a reviewer’s time and problems that slip through. Choosing a threshold is choosing between them, under the limits of the people who handle the flags.

This lesson assumes conditional probability from the previous lesson.

What a confusion matrix separates

A confusion matrix crosses a yes/no prediction with the known outcome. A true positive is a correctly flagged problem; a false positive is a flag on a legitimate case. A false negative is a missed problem, and a true negative is a correctly cleared case. These four counts describe one labeled population at one threshold.

Precision and recall use the conditional-probability idea from the previous lesson. Precision asks what share of flags are real problems; recall asks what share of real problems were flagged. Accuracy counts all correct decisions. Changing the score threshold changes which cases enter the review queue, so a threshold decision must consider both the target and the available review capacity.

01Build the matrix

Build the matrix

A fraud model that flags nothing is right about 99% of transactions, because 99% are legitimate. The review team needs two other numbers: how many flags are really fraud, and how much fraud gets flagged. Each has its own denominator, and changing the threshold changes the review queue.

Last month’s labeled transactions fall into four score bands: score 90 had 40 fraud and 10 legitimate, score 70 had 30 and 90, score 50 had 20 and 400, and score 20 had 10 and 9,400. Flag everything scoring 70 or more: the flagged fraud (70) are true positives, the flagged legitimate (100) false positives, the missed fraud (30) false negatives and the cleared legitimate (9,800) true negatives. Predict the three lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Flagged at score 70 or above: 40 + 30 fraud and 10 + 90 legitimate transactions.
const [tp, fp, fn, tn] = [70, 100, 30, 9800];

console.log((tp / (tp + fp)).toFixed(4)); // of the flags
console.log((tp / (tp + fn)).toFixed(4)); // of the fraud
console.log(((tp + tn) / (tp + fp + fn + tn)).toFixed(4)); // of everything
Which line answers “how many flags are really fraud”?

02Name the denominators

Name the denominators

The reviewers ask how many of their 170 flags were worth investigating: 70 / 170. The fraud team asks how much of the 100 confirmed fraud cases they caught: 70 / 100. Precision divides by the flags, TP + FP. Recall divides by the fraud, TP + FN. Accuracy divides by everything. When a denominator is 0 the ratio has no value: a queue that flagged nothing has no precision, and a month with no fraud has no recall.

TypeScript divides numbers in floating point, so tp / fraud * 100 works; 0 / 0 gives NaN, which needs its own check.

03Why accuracy misleads

Why accuracy misleads

What accuracy does a model that flags nothing reach on these 10,000 transactions?

Suppose next month has 50 fraud cases and the same 9,900 legitimate transactions. Keep each class’s score distribution unchanged. Expected flagged fraud halves from 70 to 35, while false positives stay at 100: precision is 35 / 135 ≈ 0.26 and recall is 35 / 50 = 0.70. The review queue’s usefulness depends on the population as well as the model. Changed score distributions would need a fresh calculation.

04Recall cannot rise as the threshold rises

Recall cannot rise as the threshold rises

The same bands at each threshold
Flag atFlagsPrecisionRecall
≥ 90500.80000.4000
≥ 701700.41180.7000
≥ 505900.15250.9000
≥ 2010,0000.01001.0000
The team must catch at least 85% of fraud with as few reviews as possible. Which threshold should it use?

05Fit the review capacity

Fit the review capacity

At threshold 50, TP = 40 + 30 + 20 = 90 and FP = 10 + 90 + 400 = 500. That catches 90 / 100 of the fraud and creates 90 + 500 = 590 reviews. Here precision rises with the threshold, but that is a property of these bands. A high-score band with 1 fraud and 9 legitimate transactions has 10% precision; adding a lower band with 9 fraud and 1 legitimate transaction raises it to 50%. Recall and flag counts, rather than a promise about precision, justify our threshold choice.

06Tune the review queue

Tune the review queue

The payments team has a fresh set of labeled score bands. Repair its queue metrics, then classify those bands and choose a threshold the reviewers can handle. Earlier helpers stay working as each requirement is added.

In the frontend Showing precision and recall honestly in Next.js and Svelte Optional

A review dashboard shows each ratio next to the counts behind it, labeled by its question, and says “no flags yet” instead of NaN% when a denominator is empty.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Ratios with their counts, and no NaN when a denominator is empty.
function share(part: number, whole: number) {
  return whole === 0
    ? "—"
    : `${((100 * part) / whole).toFixed(1)}% (${part} of ${whole})`;
}

export function ReviewQuality({ tp, fp, fn }: { tp: number; fp: number; fn: number }) {
  return (
    <dl>
      <dt>Flags that were fraud (precision)</dt>
      <dd>{tp + fp === 0 ? "No flags yet" : share(tp, tp + fp)}</dd>
      <dt>Fraud that was flagged (recall)</dt>
      <dd>{tp + fn === 0 ? "No confirmed fraud yet" : share(tp, tp + fn)}</dd>
    </dl>
  );
}

Check this idea later
A spam filter has precision 0.95 where half the mail is spam. Keep its positive detection rate and its nonzero false-positive rate fixed. Will precision stay 0.95 where 5% is spam?
A report first truncates 90 / 100 to a whole number, then multiplies by 100. What percentage does it report?
Sources

Values above were computed in Node 22 and run in Go 1.27, Rust 1.99 and Apple clang 17. The review queue is a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.