In 30 seconds
- My first Random Forest caught 99.5% of frauds. It was cheating: four balance features leaked the answer.
- After removing them it caught 85% of frauds at 78% precision, still far ahead of a rule that caught 16 of 8,213.
- A result that looks too good is a bug until proven otherwise.
A payments platform had a fraud rule in place. When I checked it against the data, it had flagged 16 of the 8,213 frauds in 6.36 million payments. That's about 0.2%. Almost everything got through.
SQL before any model#
Before training anything, I loaded every row into a SQL Server star schema and asked simple questions in T-SQL. Three answers shaped the whole project:
- All fraud sits in two transaction types. TRANSFER and CASH_OUT carry 100% of it. Three other types have none.
- Fraud has no quiet hour. Morning, afternoon, evening and night are almost flat, so monitoring has to run around the clock.
- Fraud is high-value. The average fraud is 1.46 million, and one payment in every 775 is fraud.
That first finding is worth more on day one than any model: a team can start monitoring high-value TRANSFER and CASH_OUT payments tomorrow.
The result that looked perfect#
I trained three models (Logistic Regression, Random Forest and XGBoost) on an 80/20 split, balancing only the training set with SMOTE. The first Random Forest caught 99.5% of frauds with 95.7% precision.
On a fraud problem, a number like that should make you suspicious, not happy.
What SHAP showed me#
I had added SHAP to explain the model to stakeholders. It ended up explaining the model's mistake to me. The model leaned on four balance features. In PaySim, the simulator behind this data, a fraudulent account's balance is often reset to zero whatever the amount. That's a quirk of the simulator, not how real fraud behaves. The model wasn't learning fraud. It was reading the answer.
What I reported instead#
I removed the four features, retrained, and reported the lower numbers: 84.97% recall and 78.47% precision. On the 1,643 frauds in the test set, that means:
Logistic Regression looked better on recall (97.9%), but its precision was 3.67%. In practice that's roughly 42,200 false alarms for analysts to clear. Percentages hide that cost; counts don't.
XGBoost scored even higher than the original Random Forest. I didn't call it the champion, because it hasn't had the same leakage check yet. Until it has, I can't defend its number.
Why the lower number is the better one#
A 99% model built on a simulator quirk would fall apart on real payments, and a manager would have planned around a number that was never real. 85% is a number I can explain line by line, show the SQL behind, and stand next to in a review. That's the habit I want to bring to any team: check the result hardest when it's the one you were hoping for.
Monitor high-value TRANSFER and CASH_OUT payments now. Pilot model scoring with risk-tier checks next, and run the leakage check on XGBoost before trusting it.
Sources in the repo
02_SQL_Analytics/03_Fraud_Analytics.sqlFraud KPIs in T-SQL: 8,213 frauds, 16 caught by the rule04_Machine_Learning/notebooks/06_Model_Evaluation_Results.mdModel comparison: precision, recall, F1, ROC-AUC04_Machine_Learning/notebooks/06B_Leakage_Sensitivity_Check.ipynbThe leakage check: retraining without the four balance features04_Machine_Learning/notebooks/08_Model_Explainability_SHAP_Results.mdSHAP results that exposed the leak