Unit 5, lesson 30

Confusion Matrices and Decision Thresholds

How a decision threshold turns scores into actions, how a confusion matrix counts right and wrong decisions, what sensitivity and specificity measure, and how expected cost sets the threshold.

  • Video 30
  • Decisions, Metrics and Calibration

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 y∈{0,1}y \in \{0, 1\}, and the classifier makes a decision y^∈{0,1}\hat{y} \in \{0, 1\}. The positive class is the class coded 11, the one the task is set up to detect, and the positive decision y^=1\hat{y} = 1 is the action taken for it. Scores are oriented so that a larger score s(x)s(\mathbf{x}) 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 11 means the parcel has a material defect that warrants inspection, and decision 11 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.

ParcelABCDEF
score0.90.90.70.70.50.50.350.350.20.20.10.1
label110011001100

Decision threshold

Definition Given a score s(x)s(\mathbf{x}) and a real number tt, the threshold rule decides

y^={1,s(x)≥t,0,s(x)<t.\hat{y} = \begin{cases} 1, & s(\mathbf{x}) \ge t, \\ 0, & s(\mathbf{x}) < t. \end{cases}

The number tt 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 tt changes the decisions without refitting the model that produced the scores. For fixed scores, lowering the threshold can only switch decisions from 00 to 11, 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 0.50.5 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 0.50.5 the default does not make it the right threshold for a given problem.

Example At t=0.5t = 0.5 the rule selects A, B and C. Parcel C is selected because its score equals the threshold. At t=0.2t = 0.2 the rule selects A to E and releases only F, and at t=0.95t = 0.95 it selects nothing.

Confusion matrix

Definition Let y1,…,yny_1, \dots, y_n be the actual labels and y^1,…,y^n\hat{y}_1, \dots, \hat{y}_n the decisions for the same nn examples. Each example falls in exactly one of four cells. A true positive has yi=1y_i = 1 and y^i=1\hat{y}_i = 1, a false positive has yi=0y_i = 0 and y^i=1\hat{y}_i = 1, a true negative has yi=0y_i = 0 and y^i=0\hat{y}_i = 0, and a false negative has yi=1y_i = 1 and y^i=0\hat{y}_i = 0. 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 00 then 11.

decision 00decision 11
actual 00TNFP
actual 11FNTP

The four counts add to nn. The row totals n0=TN+FPn_0 = \text{TN} + \text{FP} and n1=FN+TPn_1 = \text{FN} + \text{TP} 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 t=0.5t = 0.5 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 00decision 11
actual 002211
actual 111122

The row totals are 33 and 33, 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 00 that receives decision 11, and a false negative is an example with actual label 11 that receives decision 00. These are the only two kinds of error a binary decision can make. For a threshold rule with fixed scores and two thresholds t′<tt' < t, every example selected at tt is also selected at t′t', so

FP(t′)≥FP(t),FN(t′)≤FN(t).\begin{gathered} \text{FP}(t') \ge \text{FP}(t), \\[0.4em] \text{FN}(t') \le \text{FN}(t). \end{gathered}

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 0.50.5 to 0.20.2 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 11 to 22 and the false negatives fall from 11 to 00.

Sensitivity and specificity

Definition Let n1=TP+FNn_1 = \text{TP} + \text{FN} be the number of actual positives and n0=TN+FPn_0 = \text{TN} + \text{FP} the number of actual negatives. When n1≥1n_1 \ge 1 and n0≥1n_0 \ge 1,

sensitivity=TPTP+FN,specificity=TNTN+FP.\begin{aligned} \text{sensitivity} &= \frac{\text{TP}}{\text{TP} + \text{FN}}, \\[0.4em] \text{specificity} &= \frac{\text{TN}}{\text{TN} + \text{FP}}. \end{aligned}

Sensitivity is also called recall or the true positive rate. The false positive rate is FP/(TN+FP)\text{FP}/(\text{TN} + \text{FP}), which equals 11 minus the specificity. When the evaluation examples are a random sample from the population of interest, the two rates estimate P(Y^=1∣Y=1)P(\hat{Y} = 1 \mid Y = 1) and P(Y^=0∣Y=0)P(\hat{Y} = 0 \mid Y = 0) 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 11 and specificity 00, and selecting none gives sensitivity 00 and specificity 11.

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 t=0.5t = 0.5 the sensitivity is 2/32/3 and the specificity is 2/32/3, so the false positive rate is 1/31/3. At t=0.2t = 0.2 the rows of the matrix are (1,2)(1, 2) and (0,3)(0, 3), the sensitivity rises to 3/3=13/3 = 1 and the specificity falls to 1/31/3. Neither pair says which threshold is better, because neither says what a miss or a false alarm costs.

Expected cost

Definition Let C(a,y)C(a, y) be the cost of decision aa when the actual label is yy, for a,y∈{0,1}a, y \in \{0, 1\}, and let p=P(Y=1∣x)p = P(Y = 1 \mid \mathbf{x}) be the conditional probability of the positive class for the case being decided. The expected cost of decision aa is

R(a∣x)=C(a,1) p+C(a,0) (1−p),\begin{aligned} R(a \mid \mathbf{x}) = {} & C(a, 1)\, p \\[0.4em] {} + {} & C(a, 0)\,(1 - p), \end{aligned}

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 x\mathbf{x}, provided pp 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 cFP=C(1,0)c_{\text{FP}} = C(1, 0) and cFN=C(0,1)c_{\text{FN}} = C(0, 1) are positive, the rule decides 11 exactly when

p≥t∗=cFPcFP+cFN.p \ge t^{*} = \frac{c_{\text{FP}}}{c_{\text{FP}} + c_{\text{FN}}}.

At p=t∗p = t^{*} both decisions have the same expected cost, and this page sends ties to the positive decision. More generally, if Δ0=C(1,0)−C(0,0)\Delta_0 = C(1, 0) - C(0, 0) and Δ1=C(0,1)−C(1,1)\Delta_1 = C(0, 1) - C(1, 1) are both positive, the threshold is Δ0/(Δ0+Δ1)\Delta_0 / (\Delta_0 + \Delta_1). On labeled data with free correct decisions, the observed cost of a rule is cFP FP+cFN FNc_{\text{FP}}\,\text{FP} + c_{\text{FN}}\,\text{FN}, 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 kk parcels can be inspected, the problem changes. The expected cost is then lowest when the inspections go to at most kk 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 pp. 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 cFP=2c_{\text{FP}} = 2 and cFN=8c_{\text{FN}} = 8 in abstract cost units, with correct decisions free. The threshold is 2/(2+8)=0.22/(2 + 8) = 0.2. If the score 0.350.35 of parcel D were a trustworthy probability, inspecting D would have expected cost 2×0.65=1.32 \times 0.65 = 1.3 and releasing it 8×0.35=2.88 \times 0.35 = 2.8, so D is inspected, even though it later turns out sound and that inspection costs 22. For parcel E at 0.20.2 both actions have expected cost 1.61.6, and the tie convention inspects it. On the six labeled parcels both thresholds have accuracy 4/64/6, yet the observed cost is 1×2+1×8=101 \times 2 + 1 \times 8 = 10 at t=0.5t = 0.5 and 2×2+0×8=42 \times 2 + 0 \times 8 = 4 at t=0.2t = 0.2. Adding up the expected costs instead gives 77 for the rule at 0.50.5 and 5.55.5 for the rule at 0.20.2. Both comparisons favor 0.20.2 here, but six outcomes are far too few to show that it is better on future parcels. With the costs reversed to cFP=8c_{\text{FP}} = 8 and cFN=2c_{\text{FN}} = 2 the threshold becomes 0.80.8, only A is inspected and the observed cost is 44, against 1616 for keeping the threshold at 0.20.2.

Check yourself

  1. Five parcels have scores 0.850.85, 0.60.6, 0.40.4, 0.30.3 and 0.150.15 and labels 11, 11, 00, 11 and 00. Using the threshold 0.40.4, 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 22 true positives and 11 false positive. The parcel with score 0.30.3 is an actual positive that is released, a false negative, and the parcel with score 0.150.15 is a true negative.

decision 00decision 11
actual 001111
actual 111122

The sensitivity is 2/(2+1)=2/32/(2 + 1) = 2/3 and the specificity is 1/(1+1)=1/21/(1 + 1) = 1/2.

  1. Four new parcels have scores 0.80.8, 0.450.45, 0.30.3 and 0.050.05 and labels 11, 00, 11 and 00. A false positive costs 33, a false negative costs 99 and correct decisions cost nothing. Find the cost-based threshold, its confusion matrix and its observed cost, and compare it with the threshold 0.50.5. Why can 0.50.5 be a poor choice here?
Show the answer

The threshold is 3/(3+9)=0.253/(3 + 9) = 0.25, so the first three parcels are inspected. That gives 22 true positives, 11 false positive, 11 true negative and no false negatives, an observed cost of 1×3=31 \times 3 = 3, sensitivity 11 and specificity 1/21/2. At 0.50.5 only the first parcel is inspected, which gives 11 true positive, 22 true negatives and 11 false negative, at a cost of 99.

Both thresholds have accuracy 3/43/4, so accuracy cannot tell them apart, while the costs differ by a factor of 33. For the third parcel, inspection has expected cost 3×0.7=2.13 \times 0.7 = 2.1 and release has expected cost 9×0.3=2.79 \times 0.3 = 2.7, so inspecting is the cheaper action in expectation. The threshold 0.50.5 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.

  1. Return to the costs cFP=2c_{\text{FP}} = 2 and cFN=8c_{\text{FN}} = 8, and add a labor cost of 33 for every inspection, whether or not the parcel is defective. A colleague keeps the threshold 2/(2+8)=0.22/(2 + 8) = 0.2. Is that right? Find the threshold that minimizes expected cost.
Show the answer

It is not right. The formula cFP/(cFP+cFN)c_{\text{FP}}/(c_{\text{FP}} + c_{\text{FN}}) assumes that correct decisions cost nothing, and now inspecting a defective parcel costs 33. The cost table is C(1,1)=3C(1, 1) = 3, C(1,0)=3+2=5C(1, 0) = 3 + 2 = 5, C(0,1)=8C(0, 1) = 8 and C(0,0)=0C(0, 0) = 0. Inspection has expected cost 3p+5(1−p)=5−2p3p + 5(1 - p) = 5 - 2p and release has expected cost 8p8p. Inspection is no more expensive than release when

5−2p≤8p5≤10pp≥0.5.\begin{aligned} 5 - 2p &\le 8p \\[0.4em] 5 &\le 10p \\[0.4em] p &\ge 0.5 . \end{aligned}

The general formula agrees, since Δ0=5\Delta_0 = 5 and Δ1=8−3=5\Delta_1 = 8 - 3 = 5 give 5/10=0.55/10 = 0.5. Parcel D, at 0.350.35, should now be released, because inspecting it has expected cost 4.34.3 against 2.82.8 for releasing it.

How this lesson connects