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
|
#!/usr/bin/python
"""
Copyright (C) 2000, 2001, 2002 RiskMap srl
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 ferdinando@ametrano.net
The license is also available online at http://quantlib.org/html/license.html
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.
"""
__version__ = "$Revision: 1.14 $"
# $Source: /cvsroot/quantlib/QuantLib-Python/QuantLib/test/american_option.py,v $
import QuantLib
import unittest
def relErr(x1,x2,reference):
if reference != 0.0:
return abs(x1-x2)/reference
else:
return 10e10
class FdAmericanOptionTest(unittest.TestCase):
def runTest(self):
"Testing step condition options"
nstp = 145
ngrd = nstp + 1
# ranges
rangeUnder = [100]
rangeQrate = [0.04, 0.05, 0.06]
rangeResTime = [1.0]
rangeStrike = [50, 100, 150]
rangeVol = [ 0.05, 0.5, 1.2]
rangeRrate = [ 0.01, 0.05, 0.15]
err_delta = 2e-3
err_gamma = 2e-3
err_theta = 2e-3
err_rho = 2e-3
err_divRho= 2e-3
err_vega = 2e-3
for pricer in [QuantLib.FdAmericanOption, QuantLib.FdShoutOption]:
for typ in ['Call','Put','Straddle']:
for under in rangeUnder:
for Qrate in rangeQrate:
for resTime in rangeResTime:
for Rrate in rangeRrate:
for strike in rangeStrike:
for vol in rangeVol:
#Check Greeks
dS = under/10000.0
dT = resTime/nstp
dVol = vol/10000.0
dR = Rrate/10000.0
dQ = Qrate/10000.0
opt = pricer(typ, under, strike, Qrate, Rrate, resTime,
vol, nstp, ngrd)
opt_val = opt.value()
if opt_val>0.00001*under:
optPs = pricer(typ, under+dS, strike, Qrate, Rrate,
resTime ,vol, nstp, ngrd)
optMs = pricer(typ, under-dS, strike, Qrate, Rrate,
resTime, vol, nstp, ngrd)
optPt = pricer(typ, under, strike, Qrate, Rrate,
resTime+dT, vol, nstp+1, ngrd)
optMt = pricer(typ, under, strike, Qrate, Rrate,
resTime-dT, vol, nstp-1, ngrd)
optPr = pricer(typ, under, strike, Qrate, Rrate+dR,
resTime, vol, nstp, ngrd)
optMr = pricer(typ, under, strike, Qrate, Rrate-dR,
resTime, vol, nstp, ngrd)
optPq = pricer(typ, under, strike, Qrate+dQ, Rrate,
resTime, vol, nstp, ngrd)
optMq = pricer(typ, under, strike, Qrate-dQ, Rrate,
resTime, vol, nstp, ngrd)
optPv = pricer(typ, under, strike, Qrate, Rrate,
resTime, vol+dVol, nstp, ngrd)
optMv = pricer(typ, under, strike, Qrate, Rrate,
resTime, vol-dVol, nstp, ngrd)
# numeric values
deltaNum = (optPs.value()-optMs.value())/(2*dS)
gammaNum = (optPs.delta()-optMs.delta())/(2*dS)
thetaNum =-(optPt.value()-optMt.value())/(2*dT)
rhoNum = (optPr.value()-optMr.value())/(2*dR)
divRhoNum = (optPq.value()-optMq.value())/(2*dQ)
vegaNum = (optPv.value()-optMv.value())/(2*dVol)
# calculated values
delta = opt.delta()
gamma = opt.gamma()
theta = opt.theta()
rho = opt.rho()
divRho = opt.dividendRho()
vega = opt.vega()
if not (relErr(delta,deltaNum,under)<=err_delta
and relErr(gamma,gammaNum,under)<=err_gamma
and relErr(theta,thetaNum,under)<=err_theta
and relErr(rho, rhoNum, under)<=err_rho
and relErr(divRho,divRhoNum,under)<=err_divRho
and relErr(vega, vegaNum, under)<=err_vega):
self.fail("""
Option details: %(typ)s %(under)f %(strike)f %(Qrate)f %(Rrate)f %(resTime)f %(vol)f
value = %(opt_val)+9.5f
delta = %(delta)+9.5f, deltaNum = %(deltaNum)+9.5f
gamma = %(gamma)+9.5f, gammaNum = %(gammaNum)+9.5f
theta = %(theta)+9.5f, thetaNum = %(thetaNum)+9.5f
rho = %(rho)+9.5f, rhoNum = %(rhoNum)+9.5f
divRho = %(divRho)+9.5f, divRhoNum = %(divRhoNum)+9.5f
vega = %(vega)+9.5f, vegaNum = %(vegaNum)+9.5f
""" % locals())
if __name__ == '__main__':
print 'testing QuantLib', QuantLib.__version__, QuantLib.QuantLibc.__file__, QuantLib.__file__
import sys
suite = unittest.TestSuite()
suite.addTest(FdAmericanOptionTest())
if sys.hexversion >= 0x020100f0:
unittest.TextTestRunner(verbosity=2).run(suite)
else:
unittest.TextTestRunner().run(suite)
raw_input('press any key to continue')
|