diff options
Diffstat (limited to 'python/calibrate_tranches.py')
| -rw-r--r-- | python/calibrate_tranches.py | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/python/calibrate_tranches.py b/python/calibrate_tranches.py index c9e64e5f..98a87953 100644 --- a/python/calibrate_tranches.py +++ b/python/calibrate_tranches.py @@ -13,24 +13,38 @@ Z, w = GHquad(n_int) with open("../R/index_definitions.yml") as fh: indices = yaml.load(fh, Loader=yaml.FullLoader) -indices['hy21']['maturity'] = datetime.date(1970, 1, 1) + datetime.timedelta(indices['hy21']['maturity']) -hy21 = indices['hy21'] +indices["hy21"]["maturity"] = datetime.date(1970, 1, 1) + datetime.timedelta( + indices["hy21"]["maturity"] +) +hy21 = indices["hy21"] hy21["startdate"] = datetime.date(2013, 9, 20) -dates = [f[9:19] for f in os.listdir(os.path.join(os.environ['DATA_DIR'], "Backtest")) if "survprob" in f] +dates = [ + f[9:19] + for f in os.listdir(os.path.join(os.environ["DATA_DIR"], "Backtest")) + if "survprob" in f +] Rho = np.zeros((len(dates), 3)) for i, d in enumerate(dates): startdate = datetime.datetime.strptime(d, "%Y-%m-%d") ts = YC(startdate) - with open(os.path.join(os.environ['DATA_DIR'], "Backtest", "recov_{0}.csv".format(d))) as fh: - recov = np.array([float(e) for e in fh], dtype='double', order='F') + with open( + os.path.join(os.environ["DATA_DIR"], "Backtest", "recov_{0}.csv".format(d)) + ) as fh: + recov = np.array([float(e) for e in fh], dtype="double", order="F") - with open(os.path.join(os.environ['DATA_DIR'], "Backtest", "survprob_{0}.csv".format(d))) as fh: - fh.readline() ##skip header - SurvProb = np.array([[float(e) for e in line.split(",")] for line in fh], dtype='double', order='F') + with open( + os.path.join(os.environ["DATA_DIR"], "Backtest", "survprob_{0}.csv".format(d)) + ) as fh: + fh.readline() ##skip header + SurvProb = np.array( + [[float(e) for e in line.split(",")] for line in fh], + dtype="double", + order="F", + ) defaultprob = 1 - SurvProb - issuerweights = np.ones(100)/100 + issuerweights = np.ones(100) / 100 rho = 0.4 Ngrid = 101 @@ -38,25 +52,33 @@ for i, d in enumerate(dates): K = np.array([0, 0.15, 0.25, 0.35, 1]) Kmod = adjust_attachments(K, hy21["loss"], hy21["factor"]) - quotes = pd.read_csv(os.path.join(os.environ['BASE_DIR'], "Scenarios", "Calibration", - "hy21_tranches_{0}.csv".format(d))) - quotes = quotes["Mid"]/100 + quotes = pd.read_csv( + os.path.join( + os.environ["BASE_DIR"], + "Scenarios", + "Calibration", + "hy21_tranches_{0}.csv".format(d), + ) + ) + quotes = quotes["Mid"] / 100 dK = np.diff(Kmod) - quotes = np.cumsum(dK * (1-quotes)) + quotes = np.cumsum(dK * (1 - quotes)) sched = creditSchedule(startdate, "5Yr", 0.05, ts, enddate=hy21["maturity"]) acc = cdsAccrued(startdate, 0.05) for j, q in enumerate(quotes[:-1]): + def aux(rho): L, R = BClossdist(defaultprob, issuerweights, recov, rho, Z, w, 101) - cl = tranche_cl(L, R, sched, 0, Kmod[j+1]) - pl = tranche_pl(L, sched, 0, Kmod[j+1]) - return cl+pl+q-acc + cl = tranche_cl(L, R, sched, 0, Kmod[j + 1]) + pl = tranche_pl(L, sched, 0, Kmod[j + 1]) + return cl + pl + q - acc + l, u = (0, 1) for _ in range(10): - rho = (l+u)/2. + rho = (l + u) / 2.0 if aux(rho) > 0: u = rho else: l = rho - Rho[i, j] = (l+u)/2. - print(Rho[i,:]) + Rho[i, j] = (l + u) / 2.0 + print(Rho[i, :]) |
