aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/calibrate_tranches_BC.R27
-rw-r--r--R/calibration.R13
2 files changed, 27 insertions, 13 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index 69298f13..4cb139eb 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -18,9 +18,9 @@ Ngrid <- 201
alldates <- seq(as.Date("2014-01-01"), as.Date("2014-01-05"), by="1 day")
-aux <- function(rho, index, N, i){
+aux <- function(rho, index, N, K, quote){
temp <- BClossdistC(index$defaultprob, index$issuerweights, index$recov, rho, Z, w, N)
- return(abs(tranche.pv(temp$L, temp$R, index$cs, 0, index$K[i+1]) + index$quotes[i]))
+ return(abs(tranche.pv(temp$L, temp$R, index$cs, 0, K) + quote))
}
bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
@@ -31,27 +31,36 @@ for(index.name in c("hy19", "hy21"){
for(i in seq_along(bus.dates)){
tradedate <- bus.dates[i]
exportYC(tradedate)
- index <- load.index(index.name, tradedate)
- ## calibrate HY21 the single names curves
+ index <- load.index(index.name, tradedate, "5yr")
+ ## calibrate the single names curves
index <- set.singlenamesdata(index, tradedate)
index <- set.tranchedata(index, tradedate)
-
## calibrate the tranches using base correlation
- rhovec <- c()
+ dT <- index$cs$coupons/index$spreadref
+ rhovec <- c(0)
for(j in 1:(length(index$K)-1)){
- rho <- optimize(aux, interval=c(0,1), index=index, N=Ngrid, i=j)$minimum
+ ##use the current tranche coupon
+ index$cs$coupons <- dT*index$tranche.running[j]
+ ## we compute the 0-index$K[j+1] equivalent quote using the coupon of the jth quote
+ if(j==1){
+ q <- index$quotes[j]*index$K[j+1]
+ }else{
+ q <- BCtranche.pv(index, 0, index$K[j], 0, rhovec[j], Z, w, Ngrid, TRUE)$bp * index$K[j]+
+ index$quotes[j]*(index$K[j+1]-index$K[j])
+ }
+ rho <- optimize(aux, interval=c(0,1), index=index, N=Ngrid, K=index$K[j+1], quote=q)$minimum
rhovec <- c(rhovec, rho)
}
- rhovec <- c(0, rhovec)
index$rho <- rhovec
temp <- BCtranche.delta(index, Z, w, Ngrid, tradedate)
deltasmat <- rbind(deltasmat, temp$deltas)
gammasmat <- rbind(gammasmat, temp$gammas)
+ index$cs$coupons <- dT*index$spreadref
cl <- c()
for(j in 1:(length(index$K)-1)){
cl <- c(cl, BCtranche.pv(index, index$K[j], index$K[j+1], rhovec[j], rhovec[j+1], Z, w, Ngrid)$cl)
}
- duration <- (cl - cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
+ duration <- (cl - cdsAccrued(tradedate, index$spreadref))/index$spreadref
rhomat <- rbind(rhomat, rhovec)
cat(i, "\n")
}
diff --git a/R/calibration.R b/R/calibration.R
index e1b8a74f..97b06ed4 100644
--- a/R/calibration.R
+++ b/R/calibration.R
@@ -49,14 +49,19 @@ set.singlenamesdata <- function(index, tradedate){
set.tranchedata <- function(index, tradedate){
temp <- get.tranchequotes(index$name, index$tenor, tradedate)
+ index$spreadref <- temp$indexrefspread[1]*1e-4
+ if(index$name=="ig19" || index$name=="ig21"){
+ index$spreadref <- 0.01
+ }
+ index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", index$spreadref,
+ 0, tradedate, IMMDate(tradedate, "prev"))
if(!is.na(temp$indexrefprice[1])&&temp$indexrefprice[1]!=0){
index$priceref <- temp$indexrefprice[1]/100
}else{
- index$priceref <- 1
+ sc <- new("flatcurve", h=temp$indexrefspread[1]*1e-4/(1-index$recovery))
+ index$priceref <- 1 + cdspv(index$cs, sc, index$recovery, tradedate)-
+ cdsAccrued(tradedate, index$spreadref)
}
- index$spreadref <- temp$indexrefspread[1]*1e-4
- index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", index$spreadref,
- 0, tradedate, IMMDate(tradedate, "prev"))
index$portfolio <- tweakcurves(index, tradedate)$portfolio
index$defaultprob <- 1 - SPmatrix(index$portfolio, length(index$cs$dates))
negprob <- which(index$defaultprob<0, arr.ind=T)