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")) source(file.path(root.dir, "code", "R", "calibration.R")) source(file.path(root.dir, "code", "R", "mlpdb.R")) library(lossdistrib) n.int <- 250 list2env(GHquad(n.int), envir=parent.frame()) Ngrid <- 201 alldates <- seq(as.Date("2014-03-04"), as.Date("2014-01-05"), by="1 day") aux <- function(rho, index, K, quote, spread){ temp <- BCtranche.legs(index, K, rho) return(abs(temp$pl+temp$cl*spread + quote)) } bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates)))) for(index.name in c("hy19", "hy21", "ig19", "ig21")){ rhomat <- c() deltasmat <- c() gammasmat <- c() for(i in seq_along(bus.dates)){ tradedate <- bus.dates[i] exportYC(tradedate) index <- load.index(index.name, tradedate, "5yr", Z, w, Ngrid) ## calibrate the single names curves index <- set.singlenamesdata(index, tradedate) index <- set.tranchedata(index, tradedate) ## calibrate the tranches using base correlation rhovec <- c(0) for(j in 1:(length(index$K)-1)){ ##use the current tranche coupon ## we compute the 0-index$K[j+1] equivalent quote using the coupon of the jth quote if(j==1){ q <- index$tranche.quotes[j]*index$K[j+1] }else{ temp <- BCtranche.legs(index, index$K[j], rhovec[j]) q <- index$tranche.quotes[j] * (index$K[j+1]-index$K[j])- temp$pl - temp$cl*index$tranche.running[j] } rho <- optimize(aux, interval=c(0,1), index=index, K=index$K[j+1], quote=q, spread=index$tranche.running[j])$minimum rhovec <- c(rhovec, rho) } index$rho <- rhovec temp <- BCtranche.delta(index, tradedate) deltasmat <- rbind(deltasmat, temp$deltas) gammasmat <- rbind(gammasmat, temp$gammas) temp <- BCtranche.pv(index) duration <- (temp$cl-cdsAccrued(tradedate, index$tranche.running))/index$tranche.running rhomat <- rbind(rhomat, rhovec) cat(i, "\n") } assign(index.name, index) }