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:

© 2023 Larry Richards

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.
========================================================================================

Module Contents

Functions

delta(flag, S, K, t, r, sigma, q)

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

theta(flag, S, K, t, r, sigma, q)

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

vega(flag, S, K, t, r, sigma, q)

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

rho(flag, S, K, t, r, sigma, q)

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

gamma(flag, S, K, t, r, sigma, q)

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

test_analytical_vs_numerical()

Test by comparing analytical and numerical values.

Attributes

f

f
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

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

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

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

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

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