aboutsummaryrefslogtreecommitdiffstats
path: root/R/calibrate_tranches_BC.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/calibrate_tranches_BC.R')
-rw-r--r--R/calibrate_tranches_BC.R81
1 files changed, 14 insertions, 67 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index b8e13f7d..cd0d85fc 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -8,11 +8,13 @@ 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", "yieldcurve.R"))
source(file.path(root.dir, "code", "R", "optimization.R"))
-load.index("hy21")
+source(file.path(root.dir, "code", "R", "calibration.R"))
+load.index("hy21", "index_definitions_bt.yml")
library(lossdistrib)
n.int <- 250
attach(GHquad(n.int))
+Ngrid <- 201
alldates <- seq(as.Date("2014-01-01"), as.Date("2014-05-05"), by="1 day")
rhomat <- c()
@@ -23,74 +25,19 @@ for(i in seq_along(bus.dates)){
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
-
- cdsdates <- as.Date(character(0))
- for(tenor in paste0(1:5, "y")){
- cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
- }
-
- 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.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")
-
- ##calibrate tranches using base correlation
- defaultprob <- 1 - SurvProb
-
- 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))
+ set.singlenamesdata(hy21, tradedate)
+ set.tranchedata(hy21, tradedate)
## 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))
+ f <- function(rho, index, N, i){
+ temp <- with(index,
+ BClossdistC(defaultprob, issuerweights, 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(i in 2:length(Kmodified)){
+ for(i in 1:(length(hy21$K)-1)){
q <- quotes[i-1]
- rho <- optimize(f, interval=c(0,1),
- defaultprob, issuerweights, recov, Ngrid, Kmodified, cs, q)$minimum
+ rho <- optimize(f, interval=c(0,1), index=hy21, N=Ngrid, i=i)$minimum
rhovec <- c(rhovec, rho)
}
rhovec <- c(0, rhovec)
@@ -113,9 +60,9 @@ for(i in seq_along(bus.dates)){
## deltas <- dPVtranche/dPVindex
##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)
+ deltas <- rep(0, length(hy21$K)-1)
+ for(i in seq_along(hy21$K[-1])){
+ deltas[i] <- BCtranche.delta(hy21, hy21$K[i], hy21$K[i+1], rhovec[i], rhovec[i+1], Z, w, Ngrid, tradedate)
}
deltasmat <- rbind(deltasmat, deltas)
rhomat <- rbind(rhomat, rhovec)