# Required packages.
library(tidyquant)
library(tidyr)
library(Sim.DiffProc)
library(dplyr)
Loading required package: lubridate
Warning message:
“package ‘lubridate’ was built under R version 4.0.4”
Attaching package: ‘lubridate’
The following objects are masked from ‘package:base’:
date, intersect, setdiff, union
Loading required package: PerformanceAnalytics
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Attaching package: ‘PerformanceAnalytics’
The following object is masked from ‘package:graphics’:
legend
Loading required package: quantmod
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
══ Need to Learn tidyquant? ════════════════════════════════════════════════════
Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
</> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
Warning message:
“package ‘tidyr’ was built under R version 4.0.4”
Package 'Sim.DiffProc', version 4.8
browseVignettes('Sim.DiffProc') for more informations.
Attaching package: ‘Sim.DiffProc’
The following objects are masked from ‘package:PerformanceAnalytics’:
kurtosis, skewness
Warning message:
“package ‘dplyr’ was built under R version 4.0.4”
Attaching package: ‘dplyr’
The following objects are masked from ‘package:xts’:
first, last
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
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