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.R99
1 files changed, 75 insertions, 24 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index eb9f4bee..afebb179 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -4,6 +4,8 @@ if(.Platform$OS.type == "unix"){
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
+options(warn=2, stringsAsFactors = FALSE)
+
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"), chdir=TRUE)
@@ -19,36 +21,73 @@ aux <- function(rho, index, K, quote, spread){
return(abs(temp$pl+temp$cl*spread + quote))
}
args <- commandArgs(trailingOnly=TRUE)
+
index.name <- args[1]
-alldates <- switch(index.name,
- hy19=seq(as.Date("2013-02-01"), as.Date("2014-07-15"), by="1 day"),
- hy21=seq(as.Date("2013-10-04"), as.Date("2014-07-15"), by="1 day"))
+if(length(args)>=2&&args[2]!="update"){
+ tenor <- args[2]
+}else{
+ tenor <- "5yr"
+}
+
+updateflag <- args[length(args)]=="update"
+if(updateflag){##ghetto way of getting the last row of the file
+ runfile <- read.csv(file.path(root.dir,"Tranche_data","Runs", paste0(index.name,tenor,".csv")))
+ begin.date <- as.Date(runfile[nrow(runfile),1])+1
+}
+
+begin.date <- switch(index.name,
+ hy10=as.Date("2014-06-10"),
+ ig19=as.Date("2013-05-01"),
+ ig21=as.Date("2013-09-26"),
+ hy19=as.Date("2013-02-01"),
+ hy21=as.Date("2013-10-04"))
+alldates <- seq(begin.date, Sys.Date()-1, by="1 day")
+if(index.name=="ig19"){
+ alldates <- alldates[alldates!=as.Date("2013-11-29")] ##people are lazy the day after Thanksgiving
+}
+
bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
##check if we have all the quotes and save them
-quotes <- matrix(0, length(bus.dates), 5)
-for(i in seq_along(bus.dates)){
- tradedate <- bus.dates[i]
- index <- load.index(index.name, tradedate, "5yr", Z, w, Ngrid)
+n.tranches <- 4
+quotes <- matrix(0, length(bus.dates), 2+2*n.tranches)
+for(j in seq_along(bus.dates)){
+ tradedate <- bus.dates[j]
+ index <- load.index(index.name, tradedate, tenor, Z, w, Ngrid)
temp <- get.tranchequotes(index$name, index$tenor, tradedate)
- quotes[i,-5] <- temp$trancheupfront
- quotes[i, 5] <- temp$indexrefprice[1]
+ quotes[j, 1] <- temp$indexrefprice[1]
+ quotes[j, 2] <- temp$indexrefspread[1]
+ quotes[j, 2+1:n.tranches] <- temp$trancheupfront
+ quotes[j, 2+n.tranches+1:n.tranches] <- temp$tranchedelta
}
-risk.numbers <- list(rho=c(), delta=c(), gamma=c(), theta=c())
-ind.theta <- c()
+tranche.names <- paste(temp$attach, temp$detach, sep="-")
+colnames(quotes) <- c("indexprice", "indexref",paste(tranche.names, "Upfront"),
+ paste(tranche.names, "Dealer Delta"))
+Attach <- temp$detach
+#preallocate all the risk matrices
+indexEL <- rep(0, length(bus.dates))
+indexTheta <- rep(0, length(bus.dates))
+deltas <- matrix(0, length(bus.dates), n.tranches)
+gammas <- matrix(0, length(bus.dates), n.tranches)
+thetas <- matrix(0, length(bus.dates), n.tranches)
+rhos <- matrix(0, length(bus.dates), n.tranches)
+corr01 <- matrix(0, length(bus.dates), n.tranches)
+durations <- matrix(0, length(bus.dates), n.tranches)
+EL <- matrix(0, length(bus.dates), n.tranches)
+
for(i in seq_along(bus.dates)){
tradedate <- bus.dates[i]
exportYC(tradedate)
- index <- load.index(index.name, tradedate, "5yr", Z, w, Ngrid)
+ index <- load.index(index.name, tradedate, tenor, Z, w, Ngrid)
## calibrate the single names curves
index <- set.singlenamesdata(index, tradedate)
index <- set.tranchedata(index, tradedate)
- temp <- get.tranchequotes(index$name, index$tenor, tradedate)
-
+ indexEL[i] <- EL(index)
+ indexTheta[i] <- indextheta(index, tradedate)
## calibrate the tranches using base correlation
rhovec <- c(0)
- for(j in 1:(length(index$K)-1)){
+ for(j in 1:(length(index$K)-2)){
##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){
@@ -62,16 +101,28 @@ for(i in seq_along(bus.dates)){
spread=index$tranche.running[j])$minimum
rhovec <- c(rhovec, rho)
}
- index$rho <- rhovec
+ index$rho <- c(rhovec, NA)
temp <- BCtranche.delta(index, tradedate)
- risk.numbers$delta <- rbind(risk.numbers$delta, temp$deltas)
- risk.numbers$gamma <- rbind(risk.numbers$gamma, temp$gammas)
- risk.numbers$theta <- rbind(risk.numbers$theta, BCtranche.theta(index))
- risk.numbers$rho <- rbind(risk.numbers$rho, rhovec)
- ind.theta <- c(ind.theta, indextheta(index, tradedate))
+ deltas[i,] <- temp$deltas
+ gammas[i,] <- temp$gammas
+ thetas[i,] <- BCtranche.theta(index)
+ rhos[i,] <- index$rho[-1]
+ corr01[i,] <- BCtranche.corr01(index)
temp <- BCtranche.pv(index, protection=TRUE)
- duration <- (temp$cl-cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
- risk.numbers$duration <- rbind(risk.numbers$duration, duration)
+ durations[i,] <- (temp$cl-cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
+ EL[i,] <- -temp$pl*diff(index$K)
print(tradedate)
}
-save.image(file=paste0(index.name,"run.RData"))
+risk.numbers <- data.frame(deltas, gammas, thetas, rhos, corr01, durations, EL)
+colnames(risk.numbers) <- c(paste(tranche.names, "Model Delta"),
+ paste(tranche.names, "Gamma"),
+ paste(tranche.names, "Theta"),
+ paste(Attach, "Corr"),
+ paste(tranche.names, "Corr01"),
+ paste(tranche.names, "Dur"),
+ paste(tranche.names, "EL"))
+data <- cbind(quotes, indexEL, indexTheta, risk.numbers)
+row.names(data) <- as.character(bus.dates)
+
+write.csv(data, file.path(root.dir,"Tranche_data","Runs", paste(index.name,tenor,"csv",sep=".")),
+ append=updateflag, col.names=!updateflag)