diff options
Diffstat (limited to 'R/cds_functions_generic.R')
| -rw-r--r-- | R/cds_functions_generic.R | 12 |
1 files changed, 9 insertions, 3 deletions
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 |
