diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/calibrate_tranches_BC.R | 233 |
1 files changed, 141 insertions, 92 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R index 7257bb01..bb9453b8 100644 --- a/R/calibrate_tranches_BC.R +++ b/R/calibrate_tranches_BC.R @@ -1,9 +1,62 @@ +## parse command line arguments
+args <- commandArgs(trailingOnly=TRUE)
+if(length(args)==0 || args[1]=="-h" || args[1]=="--help"){
+ cat("usage: Rscript calibrate_tranches_BC.R [-u] indexname [tenor]\n")
+ cat(" Rscript calibrate_tranches_BC.R [-u] -c configfile\n")
+ cat("\n")
+ cat("If -u flag is provided, it will update from the last run date\n")
+ cat("tenor is 5yr by default\n")
+ cat("\n")
+ cat("Examples:\n")
+ cat("\tRscript calibrate_tranches_BC.R hy19 7yr: will run hy19 7yr from the beginning\n")
+ cat("\tRscript calibrate_tranches_BC.R -c conf.yaml: will run the list of indices provided in conf.yml\n")
+ quit("no")
+}
+
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
}else{
root.dir <- "//WDSENTINEL/share/CorpCDOs"
}
+## default values
+updateflag <- FALSE
+tenor <- "5yr"
+if(args[1]=="-u"){
+ updateflag <- TRUE
+ if(length(args) >=2 || args[2]=="-c"){
+ if(length(args) < 3){
+ stop("Please provide a config file")
+ }else{
+ library(yaml)
+ runs <- yaml.load_file(file.path(root.dir,"code", "etc", args[3]))
+ }
+ }else{
+ if(length(args) >= 2){
+ index.name <- args[2]
+ }else{
+ stop("Please provind an index name")
+ }
+ if(length(args) >= 3){
+ tenor <- args[3]
+ }
+ }
+}else if(args[1]=="-c"){
+ if(length(args) < 2){
+ stop("Please provide a config file")
+ }else{
+ library(yaml)
+ runs <- yaml.load_file(file.path(root.dir,"code", "etc", args[2]))
+ }
+}else{
+ index.name <- args[1]
+ if(length(args) >= 2){
+ tenor <- args[2]
+ }
+}
+if(!exists("runs")){
+ runs <- list(name=index.name, tenor=tenor)
+}
options(stringsAsFactors = FALSE)
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
@@ -15,102 +68,98 @@ library(lossdistrib) n.int <- 250
list2env(GHquad(n.int), envir=parent.frame())
Ngrid <- 201
+for(i in seq_along(runs$name)){
+ index.name <- runs$name[i]
+ tenor <- runs$tenor[i]
+ if(updateflag){##ghetto way of getting the last row of the file
+ runfile <- read.csv(file.path(root.dir,"Tranche_data","Runs", paste(index.name,tenor,"csv",sep=".")))
+ begin.date <- as.Date(runfile[nrow(runfile),1])+1
+ }else{
+ begin.date <- switch(index.name,
+ hy10 = as.Date("2014-08-11"),
+ hy15 = as.Date("2014-06-10"),
+ hy17 = as.Date("2013-01-01"),
+ hy19 = as.Date("2013-02-01"),
+ hy21 = as.Date("2013-10-04"),
+ ig9 = as.Date("2013-01-01"),
+ ig19 = as.Date("2013-05-01"),
+ ig21 = as.Date("2013-09-26"))
+ }
-#parse command line arguments
-args <- commandArgs(trailingOnly=TRUE)
-index.name <- args[1]
-if(length(args)>=2&&args[2]!="update"){
- tenor <- args[2]
-}else{
- tenor <- "5yr"
-}
+ 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))))
-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", paste(index.name,tenor,"csv",sep=".")))
- begin.date <- as.Date(runfile[nrow(runfile),1])+1
-}else{
- begin.date <- switch(index.name,
- hy10 = as.Date("2014-08-11"),
- hy15 = as.Date("2014-06-10"),
- hy17 = as.Date("2013-01-01"),
- hy19 = as.Date("2013-02-01"),
- hy21 = as.Date("2013-10-04"),
- ig9 = as.Date("2013-01-01"),
- ig19 = as.Date("2013-05-01"),
- ig21 = as.Date("2013-09-26"))
-}
+ ##check if we have all the quotes and save them
+ n.tranches <- 4
+ if(index.name=="ig9"){
+ n.tranches <- 6
+ }
+ 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[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
+ }
-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))))
+ 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)
+ ELmat <- matrix(0, length(bus.dates), n.tranches)
-##check if we have all the quotes and save them
-n.tranches <- 4
-if(index.name=="ig9"){
- n.tranches <- 6
-}
-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[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
-}
+ for(i in seq_along(bus.dates)){
+ tradedate <- bus.dates[i]
+ exportYC(tradedate)
+ 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)
+ indexEL[i] <- EL(index)
+ indexTheta[i] <- indextheta(index, tradedate)
+ ## calibrate the tranches using base correlation
+ index$rho <- build.skew(index)
+ index <- c(index, BCtranche.delta(index))
+ deltas[i,] <- index$deltas
+ gammas[i,] <- index$gammas
+ index<- c(index, BCtranche.theta(index, method="TLP"))
+ thetas[i,] <- index$theta
+ rhos[i,] <- index$rho[-1]
+ corr01[i,] <- BCtranche.corr01(index)
+ temp <- BCtranche.pv(index, protection=TRUE)
+ durations[i,] <- (temp$cl-cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
+ ELmat[i,] <- -temp$pl*diff(index$K)
+ save(index, file=file.path(root.dir, "Tranche_data", "Objects",
+ paste0(paste(index.name, tenor, as.character(tradedate), sep="_"),".RData")))
+ print(tradedate)
+ }
-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)
-ELmat <- matrix(0, length(bus.dates), n.tranches)
+ risk.numbers <- data.frame(deltas, gammas, thetas, rhos, corr01, durations, ELmat)
+ 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(bus.dates, quotes, indexEL, indexTheta, risk.numbers)
+ colnames(data)[1] <- "date"
-for(i in seq_along(bus.dates)){
- tradedate <- bus.dates[i]
- exportYC(tradedate)
- 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)
- indexEL[i] <- EL(index)
- indexTheta[i] <- indextheta(index, tradedate)
- ## calibrate the tranches using base correlation
- index$rho <- build.skew(index)
- temp <- BCtranche.delta(index)
- deltas[i,] <- temp$deltas
- gammas[i,] <- temp$gammas
- thetas[i,] <- BCtranche.theta(index)$theta
- rhos[i,] <- index$rho[-1]
- corr01[i,] <- BCtranche.corr01(index)
- temp <- BCtranche.pv(index, protection=TRUE)
- durations[i,] <- (temp$cl-cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
- ELmat[i,] <- -temp$pl*diff(index$K)
- print(tradedate)
+ write.table(data, file=file.path(root.dir,"Tranche_data","Runs", paste(index.name,tenor,"csv",sep=".")),
+ append=updateflag, col.names=!updateflag, qmethod="double", sep=",", row.names=FALSE)
}
-
-risk.numbers <- data.frame(deltas, gammas, thetas, rhos, corr01, durations, ELmat)
-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(bus.dates, quotes, indexEL, indexTheta, risk.numbers)
-colnames(data)[1] <- "date"
-
-write.table(data, file=file.path(root.dir,"Tranche_data","Runs", paste(index.name,tenor,"csv",sep=".")),
- append=updateflag, col.names=!updateflag, qmethod="double", sep=",", row.names=FALSE)
|
