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.
// 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
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
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
| Flag at | Flags | Precision | Recall |
|---|---|---|---|
| ≥ 90 | 50 | 0.8000 | 0.4000 |
| ≥ 70 | 170 | 0.4118 | 0.7000 |
| ≥ 50 | 590 | 0.1525 | 0.9000 |
| ≥ 20 | 10,000 | 0.0100 | 1.0000 |
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.
A fraud review queue
Score the queue, build the confusion matrix, then choose the threshold.
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.
// 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
Sources
- Google ML Crash Course: accuracy, precision and recall
- Go specification: integer division
- Rust: integer division truncates
- C++: multiplicative operators
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.
Sign in to keep your read marks on every device. Sign in
Was this helpful?