aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/test_scenarios.py
blob: b7f5f9f0aebb7287099d25eba99919ae65dbd6b6 (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
import unittest
import datetime
import numpy as np
import pandas as pd

from analytics import Index, BlackSwaption, Portfolio, VolatilitySurface
from pandas.tseries.offsets import BDay
from analytics.scenarios import run_portfolio_scenarios, run_swaption_scenarios, run_index_scenarios

class TestSenarios(unittest.TestCase):
    option_delta = Index.from_tradeid(874)
    option1 = BlackSwaption.from_tradeid(7, option_delta)
    option2 = BlackSwaption.from_tradeid(8, option_delta)
    portf = Portfolio([option1, option2, option_delta])
    date_range = pd.bdate_range(option_delta.trade_date, pd.Timestamp('2017-05-17') - BDay(), freq = '5B')

    def test_portfolio(self):
        """ check that run_portfolio_scenarios match the sum of the individual pieces"""
        vol_shock = np.arange(-0.15, 0.3, 0.01)
        spread_shock = np.arange(-0.2, 0.3, 0.01)
        vs = VolatilitySurface("IG", 28, trade_date=self.option_delta.trade_date)
        vol_surface = vs[vs.list(model="black", source="BAML")[-1]]
        df = run_portfolio_scenarios(self.portf, self.date_range,
                                     spread_shock, vol_shock, vol_surface)
        df = df.set_index(['spread', 'vol_shock'], append=True)

        df1 = run_swaption_scenarios(self.option1, self.date_range,
                                     spread_shock, vol_shock, vol_surface, ["pnl"])
        df2 = run_swaption_scenarios(self.option2, self.date_range,
                                     spread_shock, vol_shock, vol_surface, ["pnl"])
        df_index = run_index_scenarios(self.option_delta, self.date_range, spread_shock)
        df1 = df1.set_index(['spread', 'vol_shock'], append=True)
        df2 = df2.set_index(['spread', 'vol_shock'], append=True)
        df_index = df_index.set_index(['spread'], append=True)
        df_swaptions = df1 + df2
        df_swaptions = df_swaptions.reset_index(level='vol_shock')
        df_orig = df_index.add(df_swaptions, fill_value=0)
        df_orig = df_orig.set_index('vol_shock', append=True)
        self.assertFalse(np.any((df-df_orig).values))

if __name__=="__main__":
    unittest.main()