diff options
Diffstat (limited to 'R/build_scenarios.R')
| -rw-r--r-- | R/build_scenarios.R | 348 |
1 files changed, 174 insertions, 174 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R index a2c0c190..f5255836 100644 --- a/R/build_scenarios.R +++ b/R/build_scenarios.R @@ -1,174 +1,174 @@ -library(doParallel)
-library(yaml)
-
-hostname <- system("hostname", intern=TRUE)
-if(hostname=="debian"){
- registerDoParallel(8)
-}else{
- registerDoParallel(4)
-}
-
-args <- commandArgs(trailingOnly=TRUE)
-
-if(.Platform$OS.type == "unix"){
- root.dir <- "/home/share/CorpCDOs"
-}else{
- root.dir <- "//WDSENTINEL/share/CorpCDOs"
-}
-
-code.dir <- if(Sys.getenv("CODE_DIR")=="") root.dir else Sys.getenv("CODE_DIR")
-source(file.path(code.dir, "code", "R", "intex_deal_functions.R"), chdir=TRUE)
-source(file.path(code.dir, "code", "R", "yieldcurve.R"))
-source(file.path(code.dir, "code", "R", "serenitasdb.R"), chdir=TRUE)
-source(file.path(code.dir, "code", "R", "tranche_functions.R"))
-
-if(interactive()) {
- tradedate <- as.Date("2016-02-25")
- dealnames <- c("beto", "ozlmf5")
- reinvflags <- c(TRUE, TRUE)
-}else{
- tradedate <- as.Date(args[1])
- if(length(args) >=2){
- argslist <- strsplit(args[-1], ",")
- dealnames <- unlist(lapply(argslist, function(x)x[1]))
- reinvflags <- as.logical(unlist(lapply(argslist, function(x)x[2])))
- }
-}
-
-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
-exportYC(calibration.date)
-
-Ngrid <- 201
-support <- seq(0, 1, length = Ngrid)
-n.scenarios <- 100
-recov.adj <- 1
-params <- yaml.load_file(file.path(root.dir, "code", "etc", "params.yml"))
-
-for(j in seq_along(dealnames)){
- load(file.path(root.dir, "Scenarios", paste("Portfolios", tradedate, sep="_"),
- paste(dealnames[j], "RData", sep=".")))
- if(is.na(deal.data$reinv_end_date)){
- reinvflags[j] <- FALSE
- }
- dp <- A$DP
- pp <- A$PP
- dpmod <- MFupdate.prob(Z, w, deal.portfolio$beta, dp)
- ppmod <- MFupdate.prob(-Z, w, deal.portfolio$beta, pp)
- dist.joint <- MFlossdist.prepay.joint(w, Z, deal.portfolio$beta, dp, dpmod, pp, ppmod,
- deal.weights, 1-S, Ngrid)
- dist.joint <- pmax(dist.joint, 0)
- distDR <- dist.transform(dist.joint)
- ## compute E(R|D)
- R <- matrix(0, Ngrid, dim(distDR)[1])
- for(t in 1:dim(distDR)[1]){
- R[,t] <- sweep(distDR[t,,], 1, rowSums(distDR[t,,]), "/") %*% support
- R[1,t] <- 0
- if(t >= 2){
- R[,t] <- pmax(R[,t], R[,t-1])
- }
- }
-
- ## compute scenariosd
- scenariosd <- matrix(0, n.scenarios, dim(distDR)[1])
- scenariosr <- matrix(0, n.scenarios, dim(distDR)[1])
- percentiles <- seq(0, 1, 1/n.scenarios)
- for(t in 1:dim(distDR)[1]){
- D <- rowSums(distDR[t,,])
- Dfun <- splinefun(c(0, cumsum(D)), c(0, support), "hyman")
- Rfun <- approxfun(support, R[,t], rule=2)
- for(i in 1:n.scenarios){
- ## this is roughtly E(D|D is in ith percentile)
- ## using trapezoidal approximation
- if(i==1){
- scenariosd[i,t] <- 0.5* Dfun(0.01)
- }else{
- scenariosd[i,t] <- 0.5 * (Dfun((i-1)*0.01)+Dfun(i*0.01))
- }
- if(t>=2 && scenariosd[i,t] < scenariosd[i,t-1]){
- scenariosd[i,t] <- scenariosd[i,t-1]
- }
- scenariosr[i,t] <- Rfun(scenariosd[i,t])
- if(t>=2 && scenariosr[i,t] < scenariosr[i,t-1]){
- scenariosr[i,t] <- scenariosr[i,t-1]
- }
- }
- }
-
- ## we need to adjust the recovery because intex has some embedded amortization assumptions
- ## that we can't turn off (multiply by recov.adj)
- intexrecov <- matrix(0, n.scenarios, dim(distDR)[1])
- for(i in 1:dim(distDR)[1]){
- if(i==1){
- intexrecov[,i] <- recov.adj * (scenariosr[,i]/scenariosd[,1])
- }else{
- intexrecov[,i] <- recov.adj * (scenariosr[,i]-scenariosr[,i-1])/(scenariosd[,i]-scenariosd[,i-1])
- }
- }
- deal.dates <- getdealschedule(deal.data, "Quarterly")
- deal.dates <- deal.dates[deal.dates>=settledate]
- cdr <- cdrfromscenarios(scenariosd, deal.dates, tradedate)
- ## linear approximation for monthly scenarios
- deal.data <- getdealdata(dealnames[j], tradedate)
- deal.datesmonthly <- getdealschedule(deal.data, "Monthly")
- deal.datesmonthly <- deal.datesmonthly[deal.datesmonthly>=settledate]
-
- cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
- recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
- scenariosrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
- scenariosdmonthly <- 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
- scenariosrmonthly[i,] <- approx(deal.dates, scenariosr[i,], deal.datesmonthly, rule=2)$y
- scenariosdmonthly[i,] <- approx(deal.dates, scenariosd[i,], deal.datesmonthly, rule=2)$y
- }
- recoverymonthly <- pmin(recoverymonthly,1)
- recoverymonthly[!is.finite(recoverymonthly)] <- 100
-
- 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))
- }else{
- rollingmaturity <- params$rollingmaturity
- }
-
- ## 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")
- 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")
- if(!file.exists(save.dir)){
- dir.create(save.dir, recursive = T)
- }
-
- write.table(cdrmonthly,
- file= file.path(save.dir, paste0(dealnames[j],"-cdr.csv")),
- row.names=F, col.names=F, sep=",")
- write.table(100 * recoverymonthly,
- file=file.path(save.dir, paste0(dealnames[j],"-recovery.csv")),
- row.names=F, col.names=F, sep=",", na="NaN")
- write.table(reinvprices, file = file.path(save.dir, paste0(dealnames[j], "-reinvprices.csv")),
- row.names=F, col.names=T, sep=",")
-
- configfile <- file.path(save.dir, paste0(dealnames[j], ".config"))
- config <- list(rollingmat = as.integer(rollingmaturity/365*12),
- reinvflag = reinvflags[j])
- cat(as.yaml(config), file = configfile)
- save(scenariosd, scenariosr, dist.joint, file=file.path(save.dir, paste0(dealnames[j], ".RData")),
- compress="xz")
-
- cat("generated scenarios for:", dealnames[j], "\n")
-}
+library(doParallel) +library(yaml) + +hostname <- system("hostname", intern=TRUE) +if(hostname=="debian"){ + registerDoParallel(8) +}else{ + registerDoParallel(4) +} + +args <- commandArgs(trailingOnly=TRUE) + +if(.Platform$OS.type == "unix"){ + root.dir <- "/home/share/CorpCDOs" +}else{ + root.dir <- "//WDSENTINEL/share/CorpCDOs" +} + +code.dir <- if(Sys.getenv("CODE_DIR")=="") root.dir else Sys.getenv("CODE_DIR") +source(file.path(code.dir, "code", "R", "intex_deal_functions.R"), chdir=TRUE) +source(file.path(code.dir, "code", "R", "yieldcurve.R")) +source(file.path(code.dir, "code", "R", "serenitasdb.R"), chdir=TRUE) +source(file.path(code.dir, "code", "R", "tranche_functions.R")) + +if(interactive()) { + tradedate <- as.Date("2016-02-25") + dealnames <- c("beto", "ozlmf5") + reinvflags <- c(TRUE, TRUE) +}else{ + tradedate <- as.Date(args[1]) + if(length(args) >=2){ + argslist <- strsplit(args[-1], ",") + dealnames <- unlist(lapply(argslist, function(x)x[1])) + reinvflags <- as.logical(unlist(lapply(argslist, function(x)x[2]))) + } +} + +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 +exportYC(calibration.date) + +Ngrid <- 201 +support <- seq(0, 1, length = Ngrid) +n.scenarios <- 100 +recov.adj <- 1 +params <- yaml.load_file(file.path(root.dir, "code", "etc", "params.yml")) + +for(j in seq_along(dealnames)){ + load(file.path(root.dir, "Scenarios", paste("Portfolios", tradedate, sep="_"), + paste(dealnames[j], "RData", sep="."))) + if(is.na(deal.data$reinv_end_date)){ + reinvflags[j] <- FALSE + } + dp <- A$DP + pp <- A$PP + dpmod <- MFupdate.prob(Z, w, deal.portfolio$beta, dp) + ppmod <- MFupdate.prob(-Z, w, deal.portfolio$beta, pp) + dist.joint <- MFlossdist.prepay.joint(w, Z, deal.portfolio$beta, dp, dpmod, pp, ppmod, + deal.weights, 1-S, Ngrid) + dist.joint <- pmax(dist.joint, 0) + distDR <- dist.transform(dist.joint) + ## compute E(R|D) + R <- matrix(0, Ngrid, dim(distDR)[1]) + for(t in 1:dim(distDR)[1]){ + R[,t] <- sweep(distDR[t,,], 1, rowSums(distDR[t,,]), "/") %*% support + R[1,t] <- 0 + if(t >= 2){ + R[,t] <- pmax(R[,t], R[,t-1]) + } + } + + ## compute scenariosd + scenariosd <- matrix(0, n.scenarios, dim(distDR)[1]) + scenariosr <- matrix(0, n.scenarios, dim(distDR)[1]) + percentiles <- seq(0, 1, 1/n.scenarios) + for(t in 1:dim(distDR)[1]){ + D <- rowSums(distDR[t,,]) + Dfun <- splinefun(c(0, cumsum(D)), c(0, support), "hyman") + Rfun <- approxfun(support, R[,t], rule=2) + for(i in 1:n.scenarios){ + ## this is roughtly E(D|D is in ith percentile) + ## using trapezoidal approximation + if(i==1){ + scenariosd[i,t] <- 0.5* Dfun(0.01) + }else{ + scenariosd[i,t] <- 0.5 * (Dfun((i-1)*0.01)+Dfun(i*0.01)) + } + if(t>=2 && scenariosd[i,t] < scenariosd[i,t-1]){ + scenariosd[i,t] <- scenariosd[i,t-1] + } + scenariosr[i,t] <- Rfun(scenariosd[i,t]) + if(t>=2 && scenariosr[i,t] < scenariosr[i,t-1]){ + scenariosr[i,t] <- scenariosr[i,t-1] + } + } + } + + ## we need to adjust the recovery because intex has some embedded amortization assumptions + ## that we can't turn off (multiply by recov.adj) + intexrecov <- matrix(0, n.scenarios, dim(distDR)[1]) + for(i in 1:dim(distDR)[1]){ + if(i==1){ + intexrecov[,i] <- recov.adj * (scenariosr[,i]/scenariosd[,1]) + }else{ + intexrecov[,i] <- recov.adj * (scenariosr[,i]-scenariosr[,i-1])/(scenariosd[,i]-scenariosd[,i-1]) + } + } + deal.dates <- getdealschedule(deal.data, "Quarterly") + deal.dates <- deal.dates[deal.dates>=settledate] + cdr <- cdrfromscenarios(scenariosd, deal.dates, tradedate) + ## linear approximation for monthly scenarios + deal.data <- getdealdata(dealnames[j], tradedate) + deal.datesmonthly <- getdealschedule(deal.data, "Monthly") + deal.datesmonthly <- deal.datesmonthly[deal.datesmonthly>=settledate] + + cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly)) + recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly)) + scenariosrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly)) + scenariosdmonthly <- 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 + scenariosrmonthly[i,] <- approx(deal.dates, scenariosr[i,], deal.datesmonthly, rule=2)$y + scenariosdmonthly[i,] <- approx(deal.dates, scenariosd[i,], deal.datesmonthly, rule=2)$y + } + recoverymonthly <- pmin(recoverymonthly,1) + recoverymonthly[!is.finite(recoverymonthly)] <- 100 + + 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)) + }else{ + rollingmaturity <- params$rollingmaturity + } + + ## 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") + 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") + if(!file.exists(save.dir)){ + dir.create(save.dir, recursive = T) + } + + write.table(cdrmonthly, + file= file.path(save.dir, paste0(dealnames[j],"-cdr.csv")), + row.names=F, col.names=F, sep=",") + write.table(100 * recoverymonthly, + file=file.path(save.dir, paste0(dealnames[j],"-recovery.csv")), + row.names=F, col.names=F, sep=",", na="NaN") + write.table(reinvprices, file = file.path(save.dir, paste0(dealnames[j], "-reinvprices.csv")), + row.names=F, col.names=T, sep=",") + + configfile <- file.path(save.dir, paste0(dealnames[j], ".config")) + config <- list(rollingmat = as.integer(rollingmaturity/365*12), + reinvflag = reinvflags[j]) + cat(as.yaml(config), file = configfile) + save(scenariosd, scenariosr, dist.joint, file=file.path(save.dir, paste0(dealnames[j], ".RData")), + compress="xz") + + cat("generated scenarios for:", dealnames[j], "\n") +} |
