aboutsummaryrefslogtreecommitdiffstats
path: root/python/calibrate_tranches_BC.py
blob: 7b55ae38114d574e300dbc927076ed6952ddbf4b (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
from db import dbconn, dbengine
from analytics import TrancheBasket
import datetime
import pandas as pd
from yaml import load

start_dates = {'ig19': datetime.date(2013, 5, 1),
               'ig21': datetime.date(2013, 9, 26),
               'ig23': datetime.date(2014, 10, 14),
               'ig25': datetime.date(2015, 9, 22),
               'ig27': datetime.date(2016, 9, 27),
               'ig29': datetime.date(2017, 9, 26),
               'ig31': datetime.date(2018, 9, 25)}

serenitas_engine = dbengine('serenitasdb')

with open("/home/guillaume/projects/code/etc/runs.yml") as fh:
    runs = load(fh)['runs']
    for index, tenor in runs:
        print(index, tenor)
        if index not in start_dates:
            continue
        else:
            begin_date = start_dates[index]
        index, series = index[:2].upper(), int(index[2:])
        tranche_index = TrancheBasket(index, series, tenor)
        dr = pd.bdate_range(begin_date, "2018-10-10")
        for d in dr:
            print(d)
            try:
                tranche_index.value_date = d
            except ValueError as e:
                print(e)
                continue
            tranche_index.tweak()
            tranche_index.build_skew()
            df = pd.concat([tranche_index.tranche_deltas(),
                            tranche_index.tranche_thetas(),
                            tranche_index.tranche_fwd_deltas(),
                            tranche_index.tranche_durations(),
                            tranche_index.tranche_EL(),
                            tranche_index.tranche_spreads()], axis=1)
            df['index_duration'], df['index_expected_loss'], df['index_price'] = tranche_index.index_pv()
            df['index_expected_loss'] *= -1
            df['index_duration'] -= tranche_index.accrued()
            df['index_basis'] = tranche_index.tweaks[0]
            df['index_theta'] = tranche_index.theta()[tenor]
            df['tranche_id'] = tranche_index.tranche_quotes.id.values
            df['corr_at_detach'] = tranche_index.rho[1:]
            df['corr01'] = tranche_index.tranche_corr01()
            del df['fwd_gamma']
            df.to_sql("tranche_risk", serenitas_engine, if_exists='append', index=False)