Contents
02What goes wrong
Before fixing anything we need to see the problem clearly, on real data, and understand where it comes from.
2.1The data and the model
Dal Pozzolo et al. (2015) released, together with their paper on this very problem, two days of card payments made by European cardholders in September 2013: 284{,}807 transactions, of which 492 are fraud. To protect the cardholders, 28 of the features are principal components of the original variables, named V_1,\dots,V_{28}; the others are the time and the amount.
We do what a bank would do: train on the past and test on the future (Figure 1a). Day 1 trains a logistic regression on the 28 components and the logarithm of the amount. It has n_+=281 frauds and n_-=144{,}505 legitimate payments, so w=n_-/n_+=514: there are 514 legitimate payments for every fraud. Day 2, with 211 frauds among 140{,}021 payments, is the test set and is used only once, at the end.
2.2One loss term per transaction
Logistic regression is trained by minimizing the log loss, a sum with one term per training transaction:
where \theta=(\beta,b) are the slopes and the intercept. A fraud contributes -\log p_i, which is large when the model gives it a low probability; a legitimate transaction contributes -\log(1-p_i), large when the model gives it a high one. Of the 144{,}786 terms, only 281 are frauds. The optimizer reduces the total, and the total is almost entirely legitimate transactions: rare classes have a small effect on the overall loss, so the model focuses on the common class (Murphy, 2022, §10.3.8.2). Think of a vote in which one side has 514 times as many voters.
There is a neat way to see how far this goes. At the minimum, the derivative of (1) with respect to the intercept is zero, and since \partial p_i/\partial b=p_i(1-p_i),
The model’s average predicted probability equals the fraction of frauds in the training data: the expected number of frauds matches the observed number (Hastie et al., 2009, §4.4.1). On day 1 both are 0.19\%. The model’s scores live near 0.2\%, and only a transaction that looks overwhelmingly like fraud gets past p=0.5. (The penalty that regularizes the slopes does not touch the intercept, so (2) holds for our model too.)
Worked example 1 (The plain model at threshold 0.5). On the test day the model flags 118 of the 211 frauds: recall 118/211=56\%. The other 93 frauds are false negatives. Only 45 legitimate payments are flagged. Figure 1b shows why: the legitimate payments form one hump far to the left, near p=10^{-4}, and the frauds are spread out, with almost half of them left of the line.
Its accuracy, 99.9\%, looks excellent, and that is exactly why accuracy is the wrong measure here (Episode 12): a model that never flags anything scores 99.85\%.
Pause and think. A colleague trains the same model on a month in which one payment in 100 was fraud. Without running anything, what is the average predicted probability on that month’s training data?
To flag more frauds, the frauds must count more in training. How can we do that? Chapter 3 starts with the most direct idea: change the data.