aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/index.py7
-rw-r--r--python/cds_curve.py48
2 files changed, 41 insertions, 14 deletions
diff --git a/python/analytics/index.py b/python/analytics/index.py
index 75a6e2a6..a8c9a8ef 100644
--- a/python/analytics/index.py
+++ b/python/analytics/index.py
@@ -299,6 +299,8 @@ class Index(object):
@trade_date.setter
def trade_date(self, d):
+ if isinstance(d, pd.Timestamp):
+ d = d.date()
prebuilt_curves = getattr(yieldcurve, '_{}_curves'.format(self.currency))
self.start_date = previous_twentieth(d)
if d in prebuilt_curves:
@@ -484,7 +486,10 @@ class ForwardIndex(object):
'__weakref__']
def __init__(self, index, forward_date):
self.index = index
- self.forward_date = forward_date
+ if isinstance(forward_date, pd.Timestamp):
+ self.forward_date = forward_date.date()
+ else:
+ self.forward_date = forward_date
self.exercise_date_settle = pd.Timestamp(forward_date) + 3* BDay()
self.df = index._yc.discount_factor(self.exercise_date_settle)
self._update()
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()