machine-learning

BankruptSense: predicting corporate bankruptcy when a miss costs ten false alarms

published
reading
8 min
tags
machine-learning, credit-risk, lightgbm, imbalanced-data, case-study
On this page
  1. The problem in one number
  2. 96.8% accurate, catches nothing
  3. The best feature looked like noise
  4. Two things I refused to throw away
  5. Fighting 30 : 1 without inventing companies
  6. Eight models, one decision
  7. The threshold matters more than the model
  8. Results on the test set
  9. What the model looks at
  10. The nine I missed
  11. If this shipped tomorrow
  12. One open question

BankruptSense is an early-warning model for a bank's credit-risk team. It takes a company's financial ratios and puts it in a ranked review queue, so analysts look at the riskiest borrowers first. I built it end to end: framing, data cleaning, feature engineering, model selection, threshold, and the error analysis at the end.

Open the slide deck → It tells the same story in 14 slides, for readers who prefer the short version.

Try BankruptSense → The product is live at bankruptsense.app.

#The problem in one number

The data is the public Company Bankruptcy Prediction dataset: 6,819 companies listed in Taiwan between 1999 and 2009, each described by 95 financial ratios. 220 of them, 3.2%, went bankrupt.

Every decision in this project is a trade between two mistakes:

MistakeWhat actually happensCost (assumed)
Missed bankruptcythe loan is written off10
False alarman analyst spends an afternoon on a healthy firm1

The 10 : 1 ratio is my assumption, not something in the data. I state it up front because it drives the metric, the threshold, and the final comparison.

#96.8% accurate, catches nothing

The classes are imbalanced at about 30 : 1. On the held-out test set of 1,364 companies (44 bankrupt), a model that calls every company healthy is 96.8% accurate and catches zero bankruptcies. At 10 : 1 its cost is 440: that is the bill to beat.

Bar chart of the target variable: 6,599 healthy companies against 220 bankrupt ones.

So accuracy is out. Models are ranked on PR-AUC, where random guessing scores 0.03, and the final decision is judged on two counts a credit team understands: bankruptcies caught and false alarms raised.

#The best feature looked like noise

The model's most important feature is the interest rate a company pays on its interest-bearing debt. Its Pearson correlation with bankruptcy is −0.023, which ranks it 58th of 95. A correlation screen would have dropped it.

Split it into quintiles and the pattern is plain: bankruptcy rises from 0.5% for the cheapest debt to 6.2% for the most expensive, about 12×. The correlation hides it because a handful of values near 1e9 sit in a column whose typical value is around 0.0003. Those few rows swamp a linear correlation; ranks and tree models are not fooled.

My reading: lenders already charge more to firms they think are risky, so this column carries the market's opinion of the firm before any of my analysis.

#Two things I refused to throw away

"Dirty" dataTempting moveWhat the data saidWhat I did
131 rows with impossible values (~9.99e9) in 13 columnsdelete the rowsthese rows go bankrupt 16.8% of the time vs 3.0% for the restset the values to missing, impute the median, and count them in an n_anomalies feature
Heavy outliers (30 columns with over 10% IQR outliers)trim the outliersthe outliers are the distressed firmscap at the 1st/99th percentile for linear models only; keep every row

Every cleaning step (imputation, winsorizing, a |r| > 0.95 correlation filter) is fitted inside the cross-validation pipeline, so no statistic from a validation fold leaks into training.

I also engineered a few features from classic credit analysis: an Altman-Z proxy, a debt × loss interaction, a liquidity gap, cash-flow-to-debt, and a leverage band. Two of them ended up in the model's top four.

Distributions of the engineered features for healthy and bankrupt companies.

#Fighting 30 : 1 without inventing companies

I compared class weights (each bankrupt firm counts about 30× in the loss) with SMOTE, which creates synthetic bankrupt firms inside each fold.

ModelClass weights+ SMOTE
Logistic Regression0.3790.349
LightGBM0.4450.458

Cross-validated PR-AUC; fold-to-fold noise is about ±0.06.

The differences go both ways and are smaller than the noise, so the final model uses class weights and no synthetic data.

#Eight models, one decision

All models were compared with stratified 5-fold cross-validation on the training 80%, so every fold contains bankruptcies.

