Someplace in the midst of my final-year challenge, I skilled six totally different fashions utilizing the identical fraud dataset, logged each coaching run, and picked a winner. Proper now, the mannequin that powers the present deployment will not be the successful mannequin.
NairaShield is an AI-based fraud detection system that I constructed as my remaining yr challenge for banking transactions. It began as a simple classification downside and ended up as one thing nearer to a decision-support pipeline, partly due to that mismatch, and partly as a result of my supervisor wasn’t glad with the simple model.
He actually favored the challenge, however there was one little factor; he had seen an anti-money laundering challenge by one in all my classmates and felt that the 2 had been too related.
I attempted so onerous to clarify that the modelling downside was totally different, however he nonetheless wasn’t satisfied.
We had fairly just a few arguments over this matter earlier than he advised me what I wanted to do to resolve the issue at hand. And that single notice is the rationale the system ended up with a full regulatory overview workflow and a second mannequin household it would not in any other case have had. It is also most of what this piece is definitely about.
That is the very first thing I’ve correctly written since I left college just a few weeks in the past, capping off a two-month stretch away from writing whereas coursework and this challenge ate up every part else, so it felt proper to come back again with the challenge that has essentially the most to really present.
Merging two datasets that do not agree with one another
I skilled on two public datasets that haven’t any frequent schema. PaySim is a simulation of cellular cash transactions and has attributes like oldbalanceOrg and newbalanceOrig. However, the IEEE-CIS fraud detection dataset covers card transactions and has none of that; as a substitute, it has solely ProductCD, card1, card2, and a protracted tail of different anonymized options. Earlier than any of that merging truly occurs, every dataset will get validated towards its personal schema first, so a malformed row fails loudly as a substitute of quietly poisoning the coaching set:
As soon as each side cross validation, mapping them into one shared construction appears to be like like this:
What took me manner too lengthy to appreciate was that having a unified schema meant the chosen options must be ones each datasets shared.
The paySim balances did not make the lower as a result of IEEE-CIS does not have an analogous subject. The precise deployed mannequin is skilled utilizing three issues: the quantity of transaction, one-hot encoding of the channel, and one-hot encoding of the supply dataset.
I am flagging that now as a result of it issues so much later on this piece, and ignoring it will undercut an excellent factor I’m attempting to say right here.
Balancing a dataset the place fraud barely exhibits up
Since there may be little or no fraud in each information sources, a mannequin will be capable to obtain excessive accuracy just by predicting “no fraud” a number of instances whereas not predicting many fraud cases.
I used SMOTE to oversample the minority class on the coaching break up solely, with a fallback to a hand-written interpolation model in case imbalanced-learn wasn’t accessible within the deployment surroundings:
The zero dependency fallback makes use of interpolation between a pattern and its closest neighbors via Euclidean distance to provide artificial minority samples, which is actually an easier model of how SMOTE works internally however left within the code for the sake of stopping failure when in a minimalistic setting.
Six fashions and a spreadsheet that argues with itself
I skilled Random Forest, Logistic Regression, XGBoost (each baseline and optimized variations), and LightGBM (once more, each baseline and optimized). Each one in all them ran via the identical analysis helper, so the numbers would truly be comparable as a substitute of six barely totally different measurement approaches carrying one outcome:
Each run, tuned or not, will get logged to experiment_runs.json with its personal hyperparameters and metrics, which is the one purpose it was attainable to note, months later, that the mannequin in manufacturing wasn’t the one on the prime of that log:

