aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/build_portfolios.R26
-rw-r--r--R/build_scenarios.R18
2 files changed, 26 insertions, 18 deletions
diff --git a/R/build_portfolios.R b/R/build_portfolios.R
index 7e63d548..ef32bf1e 100644
--- a/R/build_portfolios.R
+++ b/R/build_portfolios.R
@@ -1,5 +1,6 @@
library("RQuantLib")
-library("parallel")
+
+args <- commandArgs(trailingOnly=TRUE)
if(.Platform$OS.type == "unix"){
root.dir <- "/home/share/CorpCDOs"
@@ -9,20 +10,23 @@ if(.Platform$OS.type == "unix"){
source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
-dealnames <- unique(dealnamefromcusip(read.table(file.path(root.dir, "scripts", "cusips_to_price.txt"))$V1))
-unlink(file.path(root.dir, "scripts", "cusips_to_price.txt"))
+if(length(args) >=2){
+ dealnames <- args[2:length(args)]
+}else{
+ dealnames <- unique(dealnamefromcusip(read.table(file.path(root.dir, "scripts", "cusips_to_price.txt"))$V1))
+ unlink(file.path(root.dir, "scripts", "cusips_to_price.txt"))
+}
+
source(file.path(root.dir, "code", "R", "index_definitions.R"))
+source(file.path(root.dir, "code", "R", "cds_utils.R"))
-prevBusDay <- function(){
- i <- 1
- while(!isBusinessDay(calendar = "UnitedStates/GovernmentBond", Sys.Date() - i)){
- i <- i+1
- }
- return( Sys.Date() - i )
+if(length(args) >= 1){
+ workdate <- as.Date(args[1])
+}else{
+ workdate <- Sys.Date()
}
-workdate <- Sys.Date()
-calibration.date <- prevBusDay()
+calibration.date <- prevBusDay(workdate)
MarkitData <- getMarkitIRData(calibration.date)
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
diff --git a/R/build_scenarios.R b/R/build_scenarios.R
index 18099933..82c58562 100644
--- a/R/build_scenarios.R
+++ b/R/build_scenarios.R
@@ -50,7 +50,7 @@ compute.reinvprices <- function(df, forwards, cdrmonthly, recoverymonthly,
floatcoupon <- (forwards[index]+ spread)/12
fixedcoupon <- fixedrate/12
currbalance <- 1 - cdrmonthly[,index]/100/12
- browser()
+
if(i < ncol(cdrmonthly)-1){
currbalance <- t(apply(currbalance, 1, cumprod))
recov <- -t(apply(cbind(1, currbalance), 1, diff)) *
@@ -60,9 +60,11 @@ compute.reinvprices <- function(df, forwards, cdrmonthly, recoverymonthly,
pl <- currbalance[,dim(currbalance)[2]]*currdf[length(currdf)] +
recov %*% laggeddf
}else{
- fixedcl <- currbalance*fixedcoupon*df
- floatcl <- currbalance*floatcoupon*df
- pl <- currbalance * df
+ recov <- -t(apply(cbind(1, currbalance), 1, diff)) *
+ recoverymonthly[,index]
+ fixedcl <- as.numeric(currbalance*fixedcoupon*currdf)
+ floatcl <- as.numeric(currbalance*floatcoupon*currdf)
+ pl <- as.numeric(currbalance * currdf + recov * laggeddf)
}
floatbp <- cbind(floatbp, pl+floatcl)
fixedbp <- cbind(fixedbp, pl+fixedcl)
@@ -148,7 +150,8 @@ 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", lag = recoverylag)
+ deal.datesmonthly <- getdealschedule(deal.data, "1 month")
+ deal.datesmonthlylagged <- getdealschedule(deal.data, "1 month", workdate, 90)
cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
for(i in 1:n.scenarios){
@@ -157,9 +160,10 @@ for(deal.name in dealnames){
}
## compute reinvestment price
- DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthly))
+ DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthlylagged))
df <- DC$discounts
- reinvprices <- compute.reinvprices(df, cdrmonthly, recoverymonthly, 0.025, 0.07, 84)
+ forwards <- DC$forwards
+ reinvprices <- compute.reinvprices(df, forwards, cdrmonthly, recoverymonthly, 0.025, 0.07, 84, 3)
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"), "csv")
if(!file.exists(save.dir)){