aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/basket_index.py
blob: 2d84e5641255900536efdf324d0c4e52c069d264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from .index_data import get_index_quotes, get_singlenames_curves
from . import serenitas_engine
from .utils import tenor_t
from functools import partial
from pyisda.cdsone import upfront_charge, spread_from_upfront
from pyisda.credit_index import CreditIndex
from pyisda.date import previous_twentieth
from typing import List
from yieldcurve import get_curve
import datetime
import logging
import numpy as np
import pandas as pd
from math import exp
from scipy.optimize import brentq
from pandas.tseries.offsets import Day, BDay

logger = logging.getLogger(__name__)


def make_index(t, d, args):
    instance = t.__new__(t)
    CreditIndex.__init__(instance, *args)
    instance.__dict__.update(d)
    return instance


class BasketIndex(CreditIndex):
    index_type: str
    series: int
    recovery: float
    step_in_date: pd.Timestamp
    value_date: pd.Timestamp
    tweaks: List[float]

    def __init__(
        self,
        index_type: str,
        series: int,
        tenors: List[str],
        *,
        value_date: pd.Timestamp = pd.Timestamp.today().normalize() - BDay(),
    ):
        self.index_type = index_type
        self.series = series
        if index_type == "HY":
            self.recovery = 0.3
        else:
            self.recovery = 0.4
        self.index_desc = pd.read_sql_query(
            "SELECT tenor, maturity, coupon * 1e-4 AS coupon, "
            "issue_date "
            "FROM index_maturity "
            "WHERE index=%s AND series=%s",
            serenitas_engine,
            index_col="tenor",
            params=(index_type, series),
            parse_dates=["maturity", "issue_date"],
        )
        if self.index_desc.empty:
            raise ValueError(f"Index {index_type} {series} doesn't exist")
        self._index_version = tuple(
            tuple(r.values())
            for r in serenitas_engine.execute(
                "SELECT lastdate,"
                "       indexfactor/100 AS factor,"
                "       cumulativeloss/100 AS cum_loss,"
                "       version "
                "FROM index_version "
                "WHERE index = %s AND series = %s"
                "ORDER BY lastdate",
                (index_type, series),
            )
        )
        self._update_factor(value_date)
        self.issue_date = self.index_desc.issue_date[0]
        self.index_desc = self.index_desc.loc[tenors].sort_values("maturity")
        self.tenors = {t: m.date() for t, m in self.index_desc.maturity.items()}
        maturities = self.index_desc.maturity.dt.to_pydatetime()
        self.index_desc = self.index_desc.reset_index().set_index("maturity")
        self.index_desc.tenor = self.index_desc.tenor.astype(tenor_t)
        max_tenor = int(tenors[-1][:-2])
        self._curve_tenors = tuple(
            t for t in (0.5, 1, 2, 3, 4, 5, 7, 10) if t <= max_tenor
        )
        curves = get_singlenames_curves(
            index_type, series, value_date, self._curve_tenors
        )

        self.currency = "EUR" if index_type in ["XO", "EU"] else "USD"
        self.yc = get_curve(value_date, self.currency)
        self.step_in_date = value_date + Day()
        self.cash_settle_date = value_date + 3 * BDay()
        self.tweaks = []
        self.start_date = previous_twentieth(value_date)
        self._ignore_hash = set(
            [
                "_Z",
                "_w",
                "_skew",
                "tenors",
                "index_desc",
                "tweaks",
                "_Legs",
                "_ignore_hash",
            ]
        )
        super().__init__(self.issue_date, maturities, curves, value_date=value_date)

    def __reduce__(self):
        _, args = CreditIndex.__reduce__(self)
        d = vars(self)
        return partial(make_index, self.__class__), (d, args)

    def __hash__(self):
        def aux(v):
            if isinstance(v, list):
                return hash(tuple(v))
            elif type(v) is np.ndarray:
                return hash(v.tobytes())
            else:
                return hash(v)

        return hash(
            (CreditIndex.__hash__(self),)
            + tuple(aux(v) for k, v in vars(self).items() if k not in self._ignore_hash)
        )

    def _update_factor(self, d):
        if isinstance(d, datetime.datetime):
            d = d.date()
        for lastdate, *data in self._index_version:
            if lastdate >= d:
                self._factor, self._cumloss, self._version = data
                break

    @property
    def factor(self):
        return self._factor

    @property
    def cumloss(self):
        return self._cumloss

    @property
    def version(self):
        return self._version

    def _get_quotes(self, *args):
        """ allow to tweak based on manually inputed quotes"""
        if self.index_type == "HY":
            return {m: (100 - p) / 100 for m, p in zip(self.maturities, args[0])}
        else:
            return {
                m: self._snacpv(s * 1e-4, self.coupon(m), self.recovery, m)
                for m, s in zip(self.maturities, args[0])
            }

    value_date = property(CreditIndex.value_date.__get__)

    @value_date.setter
    def value_date(self, d: pd.Timestamp):
        self.curves = get_singlenames_curves(
            self.index_type, self.series, d, self._curve_tenors
        )
        self.yc = get_curve(d, self.currency)
        self.step_in_date = d + Day()
        self.cash_settle_date = d + 3 * BDay()
        self.start_date = previous_twentieth(d)  # or d + 1?
        self._update_factor(d)
        CreditIndex.value_date.__set__(self, d)

    @property
    def recovery_rates(self):
        # we don't always have the 6 months data point
        # so pick arbitrarily the 1 year point
        return np.array([c.recovery_rates[0] for _, c in self.curves])

    def spreads(self):
        return super().spreads(self.yc)

    def pv(self, maturity=None, epsilon=0.0, coupon=None):
        if maturity is None:
            r = []
            for m in self.maturities:
                coupon = self.index_desc.coupon[m]
                r.append(
                    super().pv(
                        self.step_in_date,
                        self.cash_settle_date,
                        m,
                        self.yc,
                        coupon,
                        epsilon,
                    )
                )
            return pd.Series(r, index=self.index_desc.tenor, name="pv")
        else:
            return super().pv(
                self.step_in_date,
                self.cash_settle_date,
                maturity,
                self.yc,
                coupon or self.coupon(maturity),
                epsilon,
            )

    def pv_vec(self):
        return (
            super().pv_vec(self.step_in_date, self.cash_settle_date, self.yc).unstack(0)
        )

    def coupon_leg(self, maturity=None):
        return self.index_desc.coupon.values * self.duration()

    def spread(self, maturity=None):
        return self.protection_leg(maturity) / self.duration(maturity) * 1e4

    def protection_leg(self, maturity=None):
        if maturity is None:
            r = []
            for m in self.maturities:
                r.append(
                    super().protection_leg(
                        self.step_in_date, self.cash_settle_date, m, self.yc
                    )
                )
            return pd.Series(r, index=self.index_desc.tenor, name="protection_leg")
        else:
            return super().protection_leg(
                self.step_in_date, self.cash_settle_date, maturity, self.yc
            )

    def duration(self, maturity=None):
        if maturity is None:
            r = []
            for m in self.maturities:
                r.append(
                    super().duration(
                        self.step_in_date, self.cash_settle_date, m, self.yc
                    )
                )
            return pd.Series(r, index=self.index_desc.tenor, name="duration")
        else:
            return super().duration(
                self.step_in_date, self.cash_settle_date, maturity, self.yc
            )

    def theta(self, maturity=None, coupon=None, theta_date=None):
        """ index thetas

        if maturity is None, returns a series of theta for all tenors.
        Otherwise computes the theta for that specific maturity (which needs
        not be an existing tenor)

        if theta_date is provided, computes the theta to that specific date
        instead of one-year theta"""
        try:
            index_quotes = self._get_quotes()
        except ValueError:
            index_quotes = {}
        if maturity is None:
            r = []
            for m in self.maturities:
                coupon = self.index_desc.coupon[m]
                index_quote = index_quotes.get(m, np.nan)
                r.append(
                    super().theta(
                        self.step_in_date,
                        self.cash_settle_date,
                        m,
                        self.yc,
                        coupon,
                        index_quote,
                        theta_date,
                    )
                )
            return pd.Series(r, index=self.index_desc.tenor, name="theta")
        else:
            return super().theta(
                self.step_in_date,
                self.cash_settle_date,
                maturity,
                self.yc,
                coupon or self.coupon(maturity),
                np.nan,
                theta_date,
            )

    def coupon(self, maturity=None, assume_flat=True):
        if maturity is None:
            return self.index_desc.set_index("tenor").coupon
        else:
            try:
                return self.index_desc.coupon[maturity]
            except KeyError:
                if assume_flat:
                    return self.index_desc.coupon.iat[0]
                else:
                    raise ValueError("Non standard maturity: coupon must be provided")

    def tweak(self, *args):
        """ tweak the singlename curves to match index quotes"""
        quotes = self._get_quotes(*args)
        self.tweaks = []

        for m in self.maturities:
            if m not in quotes:
                self.tweaks.append(np.nan)
                continue
            else:
                index_quote = quotes[m]
                if abs(self.pv(m) - index_quote) < 1e-12:  # early exit
                    self.tweaks.append(0.0)
                    continue
            lo, hi = -0.3, 0.3
            hi_tilde = exp(hi) - 1
            while hi_tilde < 5:
                # map range to (-1, +inf)
                lo_tilde = exp(lo) - 1
                hi_tilde = exp(hi) - 1
                try:
                    eps = brentq(
                        lambda epsilon: self.pv(m, epsilon) - index_quote,
                        lo_tilde,
                        hi_tilde,
                    )
                except ValueError:
                    lo *= 1.1
                    hi *= 1.1
                else:
                    break
            else:
                logger.warning(
                    f"couldn't calibrate for date: {self.value_date} and maturity: {m}"
                )
                self.tweaks.append(np.NaN)
                continue
            self.tweaks.append(eps)
            self.tweak_portfolio(eps, m)

    def _snacpv(self, spread, coupon, recov, maturity):
        return upfront_charge(
            self.value_date,
            self.cash_settle_date,
            self.start_date,
            self.step_in_date,
            self.start_date,
            maturity,
            coupon,
            self.yc,
            spread,
            recov,
        )

    def _snacspread(self, coupon, recov, maturity):
        return spread_from_upfront(
            self.value_date,
            self.cash_settle_date,
            self.start_date,
            self.step_in_date,
            self.start_date,
            maturity,
            coupon,
            self.yc,
            self.pv(maturity),
            recov,
        )


