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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
"""
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.Settings.instance().evaluationDate
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(365, ql.Days))
)
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.Settings.instance().evaluationDate
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)
def testCashDividendEuropeanEngine(self):
"""Testing cash dividend European engine"""
today = today = ql.Settings.instance().evaluationDate
dc = ql.Actual365Fixed()
maturityDate = today + ql.Period(366, ql.Days)
option = ql.VanillaOption(
ql.PlainVanillaPayoff(ql.Option.Call, 100.0),
ql.EuropeanExercise(maturityDate)
)
div_schedule = ql.DividendSchedule()
div_schedule.append(ql.FixedDividend(5.0, today + ql.Period(92, ql.Days)))
process = ql.BlackScholesMertonProcess(
ql.makeQuoteHandle(100.0),
ql.YieldTermStructureHandle(ql.FlatForward(today, 0.075, dc)),
ql.YieldTermStructureHandle(ql.FlatForward(today, 0.05, dc)),
ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today, ql.TARGET(), 0.3, dc))
)
option.setPricingEngine(
ql.CashDividendEuropeanEngine(
process, div_schedule, ql.CashDividendEuropeanEngine.Escrowed
)
)
calculated = option.NPV()
option.setPricingEngine(ql.AnalyticDividendEuropeanEngine(process, div_schedule))
expected = option.NPV()
self.assertAlmostEqual(expected, calculated, 8)
option.setPricingEngine(
ql.CashDividendEuropeanEngine(
process, div_schedule, ql.CashDividendEuropeanEngine.Spot
)
)
self.assertAlmostEqual(7.9193, option.NPV(), 3)
if __name__ == '__main__':
print("testing QuantLib", ql.__version__)
unittest.main(verbosity=2)
|