简体   繁体   中英

QuantLib for Python RuntimeError: vega not provided

Pricing a plain vanilla American option with the binomial pricing engine and Cox-Rubinstein model. When attempting to retrieve vega, I receive the subject error:

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

This despite vega being a method of 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']

Here is the code which is based on several online examples:

>>> 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())

Versions:

>>> import QuantLib
>>> print(QuantLib.__version__)
1.11

Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)

Question is why is vega not provided? What is causing the error?

The VanillaOption class declares the vega method, but the latter can only return a result if the chosen engine calculates it.

In general, engines that use an analytic formula are able to return Greeks cheaply, because they also have an analytic expression; an engine based on a binomial tree, like the one you're using, doesn't have a simple way to calculate the vega. In order to provide it, it should perform an expensive operation (that is, recalculate with a perturbed volatility and get the vega numerically) and therefore it bails out and leaves it to you to perform the expensive calculation explicitly.

In this case, you can calculate the vega by increasing the volatility, calculating a new option price, and calculate the derivative numerically.

I elaborate more on this and provide a few basic examples in this video .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM