diff options
Diffstat (limited to 'python/analytics/tranche_basket.py')
| -rw-r--r-- | python/analytics/tranche_basket.py | 72 |
1 files changed, 29 insertions, 43 deletions
diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py index a356cd9e..8820e83a 100644 --- a/python/analytics/tranche_basket.py +++ b/python/analytics/tranche_basket.py @@ -1106,56 +1106,42 @@ class TrancheBasket(BasketIndex): return pl + cl * spread + quote if skew_type == "bottomup": - for j in range(len(dK) - 1): - cl, pl = self.tranche_legs(self.K[j], self.rho[j]) - q = ( - self.tranche_quotes.quotes.iat[j] * dK[j] - - pl - - cl * self.tranche_quotes.running.iat[j] - ) - try: - x0, r = brentq( - aux, - 0.0, - 1.0, - args=( - self, - self.K[j + 1], - q, - self.tranche_quotes.running.iat[j], - False, - ), - full_output=True, - ) - except ValueError as e: - raise ValueError(f"can't calibrate skew at attach {self.K[j+1]}") - if r.converged: - self.rho[j + 1] = x0 - else: - print(r.flag) - break + r = range(0, len(dK) - 1) elif skew_type == "topdown": - for j in range(len(dK) - 1, 0, -1): - cl, pl = self.tranche_legs( - self.K[j + 1], self.rho[j + 1], complement=True - ) - q = ( - self.tranche_quotes.quotes.iat[j] * dK[j] - - pl - - cl * self.tranche_quotes.running.iat[j] - ) + r = range(-1, -len(dK), -1) + skew_is_topdown = skew_type == "topdown" + for j in r: + cl, pl = self.tranche_legs( + self.K[j], self.rho[j], complement=skew_is_topdown + ) + q = ( + self.tranche_quotes.quotes.iat[j] * dK[j] + - pl + - cl * self.tranche_quotes.running.iat[j] + ) + nextj = j - 1 if skew_is_topdown else j + 1 + try: x0, r = brentq( aux, 0.0, 1.0, - args=(self, self.K[j], q, self.tranche_quotes.running.iat[j], True), + args=( + self, + self.K[nextj], + q, + self.tranche_quotes.running.iat[j], + skew_is_topdown, + ), full_output=True, ) - if r.converged: - self.rho[j] = x0 - else: - print(r.flag) - break + except ValueError as e: + raise ValueError(f"can't calibrate skew at attach {self.K[nextj]}") + if r.converged: + self.rho[nextj] = x0 + else: + print(r.flag) + break + self._skew = CubicSpline( np.log(self.K[1:-1] / self.expected_loss()), logit(self.rho[1:-1]), |
