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")) dealnames <- read.table(file.path(root.dir, "scripts", "scenarios.txt"))$V1 unlink(file.path(root.dir, "scripts", "scenarios.txt")) source(file.path(root.dir, "code", "R", "index_definitions.R")) prevBusDay <- function(){ i <- 1 while(!isBusinessDay(calendar = "UnitedStates/GovernmentBond", Sys.Date() - i)){ i <- i+1 } return( Sys.Date() - i ) } workdate <- Sys.Date() calibration.date <- prevBusDay() 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 ## dealnames <- list.files(file.path(root.dir, "Scenarios", paste0("Portfolios_", calibration.date)), ## pattern="*.RData") ## dealnames <- sapply(strsplit(dealnames, "\\."), function(x)x[1]) support <- seq(0, 1, length = Ngrid) for(deal.name in dealnames){ load(file.path(root.dir, "Scenarios", paste("Portfolios", workdate, sep="_"), paste(deal.name, "RData", sep="."))) dp <- A$DP pp <- A$PP dpmod <- MFupdate.probC(Z, w, rho, dp) ppmod <- MFupdate.probC(-Z, w, rho, pp) dist.joint <- MFlossdist.prepay.joint2(w, Z, rho, dp, dpmod, pp, ppmod, deal.weights, 1-S, Ngrid) 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", workdate, 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") }