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:

© 2023 Larry Richards

license:

MIT, see LICENSE for more details.

Module Contents

Functions

delta(flag, F, K, t, r, sigma)

Returns the Black delta of an option.

theta(flag, F, K, t, r, sigma)

Returns the Black theta of an option.

vega(flag, F, K, t, r, sigma)

Returns the Black vega of an option.

rho(flag, F, K, t, r, sigma)

Returns the Black rho of an option.

gamma(flag, F, K, t, r, sigma)

Returns the Black gamma of an option.

test()

Tests by comparing the analytical and numerical greek values.

Attributes

f

f
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

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

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

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

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

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