py_vollib.ref_python.black_scholes.implied_volatility
¶
A library for option pricing, implied volatility, and greek calculation. py_vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.
- copyright:
© 2023 Larry Richards
- license:
MIT, see LICENSE for more details.
py_vollib.ref_python is a pure python version of py_vollib without any dependence on LetsBeRational. It is provided purely as a reference implementation for sanity checking. It is not recommended for industrial use.¶
Module Contents¶
Functions¶
|
Calculate the Black-Scholes implied volatility. |
- implied_volatility(price, S, K, t, r, flag)[source]¶
Calculate the Black-Scholes implied volatility.
- Parameters:
price (float) – the Black-Scholes option price
S (float) – underlying asset price
K (float) – strike price
t (float) – time to expiration in years
r (float) – risk-free interest rate
flag (str) – ‘c’ or ‘p’ for call or put.
>>> S = 100 >>> K = 100 >>> sigma = .2 >>> r = .01 >>> flag = 'c' >>> t = .5
>>> price = black_scholes(flag, S, K, t, r, sigma) >>> iv = implied_volatility(price, S, K, t, r, flag)
>>> expected_price = 5.87602423383 >>> expected_iv = 0.2
>>> abs(expected_price - price) < 0.00001 True >>> abs(expected_iv - iv) < 0.01 True
>>> sigma = 0.3 >>> S, K, t, r, flag = 100.0, 1000.0, 0.5, 0.05, 'p' >>> price = black_scholes(flag, S, K, t, r, sigma) >>> print (price) 875.309912028 >>> iv = implied_volatility(price, S, K, t, r, flag)
>>> print (round(iv, 1)) 0.0