aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/scenarios.py
blob: fe921e4cac0fe62220b73a5b016fd258768b427b (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
from analytics import ATMstrike
from joblib import delayed, Parallel
import pandas as pd
from copy import deepcopy

def run_swaption_scenarios(swaption, date_range, spread_shock, vol_shock,
                           vol_surface, params=["pv"]):
    """computes the pv of a swaption for a range of scenarios

    Parameters
    ----------
    swaption_copy : Swaption
    date_range : `pandas.Datetime.Index`
    spread_shock : `np.array`
    vol_shock : `np.array`
    vol_surface
    params : list of strings
       list of attributes to call on the swaption object.
    """
    r = []
    swaption = deepcopy(swaption)
    spreads = swaption.ref * (1 + spread_shock)

    for date in date_range:
        swaption.index.trade_date = date.date()
        T = swaption.T
        for s in spreads:
            swaption.ref = s
            curr_vol = float(vol_surface(T, swaption.moneyness))
            for vs in vol_shock:
                swaption.sigma = curr_vol * (1 + vs)
                r.append([date, s, vs] + [getattr(swaption, p) for p in params])
    df = pd.DataFrame.from_records(r, columns=['date', 'spread_shock', 'vol_shock'] + params)
    return df.set_index('date')


def run_index_scenarios(index, date_range, spread_shock):
    r = []
    index = deepcopy(index)
    starting_pv = index.clean_pv
    starting_date = index.trade_date
    spreads = index.spread * (1 + spread_shock)

    for date in date_range:
        index.trade_date = date.date()
        for s in spread:
            index.spread = s
            r.append([date, s, index.pnl])
    df = pd.DataFrame.from_records(r, columns=['date', 'spread', 'pnl'])
    return df.set_index('date')