aboutsummaryrefslogtreecommitdiffstats
path: root/R/build_SC.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/build_SC.R')
-rw-r--r--R/build_SC.R105
1 files changed, 68 insertions, 37 deletions
diff --git a/R/build_SC.R b/R/build_SC.R
index 11e2ec7b..9807b2de 100644
--- a/R/build_SC.R
+++ b/R/build_SC.R
@@ -1,15 +1,18 @@
library("RQuantLib")
library("parallel")
-root = "//WDSENTINEL/share/CorpCDOs"
-source(file.path(root, "R", "intex_deals_functions.R"))
+root.dir <- "//WDSENTINEL/share/CorpCDOs"
+source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
+source(file.path(root.dir, "code", "R", "index_definitions.R"))
MarkitData <- getMarkitIRData()
+
L1m <- buildMarkitYC(MarkitData, dt = 1/12)
L2m <- buildMarkitYC(MarkitData, dt = 1/6)
L3m <- buildMarkitYC(MarkitData)
L6m <- buildMarkitYC(MarkitData, dt = 1/2)
L12m <- buildMarkitYC(MarkitData, dt = 1)
setEvaluationDate(as.Date(MarkitData$effectiveasof))
+
today <- function(){
return(as.Date(MarkitData$effectiveasof))
}
@@ -17,10 +20,12 @@ today <- function(){
bps <- 1e-4
global.params <- list()
global.params$recovery.assumptions <- list("Loan"=0.7,
+ "Senior Notes"=0.7,
"SecondLien"=0.3,
"Bond"=0.4,
"Mezzanine"=0.15,
- "Adj_Covlite"=0.1)
+ "Adj_Covlite"=0.1,
+ "Credit Default Swap"=0)
global.params$cdoprices <- list("Aaa"=90,
"Aa"=80,
@@ -39,10 +44,33 @@ global.params$alpha <- 0.25
global.params$beta <- 15
global.params$shape <- function(T)0.25+(1-exp(-T/5))
-## dealnames <- c("stonln1", "babs072", "symph4", "flags5", "cent11", "wasatl", "oceant2", "acacl071", "limes")
-dealnames <- c("acacl071", "limes")
-calibration.date <- "2012-11-08"
-calibration <- read.table(file.path(root, "Scenarios", paste0("calibration-",calibration.date,".csv")),
+
+dealnames <- listdealnames()
+
+cusipdata <- cusip.data()
+do <- function(deal.name){
+ deal.data <- getdealdata(deal.name)
+ if(deal.data$"Curr Collat Bal" < 1 ||is.na(deal.data$maturity)||deal.data$maturity<=today()+90){
+ return()
+ }
+ deal.portfolio <- buildSC.portfolio(deal.name, deal.data, cusipdata, global.params)
+ A <- SPmatrix2(deal.portfolio$SC, deal.data, freq="3 months")
+ S <- 1 - sapply(deal.portfolio$SC, attr, "recov")
+ deal.weights <- deal.portfolio$notional/sum(deal.portfolio$notional)
+ deal.dates <- getdealschedule(deal.data)
+ deal.spread <- portfoliospread(deal.portfolio, hy19$maturity)
+ save(deal.portfolio, A, S, deal.weights, deal.dates, deal.spread,
+ file=file.path(root.dir, "Scenarios", "Portfolios", paste0(deal.name, ".RData")))
+ cat(deal.name, "... done\n")
+}
+
+## build portfolio data
+for(deal.name in dealnames){
+ do(deal.name)
+}
+
+calibration.date <- "2012-12-11"
+calibration <- read.table(file.path(root.dir, "Scenarios", paste0("calibration-", calibration.date,".csv")),
sep=",", header=T)
Z <- calibration$Z
w <- calibration$w
@@ -50,42 +78,29 @@ w <- calibration$w
rho <- 0.45
Ngrid <- 201
cl <- makeCluster(6)
-clusterExport(cl, list("shockprob", "lossdistC.prepay.joint", "lossrecovdist.joint.term", "lossdistC.joint"))
+clusterExport(cl, list("root.dir", "shockprob", "lossdistC.prepay.joint", "lossrecovdist.joint.term", "lossdistC.joint"))
support <- seq(0, 1, length=Ngrid)
-for(deal.name in dealnames){
- deal.portfolio <- buildSC.portfolio(deal.name, global.params)
- deal.data <- getdealdata(deal.name)
- A <- SPmatrix2(deal.portfolio$SC, deal.data, freq="3 months")
- S <- 1 - sapply(deal.portfolio$SC, attr, "recov")
- deal.weights <- deal.portfolio$notional/sum(deal.portfolio$notional)
- clusterExport(cl, list("deal.weights"))
- deal.dates <- getdealschedule(deal.data)
- ## compute reinvestment price
- reinvloanprice <- rep(0, length(deal.dates))
- reinvmezzprice <- rep(0, length(deal.dates))
- for(i in 1:length(deal.dates)){
- reinvloanprice[i] <- forwardportfolioprice(deal.portfolio, deal.dates[i], global.params$rollingmaturity, "FLOAT", 0.0235)
- reinvmezzprice[i] <- forwardportfolioprice(deal.portfolio, deal.dates[i], global.params$rollingmaturity, "FLOAT", 0.0385)
- }
+useCluster <- TRUE
+for(deal.name in c("oceant2", "atr4cdo")){
+ load(file.path(root.dir, "Scenarios", "Portfolios", paste(deal.name, "RData", sep=".")))
dp <- A$DP
pp <- A$PP
- Smat <- matrix(S, length(deal.weights), ncol(dp))
- ##no correlation setup
- ##test <- lossrecovdist.joint.term(dp, pp, issuerweights, Smat, Ngrid)
-
dpmod <- MFupdate.prob(Z, w, rho, dp)
ppmod <- MFupdate.prob(-Z, w, rho, pp)
## dist <- MFlossrecovdist.prepay(w, Z, rho, dp, dpmod, pp, ppmod, deal.weights, 1-S, Ngrid, TRUE)
- dist.joint <- MFlossdist.prepay.joint(cl, w, Z, rho, dp, dpmod,
- pp, ppmod, deal.weights, 1-S, Ngrid, FALSE, n.chunks=4)
- clusterCall(cl, gc)
- gc()
- ## if don't want to use cluster (use less memory)
- ## dist.joint <- MFlossdist.prepay.joint(NULL, w, Z, rho, dp, dpmod,
- ## pp, ppmod, issuerweights, 1-S, Ngrid=201, FALSE)
-
+ if(useCluster){
+ clusterExport(cl, list("deal.weights"))
+ dist.joint <- MFlossdist.prepay.joint(cl, w, Z, rho, dp, dpmod,
+ pp, ppmod, deal.weights, 1-S, Ngrid, FALSE, n.chunks=4)
+ clusterCall(cl, gc)
+ gc()
+ }else{
+ ## if don't want to use cluster (use less memory) but slower
+ dist.joint <- MFlossdist.prepay.joint(NULL, w, Z, rho, dp, dpmod,
+ pp, ppmod, deal.weights, 1-S, Ngrid, FALSE)
+ }
## two ways to compute the joint (D, R) distribution
## first using the actual function (numerically instable)
## dist.joint2 <- MFlossdist.prepay.joint(cl, w, Z, rho, dp, dpmod,
@@ -126,7 +141,15 @@ for(deal.name in dealnames){
intexrecov <- recoveryfromscenarios(scenariosd, scenariosr)
## linear approximation for monthly scenarios
+ deal.data <- getdealdata(deal.name)
deal.datesmonthly <- getdealschedule(deal.data, "1 month")
+ ## compute reinvestment price
+ reinvloanprice <- rep(0, length(deal.datesmonthly))
+ reinvbondprice <- rep(0, length(deal.datesmonthly))
+ for(i in 1:length(deal.datesmonthly)){
+ reinvloanprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], global.params$rollingmaturity, "FLOAT", 0.025)
+ reinvbondprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], global.params$rollingmaturity, "FIXED", 0.07)
+ }
cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
for(i in 1:n.scenarios){
@@ -134,7 +157,15 @@ for(deal.name in dealnames){
recoverymonthly[i,] <- approx(deal.dates, intexrecov[i,], deal.datesmonthly, rule=2)$y
}
- write.table(cdrmonthly, file=file.path(root, "Scenarios", paste0(deal.name,"-cdr.csv")), row.names=F, col.names=F, sep=",")
- write.table(recoverymonthly * 100, file=file.path(root, "Scenarios", paste0(deal.name,"-recovery.csv")), row.names=F, col.names=F, sep=",")
+ write.table(cdrmonthly, file=file.path(root.dir, "Scenarios", "Intex curves", "csv",
+ paste0(deal.name,"-cdr.csv")),
+ row.names=F, col.names=F, sep=",")
+ write.table(recoverymonthly * 100, file=file.path(root.dir, "Scenarios", "Intex curves", "csv",
+ paste0(deal.name,"-recovery.csv")),
+ row.names=F, col.names=F, sep=",")
+ write.table(rbind(100*reinvloanprice, 100*reinvbondprice),
+ file = file.path(root.dir, "Scenarios", "Intex curves", "csv",
+ paste0(deal.name,"-reinvprices.csv")),
+ row.names=F, col.names=F, sep=",")
cat("generated scenarios for:", deal.name, "\n")
}