diff options
Diffstat (limited to 'R')
| -rw-r--r-- | R/build_scenarios.R | 60 | ||||
| -rw-r--r-- | R/load_cf.R | 6 |
2 files changed, 29 insertions, 37 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R index e52d9794..5b339222 100644 --- a/R/build_scenarios.R +++ b/R/build_scenarios.R @@ -1,4 +1,5 @@ library(doParallel)
+library(yaml)
hostname <- system("hostname", intern=TRUE)
if(hostname=="debian"){
registerDoParallel(8)
@@ -42,8 +43,10 @@ get.reinvassets <- function(dealname, workdate){ sqlstr <- sprintf("select * from et_historicaldealinfo('%s', '%s') where ReinvFlag Is true",
dealname, workdate)
data <- dbGetQuery(dbCon, sqlstr)
- for(i in 1:dim(data)[1]){
- r[[data$issuername[i]]] <- list(coupontype=data$fixedorfloat[i], liborfloor=data$liborfloor[i])
+ if(nrow(data)>0){
+ for(i in 1:nrow(data)){
+ r[[data$issuername[i]]] <- list(coupontype=data$fixedorfloat[i], liborfloor=data$liborfloor[i])
+ }
}
return( r )
}
@@ -214,26 +217,25 @@ for(j in seq_along(dealnames)){ DC <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(L3m$params$tradeDate, deal.datesmonthlylagged))
df <- DC$discounts
forwards <- DC$forwards
- ##we need to jack up the reinvestment prices somehow so let's add a 75bps tweak...
- ## reinvprices.tweak <- compute.reinvprices(df, forwards, cdrmonthly, recoverymonthly, reinvspread+0.0075,
- ## reinvfixed+0.0075, rollingmaturity, reinvlag)
reinvassets <- get.reinvassets(dealnames[j], workdate)
reinvprices <- list()
- for(assetname in names(reinvassets)){
- asset <- reinvassets[[assetname]]
- if(asset$coupontype=="FLOAT") {
- coupon <- reinvspread
- }else{
- coupon <- reinvfixed
- }
- #reinvest tweak
- coupon <- coupon-0.0075
+ 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.0075
- 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)
+ 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)
+ }
}
}
## reinvprices <- compute.reinvprices(df, forwards, cdrmonthly, recoverymonthly, reinvspread,
@@ -286,27 +288,13 @@ for(j in seq_along(dealnames)){ 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(100 * reinvprices$loan,
- ## file = file.path(save.dir, paste0(dealnames[j], "-floatreinvprices.csv")),
- ## row.names=F, col.names=F, sep=",")
- ## write.table(100 * reinvprices$bond,
- ## file = file.path(save.dir, paste0(dealnames[j], "-fixedreinvprices.csv")),
- ## row.names=F, col.names=F, sep=",")
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"))
- cat("[config]\n", file = configfile)
- cat(paste("rollingmat", as.integer(rollingmaturity), sep="="), "\n",
- append=TRUE, file = configfile)
-
- topythonbool <- function(x){
- stopifnot(is.logical(x))
- return(if(x) "True" else "False")
- }
-
- cat(paste("reinvflag", topythonbool(reinvflags[j]), sep="="), "\n",
- append = TRUE, file=configfile)
-
+ config <- list(rollingmat = as.integer(rollingmaturity),
+ 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")
diff --git a/R/load_cf.R b/R/load_cf.R index ff9a8d6b..a8500225 100644 --- a/R/load_cf.R +++ b/R/load_cf.R @@ -1,5 +1,6 @@ library(RPostgreSQL)
library(RQuantLib)
+library(yaml)
args <- commandArgs(trailingOnly=TRUE)
@@ -69,7 +70,10 @@ getdealcf <- function(dealnames, workdate=Sys.Date()){ sqldata <- dbGetQuery(dbCon, sqlstring)
cfdata[[dealname]]$mv <- mv
cfdata[[dealname]]$currbal <- sqldata$"Curr Collat Bal"
- if(is.na(sqldata$"Reinv End Date")){
+ config <- file.path(file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"), "csv"),
+ paste0(dealname, ".config"))
+ config <- yaml.load_file(config)
+ if(is.na(sqldata$"Reinv End Date")||!config$reinvflag){
tranches <- "COLLAT"
}else{
tranches <- c("COLLAT_INITIAL", "COLLAT_REINVEST")
|
