py_vollib.black.implied_volatility

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

implied_volatility_of_discounted_option_price(...)

Calculate the implied volatility of the Black option price

implied_volatility(discounted_option_price, F, K, r, ...)

Calculate the implied volatility of the Black option price

normalised_implied_volatility(beta, x, flag)

Calculate the normalised Black implied volatility,

normalised_implied_volatility_limited_iterations(beta, ...)

Calculate the normalised Black implied volatility,

implied_volatility_of_undiscounted_option_price(...)

Calculate the implied volatility of the undiscounted Black option price

implied_volatility_of_undiscounted_option_price_limited_iterations(...)

Calculate implied volatility of the undiscounted Black

implied_volatility_of_discounted_option_price(discounted_option_price, F, K, r, t, flag)[source]

Calculate the implied volatility of the Black option price

Parameters:
  • discounted_option_price (float) – discounted Black price of a futures option

  • F (float) – underlying futures price

  • K (float) – strike price

  • r (float) – the risk-free interest rate

  • t (float) – time to expiration in years

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

>>> F = 100
>>> K = 100
>>> sigma = .2
>>> flag = 'c'
>>> t = .5
>>> r = .02
>>> discounted_call_price = black(flag, F, K, t, r, sigma)
>>> iv = implied_volatility_of_discounted_option_price(
... discounted_call_price, F, K, r, t, flag)
>>> expected_price = 5.5811067246
>>> expected_iv = 0.2
>>> abs(expected_price - discounted_call_price) < 0.00001
True
>>> abs(expected_iv - iv) < 0.00001
True
implied_volatility(discounted_option_price, F, K, r, t, flag)[source]

Calculate the implied volatility of the Black option price

Parameters:
  • discounted_option_price (float) – discounted Black price of a futures option

  • F (float) – underlying futures price

  • K (float) – strike price

  • r (float) – the risk-free interest rate

  • t (float) – time to expiration in years

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

>>> F = 100
>>> K = 100
>>> sigma = .2
>>> flag = 'c'
>>> t = .5
>>> r = .02
>>> discounted_call_price = black(flag, F, K, t, r, sigma)
>>> iv = implied_volatility(
... discounted_call_price, F, K, r, t, flag)
>>> expected_price = 5.5811067246
>>> expected_iv = 0.2
>>> abs(expected_price - discounted_call_price) < 0.00001
True
>>> abs(expected_iv - iv) < 0.00001
True
normalised_implied_volatility(beta, x, flag)[source]

Calculate the normalised Black implied volatility, a time invariant transformation of Black implied volatility.

Keyword arguments:

Parameters:
  • x (float) – ln(F/K) where K is the strike price, and F is the futures price

  • beta (float) – the normalized Black price

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

>>> beta_call = normalised_black(0.0, 0.2, 'c')
>>> beta_put = normalised_black(0.1,0.23232323888,'p')
>>> normalized_b76_iv_call = normalised_implied_volatility(beta_call, 0.0, 'c')
>>> normalized_b76_iv_put = normalised_implied_volatility(beta_put, 0.1, 'p')
>>> expected_price = 0.0796556745541
>>> expected_iv = 0.2
>>> abs(expected_price - beta_call) < 0.00001
True
>>> abs(expected_iv - normalized_b76_iv_call) < 0.00001
True
>>> expected_price = 0.0509710222785
>>> expected_iv = 0.23232323888
>>> abs(expected_price - beta_put) < 0.00001
True
>>> abs(expected_iv - normalized_b76_iv_put) < 0.00001
True
normalised_implied_volatility_limited_iterations(beta, x, flag, N)[source]

Calculate the normalised Black implied volatility, with limited iterations.

Parameters:
  • x (float) – ln(F/K) where K is the strike price, and F is the futures price

  • beta (float) – the normalized Black price

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

>>> beta_call = normalised_black(0.0, 0.2, 'c')
>>> beta_put = normalised_black(0.1,0.23232323888,'p')
>>> normalized_b76_iv_call = normalised_implied_volatility_limited_iterations(beta_call, 0.0, 'c',1)
>>> normalized_b76_iv_put = normalised_implied_volatility_limited_iterations(beta_put, 0.1, 'p',1)
>>> expected_price = 0.0796556745541
>>> expected_iv = 0.2
>>> abs(expected_price - beta_call) < 0.00001
True
>>> abs(expected_iv - normalized_b76_iv_call) < 0.00001
True
>>> expected_price = 0.0509710222785
>>> expected_iv = 0.23232323888
>>> abs(expected_price - beta_put) < 0.00001
True
>>> abs(expected_iv - normalized_b76_iv_put) < 0.00001
True
implied_volatility_of_undiscounted_option_price(undiscounted_option_price, F, K, t, flag)[source]

Calculate the implied volatility of the undiscounted Black option price

Parameters:
  • undiscounted_option_price (float) – undiscounted Black price of a futures option

  • F (float) – underlying futures price

  • K (float) – strike price

  • t (float) – time to expiration in years

>>> F = 100
>>> K = 100
>>> sigma = .2
>>> flag = 'c'
>>> t = .5
>>> undiscounted_call_price = undiscounted_black(F, K, sigma, t, flag)
>>> iv = implied_volatility_of_undiscounted_option_price(
... undiscounted_call_price, F, K, t, flag)
>>> expected_price = 5.6371977797
>>> expected_iv = 0.2
>>> abs(expected_price - undiscounted_call_price) < 0.00001
True
>>> abs(expected_iv - iv) < 0.00001
True
implied_volatility_of_undiscounted_option_price_limited_iterations(undiscounted_option_price, F, K, t, flag, N)[source]

Calculate implied volatility of the undiscounted Black option price with limited iterations.

Parameters:
  • undiscounted_option_price (float) – undiscounted Black price of a futures option

  • F (float) – underlying futures price

  • K (float) – strike price

  • t (float) – time to expiration in years

>>> F = 100
>>> K = 100
>>> sigma = .232323232
>>> flag = 'c'
>>> t = .5
>>> price = undiscounted_black(F, K, sigma, t, flag)
>>> iv = implied_volatility_of_undiscounted_option_price_limited_iterations(
... price, F, K, t, flag, 1)
>>> expected_price = 6.54635543387
>>> expected_iv = 0.232323232
>>> abs(expected_price - price) < 0.00001
True
>>> abs(expected_iv - iv) < 0.00001
True