aboutsummaryrefslogtreecommitdiffstats
path: root/python/rmbs
diff options
context:
space:
mode:
Diffstat (limited to 'python/rmbs')
-rw-r--r--python/rmbs/CRT_data.py40
-rw-r--r--python/rmbs/marketing.py60
2 files changed, 100 insertions, 0 deletions
diff --git a/python/rmbs/CRT_data.py b/python/rmbs/CRT_data.py
new file mode 100644
index 00000000..a4084575
--- /dev/null
+++ b/python/rmbs/CRT_data.py
@@ -0,0 +1,40 @@
+from glob import iglob
+import os
+import pandas as pd
+import datetime
+from db import dbengine
+import .load_globeop_report as load_globeop
+
+import .bbg_helpers
+
+def get_CRT_notional():
+
+ BBG_IP = ['192.168.9.65']
+ bbgstartdate = pd.datetime(2011, 1, 1)
+ path = '/home/serenitas/edwin/CRT/'
+ hist_securities = pd.read_csv(path + 'all_cusips.csv', header=None)
+ hist_securities = hist_securities + ' Mtge'
+ hist_fields = ['MTG_HIST_FACT']
+ orig_ntl_field = ['MTG_ORIG_AMT']
+
+ with bbg_helpers.init_bbg_session(BBG_IP) as session:
+ hist_data = bbg_helpers.retrieve_data(session, hist_securities[0].tolist(), hist_fields)
+
+ with bbg_helpers.init_bbg_session(BBG_IP) as session:
+ orig_ntl = bbg_helpers.retrieve_data(session, hist_securities[0].tolist(), orig_ntl_field)
+
+ hist_data1 = pd.DataFrame()
+ for cusip in hist_data:
+ new_df= pd.DataFrame()
+ new_df['notional'] = hist_data[cusip]['MTG_HIST_FACT']['Factor'] * orig_ntl[cusip]['MTG_ORIG_AMT']
+ new_df['date'] = hist_data[cusip]['MTG_HIST_FACT']['Payment Date']
+ new_df['cusip'] = cusip
+ hist_data1 = hist_data1.append(new_df)
+
+ hist_data1.to_hdf(path + 'crt_notional.hdf','notionals')
+
+def calc_CRT_notional():
+ path = '/home/serenitas/edwin/CRT/'
+ hist_data1 = pd.read_hdf(path + 'crt_notional.hdf')
+ hist_data1 = hist_data1.set_index('date')
+ return hist_data1.groupby(pd.TimeGrouper('M')).sum() \ No newline at end of file
diff --git a/python/rmbs/marketing.py b/python/rmbs/marketing.py
new file mode 100644
index 00000000..120e00bd
--- /dev/null
+++ b/python/rmbs/marketing.py
@@ -0,0 +1,60 @@
+import pandas as pd
+import datetime
+from db import dbengine
+from yieldcurve import YC
+
+def ver_one():
+ date = (datetime.date.today() + pd.offsets.BMonthEnd(-1)).date()
+ df = pd.read_sql_query("select * from risk_positions(%s, 'Subprime') where price >0 and length(identifier) = 9"
+ , dbengine('dawndb'), params = [date], index_col=['identifier'])
+ v1_temp = {}
+ for cusip in df.index:
+ v1_temp[cusip] = pd.read_sql_query("select * from priced_percentiles where date(timestamp) = %s and cusip = %s and model_version = 1"
+ , dbengine('dawndb'), params = [date, cusip])
+ v1 = pd.concat(v1_temp)
+ v1 = v1.set_index('cusip')
+ v1 = v1.merge(df, left_index=True, right_index=True)
+ yc = YC(evaluation_date=date)
+ v1 = v1.replace('M_MTG_GOOD','M_MTG_LMG')
+
+ v1['return'] = v1.apply(lambda df: (df.pv/(df.price/100)) ** (1/df.moddur) - 1 + yc.zero_rate(df.moddur).rate, axis = 1)
+
+ port_ret = v1.groupby('percentile').apply(wavg, 'return', 'usd_market_value')
+ strat_ret = v1.groupby(['percentile','strategy']).apply(wavg, 'return', 'usd_market_value').unstack(-1)
+ port_ret = port_ret.rename('Portfolio')
+ strat_ret = strat_ret.join(port_ret)
+
+ return strat_ret
+
+def plot_strat():
+
+ import seaborn as sns
+
+ strat_ret = ver_one()
+ strat_ret = strat_ret.rename(columns={ \
+ 'M_MTG_FP': 'First Pay',\
+ 'M_MTG_IO': 'Interest Only',\
+ 'M_MTG_LMG': 'Long Duration Par',\
+ 'M_MTG_PR': 'Pro-Rata Pay',\
+ 'M_MTG_RW': 'R&W Dependent',\
+ 'M_MTG_SD': 'Stepdown',\
+ 'M_MTG_THRU': 'Pass-through'
+ })
+
+ sns.set(style="darkgrid")
+ ax = strat_ret.plot(figsize = [10, 3.5])
+ ax.set_ylim([-.2,.6])
+
+ vals = ax.get_yticks()
+ ax.set_yticklabels(['{:3.0f}%'.format(x*100) for x in vals])
+ ax.set_ylabel('return')
+
+ ax.legend(loc='upper center', bbox_to_anchor=(0.5, -.05), shadow=True, ncol=3)
+
+def wavg(group, avg_name, weight_name):
+ d = group[avg_name]
+ w = group[weight_name]
+ try:
+ return (d * w).sum() / w.sum()
+ except ZeroDivisionError:
+ return d.mean()