diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/build_scenarios.R | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R index 890ceded..18099933 100644 --- a/R/build_scenarios.R +++ b/R/build_scenarios.R @@ -31,18 +31,23 @@ prevBusDay <- function(workdate = Sys.Date()){ return( workdate - i )
}
-compute.reinvprices <- function(DC, cdrmonthly, recoverymonthly, spread, fixedrate, rollingmat){
+compute.reinvprices <- function(df, forwards, cdrmonthly, recoverymonthly,
+ spread, fixedrate, rollingmat, reinvlag){
+ ## reinvlag is in months
+ ## rollingmat is in months
floatbp <- c()
fixedbp <- c()
for(i in 0:(ncol(cdrmonthly)-1)){
index <- (i+1):min(i+rollingmat, ncol(cdrmonthly))
+ indexlagged <- index + reinvlag
if(i==0){
- df <- DC$discounts[index]
-
+ currdf <- df[index]
+ laggeddf <- df[indexlagged]
}else{
- df <- DC$discounts[index]/DC$discounts[i]
+ currdf <- df[index]/df[i]
+ laggeddf <- df[indexlagged]/df[i]
}
- floatcoupon <- (DC$forwards[index]+ spread)/12
+ floatcoupon <- (forwards[index]+ spread)/12
fixedcoupon <- fixedrate/12
currbalance <- 1 - cdrmonthly[,index]/100/12
browser()
@@ -50,14 +55,14 @@ compute.reinvprices <- function(DC, cdrmonthly, recoverymonthly, spread, fixedra currbalance <- t(apply(currbalance, 1, cumprod))
recov <- -t(apply(cbind(1, currbalance), 1, diff)) *
recoverymonthly[,index]
- fixedcl <- cbind(1,currbalance[,-length(index)])%*%(fixedcoupon*df)
- floatcl <- cbind(1,currbalance[,-length(index)])%*%(floatcoupon*df)
- pl <- currbalance[,dim(currbalance)[2]]*df[length(df)] +
- recov %*% df
+ fixedcl <- cbind(1,currbalance[,-length(index)])%*%(fixedcoupon*currdf)
+ floatcl <- cbind(1,currbalance[,-length(index)])%*%(floatcoupon*currdf)
+ pl <- currbalance[,dim(currbalance)[2]]*currdf[length(currdf)] +
+ recov %*% laggeddf
}else{
fixedcl <- currbalance*fixedcoupon*df
floatcl <- currbalance*floatcoupon*df
- pl <- currbalance*df
+ pl <- currbalance * df
}
floatbp <- cbind(floatbp, pl+floatcl)
fixedbp <- cbind(fixedbp, pl+fixedcl)
@@ -85,6 +90,8 @@ L12m <- buildMarkitYC(MarkitData, dt = 1) setEvaluationDate(as.Date(MarkitData$effectiveasof))
support <- seq(0, 1, length = Ngrid)
+## 3 months
+recoverylag <- 90
for(deal.name in dealnames){
load(file.path(root.dir, "Scenarios", paste("Portfolios", workdate, sep="_"),
paste(deal.name, "RData", sep=".")))
@@ -141,7 +148,7 @@ for(deal.name in dealnames){ cdr <- cdrfromscenarios(scenariosd, deal.dates)
## linear approximation for monthly scenarios
deal.data <- getdealdata(deal.name)
- deal.datesmonthly <- getdealschedule(deal.data, "1 month")
+ deal.datesmonthly <- getdealschedule(deal.data, "1 month", lag = recoverylag)
cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
for(i in 1:n.scenarios){
@@ -151,7 +158,8 @@ for(deal.name in dealnames){ ## compute reinvestment price
DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthly))
- reinvprices <- compute.reinvprices(DC, cdrmonthly, recoverymonthly, 0.025, 0.07, 84)
+ df <- DC$discounts
+ reinvprices <- compute.reinvprices(df, cdrmonthly, recoverymonthly, 0.025, 0.07, 84)
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"), "csv")
if(!file.exists(save.dir)){
|
