aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_BC.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/calibrate_tranches_BC.R')
-rw-r--r--R/calibrate_tranches_BC.R116
1 files changed, 116 insertions, 0 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
new file mode 100644
index 00000000..48d228cc
--- /dev/null
+++ b/R/calibrate_tranches_BC.R
@@ -0,0 +1,116 @@
+if(.Platform$OS.type == "unix"){
+ root.dir <- "/home/share/CorpCDOs"
+}else{
+ root.dir <- "//WDSENTINEL/share/CorpCDOs"
+}
+
+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", "tranche_functions.R"))
+source(file.path(root.dir, "code", "R", "yieldcurve.R"))
+source(file.path(root.dir, "code", "R", "optimization.R"))
+load.index("hy21")
+
+tradedate <- as.Date("2014-04-16")
+MarkitData <- getMarkitIRData(tradedate)
+L1m <- buildMarkitYC(MarkitData, dt = 1/12)
+L2m <- buildMarkitYC(MarkitData, dt = 1/6)
+L3m <- buildMarkitYC(MarkitData)
+L6m <- buildMarkitYC(MarkitData, dt = 1/2)
+setEvaluationDate(as.Date(MarkitData$effectiveasof))
+setCalendarContext("TARGET")
+
+## calibrate HY21
+## calibrate the single names curves
+singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
+ paste0("hy21_singlenames_", tradedate, ".csv")))
+nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy21$defaulted,]
+bps <- 1e-4
+
+cdsdates <- as.Date(character(0))
+for(tenor in paste0(1:5, "y")){
+ cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
+}
+
+hy21portfolio <- c()
+cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
+ 1, tradedate, IMMDate(tradedate, "prev"))
+for(i in 1:nrow(nondefaulted)){
+ SC <- new("creditcurve",
+ recovery=nondefaulted$recovery[i]/100,
+ startdate=tradedate,
+ issuer=as.character(nondefaulted$ticker[i]))
+ quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8])*0.01,
+ running=rep(nondefaulted$running[i]*bps, 5))
+ SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate, cs)
+ hy21portfolio <- c(hy21portfolio, SC)
+}
+
+issuerweights <- rep(1/length(hy21portfolio), length(hy21portfolio))
+hy21$indexref <- 1.0725
+
+temp <- tweakcurves(hy21portfolio, hy21, tradedate)
+hy21portfolio.tweaked <- temp$portfolio
+cs <- couponSchedule(IMMDate(tradedate), hy21$maturity,"Q", "FIXED", 0.05, 0, tradedate,
+ IMMDate(tradedate, "prev"))
+SurvProb <- SPmatrix(hy21portfolio.tweaked, length(cs$dates))
+## load common parameters
+K <- c(0, 0.05, 0.1, 0.15, 0.25, 0.35, 1)
+Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor)
+tranche.upf <- c(15.2, 62.125, 86.625, 102.875, 113.4375, 119.0375)
+tranche.running <- rep(0.05, 6)
+
+Ngrid <- 2 * nrow(nondefaulted)+1
+recov <- sapply(hy21portfolio.tweaked, attr, "recovery")
+
+##calibrate tranches using base correlation
+n.int <- 250
+n.credit <- length(hy21portfolio)
+quadrature <- gauss.quad.prob(n.int, "normal")
+w <- quadrature$weights
+Z <- quadrature$nodes
+defaultprob <- 1 - SurvProb
+
+dK <- diff(Kmodified)
+acc <- cdsAccrued(tradedate, 0.05)
+
+##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...
+quotes <- cumsum(dK * (1-tranche.upf/100-acc))
+
+## calibrate the tranches using base correlation
+rhovec <- c()
+f <- function(rho, ...){
+ temp <- BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, Ngrid)
+ return(abs(tranche.pv(temp$L, temp$R, cs, 0, Kmodified[i]) + q))
+}
+
+rhovec <- c()
+for(i in 2:length(Kmodified)){
+ q <- quotes[i-1]
+ rho <- optimize(f, interval=c(0,1),
+ defaultprob, issuerweights, recov, Ngrid, Kmodified, cs, q)$minimum
+ rhovec <- c(rhovec, rho)
+}
+
+
+##compute deltas by blipping the curves
+rhovec <- c(0, rhovec)
+portfolioplus <- tweakportfolio(hy21portfolio.tweaked, 1e-4)
+defaultprobplus <- 1 - SPmatrix(portfolioplus, length(cs$dates))
+portfoliominus <- tweakportfolio(hy21portfolio.tweaked, -1e-4)
+defaultprobminus <- 1 - SPmatrix(portfoliominus, length(cs$dates))
+test <- matrix(0, 6, 2)
+for(i in 2:7){
+ tempminus <- BClossdistC(defaultprobminus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
+ tempplus <- BClossdistC(defaultprobplus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
+ test[i-1,1] <- tranche.pv(tempminus$L, tempminus$R, cs, 0, Kmodified[i])
+ test[i-1,2] <- tranche.pv(tempplus$L, tempplus$R, cs, 0, Kmodified[i])
+}
+dPVtranche <- diff(c(0, test[,1]))/dK - diff(c(0, test[,2]))/dK
+dPVindex <- indexpv(portfoliominus, hy21, tradedate=tradedate, clean=FALSE)$bp-
+ indexpv(portfolioplus, hy21, tradedate=tradedate, clean=FALSE)$bp
+deltas <- dPVtranche/dPVindex