Credit Risk Modelling | Credit Default Prediction

2026
credit-risk machine-learning python synthetic-data logistic-regression scikit-learn mysql classification imbalanced-data

Most credit-risk projects use Kaggle datasets that have already been cleaned and
packaged. I wanted to build the whole pipeline myself: generate the data, engineer
the features, train the model, and ship the interface.

The result is a Django application that lets you enter a hypothetical borrower's
profile and returns a probability of default, a risk band, the factors that drove
the prediction, and a simulated repayment schedule. Behind it sits a machine
learning model trained on 5,049 synthetic loans, of which 930 defaulted.

Real lending data is confidential. So I wrote a generator that builds a synthetic
lending portfolio customer by customer.

Each customer gets a coherent story: demographic profile, employment history,
income trajectory, credit accounts, financial transactions, borrowing behaviour,
repayment behaviour. Everything downstream is derived from that same profile, so
the relationships hold up. Income determines loan size. Employment stability
affects repayment. Defaults correlate with debt-service ratios.

Final dataset:
  · 18,000 customers
  · 17,300 employment records
  · 2 million income-history entries
  · 9 million financial transactions
  · 27,000 loan applications
  · 5,049 funded loans
  · 930 defaults (18.4%)

The training CSV is 5,049 rows, one per funded loan, with 30 features per
application.

Getting the label to be learnable.
The first model hit 0.56 AUC. The problem was that the default formula leaned
heavily on hidden risk variables the model would never see. I had to redesign the
formula so observable features (debt-to-income, payment-to-income, income level,
employment status) dominated the signal. That single change pushed AUC to 0.74.

Time anchoring.
Birth dates were originally computed backwards from the observation end date.
Once the data window expanded, that meant customers were applying for loans at
15 years old. Fixed by anchoring every derived date to the customer's registration
date instead.

Signal versus noise.
The synthetic label needs randomness or the problem is trivially clean. But too
much randomness and no model can learn it. Tuning the noise standard deviation
from 0.35 down to 0.15 was the difference between a useless model and a usable one.

Class imbalance.
Defaults are the minority class. Accuracy is meaningless here, so I tracked
PR-AUC and recall on the default class alongside ROC-AUC.

1. Generate the dataset
   python scripts/generate_credit_risk_data.py
   Builds 18,000 customers and everything downstream in about 30 minutes.

2. Validate
   python scripts/validate_credit_risk_data.py
   35 integrity checks across record counts, foreign keys, date consistency,
   affordability bounds, and default behaviour.

3. Extract training data
   python scripts/prepare_ml_data.py
   Flattens the database into a single CSV at application level.

4. Train and compare models
   python ml/src/train.py
   Trains Logistic Regression, Random Forest, and XGBoost on the same split.
   Saves the best one.

5. Generate explainability plots
   python ml/src/generate_explainability.py
   ROC, PR, calibration, confusion matrix, and SHAP beeswarm.

6. Run the site
   python manage.py runserver
   Enter a borrower's details and get a risk score.

The label has to be learnable from the features.
If the outcome depends on things the model can't see, no algorithm can fix it.
The single biggest improvement in this project came from changing how the labels
were generated, not from changing the model.

On small datasets, simple beats complex.
Logistic Regression matched Random Forest on ROC-AUC and beat it on PR-AUC.
XGBoost under-performed both. With 4,000 training rows, a linear model with
regularisation is hard to beat.

Interpretability is worth more than a fraction of a point.
The difference between AUC 0.74 and 0.78 is invisible to a reviewer. Being able
to say "this applicant was flagged because of their debt-to-income ratio" is not.

Synthetic data is a design problem.
Making relationships coherent customer-by-customer is much more work than
randomising tables independently, but it is the difference between a dataset a
model can learn from and one it cannot.