class MarkitBasketIndex(BasketIndex):
    def __init__(
        self,
        index_type: str,
        series: int,
        tenors: List[str],
        *,
        value_date: pd.Timestamp = pd.Timestamp.today().normalize() - BDay(),
    ):
        super().__init__(index_type, series, tenors, value_date=value_date)
        self.index_quotes = (
            get_index_quotes(
                index_type, series, tenors, years=None, remove_holidays=False
            )[["close_price", "id"]]
            .groupby(level=["date", "tenor"], as_index=True)
            .nth(0)
        )
        self.index_quotes.close_price = 1 - self.index_quotes.close_price / 100

    def _get_quotes(self):
        quotes = self.index_quotes.loc[self.value_date, "close_price"]
        return {self.tenors[t]: q for t, q in quotes.items()}


if __name__ == "__main__":
    ig28 = BasketIndex("IG", 28, ["3yr", "5yr", "7yr", "10yr"])
    from quantlib.time.api import Schedule, Rule, Date, Period, WeekendsOnly
    from quantlib.settings import Settings

    settings = Settings()

    cds_schedule = Schedule.from_rule(
        settings.evaluation_date,
        Date.from_datetime(ig28.maturities[-1]),
        Period("3M"),
        WeekendsOnly(),
        date_generation_rule=Rule.CDS2015,
    )

    sp = ig28.survival_matrix()