diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/calibration.R | 35 | ||||
| -rw-r--r-- | R/cds_functions_generic.R | 4 | ||||
| -rw-r--r-- | R/cds_utils.R | 4 | ||||
| -rw-r--r-- | R/mlpdb.R | 3 |
4 files changed, 23 insertions, 23 deletions
diff --git a/R/calibration.R b/R/calibration.R index 73479fbd..e1b8a74f 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -48,33 +48,30 @@ set.singlenamesdata <- function(index, tradedate){ } set.tranchedata <- function(index, tradedate){ - temp <- get.tranchequotes(index$name, tradedate) - if(!is.na(index$indexrefprice[1])){ - index$indexrefprice <- temp$indexrefprice[1]/100 + temp <- get.tranchequotes(index$name, index$tenor, tradedate) + if(!is.na(temp$indexrefprice[1])&&temp$indexrefprice[1]!=0){ + index$priceref <- temp$indexrefprice[1]/100 }else{ - index$indexrefprice <- 1 + index$priceref <- 1 } - index$indexrefspread <- temp$indexrefspread*1e-4 - index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", index$indexrefspread, + 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)) + index$defaultprob <- 1 - SPmatrix(index$portfolio, length(index$cs$dates)) negprob <- which(index$defaultprob<0, arr.ind=T) if(nrow(negprob)>0){ stop(paste(index$portfolio[[negprob[1,]]]@issuer, "has negative probability, check single names data")) } K <- c(0, temp$detach/100) index$K <- adjust.attachments(K, index$loss, index$factor) - index$tranche.upf <- temp$upfront - index$tranche.running <- temp$running*1e-4 - - ## 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))) - return(index) + index$tranche.upf <- temp$trancheupfront + index$tranche.running <- temp$trancherunning*1e-4 + ## compute dirty protection price + if(length(grep("hy", index$name, ignore.case=TRUE))>0){ + index$quotes <- 1-index$tranche.upf/100-cdsAccrued(tradedate, index$tranche.running) + }else{ + index$quotes <- index$tranche.upf/100+cdsAccrued(tradedate, index$tranche.running) + } + return( index ) } diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index 26037343..064b50eb 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -553,7 +553,7 @@ indexpv <- function(index, epsilon=0, tradedate=Sys.Date(), clean=TRUE){ startdate <- tradedate + 1
cl.list <- unlist(lapply(portfolio, function(x){couponleg(index$cs, x@curve, startdate)}))
pl.list <- unlist(lapply(portfolio, function(x){defaultleg(index$cs, x@curve, x@recovery, startdate)}))
- accrued <- cdsAccrued(tradedate, index$coupon)
+ accrued <- cdsAccrued(tradedate, index$spreadref)
r <- list(cl = mean(cl.list) - accrued, pl = mean(pl.list), bp = 1+mean(cl.list-pl.list))
if(clean){
r$bp <- r$bp-accrued
@@ -614,7 +614,7 @@ tweakcurves <- function(index, tradedate=Sys.Date()){ ## computes the tweaking factor
epsilon <- 0
f <- function(epsilon, ...){
- abs(indexpv(index, epsilon, tradedate)$bp - index$indexref)
+ abs(indexpv(index, epsilon, tradedate)$bp - index$priceref)
}
epsilon <- optimize(f, c(-0.5, 0.5), index, tol=1e-6)$minimum
cat("tweak = ", epsilon, "\n")
diff --git a/R/cds_utils.R b/R/cds_utils.R index ec1197bc..d48bbd11 100644 --- a/R/cds_utils.R +++ b/R/cds_utils.R @@ -137,7 +137,9 @@ cdsAccrued <- function(tradedate, coupon){ }
cdsMaturity <- function(tenor, date=Sys.Date()){
- return ( IMMDate(addTenor(date, tenor)) )
+ r <- IMMDate(addTenor(date, tenor))
+ names(r) <- tenor
+ return ( r )
}
yearFrac <- function(date1, date2, daycount="act/365") {
@@ -51,7 +51,8 @@ indexsplit <- function(indexname){ get.tranchequotes <- function(indexname, tenor='5yr', date=Sys.Date()){ sqlstr <- paste("select * from tranche_quotes where index='%s'", - "and series=%s and quotedate='%s' and tenor = '%s' order by attach asc") + "and series=%s and quotedate='%s' and tenor = '%s'", + "and quotesource='MKIT' order by attach asc") temp <- indexsplit(indexname) r <- dbGetQuery(mlpdbCon, sprintf(sqlstr, temp$index, temp$series, date, tenor)) return( r ) |
