aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics')
-rw-r--r--python/analytics/tranche_basket.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py
index 9b63f80e..ee7f25ca 100644
--- a/python/analytics/tranche_basket.py
+++ b/python/analytics/tranche_basket.py
@@ -251,9 +251,11 @@ class DualCorrTranche():
names=['spread_shock', 'corr_shock']))
def mark(self, **args):
- sql_string = ("SELECT corr_at_detach FROM get_tranche_quotes(%s, %s, %s, %s) a "
- "LEFT JOIN risk_numbers_new b ON a.id = b.tranche_id "
- "WHERE a.detach = %s OR a.attach = %s ORDER BY a.attach")
+ sql_string = ("SELECT corr_at_detach FROM tranche_risk b "
+ "LEFT JOIN tranche_quotes a ON a.id = b.tranche_id "
+ "WHERE a.index=%s AND a.series=%s AND a.tenor=%s "
+ "AND quotedate::date=%s "
+ "AND (a.detach = %s OR a.attach = %s) ORDER BY a.attach")
with dbconn('serenitasdb') as conn:
with conn.cursor() as c:
c.execute(sql_string, (self.index_type, self.series,
@@ -401,7 +403,7 @@ class TrancheBasket(BasketIndex):
if ((K == 0. and not complement) or (K == 1. and complement)):
return 0., 0.
elif ((K == 1. and not complement) or (K == 0. and complement)):
- self.index_pv()[:-1]
+ return self.index_pv()[:-1]
elif np.isnan(rho):
raise ValueError("rho needs to be a real number between 0. and 1.")
else:
@@ -419,7 +421,7 @@ class TrancheBasket(BasketIndex):
if complement:
return tranche_cl(L, R, cs, K, 1.), tranche_pl(L, cs, K, 1.)
else:
- return tranche_cl(L, R, self.cs, 0., K), tranche_pl(L, self.cs, 0., K)
+ return tranche_cl(L, R, cs, 0., K), tranche_pl(L, cs, 0., K)
def tranche_pvs(self, protection=False, complement=False, shortened=0):
""" computes coupon leg, protection leg and bond price.
@@ -516,6 +518,12 @@ class TrancheBasket(BasketIndex):
durations.name = 'duration'
return durations
+ def tranche_EL(self, complement=False):
+ pl = self.tranche_pvs(complement=complement).protection_leg
+ EL = pd.Series(-pl * np.diff(self.K), index=self._row_names)
+ EL.name = 'expected_loss'
+ return EL
+
def tranche_spreads(self, complement=False):
cl, pl, _ = self.tranche_pvs(complement=complement)
durations = (cl - self._accrued) / self.tranche_quotes.running.values
@@ -569,6 +577,15 @@ class TrancheBasket(BasketIndex):
return pd.DataFrame({'delta': deltas, 'gamma': gammas},
index=self._row_names)
+ def tranche_corr01(self, eps=0.01, complement=False):
+ bp = self.tranche_pvs(complement=complement).bond_price
+ rho_saved = self.rho
+ self.rho = np.power(self.rho, 1-eps)
+ corr01 = self.tranche_pvs(complement=complement).bond_price - bp
+ self.rho = rho_saved
+ return corr01
+
+
def build_skew(self, skew_type="bottomup"):
assert(skew_type == "bottomup" or skew_type == "topdown")
dK = np.diff(self.K)
@@ -601,7 +618,7 @@ class TrancheBasket(BasketIndex):
if r.converged:
self.rho[j+1] = x0
else:
- print(res.flag)
+ print(r.flag)
break
self._skew = CubicSpline(logit(self.K[1:-1]),
logit(self.rho[1:-1]), bc_type='natural')