aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/calibrate_tranches_BC.R293
-rw-r--r--R/calibrate_tranches_MF.R94
2 files changed, 189 insertions, 198 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index 7652a90f..f65a1a49 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -1,148 +1,145 @@
-## parse command line arguments
-root.dir <- if(.Platform$OS.type == "unix"){
- "/home/share/CorpCDOs"
-}else{
- "//WDSENTINEL/share/CorpCDOs"
-}
-library(logging)
-basicConfig()
-removeHandler('basic.stdout')
-addHandler(writeToFile, file=file.path(root.dir, "logs", "calibrate_tranches_BC.log"))
-if(!interactive()){
- library(optparse)
- option_list <- list(
- make_option(c("-u", "--update"), action="store_true", default=FALSE,
- help="Update from the last run date [default %default]"),
- make_option(c("-c", "--config"), metavar="config_file",
- help="Runs the list of indices provided in CONFIG_FILE"),
- make_option(c("-i", "--index"), help="Index name we want to run"),
- make_option("--tenor", default="5yr", help="Tenor we want to run [default %default]"),
- make_option("--until", default=Sys.Date()-1, type="character",
- help="last date to run [default %default]"))
- args <- parse_args(OptionParser(option_list=option_list))
- ## default values
- if(is.null(args$config)){
- if(is.null(args$index)){
- stop("Please provide an index name")
- }
- config <- list(runs=list(c(args$index, args$tenor)))
- }else{
- config <- yaml::yaml.load_file(file.path(root.dir,"code", "etc", args$config))
- }
-} else {
- ## args <- list(config=yaml::yaml.load_file(file.path(root.dir,"code", "etc", "runs.yml")),
- ## until = Sys.Date()-1,
- ## update = TRUE)
- args <- list(update=TRUE, until = Sys.Date()-1)
- config <- list(runs=list(c("hy21", "3yr")))
- options(error=recover)
-}
-
-options(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)
-source(file.path(root.dir, "code", "R", "serenitasdb.R"))
-source(file.path(root.dir, "code", "R", "creditIndex.R"))
-source(file.path(root.dir, "code", "R", "tranche_functions.R"))
-
-for(run in config$runs){
- index.name <- run[1]
- tenor <- run[2]
- filename <- file.path(root.dir, "Tranche_data", "Runs",
- paste(tolower(index.name), tenor, "csv",sep="."))
- ## if(!file.exists(filename)){
- ## args$update <- FALSE
- ## }
- if(args$update && !is.na(begin.date <- getlastdate(index.name, tenor))){
- }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"),
- hy23 = as.Date("2014-10-16"),
- hy25 = as.Date("2015-10-01"),
- hy27 = as.Date("2016-10-04"),
- ig9 = as.Date("2013-01-01"),
- ig19 = as.Date("2013-05-01"),
- ig21 = as.Date("2013-09-26"),
- ig23 = as.Date("2014-10-14"),
- ig25 = as.Date("2015-09-22"),
- ig27 = as.Date("2016-09-27"),
- xo22 = as.Date("2014-10-20"),
- xo24 = as.Date("2015-09-28"),
- xo26 = as.Date("2016-09-27"),
- eu9 = as.Date("2014-09-15"),
- eu19 = as.Date("2013-04-03"),
- eu21 = as.Date("2014-03-27"),
- eu22 = as.Date("2014-10-22"),
- eu24 = as.Date("2015-09-23"),
- eu26 = as.Date("2016-09-27"))
- }
- if(begin.date > as.Date(args$until)){
- next
- }
- alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day")
- if(tolower(substr(index.name,1,2)) %in% c("xo", "eu")){
- curr <- "EUR"
- cal <- Calendar$new("TARGET")
- }else{
- curr <- "USD"
- cal <- Calendar$new("UnitedStates/GovernmentBond")
- }
- bus.dates <- alldates[cal$isBusinessDay(alldates)]
-
- for(j in seq_along(bus.dates)){
- tradedate <- bus.dates[j]
- loginfo(paste("calibrating", index.name, tenor, "for", as.character(tradedate)))
- exportYC(tradedate, curr)
- index <- creditIndex(index.name, tenor)
- index <- set.index.desc(index, tradedate)
- ## calibrate the single names curves
- index <- set.singlenamesdata(index, tradedate)
- index <- tryCatch(set.tranchedata(index, tradedate),
- error = function(e) logerror(e$message))
- if(is.null(index)) {
- loginfo(paste("skipping", index.name, tenor))
- next
- }
- temp <- BCindex.pv(index)
- index$EL <- -temp$pl
- index$duration <- temp$cl-cdsAccrued(tradedate, 1)
- index$theta <- indextheta(index, tradedate)
-
- ## calibrate the tranches using base correlation
- index$rho <- build.skew(index)
-
- ## compute various risk numbers
- index$tranches <- cbind(index$tranches, BCtranche.delta(index))
- index$tranches <- cbind(index$tranches, tryCatch(BCtranche.theta(index, method="TLP"),
- error = function(e) {
- logerror(e$message)
- n <- nrow(index$tranches)
- data.frame(theta=rep(NA, n),
- fw.delta=rep(NA, n)) }))
- index$tranches$corr01 <- BCtranche.corr01(index)
- temp <- BCtranche.pv(index, protection=TRUE)
- index$tranches$duration <-
- (temp$cl-cdsAccrued(tradedate, index$tranches$running))/index$tranches$running
- index$tranches$EL <- -temp$pl*diff(index$K)
-
- ## save the index object
- save(index, file=file.path(root.dir, "Tranche_data", "Objects",
- paste0(paste(index.name, tenor, as.character(tradedate), sep="_"),".RData")))
-
- ## save risk numbers into the csv file
- if(!args$update && j==1){
- cat(csvheaders(index), sep="\n", file=filename)
- }
- cat(tocsv(index), sep="\n", file=filename, append=TRUE)
- r <- with(tosql(index),
- tryCatch(dbGetQuery(serenitasdb, stmt, params),
- error = function(e) logerror(e$message)))
- loginfo("done")
- }
-}
-try(dbDisconnect(serenitasdb), TRUE)
+## parse command line arguments
+code.dir <- Sys.getenv("CODE_DIR")
+tranchedata.dir <- fole.path(Sys.getenv("DATA_DIR"), "..", "Tranche_data")
+library(logging)
+basicConfig()
+removeHandler('basic.stdout')
+addHandler(writeToFile, file=file.path(Sys.getenv("LOG_DIR"), "calibrate_tranches_BC.log"))
+if(!interactive()){
+ library(optparse)
+ option_list <- list(
+ make_option(c("-u", "--update"), action="store_true", default=FALSE,
+ help="Update from the last run date [default %default]"),
+ make_option(c("-c", "--config"), metavar="config_file",
+ help="Runs the list of indices provided in CONFIG_FILE"),
+ make_option(c("-i", "--index"), help="Index name we want to run"),
+ make_option("--tenor", default="5yr", help="Tenor we want to run [default %default]"),
+ make_option("--until", default=Sys.Date()-1, type="character",
+ help="last date to run [default %default]"))
+ args <- parse_args(OptionParser(option_list=option_list))
+ ## default values
+ if(is.null(args$config)){
+ if(is.null(args$index)){
+ stop("Please provide an index name")
+ }
+ config <- list(runs=list(c(args$index, args$tenor)))
+ }else{
+ config <- yaml::yaml.load_file(file.path(code.dir, "etc", args$config))
+ }
+} else {
+ ## args <- list(config=yaml::yaml.load_file(file.path(root.dir,"code", "etc", "runs.yml")),
+ ## until = Sys.Date()-1,
+ ## update = TRUE)
+ args <- list(update=TRUE, until = Sys.Date()-1)
+ config <- list(runs=list(c("hy21", "3yr")))
+ options(error=recover)
+}
+
+options(stringsAsFactors = FALSE)
+source(file.path(code.dir, "R", "yieldcurve.R"))
+source(file.path(code.dir, "R", "optimization.R"))
+source(file.path(code.dir, "R", "calibration.R"), chdir=TRUE)
+source(file.path(code.dir, "R", "serenitasdb.R"))
+source(file.path(code.dir, "R", "creditIndex.R"))
+source(file.path(code.dir, "R", "tranche_functions.R"))
+
+for(run in config$runs){
+ index.name <- run[1]
+ tenor <- run[2]
+ filename <- file.path(thranchedata.dir, "Runs",
+ paste(tolower(index.name), tenor, "csv",sep="."))
+ ## if(!file.exists(filename)){
+ ## args$update <- FALSE
+ ## }
+ if(args$update && !is.na(begin.date <- getlastdate(index.name, tenor))){
+ }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"),
+ hy23 = as.Date("2014-10-16"),
+ hy25 = as.Date("2015-10-01"),
+ hy27 = as.Date("2016-10-04"),
+ ig9 = as.Date("2013-01-01"),
+ ig19 = as.Date("2013-05-01"),
+ ig21 = as.Date("2013-09-26"),
+ ig23 = as.Date("2014-10-14"),
+ ig25 = as.Date("2015-09-22"),
+ ig27 = as.Date("2016-09-27"),
+ xo22 = as.Date("2014-10-20"),
+ xo24 = as.Date("2015-09-28"),
+ xo26 = as.Date("2016-09-27"),
+ eu9 = as.Date("2014-09-15"),
+ eu19 = as.Date("2013-04-03"),
+ eu21 = as.Date("2014-03-27"),
+ eu22 = as.Date("2014-10-22"),
+ eu24 = as.Date("2015-09-23"),
+ eu26 = as.Date("2016-09-27"))
+ }
+ if(begin.date > as.Date(args$until)){
+ next
+ }
+ alldates <- seq(begin.date, as.Date(as.character(args$until)), by="1 day")
+ if(tolower(substr(index.name,1,2)) %in% c("xo", "eu")){
+ curr <- "EUR"
+ cal <- Calendar$new("TARGET")
+ }else{
+ curr <- "USD"
+ cal <- Calendar$new("UnitedStates/GovernmentBond")
+ }
+ bus.dates <- alldates[cal$isBusinessDay(alldates)]
+
+ for(j in seq_along(bus.dates)){
+ tradedate <- bus.dates[j]
+ loginfo(paste("calibrating", index.name, tenor, "for", as.character(tradedate)))
+ exportYC(tradedate, curr)
+ index <- creditIndex(index.name, tenor)
+ index <- set.index.desc(index, tradedate)
+ ## calibrate the single names curves
+ index <- set.singlenamesdata(index, tradedate)
+ index <- tryCatch(set.tranchedata(index, tradedate),
+ error = function(e) logerror(e$message))
+ if(is.null(index)) {
+ loginfo(paste("skipping", index.name, tenor))
+ next
+ }
+ temp <- BCindex.pv(index)
+ index$EL <- -temp$pl
+ index$duration <- temp$cl-cdsAccrued(tradedate, 1)
+ index$theta <- indextheta(index, tradedate)
+
+ ## calibrate the tranches using base correlation
+ index$rho <- build.skew(index)
+
+ ## compute various risk numbers
+ index$tranches <- cbind(index$tranches, BCtranche.delta(index))
+ index$tranches <- cbind(index$tranches, tryCatch(BCtranche.theta(index, method="TLP"),
+ error = function(e) {
+ logerror(e$message)
+ n <- nrow(index$tranches)
+ data.frame(theta=rep(NA, n),
+ fw.delta=rep(NA, n)) }))
+ index$tranches$corr01 <- BCtranche.corr01(index)
+ temp <- BCtranche.pv(index, protection=TRUE)
+ index$tranches$duration <-
+ (temp$cl-cdsAccrued(tradedate, index$tranches$running))/index$tranches$running
+ index$tranches$EL <- -temp$pl*diff(index$K)
+
+ ## save the index object
+ save(index, file=file.path(tranchedata.dir, "Objects",
+ paste0(paste(index.name, tenor, as.character(tradedate), sep="_"),".RData")))
+
+ ## save risk numbers into the csv file
+ if(!args$update && j==1){
+ cat(csvheaders(index), sep="\n", file=filename)
+ }
+ cat(tocsv(index), sep="\n", file=filename, append=TRUE)
+ r <- with(tosql(index),
+ tryCatch(dbGetQuery(serenitasdb, stmt, params),
+ error = function(e) logerror(e$message)))
+ loginfo("done")
+ }
+}
+try(dbDisconnect(serenitasdb), TRUE)
diff --git a/R/calibrate_tranches_MF.R b/R/calibrate_tranches_MF.R
index badfba76..aa2c49bf 100644
--- a/R/calibrate_tranches_MF.R
+++ b/R/calibrate_tranches_MF.R
@@ -1,50 +1,44 @@
-#!/usr/bin/Rscript
-require(methods)
-library(logging)
-
-args <- commandArgs(trailingOnly=TRUE)
-
-if(.Platform$OS.type == "unix"){
- root.dir <- "/home/share/CorpCDOs"
-}else{
- root.dir <- "//WDSENTINEL/share/CorpCDOs"
-}
-
-basicConfig()
-removeHandler('basic.stdout')
-addHandler(writeToFile, file=file.path(root.dir, "logs", "calibrate_tranches_MF.log"))
-##options(warn=2)
-code.dir <- if(Sys.getenv("CODE_DIR")!="") Sys.getenv("CODE_DIR") else root.dir
-
-source(file.path(code.dir, "code", "R", "yieldcurve.R"))
-source(file.path(code.dir, "code", "R", "optimization.R"))
-source(file.path(code.dir, "code", "R", "calibration.R"), chdir=TRUE)
-source(file.path(code.dir, "code", "R", "serenitasdb.R"))
-source(file.path(code.dir, "code", "R", "creditIndex.R"))
-source(file.path(code.dir, "code", "R", "tranche_functions.R"))
-
-##figure out the tradedate
-if(length(args) >= 1){
- tradedate <- as.Date(args[1])
-}else{
- tradedate <- addBusDay(Sys.Date(), -1)
-}
-
-exportYC(tradedate)
-## calibrate HY25
-## calibrate the single names curves
-index <- creditIndex("hy27", "5yr")
-index <- set.index.desc(index, tradedate)
-index <- set.singlenamesdata(index, tradedate)
-## load tranche data
-index <- set.tranchedata(index, tradedate)
-##calibrate by modifying the factor distribution
-index$w.mod <- build.MFdist(index)
-dist <- MFlossdist(index)
-write.table(data.frame(Z=index$Z, w=index$w.mod),
- file=file.path(root.dir, "Scenarios", "Calibration",
- paste0("calibration-", tradedate, ".csv")),
- col.names=T, row.names=F, sep=",")
-
-save(index, dist, file = file.path(root.dir, "Scenarios", "Calibration",
- paste0("marketdata-", tradedate, ".RData")), compress="xz")
+#!/usr/bin/Rscript
+require(methods)
+library(logging)
+
+args <- commandArgs(trailingOnly=TRUE)
+
+basicConfig()
+removeHandler('basic.stdout')
+addHandler(writeToFile, file=file.path(Sys.getenv("LOG_DIR"), "calibrate_tranches_MF.log"))
+##options(warn=2)
+code.dir <- Sys.getenv("CODE_DIR")
+data.dir <- file.path(Sys.getenv("DATA_DIR"), "..", "Scenarios", "Calibration")
+source(file.path(code.dir, "R", "yieldcurve.R"))
+source(file.path(code.dir, "R", "optimization.R"))
+source(file.path(code.dir, "R", "calibration.R"), chdir=TRUE)
+source(file.path(code.dir, "R", "serenitasdb.R"))
+source(file.path(code.dir, "R", "creditIndex.R"))
+source(file.path(code.dir, "R", "tranche_functions.R"))
+
+##figure out the tradedate
+if(length(args) >= 1){
+ tradedate <- as.Date(args[1])
+}else{
+ tradedate <- addBusDay(Sys.Date(), -1)
+}
+
+exportYC(tradedate)
+## calibrate HY25
+## calibrate the single names curves
+index <- creditIndex("hy27", "5yr")
+index <- set.index.desc(index, tradedate)
+index <- set.singlenamesdata(index, tradedate)
+## load tranche data
+index <- set.tranchedata(index, tradedate)
+##calibrate by modifying the factor distribution
+index$w.mod <- build.MFdist(index)
+dist <- MFlossdist(index)
+write.table(data.frame(Z=index$Z, w=index$w.mod),
+ file=file.path(data.dir,
+ paste0("calibration-", tradedate, ".csv")),
+ col.names=T, row.names=F, sep=",")
+
+save(index, dist, file = file.path(data.dir,
+ paste0("marketdata-", tradedate, ".RData")), compress="xz")