diff options
Diffstat (limited to 'R/build_scenarios.R')
| -rw-r--r-- | R/build_scenarios.R | 66 |
1 files changed, 15 insertions, 51 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R index 98b55122..b6b3d63f 100644 --- a/R/build_scenarios.R +++ b/R/build_scenarios.R @@ -39,14 +39,14 @@ source(file.path(root.dir, "code", "R", "etdb.R")) source(file.path(root.dir, "code", "R", "cds_functions_generic.R"))
source(file.path(root.dir, "code", "R", "cds_utils.R"))
-calibration.date <- prevBusDay(tradedate)
-settledate <- getsettledate(tradedate)
+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
-Ngrid <- 201
+
MarkitData <- getMarkitIRData(calibration.date)
futurequotes <- read.csv(file.path(root.dir, "data", "Yield Curves",
sprintf("futures-%s.csv", calibration.date)), header=F)
@@ -58,16 +58,11 @@ L3m <- buildMarkitYC(MarkitData, futurequotes[,2]) L6m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1/2)
L12m <- buildMarkitYC(MarkitData, futurequotes[,2], dt = 1)
+Ngrid <- 201
support <- seq(0, 1, length = Ngrid)
-recoverylag <- 90 ##days
-## reinvestment parameters
-## need to match parameters in build_portfolios.R
-reinvspread <- 0.04
-reinvfixed <- 0.07
-basecase.rollingmaturity <- 84 ##months
-reinvlag <- 3 ##months
n.scenarios <- 100
recov.adj <- 1
+params <- yaml.load_file(file.path(root.dir, "code", "R", "params.yml"))
for(j in seq_along(dealnames)){
load(file.path(root.dir, "Scenarios", paste("Portfolios", tradedate, sep="_"),
@@ -131,9 +126,7 @@ for(j in seq_along(dealnames)){ ## linear approximation for monthly scenarios
deal.data <- getdealdata(dealnames[j], tradedate)
deal.datesmonthly <- getdealschedule(deal.data, "1 month")
- deal.datesmonthlylagged <- getdealschedule(deal.data, "1 month", lag=92)
deal.datesmonthly <- deal.datesmonthly[deal.datesmonthly>=settledate]
- deal.datesmonthlylagged <- deal.datesmonthly[deal.datesmonthlylagged>=settledate]
cdrmonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
recoverymonthly <- matrix(0, n.scenarios, length(deal.datesmonthly))
@@ -147,53 +140,24 @@ for(j in seq_along(dealnames)){ }
recoverymonthly <- pmin(recoverymonthly,1)
recoverymonthly[!is.finite(recoverymonthly)] <- 100
- ## compute reinvestment price
+
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))/365*12
+ - as.numeric(tradedate))
}else{
- rollingmaturity <- basecase.rollingmaturity
+ rollingmaturity <- params$rollingmaturity
}
- ## DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthlylagged))
- ## df <- DC$discounts
- ## forwards <- DC$forwards
+
+ ## 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")
- reinvassets <- get.reinvassets(dealnames[j], tradedate)
-
- reinvprices <- list()
- if(reinvflags[j] && length(reinvassets)>0){
- for(assetname in names(reinvassets)){
- asset <- reinvassets[[assetname]]
- if(asset$coupontype=="FLOAT") {
- coupon <- reinvspread
- }else{
- coupon <- reinvfixed
- }
- ##reinvest tweak
- #coupon <- coupon-0.01
-
- reinvprices[[assetname]] <- foreach(date = iter(deal.datesmonthly), .combine=c) %dopar% {
- 100 * forwardportfolioprice2(cdrmonthly.dt, recoverymonthly.dt, date,
- min(date+rollingmaturity*30, deal.data$maturity),
- asset$coupontype, coupon, asset$liborfloor/100)
- }
- ## reinvprices[[assetname]] <- foreach(date = iter(deal.datesmonthly), .combine=c) %dopar% {
- ## 100 * forwardportfolioprice(deal.portfolio, date,
- ## min(date+rollingmaturity*30, deal.data$maturity),
- ## asset$coupontype, coupon, asset$liborfloor/100)
- ## }
- ## for(i in seq_along(deal.datesmonthly)){
- ## date <- deal.datesmonthly[i]
- ## reinvprices[[assetname]] <-
- ## 100 * forwardportfolioprice2(cdrmonthly.dt, recoverymonthly.dt, date,
- ## min(date+rollingmaturity*30, deal.data$maturity),
- ## asset$coupontype, coupon, asset$liborfloor/100)
- ## }
-
- }
+ 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")
|
