aboutsummaryrefslogtreecommitdiffstats
path: root/python/calibrate_tranches.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/calibrate_tranches.py')
-rw-r--r--python/calibrate_tranches.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/python/calibrate_tranches.py b/python/calibrate_tranches.py
new file mode 100644
index 00000000..743a0df7
--- /dev/null
+++ b/python/calibrate_tranches.py
@@ -0,0 +1,63 @@
+import numpy as np
+from tranche_functions import *
+from yieldcurve import YC
+import yaml
+import datetime
+import os
+import pandas as pd
+import pdb
+
+n_int = 500
+n_credit = 100
+Z, w = GHquad(n_int)
+
+with open("../R/index_definitions.yml") as fh:
+ indices = yaml.load(fh)
+indices['hy21']['maturity'] = datetime.date(1970, 1, 1) + datetime.timedelta(indices['hy21']['maturity'])
+hy21 = indices['hy21']
+hy21["startdate"] = datetime.date(2013, 9, 20)
+root = "/home/share/CorpCDOs"
+dates = [f[9:19] for f in os.listdir(os.path.join(root, "data", "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(root, "data", "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(root, "data", "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
+
+ rho = 0.4
+ Ngrid = 101
+
+ 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(root, "Scenarios", "Calibration",
+ "hy21_tranches_{0}.csv".format(d)))
+ quotes = quotes["Mid"]/100
+ dK = np.diff(Kmod)
+ 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
+ l, u = (0, 1)
+ for _ in range(10):
+ rho = (l+u)/2.
+ if aux(rho) > 0:
+ u = rho
+ else:
+ l = rho
+ Rho[i, j] = (l+u)/2.
+ print(Rho[i,:])