我正在寻找一个库,我可以用它来更快地计算python中的隐含波动性。我有关于1+百万行的期权数据,我想要计算其隐含波动率。计算IV的最快方法是什么?我试过使用py_vollib,但它不支持矢量化。大约需要5分钟。去计算。有没有其他的库可以帮助提高计算速度。在每秒都有数百万行的实时波动率计算中,人们使用什么?
发布于 2020-04-22 22:06:10
你必须意识到隐含波动率的计算成本很高,如果你想要实时的数字,python可能不是最好的解决方案。
以下是您需要的函数的示例:
import numpy as np
from scipy.stats import norm
N = norm.cdf
def bs_call(S, K, T, r, vol):
d1 = (np.log(S/K) + (r + 0.5*vol**2)*T) / (vol*np.sqrt(T))
d2 = d1 - vol * np.sqrt(T)
return S * norm.cdf(d1) - np.exp(-r * T) * K * norm.cdf(d2)
def bs_vega(S, K, T, r, sigma):
d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
return S * norm.pdf(d1) * np.sqrt(T)
def find_vol(target_value, S, K, T, r, *args):
MAX_ITERATIONS = 200
PRECISION = 1.0e-5
sigma = 0.5
for i in range(0, MAX_ITERATIONS):
price = bs_call(S, K, T, r, sigma)
vega = bs_vega(S, K, T, r, sigma)
diff = target_value - price # our root
if (abs(diff) < PRECISION):
return sigma
sigma = sigma + diff/vega # f(x) / f'(x)
return sigma # value wasn't found, return best guess so far计算单个值就足够快了
S = 100
K = 100
T = 11
r = 0.01
vol = 0.25
V_market = bs_call(S, K, T, r, vol)
implied_vol = find_vol(V_market, S, K, T, r)
print ('Implied vol: %.2f%%' % (implied_vol * 100))
print ('Market price = %.2f' % V_market)
print ('Model price = %.2f' % bs_call(S, K, T, r, implied_vol))隐含音量: 25.00%
市场价格= 35.94
型号价格= 35.94
但如果你尝试计算更多,你会意识到这需要一些时间……
%%time
size = 10000
S = np.random.randint(100, 200, size)
K = S * 1.25
T = np.ones(size)
R = np.random.randint(0, 3, size) / 100
vols = np.random.randint(15, 50, size) / 100
prices = bs_call(S, K, T, R, vols)
params = np.vstack((prices, S, K, T, R, vols))
vols = list(map(find_vol, *params))挂墙时间: 10.5秒
https://stackoverflow.com/questions/61289020
复制相似问题