Real-time card fraud scoring with the parts that matter after launch: point-in-time features with no train/serve skew, alerts ranked by expected dollar loss, per-decision explanations, drift and label monitoring, and a champion/challenger retrain when a new fraud pattern appears. Serving takes about 0.06 ms per transaction in-process and follows the SageMaker container contract. AWS infrastructure is in Terraform.
Transactions are synthetic (
fraudscore generate): 453,440 transactions on 5,000 cards over 60 days, 0.42% fraud. The data includes legitimate behavior that looks suspicious: trips, new phones, big-ticket purchases, bursts of parking charges. From day 50 a new fraud pattern appears that the first model never saw.
From fraudscore train (reports/experiment.json). Analysts can review 0.5% of transactions, and alerts go to the highest expected loss (probability × amount).
1. Validation (days 40–50, model trained on days 0–40)
| Model | PR-AUC | Fraud transactions caught | Fraud dollars caught | Net savings* |
|---|---|---|---|---|
| Logistic regression | 0.938 | 48.0% | 99.0% | $86,173 |
| LightGBM (v1) | 0.908 | 96.5% | 95.6% | $83,216 |
*Fraud dollars caught minus $5 analyst cost per alert.
Logistic regression won on PR-AUC and dollars; LightGBM caught twice as many fraudulent transactions with the same review capacity. I shipped LightGBM because each missed fraud is also a customer who has to dispute a charge, and because LightGBM gives exact per-decision feature contributions (SHAP) for the analyst queue. It was a judgment call, and the table shows the cost of it.
2. A new pattern appears (days 50–55): gift-card cash-outs from the cardholder's own device
| Monitoring signal | Result |
|---|---|
| Input drift (PSI on every feature and on the score) | all under 0.005, "stable" (missed it) |
| Recall on delayed chargeback labels | 96.5% → 41.1% (alert fired) |
| v1 recall on the new pattern | 19.3% |
3. Retrain and champion/challenger (days 55–60, same 0.5% capacity)
| v1 (champion) | v2 (retrained through day 55) | |
|---|---|---|
| PR-AUC | 0.424 | 0.982 |
| Fraud dollars caught | 52.4% | 76.5% |
| Net savings | $73,093 | $107,086 → promoted |
The fraud rate in this window (about 1%) is above the review capacity, so both models are capacity-bound. The question is where they spend it. v2 spends it on dollars, which leaves low-value card testing to a rule: 100% precision, 61% recall, flagging 0.08% of traffic for step-up authentication. The full story is written up as a blameless postmortem.
Serving latency (features + LightGBM + decision, one transaction at a time, in-process): p50 0.058 ms, p99 0.124 ms over 20,000 transactions (fraudscore bench). HTTP and network add to that in production.
flowchart LR
T[transaction] --> F[FeatureStore<br/>per-card state: windows, Welford stats,<br/>new device/merchant/city, geo speed]
F --> M[LightGBM<br/>probability]
M --> D{decision}
D -->|"card-testing rule"| SU[step-up auth]
D -->|"p × amount ≥ threshold"| RV[analyst review<br/>+ top SHAP reasons]
D --> AP[approve]
D -.->|after the decision| U[update card state]
subgraph offline[same code, offline]
R[replay history in time order] --> TR[train · validate at a review budget] --> CC[champion/challenger] --> REG[(model.txt + threshold)]
end
subgraph monitor[monitoring]
P[PSI on features and score] & L[recall on chargeback labels]
end
- No train/serve skew by construction. Training features come from replaying history through the same
FeatureStorethat serves live traffic, and a test asserts they are identical row by row. Features never see the transaction being scored or anything later. - Explanations. Every review alert carries its top contributing features (LightGBM
pred_contrib), e.g.new_device,speed_kmh,amt_z. - Deployment. The container answers
GET /pingandPOST /invocationson :8080, as SageMaker expects. Terraform creates an encrypted, versioned S3 bucket, an immutable ECR repo, a least-privilege execution role, an endpoint config that samples 20% of traffic to S3 (input for drift monitoring and the next training set), and CloudWatch alarms on p99 latency and 5xx errors.
git clone https://github.com/rakeshsaineni/realtime-fraud-scoring && cd realtime-fraud-scoring
python -m venv .venv && source .venv/bin/activate
make install
make train # generate data, run the v1 -> drift -> v2 experiment, save the promoted model
make bench # latency
make serve # POST /score, POST /invocations, GET /metricsMLflow tracking is optional: runs are logged to artifacts/mlflow.db when the mlflow extra is installed.
- Rank by expected loss, not probability. With fixed analyst capacity, a probable $2,000 fraud matters more than a probable $3 one. This one change is most of v2's net-savings gain.
- Monitor labels, not just inputs. PSI is cheap and catches pipeline breaks and population shifts, but it was blind to this attack. Delayed-label recall is what caught it, and it is why the label delay (chargebacks) is the number to shrink.
- Counters are excluded from drift alerts.
card_historygrows by design. My first PSI run flagged it as "act", which was pure noise. - Rules still have a job. Card testing is low-dollar per charge but means a compromised card. A narrow, high-precision rule handles it better than a model optimized for dollars.
- The data is synthetic. A real deployment needs the label-delay distribution, chargeback reason codes and the true review cost.
- State is in-process. In production, per-card state goes to Redis or DynamoDB behind the same
FeatureStoreinterface, with Kafka or Kinesis as the event source. - Add probability calibration (isotonic) so expected-loss thresholds mean real dollars, and a shadow-mode stage before promotion.
src/fraudscore/ synth · features (online store) · model · monitor (PSI) · experiment (v1 → drift → v2) · serve (API + rules) · cli
infra/terraform S3, ECR, IAM, SageMaker model/endpoint with data capture, CloudWatch alarms
docs/ postmortem
tests/ train/serve parity, point-in-time correctness, PSI, budget ranking, rules, end-to-end API