diff options
Diffstat (limited to 'python/tests/test_cms_spread.py')
| -rw-r--r-- | python/tests/test_cms_spread.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/python/tests/test_cms_spread.py b/python/tests/test_cms_spread.py index b45acce6..edbbd6ae 100644 --- a/python/tests/test_cms_spread.py +++ b/python/tests/test_cms_spread.py @@ -5,7 +5,7 @@ import unittest from analytics.cms_spread import ( build_spread_index, VolatilityType, get_swaption_vol_data, get_swaption_vol_matrix, get_cms_coupons, - get_params, h1, h_call, h_put) + get_params, _call_integrand, h_call, h_put, CmsSpread) from quantlib.quotes import SimpleQuote from quantlib.time.api import (Actual365Fixed, Days, Date, ModifiedFollowing, Period, Years) @@ -58,7 +58,7 @@ class TestCmsSpread(unittest.TestCase): option_tenor, spread_index) evaluation_date = datetime.date(2018, 8, 23) - self.yc.link_to(YC(evaluation_date=evaluation_date)) + self.yc.link_to(YC(evaluation_date=evaluation_date, extrapolation=True)) self.yc.extrapolation = True date, surf = get_swaption_vol_data(date=evaluation_date, vol_type=VolatilityType.ShiftedLognormal) @@ -69,6 +69,11 @@ class TestCmsSpread(unittest.TestCase): self.cms2y.set_pricer(self.cms_pricer) self.cms30y.set_pricer(self.cms_pricer) self.params = get_params(self.cms2y, self.cms30y, atm_vol) + cms_spread_pricer = LognormalCmsSpreadPricer( + self.cms_pricer, + self.ρ, + integration_points=20) + self.cms30y2y_cap.set_pricer(cms_spread_pricer) def test_black_model(self): @@ -77,11 +82,6 @@ class TestCmsSpread(unittest.TestCase): h_call(x, self.cap, *self.params, self.ρ.value)) val_put = 1 / math.sqrt(2 * math.pi) * np.dot(w, h_put(x, self.cap, *self.params, self.ρ.value)) - cms_spread_pricer = LognormalCmsSpreadPricer( - self.cms_pricer, - self.ρ, - integration_points=20) - self.cms30y2y_cap.set_pricer(cms_spread_pricer) self.assertAlmostEqual(self.cms30y2y_cap.rate, val_call) self.assertAlmostEqual(-self.cms30y2y_cap.underlying.rate, val_put - val_call) @@ -90,7 +90,7 @@ class TestCmsSpread(unittest.TestCase): def test_h1_hcall(self): args = (self.cap, *self.params, self.ρ.value) - h1_fun = LowLevelCallable(h1.ctypes).function + h1_fun = _call_integrand.function for x in np.linspace(-5, 5, 11): full_args = np.array((x, *args)) @@ -101,11 +101,18 @@ class TestCmsSpread(unittest.TestCase): def test_scipy_integrate(self): x, w = roots_hermitenorm(20) val_call = np.dot(w, h_call(x, self.cap, *self.params, self.ρ.value)) - integrand = LowLevelCallable(h1.ctypes) args = (self.cap, *self.params, self.ρ.value) - val, _ = quad(integrand, -np.inf, np.inf, args=(self.cap, *self.params, self.ρ.value)) + val, _ = quad(_call_integrand, -np.inf, np.inf, args=(self.cap, *self.params, self.ρ.value)) self.assertAlmostEqual(val, val_call) + def test_CmsSpread(self): + trade = CmsSpread(None, 2, 30, 0.0075835, Period(2, Years), value_date=self.trade_date) + trade1 = CmsSpread.from_tradeid(1) + trade.value_date = datetime.date(2018, 2, 12) + trade1.value_date = datetime.date(2018, 2, 12) + self.assertAlmostEqual(self.cms30y2y_cap.rate * 1e8 * self.yc.discount(self.cms2y.fixing_date), + trade.pv) + if __name__ == "__main__": unittest.main() |
