在固定核准率下計算壞帳率
在影片中,你學到如何在給定以下條件時,計算銀行放款組合中的壞帳率(或違約百分比):
- 一個特定的模型
- 核准率
在這個練習中,你將使用先前擬合的剪枝樹 ptree_prior,並設定核准率為 80%,來計算銀行可預期的壞帳率。提醒你,右側已繪出該決策樹。
本練習屬於課程
R 的信用風險建模
練習說明
- 在腳本中,已提供使用剪枝樹與
test_set來預測違約機率的程式碼。記住,對樹模型使用predict()時,違約機率在第 2 欄。因此在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