aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/tranche_mapping.R108
1 files changed, 108 insertions, 0 deletions
diff --git a/R/tranche_mapping.R b/R/tranche_mapping.R
new file mode 100644
index 00000000..b1e059d2
--- /dev/null
+++ b/R/tranche_mapping.R
@@ -0,0 +1,108 @@
+if(.Platform$OS.type == "unix"){
+ root.dir <- "/home/share/CorpCDOs"
+}else{
+ root.dir <- "//WDSENTINEL/share/CorpCDOs"
+}
+options(stringsAsFactors=FALSE)
+source(file.path(root.dir, "code", "R", "cds_utils.R"))
+source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
+source(file.path(root.dir, "code", "R", "yieldcurve.R"))
+source(file.path(root.dir, "code", "R", "optimization.R"))
+library(lossdistrib)
+load.index("hy21")
+load.index("hy19")
+n.int <- 250
+bps <- 1e-4
+attach(GHquad(n.int))
+
+tradedate <- as.Date("2014-05-05")
+exportYC(tradedate)
+cdsdates <- as.Date(character(0))
+for(tenor in paste0(1:5, "y")){
+ cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
+}
+cds.cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
+ 1, tradedate, IMMDate(tradedate, "prev"))
+
+
+##build portfolio
+buildSC <- function(quote, cs){
+ SC <- new("creditcurve",
+ recovery=quote$recovery/100,
+ startdate=tradedate,
+ issuer=as.character(quote$ticker))
+ quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(quote[4:8]) * 0.01,
+ running=rep(quote$running*1e-4, 5))
+ SC@curve <- cdshazardrate(quotes, SC@recovery, tradedate, cs)
+ return( SC )
+}
+
+set.singlenamesdata <- function(index, tradedate){
+ index.name <- deparse(substitute(index))
+ singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
+ paste0(index.name, "_singlenames_", tradedate, ".csv")))
+ nondefaulted <- singlenames.data[!singlenames.data$ticker %in% index$defaulted,]
+ index$portfolio <- c()
+ for(i in 1:nrow(nondefaulted)){
+ index$portfolio <- c(index$portfolio, buildSC(nondefaulted[i,], cds.cs))
+ }
+ index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio))
+ index$recov <- sapply(index$portfolio, attr, "recovery")
+ assign(index.name, index, envir=parent.env(environment()))
+}
+
+## load all the single names data
+## calibrate the single names curves
+set.singlenamesdata(hy21, tradedate)
+set.singlenamesdata(hy19, tradedate)
+
+## load tranche data
+set.tranchedata <- function(index, tradedate){
+ index.name <- deparse(substitute(index))
+ index$tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
+ paste0(index.name, "_tranches_", tradedate, ".csv")), header=TRUE)
+ index$indexref <- index$tranche.data$bidRefPrice[1]/100
+ index$portfolio.tweaked <- tweakcurves(index$portfolio, index, tradedate)$portfolio
+ index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", 0.05, 0, tradedate,
+ IMMDate(tradedate, "prev"))
+ index$defaultprob <- 1-SPmatrix(index$portfolio.tweaked, length(index$cs$dates))
+ K <- c(0, 0.15, 0.25, 0.35, 1)
+ index$K <- adjust.attachments(K, index$loss, index$factor)
+ index$tranche.upf <- index$tranche.data$Mid
+ index$tranche.running <- index$tranche.data$Coupon
+ ##convert the quotes
+ ## - we convert to protection terms x->1-x/100
+ ## - we remove accrued x->x-acc
+ ## - we weight it by the size of the tranche
+ ## - we take the cumsum to convert to 0-5, 0-10, 0-15 quotes, etc...
+ ## calibrate the tranches using base correlation
+ index$quotes <- cumsum(diff(index$K) *
+ (1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running)))
+ assign(index.name, index, envir=parent.env(environment()))
+}
+
+set.tranchedata(hy19, tradedate)
+set.tranchedata(hy21, tradedate)
+
+## load common parameters
+Ngrid <- 201
+f <- function(rho, index, N, i){
+ temp <- with(index,
+ BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, N))
+ return(abs(tranche.pv(temp$L, temp$R, index$cs, 0, index$K[i+1]) + index$quotes[i]))
+}
+
+rhovec <- c()
+for(i in 1:3){
+ rho <- optimize(f, interval=c(0,1), index=hy21, N=Ngrid, i=i)$minimum
+ rhovec <- c(rhovec, rho)
+}
+
+rhovec <- c(0, rhovec)
+K <- c(0, 0.15, 0.25, 0.35, 1)
+rhofun <- approxfun(K[-5], rhovec, rule=2)
+
+Kmapped <- rep(0, 3)
+for(i in 2:4){
+ Kmapped[i-1] <- skewmapping(hy21, rhofun, hy19, K[i], Z, w, 201)$minimum
+}