aboutsummaryrefslogtreecommitdiffstats
path: root/python/cds_curve.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/cds_curve.py')
-rw-r--r--python/cds_curve.py48
1 files changed, 35 insertions, 13 deletions
diff --git a/python/cds_curve.py b/python/cds_curve.py
index 96413b10..7b08bb62 100644
--- a/python/cds_curve.py
+++ b/python/cds_curve.py
@@ -89,7 +89,8 @@ def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_dat
serenitas_engine = dbengine('serenitasdb')
-def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr']):
+def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr'],
+ start_date=None):
if index_type == 'IG':
recovery = 0.4
else:
@@ -113,28 +114,40 @@ def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr']
issue_date = index_desc.issue_date[0]
index_desc = index_desc.set_index('maturity')
maturities = index_quotes.columns.sort_values().to_pydatetime()
- curves, _ = get_singlenames_curves(index_type, series, issue_date)
+
+ curves, _ = get_singlenames_curves(index_type, series, start_date or issue_date)
index = CreditIndex(issue_date, maturities, curves)
r = {}
+ lo, hi = -0.5, 0.5
for k, s in index_quotes.iterrows():
trade_date, version = k
+ if start_date and trade_date < start_date:
+ continue
curves, args = get_singlenames_curves(index_type, series, trade_date)
_, jp_yc, _, step_in_date, value_date, _ = args
index.curves = curves
tweak, duration, theta = [], [], []
s.name = 'index_quote'
- quotes = pd.concat([index_desc, s], axis=1)
+ quotes = pd.concat([index_desc, s], axis=1).dropna()
for m, coupon, index_quote in quotes[['coupon', 'index_quote']].itertuples():
- eps = brentq(lambda epsilon: index.pv(step_in_date, value_date,
- m, jp_yc, recovery,
- coupon, epsilon) -
- index_quote, -0.5, 0.3)
- #tweak the curves in place
- index.tweak_portfolio(eps, m)
- duration.append(index.duration(step_in_date, value_date, m, jp_yc))
- theta.append(index.theta(step_in_date, value_date,
+ try:
+ eps = brentq(lambda epsilon: index.pv(step_in_date, value_date,
+ m, jp_yc, recovery,
+ coupon, epsilon) -
+ index_quote, lo, hi)
+ except ValueError:
+ print("couldn't calibrate for date: {} and maturity: {}".
+ format(trade_date,m))
+ tweak.append(np.NaN)
+ duration.append(np.NaN)
+ theta.append(np.NaN)
+ else:
+ #tweak the curves in place
+ index.tweak_portfolio(eps, m)
+ duration.append(index.duration(step_in_date, value_date, m, jp_yc))
+ theta.append(index.theta(step_in_date, value_date,
m, jp_yc, recovery, coupon))
- tweak.append(eps)
+ tweak.append(eps)
r[trade_date] = pd.DataFrame({'duration': duration,
'theta': theta,
'tweak': tweak}, index=tenors)
@@ -142,4 +155,13 @@ def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr']
if __name__=="__main__":
enable_logging()
- df = calibrate_portfolio("IG", 25)
+ df = calibrate_portfolio("IG", 22, ['3yr', '5yr', '7yr', '10yr'],
+ start_date=pd.Timestamp('2014-06-20'))
+ conn = dbconn('serenitasdb')
+ with conn.cursor() as c:
+ for k, s in df.iterrows():
+ c.execute("UPDATE index_quotes SET duration2=%s, theta2=%s "\
+ "WHERE date=%s AND tenor=%s AND index=%s AND series=%s",
+ (s.duration, s.theta, k[0], k[1], "IG", 22))
+ conn.commit()
+ conn.close()