GithubHelp home page GithubHelp logo

erkandem / calcbsimpvol Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 5.0 7.02 MB

Calculate Black Scholes Implied Volatility - Vectorwise

Home Page: https://erkandem.github.io/calcbsimpvol/

License: MIT License

Python 65.71% Makefile 1.38% Batchfile 2.07% JavaScript 6.22% MATLAB 24.62%
python options-pricing options finance implied-volatility black-scholes

calcbsimpvol's Introduction

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.

calcbsimpvol's People

Contributors

erkandem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

calcbsimpvol's Issues

add python version 3.9 and 3.10 to CI

Is your feature request related to a problem? Please describe.
the CI runs tests for 3.8 max. we could add 3.9+ to the pipeline

Describe the solution you'd like
Add 3.9+ to the CI runs

Describe alternatives you've considered
Using chicken bones as part of a Voodoo ritual.

Additional context

`q` and `cp` broadcasting smells

Hi Erkan,

I have been using your calcbsimpvol and it's awesome! A real life saver.

Now, while trying to use it one maturity at a time (to apply it to options
on futures, where the underlying is a specific futures contract), and
passing a 1-dim vector as 'cp', I get get an error in the broadcasting
operation line 254 linked to the shape of tau. I looked further and
wanted your opinion on whether the code had an an error in the function
"calcbsimpvol" lines 187 and 189? Shouldn't size(q) and size(cp) be
size(q)==1 and size(cp)==1?


and

Best,
Greg

On Tue, Mar 24, 2020 at 5:00 PM Erkan Demiralay [email protected]
wrote:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#1, or unsubscribe
https://github.com/notifications/unsubscribe-auth/AOCW7MY2KMRWS2OJOXEOCI3RJENOLANCNFSM4LS7MXXA
.

Originally posted by @gregdesu in #1 (comment)

Possibility for GPU-backed vector computation?

I was wondering if any performance optimization has been explored on the GPU for this. In particular, I was thinking that Python JAX would be very well suited to this optimization. I'm still familiarizing myself with the source/algorithm used here for the computation, but in addition to providing optional GPU-backed numpy for computing, JAX also provides:

jit(), for speeding up your code
grad(), for taking derivatives
vmap(), for automatic vectorization or batching.

in case any of these would be helpful here. A demo of computing the greeks via differentials on Black-Scholes from grad() is shown here, although I've found that the demo is further improved with jit() on the functions there.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.