QuantLib for Python RuntimeError: vega not provided
QuantLib for Python RuntimeError: vega not provided
使用二项式定价引擎和 Cox-Rubinstein 模型为普通美式期权定价。尝试检索 vega 时,我收到主题错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/conda/lib/python3.6/site-packages/QuantLib.py", line 10506, in vega
return _QuantLib.VanillaOption_vega(self)
RuntimeError: vega not provided
尽管 vega
是 american_option
的一种方法:
>>> dir(american_option) # scroll to the right -->
['NPV', '__class__', '__del__', '__delattr__', '__deref__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__weakref__', 'asObservable', 'delta', 'dividendRho', 'errorEstimate', 'freeze', 'gamma', 'impliedVolatility', 'isExpired', 'priceCurve', 'recalculate', 'rho', 'setPricingEngine', 'strikeSensitivity', 'theta', 'thetaPerDay', 'this', 'thisown', 'unfreeze', 'vega']
以下是基于几个在线示例的代码:
>>> from QuantLib import *
>>> maturity_date = Date(15, 1, 2016)
>>> spot_price = 127.62
>>> strike_price = 130
>>> volatility = 0.20 # the historical vols for a year
>>> dividend_rate = 0.0163
>>> option_type = Option.Call
>>> risk_free_rate = 0.001
>>> day_count = Actual365Fixed()
>>> calendar = UnitedStates()
>>> calculation_date = Date(8, 5, 2015)
>>> Settings.instance().evaluationDate = calculation_date
>>> payoff = PlainVanillaPayoff(option_type, strike_price)
>>> settlement = calculation_date
>>> am_exercise = AmericanExercise(settlement, maturity_date)
>>> american_option = VanillaOption(payoff, am_exercise)
>>> spot_handle = QuoteHandle(
... SimpleQuote(spot_price)
... )
>>> flat_ts = YieldTermStructureHandle(
... FlatForward(calculation_date,
... risk_free_rate,
... day_count)
... )
>>> dividend_yield = YieldTermStructureHandle(
... FlatForward(calculation_date,
... dividend_rate,
... day_count)
... )
>>> flat_vol_ts = BlackVolTermStructureHandle(
... BlackConstantVol(calculation_date,
... calendar,
... volatility,
... day_count)
... )
>>> bsm_process = BlackScholesMertonProcess(spot_handle,
... dividend_yield,
... flat_ts,
... flat_vol_ts)
>>>
>>>
>>> binomial_engine = BinomialVanillaEngine(bsm_process, "crr", 100)
>>> american_option.setPricingEngine(binomial_engine)
>>> print(american_option.vega())
版本:
>>> import QuantLib
>>> print(QuantLib.__version__)
1.11
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
问题是为什么没有提供vega?是什么导致了错误?
VanillaOption
class 声明了 vega
方法,但后者只能 return 如果选择的引擎计算结果。
一般来说,使用解析公式的引擎能够return希腊人便宜,因为它们也有解析表达式;基于二叉树的引擎,就像您正在使用的引擎一样,没有简单的方法来计算 vega。为了提供它,它应该执行一个昂贵的操作(即,用扰动的波动率重新计算并在数值上获得 vega),因此它会退出并留给你明确地执行昂贵的计算。
在这种情况下,您可以通过增加波动率,计算新的期权价格,并计算导数来计算vega。
我对此进行了详细说明,并在 this video 中提供了一些基本示例。
使用二项式定价引擎和 Cox-Rubinstein 模型为普通美式期权定价。尝试检索 vega 时,我收到主题错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/conda/lib/python3.6/site-packages/QuantLib.py", line 10506, in vega
return _QuantLib.VanillaOption_vega(self)
RuntimeError: vega not provided
尽管 vega
是 american_option
的一种方法:
>>> dir(american_option) # scroll to the right -->
['NPV', '__class__', '__del__', '__delattr__', '__deref__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__weakref__', 'asObservable', 'delta', 'dividendRho', 'errorEstimate', 'freeze', 'gamma', 'impliedVolatility', 'isExpired', 'priceCurve', 'recalculate', 'rho', 'setPricingEngine', 'strikeSensitivity', 'theta', 'thetaPerDay', 'this', 'thisown', 'unfreeze', 'vega']
以下是基于几个在线示例的代码:
>>> from QuantLib import *
>>> maturity_date = Date(15, 1, 2016)
>>> spot_price = 127.62
>>> strike_price = 130
>>> volatility = 0.20 # the historical vols for a year
>>> dividend_rate = 0.0163
>>> option_type = Option.Call
>>> risk_free_rate = 0.001
>>> day_count = Actual365Fixed()
>>> calendar = UnitedStates()
>>> calculation_date = Date(8, 5, 2015)
>>> Settings.instance().evaluationDate = calculation_date
>>> payoff = PlainVanillaPayoff(option_type, strike_price)
>>> settlement = calculation_date
>>> am_exercise = AmericanExercise(settlement, maturity_date)
>>> american_option = VanillaOption(payoff, am_exercise)
>>> spot_handle = QuoteHandle(
... SimpleQuote(spot_price)
... )
>>> flat_ts = YieldTermStructureHandle(
... FlatForward(calculation_date,
... risk_free_rate,
... day_count)
... )
>>> dividend_yield = YieldTermStructureHandle(
... FlatForward(calculation_date,
... dividend_rate,
... day_count)
... )
>>> flat_vol_ts = BlackVolTermStructureHandle(
... BlackConstantVol(calculation_date,
... calendar,
... volatility,
... day_count)
... )
>>> bsm_process = BlackScholesMertonProcess(spot_handle,
... dividend_yield,
... flat_ts,
... flat_vol_ts)
>>>
>>>
>>> binomial_engine = BinomialVanillaEngine(bsm_process, "crr", 100)
>>> american_option.setPricingEngine(binomial_engine)
>>> print(american_option.vega())
版本:
>>> import QuantLib
>>> print(QuantLib.__version__)
1.11
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
问题是为什么没有提供vega?是什么导致了错误?
VanillaOption
class 声明了 vega
方法,但后者只能 return 如果选择的引擎计算结果。
一般来说,使用解析公式的引擎能够return希腊人便宜,因为它们也有解析表达式;基于二叉树的引擎,就像您正在使用的引擎一样,没有简单的方法来计算 vega。为了提供它,它应该执行一个昂贵的操作(即,用扰动的波动率重新计算并在数值上获得 vega),因此它会退出并留给你明确地执行昂贵的计算。
在这种情况下,您可以通过增加波动率,计算新的期权价格,并计算导数来计算vega。
我对此进行了详细说明,并在 this video 中提供了一些基本示例。