diff options
| -rw-r--r-- | R/calibration.R | 20 | ||||
| -rw-r--r-- | R/cds_functions_generic.R | 12 |
2 files changed, 21 insertions, 11 deletions
diff --git a/R/calibration.R b/R/calibration.R index b6a05c91..3b1755bb 100644 --- a/R/calibration.R +++ b/R/calibration.R @@ -11,9 +11,9 @@ buildSC <- function(quote, cs, cdsdates){ return( SC ) } -get.cdsSchedule <- function(tradedate){ +get.cdsSchedule <- function(tradedate, tenors=c(1:5, 7, 10)) { cdsdates <- as.Date(character(0)) - for(tenor in paste0(c(1:5, 7, 10), "y")){ + for(tenor in paste0(tenors, "y")){ newdate <- cdsMaturity(tenor, date=tradedate) cdsdates <- c(cdsdates, newdate) } @@ -22,18 +22,22 @@ get.cdsSchedule <- function(tradedate){ cdsdates=cdsdates) ) } -set.singlenamesdata <- function(index, tradedate, recov=NULL){ - cds.cs <- get.cdsSchedule(tradedate) +set.singlenamesdata <- function(index, tradedate, recov=NULL) { + cds.cs <- if (index$type %in% c("HY", "XO")) { # no need to build the full 10y curve + get.cdsSchedule(tradedate, 1:5) + } else { + get.cdsSchedule(tradedate) + } quotes <- get.singlenamesquotes(index$name, tradedate) tenor <- names(cds.cs$cdsdates) index$portfolio <- list() for(i in seq_along(quotes$tickers)){ if(quotes$ticker[i] %in% c("IACI", "TITANC", "ESPSAN", "CBRY")){ - quotes$spread_curve[i,] <- rep(0.001,8) - quotes$upfront_curve[i,] <- rep(0,8) - quotes$recovery[i,] <- rep(0.4,8) + quotes$spread_curve[i,] <- rep(0.001, 8) + quotes$upfront_curve[i,] <- rep(0, 8) + quotes$recovery[i,] <- rep(0.4, 8) } - quotes$upfront_curve[i, tenor] <- pmax(-yearFrac(tradedate+2,cds.cs$cdsdates)* + quotes$upfront_curve[i, tenor] <- pmax(-yearFrac(tradedate+2, cds.cs$cdsdates)* quotes$spread_curve[i, tenor]*1e-2, quotes$upfront_curve[i, tenor]) quote <- list(ticker = quotes$ticker[i], diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index 31c0bd3c..e8fd569d 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -526,7 +526,7 @@ cdshazardrate <- function(quotes, R=0.4, tradedate=Sys.Date(), cs.all){ sc <- NULL for(i in 1:nrow(quotes)){ maturity <- quotes$maturity[i] - if(tradedate+1>=maturity){ + if(tradedate + 1 >= maturity){ next } cs <- cs.all[cs.all$unadj.dates<=maturity,] @@ -541,11 +541,17 @@ cdshazardrate <- function(quotes, R=0.4, tradedate=Sys.Date(), cs.all){ } index <- c(rep(0, length(previous.hvec)), rep(1, flength)) acc <- cdsAccrued(tradedate, quotes$running[i]) - while(abs(cdspv(cs, sc, R, tradedate) + quotes$upfront[i] - acc) > eps){ - new.h <- new.h - (quotes$upfront[i] + cdspv(cs, sc, R, tradedate) - acc)/ + count <- 0 + while(abs(cdspv(cs, sc, R, tradedate) + quotes$upfront[i] - acc) > eps && count < 500) { + dh <- - (quotes$upfront[i] + cdspv(cs, sc, R, tradedate) - acc)/ dcdspv(cs, sc, R, tradedate, index) + while( (new.h + dh) < 0 ) { + dh <- dh * 0.8 + } + new.h <- new.h + dh hvec <- c(previous.hvec, rep(new.h, flength)) sc@hazardrates <- hvec + count <- count + 1 } previous.hvec <- hvec previous.maturity <- maturity |
