import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__),'QuantLibWrapper'))

import pandas
import numpy as np
from scipy.optimize import least_squares

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D  # for 3d plotting
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

from QuantLibWrapper import YieldCurve, HullWhiteModel, createSwaption


flatCurve = YieldCurve(['70y'],[0.03])
discCurve = flatCurve
projCurve = flatCurve

atmVols = pandas.read_csv('swaptionATMVols.csv', sep=';', index_col=0 )

meanReversion    = 0.1
volatilityTimes  = np.array([ 2.0,   5.0,   10.0,   20.0   ])
volatilityValues = np.array([ 0.005, 0.005,  0.005,  0.005 ])
hwModel          = HullWhiteModel(discCurve,meanReversion,volatilityTimes,volatilityValues)

# calibration targets
expiries  = np.array([ 2, 5, 10, 20 ])
swapterms = np.array([ 2, 5, 10, 20 ])
marketVols = np.zeros([len(expiries),len(swapterms)])
swaptions = []
Пример #2
0
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), 'QuantLibWrapper'))

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter

import pandas

import QuantLib as ql
import QuantLibWrapper.YieldCurve as yc

from QuantLibWrapper import MCSimulation, Payoffs, HullWhiteModel, HullWhiteModelWithDiscreteNumeraire
# yield curves

flatCurve = yc.YieldCurve(['30y'], [0.03])

terms = [
    '1y', '2y', '3y', '4y', '5y', '6y', '7y', '8y', '9y', '10y', '12y', '15y',
    '20y', '25y', '30y'
]
rates = [
    2.70e-2, 2.75e-2, 2.80e-2, 3.00e-2, 3.36e-2, 3.68e-2, 3.97e-2, 4.24e-2,
    4.50e-2, 4.75e-2, 4.75e-2, 4.70e-2, 4.50e-2, 4.30e-2, 4.30e-2
]
fwdRateYC = yc.YieldCurve(terms, rates)

# Hull-White model

meanReversion = 0.05
volatilityTimes = np.array([1.0, 2.0, 5.0, 10.0])
    3.36e-2,
    3.68e-2,
    3.97e-2,
    4.24e-2,
    4.50e-2,
    4.75e-2,
    4.75e-2,
    4.70e-2,
    4.50e-2,
    4.30e-2,
    4.30e-2,
    4.30e-2,
]
rates = [0.025 for r in rates]
rates2 = [r + 0.005 for r in rates]
discCurve = YieldCurve(terms, rates)
projCurve = YieldCurve(terms, rates2)
meanReversion = 0.05
normalVol = 0.01

# swaption(s)

maturity = 20  # in years
swaptions = []
terms = []
inputVols = []
for k in range(1, maturity):
    expTerm = str(k) + "y"
    swpTerm = str(maturity - k) + "y"
    sigma = 0.01  # atmVols.loc[expTerm,swpTerm]
    terms.append(expTerm + "-" + swpTerm)
Пример #4
0
        2.75e-2,\
        2.80e-2,\
        3.00e-2,\
        3.36e-2,\
        3.68e-2,\
        3.97e-2,\
        4.24e-2,\
        4.50e-2,\
        4.75e-2,\
        4.75e-2,\
        4.70e-2,\
        4.50e-2,\
        4.30e-2,\
        4.30e-2 ]

fwdRateYC = yc.YieldCurve(terms, rates)

print(fwdRateYC.table())

fwdRateYC.plot(1.0 / 365)

today = ql.Settings.getEvaluationDate(ql.Settings.instance())
print('Today: ' + str(today))
calendar = ql.TARGET()
period = ql.Period('500d')
bdc = ql.Following
print(str(calendar) + ', ' + str(period) + ', ' + str(bdc))
maturity = calendar.advance(today, period, bdc)
print('Maturity: ' + str(maturity))
discountFactor = fwdRateYC.discount(maturity)
print('Discount: ' + str(discountFactor))
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter

import pandas

import QuantLib as ql
import QuantLibWrapper.YieldCurve as yc

from QuantLibWrapper.HullWhiteModel import HullWhiteModel
from QuantLibWrapper.MCSimulation import MCSimulation
from QuantLibWrapper import Payoffs

# yield curves

flatCurve = yc.YieldCurve(['30y'], [0.03])

# We calibrate a Hull-White model to 100bp 'volatility' of x at 10y and 11y

meanReversion = 0.2


def obj1(sigma1):
    model = HullWhiteModel(flatCurve, meanReversion, np.array([5.0, 10.0]),
                           np.array([sigma1, sigma1]))
    return model.varianceX(0, 5.0) - (1.0e-2)**2 * 5


sigma1 = brentq(obj1, 1.0e-4, 1.0e-1)

from scipy.optimize import brentq

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D  # for 3d plotting
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

import pandas

import QuantLib as ql

from QuantLibWrapper import YieldCurve, HullWhiteModel, Swap, Swaption, createSwaption

# yield curves

flatCurve = YieldCurve(["30y"], [0.03])

terms = [
    "1y",
    "2y",
    "3y",
    "4y",
    "5y",
    "6y",
    "7y",
    "8y",
    "9y",
    "10y",
    "12y",
    "15y",
    "20y",
from QuantLibWrapper import YieldCurve, Swap, Swaption

# market data

terms = [
    '1y', '2y', '3y', '4y', '5y', '6y', '7y', '8y', '9y', '10y', '12y', '15y',
    '20y', '25y', '30y'
]
rates = [
    2.70e-2, 2.75e-2, 2.80e-2, 3.00e-2, 3.36e-2, 3.68e-2, 3.97e-2, 4.24e-2,
    4.50e-2, 4.75e-2, 4.75e-2, 4.70e-2, 4.50e-2, 4.30e-2, 4.30e-2
]
rates2 = [r + 0.005 for r in rates]

discCurve = YieldCurve(terms, rates)
projCurve = YieldCurve(terms, rates2)

normalVol = 0.01

# underlying swap

startDate = ql.Date(30, 10, 2028)
endDate = ql.Date(30, 10, 2038)
swap = Swap(startDate, endDate, 0.052513, discCurve, projCurve)

# swaption

expiryDate = ql.TARGET().advance(startDate, ql.Period('-2d'), ql.Preceding)
swaption = Swaption(swap, expiryDate, normalVol)