Classic design · Mid
Fraud detection
Classic interview design. Key points: extreme imbalance, delayed labels, and cost-sensitive thresholds.
Short answer
Classic interview design. Key points: extreme imbalance, delayed labels, and cost-sensitive thresholds. Given a set of credit card purchases in which each record is labeled as fraudulent or safe, how do you build a fraud detection algorithm? This is the classic formulation of the exercise, and the description of the course that covers it sums up the goal as identifying…
01 · CONTEXT
An interview question that looks simple
Given a set of credit card purchases in which each record is labeled as fraudulent or safe, how do you build a fraud detection algorithm? This is the classic formulation of the exercise, and the description of the course that covers it sums up the goal as identifying unusual or suspicious transactions in financial data to prevent fraudulent activity. It is a good starting point for thinking about production ML systems.
Many questions on this list are ambiguous on purpose: it is up to the person answering to ask clarifying questions and bound the scope before designing.
02 · IMBALANCE
Fraud is rare
In real payment data, fraud is rare: on the order of one in a thousand payments, according to Stripe's fraud team. In a credit card transaction dataset, fraudulent purchases can make up less than 0.1 percent of the examples, according to Google's machine learning crash course.
This imbalance hurts training: a severely imbalanced dataset may not contain enough examples of the minority class for adequate training. It also hurts evaluation, since accuracy is usually a poor metric for evaluating a model trained on imbalanced data.
Standard training mixes two goals: learning what each class looks like and learning how common each class is. The two-step technique of downsampling and upweighting the majority class separates these two goals.
| Criterion | Without rebalancing | Downsample and upweight |
|---|---|---|
| Rare class in each batch | A severely imbalanced dataset may not have enough examples of the minority class | Greatly increases the chance that each batch has enough examples of the minority class |
| What needs care | Accuracy is usually a poor metric for evaluating the model | Introduces prediction bias, corrected by upweighting the majority class by the downsampling factor |
03 · DELAYED LABELS
The label arrives months later
Fraud detection is an example of a task with long feedback loops: for a period after the transaction, the user can dispute whether it was fraudulent or not.
A typical dispute window runs from one month to three months. Once the window passes without a dispute from the user, the transaction can be presumed legitimate.
This weighs on monitoring: if there is a problem in the fraud model and it takes months to be noticed, the fraud the model let through may have driven a small business into bankruptcy. When the true label is not available or arrives too late to be useful, other distributions of interest can be monitored.
04 · COST-SENSITIVE THRESHOLD
What a mistake costs
Stripe Radar needs to balance blocking bad transactions against false positives, that is, good payments blocked, which hurt consumers and the results of Stripe's users.
A credit example shows how cost enters the equation: in the German credit dataset used by scikit-learn, classifying a bad credit as good is, on average, five times more costly than the opposite.
One solution described by scikit-learn is to tune the classifier's decision threshold after the model has been trained. The optimal threshold is chosen to maximize a given metric; in the documentation's example, the tuned threshold optimizes a utility metric defined by the business. The same documentation warns against using the same data to train the classifier and to tune the threshold, because of the risk of overfitting.
05 · THE ARCHITECTURE
Fast decision, slow learning
The diagram below brings together the three themes: the decision path, the label loop, and threshold tuning. Events go through a message queue before reaching the transaction log.
In the diagram, the buyer reaches the payments API, which is connected to the scoring service.
In the diagram, the scoring service queries the online feature store. For reference, Stripe Radar makes the fraud decision in under one hundred milliseconds.
The decision policy threshold is tuned after the model is trained, to maximize a metric; in the documentation's example, a utility metric defined by the business.
In the diagram, the API publishes each transaction and the decision to an event queue, which feeds the transaction log.
For a period after the transaction the customer can dispute it, and a typical window runs from one month to three months. Once the window passes without a dispute, the transaction can be presumed legitimate.
In the diagram, the label join feeds training, which publishes the model to the scoring service and the threshold to the decision policy. The documentation warns against using the same data to train the classifier and tune the threshold, because of the risk of overfitting.
06 · FEATURES
Real time or near real time
Real-time features are computed when the prediction request arrives, and the computation latency adds directly to the latency the user sees. Near-real-time features are precomputed with a stream processing engine; because they are computed asynchronously, their latency does not add to the user's.
Near-real-time features are computed with a stream processing engine, here fed by the event queue.
Because these features are computed asynchronously, the computation latency does not add to the latency the user sees.
In the diagram, a batch job reads the historical log and writes to the same online store.
In the diagram, the scoring service reads the ready-made features from the store. Real-time features, by contrast, are computed when the prediction request arrives, and the computation latency adds directly to the latency the user sees.
Frequently asked questions
What is Fraud detection?
Classic interview design. Key points: extreme imbalance, delayed labels, and cost-sensitive thresholds.
What does the Fraud detection design cover?
It is organized in these sections: An interview question that looks simple, Fraud is rare, The label arrives months later, What a mistake costs, Fast decision, slow learning, and Real time or near real time.
How long is Fraud detection and what level is it?
It takes about 14 minutes to read and is rated Mid level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.