Contents
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
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:
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.
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
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 data | threshold | recall | false positives |
|---|---|---|---|
| plain | 0.5 | 56\% | 45 |
| undersampling | 0.5 | 88–94\% | 5{,}386–16{,}136 |
| copies | 0.5 | 86\% | 4{,}603 |
| SMOTE | 0.5 | 84\% | 4{,}285 |
| class weights | 0.5 | 86\% | 4{,}703 |
| class weights, corrected | 4\% | 80\% | 354 |
| plain | 4\% | 74\% | 296 |
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.