A model gives each parcel in a shipment a score for a material defect, and inspectors can open any parcel before it ships. The score alone does not say which parcels to open. Someone has to choose a cutoff, and a cutoff that suits cheap inspections and expensive misses can be a poor choice when those costs are reversed. This lesson separates the score from the action, counts the results of a cutoff in a confusion matrix, and uses the costs of the two kinds of error to decide where the cutoff belongs.
Key terms
Positive class
Definition In binary classification each example has an actual label , and the classifier makes a decision . The positive class is the class coded , the one the task is set up to detect, and the positive decision is the action taken for it. Scores are oriented so that a larger score means stronger evidence for the positive class. Which class is called positive is part of the problem statement, not a judgment that the outcome is good, and every count and rate on this page depends on that choice. Swapping the two classes exchanges true positives with true negatives and false positives with false negatives, and it exchanges sensitivity with specificity.
In plain words Positive means the thing we are looking for, such as a defect, a fraud or a failure, and it says nothing about whether that thing is welcome. The label records what is actually true, and the decision records what we do. Both meanings should be fixed and written down before any counting starts, because a positive decision for an actual negative is exactly the kind of error the next cards count.
Example In the constructed parcel data on this page, label means the parcel has a material defect that warrants inspection, and decision means the parcel is sent for inspection. Six parcels A to F have the scores and labels below, so A, C and E are actual positives and B, D and F are actual negatives. The labels are known here because the parcels were checked afterward. They would not be available when a new parcel is scored.
| Parcel | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| score | ||||||
| label |
Decision threshold
Definition Given a score and a real number , the threshold rule decides
The number is the decision threshold. On this page a score equal to the threshold counts as positive. That is a convention, and it should be stated, because software does not always use the same one. Changing changes the decisions without refitting the model that produced the scores. For fixed scores, lowering the threshold can only switch decisions from to , so every example selected at one threshold is also selected at every lower threshold. A threshold above the largest score selects nothing, and a threshold at or below the smallest score selects everything.
In plain words The score says how strongly the model points toward the positive class, and the threshold says where we draw the line for acting. The same scores support many different policies. When the score is a probability, the threshold picks the more probable class. If the probabilities can be trusted, that minimizes the expected number of errors, but the number of errors is the right target only when every error costs the same. Calling the default does not make it the right threshold for a given problem.
Example At the rule selects A, B and C. Parcel C is selected because its score equals the threshold. At the rule selects A to E and releases only F, and at it selects nothing.
Confusion matrix
Definition Let be the actual labels and the decisions for the same examples. Each example falls in exactly one of four cells. A true positive has and , a false positive has and , a true negative has and , and a false negative has and . The confusion matrix is the table of the four counts TP, FP, TN and FN. On this page its rows are the actual labels and its columns are the decisions, each in the order then .
| decision | decision | |
|---|---|---|
| actual | TN | FP |
| actual | FN | TP |
The four counts add to . The row totals and are the numbers of actual negatives and actual positives. They are fixed by the labels, while the column totals count the decisions and change with the rule. Other texts and software may swap the rows and columns or list the positive class first, so read the axis labels before reading a cell.
In plain words The matrix sorts every example by two questions, what was actually true and what we decided. In the name of a cell, the word true or false says whether the decision was correct, and the word positive or negative says what the decision was. A false negative is therefore a negative decision that was wrong.
Example At parcels A and C are true positives, B is a false positive, D and F are true negatives and E is a false negative.
| decision | decision | |
|---|---|---|
| actual | ||
| actual |
The row totals are and , the three sound parcels and the three defective ones, and they stay the same at every threshold on these data.
False positive and false negative
Definition A false positive is an example with actual label that receives decision , and a false negative is an example with actual label that receives decision . These are the only two kinds of error a binary decision can make. For a threshold rule with fixed scores and two thresholds , every example selected at is also selected at , so
A change of threshold therefore never reduces both counts. Lowering it can only turn true negatives into false positives and false negatives into true positives, and raising it does the reverse.
In plain words A false positive is a false alarm and a false negative is a miss. Moving the threshold trades one for the other. Reducing both at once takes a different score, one that separates the classes more cleanly. The two errors often have very different consequences, which is why adding them up as plain errors can hide what matters.
Example For the parcels, a false positive is a sound parcel sent for an unnecessary inspection, and a false negative is a defective parcel that is released. Lowering the threshold from to also selects D and E. Parcel D moves from true negative to false positive and parcel E moves from false negative to true positive, so the false positives rise from to and the false negatives fall from to .
Sensitivity and specificity
Definition Let be the number of actual positives and the number of actual negatives. When and ,
Sensitivity is also called recall or the true positive rate. The false positive rate is , which equals minus the specificity. When the evaluation examples are a random sample from the population of interest, the two rates estimate and for that rule. With no actual positives the sensitivity is undefined, and with no actual negatives the specificity is undefined. Report that absence rather than a value a program substitutes. For a threshold rule with fixed scores, lowering the threshold never decreases the sensitivity and never increases the specificity. Selecting every example gives sensitivity and specificity , and selecting none gives sensitivity and specificity .
In plain words Each rate looks within one actual class. Sensitivity asks what fraction of the actual positives we caught, and specificity asks what fraction of the actual negatives we correctly left alone. The denominator is a class count from the labels, not the number of positive decisions. Dividing the true positives by the number of positive decisions gives precision, a different quantity covered in the next lesson.
Example At the sensitivity is and the specificity is , so the false positive rate is . At the rows of the matrix are and , the sensitivity rises to and the specificity falls to . Neither pair says which threshold is better, because neither says what a miss or a false alarm costs.
Expected cost
Definition Let be the cost of decision when the actual label is , for , and let be the conditional probability of the positive class for the case being decided. The expected cost of decision is
the probability-weighted average of its possible costs before the outcome is known. Choosing for each case the decision with the smaller expected cost minimizes the expected cost per case among all rules that use , provided is the true conditional probability, the costs are fixed and known, and each case is decided on its own with no limit on the number of positive decisions. When correct decisions cost nothing and the error costs and are positive, the rule decides exactly when
At both decisions have the same expected cost, and this page sends ties to the positive decision. More generally, if and are both positive, the threshold is . On labeled data with free correct decisions, the observed cost of a rule is , and its average over a representative validation set estimates the expected cost per case of that rule.
In plain words Before the outcome is known, each action has a cost that depends on the truth, and the probability says how much weight each possibility deserves. Pick the action that costs less on average. The cheaper a false alarm is compared with a miss, the lower the threshold. If only parcels can be inspected, the problem changes. The expected cost is then lowest when the inspections go to at most cases with the largest expected savings from inspecting, skipping any case where inspecting would cost more than releasing. The rule is only as good as the probabilities, so a score that has not been checked for calibration cannot simply be used as . In that case choose the threshold by comparing observed costs on validation data or across cross-validation folds, and keep the test set untouched for the final estimate of the chosen procedure.
Example Take and in abstract cost units, with correct decisions free. The threshold is . If the score of parcel D were a trustworthy probability, inspecting D would have expected cost and releasing it , so D is inspected, even though it later turns out sound and that inspection costs . For parcel E at both actions have expected cost , and the tie convention inspects it. On the six labeled parcels both thresholds have accuracy , yet the observed cost is at and at . Adding up the expected costs instead gives for the rule at and for the rule at . Both comparisons favor here, but six outcomes are far too few to show that it is better on future parcels. With the costs reversed to and the threshold becomes , only A is inspected and the observed cost is , against for keeping the threshold at .
Check yourself
- Five parcels have scores , , , and and labels , , , and . Using the threshold , with a score equal to the threshold counted as positive, find the confusion matrix, the sensitivity and the specificity.
Show the answer
The rule selects the first three parcels. Two of them are actual positives and one is an actual negative, so there are true positives and false positive. The parcel with score is an actual positive that is released, a false negative, and the parcel with score is a true negative.
| decision | decision | |
|---|---|---|
| actual | ||
| actual |
The sensitivity is and the specificity is .
- Four new parcels have scores , , and and labels , , and . A false positive costs , a false negative costs and correct decisions cost nothing. Find the cost-based threshold, its confusion matrix and its observed cost, and compare it with the threshold . Why can be a poor choice here?
Show the answer
The threshold is , so the first three parcels are inspected. That gives true positives, false positive, true negative and no false negatives, an observed cost of , sensitivity and specificity . At only the first parcel is inspected, which gives true positive, true negatives and false negative, at a cost of .
Both thresholds have accuracy , so accuracy cannot tell them apart, while the costs differ by a factor of . For the third parcel, inspection has expected cost and release has expected cost , so inspecting is the cheaper action in expectation. The threshold treats a miss and a false alarm as equally bad, and here a miss costs three times as much. This reasoning assumes the scores are trustworthy probabilities, and four parcels are far too few to confirm the better threshold.
- Return to the costs and , and add a labor cost of for every inspection, whether or not the parcel is defective. A colleague keeps the threshold . Is that right? Find the threshold that minimizes expected cost.
Show the answer
It is not right. The formula assumes that correct decisions cost nothing, and now inspecting a defective parcel costs . The cost table is , , and . Inspection has expected cost and release has expected cost . Inspection is no more expensive than release when
The general formula agrees, since and give . Parcel D, at , should now be released, because inspecting it has expected cost against for releasing it.