vollib.black package

Submodules

vollib.black.implied_volatility module

vollib.black.implied_volatility

A library for option pricing, implied volatility, and greek calculation. vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.

copyright:© 2015 Iota Technologies Pte Ltd
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.
======================================================================================
vollib.black.implied_volatility.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)
>>> print discounted_call_price, iv
5.5811067246 0.2
vollib.black.implied_volatility.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)
>>> print undiscounted_call_price, iv
5.6371977797 0.2
vollib.black.implied_volatility.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)
>>> print price, iv    
6.54635543387 0.232323232
vollib.black.implied_volatility.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')
>>> print beta_call, normalized_b76_iv_call
0.0796556745541 0.2
>>> print beta_put, normalized_b76_iv_put
0.0509710222785 0.23232323888
vollib.black.implied_volatility.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)
>>> print beta_call, normalized_b76_iv_call
0.0796556745541 0.2
>>> print beta_put, normalized_b76_iv_put
0.0509710222785 0.23232323888

Module contents

vollib.black

Copyright © 2015

A library for option pricing, implied volatility, and greek calculation. vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.

copyright:© 2015 Iota Technologies Pte Ltd
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.
======================================================================================
vollib.black.black(flag, F, K, t, r, sigma)[source]

Calculate the (discounted) Black option price.

Parameters:
  • F (float) – underlying futures price
  • K (float) – strike price
  • sigma (float) – annualized standard deviation, or volatility
  • t (float) – time to expiration in years
>>> F = 100
>>> K = 100
>>> sigma = .2
>>> flag = 'c'
>>> r = .02
>>> t = .5
>>> black(flag, F, K, t, r, sigma)
5.5811067246048118
vollib.black.black_call(F, K, t, r, sigma)[source]

Calculate the price of a call using Black. (Python implementation for reference.)

Parameters:
  • F (float) – underlying futures price
  • K (float) – strike price
  • sigma (float) – annualized standard deviation, or volatility
  • t (float) – time to expiration in years
  • r (float) – risk-free interest rate

Hull, page 343, example 16.7

>>> F = 620
>>> K = 600
>>> r = .05
>>> sigma = .2
>>> t = 0.5
>>> calculated_value = black_call(F, K, t, r, sigma)
>>> #44.1868533121
>>> text_book_value = 44.19
>>> abs(calculated_value - text_book_value) < .01
True
vollib.black.black_put(F, K, t, r, sigma)[source]

Calculate the price of a put using Black. (Python implementation for reference.)

Parameters:
  • F (float) – underlying futures price
  • K (float) – strike price
  • sigma (float) – annualized standard deviation, or volatility
  • t (float) – time to expiration in years
  • r (float) – risk-free interest rate

Hull, page 338, example 16.6

>>> F = 20
>>> K = 20
>>> r = .09
>>> sigma = .25
>>> t = 4/12.0
>>> calculated_value = black_put(F, K, t, r, sigma)
>>> # 1.11664145656
>>> text_book_value = 1.12
>>> abs(calculated_value - text_book_value) < .01
True
vollib.black.d1(F, K, t, r, sigma)[source]

Calculate the d1 component of the Black PDE.

Parameters:
  • F (float) – underlying futures price
  • K (float) – strike price
  • sigma (float) – annualized standard deviation, or volatility
  • t (float) – time to expiration in years
  • r (float) – risk-free interest rate

Doctest using Hull, page 343, example 16.6

>>> F = 20
>>> K = 20
>>> r = .09
>>> t = 4/12.0
>>> sigma = 0.25
>>> calculated_value = d1(F, K, t, r, sigma) 
>>> #0.0721687836487
>>> text_book_value = 0.07216
>>> abs(calculated_value - text_book_value) < .00001
True
vollib.black.d2(F, K, t, r, sigma)[source]

Calculate the d2 component of the Black PDE.

Parameters:
  • F (float) – underlying futures price
  • K (float) – strike price
  • sigma (float) – annualized standard deviation, or volatility
  • t (float) – time to expiration in years
  • r (float) – risk-free interest rate

Hull, page 343, example 16.6

>>> F = 20
>>> K = 20
>>> r = .09
>>> t = 4/12.0
>>> sigma = 0.25
>>> calculated_value = d2(F, K, t, r, sigma) 
>>> #-0.0721687836487
>>> text_book_value = -0.07216
>>> abs(calculated_value - text_book_value) < .00001
True
vollib.black.normalised_black(x, s, flag)[source]

Calculate the normalised Black value, a time invariant transformation of the Black pricing formula.

Parameters:
  • x (float) – ln(F/K) where K is the strike price, and F is the futures price
  • s (float) – volatility times the square root of time to expiration
  • flag (str) – ‘p’ or ‘c’ for put or call
>>> F = 100.
>>> K = 95.
>>> x = log(F/K)
>>> t = 0.5
>>> v = 0.3
>>> s = v * sqrt(t)    
>>> normalised_black(x,s,'p')
0.061296663817558904
>>> normalised_black(x,s,'c')
0.11259558142181655
vollib.black.test_python_vs_c_values()[source]
>>> F = 100
>>> K = 90
>>> sigma = .2
>>> flag = 'c'
>>> r = .02
>>> t = .5
>>> epsilon = .000000001
>>> v1 = black('c', F, K, t, r, sigma)
>>> v2 = black_call(F, K, t, r, sigma)
>>> print abs(v1-v2) < epsilon
True
>>> v1 = black('p', F, K, t, r, sigma)
>>> v2 = black_put(F, K, t, r, sigma)
>>> print abs(v1-v2) < epsilon
True
vollib.black.undiscounted_black(F, K, sigma, t, flag)[source]

Calculate the undiscounted Black option price.

Parameters:
  • F (float) – underlying futures price
  • K (float) – strike price
  • sigma (float) – annualized standard deviation, or volatility
  • t (float) – time to expiration in years
>>> F = 100
>>> K = 100
>>> sigma = .2
>>> flag = 'c'
>>> t = .5
>>> undiscounted_black(F, K, sigma, t, flag)
5.637197779701664