aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/cds_functions_generic.R20
-rw-r--r--R/intex_deal_functions.R8
-rw-r--r--R/load_cf.R2
-rw-r--r--R/script_calibrate_tranches.R3
4 files changed, 17 insertions, 16 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index 8ed227a0..a61dc394 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -27,7 +27,7 @@ setClass("defaultprepaycurve", representation(prepayrates="numeric"), contains="
setClass("creditcurve", representation(issuer="character", startdate="Date",
recovery="numeric", curve="defaultcurve"))
-shapedtodpc <- function(cs, sc, startdate){
+shapedtodpc <- function(cs, sc, startdate=Sys.Date()){
## convert a shaped curve to a standard defaultprepaycuve
T <- yearFrac(startdate, cs$dates)
hvec <- sc@shape(T) * sc@h
@@ -428,14 +428,14 @@ dcdspv <- function(cs, sc, recovery, index, tradedate){
}
bondpv <- function(cs, sc, recovery, tradedate=Sys.Date()){
- return( contingentleg(cs, sc, recovery) + couponleg(cs, sc) )
+ return( contingentleg(cs, sc, recovery, tradedate) + couponleg(cs, sc, tradedate) )
}
-dbondpv <- function(cs, sc, recovery, index){
+dbondpv <- function(cs, sc, recovery, index, startdate){
if(missing(index)){
- return( dcontingentleg(cs, sc, recovery) + dcouponleg(cs, sc))
+ return( dcontingentleg(cs, sc, recovery, ,startdate) + dcouponleg(cs, sc, ,startdate))
}else{
- return( dcontingentleg(cs, sc, recovery, index)+dcouponleg(cs, sc, index) )
+ return( dcontingentleg(cs, sc, recovery, index, startdate)+dcouponleg(cs, sc, index, startdate) )
}
}
@@ -495,22 +495,22 @@ cdshazardrate <- function(quotes, R=0.4, tradedate=Sys.Date()){
return(sc)
}
-bondhazardrate.shaped <- function(collateral, shape, R=0.4, alpha=0.25, beta=15){
+bondhazardrate.shaped <- function(collateral, shape, R=0.4, alpha=0.25, beta=15, startdate=Sys.Date()){
## calibrate a default prepay curve to the collateral information
cs <- couponSchedule(collateral$nextpaydate, collateral$maturity,
collateral$frequency, collateral$fixedorfloat,
- collateral$grosscoupon*0.01, collateral$spread*0.01)
+ collateral$grosscoupon*0.01, collateral$spread*0.01, startdate)
sc <- new("shapedcurve", h=0.05, shape=shape, alpha=alpha, beta=beta)
eps <- 1e-8
counter <- 0
if(collateral$price<2){
cat("price is too low\n")
sc <- new("shapedcurve", h=1e6, shape=shape, alpha=alpha, beta=beta)
- return( shapedtodpc(cs, sc) )
+ return( shapedtodpc(cs, sc, startdate) )
}
while(abs(bondpv(cs, sc, R) - collateral$price/100) > eps){
- dh <- (collateral$price/100 - bondpv(cs, sc, R))/dbondpv(cs, sc, R)
+ dh <- (collateral$price/100 - bondpv(cs, sc, R, startdate))/dbondpv(cs, sc, R, ,startdate)
while(sc@h+dh<0){
dh <- 0.5 * dh
}
@@ -521,7 +521,7 @@ bondhazardrate.shaped <- function(collateral, shape, R=0.4, alpha=0.25, beta=15)
stop("didn't reach convergence")
}
}
- return( shapedtodpc(cs, sc) )
+ return( shapedtodpc(cs, sc, startdate) )
}
tweakportfolio <- function(portfolio, epsilon, multiplicative=TRUE){
diff --git a/R/intex_deal_functions.R b/R/intex_deal_functions.R
index b1d3582f..6d1172d6 100644
--- a/R/intex_deal_functions.R
+++ b/R/intex_deal_functions.R
@@ -218,8 +218,8 @@ buildSC <- function(line.item, reinvdate, dealmaturity, global.params, startdate
SC <- stackcurve(SC, line.item, global.params, SC@startdate)
cs <- couponSchedule(line.item$nextpaydate, line.item$maturity,
line.item$frequency, line.item$fixedorfloat,
- line.item$grosscoupon*0.01, line.item$spread*0.01)
- line.item$price <- bondpv(cs, SC@curve, recovery(line.item)) * 100
+ line.item$grosscoupon*0.01, line.item$spread*0.01, startdate)
+ line.item$price <- bondpv(cs, SC@curve, recovery(line.item), startdate) * 100
}else{ #normal case
if(!is.na(line.item$assettype) && line.item$assettype=="Bond"){ #no prepay rate
alpha <- 0
@@ -227,7 +227,7 @@ buildSC <- function(line.item, reinvdate, dealmaturity, global.params, startdate
alpha <- global.params$alpha
}
try <- bondhazardrate.shaped(line.item, global.params$shape,
- recovery(line.item), alpha, global.params$beta)
+ recovery(line.item), alpha, global.params$beta, startdate)
if(is.null(try)){
SC <- stackcurve(SC, line.item, global.params, SC@startdate)
}else{
@@ -378,7 +378,7 @@ intexportfolio.forwardprice <- function(cdrmonthly, recoverymonthly, startdate,
}
forwardcs <- data.table(couponSchedule(nextpaydate=startdate+45, maturity,
frequency="Q", coupontype, margin,
- currentcoupon, startdate=startdate), key="dates")
+ currentcoupon, tradedate=startdate), key="dates")
notionals <- cdrmonthly[date>=startdate, lapply(.SD,function(x)cumprod(1-x/100*1/12)),
.SDcols=paste0("V",1:100)]
recovery <- as.matrix(recoverymonthly[date>=startdate, .SD, .SDcols=paste0("V",1:100)])*
diff --git a/R/load_cf.R b/R/load_cf.R
index c07bc987..1eaa492b 100644
--- a/R/load_cf.R
+++ b/R/load_cf.R
@@ -225,7 +225,7 @@ compute.delta <- function(indexdist, dealweight, cusip.pv, tradedate=Sys.Date(),
calibration.date <- addBusDay(tradedate)
## we assume the index is fully funded - need to be changed depending
## on how we fund the swaps (hence floating coupon instead of fixed)
- cs <- couponSchedule(nextIMMDate(tradedate), hy21$maturity, "Q",
+ cs <- couponSchedule(IMMDate(tradedate), hy21$maturity, "Q",
"FLOAT", 0.05, 0, calibration.date)
nT <- dim(indexdist$L)[2]
Ngrid <- dim(indexdist$L)[1]
diff --git a/R/script_calibrate_tranches.R b/R/script_calibrate_tranches.R
index 10e8f470..d79875c4 100644
--- a/R/script_calibrate_tranches.R
+++ b/R/script_calibrate_tranches.R
@@ -92,7 +92,8 @@ p <- defaultprob
rho <- rep(0.45, n.credit)
result <- matrix(0, 4, n.int)
-for(l in 1:150){
+err <- Inf
+while(err >0.01){
Rstoch <- array(0, dim=c(n.credit, n.int, ncol(SurvProb)))
for(t in 1:ncol(SurvProb)){
for(i in 1:n.credit){