fraud-v1: card fraud scoring model (ONNX)
A small gradient-boosted trees model that scores card transactions for fraud. It is the model behind fraud-scoring-stream, a Java 25 Kafka Streams service that scores each payment in process with ONNX Runtime.
The training data is synthetic. No real cardholder or transaction data was used. The model is a working part of a reference pipeline, not a model to put in front of real payments.
| Model | scikit-learn GradientBoostingClassifier, 200 trees of depth 3 |
| Format | ONNX, opset 17 plus ai.onnx.ml 1 (exported with skl2onnx 1.20.0) |
| Input | features: float32, shape [batch, 9] |
| Outputs | label (int64) and probabilities (float32, [batch, 2]); the fraud score is column 1 |
| Size | 110 KB |
| SHA-256 | 431f3fdd794cc24545c2c5029a60254f60f99326e59c9e8951c3c961ea7d61d6 |
| Source | trained by model/train.py at commit 01bafb1 |
Intended use
- Learning and testing streaming fraud scoring: feature contracts, training/serving parity, thresholds, fail-safes and decision metrics.
- A known-good model to run the fraud-scoring-stream service and its tests against.
Not for: real payment decisions, or any conclusion about real fraud patterns.
Features
The model expects these 9 features, in this order. The same list is stored in the model's ONNX
metadata (features), and the service refuses to start with a model whose list doesn't match its
code.
| # | Feature | Meaning |
|---|---|---|
| 0 | amount_log |
ln(1 + amount) |
| 1 | txn_count_1h |
the card's earlier transactions in the last hour |
| 2 | amount_sum_1h |
total amount of those transactions |
| 3 | txn_count_24h |
the card's earlier transactions in the last 24 hours |
| 4 | amount_ratio_24h |
amount divided by the card's average amount in the last 24 hours (1.0 if none) |
| 5 | foreign |
1 if the transaction country differs from the card's country |
| 6 | night |
1 if the transaction is between 00:00 and 05:59 UTC |
| 7 | risky_mcc |
1 if the merchant category is electronics, gift cards, crypto or jewelry |
| 8 | seconds_since_last |
seconds since the card's previous transaction, capped at 86,400 (86,400 if none in 24 hours) |
The exact definitions are in
FeatureExtractor.java
and model/train.py, which must agree (see Parity below).
Decisions and thresholds
The score becomes one of three decisions. The thresholds are stored in the model's metadata, so a new policy means a new model version, not a code change.
| Decision | Rule | How the threshold was chosen |
|---|---|---|
| Decline | score โฅ 0.664745 (decline_threshold) |
lowest score with at least 90% precision on held-out data |
| Review | score โฅ 0.020290 (review_threshold) |
the top 1% of traffic, about what an analyst team can work |
| Approve | below both |
If the model fails at runtime, the service sends the payment to review instead of approving or declining it.
Training data
Synthetic: 219,553 transactions on 1,500 cards over 30 days, 1,024 of them fraudulent (0.47%). Legitimate behaviour includes things that look risky (trips abroad, night owls, shopping sprees, electronics). Fraud includes things that look normal (daytime and domestic takeovers) and card testing (tiny payments minutes apart before larger ones).
The model trains on the first 24 days and is tested on the last 6, so it is always scored on transactions later than any it learned from.
Results (test period: 43,911 transactions, 208 fraudulent)
| Metric | Value |
|---|---|
| ROC AUC | 0.9925 |
| Average precision | 0.7730 |
| Fraud caught at review or above (recall) | 85.10% |
| Precision at review or above | 40.14% |
| Fraud declined automatically (recall) | 64.90% |
| Share of traffic sent to review or decline | 1.00% |
These numbers describe the synthetic data. They show the pipeline works; they say nothing about how the model would do on real fraud.
Use it
Python:
import numpy as np
import onnxruntime as ort
from huggingface_hub import hf_hub_download
path = hf_hub_download("MuhFawad1/fraud-scoring-model", "fraud-v1.onnx")
session = ort.InferenceSession(path)
meta = session.get_modelmeta().custom_metadata_map
print(meta["features"].split(","))
# a $65 domestic daytime purchase; 3 earlier ones today, the last 90 minutes ago
features = np.array([[np.log1p(65.0), 0, 0.0, 3, 0.9, 0, 0, 0, 5400.0]], dtype=np.float32)
score = session.run(["probabilities"], {"features": features})[0][0, 1]
if score >= float(meta["decline_threshold"]):
decision = "decline"
elif score >= float(meta["review_threshold"]):
decision = "review"
else:
decision = "approve"
print(round(float(score), 4), decision)
Java service: set MODEL_PATH to the downloaded file. See the
fraud-scoring-stream README.
Parity
parity.jsonl holds 2,189 events from training (33 of them fraudulent), each with the features and score training
computed. The service's ParityTest replays them through the Java feature code and this model;
features must match to 1e-6 (relative) and scores to 1e-5. To check the file you downloaded:
pip install onnxruntime numpy huggingface_hub
python verify_parity.py
Limits
- Synthetic data teaches the injected patterns, not real fraud.
- No fairness or bias analysis was done, because there are no real people in the data.
- Real use would need real labelled data, a review of the features for regulatory fit, drift monitoring and regular retraining.
License
MIT, the same as the source repository.