2024-06-02 05:20:28 发布
网友
我正在寻找一个库,我可以用它来更快地计算python中的隐含波动率。我有大约100多万行的期权数据,我想计算隐含波动率。我计算静脉输液的最快方法是什么。我尝试过使用py_vollib,但它不支持矢量化。计算大约需要5分钟。有没有其他库可以帮助加快计算速度。人们在实时波动率计算中使用的是什么?每秒有数百万行的数据
您必须意识到隐含波动率计算在计算上非常昂贵,如果您想要实时数字,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秒
最近,在py_vollib_vectorized上有一个py_vollib的矢量化版本,它构建在py_vollib之上,使数千份期权合约的定价和计算速度大大加快
py_vollib
如果将对norm.cdf()-method的所有调用更改为ndtr(),则性能将提高2.4倍
norm.cdf()
ndtr()
如果您将norm.pdf()-method更改为norm._pdf(),您将获得另一个(巨大的)增加
norm.pdf()
norm._pdf()
实施了这两项更改后,上面的示例在我的机器上从17.7 [s]下降到了0.99 [s]
17.7 [s]
0.99 [s]
您将失去错误检查等,但在这种情况下,您可能不需要所有这些
见:https://github.com/scipy/scipy/issues/1914
ndtr()在scipy.special
scipy.special
您必须意识到隐含波动率计算在计算上非常昂贵,如果您想要实时数字,python可能不是最好的解决方案
以下是您需要的功能示例:
计算单个值足够快
隐含容量:25.00%
市场价格=35.94
型号价格=35.94
但是如果你尝试计算很多,你会意识到这需要一些时间
壁时间:10.5秒
最近,在py_vollib_vectorized上有一个
py_vollib
的矢量化版本,它构建在py_vollib
之上,使数千份期权合约的定价和计算速度大大加快如果将对
norm.cdf()
-method的所有调用更改为ndtr()
,则性能将提高2.4倍如果您将
norm.pdf()
-method更改为norm._pdf()
,您将获得另一个(巨大的)增加实施了这两项更改后,上面的示例在我的机器上从
17.7 [s]
下降到了0.99 [s]
您将失去错误检查等,但在这种情况下,您可能不需要所有这些
见:https://github.com/scipy/scipy/issues/1914
ndtr()
在scipy.special
相关问题 更多 >
编程相关推荐