aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/calibrate_tranches_BC.R193
1 files changed, 97 insertions, 96 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index 4699f87b..b8e13f7d 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -6,116 +6,117 @@ if(.Platform$OS.type == "unix"){
source(file.path(root.dir, "code", "R", "cds_utils.R"))
source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
-source(file.path(root.dir, "code", "R", "tranche_functions.R"))
source(file.path(root.dir, "code", "R", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
load.index("hy21")
+library(lossdistrib)
-tradedate <- as.Date("2014-04-16")
-MarkitData <- getMarkitIRData(tradedate)
-L1m <- buildMarkitYC(MarkitData, dt = 1/12)
-L2m <- buildMarkitYC(MarkitData, dt = 1/6)
-L3m <- buildMarkitYC(MarkitData)
-L6m <- buildMarkitYC(MarkitData, dt = 1/2)
-setEvaluationDate(as.Date(MarkitData$effectiveasof))
-setCalendarContext("TARGET")
-
-## calibrate HY21
-## calibrate the single names curves
-singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
- paste0("hy21_singlenames_", tradedate, ".csv")))
-nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy21$defaulted,]
-bps <- 1e-4
+n.int <- 250
+attach(GHquad(n.int))
-cdsdates <- as.Date(character(0))
-for(tenor in paste0(1:5, "y")){
- cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
-}
+alldates <- seq(as.Date("2014-01-01"), as.Date("2014-05-05"), by="1 day")
+rhomat <- c()
+deltasmat <- 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)
+ ## calibrate HY21
+ ## calibrate the single names curves
+ singlenames.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
+ paste0("hy21_singlenames_", tradedate, ".csv")))
+ nondefaulted <- singlenames.data[!singlenames.data$ticker %in% hy21$defaulted,]
+ bps <- 1e-4
-hy21portfolio <- c()
-cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
- 1, tradedate, IMMDate(tradedate, "prev"))
-for(i in 1:nrow(nondefaulted)){
- SC <- new("creditcurve",
- recovery=nondefaulted$recovery[i]/100,
- startdate=tradedate,
- issuer=as.character(nondefaulted$ticker[i]))
- quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8])*0.01,
- running=rep(nondefaulted$running[i]*bps, 5))
- SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate, cs)
- hy21portfolio <- c(hy21portfolio, SC)
-}
+ cdsdates <- as.Date(character(0))
+ for(tenor in paste0(1:5, "y")){
+ cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
+ }
-issuerweights <- rep(1/length(hy21portfolio), length(hy21portfolio))
-hy21$indexref <- 1.0725
+ hy21portfolio <- c()
+ cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
+ 1, tradedate, IMMDate(tradedate, "prev"))
+ for(i in 1:nrow(nondefaulted)){
+ SC <- new("creditcurve",
+ recovery=nondefaulted$recovery[i]/100,
+ startdate=tradedate,
+ issuer=as.character(nondefaulted$ticker[i]))
+ quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8])*0.01,
+ running=rep(nondefaulted$running[i]*bps, 5))
+ SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate, cs)
+ hy21portfolio <- c(hy21portfolio, SC)
+ }
+ n.credit <- length(hy21portfolio)
+ issuerweights <- rep(1/n.credit, n.credit)
+ tranche.data <- read.csv(file.path(root.dir, "Scenarios", "Calibration",
+ paste0("hy21_tranches_", tradedate, ".csv")), header=TRUE)
+ hy21$indexref <- tranche.data$bidRefPrice[1]/100
-temp <- tweakcurves(hy21portfolio, hy21, tradedate)
-hy21portfolio.tweaked <- temp$portfolio
-cs <- couponSchedule(IMMDate(tradedate), hy21$maturity,"Q", "FIXED", 0.05, 0, tradedate,
- IMMDate(tradedate, "prev"))
-SurvProb <- SPmatrix(hy21portfolio.tweaked, length(cs$dates))
-## load common parameters
-K <- c(0, 0.05, 0.1, 0.15, 0.25, 0.35, 1)
-Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor)
-tranche.upf <- c(15.2, 62.125, 86.625, 102.875, 113.4375, 119.0375)
-tranche.running <- rep(0.05, 6)
+ temp <- tweakcurves(hy21portfolio, hy21, tradedate)
+ hy21portfolio.tweaked <- temp$portfolio
+ cs <- couponSchedule(IMMDate(tradedate), hy21$maturity,"Q", "FIXED", 0.05, 0, tradedate,
+ IMMDate(tradedate, "prev"))
+ SurvProb <- SPmatrix(hy21portfolio.tweaked, length(cs$dates))
+ ## load common parameters
+ K <- c(0, 0.15, 0.25, 0.35, 1)
+ Kmodified <- adjust.attachments(K, hy21$loss, hy21$factor)
+ tranche.upf <- tranche.data$Mid
+ tranche.running <- tranche.data$Coupon
-Ngrid <- 2 * nrow(nondefaulted)+1
-recov <- sapply(hy21portfolio.tweaked, attr, "recovery")
+ Ngrid <- 2 * nrow(nondefaulted) + 1
+ recov <- sapply(hy21portfolio.tweaked, attr, "recovery")
-##calibrate tranches using base correlation
-n.int <- 250
-n.credit <- length(hy21portfolio)
-quadrature <- gauss.quad.prob(n.int, "normal")
-w <- quadrature$weights
-Z <- quadrature$nodes
-defaultprob <- 1 - SurvProb
+ ##calibrate tranches using base correlation
+ defaultprob <- 1 - SurvProb
-dK <- diff(Kmodified)
-acc <- cdsAccrued(tradedate, 0.05)
+ dK <- diff(Kmodified)
+ acc <- cdsAccrued(tradedate, tranche.running)
-##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...
-quotes <- cumsum(dK * (1-tranche.upf/100-acc))
+ ##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...
+ quotes <- cumsum(dK * (1-tranche.upf/100-acc))
-## calibrate the tranches using base correlation
-rhovec <- c()
-f <- function(rho, ...){
- temp <- BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, Ngrid)
- return(abs(tranche.pv(temp$L, temp$R, cs, 0, Kmodified[i]) + q))
-}
+ ## calibrate the tranches using base correlation
+ rhovec <- c()
+ f <- function(rho, ...){
+ temp <- BClossdistC(defaultprob, issuerweights, recov, rho, Z, w, Ngrid)
+ return(abs(tranche.pv(temp$L, temp$R, cs, 0, Kmodified[i]) + q))
+ }
-rhovec <- c()
-for(i in 2:length(Kmodified)){
- q <- quotes[i-1]
- rho <- optimize(f, interval=c(0,1),
- defaultprob, issuerweights, recov, Ngrid, Kmodified, cs, q)$minimum
- rhovec <- c(rhovec, rho)
-}
-rhovec <- c(0, rhovec)
+ rhovec <- c()
+ for(i in 2:length(Kmodified)){
+ q <- quotes[i-1]
+ rho <- optimize(f, interval=c(0,1),
+ defaultprob, issuerweights, recov, Ngrid, Kmodified, cs, q)$minimum
+ rhovec <- c(rhovec, rho)
+ }
+ rhovec <- c(0, rhovec)
-##compute deltas by blipping the curves
-portfolioplus <- tweakportfolio(hy21portfolio.tweaked, 1e-4)
-defaultprobplus <- 1 - SPmatrix(portfolioplus, length(cs$dates))
-portfoliominus <- tweakportfolio(hy21portfolio.tweaked, -1e-4)
-defaultprobminus <- 1 - SPmatrix(portfoliominus, length(cs$dates))
-test <- matrix(0, 6, 2)
-for(i in 2:7){
- tempminus <- BClossdistC(defaultprobminus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
- tempplus <- BClossdistC(defaultprobplus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
- test[i-1,1] <- tranche.pv(tempminus$L, tempminus$R, cs, 0, Kmodified[i])
- test[i-1,2] <- tranche.pv(tempplus$L, tempplus$R, cs, 0, Kmodified[i])
-}
-dPVtranche <- diff(c(0, test[,1]))/dK - diff(c(0, test[,2]))/dK
-dPVindex <- indexpv(portfoliominus, hy21, tradedate=tradedate, clean=FALSE)$bp-
- indexpv(portfolioplus, hy21, tradedate=tradedate, clean=FALSE)$bp
-deltas <- dPVtranche/dPVindex
+ ##compute deltas by blipping the curves
+ ## portfolioplus <- tweakportfolio(hy21portfolio.tweaked, 1e-4)
+ ## defaultprobplus <- 1 - SPmatrix(portfolioplus, length(cs$dates))
+ ## portfoliominus <- tweakportfolio(hy21portfolio.tweaked, -1e-4)
+ ## defaultprobminus <- 1 - SPmatrix(portfoliominus, length(cs$dates))
+ ## test <- matrix(0, 6, 2)
+ ## for(i in 2:7){
+ ## tempminus <- BClossdistC(defaultprobminus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
+ ## tempplus <- BClossdistC(defaultprobplus, issuerweights, recov, rhovec[i], Z, w, Ngrid)
+ ## test[i-1,1] <- tranche.pv(tempminus$L, tempminus$R, cs, 0, Kmodified[i])
+ ## test[i-1,2] <- tranche.pv(tempplus$L, tempplus$R, cs, 0, Kmodified[i])
+ ## }
+ ## dPVtranche <- diff(c(0, test[,1]))/dK - diff(c(0, test[,2]))/dK
+ ## dPVindex <- indexpv(portfoliominus, hy21, tradedate=tradedate, clean=FALSE)$bp-
+ ## indexpv(portfolioplus, hy21, tradedate=tradedate, clean=FALSE)$bp
+ ## deltas <- dPVtranche/dPVindex
-##use BCtranche.delta function
-deltas <- rep(0, 6)
-for(i in 1:6){
- deltas[i] <- BCtranche.delta(hy21portfolio.tweaked, hy21, 0.05, K[i], K[i+1], rhovec[i], rhovec[i+1], Z, w, Ngrid, tradedate)
+ ##use BCtranche.delta function
+ deltas <- rep(0, length(K)-1)
+ for(i in seq_along(K[-1])){
+ deltas[i] <- BCtranche.delta(hy21portfolio.tweaked, hy21, 0.05, K[i], K[i+1], rhovec[i], rhovec[i+1], Z, w, Ngrid, tradedate)
+ }
+ deltasmat <- rbind(deltasmat, deltas)
+ rhomat <- rbind(rhomat, rhovec)
}