首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中快速计算隐含波动率

在Python中快速计算隐含波动率
EN

Stack Overflow用户
提问于 2020-04-18 20:23:23
回答 5查看 12.4K关注 0票数 3

我正在寻找一个库,我可以用它来更快地计算python中的隐含波动性。我有关于1+百万行的期权数据,我想要计算其隐含波动率。计算IV的最快方法是什么?我试过使用py_vollib,但它不支持矢量化。大约需要5分钟。去计算。有没有其他的库可以帮助提高计算速度。在每秒都有数百万行的实时波动率计算中,人们使用什么?

EN

Stack Overflow用户

发布于 2020-04-22 22:06:10

你必须意识到隐含波动率的计算成本很高,如果你想要实时的数字,python可能不是最好的解决方案。

以下是您需要的函数的示例:

代码语言:javascript
复制
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

计算单个值就足够快了

代码语言:javascript
复制
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

但如果你尝试计算更多,你会意识到这需要一些时间……

代码语言:javascript
复制
%%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秒

票数 4
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61289020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档