diff options
| -rw-r--r-- | R/calibrate_tranches_BC.R | 68 | ||||
| -rw-r--r-- | R/calibration.R | 10 |
2 files changed, 39 insertions, 39 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R index 6d0844c0..c50f3149 100644 --- a/R/calibrate_tranches_BC.R +++ b/R/calibrate_tranches_BC.R @@ -17,41 +17,43 @@ 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)
- hy21 <- load.index("hy21", tradedate)
- ## calibrate HY21
- ## calibrate the single names curves
- set.singlenamesdata(hy21, tradedate)
- set.tranchedata(hy21, tradedate)
+for(index.name in c("hy19", "hy21"){
+ 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)
+ ## calibrate HY21 the single names curves
+ index <- set.singlenamesdata(index, tradedate)
+ index <- set.tranchedata(index, 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)){
- 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)
+ ## 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(index$K)-1)){
+ rho <- optimize(f, interval=c(0,1), index=index, N=Ngrid, i=j)$minimum
+ rhovec <- c(rhovec, rho)
+ }
+ rhovec <- c(0, rhovec)
+ index$rho <- rhovec
+ temp <- BCtranche.delta(index, Z, w, Ngrid, tradedate)
+ deltasmat <- rbind(deltasmat, temp$deltas)
+ gammasmat <- rbind(gammasmat, temp$gammas)
+ cl <- c()
+ for(j in 1:(length(index$K)-1)){
+ cl <- c(cl, BCtranche.pv(index, index$K[j], index$K[j+1], rhovec[j], rhovec[j+1], Z, w, Ngrid)$cl)
+ }
+ duration <- (cl - cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
+ rhomat <- rbind(rhomat, rhovec)
+ cat(i, "\n")
}
- 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(deltasmat, file="delta.csv")
write.csv(rhomat, file="rho.csv")
diff --git a/R/calibration.R b/R/calibration.R index a7c07100..d9546737 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -27,9 +27,8 @@ get.cdsSchedule <- function(tradedate){ } set.singlenamesdata <- function(index, tradedate){ - index.name <- deparse(substitute(index)) singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration", - paste0(index.name, "_singlenames_", tradedate, ".csv"))) + paste0(index$name, "_singlenames_", tradedate, ".csv"))) nondefaulted <- singlenames.data[!singlenames.data$ticker %in% index$defaulted,] cds.cs <- get.cdsSchedule(tradedate) index$portfolio <- list() @@ -38,13 +37,12 @@ set.singlenamesdata <- function(index, tradedate){ } index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio)) index$recov <- sapply(index$portfolio, attr, "recovery") - assign(index.name, index, envir=parent.env(environment())) + return( index ) } set.tranchedata <- function(index, tradedate){ - index.name <- deparse(substitute(index)) index$tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration", - paste0(index.name, "_tranches_", tradedate, ".csv")), header=TRUE) + paste0(index$name, "_tranches_", tradedate, ".csv")), header=TRUE) index$indexref <- index$tranche.data$bidRefPrice[1]/100 index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", 0.05, 0, tradedate, IMMDate(tradedate, "prev")) @@ -66,5 +64,5 @@ set.tranchedata <- function(index, tradedate){ ## calibrate the tranches using base correlation index$quotes <- cumsum(diff(index$K) * (1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running))) - assign(index.name, index, envir=parent.env(environment())) + return(index) } |