Cross-validated PR-AUC for eight models with ±1 standard deviation error bars; boosted trees lead, the decision tree baseline trails.

Boosted trees beat linear models. Within the boosted family the gaps are inside the noise, so the honest claim is "boosted trees win", not "LightGBM beats XGBoost". I picked LightGBM because it scored best and copes with 1e9-scale garbage and near-duplicate features without extra preprocessing, then tuned it with RandomizedSearchCV (CV PR-AUC 0.480).

Things I deliberately did not do: optimise accuracy, keep the 0.5 threshold, use SMOTE in the final model, delete outlier rows, look at the test set before the very end, or reach for deep learning with only 176 bankrupt firms in training.

#The threshold matters more than the model

The same tuned model catches 18 of 44 test bankruptcies at the default 0.5 threshold and 35 at 0.011.

Out-of-fold precision, recall and F2 against the decision threshold on a log scale; F2 peaks near 0.011.

I chose the threshold from out-of-fold predictions on the training data, never the test set. The F2-optimal threshold (0.011) and the threshold that minimises the 10 : 1 cost (0.009) agree. The cut-off is so low because LightGBM's scores rank well but are not calibrated probabilities, which is also why the score should never be quoted as a probability.

#Results on the test set

ROC and precision-recall curves for the logistic baseline and tuned LightGBM, and the LightGBM confusion matrix at threshold 0.01: 1,247 true negatives, 73 false alarms, 9 misses, 35 caught.

Same 1,364 companies, same 44 failuresLogistic Regression (baseline)LightGBM (final)
Bankruptcies caught36 / 44 (82%)35 / 44 (80%)
False alarms15673 (−53%)
Firms to review192108
Precision19%32%
PR-AUC0.400.57
Cost at 10 : 1236163 (−31%)

Read it as a workload. The team reviews 108 files instead of 1,364, and one in three of them is a real bankruptcy, against one in thirty at random. The one-company gap in recall is noise; the real gain is 83 healthy firms no longer pulled into review. With only 44 positives, the 80% recall has a 95% interval of roughly 65–89%.

#What the model looks at

Top 15 features by permutation importance, measured as the drop in test PR-AUC; interest rate on debt leads, followed by the engineered Altman-Z proxy, quick ratio and debt × loss.

The top four read like a path to failure:

  1. Lenders charge more: interest rate on interest-bearing debt.
  2. Overall health is weak: Altman-Z proxy (engineered).
  3. The cushion is thin: quick ratio, with a median of 0.0027 for bankrupt firms against 0.0075 for healthy ones.
  4. Debt meets losses: debt × loss (engineered), about 2× higher for bankrupt firms.

#The nine I missed

The most useful part of the project was looking at the nine bankruptcies the model did not flag. They were not near-misses: every one scored below 0.005, under half the threshold.

MedianCaught (35)Missed (9)Healthy (1,320)
Debt ratio0.2000.1320.106
Altman-Z proxy3.724.034.26
In the very-high-debt band28 of 352 of 9—

They are "quiet decliners": slightly unprofitable, ordinary debt, clean data. And 369 healthy firms, 28% of the test set, share that profile. Lowering the threshold to catch them would bury the review team. If these nine can be caught at all, it will take new information, such as market prices, news, or group structure, not a better algorithm.

Catching more has a steep price on this test set: 41 of 44 costs 169 false alarms, and all 44 costs 373.

#If this shipped tomorrow

  • Threshold 0.011, which puts about 8% of the book in review each quarter.
  • Watch the flag rate. A sudden jump in the share of firms flagged means the data has drifted, not that the economy collapsed overnight.
  • Record every outcome, for flags and misses, and retrain yearly.
  • Residual risk: about one bankruptcy in five stays unflagged, the test set has only 44 failures, and the data is Taiwan 1999–2009 under that era's listing rules.

That last point is the gap between this case study and a product. A model that a lender can use needs ratios computed from today's filings in the lender's own market, and outcomes from that market to retrain on. That is the work I am doing next, as BankruptSense the product.

#One open question

The strongest signal was the interest rate lenders were already charging. The firms I missed were charged less than the ones I caught, so the lenders apparently did not see them coming either. Can any model built only on annual ratios beat the market's own price of risk, or is about 80% recall the ceiling for this kind of data?

Share

Related