aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/build_SC.R194
1 files changed, 0 insertions, 194 deletions
diff --git a/R/build_SC.R b/R/build_SC.R
deleted file mode 100644
index cfe67701..00000000
--- a/R/build_SC.R
+++ /dev/null
@@ -1,194 +0,0 @@
-library("RQuantLib")
-library("parallel")
-
-if(.Platform$OS.type == "unix"){
- root.dir <- "/home/share/CorpCDOs"
-}else{
- 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))
-}
-
-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,
- "Credit Default Swap"=0)
-
-global.params$cdoprices <- list("Aaa"=90,
- "Aa"=88,
- "A"=80,
- "Baa"=75,
- "Ba"=70,
- "B"=65,
- "NR"=50)
-#reinvest in 7 years assets
-global.params$rollingmaturity <- 7 * 365
-global.params$defaultedlag <- 90
-global.params$defaultcorr <- 0.45
-global.params$defaultbondhazardrate <- 1500 * bps
-global.params$defaultloanhazardrate <- 1500 * bps
-global.params$alpha <- 0.25
-global.params$beta <- 15
-global.params$shape <- function(T)0.25+(1-exp(-T/5))
-
-dealnames <- listdealnames()
-
-calibration.date <- Sys.Date()
-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.dir <- file.path(root.dir, "Scenarios", paste("Portfolios", calibration.date, sep="_"))
- if(!file.exists(save.dir)){
- dir.create(save.dir)
- }
- save(deal.portfolio, A, S, deal.weights, deal.dates, deal.spread,
- file=file.path(save.dir, paste0(deal.name, ".RData")))
- cat(deal.name, "... done\n")
-}
-
-## build portfolio data
-for(deal.name in dealnames){
- do(deal.name)
-}
-
-calibration <- read.table(file.path(root.dir, "Scenarios", "Calibration",
- paste0("calibration-", calibration.date,".csv")), sep=",", header=T)
-
-Z <- calibration$Z
-w <- calibration$w
-
-rho <- 0.45
-Ngrid <- 201
-cl <- makeCluster(6)
-clusterExport(cl, list("root.dir", "shockprob", "lossdistC.prepay.joint", "lossrecovdist.joint.term", "lossdistC.joint"))
-support <- seq(0, 1, length=Ngrid)
-
-useCluster <- TRUE
-for(deal.name in dealnames){
- load(file.path(root.dir, "Scenarios", paste("Portfolios", calibration.date, sep="_"), paste(deal.name, "RData", sep=".")))
-
- dp <- A$DP
- pp <- A$PP
- 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)
- 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,
- ## pp, ppmod, issuerweights, 1-S, Ngrid=201, TRUE)
-
- ## second, by doing a change of variable seems to work better for now
- distDR <- dist.transform(dist.joint)
-
- ## compute E(R|D)
- R <- matrix(0, Ngrid, ncol(dp))
- for(t in 1:ncol(dp)){
- R[,t] <- (sweep(distDR[t,,], 1, rowSums(distDR[t,,]), "/") %*% support)/support
- }
- R[1,] <- 0
- n.scenarios <- 100
- percentiles <- (seq(0, 1, length=n.scenarios+1)[-1]+
- seq(0, 1, length=n.scenarios+1)[-(n.scenarios+1)])/2
-
- ## compute scenariosd
- scenariosd <- matrix(0, n.scenarios, ncol(dp))
- for(t in 1:ncol(dp)){
- D <- rowSums(distDR[t,,])
- D <- D/sum(D)
- Dfun <- splinefun(c(0, cumsum(D)), c(0, support), "monoH.FC")
- ## dvallow <- floor(Dfun(percentiles)*(Ngrid-1))
- ## dvalup <- ceil(Dfun(percentiles)*(Ngrid-1))
- scenariosd[,t] <- Dfun(percentiles)
- }
-
- ## compute scenariosr
- scenariosr <- matrix(0, n.scenarios, ncol(dp))
- for(t in 1:ncol(dp)){
- Rfun <- approxfun(support, R[,t], rule=2)
- scenariosr[,t] <- Rfun(scenariosd[,t])
- }
-
- cdr <- cdrfromscenarios(scenariosd, deal.dates)
- 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)){
- ## reinvmaturity <- min(deal.datesmonthly[i]+global.params$rollingmaturity, deal.dates[length(deal.dates)])
- ## if(deal.datesmonthly[i]>reinvmaturity-45){
- ## reinvloanprice <- 1
- ## reinvbondprice <- 1
- ## }else{
- ## reinvloanprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], reinvmaturity, "FLOAT", 0.025)
- ## reinvbondprice[i] <- forwardportfolioprice(deal.portfolio, deal.datesmonthly[i], reinvmaturity, "FIXED", 0.07)
- ## }
-
- ## }
- reinvloanprice <- 0.965
- reinvbondprice <- 0.965
- cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
- recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
- for(i in 1:n.scenarios){
- cdrmonthly[i,] <- approx(deal.dates, cdr[i,], deal.datesmonthly, rule=2)$y
- recoverymonthly[i,] <- approx(deal.dates, intexrecov[i,], deal.datesmonthly, rule=2)$y
- }
-
- save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", calibration.date, sep="_"), "csv")
- if(!file.exists(save.dir)){
- dir.create(save.dir, recursive = T)
- }
- recoverymonthly <- pmin(recoverymonthly,1)
- write.table(cdrmonthly,
- file= file.path(save.dir, paste0(deal.name,"-cdr.csv")),
- row.names=F, col.names=F, sep=",")
- write.table(100 * recoverymonthly,
- file=file.path(save.dir, paste0(deal.name,"-recovery.csv")),
- row.names=F, col.names=F, sep=",")
- write.table(rbind(100*reinvloanprice, 100*reinvbondprice),
- file = file.path(save.dir, paste0(deal.name,"-reinvprices.csv")),
- row.names=F, col.names=F, sep=",")
- save(scenariosd, scenariosr, file=file.path(save.dir, paste0(deal.name, ".RData")))
- cat("generated scenarios for:", deal.name, "\n")
-}