Skip to main content

A fast, vectorized approach to calculating Implied Volatility and Greeks using the Black, Black-Scholes and Black-Scholes-Merton pricing.

Project description

py_vollib_vectorized

Introduction

The py_vollib_vectorized package makes pricing thousands of option contracts and calculating greeks fast and effortless. It is built on top of the py_vollib library. Upon import, it will automatically patch the corresponding py_vollib functions so as to support vectorization. Inputs can then be passed as floats, tuples, lists, numpy.array, or pandas.Series. Automatic broadcasting is performed on the inputs.

On top of vectorization, modifications to py_vollib include additional numba speedups; as such, numba is required. These speedups make py_vollib_vectorized the fastest library for pricing option contracts.

See the documentation for more details.

Installation

pip install py_vollib_vectorized

Requirements

  • Written for Python 3.5+
  • Requires py_vollib, numba, numpy, pandas, scipy

Code samples

The library can be used in two ways. Upon import, it monkey-patches (i.e. replaces) the corresponding functions in py_vollib.

As a more versatile alternative, users that would prefer to work with a dedicated option pricing API can make use of the utility functions provided by the library.

Patching py_vollib

# The usual py_vollib syntax

import numpy as np
import pandas as pd

import py_vollib.black_scholes
flag = 'c'  # 'c' for call, 'p' for put
S = 100  # Underlying asset price
K = 90  # Strike
t = 0.5  # (Annualized) time-to-expiration
r = 0.01  # Interest free rate
iv = 0.2  # Implied Volatility

option_price = py_vollib.black_scholes.black_scholes(flag, S, K, t, r, iv)  # 12.111581435

# This library keeps the same syntax, but you can pass as input any iterable of values.
# This includes list, tuple, numpy.array, pd.Series, pd.DataFrame (with only a single column).
# Note that you must pass a value for each contract as *no broadcasting* is done on the inputs.


# Patch the original py_vollib library by importing py_vollib_vectorized
import py_vollib_vectorized  # The same functions now accept vectors as input!

# Note that the input arguments are broadcasted.
# You can specify ints, floats, tuples, lists, numpy arrays or Series.

flag = ['c', 'p']  # 'c' for call, 'p' for put
S = (100, 100)  # Underlying asset prices
K = [90]  # Strikes
t = pd.Series([0.5, 0.6])  # (Annualized) times-to-expiration
r = np.array([0.01])  # Interest free rates
iv = 0.2  # Implied Volatilities

option_price = py_vollib.black_scholes.black_scholes(flag, S, K, t, r, iv, return_as='array')  
# array([12.11158143,  2.02418536])

Utility functions

We also define other utility functions to get all contract prices, implied volatilities, and greeks in a single call.

import pandas as pd
from py_vollib_vectorized import price_dataframe, get_all_greeks

# Using the data above, we can calculate all contracts greeks in a single call
greeks = get_all_greeks(flag, S, K, t, r, iv, model='black_scholes', return_as='dict')

#   {'delta': array([ 0.80263679, -0.21293214]),
#    'gamma': array([0.0196385, 0.01875498]),
#    'theta': array([-0.01263557, -0.00964498]),
#    'rho': array([0.34073321, -0.13994668]),
#    'vega': array([0.19626478, 0.22493816])}

# We can also price a dataframe easily by specifying a dataframe and the corresponding columns

df = pd.DataFrame()
df['Flag'] = ['c', 'p']
df['S'] = 95
df['K'] = [100, 90]
df['T'] = 0.2
df['R'] = 0.2
df['IV'] = 0.2
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                     riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
#   Price       delta       gamma       theta       rho        vega
#   2.895588    0.467506    0.046795    -0.045900   0.083035   0.168926
#   0.611094    -0.136447   0.025739    -0.005335   -0.027151  0.092838

See the documentation for more details.

Benchmarking

Compared to looping through contracts or to using built-in pandas functionality, this library is very memory efficient and scales fast and well to a large number of contracts.

Performance of the py_vollib_vectorized libary

Acknowledgements

This library optimizes the py_vollib codebase, itself built upon Peter Jäckel's Let's be rational methodology.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_vollib_vectorized-0.1.1.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

py_vollib_vectorized-0.1.1-py3-none-any.whl (30.5 kB view details)

Uploaded Python 3

File details

Details for the file py_vollib_vectorized-0.1.1.tar.gz.

File metadata

  • Download URL: py_vollib_vectorized-0.1.1.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.8

File hashes

Hashes for py_vollib_vectorized-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5d10d2eb94d462ae4a0edabe33c38d9fec47e8f432b48d63ae34cddd73b8d1a8
MD5 9bb0609532db2845b202095e1ced7411
BLAKE2b-256 6e0bf29a131bf3a46a0413fad1729ec39d21f7be98f3f58de9833b735e806a65

See more details on using hashes here.

File details

Details for the file py_vollib_vectorized-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: py_vollib_vectorized-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 30.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.8

File hashes

Hashes for py_vollib_vectorized-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8ebd2079dd52fd9cfef5a9a7afe83f8876d54218abc711990fd97d125b3e1d9
MD5 60c2fb9e617be8d5c7ae69e90dddd033
BLAKE2b-256 e0e657012148e9d899505ac1d99abe5ce0d22ff8c233c462a9c57b9c6388683a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page