Mô hình hóa Rủi ro Tín dụng bằng Python
Michael Crabtree
Data Scientist, Ford Motor Company
# Đặt các tỷ lệ chấp nhận cần thử
accept_rates = [1.0, 0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.65, 0.6, 0.55,
0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05]
# Tạo danh sách lưu ngưỡng và bad rate
thresholds = []
bad_rates = []
for rate in accept_rates:
# Tính ngưỡng
threshold = np.quantile(preds_df['prob_default'], rate).round(3)
# Lưu ngưỡng vào danh sách
thresholds.append(np.quantile(preds_gbt['prob_default'], rate).round(3))
# Áp dụng ngưỡng để gán lại loan_status
test_pred_df['pred_loan_status'] = \
test_pred_df['prob_default'].apply(lambda x: 1 if x > thresh else 0)
# Tạo tập khoản vay được chấp nhận (dự đoán không vỡ nợ)
accepted_loans = test_pred_df[test_pred_df['pred_loan_status'] == 0]
# Tính và lưu bad rate
bad_rates.append(np.sum((accepted_loans['true_loan_status'])
/ accepted_loans['true_loan_status'].count()).round(3))
strat_df = pd.DataFrame(zip(accept_rates, thresholds, bad_rates),
columns = ['Acceptance Rate','Threshold','Bad Rate'])
len() hoặc .count()loan_amnt trung bình từ dữ liệu testloan_amnt# Xác suất vỡ nợ (PD)
test_pred_df['prob_default']
# Dư nợ tại thời điểm vỡ nợ = số tiền vay (EAD)
test_pred_df['loan_amnt']
# Tỷ lệ tổn thất khi vỡ nợ = 1.0 nếu mất toàn bộ (LGD)
test_pred_df['loss_given_default']
Mô hình hóa Rủi ro Tín dụng bằng Python