aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/cds_curve.py64
1 files changed, 34 insertions, 30 deletions
diff --git a/python/cds_curve.py b/python/cds_curve.py
index 7b08bb62..ac4ba8e7 100644
--- a/python/cds_curve.py
+++ b/python/cds_curve.py
@@ -33,7 +33,7 @@ def build_curve(r, today_date, yc, start_date, step_in_date, value_date, end_dat
sc = SpreadCurve(today_date, yc, start_date, step_in_date, value_date,
end_dates, spread_curve, upfront_curve, recovery_curve,
ticker=r['cds_ticker'])
- if np.any(np.isnan(upfront_curve)):
+ if len(sc) != end_dates.shape[0]:
sc = fill_curve(sc, end_dates)
except ValueError as e:
print(e)
@@ -64,7 +64,7 @@ def get_singlenames_curves(index_type, series, 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)
+ curves = build_curves(sn_quotes, args)
return curves, args
def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_date, maturities):
@@ -114,15 +114,13 @@ 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, start_date or issue_date)
+ start_date = start_date or index_quotes.index.get_level_values(0)[0]
+ index_quotes = index_quotes[start_date:]
+ curves, _ = get_singlenames_curves(index_type, series, start_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
@@ -130,24 +128,31 @@ def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr']
s.name = 'index_quote'
quotes = pd.concat([index_desc, s], axis=1).dropna()
for m, coupon, index_quote in quotes[['coupon', 'index_quote']].itertuples():
- try:
- eps = brentq(lambda epsilon: index.pv(step_in_date, value_date,
- m, jp_yc, recovery,
- coupon, epsilon) -
- index_quote, lo, hi)
- except ValueError:
+ lo, hi = -0.3, 0.3
+ while lo > -1:
+ try:
+ eps = brentq(lambda epsilon: index.pv(step_in_date, value_date,
+ m, jp_yc, recovery,
+ coupon, epsilon) -
+ index_quote, lo, hi)
+ except ValueError:
+ lo *= 1.1
+ hi *= 1.1
+ else:
+ break
+ else:
print("couldn't calibrate for date: {} and maturity: {}".
- format(trade_date,m))
+ format(trade_date.date(), m.date()))
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,
+ continue
+ #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)
@@ -155,13 +160,12 @@ def calibrate_portfolio(index_type, series, tenors=['3yr', '5yr', '7yr', '10yr']
if __name__=="__main__":
enable_logging()
- 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()
+ df = calibrate_portfolio("HY", 25, ['3yr', '5yr', '7yr', '10yr'])
+ # 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()