aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/tranche_basket.py47
1 files changed, 38 insertions, 9 deletions
diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py
index 6681b53f..c97598d0 100644
--- a/python/analytics/tranche_basket.py
+++ b/python/analytics/tranche_basket.py
@@ -5,6 +5,7 @@ from .tranche_functions import (
from .index_data import get_tranche_quotes
from cityhash import CityHash64
from collections import namedtuple
+from db import dbconn
from copy import deepcopy
from lru import LRU
from pyisda.date import cds_accrued
@@ -15,7 +16,10 @@ import datetime
import pandas as pd
import numpy as np
+
_cache = LRU(64)
+
+
def BCloss_recov_dist_cached(default_prob,
weights,
recovery_rates,
@@ -33,6 +37,8 @@ def BCloss_recov_dist_cached(default_prob,
Z, w, Ngrid)
return _cache[h]
+
+
class DualCorrTranche(BasketIndex):
def __init__(self, index_type: str, series: int, tenor: str, *,
attach: float, detach: float, corr_attach: float,
@@ -53,19 +59,39 @@ class DualCorrTranche(BasketIndex):
self._direction = -1. if notional > 0 else 1.
self.start_date, self.cs = credit_schedule(value_date, self.tenor[:-1],
1., self.yc)
- self.default_prob, _ = super().survival_matrix(self.cs.index.values.astype('M8[D]').
- view('int') + 134774)
+ self.default_prob = 1 - super().survival_matrix(self.cs.index.values.astype('M8[D]').
+ view('int') + 134774)[0]
self._accrued = cds_accrued(value_date, self.tranche_running * 1e-4)
value_date = property(BasketIndex.value_date.__get__)
+ @classmethod
+ def from_tradeid(cls, trade_id):
+ with dbconn('dawndb') as conn:
+ with conn.cursor() as c:
+ c.execute("SELECT * FROM cds LEFT JOIN index_desc "
+ "ON security_id = redindexcode AND "
+ "cds.maturity = index_desc.maturity "
+ "WHERE id=%s", (trade_id,))
+ rec = c.fetchone()
+ instance = cls(rec['index'], rec['series'], rec['tenor'],
+ attach=rec['attach'],
+ detach=rec['detach'],
+ corr_attach=np.nan,
+ corr_detach=np.nan,
+ notional=rec['notional'],
+ tranche_running=rec['coupon'],
+ value_date=rec['trade_date'])
+ instance.direction = rec['protection']
+ return instance
+
@value_date.setter
def value_date(self, d: pd.Timestamp):
BasketIndex.value_date.__set__(self, d)
self.start_date, self.cs = credit_schedule(d, self.tenor[:-1],
1., self.yc)
- self.default_prob, _ = super().survival_matrix(self.cs.index.values.astype('M8[D]').
- view('int') + 134774)
+ self.default_prob= 1 - super().survival_matrix(self.cs.index.values.astype('M8[D]').
+ view('int') + 134774)[0]
self._accrued = cds_accrued(d, self.tranche_running * 1e-4)
def tranche_legs(self, K, rho):
@@ -136,6 +162,8 @@ class DualCorrTranche(BasketIndex):
def shock(self, params=['pnl'], *, corr_shock, kwargs):
pass
+
+
class TrancheBasket(BasketIndex):
def __init__(self, index_type: str, series: int, tenor: str, *,
value_date: pd.Timestamp=pd.Timestamp.today().normalize()):
@@ -226,7 +254,8 @@ class TrancheBasket(BasketIndex):
@property
def default_prob(self):
- sm, tickers = super().survival_matrix(self.cs.index.values.astype('M8[D]').view('int') + 134774)
+ sm, tickers = super().survival_matrix(self.cs.index.values.
+ astype('M8[D]').view('int') + 134774)
return pd.DataFrame(1 - sm, index=tickers, columns=self.cs.index)
def tranche_legs(self, K, rho, complement=False, shortened=0):
@@ -238,7 +267,7 @@ class TrancheBasket(BasketIndex):
raise ValueError("rho needs to be a real number between 0. and 1.")
else:
if shortened > 0:
- default_prob = self.default_prob.values[:,:-shortened]
+ default_prob = self.default_prob.values[:, :-shortened]
cs = self.cs[:-shortened]
else:
default_prob = self.default_prob.values
@@ -302,7 +331,7 @@ class TrancheBasket(BasketIndex):
def expected_loss(self, discounted=True, shortened=0):
if shortened > 0:
- DP = self.default_prob.values[:,:-shortened]
+ DP = self.default_prob.values[:, :-shortened]
df = self.cs.df.values[:-shortened]
else:
DP = self.default_prob.values
@@ -318,7 +347,7 @@ class TrancheBasket(BasketIndex):
if rho is None:
rho = expit(self._skew(logit(K)))
if shortened > 0:
- DP = self.default_prob.values[:,:-shortened]
+ DP = self.default_prob.values[:, :-shortened]
df = self.cs.df.values[:-shortened]
else:
DP = self.default_prob.values
@@ -453,7 +482,7 @@ class TrancheBasket(BasketIndex):
def aux2(x, index1, index2, K2, shortened):
newrho = expit(index1._skew(logit(x)))
- assert newrho >= 0 and newrho <=1, "Something went wrong"
+ assert newrho >= 0 and newrho <= 1, "Something went wrong"
return np.log(self.probability_trunc(x, newrho)) - \
np.log(index2.probability_trunc(K2, newrho, shortened))