Just a few issues which can be value contemplating relatively than skipping over. Logistic Regression exhibits the best worth of recall amongst all fashions at 0.95.
However do not simply have a look at that; it additionally proves to be the least usable mannequin of the six, as its low precision worth of 0.14 implies that most of its recognized instances of fraud are false positives.
Tuning XGBoost improved AUC-PR barely however truly made precision worse than the baseline, which is a reminder that hyperparameter search optimizes no matter metric you level it at, not essentially the one that really issues as soon as the mannequin is making actual choices.
Lastly, the baseline settings for LightGBM proved to be extra environment friendly when it comes to AUC-PR than optimized ones and had been the very best amongst all different fashions within the take a look at.
You possibly can at all times look again as much as the diagram above to see what I am speaking about.
The manufacturing API nonetheless makes use of XGBoost (Tuned) as an energetic mannequin, as this alternative was made earlier than the comparability of LightGBM fashions was accomplished.
Which, while you have a look at it now, is truthfully value revisiting.
SHAP is what makes any of those numbers accountable to a human. Each flagged prediction comes with a listing of options of what pushed it towards fraud, since “the mannequin mentioned so” is not a solution a financial institution or a regulator ought to settle for:
What my supervisor truly wished
The anti-money laundering challenge by a colleague of mine from the start of this piece is value returning to, as a result of the similarity my supervisor noticed wasn’t imagined. On the floor, his work and mine had been doing the identical fundamental job, each taking transaction information and deciding whether or not it regarded suspicious, and my supervisor wasn’t incorrect to note that.
What he was truly pushing me to construct wasn’t one other algorithm.
No.
He was pointing me towards the step that comes after a transaction will get flagged, the half the place one thing truly occurs subsequent. From what I discovered digging into different educational AML work, most of it stops proper at classification. His remark was what received me fascinated about what follows after the flag.
This led to the creation of a Regulatory Notification Heart; a overview layer constructed round position sorts modeled on Nigeria’s Central Financial institution, the EFCC, and NDIC, every having a separate login and their very own queue of flagged transactions.
A regulatory reviewer can approve, ask for OTP verification, or block it outright, and every determination will probably be documented within the audit log with who acted and when. Constructing that workflow can also be what pressured me to incorporate LightGBM within the comparability.
The place the boldness gate truly comes from
The API does not deal with a fraud prediction as a single yes-or-no. The bottom classification threshold sits at 0.50, however what occurs after that’s break up into bands:
Something that scores lower than 0.50 passes with none alarm in any respect going off. If the rating falls between 0.50 and 0.80, then the transaction turns into flagged however will not be stopped; it is routed to PENDING_OTP, which mainly implies that a human or a secondary verification step will get concerned earlier than something remaining occurs.
Scores from 0.80 and above block the transaction from taking place, whereas at 0.85 a right away alarm is shipped both by way of SMS or e-mail. The background thread sends it instantly with out ready for any request-response cycle.

···
Protection day
The panel began with the same old, or would I say anticipated, set of questions. Which fashions I used, what hyperparameters I tuned and the way I arrived at them, the place the datasets got here from, and whether or not they had been consultant sufficient of transaction conduct particularly.
I advised them XGBoost (Tuned), as a result of that is what was truly in manufacturing. What I ignored, largely as a result of I hadn’t sat with it correctly myself but, was that LightGBM’s baseline had already overwhelmed it on the metric that mattered most, days earlier than I stood in that room. No person adopted up on it. I am the one following up on it now.
Then it moved to the one I had probably not ready for: what’s particular about this fraud detection resolution in comparison with all of the others that exist already, and what issues with different options does it resolve?
The situation questions got here subsequent.
If the CEO of an enormous firm decides one random afternoon to maneuver one million {dollars} in a single transaction, would it not get flagged?
And if a pupil tried the identical transaction, what would occur?
I advised them the system research patterns and conduct, which means it will deal with these two transactions in a different way relying on who was making them.
Penning this months later, with the code in entrance of me as a substitute of a panel throughout the desk, I wish to give the extra correct reply as a substitute of the extra snug one.
With the system as it’s truly constructed now, these two transactions would obtain an equivalent prediction. There is not any account id or transaction historical past within the 13 options on which the mannequin learns to take a look at how a lot cash modified arms, via what channel, and the way related the transaction sample is to both of the 2 supply datasets.
The sincere reply is that my system at the moment treats one million {dollars} the identical no matter whose account it left. It sees one million {dollars}, and that is all, and it does nothing with that data besides deal with it like some other transaction.
The factor it does, although, is that it does not depend on that quantity alone to decide. In my system, if a million-dollar transaction is predicted to be fraud at a chance of 0.85, it’s flagged, escalated, and within the regulatory workflow, is shipped to a human for overview. That human could have extra context to work with than the mannequin.
The boldness gate is doing the job that account-level reasoning would do if it existed but. It is an actual reply, only a extra modest one than the reply I gave that day.
Last ideas and what I would truly do in a different way
That leads us straight to the subsequent step, which is the one the protection query revealed: including behavioral options per account, averaging transactions, and deviation from historic person quantities. With time, the mannequin ought to then be capable to make the excellence I spoke of, relatively than approximating it via the boldness gate.
Moreover, I might revisit defaulting to XGBoost (Tuned) because the manufacturing mannequin given LightGBM’s baseline configuration scored higher on the metric that issues most for this downside.
Each of these come from the identical mistake: I locked in a manufacturing mannequin earlier than the comparability was even completed, and solely actually observed whereas scripting this down.
If there’s one factor value taking from all of it, it is that the very best rating on a spreadsheet is not the identical as the appropriate manufacturing determination, and a mannequin was by no means going to be the entire system by itself. The boldness gate, the audit log, the human studying a PENDING_OTP alert, that is doing as a lot of the actual fraud-catching because the mannequin is.
I am again to writing recurrently after two months away, and this felt like the appropriate challenge to be sincere about first. There’s so much I have not coated right here: the RBAC setup, the audit logging, the regulatory notification pipeline itself; that is in all probability its personal piece as soon as this one’s out on the planet.
···
Earlier than you go!
I write about the actual, messy engineering behind AI techniques, the place abstractions assist, the place they damage, and what it takes to construct reliably.
You possibly can subscribe to my e-newsletter if you would like extra of that.
Join With Me















