diff options
Diffstat (limited to 'python/analytics')
| -rw-r--r-- | python/analytics/cms_spread.py | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/python/analytics/cms_spread.py b/python/analytics/cms_spread.py index da1672f1..7d34b7b1 100644 --- a/python/analytics/cms_spread.py +++ b/python/analytics/cms_spread.py @@ -1,15 +1,10 @@ -import datetime import numpy as np import matplotlib.pyplot as plt -from mpl_toolkits.mplot3d import Axes3D from .tranche_functions import GHquad from math import exp, sqrt, log from .black import bachelier -from bbg_helpers import BBG_IP, retrieve_data, init_bbg_session from quantlib.time.api import ( - Date, Period, Days, Months, Years, UnitedStates, Actual365Fixed, today, - Following) -from quantlib.time.calendars.united_states import GovernmentBond + Date, Period, Days, Months, Years, UnitedStates, Actual365Fixed, Following) from quantlib.termstructures.yields.api import YieldTermStructure from quantlib.indexes.swap.usd_libor_swap import UsdLiborSwapIsdaFixAm from quantlib.experimental.coupons.swap_spread_index import SwapSpreadIndex @@ -19,20 +14,25 @@ from quantlib.experimental.coupons.cms_spread_coupon import \ CappedFlooredCmsSpreadCoupon from quantlib.termstructures.volatility.api import ( VolatilityType, SwaptionVolatilityMatrix) -from quantlib.cashflows.conundrum_pricer import ( - YieldCurveModel, NumericHaganPricer, AnalyticHaganPricer) from quantlib.cashflows.linear_tsr_pricer import LinearTsrPricer from quantlib.quotes import SimpleQuote from quantlib.math.matrix import Matrix from scipy.interpolate import RectBivariateSpline -from yieldcurve import YC from db import dbconn + def CMS_spread(T_alpha, X, beta, gamma): Z, w = GHquad(100) return np.inner(f(Z), w) + +def h(v, X, S_alpha_beta, mu_beta, sigma_alpha_beta, T_alpha): + r = (mu_beta - 0.5 * sigma_alpha_beta * sigma_alpha_beta) * T_alpha + \ + sigma_alpha_beta * sqrt(T_alpha) * v + return X + S_alpha_beta * exp(r) + + def f(v, X, S_alpha_beta, S_alpha_gamma, mu_beta, mu_gamma, T_alpha, rho): h = h(v, X, S_alpha_beta, mu_beta, sigma_alpha_beta, T_alpha) u = rho * sigma_alpha_gamma * sqrt(T_alpha) * v @@ -44,11 +44,6 @@ def f(v, X, S_alpha_beta, S_alpha_gamma, mu_beta, mu_gamma, T_alpha, rho): return 0.5 * (S_alpha_gamma * exp(r) * cnd_erf(u1 / d) - h * cnd_erf(u2 / d)) -def h(v, X, S_alpha_beta, mu_beta, sigma_alpha_beta, T_alpha): - r = (mu_beta - 0.5 * sigma_alpha_beta * sigma_alpha_beta) * T_alpha + \ - sigma_alpha_beta * sqrt(T_alpha) * v - return X + S_alpha_beta * exp(r) - def get_fixings(conn, tenor1, tenor2, fixing_date=None): if fixing_date: sql_str = f'SELECT "{tenor1}y" ,"{tenor2}y" FROM USD_swap_fixings ' \ @@ -71,6 +66,7 @@ def get_fixings(conn, tenor1, tenor2, fixing_date=None): fixing2 = float(fixing2) return date, fixing1, fixing2 + def build_spread_index(tenor1, tenor2): yc = YieldTermStructure() USISDA1 = UsdLiborSwapIsdaFixAm(Period(tenor1, Years), yc) @@ -78,6 +74,7 @@ def build_spread_index(tenor1, tenor2): spread_index = SwapSpreadIndex(f"{tenor1}-{tenor2}", USISDA1, USISDA2) return spread_index, yc + def get_swaption_vol_data(source="ICPL", vol_type=VolatilityType.ShiftedLognormal, date=None): if vol_type == VolatilityType.Normal: @@ -97,11 +94,13 @@ def get_swaption_vol_data(source="ICPL", vol_type=VolatilityType.ShiftedLognorma surf_data = next(c) return surf_data[0], np.array(surf_data[1:-1], order='F').T + def get_swaption_vol_surface(date, vol_type): date, surf, _ = get_swaption_vol_data(date=date, vol_type=vol_type) tenors = [1/12, 0.25, 0.5, 0.75] + list(range(1, 11)) + [15., 20., 25., 30.] return RectBivariateSpline(tenors, tenors[-14:], surf) + def get_swaption_vol_matrix(date, data, vol_type=VolatilityType.ShiftedLognormal): # figure out what to do with nan calendar = UnitedStates() @@ -121,6 +120,7 @@ def get_swaption_vol_matrix(date, data, vol_type=VolatilityType.ShiftedLognormal Actual365Fixed(), vol_type=vol_type)) + def quantlib_model(date, spread_index, yc, cap, rho, maturity, mean_rev=0., vol_type=VolatilityType.ShiftedLognormal): date, surf = get_swaption_vol_data(date=date, vol_type=vol_type) @@ -143,12 +143,14 @@ def quantlib_model(date, spread_index, yc, cap, rho, maturity, mean_rev=0., cms_spread_coupon.set_pricer(cmsspread_pricer) return cms_spread_coupon + def plot_surf(surf, tenors): xx, yy = np.meshgrid(tenors, tenors[-14:]) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(xx, yy, surf.ev(xx, yy)) + def globeop_model(date, spread_index, yc, strike, rho, maturity, vol_type=VolatilityType.Normal): """ price cap spread option without convexity adjustment |
