diff options
| -rw-r--r-- | R/build_scenarios.R | 57 | ||||
| -rw-r--r-- | python/intex_scenarios.py | 144 |
2 files changed, 106 insertions, 95 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R index a4b952e2..5c9b6033 100644 --- a/R/build_scenarios.R +++ b/R/build_scenarios.R @@ -201,24 +201,23 @@ for(j in seq_along(dealnames)){ }
recoverymonthly <- pmin(recoverymonthly,1)
## compute reinvestment price
- if(reinvflags[j]){
- if(deal.data$"Reinv End Date" <= workdate){
+ if(!is.na(deal.data$"Reinv End Date") && deal.data$"Reinv End Date" <= workdate){
## 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(workdate))/365*12
- }else{
+ }else{
rollingmaturity <- basecase.rollingmaturity
- }
- 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)
- loanprices <- c()
- bondprices <- c()
- for(i in seq_along(deal.datesmonthly)){
+ }
+ 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)
+ loanprices <- c()
+ bondprices <- c()
+ for(i in seq_along(deal.datesmonthly)){
loanprices <- c(loanprices,
forwardportfolioprice(deal.portfolio, deal.datesmonthly[i],
min(deal.datesmonthly[i]+rollingmaturity*30, deal.data$maturity),
@@ -227,7 +226,7 @@ for(j in seq_along(dealnames)){ forwardportfolioprice(deal.portfolio, deal.datesmonthly[i],
min(deal.datesmonthly[i]+rollingmaturity*30, deal.data$maturity),
"fixed", reinvfixed, 0.4))
- }
+ }
## reinvprices <- compute.reinvprices(df, forwards, cdrmonthly, recoverymonthly, reinvspread,
## reinvfixed, rollingmaturity, reinvlag)
@@ -266,7 +265,6 @@ for(j in seq_along(dealnames)){ ## maturingbalance[,min(t+rollingmaturity, length(deal.datesmonthly))] +
## reinvdollar[,t]/loanprices[t]
## }
- }
save.dir <- file.path(root.dir, "Scenarios", paste("Intex curves", workdate, sep="_"), "csv")
if(!file.exists(save.dir)){
@@ -279,18 +277,27 @@ 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(cbind(100 * loanprices, 100 * bondprices),
+ file = file.path(save.dir, paste0(dealnames[j], "-reinvprices.csv")),
+ row.names=F, col.names=F, 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)
if(reinvflags[j]){
- ## 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(cbind(100 * loanprices, 100 * bondprices),
- file = file.path(save.dir, paste0(dealnames[j], "-reinvprices.csv")),
- row.names=F, col.names=F, sep=",")
- cat(as.integer(rollingmaturity), file=file.path(save.dir, paste0(dealnames[j], "-rollingmat")))
+ cat(paste("reinvflag", "True", sep="="), "\n", append= TRUE,
+ file = configfile)
+ }else{
+ cat(paste("reinvflag", "False", sep="="), "\n", append= TRUE,
+ file = configfile)
}
+
save(scenariosd, scenariosr, dist.joint, file=file.path(save.dir, paste0(dealnames[j], ".RData")),
compress="xz")
diff --git a/python/intex_scenarios.py b/python/intex_scenarios.py index 047f9942..f675b07d 100644 --- a/python/intex_scenarios.py +++ b/python/intex_scenarios.py @@ -8,6 +8,7 @@ import re import psycopg2
import common
import sys
+import ConfigParser
pattern11 = re.compile("(REINVEST\[)\w+(::REINV_TBA1\]\[DEAL,\d+\]=)(.*$)")
pattern12 = re.compile("(REINVEST\[)\w+(::REINV_TBA2\]\[DEAL,\d+\]=)(.*$)")
@@ -22,7 +23,7 @@ pattern5 = re.compile("(STANDARD_VAR\[)\w+(::#REINVPCT_TBA1,\d+\]=)(.*$)") # reinv fixed percentage
pattern6 = re.compile("(STANDARD_VAR\[)\w+(::#REINVPCT_TBA2,\d+\]=)(.*$)")
pattern7 = re.compile("GOLDL3")
-
+pattern8 = re.compile("(#COLLATREINV_REINV_PCT_EXT\[)\w+(::\*\]\[DEAL,\d+\])=100")
reinvfloatpercentage = 85
reinvfixedpercentage = 15
@@ -54,23 +55,25 @@ def convert_reinvtoperct(d): def generate_scenarios(workdate, dealnames, conn, cursor):
prometheus = os.path.join(common.root, "Scenarios", "prometheus.sss")
n_scenarios = 100
+ basedir = os.path.join(common.root, "Scenarios", "Intex curves_" + workdate)
+ config = ConfigParser.ConfigParser()
for dealname in dealnames:
cursor.execute('SELECT \"Reinv End Date\" from latest_clo_universe where dealname=%s', (dealname,))
reinvenddate = cursor.fetchone()[0]
+ config.read(os.path.join(basedir, "csv", dealname + ".config"))
if reinvenddate:
reinvenddate = reinvenddate.strftime("%Y%m%d")
reinvflag = True
else:
print "missing reinvestment end date"
reinvflag = False
+ reinvflag = config.getboolean('config', 'reinvflag')
+ rollingmat = config.getint('config', 'rollingmat')
reinv_assets = get_reinv_assets(dealname, workdate, cursor)
perct_reinv_assets = convert_reinvtoperct(reinv_assets)
- basedir = os.path.join(common.root, "Scenarios", "Intex curves_" + workdate)
floatreinvprices = []
fixedreinvprices = []
try:
- with open(os.path.join(basedir, "csv", dealname + "-rollingmat"), "rb") as fh:
- rollingmat = fh.readline().rstrip()
# with open(os.path.join(basedir, "csv", dealname + "-floatreinvprices.csv"), "rb") as fhreinv:
# for line in fhreinv:
# floatreinvprices.append(line.rstrip("\n").split(","))
@@ -79,11 +82,11 @@ def generate_scenarios(workdate, dealnames, conn, cursor): # fixedreinvprices.append(line.rstrip("\n").split(","))
with open(os.path.join(basedir, "csv", dealname + "-reinvprices.csv"), "rb") as fh:
for line in fh:
- a, b = line.split(",")
+ a, b = line.rstrip().split(",")
floatreinvprices.append(a)
fixedreinvprices.append(b)
- floatreinvprices = [floatreinvprices] * 100
- fixedreinvprices = [fixedreinvprices] * 100
+ floatreinvprices = ["{0:.3f}".format(float(p))for p in floatreinvprices]
+ fixedreinvprices = ["{0:.3f}".format(float(p)) for p in fixedreinvprices]
except IOError:
reinvflag = False
@@ -110,71 +113,72 @@ def generate_scenarios(workdate, dealnames, conn, cursor): newline = "DEAL_NAME=" + dealname.upper() + "\r\n"
fh2.write(newline)
continue
- if not reinvflag and "DO_REINV" in line:
- fh2.write("DO_REINV[DEAL]=0\r\n")
+ if not reinvflag and pattern8.match(line):
+ line = re.sub(pattern8, r"\1{0}\2=0".format(dealname.upper()), line)
+ fh2.write(line + "\r\n")
continue
- if reinvflag:
- if pattern11.match(line):
- line = re.sub(pattern11, r"\1{0}\2", line).format(dealname.upper())
+ if pattern11.match(line):
+ # pdb.set_trace()
+ line = re.sub(pattern11, r"\1{0}\2", line).format(dealname.upper())
- if reinv_assets["REINV_TBA1"] == "FLOAT":
- coupon = 4
- elif reinv_assets["REINV_TBA1"] == "FIXED":
- coupon = 7
- line = line + \
- "COUP_SPR={0}|AMORT=Bullet|USE_REINVEST_PIP=1|MAT_DATE={1}|".format(coupon,
- rollingmat)
- fh2.write(line + "\r\n")
- continue
- if pattern12.match(line):
- line = re.sub(pattern12, r"\1{0}\2", line).format(dealname.upper())
- if reinv_assets["REINV_TBA2"] == "FLOAT":
- coupon = 4
- elif reinv_assets["REINV_TBA2"] == "FIXED":
- coupon = 7
- line = line + \
- "COUP_SPR={0}|AMORT=Bullet|USE_REINVEST_PIP=1|MAT_DATE={1}|".format(coupon,
- rollingmat)
- fh2.write(line + "\r\n")
- continue
- if pattern2.match(line):
- line = re.sub(pattern2, r"\1{0}\2{1}", line).format(dealname.upper(), reinvenddate)
- fh2.write(line + "\r\n")
- continue
- if pattern3.match(line):
- if reinv_assets['REINV_TBA1'] == 'FIXED':
- line = re.sub(pattern3, r"\1{0}\2{1}", line).format(dealname.upper(),
- " ".join(fixedreinvprices[i-2]))
- elif reinv_assets['REINV_TBA1'] == 'FLOAT':
- line = re.sub(pattern3, r"\1{0}\2{1}", line).format(dealname.upper(),
- " ".join(floatreinvprices[i-2]))
- fh2.write(line + "\r\n")
- continue
- if pattern4.match(line):
- if reinv_assets['REINV_TBA2'] == 'FIXED':
- line = re.sub(pattern4, r"\1{0}\2{1}", line).format(dealname.upper(),
- " ".join(fixedreinvprices[i-2]))
- elif reinv_assets['REINV_TBA2'] == 'FLOAT':
- line = re.sub(pattern4, r"\1{0}\2{1}", line).format(dealname.upper(),
- " ".join(floatreinvprices[i-2]))
- fh2.write(line + "\r\n")
- continue
- if pattern5.match(line):
- if reinv_assets['REINV_TBA1']:
- line = re.sub(pattern5, r"\1{0}\2{1}", line).format(
- dealname.upper(), perct_reinv_assets['REINV_TBA1']).rstrip()
- fh2.write(line + "\r\n")
- continue
- if pattern6.match(line):
- if reinv_assets['REINV_TBA2']:
- line = re.sub(pattern6, r"\1{0}\2{1}", line).format(
- dealname.upper(), perct_reinv_assets['REINV_TBA2']).rstrip()
- fh2.write(line + "\r\n")
- continue
- if pattern7.search(line):
- line = re.sub(pattern7, dealname.upper(), line)
- fh2.write(line + "\r\n")
- continue
+ if reinv_assets["REINV_TBA1"] == "FLOAT":
+ coupon = 4
+ elif reinv_assets["REINV_TBA1"] == "FIXED":
+ coupon = 7
+ line = line + \
+ "COUP_SPR={0}|AMORT=Bullet|USE_REINVEST_PIP=1|MAT_DATE={1}|".format(coupon,
+ rollingmat)
+ fh2.write(line + "\r\n")
+ continue
+ if pattern12.match(line):
+ line = re.sub(pattern12, r"\1{0}\2", line).format(dealname.upper())
+ if reinv_assets["REINV_TBA2"] == "FLOAT":
+ coupon = 4
+ elif reinv_assets["REINV_TBA2"] == "FIXED":
+ coupon = 7
+ line = line + \
+ "COUP_SPR={0}|AMORT=Bullet|USE_REINVEST_PIP=1|MAT_DATE={1}|".format(coupon,
+ rollingmat)
+ fh2.write(line + "\r\n")
+ continue
+ if pattern2.match(line):
+ line = re.sub(pattern2, r"\1{0}\2{1}", line).format(dealname.upper(), reinvenddate)
+ fh2.write(line + "\r\n")
+ continue
+ if pattern3.match(line):
+ if reinv_assets['REINV_TBA1'] == 'FIXED':
+ line = re.sub(pattern3, r"\1{0}\2{1}", line).format(dealname.upper(),
+ " ".join(fixedreinvprices))
+ elif reinv_assets['REINV_TBA1'] == 'FLOAT':
+ line = re.sub(pattern3, r"\1{0}\2{1}", line).format(dealname.upper(),
+ " ".join(floatreinvprices))
+ fh2.write(line + "\r\n")
+ continue
+ if pattern4.match(line):
+ if reinv_assets['REINV_TBA2'] == 'FIXED':
+ line = re.sub(pattern4, r"\1{0}\2{1}", line).format(dealname.upper(),
+ " ".join(fixedreinvprices))
+ elif reinv_assets['REINV_TBA2'] == 'FLOAT':
+ line = re.sub(pattern4, r"\1{0}\2{1}", line).format(dealname.upper(),
+ " ".join(floatreinvprices))
+ fh2.write(line + "\r\n")
+ continue
+ if pattern5.match(line):
+ if reinv_assets['REINV_TBA1']:
+ line = re.sub(pattern5, r"\1{0}\2{1}", line).format(
+ dealname.upper(), perct_reinv_assets['REINV_TBA1']).rstrip()
+ fh2.write(line + "\r\n")
+ continue
+ if pattern6.match(line):
+ if reinv_assets['REINV_TBA2']:
+ line = re.sub(pattern6, r"\1{0}\2{1}", line).format(
+ dealname.upper(), perct_reinv_assets['REINV_TBA2']).rstrip()
+ fh2.write(line + "\r\n")
+ continue
+ if pattern7.search(line):
+ line = re.sub(pattern7, dealname.upper(), line)
+ fh2.write(line + "\r\n")
+ continue
# if "STANDARD_VAR" in line:
# newline = "STANDARD_VAR[REINVEST_PRICE,1]=" + " ".join(reinvprices)
# fh2.write(newline)
|
