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:
© 2023 Larry Richards
- 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.¶
Subpackages¶
Submodules¶
Package Contents¶
Functions¶
|
Calculate the d1 component of the Black PDE. |
|
Calculate the d2 component of the Black PDE. |
|
Calculate the price of a call using Black. (Python |
|
Calculate the price of a put using Black. (Python |
|
Calculate the (discounted) Black option price. |
Attributes¶
From John C. Hull, "Options, Futures and Other Derivatives," |
- N¶
From John C. Hull, “Options, Futures and Other Derivatives,” 7th edition, Chapter 16.8, page 342
- 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
- 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
- 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
- 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
- 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