diff options
Diffstat (limited to 'python/cds_curve.py')
| -rw-r--r-- | python/cds_curve.py | 75 |
1 files changed, 31 insertions, 44 deletions
diff --git a/python/cds_curve.py b/python/cds_curve.py index 4cd09f81..fcdefd3a 100644 --- a/python/cds_curve.py +++ b/python/cds_curve.py @@ -19,6 +19,7 @@ from pandas.tseries.offsets import BDay from scipy.optimize import brentq from dateutil.relativedelta import relativedelta from pyisda.logging import enable_logging +from analytics.utils import roll_date, previous_twentieth def get_singlenames_quotes(indexname, date): conn = dbconn('serenitasdb') @@ -54,6 +55,20 @@ def build_curves_dist(quotes, args, workers=4): def build_curves(quotes, args): return [build_curve(q, *args) for q in quotes if q is not None] +def get_singlenames_curves(index_type, series, trade_date): + end_dates = roll_date(trade_date, [1, 2, 3, 4, 5, 7, 10]) + sn_quotes = get_singlenames_quotes("{}{}".format(index_type.lower(), series), + trade_date.date()) + Settings().evaluation_date = Date.from_datetime(trade_date) + yc = YC() + jp_yc = ql_to_jp(yc) + start_date = previous_twentieth(trade_date) + step_in_date = trade_date + datetime.timedelta(days=1) + value_date = pd.Timestamp(trade_date) + 3* BDay() + args = (trade_date, jp_yc, start_date, step_in_date, value_date, end_dates) + curves = build_curves_dist(sn_quotes, args) + return curves, args + def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_date, maturities): r = {} for d in maturities: @@ -94,26 +109,6 @@ def forward_hazard_rates(sc): h1, t1 = h2, t2 return t, r -def roll_date(d, tenor): - """ roll_date to the next CDS maturity - - convention is only valid starting 09-20-2015""" - if isinstance(tenor, (int, float)): - d_rolled = d + relativedelta(years=tenor) - else: - d_rolled = d + relativedelta(years=1) - if((d >= d + relativedelta(month=9, day=20)) or \ - (d < d + relativedelta(month=3, day=20))): - d_rolled += relativedelta(month=12, day=20) - if d.month <= 3: - d_rolled -= relativedelta(years=1) - else: - d_rolled += relativedelta(month=6, day=20) - if isinstance(tenor, (int, float)): - return d_rolled - else: - return [d_rolled + relativedelta(years=t-1) for t in tenor] - serenitas_engine = dbengine('serenitasdb') def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr']): @@ -133,28 +128,17 @@ def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr'] index_quotes.columns = index_desc.loc[index_quotes.columns, "maturity"] index_quotes = 1 - index_quotes / 100 issue_date = index_desc.issue_date[0] - end_dates = [issue_date + relativedelta(years=i, months=3) for i \ - in [1, 2, 3, 4, 5, 7, 10]] index_desc = index_desc.set_index('maturity') maturities = index_quotes.columns.sort_values().to_pydatetime() + curves, _ = get_singlenames_curves(index_type, series, issue_date) + index = CreditIndex(issue_date, maturities, curves) r = {} for k, s in index_quotes.iterrows(): trade_date = k[0] - print(trade_date) - end_dates = roll_date(trade_date, [1,2,3,4,5,7,10]) - sn_quotes = get_singlenames_quotes("{}{}".format(index_type.lower(), series), - trade_date.date()) - Settings().evaluation_date = Date.from_datetime(trade_date) - yc = YC() - jp_yc = ql_to_jp(yc) - step_in_date = trade_date + datetime.timedelta(days=1) - value_date = pd.Timestamp(trade_date) + 3* BDay() - args = (trade_date, jp_yc, issue_date, step_in_date, value_date, end_dates) - curves = build_curves_dist(sn_quotes, args) - index = CreditIndex(issue_date, maturities, curves) - d = {'tweak':[], - 'duration':[], - 'theta':[]} + curves, args = get_singlenames_curves(index_type, series, trade_date) + _, jp_yc, _, step_in_date, value_date, _ = args + index.curves = [c for _, c in curves] + tweak, duration, theta = [], [], [] s.name = 'index_quote' quotes = pd.concat([index_desc, s], axis=1) for m, coupon, index_quote in quotes[['coupon', 'index_quote']].itertuples(): @@ -164,13 +148,16 @@ def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr'] index_quote, -0.35, 0.3) #tweak the curves in place index.tweak_portfolio(eps, m) - d['duration'].append(index.duration(step_in_date, value_date, m, jp_yc)) - d['theta'].append(index_quote - index.theta(step_in_date, value_date, - m - relativedelta(years=1), - jp_yc, recovery, coupon) + - coupon) - d['tweak'].append(eps) - r[trade_date] = pd.DataFrame(d, index=tenors) + duration.append(index.duration(step_in_date, value_date, m, jp_yc)) + theta.append(index_quote - + index.theta(step_in_date, value_date, + m - relativedelta(years=1), + jp_yc, recovery, coupon) + + coupon) + tweak.append(eps) + r[trade_date] = pd.DataFrame({'duration': duration, + 'theta': theta, + 'tweak': tweak}, index=tenors) return pd.concat(r) if __name__=="__main__": |
