diff options
Diffstat (limited to 'R/calibration.R')
| -rw-r--r-- | R/calibration.R | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/R/calibration.R b/R/calibration.R new file mode 100644 index 00000000..96bf03f2 --- /dev/null +++ b/R/calibration.R @@ -0,0 +1,66 @@ +if(.Platform$OS.type == "unix"){ + root.dir <- "/home/share/CorpCDOs" +}else{ + root.dir <- "//WDSENTINEL/share/CorpCDOs" +} +source(file.path(root.dir, "code", "R", "cds_utils.R")) +source(file.path(root.dir, "code", "R", "cds_functions_generic.R")) + +buildSC <- function(quote, cs, cdsdates){ + SC <- new("creditcurve", + recovery=quote$recovery/100, + startdate=tradedate, + issuer=as.character(quote$ticker)) + quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(quote[4:8]) * 0.01, + running=rep(quote$running*1e-4, 5)) + SC@curve <- cdshazardrate(quotes, SC@recovery, tradedate, cs) + return( SC ) +} + +get.cdsSchedule <- function(tradedate){ + cdsdates <- as.Date(character(0)) + for(tenor in paste0(1:5, "y")){ + cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate)) + } + return( list(cs=couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED", + 1, tradedate, IMMDate(tradedate, "prev")), cdsdates=cdsdates) ) +} + +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"))) + nondefaulted <- singlenames.data[!singlenames.data$ticker %in% index$defaulted,] + cds.cs <- get.cdsSchedule(tradedate) + index$portfolio <- c() + for(i in 1:nrow(nondefaulted)){ + index$portfolio <- c(index$portfolio, buildSC(nondefaulted[i,], cds.cs$cs, cds.cs$cdsdates)) + } + 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())) +} + +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) + index$indexref <- index$tranche.data$bidRefPrice[1]/100 + index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", 0.05, 0, tradedate, + IMMDate(tradedate, "prev")) + index$portfolio.tweaked <- tweakcurves(index, tradedate)$portfolio + index$defaultprob <- 1-SPmatrix(index$portfolio.tweaked, length(index$cs$dates)) + K <- c(0, 0.15, 0.25, 0.35, 1) + index$K <- adjust.attachments(K, index$loss, index$factor) + index$tranche.upf <- index$tranche.data$Mid + index$tranche.running <- index$tranche.data$Coupon + ##convert the quotes + ## - we convert to protection terms x->1-x/100 + ## - we remove accrued x->x-acc + ## - we weight it by the size of the tranche + ## - we take the cumsum to convert to 0-5, 0-10, 0-15 quotes, etc... + ## 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())) +} |
