Business PD assessment

One application in, a probability of default out, with every stage of the review shown in order.

Business problem

A lender has to decide, for every business loan, how likely that borrower is to default. The file already contains the facts: revenue, profit, debt, assets, cash flow, loan size, credit score, debt-to-income, recent delinquencies, and years in business.

Reading those facts one file at a time is slow, and two reviewers can weigh the same numbers differently. The book also needs a stored score so a later question can find the latest result for a customer, an industry, or a risk cutoff.

The label used here is a teaching target: 0 means Good and 1 means Bad. A live book would use the institution’s own default policy. The risk bands below 5%, from 5% to 10%, through 20%, and above 20% are settings of this project.

How this helps

One review, every file

The same checks, ratios, and model run for each application, so the score is comparable across the book.

A number and a class

The result is a probability of default and a risk class the reviewer can read without opening the model file.

The reason stays visible

Each stage is written down: the inputs, the checks, the ratios, the model drivers, and the band the score fell into.

The book stays queryable

The customer, the loan, and the prediction are stored, so a later lookup or a plain-language question reads the same record.

Architecture

A synthetic book is cleaned, turned into ratios, split, and used to train one logistic model. Scores are stored in PostgreSQL. The API is the only door the page, the tools, and the question router use.

Synthetic book→ Clean, ratios, split→ Logistic model→ PostgreSQL→ FastAPI

This page

Takes the application you type and walks the assessment one stage at a time.

MCP tools

Six tools call the API for a customer, a score, a search, the portfolio, or the model card. They do not accept SQL.

Question router

A plain question such as “PD for B1001” or “PD above 15%” is sent to the matching API route.

Technologies

Python, pandas, NumPy

Build the 10,000-row book, check each row, and calculate the ratios.

scikit-learn

Standardize the training rows, fit logistic regression, and score a new application.

joblib

Saves model pd_model_v1 so scoring uses the same fitted pipeline.

PostgreSQL 16

Holds the customer, the loan application, and each prediction.

SQLAlchemy

Writes and reads those three tables from the API.

FastAPI

Serves this page, the score, lookups, the portfolio, and the model card.

Docker Compose

Runs the database and the API together.

MCP and the router

Lets a client ask in tools or in a sentence, through the same API.

Holdout validation

These figures are from the 2,000 businesses held out of training. The score orders risk: the observed default rate rises from the lowest decile to the highest. Predicted probabilities sit above those observed rates because training treats Good and Bad as equally common.

Predicted probability and observed default rate by score decile

Mean predicted PD Observed default rate

Source: holdout file test.csv, model pd_model_v1. Each decile has 200 businesses. Decile 1 is the lowest predicted probability. The vertical axis is a rate from 0 to 1.

Classification at a 0.50 probability cutoff

Counts on the same 2,000 holdout businesses. Good is label 0 and Bad is label 1.

How the book was built

  1. Synthetic data. 10,000 businesses, seed 42. About 23.7% are labeled Bad.
  2. Cleaning. Blank ids, duplicate ids, missing numbers, and out-of-range values are rejected. Values are not clipped.
  3. Ratios. Debt to asset, loan to revenue, profit margin, and cash-flow coverage.
  4. Split. Stratified 80/20 on the label, seed 42. Train 8,000, holdout 2,000.
  5. Training. Scaler and logistic regression fit on the training file only. Class weights are balanced.
  6. Holdout. ROC-AUC 0.884, Gini 0.767, KS 0.615. The score ranks risk. It is a ranking score from balanced training, so the probability sits above the observed default rate.
  7. Storage. Schema, then one customer, one application, and one score per business.
  8. Access. SQL reports, the API, Docker, six MCP tools, the question router, and this page.

What you will see

Enter an application, or use Higher risk and Stronger file, then choose Run assessment. The review appears in this order:

  1. Application received. The figures you entered.
  2. Data checks. Eight rules, each pass or fail.
  3. Banking ratios. The four ratios and the formula behind each one.
  4. Model score. The strongest drivers and whether each one raises or lowers default risk.
  5. Risk class. The probability and the band it falls in.
  6. Saved to the book. Customer, scored application, and prediction.
  7. Assessment. One sentence with the business, the probability, and the class.

Application

Review

The steps will appear here, one at a time.