Skip to main content

Build Status

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

ToDos

  • make the code compatible with Python 2
  • make it PyPy compatible

References

  1. Li, 2006, "You Don't Have to Bother Newton for Implied Volatility"

    http://papers.ssrn.com/sol3/papers.cfm?abstract_id=952727

  2. 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.

Metadata

Release files for calcbsimpvol 1.14.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for calcbsimpvol 1.14.0
File Size Uploaded Provenance
calcbsimpvol-1.14.0.tar.gz 13.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for calcbsimpvol 1.14.0
File Interpreter ABI Platform
calcbsimpvol-1.14.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / calcbsimpvol-1.14.0.tar.gz

Download URL calcbsimpvol-1.14.0.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ed007fa705c5c88c66d851198e6adb873a9a1472b8795365631b9b514bb1425d
BLAKE2b-256 checksum
How to use checksums
30e51ea9f1ea7de093a898cb8471827d31199b37f3b4db9992e5a19f1cc4aa85
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / calcbsimpvol-1.14.0-py3-none-any.whl

Download URL calcbsimpvol-1.14.0-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4be70fbab974796340a4b2fd53323666925c78b29666234df4227f8078a94b68
BLAKE2b-256 checksum
How to use checksums
f150ea4c122ee7d41906cb7a718c9778c394c5073d98a3d5b5958a8d7da10b33
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for calcbsimpvol 1.14.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page