py_vollib.ref_python.black.greeks package

Submodules

py_vollib.ref_python.black.greeks.analytical module

py_vollib.ref_python.black.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.

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.

py_vollib.ref_python.black.greeks.analytical.delta(flag, F, K, t, r, sigma)[source]

Returns the Black delta of an option.

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

float

>>> F = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> sigma = 0.2
>>> flag = 'c'
>>> v1 = delta(flag, F, K, t, r, sigma)
>>> v2 = 0.45107017482201828
>>> abs(v1-v2) < .000001
True
py_vollib.ref_python.black.greeks.analytical.gamma(flag, F, K, t, r, sigma)[source]

Returns the Black gamma of an option.

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

float

>>> F = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> sigma = 0.2
>>> flag = 'c'
>>> v1 = gamma(flag, F, K, t, r, sigma)
>>> # 0.0640646705882
>>> v2 = 0.0640646705882
>>> abs(v1-v2) < .000001
True
py_vollib.ref_python.black.greeks.analytical.rho(flag, F, K, t, r, sigma)[source]

Returns the Black rho of an option.

Parameters:
  • flag (str) – ‘c’ or ‘p’ for call or put.
  • F (float) – underlying futures price
  • K (float) – strike price
  • t (float) – time to expiration in years
  • r (float) – annual risk-free interest rate
  • sigma (float) – volatility
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.
==========================================================
>>> F = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> sigma = 0.2
>>> flag = 'c'
>>> v1 = rho(flag, F, K, t, r, sigma)
>>> v2 = -0.0074705380059582258
>>> abs(v1-v2) < .000001
True
>>> flag = 'p'
>>> v1 = rho(flag, F, K, t, r, sigma)
>>> v2 = -0.011243286001308292
>>> abs(v1-v2) < .000001
True
py_vollib.ref_python.black.greeks.analytical.theta(flag, F, K, t, r, sigma)[source]

Returns the Black theta of an option.

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

float

>>> F = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> sigma = 0.2
>>> flag = 'c'
>>> v1 = theta(flag, F, K, t, r, sigma)
>>> v2 = -0.00816236877462
>>> abs(v1-v2) < .000001
True
>>> flag = 'p'
>>> v1 = theta(flag, F, K, t, r, sigma)
>>> v2 = -0.00802799155312
>>> abs(v1-v2) < .000001
True
py_vollib.ref_python.black.greeks.analytical.vega(flag, F, K, t, r, sigma)[source]

Returns the Black vega of an option.

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

float

==========================================================
Note: 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.
==========================================================
>>> F = 49
>>> K = 50
>>> r = .05
>>> t = 0.3846
>>> sigma = 0.2
>>> flag = 'c'
>>> v1 = vega(flag, F, K, t, r, sigma)
>>> # 0.118317785624
>>> v2 = 0.118317785624
>>> abs(v1-v2) < .000001
True

py_vollib.ref_python.black.greeks.numerical module

py_vollib.ref_python.black.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.

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.

py_vollib.ref_python.black.greeks.numerical.delta(flag, F, K, t, r, sigma)[source]

Returns the Black delta of an option.

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

float

py_vollib.ref_python.black.greeks.numerical.f(flag, F, K, t, r, sigma, b)
py_vollib.ref_python.black.greeks.numerical.gamma(flag, F, K, t, r, sigma)[source]

Returns the Black gamma of an option.

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

float

py_vollib.ref_python.black.greeks.numerical.rho(flag, F, K, t, r, sigma)[source]

Returns the Black rho of an option.

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

float

py_vollib.ref_python.black.greeks.numerical.test()[source]

Tests by comparing the analytical and numerical greek values.

>>> S =  49
>>> K = 50 
>>> r = .05
>>> t = 0.3846
>>> sigma = 0.2
>>> flag = 'c'
>>> epsilon = .0001
>>> v1 = delta(flag, S, K, t, r, sigma)
>>> v2 = adelta(flag, S, K, t, r, sigma)
>>> abs(v1-v2)<epsilon
True
>>> v1 = gamma(flag, S, K, t, r, sigma)
>>> v2 = agamma(flag, S, K, t, r, sigma)
>>> abs(v1-v2)<epsilon
True
>>> v1 = rho(flag, S, K, t, r, sigma)
>>> v2 = arho(flag, S, K, t, r, sigma)
>>> abs(v1-v2)<epsilon
True
>>> v1 = vega(flag, S, K, t, r, sigma)
>>> v2 = avega(flag, S, K, t, r, sigma)
>>> abs(v1-v2)<epsilon
True
>>> v1 = theta(flag, S, K, t, r, sigma)
>>> v2 = atheta(flag, S, K, t, r, sigma)
>>> abs(v1-v2)<epsilon
True
py_vollib.ref_python.black.greeks.numerical.theta(flag, F, K, t, r, sigma)[source]

Returns the Black theta of an option.

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

float

py_vollib.ref_python.black.greeks.numerical.vega(flag, F, K, t, r, sigma)[source]

Returns the Black vega of an option.

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

float

Module contents