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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
"""
Copyright (C) 2021 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
"""
import unittest
import QuantLib as ql
class OptionsTest(unittest.TestCase):
def testFdHestonHullWhite(self):
""" Testing FDM Heston Hull-White pricing """
dc = ql.Actual365Fixed()
todays_date = ql.Date(19, ql.May, 2021)
r = ql.YieldTermStructureHandle(ql.FlatForward(todays_date, 0.075, dc))
d = ql.YieldTermStructureHandle(ql.FlatForward(todays_date, 0.01, dc))
s0 = 8.0
v0 = 0.2*0.2
kappa = 1.0
theta = v0
sigma = 0.4
rho = -0.75
a = 0.00883
sig = 0.00631
underlying = ql.makeQuoteHandle(s0)
option = ql.VanillaOption(
ql.PlainVanillaPayoff(ql.Option.Call, s0),
ql.EuropeanExercise(todays_date + ql.Period(1, ql.Years))
)
hull_white_process = ql.HullWhiteProcess(r, a, sig)
heston_process = ql.HestonProcess(r, d, underlying, v0, kappa, theta, sigma, rho)
option.setPricingEngine(
ql.FdHestonHullWhiteVanillaEngine(
ql.HestonModel(heston_process), hull_white_process, -0.5,
10, 200, 25, 10, 0, True
)
)
self.assertAlmostEqual(0.87628, option.NPV(), 4)
def testAnalyticHestonHullWhite(self):
""" Testing Analytic Heston Hull-White pricing """
today = ql.Date.todaysDate()
dc = ql.Actual365Fixed()
maturityDate = today + ql.Period(10 * 365, ql.Days)
v0 = 0.04
kappa = 0.5
theta = 0.04
sigma = 1.0
sig = 0.09
rho = -0.9
a = 0.08
r = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.05, dc))
q = ql.YieldTermStructureHandle(ql.FlatForward(today, 0.03, dc))
option = ql.VanillaOption(
ql.PlainVanillaPayoff(ql.Option.Call, 100.0),
ql.EuropeanExercise(maturityDate)
)
expected = 40.028973
s0 = 100
underlying = ql.makeQuoteHandle(s0)
hull_white_model = ql.HullWhite(r, a, sig)
heston_model = ql.HestonModel(
ql.HestonProcess(r, q, underlying, v0, kappa, theta, sigma, rho)
)
option.setPricingEngine(
ql.AnalyticHestonHullWhiteEngine(heston_model, hull_white_model)
)
self.assertAlmostEqual(expected, option.NPV(), 5)
option.setPricingEngine(
ql.AnalyticH1HWEngine(heston_model, hull_white_model, 0.0)
)
self.assertAlmostEqual(expected, option.NPV(), 5)
if __name__ == '__main__':
print("testing QuantLib", ql.__version__)
unittest.main(verbosity=2)
|