diff options
Diffstat (limited to 'python/analytics/tranche_basket.py')
| -rw-r--r-- | python/analytics/tranche_basket.py | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py index 475f9ebb..e4fed1f3 100644 --- a/python/analytics/tranche_basket.py +++ b/python/analytics/tranche_basket.py @@ -36,7 +36,7 @@ class Skew(): yield self.skew_fun def __call__(self, k): - return expit(self.skew_fun(logit(k))) + return expit(self.skew_fun(np.log(k))) @classmethod def from_desc(cls, index_type: str, series: int, tenor: str, *, @@ -76,7 +76,7 @@ class Skew(): K.append(100) K = np.array(K) / 100 K = adjust_attachments(K, cumloss/100, factor/100) - skew_fun = CubicSpline(logit(K[1:-1]), logit(rho), bc_type='natural') + skew_fun = CubicSpline(np.log(K[1:-1]/el), logit(rho), bc_type='natural') s = Skew(el, skew_fun) Skew._cache[key] = s return s @@ -485,7 +485,7 @@ class DualCorrTranche(): if x == 0. or x == 1.: newrho = x else: - newrho = skew(x) + newrho = skew(x / el) return self.expected_loss_trunc(x, rho=newrho) / el - \ self.expected_loss_trunc(K2, newrho, shortened) / el2 @@ -506,16 +506,16 @@ class DualCorrTranche(): rho_orig = self.rho el2 = self.expected_loss(shortened=4) if method == "ATM": - Keq = np.clip(el/el2 * self.K, None, .999) + moneyness_eq = self.K / el2 elif method == "TLP": - Keq = [] + moneyness_eq = [] for k in self.K: if k == 0. or k == 1.: - Keq.append(k) + moneyness_eq.append(k/el) else: kbound = find_upper_bound(k, 4) - Keq.append(brentq(aux, 0., kbound, (k, 4))) - self.rho = skew(Keq) + moneyness_eq.append(brentq(aux, 0., kbound, (k, 4))/el) + self.rho = skew(moneyness_eq) self.maturity += relativedelta(years=-1) r = self.pv - pv_orig self.rho = rho_orig @@ -721,8 +721,8 @@ class TrancheBasket(BasketIndex): self._cumloss = orig_cumloss + L self._factor = orig_factor * (1 - weight) self.K = adjust_attachments(self.K_orig, self.cumloss, self.factor) - Korig_eq = np.clip(el_orig / self.expected_loss() * self.K[1:-1], None, .999) - self.rho = np.hstack([np.nan, expit(self._skew(logit(Korig_eq))), np.nan]) + Korig_eq = self.K[1:1] / self.expected_loss() + self.rho = np.hstack([np.nan, expit(self._skew(np.log(Korig_eq))), np.nan]) upfs = self.tranche_factors() * self.tranche_pvs(protection=True).bond_price #we allocate the loss to the different tranches loss = np.diff([0, *(min(k, L) for k in self.K[1:])]) @@ -797,7 +797,7 @@ class TrancheBasket(BasketIndex): def expected_loss_trunc(self, K, rho=None, shortened=0): if rho is None: - rho = expit(self._skew(logit(K))) + rho = expit(self._skew(log(K/self.expected_loss()))) if shortened > 0: DP = self.default_prob.values[:, :-shortened] df = self.cs.df.values[:-shortened] @@ -814,7 +814,7 @@ class TrancheBasket(BasketIndex): def probability_trunc(self, K, rho=None, shortened=0): if rho is None: - rho = expit(self._skew(logit(K))) + rho = expit(self._skew(log(K/self.expected_loss()))) L, _ = BCloss_recov_dist(self.default_prob.values[:,-(1+shortened),np.newaxis], self.weights, self.recovery_rates, @@ -936,7 +936,7 @@ class TrancheBasket(BasketIndex): else: print(r.flag) break - self._skew = CubicSpline(logit(self.K[1:-1]), + self._skew = CubicSpline(np.log(self.K[1:-1] / self.expected_loss()), logit(self.rho[1:-1]), bc_type='natural') def map_skew(self, index2, method="ATM", shortened=0): @@ -944,13 +944,13 @@ class TrancheBasket(BasketIndex): if x == 0. or x == 1.: newrho = x else: - newrho = expit(index1._skew(logit(x))) + newrho = expit(index1._skew(log(x / el1))) assert newrho >= 0. and newrho <= 1., f"Something went wrong x: {x}, rho: {newrho}" return self.expected_loss_trunc(x, rho=newrho) / el1 - \ index2.expected_loss_trunc(K2, newrho, shortened) / el2 def aux2(x, index1, index2, K2, shortened): - newrho = expit(index1._skew(logit(x))) + newrho = expit(index1._skew(log(x))) assert newrho >= 0 and newrho <= 1, f"Something went wrong x: {x}, rho: {newrho}" return np.log(self.probability_trunc(x, newrho)) - \ np.log(index2.probability_trunc(K2, newrho, shortened)) @@ -971,18 +971,18 @@ class TrancheBasket(BasketIndex): el2 = index2.expected_loss(shortened=shortened) if method == "ATM": - K1eq = np.clip(el1 / el2 * index2.K[1:-1], None, .999) + moneyness1_eq = index2.K[1:-1] / el2 elif method == "TLP": - K1eq = [] + moneyness1_eq = [] for K2 in index2.K[1:-1]: b = find_upper_bound(self, el1, index2, el2, K2, shortened) - K1eq.append(brentq(aux, 0., b, - (self, el1, index2, el2, K2, shortened))) + moneyness1_eq.append(brentq(aux, 0., b, + (self, el1, index2, el2, K2, shortened)) / el1) K1eq = np.array(K1eq) elif method == "PM": - K1eq = [] + moneyness1_eq = [] for K2 in index2.K[1:-1]: # need to figure out a better way of setting the bounds - K1eq.append(brentq(aux2, K2 * 0.1, K2 * 2.5, - (self, index2, K2, shortened))) - return np.hstack([np.nan, expit(self._skew(logit(K1eq))), np.nan]) + moneyness1_eq.append(brentq(aux2, K2 * 0.1/el1, K2 * 2.5/el1, + (self, index2, K2, shortened))) + return np.hstack([np.nan, expit(self._skew(log(moneyness1_eq))), np.nan]) |
