aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/scenarios.py
blob: 45db50965b4d2dca1271cbad02382aac478468c4 (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
from analytics import ATMstrike
import pandas as pd

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

    Parameters
    ----------
    swaption : Swaption
    date_range : `pandas.Datetime.Index`
    spread_shock : `np.array`
    vol_shock : `np.array`
    vol_surface
    """
    r = []
    spread_start = swaption.index.spread
    for date in date_range:
        swaption.index.trade_date = date.date()
        T = swaption.T
        for ss in spread_shock:
            spread = spread_start * (1 + ss)
            swaption.index.ref = spread
            swaption._update()
            atm_strike = ATMstrike(swaption.index, swaption.exercise_date)
            moneyness = (swaption.index.spread / atm_strike)
            curr_vol = float(vol_surface.ev(T, moneyness))
            for vs in vol_shock:
                vol = curr_vol * (1 + vs)
                swaption.sigma = vol
                r.append([date, ss, vs, swaption.pv_black])
    swaption.index.spread = spread_start
    df = pd.DataFrame.from_records(r, columns=['date', 'spread_shock',
                                               'vol_shock', 'pv'])
    return df.set_index('date')