Calculate Black Scholes Implied Volatility - Vectorwise
Project description
calcbsimpvol
Calculate Black-Scholes Implied Volatility - Vectorwise
:)
native python code:)
lightweight footprint:)
sample data included:(
not suited for single / low number of options:(
code reads un-pythonic:(
not yet thoroughly tested
Getting started
Requirements
- Python 3.x (currently) or PyPy3
- NumPy
- SciPy
- (MatPlotLib to visualize results in some examples)
Installation
While the code consists of single digit functions,
I recommend using the pip install
way to get the code.
That way you would take advantage of bug fixes, updates,
and possible extensions.
$ pip install calcbsimpvol
Example
Pass your args
bundled in a dict
.
from calcbsimpvol import calcbsimpvol
import numpy as np
S = np.asarray(100)
K_value = np.arange(40, 160, 25)
K = np.ones((np.size(K_value), 1))
K[:, 0] = K_value
tau_value = np.arange(0.25, 1.01, 0.25)
tau = np.ones((np.size(tau_value), 1))
tau[:, 0] = tau_value
r = np.asarray(0.01)
q = np.asarray(0.03)
cp = np.asarray(1)
P = [[59.35, 34.41, 10.34, 0.50, 0.01],
[58.71, 33.85, 10.99, 1.36, 0.14],
[58.07, 33.35, 11.50, 2.12, 0.40],
[57.44, 32.91, 11.90, 2.77, 0.70]]
P = np.asarray(P)
[K, tau] = np.meshgrid(K, tau)
sigma = calcbsimpvol(dict(cp=cp, P=P, S=S, K=K, tau=tau, r=r, q=q))
print(sigma)
# [[ nan, nan, 0.20709362, 0.21820954, 0.24188675],
# [ nan, 0.22279836, 0.20240934, 0.21386148, 0.23738982],
# [ nan, 0.22442837, 0.1987048 , 0.21063506, 0.23450013],
# [ nan, 0.22188111, 0.19564657, 0.20798285, 0.23045406]]
More usage examples are available in example3.py (additional sample data required which is available at GitHub Repo
Performance
Design a test.
Get the results you want.
k_max = 10
(default)tolerance = 10E-12
(default)- linear regression steps are commented out (default)
# assuming you did install it already
git clone https://github.com/erkandem/calcbsimpvol.git
cd calcbsimpvol
python examples/example3.py --steps 100 --mode reference
- 15 µs per option
- 41 ms per surface
tested with 3.6, 3.7 and PyPy3
matlab -nodisplay -nosplash -nodesktop -r "run('mlb_reference_example.m');"
- 12 µs per option
- 34 ms per surface
Obviously, these values are per core (i5 4210U 1.7 GHz).
Notes
Good Python code reads like a novel. Right? So should math. I preferred short math-like variable names in this case. That makes the code less readable compared to other Python code but the docstrings should make up for the lack of readability.
Originally, I left the camelCase function name and spelling in place but eventually got annoyed.
calcbsimpvol it is
Code Origin
- first thought of by Li (2006) (see References)
- implemented and published by Mark Whirdy as MATLAB .m-code (see References)
- numpyified from
.m
to.py
by me
Contact
- email: erkan.dem@pm.me
- documentation: erkandem.github.io/calcbsimpvol/
- source: github.com/erkandem/calcbsimpvol
- issues: github.com/erkandem/calcbsimpvol/issues
ToDos
- make the code compatible with
Python 2
- make it
PyPy
compatible
References
-
Li, 2006, "You Don't Have to Bother Newton for Implied Volatility"
-
MATLAB source code available at:
https://www.mathworks.com/matlabcentral/fileexchange/41473-calcbsimpvol-cp-p-s-k-t-r-q
License
The included Python code is licensed under MIT
License
The Code by Mark Whirdy is licensed under MIT
License
The translation is not related or endorsed by the original author.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file calcbsimpvol-1.14.0.tar.gz
.
File metadata
- Download URL: calcbsimpvol-1.14.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed007fa705c5c88c66d851198e6adb873a9a1472b8795365631b9b514bb1425d |
|
MD5 | e00e7f504016017969df381ebcf913c1 |
|
BLAKE2b-256 | 30e51ea9f1ea7de093a898cb8471827d31199b37f3b4db9992e5a19f1cc4aa85 |
File details
Details for the file calcbsimpvol-1.14.0-py3-none-any.whl
.
File metadata
- Download URL: calcbsimpvol-1.14.0-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4be70fbab974796340a4b2fd53323666925c78b29666234df4227f8078a94b68 |
|
MD5 | 248ad0f5f2359a717433ddbdd5d1208b |
|
BLAKE2b-256 | f150ea4c122ee7d41906cb7a718c9778c394c5073d98a3d5b5958a8d7da10b33 |