FinRisk: predicting loan default and serving it in under 100 ms
An end-to-end credit-risk pipeline: ETL into SQLite, a Random Forest and an XGBoost model predicting loan default compared in MLflow, rule-based fraud flags, and a FastAPI service with REST and GraphQL endpoints behind a Chart.js dashboard.
- Result
- 93.4% precision on declined loans, scored in 67 ms
- Built for
- A lender deciding which applications to decline outright and which to approve or review.
- Role
- Sole developer: design, build, tests and deployment
- When
- Oct 2025 – Dec 2025
How it works
- A seeded generator creates 12,500 customers, 10,586 loan applications and about 344,000 card transactions
- Each loan's default is drawn from a probability driven by credit score, late-payment history, utilization, DTI, income and tenure
- Random Forest and XGBoost are trained on 80% of loans and compared on the held-out 20%; both runs are logged to MLflow
- FastAPI serves POST /api/predict with the model loaded once and cached, plus REST KPIs and a Strawberry GraphQL layer
Data
Fully synthetic (fixed seed), with a 23.4% default rate similar to a subprime lender's book. No real customer data.
Problems found and decisions made
- The first version predicted the approve/reject decision, which a fixed rule computes from the same features, and scored a meaningless 100%
- The model now predicts actual default, and neither the underwriting decision nor the simulator's true probability is used as a feature.
- Reloading the model from disk on every request made latency depend on model size
- The model is loaded once and cached, giving a median of 67 ms per score (95th percentile 85 ms).
- A single accuracy number hides the business trade-off
- Precision and recall are reported at an explicit decline threshold (80% predicted default probability).
Testing and reliability
- CI trains both models, checks that MLflow logged both runs, boots the API and calls its REST and GraphQL endpoints
- Tests check that a risky applicant is declined and a safe one approved, and fail if median scoring latency reaches 100 ms
Results
- Random Forest: ROC AUC 0.92 and 93.4% precision when declining scores of 80% or more (34% of defaults caught, 8.6% of applications declined)
- XGBoost: 91.3% precision with 44.6% recall at the same threshold
- Median scoring latency 67 ms
Limits and next steps
- Synthetic data: the effects are built into the generator, so the model recovers relationships that were put there
- The decline threshold is fixed; a production system would set it from loss and approval-rate targets