aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/build_scenarios.R66
-rw-r--r--R/cds_functions_generic.R36
-rw-r--r--R/cds_utils.R4
-rw-r--r--R/intex_deal_functions.R48
-rw-r--r--R/params.yml2
5 files changed, 68 insertions, 88 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R
index 98b55122..b6b3d63f 100644
--- a/R/build_scenarios.R
+++ b/R/build_scenarios.R
@@ -39,14 +39,14 @@ source(file.path(root.dir, "code", "R", "etdb.R"))
source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
source(file.path(root.dir, "code", "R", "cds_utils.R"))
-calibration.date <- prevBusDay(tradedate)
-settledate <- getsettledate(tradedate)
+calibration.date <- addBusDay(tradedate, -1)
+settledate <- addBusDay(tradedate, 3)
calibration <- read.table(file.path(root.dir, "Scenarios", "Calibration",
paste0("calibration-", calibration.date,".csv")), sep=",", header=T)
Z <- calibration$Z
w <- calibration$w
-Ngrid <- 201
+
MarkitData <- getMarkitIRData(calibration.date)
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", calibration.date)), header=F)
@@ -58,16 +58,11 @@ L3m <- buildMarkitYC(MarkitData, futurequotes[,2])
L6m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/2)
L12m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1)
+Ngrid <- 201
support <- seq(0, 1, length = Ngrid)
-recoverylag <- 90 ##days
-## reinvestment parameters
-## need to match parameters in build_portfolios.R
-reinvspread <- 0.04
-reinvfixed <- 0.07
-basecase.rollingmaturity <- 84 ##months
-reinvlag <- 3 ##months
n.scenarios <- 100
recov.adj <- 1
+params <- yaml.load_file(file.path(root.dir, "code", "R", "params.yml"))
for(j in seq_along(dealnames)){
load(file.path(root.dir, "Scenarios", paste("Portfolios", tradedate, sep="_"),
@@ -131,9 +126,7 @@ for(j in seq_along(dealnames)){
## linear approximation for monthly scenarios
deal.data <- getdealdata(dealnames[j], tradedate)
deal.datesmonthly <- getdealschedule(deal.data, "1 month")
- deal.datesmonthlylagged <- getdealschedule(deal.data, "1 month", lag=92)
deal.datesmonthly <- deal.datesmonthly[deal.datesmonthly>=settledate]
- deal.datesmonthlylagged <- deal.datesmonthly[deal.datesmonthlylagged>=settledate]
cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
@@ -147,53 +140,24 @@ for(j in seq_along(dealnames)){
}
recoverymonthly <- pmin(recoverymonthly,1)
recoverymonthly[!is.finite(recoverymonthly)] <- 100
- ## compute reinvestment price
+
if(!is.na(deal.data$"Reinv End Date") && deal.data$"Reinv End Date" <= tradedate){
## we cap rolling maturity at the current weighted average maturity of the portfolio
rollingmaturity <- (crossprod(sapply(deal.portfolio$SC, creditcurve.maturity),
deal.portfolio$notional)/sum(deal.portfolio$notional)
- - as.numeric(tradedate))/365*12
+ - as.numeric(tradedate))
}else{
- rollingmaturity <- basecase.rollingmaturity
+ rollingmaturity <- params$rollingmaturity
}
- ## DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthlylagged))
- ## df <- DC$discounts
- ## forwards <- DC$forwards
+
+ ## compute reinvestment price
cdrmonthly.dt <- data.table(date=deal.datesmonthly, t(cdrmonthly), key="date")
recoverymonthly.dt <- data.table(date=deal.datesmonthly, t(recoverymonthly), key="date")
- reinvassets <- get.reinvassets(dealnames[j], tradedate)
-
- reinvprices <- list()
- if(reinvflags[j] && length(reinvassets)>0){
- for(assetname in names(reinvassets)){
- asset <- reinvassets[[assetname]]
- if(asset$coupontype=="FLOAT") {
- coupon <- reinvspread
- }else{
- coupon <- reinvfixed
- }
- ##reinvest tweak
- #coupon <- coupon-0.01
-
- reinvprices[[assetname]] <- foreach(date = iter(deal.datesmonthly), .combine=c) %dopar% {
- 100 * forwardportfolioprice2(cdrmonthly.dt, recoverymonthly.dt, date,
- min(date+rollingmaturity*30, deal.data$maturity),
- asset$coupontype, coupon, asset$liborfloor/100)
- }
- ## reinvprices[[assetname]] <- foreach(date = iter(deal.datesmonthly), .combine=c) %dopar% {
- ## 100 * forwardportfolioprice(deal.portfolio, date,
- ## min(date+rollingmaturity*30, deal.data$maturity),
- ## asset$coupontype, coupon, asset$liborfloor/100)
- ## }
- ## for(i in seq_along(deal.datesmonthly)){
- ## date <- deal.datesmonthly[i]
- ## reinvprices[[assetname]] <-
- ## 100 * forwardportfolioprice2(cdrmonthly.dt, recoverymonthly.dt, date,
- ## min(date+rollingmaturity*30, deal.data$maturity),
- ## asset$coupontype, coupon, asset$liborfloor/100)
- ## }
-
- }
+ if(reinvflags[j]){
+ reinvprices <- compute.reinvprices(dealnames[j], cdrmonthly.dt, recoverymonthly.dt,
+ params, rollingmaturity, tradedate)
+ }else{
+ reinvprices <- list()
}
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", tradedate, sep="_"), "csv")
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index aadd99ba..073075de 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -738,15 +738,11 @@ getdealschedule <- function(dealdata, freq="1 month", lag=0, adjust=FALSE){
return(sched)
}
-getsettledate <- function(tradedate, settledays = 3, calendar="UnitedStates/GovernmentBond"){
- return(advance(calendar=calendar, tradedate, n = settledays, timeUnit = 0))
-}
-
SPmatrix2 <- function(portfolio, dealdata, freq="3 months", startdate=Sys.Date()){
## computes the default and prepay probability matrix of a portfolio
## at the dates specified from dealdata
dates <- getdealschedule(dealdata, freq)
- dates <- dates[dates>=getsettledate(startdate)]
+ dates <- dates[dates>=addBusDay(startdate, 3)]
DP <- matrix(0, length(portfolio), length(dates))
PP <- matrix(0, length(portfolio), length(dates))
for(i in seq_along(portfolio)){
@@ -792,36 +788,6 @@ forwardportfolioprice <- function(portfolio, startdate, maturity, coupontype, ma
sum(portfolio$notional[idx]))
}
-forwardportfolioprice2 <- function(cdrmonthly, recoverymonthly, startdate, maturity,
- coupontype, margin, liborfloor){
- if(missing(liborfloor)||is.na(liborfloor)){
- currentcoupon <- margin
- }else{
- currentcoupon <- margin + liborfloor
- }
- forwardcs <- data.table(couponSchedule(nextpaydate=startdate+45, maturity,
- frequency="Q", coupontype, margin,
- currentcoupon, startdate=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)])*
- -apply(rbind(1,as.matrix(notionals)), 2, diff)
- if(nrow(recovery)==1){
- recovery <- recovery*last(forwardcs[,df])
- }else{
- recovery <- data.table(dates=cdrmonthly[date>=startdate,date],apply(recovery, 2, cumsum),key="dates")
- recovery <- recovery[forwardcs, roll=TRUE]
- df <- recovery[,df]
- recovery <- t(df)%*%as.matrix(recovery[,lapply(.SD,function(x)diff(c(0,x))),.SDcols=paste0("V",1:100)])
- }
- notionals <- data.table(dates=cdrmonthly[date>=startdate,date], notionals, key="dates")
- outstanding <- notionals[forwardcs, roll=TRUE]
- mat.outstanding <- as.matrix(outstanding[,.SD,.SDcols=paste0("V",1:100)])
- po <- mat.outstanding[nrow(mat.outstanding),]*last(outstanding)[,df]
- io <- outstanding[, df*coupons]%*%mat.outstanding
- mean(recovery+po+io)
-}
-
creditcurve.maturity <- function(creditcurve){
if(class(creditcurve)=="creditcurve"){
dates <- creditcurve@curve@dates
diff --git a/R/cds_utils.R b/R/cds_utils.R
index 77e3a86d..2a93c52f 100644
--- a/R/cds_utils.R
+++ b/R/cds_utils.R
@@ -4,8 +4,8 @@ today <- function() {
Sys.Date()
}
-prevBusDay <- function(workdate = Sys.Date()){
- advance(calendar = "UnitedStates/GovernmentBond", dates=workdate, n=-1, timeUnit=0)
+addBusDay <- function(tradedate = Sys.Date(), n.days = 3, calendar = "UnitedStates/GovernmentBond"){
+ advance(calendar = "UnitedStates/GovernmentBond", dates=tradedate, n=n.days, timeUnit=0)
}
convertTenor <- function(tenor) {
diff --git a/R/intex_deal_functions.R b/R/intex_deal_functions.R
index 306eb345..42d9f452 100644
--- a/R/intex_deal_functions.R
+++ b/R/intex_deal_functions.R
@@ -347,3 +347,51 @@ get.reinvassets <- function(dealname, tradedate){
}
return( r )
}
+
+forwardportfolioprice2 <- function(cdrmonthly, recoverymonthly, startdate, maturity,
+ coupontype, margin, liborfloor){
+ if(missing(liborfloor)||is.na(liborfloor)){
+ currentcoupon <- margin
+ }else{
+ currentcoupon <- margin + liborfloor
+ }
+ forwardcs <- data.table(couponSchedule(nextpaydate=startdate+45, maturity,
+ frequency="Q", coupontype, margin,
+ currentcoupon, startdate=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)])*
+ -apply(rbind(1,as.matrix(notionals)), 2, diff)
+ if(nrow(recovery)==1){
+ recovery <- recovery*last(forwardcs[,df])
+ }else{
+ recovery <- data.table(dates=cdrmonthly[date>=startdate,date],apply(recovery, 2, cumsum),key="dates")
+ recovery <- recovery[forwardcs, roll=TRUE]
+ df <- recovery[,df]
+ recovery <- t(df)%*%as.matrix(recovery[,lapply(.SD,function(x)diff(c(0,x))),.SDcols=paste0("V",1:100)])
+ }
+ notionals <- data.table(dates=cdrmonthly[date>=startdate,date], notionals, key="dates")
+ outstanding <- notionals[forwardcs, roll=TRUE]
+ mat.outstanding <- as.matrix(outstanding[,.SD,.SDcols=paste0("V",1:100)])
+ po <- mat.outstanding[nrow(mat.outstanding),]*last(outstanding)[,df]
+ io <- outstanding[, df*coupons]%*%mat.outstanding
+ mean(recovery+po+io)
+}
+
+compute.reinvprices <- function(dealname, cdrmonthly, recoverymonthly, params, rollingmaturity, tradedate){
+ reinvassets <- get.reinvassets(dealname, tradedate)
+ reinvprices <- list()
+ if(length(reinvassets)>0){
+ maturity <- cdrmonthly$date[nrow(cdrmonthly)]
+ for(assetname in names(reinvassets)){
+ asset <- reinvassets[[assetname]]
+ coupon <- if(asset$coupontype=="FLOAT") params$reinvfloat else params$reinvfixed
+ reinvprices[[assetname]] <- foreach(date = iter(cdrmonthly$date), .combine=c) %dopar% {
+ 100 * forwardportfolioprice2(cdrmonthly.dt, recoverymonthly.dt, date,
+ min(date+rollingmaturity, maturity),
+ asset$coupontype, coupon, asset$liborfloor/100)
+ }
+ }
+ }
+ return(reinvprices)
+}
diff --git a/R/params.yml b/R/params.yml
index e0f39cca..42437ec8 100644
--- a/R/params.yml
+++ b/R/params.yml
@@ -25,3 +25,5 @@ shape: !expr |
function (T)
0.5 + (1 - 0.5) * (1 - exp(-T))
reinvflag: yes
+reinvfloat: 0.04
+reinvfixed: 0.07