Created
October 16, 2019 16:31
-
-
Save rwev/d6c090ff73f642ee46e8e61022574e19 to your computer and use it in GitHub Desktop.
Test speed of BAW option pricing calculations in Cython.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
from setuptools import setup | |
except ImportError: | |
from distutils.core import setup | |
import pyximport; pyximport.install() | |
import baw, cybaw | |
from time import time | |
from collections import namedtuple | |
from random import choice, uniform | |
from pprint import pprint | |
def test(): | |
bounds = namedtuple('bounds', ['min', 'max']) | |
pricing_inputs = namedtuple('pricing_inputs', ['greek', 'type','spot', 'strike', 'years', 'interest', 'carry', 'volatility']) | |
OPTIONS_TO_PRICE = 10000 | |
greek_choice = ('p', 'd', 'g', 'v', 't') | |
type_choice = ('c', 'p') | |
spot_bounds = bounds(min = 95, max = 105) | |
strike_bounds = bounds(min = 90, max = 110) | |
year_bounds = bounds(min = 1 / 12.0, max = 4.0) | |
interest_bounds = bounds(min = 0.000, max = 0.025) | |
carry_bounds = bounds(min = 0.000, max = 0.000) | |
volatility_bounds = bounds(min = 0.025, max = 0.075) | |
to_price = [] | |
for i in range(OPTIONS_TO_PRICE): | |
t_greek = choice(greek_choice) | |
t_type = choice(type_choice) | |
t_spot = uniform(spot_bounds.min, spot_bounds.max) | |
t_strike = uniform(strike_bounds.min, strike_bounds.max) | |
t_years = uniform(year_bounds.min, year_bounds.max) | |
t_interest = uniform(interest_bounds.min, interest_bounds.max) | |
t_carry = uniform(carry_bounds.min, carry_bounds.max) | |
t_volatility = uniform(volatility_bounds.min, volatility_bounds.max) | |
t_inputs = pricing_inputs( | |
greek = t_greek, | |
type = t_type, | |
spot = t_spot, | |
strike = t_strike, | |
years = t_years, | |
interest = t_interest, | |
carry = t_carry, | |
volatility = t_volatility | |
) | |
to_price.append(t_inputs) | |
t = time() | |
for pinp in to_price: | |
# pprint(dict(pinp._asdict())) | |
baw.greeks_WhaleyApproximation( | |
pinp.greek, | |
pinp.type, | |
pinp.spot, | |
pinp.strike, | |
pinp.years, | |
pinp.interest, | |
pinp.carry, | |
pinp.volatility | |
) | |
pure_py_ms = 1000* (time() - t) | |
print "\nPYTHON BAW = %0.0f ms" % (pure_py_ms,) | |
t = time() | |
for pinp in to_price: | |
cybaw.greeks_WhaleyApproximation( | |
pinp.greek, | |
pinp.type, | |
pinp.spot, | |
pinp.strike, | |
pinp.years, | |
pinp.interest, | |
pinp.carry, | |
pinp.volatility | |
) | |
cython_ms = 1000* (time() - t) | |
print "\nCYTHON CYBAW = %0.0f ms" % (cython_ms,) | |
print "\nSPEEDUP = %0.0f\n" % (pure_py_ms / cython_ms) | |
if __name__ == '__main__': | |
test() | |
# import cProfile | |
# cProfile.run('test()', sort='time') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment