Contents
04Class weights
4.1Weight the loss
The second family leaves the data alone and changes the loss. Give every fraud a weight w and every legitimate transaction a weight 1:
The usual choice is w=n_-/n_+=514, so that the two classes carry the same total weight: 281\times514\approx144{,}505. This is cost-sensitive learning at its simplest: an error on a fraud now costs 514 times as much in training (Elkan, 2001; He and Garcia, 2009).
Proposition 1 (A weight is a number of copies). Training on (4) with weight w gives the same model as training on the original data with every fraud copied w times.
The loss (1) has one term per training example. A copy of fraud i is the same point with the same label, so it adds the same term -\log p_i once more. With w copies of every fraud, fraud i contributes w\,(-\log p_i), which is its term in (4); the legitimate terms are unchanged. The two objectives are the same function of \theta, so they have the same minimizer.
Random oversampling draws the copies at random, so each fraud gets roughly, not exactly, 514 copies; that is the only difference. Weights give the exact version, with one line of code, no larger data set and no randomness. The same idea works for most learners: a weighted hinge loss for a support vector machine, a weighted impurity for a decision tree.
Worked example 3 (Weight 514 on day 1). With class weights the model flags 181 of the 211 test frauds: recall 86\%. Random copies give the same recall, as Proposition 1 predicts. But there is a price: the false positives jump from 45 to 4{,}703 (copies: 4{,}603).
4.2Why the scores split into two humps
Figure 3 compares the scores of the plain model and the weighted model on the test day. Two things change. Everything moves to the right, which is why so many legitimate payments now pass the threshold. And the single hump of legitimate payments splits into two.
The reason is that the weighted model is a different model, not the plain model shifted. With the frauds now carrying half of the total weight, the fit leans on the features where frauds stand out. Frauds tend to have low values of the component V_6, and its coefficient grows from -0.08 to -5.7; the coefficient of V_{24} grows in the same way. But the same scoring formula is applied to every legitimate payment too. Those that look like frauds on these features are lifted together with the frauds, the others are pushed down, and one hump becomes two. In one sentence: to lift the frauds, the model leans on features where frauds stand out, so legitimate transactions that look like frauds get lifted too.
For a logistic model that is exactly right, changing the class balance would change only the intercept, by \ln w, and leave every slope alone (Prentice and Pyke, 1979; King and Zeng, 2001). Real data are never fitted exactly by a linear model, and the fit then moves its slopes toward wherever the weight sits. This is worth knowing, because the correction of the next chapter is exact only in that ideal case.
Pause and think. A colleague adds class weights and reports: “recall went from 56\% to 86\%”. What do you ask?
Show the answer
“At what threshold, and how many false positives?” Recall alone can always be raised by flagging more. Here the same change took the false positives from 45 to 4{,}703; the precision, the share of flagged payments that are fraud, fell from 72\% to 4\%.
Balancing worked for recall, but the scores are now far too high. Why are they so high? Chapter 5 answers it, and fixes them.