Contents
03Balance the data
The first family of remedies changes the training data until the two classes are about equally common. There are two ways to get there: remove legitimate payments, or add frauds.
3.1Undersampling: throw data away
Random undersampling keeps all 281 frauds and draws 281 of the 144{,}505 legitimate payments at random. The data are now balanced, and the model trains in a blink. But 144{,}224 legitimate payments, 99.8\% of them, are thrown away, and with them most of what the data could teach about normal behavior.
The model now depends heavily on which 281 payments happened to be drawn. Repeat the recipe with a different random draw and you get a different model: over five draws, the number of false positives on the test day at threshold 0.5 ranged from 5{,}386 to 16{,}136 (Figure 2a). A model that changes a lot when the training sample changes has high variance; it is unstable, in the language of the bias–variance tradeoff of Episode 2. Undersampling is still useful when the data are enormous, and averaging many models built on different undersamples reduces the variance again (He and Garcia, 2009). With our data, it is too expensive.
3.2Oversampling: copy the frauds
To keep all the data, go the other way. Random oversampling copies frauds until there are as many frauds as legitimate payments: 144{,}505 frauds, about 514 copies of each. Nothing is thrown away. But nothing is added either: a copy is the same point again. A flexible model, such as a decision tree, can then draw a tiny region around each copied point and memorize it, which is overfitting (Chawla et al., 2002; He and Garcia, 2009). Copies add weight, not information.
3.3SMOTE: create new frauds
Chawla et al. (2002) proposed to create new, synthetic frauds instead: the Synthetic Minority Over-sampling Technique, SMOTE. A new fraud is placed on the line segment between a real fraud and one of its fraud neighbors:
- pick a fraud x from the training data;
- find its k=5 nearest neighbors among the frauds (Euclidean distance on the standardized features);
- pick one of them at random, x_{\mathrm{nb}};
- draw a number \lambda uniformly between 0 and 1;
create the new fraud
x_{\mathrm{new}}=x+\lambda\,(x_{\mathrm{nb}}-x); \tag{3}- repeat until the classes are balanced; here, 144{,}224 new frauds.
At \lambda=0 the new point is x itself, at \lambda=1 it is the neighbor, and in between it is a mixture of the two, feature by feature. Figure 2b shows one step in two of the 29 features (the real algorithm works in all 29). Instead of stacking copies on the same points, SMOTE fills in the region between neighboring frauds, so the region the model assigns to fraud becomes larger and less specific, which is exactly what Chawla and colleagues wanted.
Worked example 2 (SMOTE on day 1). Train the same logistic regression on day 1 plus the 144{,}224 synthetic frauds, and test once on day 2. At threshold 0.5 the model now flags 178 of the 211 frauds: recall rises from 56\% to 84\%.
SMOTE has its own weaknesses. It assumes that the space between two frauds is fraud too. When frauds sit close to legitimate payments, as the fraud x in Figure 2b does, the new points can land among legitimate ones and blur the boundary, a problem He and Garcia (2009) call over-generalization. Distances need standardized numeric features; categorical features need a variant, SMOTE-NC, proposed in the same paper. And the synthetic points are made from the training frauds only, a rule we return to in Chapter 6.
Pause and think. What does SMOTE become if \lambda is always 0?
Show the answer
Random oversampling: by (3) every new fraud is an exact copy of an existing fraud. SMOTE is oversampling with the copies spread out along the segments to the neighbors.
All three methods make the frauds count more by changing the data. But copying a fraud 514 times just makes the loss count it 514 times. Is there a simpler way? Chapter 4 has it.