In [1]:
import numpy as np
In [2]:
# import
#from option_models import bsm
#from option_models import normal
#import option_models as opt
from option_models import bsm
In [3]:
### only run this when you changed the class definition
import imp
imp.reload(bsm)
Out[3]:
In [ ]:
In [4]:
# create model
texp = 0.25
vol = 0.2
bsm1 = bsm.Model(texp, vol)
In [5]:
# price
strike = 102
spot = 100
price = bsm1.price(strike=strike, spot=spot, cp_sign=1)
print(price)
assert( abs(price - 3.10628366655) < 1e-10 )
In [6]:
# Randomly generate spot/strike/expiry/intr/divr/cp_sign
# Then test implied volatility
for k in range(100):
spot = np.random.uniform(80,100)
strike = np.random.uniform(80,100)
vol = np.random.uniform(0.0001, 0.4)
texp = np.random.uniform(0.1, 5)
intr = np.random.uniform(0, 0.3)
divr = np.random.uniform(0, 0.3)
cp_sign = 1 if np.random.rand() > 0.5 else -1
#print( spot, strike, vol, texp, intr, divr, cp_sign)
bsm2 = bsm.Model(texp=texp, vol=vol, intr=intr, divr=divr)
price = bsm2.price(strike, spot, cp_sign=cp_sign )
# get implied vol
vol_imp = bsm2.impvol(price, strike, spot, cp_sign=cp_sign)
# now price option with the obtained implied vol
price_imp = bsm2.price(strike, spot, vol=vol_imp, cp_sign=cp_sign )
# compare the two prices
assert( abs(price - price_imp) < 1e-8 )
In [7]:
Out[7]:
In [ ]: