aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/calibrate_tranches_BC.R33
-rw-r--r--R/cds_functions_generic.R5
-rw-r--r--R/mlpdb.R11
3 files changed, 26 insertions, 23 deletions
diff --git a/R/calibrate_tranches_BC.R b/R/calibrate_tranches_BC.R
index b002e515..319bfb7b 100644
--- a/R/calibrate_tranches_BC.R
+++ b/R/calibrate_tranches_BC.R
@@ -13,56 +13,53 @@ source(file.path(root.dir, "code", "R", "mlpdb.R"))
library(lossdistrib)
n.int <- 250
-attach(GHquad(n.int))
+list2env(GHquad(n.int), envir=parent.frame())
Ngrid <- 201
alldates <- seq(as.Date("2014-03-04"), as.Date("2014-01-05"), by="1 day")
-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, K) + quote))
+aux <- function(rho, index, K, quote, spread){
+ temp <- BCtranche.legs(index, K, rho)
+ return(abs(temp$pl+temp$cl*spread + quote))
}
bus.dates <- as.Date(names(which(isBusinessDay(calendar="UnitedStates/GovernmentBond", alldates))))
-for(index.name in c("hy19")){
+for(index.name in c("hy19", "hy21", "ig19", "ig21")){
rhomat <- c()
deltasmat <- c()
gammasmat <- c()
for(i in seq_along(bus.dates)){
tradedate <- bus.dates[i]
exportYC(tradedate)
- index <- load.index(index.name, tradedate, "5yr")
+ index <- load.index(index.name, tradedate, "5yr", Z, w, Ngrid)
## calibrate the single names curves
index <- set.singlenamesdata(index, tradedate)
index <- set.tranchedata(index, tradedate)
## calibrate the tranches using base correlation
- dT <- index$cs$coupons/index$spreadref
rhovec <- c(0)
for(j in 1:(length(index$K)-1)){
##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$tranche.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$tranche.quotes[j]*(index$K[j+1]-index$K[j])
+ temp <- BCtranche.legs(index, index$K[j], rhovec[j])
+ q <- index$tranche.quotes[j] * (index$K[j+1]-index$K[j])-
+ temp$pl - temp$cl*index$tranche.running[j]
}
- rho <- optimize(aux, interval=c(0,1), index=index, N=Ngrid, K=index$K[j+1], quote=q)$minimum
+ rho <- optimize(aux, interval=c(0,1), index=index, K=index$K[j+1], quote=q,
+ spread=index$tranche.running[j])$minimum
rhovec <- c(rhovec, rho)
}
index$rho <- rhovec
- index$cs$coupons <- dT*index$spreadref
- temp <- BCtranche.delta(index, Z, w, Ngrid, tradedate)
+ temp <- BCtranche.delta(index, tradedate)
deltasmat <- rbind(deltasmat, temp$deltas)
gammasmat <- rbind(gammasmat, temp$gammas)
- 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$spreadref))/index$spreadref
+ temp <- BCtranche.pv(index)
+ duration <- (temp$cl-cdsAccrued(tradedate, index$tranche.running))/index$tranche.running
rhomat <- rbind(rhomat, rhovec)
cat(i, "\n")
}
+ assign(index.name, index)
}
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index f5afd0f5..53432c5b 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -561,10 +561,11 @@ indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE,
cl.list <- unlist(lapply(portfolio, function(x){couponleg(cs, x@curve, startdate)}))
pl.list <- unlist(lapply(portfolio, function(x){defaultleg(cs, x@curve, x@recovery, startdate)}))
spread <- index$quotes$spread[index$quotes$maturity==maturity]
- accrued <- cdsAccrued(tradedate, spread)
- r <- list(cl = mean(cl.list) - accrued, pl = mean(pl.list), bp = 1+mean(cl.list-pl.list))
+ r <- list(cl = spread * mean(cl.list), pl = mean(pl.list), bp = 1+mean(spread*cl.list-pl.list))
if(clean){
+ accrued <- cdsAccrued(tradedate, spread)
r$bp <- r$bp-accrued
+ r$cl <- r$cl-accrued
}
return(r)
}
diff --git a/R/mlpdb.R b/R/mlpdb.R
index a3ebd5ec..11c146a5 100644
--- a/R/mlpdb.R
+++ b/R/mlpdb.R
@@ -9,12 +9,17 @@ nameToBasketID <- function(name, date=Sys.Date()){
return(as.integer(r))
}
-load.index <- function(name, date=Sys.Date(), tenor="5yr", Z=Z, w=w, N=Ngrid){
+load.index <- function(name, date=Sys.Date(), tenor="5yr", Z, w, N){
id <- nameToBasketID(name, date)
sqlstr <- "SELECT indexfactor, cumulativeloss, maturity from index_desc where basketid=%s and tenor='%s'"
r <- as.list(dbGetQuery(mlpdbCon, sprintf(sqlstr, id, tenor)))
- return(list(tenor=tenor, factor=r$indexfactor/100, maturity=r$maturity,
- loss=r$cumulativeloss/100, recovery=0.4, name=name, Z=Z, w=w, N=N))
+ if(any(c(missing(Z), missing(w), missing(N)))){
+ return( list(tenor=tenor, factor=r$indexfactor/100, maturity=r$maturity,
+ loss=r$cumulativeloss/100, recovery=0.4))
+ }else{
+ return( list(tenor=tenor, factor=r$indexfactor/100, maturity=r$maturity,
+ loss=r$cumulativeloss/100, recovery=0.4, name=name, Z=Z, w=w, N=N))
+ }
}
cdslist <- function(indexname, date=Sys.Date()){