diff options
| -rw-r--r-- | R/calibration.R | 27 | ||||
| -rw-r--r-- | R/mlpdb.R | 10 |
2 files changed, 23 insertions, 14 deletions
diff --git a/R/calibration.R b/R/calibration.R index 674e1817..73479fbd 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -17,25 +17,29 @@ buildSC <- function(quote, cs, cdsdates){ return( SC ) } -get.cdsSchedule <- function(tradedate){ +get.cdsSchedule <- function(tradedate, indexmaturity){ cdsdates <- as.Date(character(0)) - for(tenor in paste0(1:5, "y")){ - cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate)) + for(tenor in paste0(c(1:5, 7,10), "y")){ + newdate <- cdsMaturity(tenor, date=tradedate) + cdsdates <- c(cdsdates, newdate) + if(newdate>indexmaturity){ + break + } } return( list(cs=couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED", 1, tradedate, IMMDate(tradedate, "prev")), cdsdates=cdsdates) ) } set.singlenamesdata <- function(index, tradedate){ - cds.cs <- get.cdsSchedule(tradedate) + cds.cs <- get.cdsSchedule(tradedate, index$maturity) quotes <- get.indexquotes(index$name, tradedate) - tenor <- paste0(1:5, "y") + tenor <- names(cds.cs$cdsdates) index$portfolio <- list() for(i in seq_along(quotes$tickers)){ quote <- list(ticker = quotes$ticker[i], running = quotes$spread_curve[i, tenor] * 1e-4, upfront = quotes$upfront_curve[i, tenor] * 0.01, - recovery = quotes$recovery[i,tenor][1]) + recovery = as.double(quotes$recovery[i,tenor][1])) index$portfolio <- c(index$portfolio, buildSC(quote, cds.cs$cs, cds.cs$cdsdates)) } index$issuerweights <- rep(1/length(index$portfolio), length(index$portfolio)) @@ -45,9 +49,14 @@ set.singlenamesdata <- function(index, tradedate){ set.tranchedata <- function(index, tradedate){ temp <- get.tranchequotes(index$name, tradedate) - index$indexref <- temp$refbasketprice[1]/100 - index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", 0.05, 0, tradedate, - IMMDate(tradedate, "prev")) + if(!is.na(index$indexrefprice[1])){ + index$indexrefprice <- temp$indexrefprice[1]/100 + }else{ + index$indexrefprice <- 1 + } + index$indexrefspread <- temp$indexrefspread*1e-4 + index$cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", index$indexrefspread, + 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) @@ -13,7 +13,7 @@ load.index <- function(name, date=Sys.Date(), tenor="5yr"){ 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(coupon=0.05, factor=r$indexfactor/100, maturity=r$maturity, + return(list(tenor=tenor, factor=r$indexfactor/100, maturity=r$maturity, loss=r$cumulativeloss/100, recovery=0.4, name=name)) } @@ -49,10 +49,10 @@ indexsplit <- function(indexname){ return(list(index=toupper(index), series=series)) } -get.tranchequotes <- function(indexname, date=Sys.Date()){ - sqlstr <- paste("select * from quotes where index='%s'", - "and series=%s and quotedate='%s' order by attach asc") +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") temp <- indexsplit(indexname) - r <- dbGetQuery(mlpdbCon, sprintf(sqlstr, temp$index, temp$series, date)) + r <- dbGetQuery(mlpdbCon, sprintf(sqlstr, temp$index, temp$series, date, tenor)) return( r ) } |
