library(ggplot2) date <- Sys.Date() root.dir <- Sys.getenv("BASE_DIR") dists <- list() for(deal in c('ares29', 'bally131')) { load(file.path(root.dir, "Scenarios", sprintf("Intex Curves_%s", date), "csv", paste(deal, "RData", sep="."))) dists[[deal]] <- dist.joint } support <- seq(0, 1, length = 201) ##5 year ggplot() + geom_line(aes(x=support, y=rowSums(dists$ares29[17,,]), colour='Ares XXIX CLO')) + geom_line(aes(x=support, y=rowSums(dists$bally131[17,,]), colour='Ballyrock CLO 2013-1')) + ylab("loss density")+xlab("percentage of loss") + geom_vline(aes(colour='Market assumption', xintercept=0.03)) + annotate("text", x=0.5, y=0.025, label="T=5 year", size=4) + annotate("text", x=0.5, y=0, label="2 CDR, 70% recovery", colour="#F8766D", size=4) + labs(colour="") + theme(legend.position="bottom") ggsave("Loss distribution.png", width=4.35, height=4) ##ggsave("Loss distribution.png") tradedate <- as.Date("2016-10-19") calibration.date <- as.Date("2016-10-18") load(file.path(root.dir, "Scenarios", "Calibration", sprintf("marketdata-%s.RData", calibration.date))) load(file.path(root.dir, "Scenarios", paste0("Prices_", tradedate), "cashflows.RData")) getcusip.pv <- function(cusip, dealname, cusipdata, date) { indic.data <- getcusip_indicdata(cusip, dealname, date) cusip.pv <- cusipdata[[cusip]]$fields[,"Cashflow"]/indic.data$curr_balance return( cusip.pv ) } index.pv <- function(indexdist, dealdata) { dealweight <- dealdata$weight dealprice <- dealdata$price nT <- dim(indexdist$L)[2] Ngrid <- dim(indexdist$L)[1] scenariosl <- matrix(0, length(dealweight), nT) scenariosr <- matrix(0, length(dealweight), nT) for(t in 1:nT){ scenariosl[,t] <- interpvalues(indexdist$L[,t], seq(0, 1, length=Ngrid), dealweight) ## numerical artefact, but we want scenariosr[i,] + scenariosl[i,] <= 1 at all times scenariosr[,t] <- pmin(interpvalues(indexdist$R[,t], seq(0, 1, length=Ngrid), dealweight), 1-scenariosl[,t]) } ## we assume the index is fully funded - need to be changed depending ## on how we fund the swaps (hence floating coupon instead of fixed) indexpv <- c() for(i in 1:length(dealweight)){ indexpv <- c(indexpv, funded.tranche.pv(scenariosl[i,], scenariosr[i,], cs, 0, 1, TRUE)) } return(indexpv) } getcusip_indicdata <- function(Cusip, dealname, date){ sqlstr <- "SELECT DISTINCT isin FROM cusip_universe WHERE cusip=$1" r <- tryCatch(dbGetQuery(etdb, sqlstr, params=list(Cusip)), error = function(w) logerror(w$message)) if(length(r$isin)>1){ stop("We have a problem") } isinval <- r$isin[1] sqlstr <- "SELECT * FROM historical_dealname_universe($1, $2)" r <- tryCatch(dbGetQuery(etdb, sqlstr, params = list(dealname, date)), error = function(w) logerror(w$message)) if(!is.na(isinval)){ return(r %>% group_by(isin) %>% slice(1) %>% summarize(cusip, curr_balance, orig_balance, spread, curr_attach) %>% arrange(desc(curr_attach)) %>% mutate(cum_bal = cumsum(curr_balance)) %>% filter(isin==isinval) ) }else{ return(r %>% select(cusip, curr_balance, orig_balance, spread, curr_attach) %>% arrange(desc(curr_attach)) %>% mutate(cum_bal = cumsum(curr_balance)) %>% filter(cusip==Cusip) ) } } tradedate <- as.Date("2016-10-19") dealname <- "bally131" hy.pv <- index.pv(dist, cfdata[[dealname]]) bally131 <- data.frame(cusips = c("05874PAA4", "05874PAC0", "05874PAE6", "05874PAG1", "05874QAC8", "05874QAA2"), name = c("A", "B", "C", "D", "E", "SUB"), rating = c("AAA", "AA", "A", "BBB", "BB", "NA")) support <- cumsum(cfdata[[dealname]]$weight) * 100 df <- data.frame(x =support, pv = hy.pv * 100, name = "HY27 5Y") for(i in 1:nrow(bally131)) { df <- rbind(df, data.frame(x =support, pv = getcusip.pv(bally131$cusips[i], dealname, cusipdata, date)*100, name = bally131$rating[i])) } ggplot(df) + geom_line(aes(x=x, y =pv, group = name, colour = factor(name))) + ylab("Expected price")+xlab("scenario percentile") + theme(legend.position="bottom") for(cusip in cusips ) { cusip.pv <- getcusip.pv(cusip, dealname, cusipdata, date) plot <- plot + geom_line(aes(x=support, y =cusip.pv * 100, colour=sprintf('Ballyrock CLO 2013-1 %s', cusip))) }