【问题标题】:Implied volatility calculator is wrong隐含波动率计算器是错误的
【发布时间】:2015-05-20 21:37:14
【问题描述】:

我是一名计算机科学家,试图更多地了解量化金融。我有一个用于计算 Black-Scholes 模型中欧式看涨期权价值的程序,并且正在尝试添加一种计算隐含波动率的方法。

import math
import numpy as np
import pdb
from scipy.stats import norm

class BlackScholes(object):
  '''Class wrapper for methods.'''

  def __init__(self, s, k, t, r, sigma):
    '''Initialize a model with the given parameters.
       @param s: initial stock price
       @param k: strike price
       @param t: time to maturity (in years)
       @param r: Constant, riskless short rate (1 equals 100%)
       @param sigma: Guess for volatility. (1 equals 100%)
    '''
    self.s = s
    self.k = k
    self.t = t
    self.r = r
    self.sigma = sigma
    self.d = self.factors()

  def euro_call(self):
    ''' Calculate the value of a European call option
        using Black-Scholes. No dividends.
        @return: The value for an option with the given parameters.'''
    return norm.cdf(self.d[0]) * self.s - (norm.cdf(self.d[1]) * self.k *
                                     np.exp(-self.r * self.t))

  def factors(self):
    '''
      Calculates the d1 and d2 factors used in a large
      number of Black Scholes equations.
    '''
    d1 = (1.0 / (self.sigma * np.sqrt(self.t)) * (math.log(self.s / self.k)
                    + (self.r + self.sigma ** 2 / 2) * self.t))
    d2 = (1.0 / (self.sigma * np.sqrt(self.t)) * (math.log(self.s / self.k)
                    + (self.r - self.sigma ** 2 / 2) * self.t))
    if math.isnan(d1):
      pdb.set_trace()
    assert(not math.isnan(d1))
    assert(not math.isnan(d2))
    return (d1, d2) 

  def imp_vol(self, C0):
    ''' Calculate the implied volatility of a call option,
        where sigma is interpretered as a best guess.
        Updates sigma as a side effect.
        @rtype: float
        @return: Implied volatility.'''
    for i in range(128):
      self.sigma -= (self.euro_call() - C0) / self.vega()
      assert(self.sigma != -float("inf"))
      assert(self.sigma != float("inf"))
      self.d = self.factors()
    print(C0,
      BlackScholes(self.s, self.k, self.t, self.r, self.sigma).euro_call())
    return self.sigma

  def vega(self):
    ''' Returns vega, which is the derivative of the
        option value with respect to the asset's volatility.
        It is the same for both calls and puts.
        @rtype: float
        @return: vega'''
    v = self.s * norm.pdf(self.d[0]) * np.sqrt(self.t)
    assert(not math.isnan(v))
    return v

这是我目前拥有的两个测试用例:

print(BlackScholes(17.6639, 1.0, 1.0, .01, 2.0).imp_vol(16.85))
print(BlackScholes(17.6639, 1.0, .049, .01, 2.0).imp_vol(16.85))

顶部打印出 1.94,这与http://www.option-price.com/implied-volatility.php 给出的 195.21% 的值相当接近。然而,底部的(如果您删除断言语句)会打印出“nan”和以下警告消息。使用 assert 语句,self.vega() 在 imp_vol 方法中返回零,然后是 assert(self.sigma != -float("inf"))

so.py:51: RuntimeWarning: divide by zero encountered in double_scalars
  self.sigma -= (self.euro_call() - C0) / self.vega()
so.py:37: RuntimeWarning: invalid value encountered in double_scalars
  + (self.r + self.sigma ** 2 / 2) * self.t))
so.py:39: RuntimeWarning: invalid value encountered in double_scalars
  + (self.r - self.sigma ** 2 / 2) * self.t))

【问题讨论】:

  • 你用的是哪个python版本?
  • 我使用的是 Python 2.7.8。
  • 我在该网站上输入时将 17.6639 舍入到 17.66,加上剩余的小数使其完全一致。
  • 期权定价中无限波动的想法没有任何实际意义,所以我有 99% 的把握我的输出是一个错误,但我对 Black-Scholes 方程的理解不够好调试它。
  • 那么,怪罪浮点怪异?

标签: python finance


【解决方案1】:

你所做的没有多大意义。您正试图撤消大量货币短期期权的隐含波动率。此选项上的 vega 实际上为 0,因此您获得的隐含卷数将毫无意义。浮点舍入给了你无限的体积,我一点也不惊讶。

【讨论】:

    【解决方案2】:

    如果您使用 vega 来估计隐含波动率,您可能正在做一些牛顿梯度搜索的变体,它不会在所有情况下都收敛到一个解决方案,我用 R 或 VBA 编程,所以只能提供一个解决方案你翻译一下,二分搜索方法简单健壮并且总是收敛,这里写了完整的期权定价模型一书的人是 Espen Haugs 算法,用于二分搜索以找到隐含波动率;

    Newton-Raphson 方法需要了解部分 期权定价公式关于波动率的导数 (vega) 在搜索隐含波动率时。对于一些选项 (特别是异国情调和美式期权),vega 不为人所知 溶解地。二分法是一种更简单的估计方法 vega 未知时的隐含波动率。二分法 需要两个初始波动率估计(种子值):

    1. 隐含波动率 al 的“低”估计值,对应于 一个选项值,CL
    2. “高”波动率估计,aH,对应于期权 值,CH 期权市场价格 Cm 位于 CL 和 cH 之间。二分法- 估计被给出为两者之间的线性插值 估计: 如果 c(cr, + ) c m 直到 lcm — c(cri+i)i
          Function  GBlackScholesImpVolBisection(CallPutFlag 
           As String, S As Double,
           X As Double, T As Double, r As Double, _
           b As Double, cm As Double) As Variant
           Dim vLow As Double, vHigh As Double, vi As Double
           Dim cLow As Double, cHigh As Double, epsilon As 
       Double
       Dim counter As Integer
       vLow = 0.005
       vHigh = 4
       epsilon = le-08
       cLow = GBlackScholes ( CallPutFlag , S, X, T, r, b, vLow)
       cHigh = GBlackScholes ( CallPutFlag , S, X, T, r, b, vHigh)
       counter = 0
       vi = vLow + (cm — cLow ) * (vHigh — vLow) / ( cHigh — cLow)
       While Abs(cm — GBlackScholes ( CallPutFlag , S, X, T, r, b, vi )) > epsilon
       counter = counter + 1
       If counter = 100 Then
       GBlackScholesImpVolBisection
       Exit Function
       End If
       If GBlackScholes ( CallPutFlag , S, X, T, r, b, vi ) < cm Then
       vLow = vi
       Else
       vHigh = vi
       End If
       cLow = GBlackScholes ( CallPutFlag , S, X, T, r, b, vLow)
       cHigh = GBlackScholes ( CallPutFlag , S, X, T, r, b, vHigh )
       vi = vLow + (cm — cLow ) * (vHigh — vLow) / ( cHigh — cLow)
       Wend
       GBlackScholesImpVolBisection = vi
       End Function```
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      相关资源
      最近更新 更多