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")) load.index("hy21", "index_definitions_bt.yml") library(lossdistrib) n.int <- 250 attach(GHquad(n.int)) Ngrid <- 201 alldates <- seq(as.Date("2014-01-01"), as.Date("2014-01-05"), by="1 day") rhomat <- c() deltasmat <- c() gammasmat <- 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 set.singlenamesdata(hy21, tradedate) set.tranchedata(hy21, tradedate) ## calibrate the tranches using base correlation f <- function(rho, index, N, i){ temp <- BClossdistC(index$defaultprob, index$issuerweights, index$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(j in 1:(length(hy21$K)-1)){ ##q <- quotes[i-1] rho <- optimize(f, interval=c(0,1), index=hy21, N=Ngrid, i=j)$minimum rhovec <- c(rhovec, rho) } rhovec <- c(0, rhovec) hy21$rho <- rhovec temp <- BCtranche.delta(hy21, Z, w, Ngrid, tradedate) deltasmat <- rbind(deltasmat, temp$deltas) gammasmat <- rbind(gammasmat, temp$gammas) cl <- c() for(j in 1:(length(hy21$K)-1)){ cl <- c(cl, BCtranche.pv(hy21, hy21$K[j], hy21$K[j+1], rhovec[j], rhovec[j+1], Z, w, Ngrid)$cl) } duration <- (cl - cdsAccrued(tradedate, hy21$tranche.data$Coupon))/hy21$tranche.data$Coupon rhomat <- rbind(rhomat, rhovec) cat(i, "\n") } write.csv(deltasmat, file="delta.csv") write.csv(rhomat, file="rho.csv")