diff options
| -rw-r--r-- | R/build_scenarios.R | 121 |
1 files changed, 82 insertions, 39 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R index 03be570d..9d9978f7 100644 --- a/R/build_scenarios.R +++ b/R/build_scenarios.R @@ -7,7 +7,7 @@ if(.Platform$OS.type == "unix"){ }
if(length(args) >= 1){
- workdate <- args[1]
+ workdate <- as.Date(args[1])
}else{
workdate <- Sys.Date()
}
@@ -18,7 +18,6 @@ if(length(args) >=2){ dealnames <- read.table(file.path(root.dir, "scripts", "scenarios.txt"))$V1
unlink(file.path(root.dir, "scripts", "scenarios.txt"))
}
-print(dealnames)
source(file.path(root.dir, "code", "R", "intex_deal_functions.R"))
source(file.path(root.dir, "code", "R", "index_definitions.R"))
@@ -32,6 +31,40 @@ prevBusDay <- function(workdate = Sys.Date()){ return( workdate - i )
}
+compute.reinvprices <- function(DC, cdrmonthly, recoverymonthly, spread, fixedrate, rollingmat){
+ floatbp <- c()
+ fixedbp <- c()
+ for(i in 0:(ncol(cdrmonthly)-1)){
+ index <- (i+1):min(i+rollingmat, ncol(cdrmonthly))
+ if(i==0){
+ df <- DC$discounts[index]
+
+ }else{
+ df <- DC$discounts[index]/DC$discounts[i]
+ }
+ floatcoupon <- (DC$forwards[index]+ spread)/12
+ fixedcoupon <- fixedrate/12
+ currbalance <- 1 - cdrmonthly[,index]/100/12
+ browser()
+ if(i < ncol(cdrmonthly)-1){
+ currbalance <- t(apply(currbalance, 1, cumprod))
+ recov <- -t(apply(cbind(1, currbalance), 1, diff)) *
+ recoverymonthly[,index]
+ fixedcl <- cbind(1,currbalance[,-length(index)])%*%(fixedcoupon*df)
+ floatcl <- cbind(1,currbalance[,-length(index)])%*%(floatcoupon*df)
+ pl <- currbalance[,dim(currbalance)[2]]*df[length(df)] +
+ recov %*% df
+ }else{
+ fixedcl <- currbalance*fixedcoupon*df
+ floatcl <- currbalance*floatcoupon*df
+ pl <- currbalance*df
+ }
+ floatbp <- cbind(floatbp, pl+floatcl)
+ fixedbp <- cbind(fixedbp, pl+fixedcl)
+ }
+ return( list(loan=floatbp, bond=fixedbp) )
+}
+
calibration.date <- prevBusDay(workdate)
calibration <- read.table(file.path(root.dir, "Scenarios", "Calibration",
paste0("calibration-", calibration.date,".csv")), sep=",", header=T)
@@ -43,6 +76,13 @@ Ngrid <- 201 ## dealnames <- list.files(file.path(root.dir, "Scenarios", paste0("Portfolios_", calibration.date)),
## pattern="*.RData")
## dealnames <- sapply(strsplit(dealnames, "\\."), function(x)x[1])
+MarkitData <- getMarkitIRData(calibration.date)
+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))
support <- seq(0, 1, length = Ngrid)
for(deal.name in dealnames){
@@ -57,33 +97,45 @@ for(deal.name in dealnames){ 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 <- 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])
+ }
}
- 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
+ n.scenarios <- 100
## compute scenariosd
- scenariosd <- matrix(0, n.scenarios, ncol(dp))
- for(t in 1:ncol(dp)){
+ scenariosd <- matrix(0, n.scenarios, dim(distDR)[1])
+ scenariosr <- matrix(0, n.scenarios, dim(distDR)[1])
+ percentiles <- seq(0, 1, 0.01)
+ for(t in 1:dim(distDR)[1]){
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])
+ for(i in 1:n.scenarios){
+ 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]
+ }
+ }
+ }
+ intexrecov <- matrix(0, n.scenarios, dim(distDR)[1])
+ for(i in 1:dim(distDR)[1]){
+ if(i==1){
+ intexrecov[,i] <- (scenariosr[,i]/scenariosd[,1])
+ }else{
+ intexrecov[,i] <- (scenariosr[,i]-scenariosr[,i-1])/(scenariosd[,i]-scenariosd[,i-1])
+ }
}
cdr <- cdrfromscenarios(scenariosd, deal.dates)
@@ -91,22 +143,6 @@ for(deal.name in dealnames){ ## 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){
@@ -114,6 +150,10 @@ for(deal.name in dealnames){ recoverymonthly[i,] <- approx(deal.dates, intexrecov[i,], deal.datesmonthly, rule=2)$y
}
+ ## compute reinvestment price
+ DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthly))
+ reinvprices <- compute.reinvprices(DC, cdrmonthly, recoverymonthly, 0.025, 0.07, 84)
+
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"), "csv")
if(!file.exists(save.dir)){
dir.create(save.dir, recursive = T)
@@ -125,9 +165,12 @@ for(deal.name in dealnames){ 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")),
+ write.table(100 * reinvprices$loan,
+ file = file.path(save.dir, paste0(deal.name, "-floatreinvprices.csv")),
+ row.names=F, col.names=F, sep=",")
+ write.table(100 * reinvprices$bond,
+ file = file.path(save.dir, paste0(deal.name, "-fixedreinvprices.csv")),
row.names=F, col.names=F, sep=",")
- save(scenariosd, scenariosr, distDR, file=file.path(save.dir, paste0(deal.name, ".RData")))
+ save(scenariosd, scenariosr, distjoint, file=file.path(save.dir, paste0(deal.name, ".RData")))
cat("generated scenarios for:", deal.name, "\n")
}
|
