在固定接受率下计算坏账率
在视频中,您学习了在给定以下条件时,如何计算银行贷款组合中的坏账率(或违约百分比):
- 一个特定的模型
- 接受率
在本练习中,您将基于之前拟合的剪枝树 ptree_prior,并设定 80% 的接受率,计算银行可预期的坏账率。提醒一下,右侧已绘制出该树。
本练习是课程的一部分
R 中的信用风险建模
练习说明
- 脚本中已提供使用剪枝树与
test_set预测违约概率的代码。请记住,当您对树使用predict()函数时,违约概率在第二列。因此在predict()函数后加入了[,2]。 - 使用
prob_default_prior计算能得到 80% 接受率的阈值。可使用 quantile() 函数来完成,将第二个参数设为 0.8。将结果命名为cutoff_prior。 - 已提供生成实际二元违约预测(0 或 1)的代码。这里使用 ifelse()。将对象命名为
bin_pred_prior_80。 - 已提供根据 80% 接受率,选取
test_set中被接受贷款的违约指示的代码。 - 计算被接受贷款的违约百分比(即"坏账率")。也就是
accepted_status_prior_80中取值为1的出现次数,除以该向量的总长度。请将结果打印到您的 R 控制台。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Make predictions for the probability of default using the pruned tree and the test set.
prob_default_prior <- predict(ptree_prior, newdata = test_set)[ ,2]
# Obtain the cutoff for acceptance rate 80%
# Obtain the binary predictions.
bin_pred_prior_80 <- ifelse(prob_default_prior > cutoff_prior, 1, 0)
# Obtain the actual default status for the accepted loans
accepted_status_prior_80 <- test_set$loan_status[bin_pred_prior_80 == 0]
# Obtain the bad rate for the accepted loans