O teste de stress para a PD com sensibilidade à fatores macroeconômicos é realizado nessa modelagem, levando-se em conta flutuações e Months on Book.
Ref: https://github.com/KhalilBelghouat/StressTestingLoanPortfolio
# SUPPLEMENTARY CODE FOR THE MASTER PROJECT:
# Stress Testing: Predicting Loss under Adverse Economic Conditions
# Load required libraries
library(readxl) # For reading Excel files
library(dplyr) # For data manipulation
library(ggplot2) # For plotting
library(stats) # For generalized linear models (glm)
# Define file path
path <- "C:\\Users\\Lenovo\\Desktop\\Desktop\\StressTesting\\StressTestingLoanPortfolio-main\\StressTestingLoanPortfolio-main\\data\\"
# Read credit portfolio
myPortfolio <- read_excel(paste0(path, "OutstandingLoans.xlsx"))
# Read historical loans
myLoanHistory <- read_excel(paste0(path, "OldLoans.xlsx"))
# Read economic and financial data from the Fed
FedHistory <- read.csv(paste0(path, "Simplified_FedHistory.csv"))
# Combine both datasets
combinedHistory <- merge(myLoanHistory, FedHistory, by = "Year")
# Model building
model <- glm(Default ~ RiskLevel + YOB + DJX_Return + GDP,
data = combinedHistory,
family = binomial)
# Model fitting and display results
summary(model)
# Estimated default probabilities
predictions <- predict(model, type = "response")
# Compute historical portfolio default rates
Default <- combinedHistory %>%
group_by(YOB) %>%
summarise(DefaultRate = sum(Default) / n() * 100)
# Dataframe containing years on books and predictive model probabilities
myModel <- data.frame(YOB = combinedHistory$YOB, estDefault = predictions)
# Compute estimated default rates
estDefault <- myModel %>%
group_by(YOB) %>%
summarise(estDefaultRate = mean(estDefault) * 100)
# Adverse economic scenario
AdverseScenario <- read.csv(paste0(path, "Simplified_Adverse.csv"))
# Portfolio under adverse economic conditions
AdversePortfolio <- myPortfolio %>%
mutate(Year = AdverseScenario$Year[1],
DJX_Return = AdverseScenario$DJX_Return[1],
GDP = AdverseScenario$GDP[1])
# Predicted default probabilities under adverse economic conditions
PD <- predict(model, newdata = AdversePortfolio, type = "response")
# Dataframe containing years on books and predictive model probabilities under adverse economic conditions
predPD <- data.frame(YOB = myPortfolio$YOB, PD = PD)
# Compute predicted default rates
predDefault <- predPD %>%
group_by(YOB) %>%
summarise(predDefaultRate = mean(PD) * 100)
# Compute the expected loss of the loan portfolio under adverse economic conditions
ExpectedLoss <- sum(AdversePortfolio$EAD * AdversePortfolio$LGD * PD)
# Visualization
ggplot() +
geom_line(data = Default, aes(x = YOB, y = DefaultRate, color = "Historical Portfolio"), size = 1) +
geom_line(data = estDefault, aes(x = YOB, y = estDefaultRate, color = "Fitted Model"), size = 1) +
geom_line(data = predDefault, aes(x = YOB, y = predDefaultRate, color = "Predicted Adverse Portfolio"), size = 1) +
labs(title = "Default Rate (%)", x = "Years on Books", y = "Default Rate (%)") +
scale_color_manual(values = c("blue", "green", "red")) +
theme_minimal() +
theme(legend.title = element_blank())