diff options
| -rw-r--r-- | R/calibrate_tranches_BC.R | 93 |
1 files changed, 52 insertions, 41 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R index a606528e..eb9f4bee 100644 --- a/R/calibrate_tranches_BC.R +++ b/R/calibrate_tranches_BC.R @@ -18,49 +18,60 @@ aux <- function(rho, index, K, quote, spread){ temp <- BCtranche.legs(index, K, rho)
return(abs(temp$pl+temp$cl*spread + quote))
}
-
-alldates <- seq(as.Date("2014-01-01"), as.Date("2014-06-10"), by="1 day")
+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"))
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(index.name in c("hy21")){
- risk.numbers <- list(rho=c(), delta=c(), gamma=c(), theta=c())
- i.theta <- 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)
- temp <- get.tranchequotes(index$name, index$tenor, tradedate)
- quotes[i,-5] <- temp$trancheupfront
- quotes[i, 5] <- temp$indexrefprice[1]
- ## 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)
+for(i in seq_along(bus.dates)){
+ tradedate <- bus.dates[i]
+ index <- load.index(index.name, tradedate, "5yr", Z, w, Ngrid)
+ temp <- get.tranchequotes(index$name, index$tenor, tradedate)
+ quotes[i,-5] <- temp$trancheupfront
+ quotes[i, 5] <- temp$indexrefprice[1]
+}
+
+risk.numbers <- list(rho=c(), delta=c(), gamma=c(), theta=c())
+ind.theta <- 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)
+ temp <- get.tranchequotes(index$name, index$tenor, 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]
}
- index$rho <- rhovec
- 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)
- i.theta <- c(i.theta, indextheta(index, tradedate))
- 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)
- cat(i, "\n")
+ 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)
+ 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))
+ 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)
+ print(tradedate)
}
+save.image(file=paste0(index.name,"run.RData"))
|
