diff options
| -rw-r--r-- | python/cds_curve.py | 87 |
1 files changed, 37 insertions, 50 deletions
diff --git a/python/cds_curve.py b/python/cds_curve.py index 4f96fbfb..d967c50c 100644 --- a/python/cds_curve.py +++ b/python/cds_curve.py @@ -1,4 +1,5 @@ -from pyisda.curve import YieldCurve, BadDay, SpreadCurve, BasketIndex +from pyisda.curve import YieldCurve, BadDay, SpreadCurve +from pyisda.credit_index import CreditIndex from pyisda.legs import FeeLeg, ContingentLeg from pyisda.logging import enable_logging import datetime @@ -43,28 +44,9 @@ def build_curves(quotes, args): return [build_curve(q, *args) for q in quotes if q is not None] -Settings().evaluation_date = Date(6, 2, 2017) -yc = YC() -jp_yc = ql_to_jp(yc) -today_date = datetime.date(2017, 2, 6) -step_in_date = datetime.date(2017, 2, 7) -value_date = datetime.date(2017, 2, 9) -start_date = datetime.date(2016, 12, 20) -end_dates = [datetime.date(2017, 12, 20), - datetime.date(2018, 12, 20), - datetime.date(2019, 12, 20), - datetime.date(2020, 12, 20), - datetime.date(2021, 12, 20), - datetime.date(2023, 12, 20), - datetime.date(2026, 12, 20)] - -quotes = get_singlenames_quotes("ig27", today_date) -args = (today_date, jp_yc, start_date, step_in_date, value_date, end_dates) -curves = build_curves_dist(quotes, args) - -def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_date, end_dates): +def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_date, maturities): r = {} - for d in end_dates: + for d in maturities: tenor = {} coupon_leg = FeeLeg(start_date, d, True, 1., 1.) default_leg = ContingentLeg(start_date, d, True) @@ -80,8 +62,7 @@ def all_curves_pv(curves, today_date, jp_yc, start_date, step_in_date, value_dat r[pd.Timestamp(d)] = pd.DataFrame.from_records(data, index=tickers, columns=['duration', 'protection_pv']) - indexpv = r.mean().unstack() - return indexpv.protection_pv-indexpv.duration*0.01 + return pd.concat(r, axis=1).swaplevel(axis=1).sort_index(axis=1,level=0) def stack_curves(curves): dates = [d for d, _ in curves[0].inspect()['data']] @@ -90,19 +71,6 @@ def stack_curves(curves): hazard_rates[i] = np.array([h for _, h in sc.inspect()['data']]) return hazard_rates, dates -def all_curves_pv2(curves, today_date, jp_yc, start_date, step_in_date, value_date, end_dates): - tickers = [t for t, _ in curves] - hazard_rates, end_dates = stack_curves([c for _, c in curves]) - r = {} - for maturity in end_dates: - data = [] - ig27 = BasketIndex(today_date, start_date, maturity, end_dates, hazard_rates) - data.append(ig27.pv(step_in_date, value_date, jp_yc, 0.4)) - r[pd.Timestamp(d)] = pd.DataFrame.from_records(data, - index=tickers, - columns=['duration', 'protection_pv']) - return r - def forward_hazard_rates(sc): r = [] t = [] @@ -117,17 +85,36 @@ def forward_hazard_rates(sc): h1, t1 = h2, t2 return t, r -ticker, sc = curves[0] -sc2 = sc.tweak_curve(0.01, inplace=False) -# class CreditIndex: -# def __init__(name, trade_date): +Settings().evaluation_date = Date(6, 2, 2017) +yc = YC() +jp_yc = ql_to_jp(yc) +today_date = datetime.date(2017, 2, 6) +step_in_date = datetime.date(2017, 2, 7) +value_date = datetime.date(2017, 2, 9) +start_date = datetime.date(2016, 12, 20) +end_dates = [datetime.date(2017, 12, 20), + datetime.date(2018, 12, 20), + datetime.date(2019, 12, 20), + datetime.date(2020, 12, 20), + datetime.date(2021, 12, 20), + datetime.date(2023, 12, 20), + datetime.date(2026, 12, 20)] + +quotes = get_singlenames_quotes("ig27", today_date) +maturities = [datetime.date(2019, 12, 20), + datetime.date(2021, 12, 20), + datetime.date(2023, 12, 20), + datetime.date(2026, 12, 20)] +args = (today_date, jp_yc, start_date, step_in_date, value_date, maturities) +curves = build_curves_dist(quotes, args) +test = all_curves_pv(curves, *args) +ig27 = CreditIndex(start_date, maturities, curves) +test2 = ig27.pv_vec(step_in_date, value_date, jp_yc, 0.4) + +from index_data import get_index_quotes +quotes = get_index_quotes("IG", 27, ['3yr', '5yr', '7yr', '10yr']) -# @property -# def quotes(): -# pass -# forward_hazard_rates() -# for d, q in -# @quotes.setter -# def quotes(val): -# self._quotes = val -# for d, q in self._quotes: +def calibrate_portfolio(index, step_in_date, value_date, yc, recovery, quotes): + for i, m in index.maturities: + eps = brentq(lambda epsilon: index.pv(step_in_date, value_date, m, yc, recovery, epsilon) + - quote, -0.15, 0.3) |
