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 02 of 09

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.

(a) Transactions per hour over the two days of the data of Dal Pozzolo et al. (2015); each red dot is one fraud. Day 1 trains the model, day 2 tests it. (b) The plain model’s scores on the test day, on a logarithmic odds axis. Bars: legitimate transactions (thousands per bin); below the axis, one red dot per fraud. Everything right of the line (p\ge0.5) is flagged. Transactions beyond the axis are counted at its edges. Computed for this tutorial, with the same data and model as the companion video.
Figure 1. (a) Transactions per hour over the two days of the data of Dal Pozzolo et al. (2015); each red dot is one fraud. Day 1 trains the model, day 2 tests it. (b) The plain model’s scores on the test day, on a logarithmic odds axis. Bars: legitimate transactions (thousands per bin); below the axis, one red dot per fraud. Everything right of the line (p\ge0.5) is flagged. Transactions beyond the axis are counted at its edges. Computed for this tutorial, with the same data and model as the companion video.

2.2One loss term per transaction

Logistic regression is trained by minimizing the log loss, a sum with one term per training transaction:

L(\theta)=-\sum_{i=1}^{n}\Bigl[y_i\log p_i+(1-y_i)\log(1-p_i)\Bigr],\qquad p_i=\sigma(\beta^\top x_i+b)=\frac{1}{1+e^{-(\beta^\top x_i+b)}}, \tag{1}

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),

\frac{\partial L}{\partial b}=\sum_{i=1}^{n}\,(p_i-y_i)=0\qquad\Longrightarrow\qquad\frac1n\sum_{i=1}^{n}p_i=\frac1n\sum_{i=1}^{n}y_i=\frac{n_+}{n}. \tag{2}

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?

Show the answer

1\%. By (2), a logistic regression with an intercept matches the average predicted probability to the fraction of positives in its training data. The scores follow the class balance the model was trained on, an observation that will matter in Chapter 5.

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.