Calculate Implied Volatility from option prices.
Implied Volatility¶
-
py_vollib_vectorized.implied_volatility.
vectorized_implied_volatility
(price, S, K, t, r, flag, q=None, *, on_error='warn', model='black_scholes', return_as='dataframe', dtype=<class 'numpy.float64'>, **kwargs)¶ An extremely fast, efficient and accurate Implied Volatility calculator for option/future contracts. Inputs can be lists, tuples, floats,
pd.Series
, or numpy.arrays. Broadcasting is applied on the inputs.- Parameters
price – The price of the option.
S – The price of the underlying asset.
K – The strike price.
t – The annualized time to expiration. Must be positive. For small TTEs, use a small value (1e-3).
r – The Interest Free Rate.
flag – For each contract, this should be specified as c for a call option and p for a put option.
q – The annualized continuous dividend yield.
on_error – Either “raise”, “warn” or “ignore”.
model – Must be one of “black_scholes” or “black_scholes_merton”. Use vectorized_implied_volatility_black for the Black model.
return_as – To return as a
pd.Series
object, use “series”. To return as apd.DataFrame
object, use “dataframe”. Any other value will return anumpy.array
object.dtype – Data type.
kwargs – Other keyword arguments are ignored.
- Returns
pd.Series
,pd.DataFrame
ornumpy.array
object containing the implied volatility for each contract.
>>> import py_vollib.black_scholes_merton.implied_volatility >>> import py_vollib_vectorized >>> price = 0.10 >>> S = 95 >>> K = [100, 90] >>> t = .2 >>> r = .2 >>> flag = ['c', 'p'] >>> py_vollib.black_scholes_merton.implied_volatility.implied_volatility(price, S, K, t, r, flag, q=0, return_as='numpy') array([0.02621257, 0.12585767]) >>> py_vollib_vectorized.vectorized_implied_volatility(price, S, K, t, r, flag, q=0, model='black_scholes_merton',return_as='numpy') # equivalent array([0.02621257, 0.12585767])
-
py_vollib_vectorized.implied_volatility.
vectorized_implied_volatility_black
(price, F, K, r, t, flag, *, on_error='warn', return_as='dataframe', dtype=<class 'numpy.float64'>, **kwargs)¶ An extremely fast, efficient and accurate Implied Volatility calculator for option/future contracts. Inputs can be lists, tuples, floats,
pd.Series
, or numpy.arrays. Broadcasting is applied on the inputs. This method should only be used in the black model of pricing. Argument order is kept consistent with that of the py_vollib package.- Parameters
price – The price of the option.
F – The price of the underlying asset.
K – The strike price.
t – The annualized time to expiration. Must be positive. For small TTEs, use a small value (1e-3).
r – The Interest Free Rate.
flag – For each contract, this should be specified as c for a call option and p for a put option.
on_error – Either “raise”, “warn” or “ignore”.
return_as – To return as a
pd.Series
object, use “series”. To return as apd.DataFrame
object, use “dataframe”. Any other value will return anumpy.array
object.dtype – Data type.
kwargs – Other keyword arguments are ignored.
- Returns
pd.Series
,pd.DataFrame
ornumpy.array
object containing the implied volatility for each contract.
>>> import py_vollib.black.implied_volatility >>> import py_vollib_vectorized >>> price = 0.10 >>> F = 95 >>> K = [100, 90] >>> t = .2 >>> r = .2 >>> flag = ['c', 'p'] >>> py_vollib.black.implied_volatility.implied_volatility(price, F, K, r, t, flag, return_as='numpy') array([0.02621257, 0.12585767]) >>> py_vollib_vectorized.vectorized_implied_volatility_black(price, F, K, r, t, flag, return_as='numpy') # equivalent array([0.02621257, 0.12585767])