MoonSay /Tutorials/Machine Learning Interview #13
Contents
  1. 01Introduction
  2. 02What goes wrong
  3. 03Balance the data
  4. 04Class weights
  5. 05Fix the scores
  6. 06Interview follow-ups
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Imbalanced Data/Chapter 05 of 09

05Fix the scores

5.1The model learned a 50/50 world

Repeat the argument of (2) for the weighted loss (4). Setting its derivative with respect to the intercept to zero gives

\sum_{i=1}^{n}c_{y_i}\,(p_i-y_i)=0\qquad\Longrightarrow\qquad\frac{\sum_i c_{y_i}\,p_i}{\sum_i c_{y_i}}=\frac{w\,n_+}{w\,n_++n_-}=\frac12 . \tag{5}

The weighted average of the predicted probabilities is one half: the model believes that half of the (weighted) transactions are fraud, because in its training data they were. The same is true after copying or SMOTE, which balance the data literally. On the test day, the weighted model’s average fraud probability is 7\%, while the real fraud rate is 0.15\%. Its probabilities describe a balanced world, not ours, and the fix has two steps (Elkan, 2001): undo the balancing, then choose the threshold from costs.

5.2Step 1: undo the balancing

Start with counts. Suppose the balanced model gives a transaction p_{\mathrm{bal}}=50\%. In its balanced world this means: among transactions like this one, one fraud for every one legitimate. But in that world every fraud was counted 514 times. In reality, legitimate payments are 514 times more common, so it is one fraud for every 514 legitimate payments: p=1/515=0.19\%.

Proposition 2 (Prior correction). If the training data were balanced by giving the frauds weight w (or w copies), the probability p in the real proportions follows from the balanced model’s probability p_{\mathrm{bal}} by dividing the odds by w:

\frac{p}{1-p}=\frac{1}{w}\,\frac{p_{\mathrm{bal}}}{1-p_{\mathrm{bal}}},\qquad\text{equivalently}\qquad \operatorname{logit}p=\operatorname{logit}p_{\mathrm{bal}}-\ln w . \tag{6}

Consider the training transactions that look like x, say n_1(x) frauds and n_0(x) legitimate ones. A model that fits the data well estimates the odds of fraud at x by their ratio, n_1(x)/n_0(x). Weighting or copying every fraud w times turns n_1(x) into w\,n_1(x) and leaves n_0(x) unchanged, so the balanced model estimates w\,n_1(x)/n_0(x), which is w times the real odds. Divide by w. Undersampling the legitimate payments by a factor w instead turns n_0(x) into n_0(x)/w, with the same result.

This is the classic “compensating for class priors” of Bishop (2006, §1.5.4): divide the probabilities by the class fractions of the training set, multiply by the real ones, and normalize. King and Zeng (2001) write it as a correction of the logistic intercept by \ln w, and Saerens et al. (2002) extend it to any number of classes. For SMOTE, which adds frauds near existing frauds, it is approximate.

Why divide the odds and not simply the probability? Because the probability of fraud and the probability of legitimate must still add up to one. Dividing p_{\mathrm{bal}}=50\% by 514 gives 0.097\%, half the right answer; dividing p_{\mathrm{bal}}=99\% gives 0.19\%, while (6) gives 16\% (Figure 4a). In the counts picture, 99\% means 99 frauds for every legitimate payment in the balanced world; in reality that is 99 frauds for every 514 legitimate payments, which is 16\%.

Notice also what the correction does not change: it is an increasing function of p_{\mathrm{bal}}, so the order of the transactions, and with it the ROC curve and the precision–recall curve, stays exactly the same. It changes what each score means. A balanced 50\% is a real 0.19\%: flagging at the balanced threshold 0.5 means flagging every payment with a one-in-515 chance of fraud, and that is where the thousands of false positives came from.

(a) The correction of Proposition 2: dividing the odds by 514 (blue) against dividing the probability (dashed, wrong); dotted: the threshold 4\% of Proposition 3. (b) The weighted model’s corrected scores on the test day, drawn as in Figure 1b, with the threshold 4\%. Computed for this tutorial, with the same data and model as the companion video.
Figure 4. (a) The correction of Proposition 2: dividing the odds by 514 (blue) against dividing the probability (dashed, wrong); dotted: the threshold 4\% of Proposition 3. (b) The weighted model’s corrected scores on the test day, drawn as in Figure 1b, with the threshold 4\%. Computed for this tutorial, with the same data and model as the companion video.
Try itUndo the balancing

Choose the balanced model's probability p_{\mathrm{bal}} and the balancing factor w (514 for our data). Left: the correction of equation (6), dividing the odds by w, against dividing the probability. Right: the corrected probability against the cost-based threshold of 4%.

5.3Step 2: set the threshold from costs

With probabilities that mean what they say, the threshold should come from what the two errors cost. A false negative lets a fraud through and costs about its amount; a false positive costs the check, say a phone call to the customer.

Proposition 3 (The cost-based threshold, Murphy, 2022, eq. 5.10; Elkan, 2001). For a transaction with fraud probability p, flagging it has expected cost (1-p)\,C_{\mathrm{FP}} and letting it pass has expected cost p\,C_{\mathrm{FN}}. Flagging is cheaper exactly when

p\;\ge\;\tau^*=\frac{C_{\mathrm{FP}}}{C_{\mathrm{FP}}+C_{\mathrm{FN}}}. \tag{7}

If we flag, we pay C_{\mathrm{FP}} when the payment is legitimate, which happens with probability 1-p. If we pass, we pay C_{\mathrm{FN}} when it is fraud, with probability p. Flag when (1-p)\,C_{\mathrm{FP}}\le p\,C_{\mathrm{FN}}, that is, C_{\mathrm{FP}}\le p\,(C_{\mathrm{FP}}+C_{\mathrm{FN}}), which is (7).

The average fraud on day 1 was about 120 euros, so we take C_{\mathrm{FN}}=120 euros, and assume a false positive costs C_{\mathrm{FP}}=5 euros. Then \tau^*=5/125=4\%. The threshold 0.5 is right only when both errors cost the same. Read the other way, flagging at a balanced 0.5 treats a false negative as w times as costly as a false positive: rebalancing the data is the same as moving the threshold (Elkan, 2001).

Worked example 4 (The two steps on the test day). Correct the weighted model’s probabilities with (6) and flag every test transaction with p\ge4\%. The model flags 169 of the 211 frauds, a recall of 80\%, with 354 false positives instead of 4{,}703 (Figure 4b). A little recall was given back, and most of the false positives went away.

Table 1 puts all the remedies side by side. The corrected probabilities are close to calibrated but not perfect: their average on the test day is 0.23\%, against a real fraud rate of 0.15\% on that day, because the weighted model also changed its slopes (Chapter 4). For a well-specified model the correction would be exact.

training datathresholdrecallfalse positives
plain0.556\%45
undersampling0.588–94\%5{,}386–16{,}136
copies0.586\%4{,}603
SMOTE0.584\%4{,}285
class weights0.586\%4{,}703
class weights, corrected4\%80\%354
plain4\%74\%296
Table 1. Every remedy trained on day 1 and tested once on day 2 (211 frauds). Undersampling: the range over five random draws. Computed for this tutorial, with the same data and model as the companion video.
Try itSet the threshold from costs

Price a false negative and a false positive. The threshold follows from equation (7). Left: the total cost on the test day at every threshold, for the weighted model after the correction and for the plain model; dots: the cost-based threshold. Right: what the corrected model does at that threshold.

Pause and think. A balanced model gives a transaction p_{\mathrm{bal}}=90\%. With w=514 and the costs above, do you flag it?

Show the answer

No. Its odds are 9; divided by 514 they are 0.0175, so p=1.7\%, below the threshold of 4\%. A balanced score has to reach about 95\% before it is flagged.

That is the classic answer: balance the data or weight the classes if you need to, undo the balancing, and set the threshold from costs. Now, the follow-ups interviewers love.