# Required packages.
library(tidyquant)
library(tidyr)
library(Sim.DiffProc)
library(dplyr)
loan_data <- readRDS("loan_data_ARF.rds")
high_income <- loan_data[(loan_data$annual_inc > 1000000), ]
high_income_index <- data.frame(value = as.integer(rownames(high_income)))
loan_data <- loan_data[-high_income_index$value,]
# It is convenient to set the loan_status as factor.
loan_data$loan_status <- as.factor(loan_data$loan_status)
set.seed(567)
index_train <- cbind(runif(1 : nrow(loan_data), 0 , 1),
c(1 : nrow(loan_data)))
index_train <- order(index_train[, 1])
index_train <- index_train[1: (2/3 * nrow(loan_data))]
# Create training_set
training_set <- loan_data[index_train, ]
# Create test_set
test_set <- loan_data[-index_train, ]
mean(test_set$loan_status == 1)
# Merton assumes V follows a geometric brownian motion process.
V0.sim <- function(i) { # the argument i is not used here.
GBM(N = 365, T = 1, t0 = 0, x0 = 12.39578, theta = 0.05, sigma = 0.2123041)
}
set.seed(3) # Reproducibility
paths <- sapply(1:100, V0.sim) # Create 10 paths for V.
# Plot results.
matplot(paths, type ="l", col = "black", lwd = 1, lty = 1,
ylab = expression(paste(V[t])), xlab = "Time in days (0 to T)")
lines(paths[,71], col = "purple", lwd = 2)
abline(h = 10, col = "red", lwd = 2)
points(1, 12.39578, pch = 19, cex = 1.5, col = "blue")
which(paths[366,] > 20)
paths[366,71]
# function(m, np, hr, rf, rr)
s <- CDS_Spread(1, 1, 0.02, 0.05, 0.4)
s*10000
# function(m, np, hr, rf, rr)
s <- CDS_Spread(5, 1, 0.02, 0.05, 0.4)
s*10000
# function(m, np, hr, rf, rr)
s <- CDS_Spread(10, 1, 0.02, 0.05, 0.4)
s*10000