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", "yieldcurve.R")) source(file.path(root.dir, "code", "R", "optimization.R")) load.index("hy21") library(lossdistrib) n.int <- 250 attach(GHquad(n.int)) alldates <- seq(as.Date("2014-01-01"), as.Date("2014-05-05"), by="1 day") rhomat <- c() deltasmat <- c() bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)))) for(i in seq_along(bus.dates)){ tradedate <- bus.dates[i] exportYC(tradedate) ## 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) } n.credit <- length(hy21portfolio) issuerweights <- rep(1/n.credit, n.credit) tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration", paste0("hy21_tranches_", tradedate, ".csv")), header=TRUE) hy21$indexref <- tranche.data$bidRefPrice[1]/100 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.15, 0.25, 0.35, 1) Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor) tranche.upf <- tranche.data$Mid tranche.running <- tranche.data$Coupon Ngrid <- 2 * nrow(nondefaulted) + 1 recov <- sapply(hy21portfolio.tweaked, attr, "recovery") ##calibrate tranches using base correlation defaultprob <- 1 - SurvProb dK <- diff(Kmodified) acc <- cdsAccrued(tradedate, tranche.running) ##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) } rhovec <- c(0, rhovec) ##compute deltas by blipping the curves ## 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 ##use BCtranche.delta function deltas <- rep(0, length(K)-1) for(i in seq_along(K[-1])){ deltas[i] <- BCtranche.delta(hy21portfolio.tweaked, hy21, 0.05, K[i], K[i+1], rhovec[i], rhovec[i+1], Z, w, Ngrid, tradedate) } deltasmat <- rbind(deltasmat, deltas) rhomat <- rbind(rhomat, rhovec) }