Always vectorize computation

Don't use loop


In [11]:
import numpy as np
import timeit
from option_models import normal
from option_models import bsm

In [2]:
### only run this when you changed the class definition
import imp
imp.reload(normal)
imp.reload(bsm)


Out[2]:
<module 'option_models.bsm' from 'D:\\Mega\\PHBS\\2017 M1 ASP\\SABRmodel_Base\\option_models\\bsm.py'>

In [6]:
n_vec = 10000
strike = np.random.rand(n_vec)*200
spot = 100
texp = 1
vol = np.random.rand(n_vec)*10

In [ ]:


In [7]:
start_time = timeit.default_timer()
price_vec = normal.price(strike, spot, texp, vol)
elapsed = timeit.default_timer() - start_time
print(elapsed)


0.012746952558359226

In [9]:
price_loop = np.zeros(strike.size)
start_time = timeit.default_timer()
for ind in range(strike.size):
    price_loop[ind] = normal.price(strike[ind], spot, texp, vol[ind])

elapsed = timeit.default_timer() - start_time
print(elapsed)


1.4413929842040716

In [10]:
price_vec - price_loop


Out[10]:
array([ 0.,  0.,  0., ...,  0.,  0.,  0.])

In [ ]:


In [ ]:


In [ ]: