py_vollib.ref_python.black package

Submodules

py_vollib.ref_python.black.implied_volatility module

py_vollib.ref_python.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:© 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.implied_volatility.implied_volatility(price, F, K, r, t, flag)[source]

Returns the Black delta of an option.

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

float

>>> F = 101.0
>>> K = 102.0
>>> t = .5
>>> r = .01
>>> flag = 'p'
>>> sigma_in = 0.2
>>> price = black(flag, F, K, t, r, sigma_in)
>>> expected_price = 6.20451158097
>>> abs(expected_price - price) < 0.00001
True
>>> sigma_out = implied_volatility(price, F, K, r, t, flag)
>>> sigma_in == sigma_out or abs(sigma_in - sigma_out) < 0.00001
True
>>> 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_discounted_call_price = 5.5811067246
>>> expected_iv = 0.2
>>> abs(expected_discounted_call_price - discounted_call_price) < 0.00001
True
>>> abs(expected_iv - iv) < 0.00001
True

Module contents

py_vollib.ref_python.black

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.N = <bound method rv_continuous.cdf of <scipy.stats._continuous_distns.norm_gen object>>

From John C. Hull, “Options, Futures and Other Derivatives,” 7th edition, Chapter 16.8, page 342

py_vollib.ref_python.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
>>> expected = 5.5811067246048118
>>> actual = black(flag, F, K, t, r, sigma)
>>> abs(expected - actual) < 1e-12
True
py_vollib.ref_python.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
py_vollib.ref_python.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
py_vollib.ref_python.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
py_vollib.ref_python.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