vollib.ref_python.black_scholes_merton package

Subpackages

Submodules

vollib.ref_python.black_scholes_merton.implied_volatility module

vollib.ref_python.black_scholes_merton.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.black_scholes_merton.implied_volatility.implied_volatility(price, S, K, t, r, q, flag)[source]

Calculate the Black-Scholes-Merton implied volatility.

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

  • q (float) – annualized continuous dividend rate

  • flag (str) – ‘c’ or ‘p’ for call or put.

>>> S = 100
>>> K = 100
>>> sigma = .2
>>> r = .01
>>> flag = 'c'
>>> t = .5
>>> q = .02
>>> price = black_scholes_merton(flag, S, K, t, r, sigma, q)
>>> implied_volatility(price, S, K, t, r, q, flag)
0.20000000000000018
>>> flac = 'p'
>>> sigma = 0.3
>>> price = black_scholes_merton(flag, S, K, t, r, sigma, q)
>>> price
8.138101080183894
>>> implied_volatility(price, S, K, t, r, q, flag)
0.30000000000000027

Module contents

vollib.ref_python.black_scholes_merton

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_merton.black_scholes_merton(flag, S, K, t, r, sigma, q)[source]

Return the Black-Scholes-Merton call 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

  • q (float) – annualized continuous dividend rate

  • flag (str) – ‘c’ or ‘p’ for call or put.

From Espen Haug, The Complete Guide To Option Pricing Formulas Page 4

>>> S=100
>>> K=95
>>> q=.05
>>> t = 0.5
>>> r = 0.1
>>> sigma = 0.2
>>> p_published_value = 2.4648
>>> p_calc = black_scholes_merton('p', S, K, t, r, sigma, q)
>>> abs(p_published_value - p_calc) < 0.0001
True
vollib.ref_python.black_scholes_merton.bsm_call(S, K, t, r, sigma, q)[source]

Return the Black-Scholes-Merton call 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

  • q (float) – annualized continuous dividend rate

vollib.ref_python.black_scholes_merton.bsm_put(S, K, t, r, sigma, q)[source]

Return the Black-Scholes-Merton put 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

  • q (float) – annualized continuous dividend rate

From Espen Haug, The Complete Guide To Option Pricing Formulas Page 4

>>> S=100
>>> K=95
>>> q=.05
>>> t = 0.5
>>> r = 0.1
>>> sigma = 0.2
>>> p_published_value = 2.4648
>>> p_calc = bsm_put(S, K, t, r, sigma, q)
>>> abs(p_published_value - p_calc) < 0.0001
True
vollib.ref_python.black_scholes_merton.d1(S, K, t, r, sigma, q)[source]

Calculate the d1 component of the Black-Scholes-Merton 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

  • q (float) – annualized continuous dividend rate

From Espen Haug, The Complete Guide To Option Pricing Formulas Page 4

>>> S=100
>>> K=95
>>> q=.05
>>> t = 0.5
>>> r = 0.1
>>> sigma = 0.2
>>> d1_published_value = 0.6102
>>> d1_calc = d1(S,K,t,r,sigma,q)
>>> abs(d1_published_value - d1_calc) < 0.0001
True
vollib.ref_python.black_scholes_merton.d2(S, K, t, r, sigma, q)[source]

Calculate the d2 component of the Black-Scholes-Merton 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

  • q (float) – annualized continuous dividend rate

From Espen Haug, The Complete Guide To Option Pricing Formulas Page 4

>>> S=100
>>> K=95
>>> q=.05
>>> t = 0.5
>>> r = 0.1
>>> sigma = 0.2
>>> d2_published_value = 0.4688
>>> d2_calc = d2(S,K,t,r,sigma,q)
>>> abs(d2_published_value - d2_calc) < 0.0001
True