vollib.ref_python.black_scholes package
Subpackages
- vollib.ref_python.black_scholes.greeks package
Submodules
vollib.ref_python.black_scholes.implied_volatility module
vollib.ref_python.black_scholes.implied_volatility
A library for option pricing, implied volatility, and greek calculation. vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.
- copyright:
© 2017 Gammon Capital LLC
- license:
MIT, see LICENSE for more details.
vollib.ref_python is a pure python version of vollib without any dependence on LetsBeRational. It is provided purely as a reference implementation for sanity checking. It is not recommended for industrial use.
- vollib.ref_python.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
Module contents
vollib.ref_python.black_scholes
A library for option pricing, implied volatility, and greek calculation. vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.
- copyright:
© 2017 Gammon Capital LLC
- license:
MIT, see LICENSE for more details.
vollib.ref_python is a pure python version of vollib without any dependence on LetsBeRational. It is provided purely as a reference implementation for sanity checking. It is not recommended for industrial use.
- vollib.ref_python.black_scholes.black_scholes(flag, S, K, t, r, sigma)[source]
- Return the Black-Scholes option price implemented in
python (for reference).
- Parameters:
S (float) – underlying asset price
K (float) – strike price
sigma (float) – annualized standard deviation, or volatility
t (float) – time to expiration in years
r (float) – risk-free interest rate
flag (str) – ‘c’ or ‘p’ for call or put.
>>> S,K,t,r,sigma = 60,65,.25,.08,.3 >>> expected = 2.13336844492 >>> actual = black_scholes('c',S,K,t,r,sigma) >>> abs(expected-actual) < 1e-11 True
- vollib.ref_python.black_scholes.d1(S, K, t, r, sigma)[source]
Calculate the d1 component of the Black-Scholes PDE.
- Parameters:
S (float) – underlying asset price
K (float) – strike price
sigma (float) – annualized standard deviation, or volatility
t (float) – time to expiration in years
r (float) – risk-free interest rate
John C. Hull, “Options, Futures and Other Derivatives,” 7th edition, Example 13.6, page 294
>>> S = 42 >>> K = 40 >>> r = .10 >>> sigma = .20 >>> t = 0.5 >>> calculated_d1 = d1(S,K,t,r,sigma) >>> text_book_d1 = 0.7693 >>> abs(calculated_d1 - text_book_d1) < 0.0001 True
- vollib.ref_python.black_scholes.d2(S, K, t, r, sigma)[source]
Calculate the d2 component of the Black-Scholes PDE.
- Parameters:
S (float) – underlying asset price
K (float) – strike price
sigma (float) – annualized standard deviation, or volatility
t (float) – time to expiration in years
r (float) – risk-free interest rate
John C. Hull, “Options, Futures and Other Derivatives,” 7th edition, Example 13.6, page 294
>>> S = 42 >>> K = 40 >>> r = .10 >>> sigma = .20 >>> t = 0.5 >>> calculated_d2 = d2(S,K,t,r,sigma) #0.627841271869 >>> text_book_d2 = 0.6278 >>> abs(calculated_d2 - text_book_d2) < 0.0001 True