diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/cds_functions_generic.R | 10 | ||||
| -rw-r--r-- | R/script_calibrate_tranches.R | 8 |
2 files changed, 10 insertions, 8 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index a61dc394..32c52e51 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -461,7 +461,7 @@ cdshazardrate.shaped <- function(upfront, running, maturity, shape, R=0.4) { return(sc)
}
-cdshazardrate <- function(quotes, R=0.4, tradedate=Sys.Date()){
+cdshazardrate <- function(quotes, R=0.4, tradedate=Sys.Date(), cs.all){
## bootstrap the implied hazard rate curve of the cds based on the upfront
## and running quotes, as well as maturity and recovery
previous.maturity <- tradedate
@@ -474,15 +474,15 @@ cdshazardrate <- function(quotes, R=0.4, tradedate=Sys.Date()){ next
}
maturity <- quotes$maturity[i]
- cs <- couponSchedule(IMMDate(tradedate), maturity, "Q", "FIXED", quotes$running[i],
- tradedate, IMMDate(tradedate, "prev"))
+ cs <- cs.all[cs.all$dates<=maturity,]
+ cs$coupons <- cs$coupons * quotes$running[i]
new.h <- 0.05
flength <- nrow(cs) - nrow(previous.cs)
hvec <- c(previous.hvec, rep(new.h, flength))
sc <- new("defaultcurve", dates=cs$dates, hazardrates=hvec)
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){
+ 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)/
dcdspv(cs, sc, R, index, tradedate)
hvec <- c(previous.hvec, rep(new.h, flength))
@@ -618,7 +618,7 @@ tweakcurves <- function(portfolio, index, tradedate=Sys.Date()){ }
epsilon <- optimize(f, c(-0.5, 0.5), portfolio, index, tol=1e-6)$minimum
cat("tweak = ", epsilon, "\n")
- return( tweakportfolio(portfolio, epsilon) )
+ return( list(portfolio=tweakportfolio(portfolio, epsilon), epsilon=epsilon) )
}
survivalProbability1 <- function(startdate, date, survival.curve) {
diff --git a/R/script_calibrate_tranches.R b/R/script_calibrate_tranches.R index d79875c4..a0f0afbf 100644 --- a/R/script_calibrate_tranches.R +++ b/R/script_calibrate_tranches.R @@ -43,10 +43,12 @@ bps <- 1e-4 cdsdates <- as.Date(character(0))
for(tenor in paste0(1:5, "y")){
- cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate+1))
+ cdsdates <- c(cdsdates, cdsMaturity(tenor, date=tradedate))
}
hy21portfolio <- c()
+cs <- couponSchedule(IMMDate(tradedate), cdsdates[length(cdsdates)], "Q", "FIXED",
+ 1, tradedate, IMMDate(tradedate, "prev"))
for(i in 1:nrow(nondefaulted)){
SC <- new("creditcurve",
recovery=nondefaulted$recovery[i]/100,
@@ -54,7 +56,7 @@ for(i in 1:nrow(nondefaulted)){ issuer=as.character(nondefaulted$ticker[i]))
quotes <- data.frame(maturity=cdsdates, upfront = as.numeric(nondefaulted[i,4:8]) /100,
running=rep(nondefaulted$running[i] * bps, 5))
- SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate)
+ SC@curve <- cdshazardrate(quotes, nondefaulted$recovery[i]/100, tradedate, cs)
hy21portfolio <- c(hy21portfolio, SC)
}
@@ -70,7 +72,7 @@ tranche.upf <- market.data$Mid tranche.running <- c(0.05, 0.05, 0.05, 0.05)
# get the index ref
hy21$indexref <- market.data$bidRefPrice[1]/100
-hy21portfolio.tweaked <- tweakcurves(hy21portfolio, hy21, tradedate)
+hy21portfolio.tweaked <- tweakcurves(hy21portfolio, hy21, tradedate)$portfolio
SurvProb <- SPmatrix(hy21portfolio.tweaked, hy21)
Ngrid <- 2 * nrow(nondefaulted) + 1
|
