py_vollib.black_scholes_merton.greeks package

Submodules

py_vollib.black_scholes_merton.greeks.analytical module

py_vollib.black_scholes_merton.greeks.analytical

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:© 2017 Gammon Capital LLC
license:MIT, see LICENSE for more details.

About LetsBeRational:

The source code of LetsBeRational resides at www.jaeckel.org/LetsBeRational.7z .

========================================================================================
Copyright © 2013-2014 Peter Jäckel.

Permission to use, copy, modify, and distribute this software is freely granted,
provided that this notice is preserved.

WARRANTY DISCLAIMER
The Software is provided "as is" without warranty of any kind, either express or implied,
including without limitation any implied warranties of condition, uninterrupted use,
merchantability, fitness for a particular purpose, or non-infringement.
========================================================================================
py_vollib.black_scholes_merton.greeks.analytical.delta(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton delta of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

Example 17.1, page 355, Hull:

>>> S = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> q = 0
>>> sigma = 0.2
>>> flag = 'c'
>>> delta_calc = delta(flag, S, K, t, r, sigma, q)
>>> # 0.521601633972
>>> delta_text_book = 0.522
>>> abs(delta_calc - delta_text_book) < .01
True
py_vollib.black_scholes_merton.greeks.analytical.gamma(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton gamma of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

Example 17.4, page 364, Hull:

>>> S = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> q = 0
>>> sigma = 0.2
>>> flag = 'c'
>>> gamma_calc = gamma(flag, S, K, t, r, sigma, q)
>>> # 0.0655453772525
>>> gamma_text_book = 0.066
>>> abs(gamma_calc - gamma_text_book) < .001
True
py_vollib.black_scholes_merton.greeks.analytical.rho(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton rho of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

The text book analytical formula does not multiply by .01, but in practice rho is defined as the change in price for each 1 percent change in r, hence we multiply by 0.01.

Example 17.7, page 368, Hull:

>>> S = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> q = 0
>>> sigma = 0.2
>>> flag = 'c'
>>> rho_calc = rho(flag, S, K, t, r, sigma, q)
>>> # 0.089065740988
>>> rho_text_book = 0.0891
>>> abs(rho_calc - rho_text_book) < .0001
True
py_vollib.black_scholes_merton.greeks.analytical.theta(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton theta of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

The text book analytical formula does not divide by 365, but in practice theta is defined as the change in price for each day change in t, hence we divide by 365.

Example 17.2, page 359, Hull:

>>> S = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> q = 0
>>> sigma = 0.2
>>> flag = 'c'
>>> annual_theta_calc = theta(flag, S, K, t, r, sigma, q) * 365
>>> # -4.30538996455
>>> annual_theta_text_book = -4.31
>>> abs(annual_theta_calc - annual_theta_text_book) < .01
True

Using the same inputs with a put. >>> S = 49 >>> K = 50 >>> r = .05 >>> t = 0.3846 >>> sigma = 0.2 >>> flag = ‘p’ >>> annual_theta_calc = theta(flag, S, K, t, r, sigma, q) * 365 >>> # -1.8530056722 >>> annual_theta_reference = -1.8530056722 >>> abs(annual_theta_calc - annual_theta_reference) < .000001 True

py_vollib.black_scholes_merton.greeks.analytical.vega(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton vega of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

The text book analytical formula does not multiply by .01, but in practice vega is defined as the change in price for each 1 percent change in IV, hence we multiply by 0.01.

Example 17.6, page 367, Hull:

>>> S = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> q = 0
>>> sigma = 0.2
>>> flag = 'c'
>>> vega_calc = vega(flag, S, K, t, r, sigma, q)
>>> # 0.121052427542
>>> vega_text_book = 0.121
>>> abs(vega_calc - vega_text_book) < .01
True

py_vollib.black_scholes_merton.greeks.numerical module

py_vollib.black_scholes_merton.greeks.numerical

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:© 2017 Gammon Capital LLC
license:MIT, see LICENSE for more details.

About LetsBeRational:

The source code of LetsBeRational resides at www.jaeckel.org/LetsBeRational.7z .

========================================================================================
Copyright © 2013-2014 Peter Jäckel.

Permission to use, copy, modify, and distribute this software is freely granted,
provided that this notice is preserved.

WARRANTY DISCLAIMER
The Software is provided "as is" without warranty of any kind, either express or implied,
including without limitation any implied warranties of condition, uninterrupted use,
merchantability, fitness for a particular purpose, or non-infringement.
========================================================================================
py_vollib.black_scholes_merton.greeks.numerical.delta(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton delta of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

py_vollib.black_scholes_merton.greeks.numerical.f(flag, S, K, t, r, sigma, b)
py_vollib.black_scholes_merton.greeks.numerical.gamma(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton gamma of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

py_vollib.black_scholes_merton.greeks.numerical.rho(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton rho of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

py_vollib.black_scholes_merton.greeks.numerical.test_analytical_vs_numerical()[source]

Test by comparing analytical and numerical values.

>>> flag='c'
>>> S=1000.0
>>> K=1000.0
>>> t=0.1
>>> r=0.05
>>> sigma=0.3
>>> q = 0.05
>>> sigma = 0.2
>>> flag = 'c'
>>> epsilon = 0.01
>>> v1 = delta(flag, S, K, t, r, sigma, q)
>>> v2 = adelta(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = gamma(flag, S, K, t, r, sigma, q)
>>> v2 = agamma(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = rho(flag, S, K, t, r, sigma, q)
>>> v2 = arho(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = vega(flag, S, K, t, r, sigma, q)
>>> v2 = avega(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = theta(flag, S, K, t, r, sigma, q)
>>> v2 = atheta(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True

Test PUT flag

>>> flag = 'p'
>>> v1 = delta(flag, S, K, t, r, sigma, q)
>>> v2 = adelta(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = gamma(flag, S, K, t, r, sigma, q)
>>> v2 = agamma(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = rho(flag, S, K, t, r, sigma, q)
>>> v2 = arho(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = vega(flag, S, K, t, r, sigma, q)
>>> v2 = avega(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
>>> v1 = theta(flag, S, K, t, r, sigma, q)
>>> v2 = atheta(flag, S, K, t, r, sigma, q)
>>> abs(v1-v2)<epsilon
True
py_vollib.black_scholes_merton.greeks.numerical.theta(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton theta of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

py_vollib.black_scholes_merton.greeks.numerical.vega(flag, S, K, t, r, sigma, q)[source]

Returns the Black-Scholes-Merton vega of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • S (float) – underlying asset price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
  • q (float) – annualized continuous dividend yield
Returns:

float

Module contents