financial-risk-api / src /datafusion_healthy_analysis.py
github-actions[bot]
Deploy a06f994: Detect other lenders in the transaction feed; measure what "looks healthy" is worth
d13dd0c
Raw History Blame Contribute Delete
2.17 kB
"""If someone LOOKS healthy across months of transactions, how often do they default?"""
import warnings, os; warnings.filterwarnings("ignore")
import numpy as np, pandas as pd, sys
sys.path.insert(0, r"C:\Users\piplu\AppData\Local\Temp\claude\c--Users-piplu-financial-risk-system\a9d077a8-2970-4cdf-96f1-f0b44768d75c\scratchpad")
from train_datafusion import build
f = build()
y = f["target"].values
print(f"population: {len(f)} clients, {y.sum()} defaults, base rate {y.mean():.2%}\n")
CASH_MCC_LOW = f["cash_ratio"] <= f["cash_ratio"].quantile(0.50)
NO_GAMBLING = f["gambling_ratio"] <= 0.001
LOW_LOAN_MCC = f["loan_mcc_ratio"] <= f["loan_mcc_ratio"].quantile(0.50)
POSITIVE_CF = f["cf_net_monthly"] > 0
STABLE_INCOME = f["income_volatility"] <= f["income_volatility"].quantile(0.50)
ALWAYS_PAID = f["months_without_income"] <= 0.0
criteria = {
"positive net cashflow": POSITIVE_CF,
"stable income": STABLE_INCOME,
"never a month without income": ALWAYS_PAID,
"low cash-advance use": CASH_MCC_LOW,
"no gambling": NO_GAMBLING,
"little other-credit spend": LOW_LOAN_MCC,
}
print("Default rate by single 'healthy' signal:")
for name, m in criteria.items():
print(f" {name:32s} n={int(m.sum()):<5} default {y[m.values].mean():.2%} "
f"(vs {y[~m.values].mean():.2%} without)")
healthy = POSITIVE_CF & STABLE_INCOME & CASH_MCC_LOW & NO_GAMBLING & LOW_LOAN_MCC
unhealthy = (~POSITIVE_CF) & (f["cash_ratio"] > f["cash_ratio"].quantile(0.75))
print(f"\nALL five healthy signals together:")
print(f" n={int(healthy.sum()):<5} default rate {y[healthy.values].mean():.2%}"
f" ({int(y[healthy.values].sum())} defaults)")
print(f" vs population base rate {y.mean():.2%}"
f" -> {y.mean()/max(1e-9,y[healthy.values].mean()):.1f}x safer")
print(f"\nClearly stressed (negative cashflow + heavy cash use):")
print(f" n={int(unhealthy.sum()):<5} default rate {y[unhealthy.values].mean():.2%}")
print(f"\nOf the {int(y.sum())} people who DID default, how many looked healthy?")
print(f" {int(y[healthy.values].sum())} of {int(y.sum())} "
f"({y[healthy.values].sum()/y.sum():.1%}) passed all five healthy checks")