implied volatility using python's pandas library - nag

26
Experts in numerical algorithms and HPC services Implied Volatility using Python’s Pandas Library Brian Spector Thalesians Meetup London 15 th January 2014

Upload: others

Post on 11-Feb-2022

28 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implied Volatility using Python's Pandas Library - Nag

Experts in numerical algorithms and HPC services

Implied Volatility using Python’s Pandas Library

Brian Spector

Thalesians Meetup London

15th January 2014

Page 2: Implied Volatility using Python's Pandas Library - Nag

2 Numerical Excellence Commercial in Confidence

Overview

β€’ Motivation

β€’ Python

β€’ Pandas

β€’ Implied Volatility

– Timings in python

– Different Volatility Curves

– Fitting data points

Page 3: Implied Volatility using Python's Pandas Library - Nag

3 Numerical Excellence Commercial in Confidence

Python

β€’ Dynamically typed language

β€’ Uses white spaces (as oppose to brackets) for control statements.

β€’ Has grown in popularity:

β€’ http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

Programming Language

2014 2009 2004 1999

Python Ranking 8 6 11 22

Page 4: Implied Volatility using Python's Pandas Library - Nag

4 Numerical Excellence Commercial in Confidence

Python

β€’ Why use python?

– Cheap

– Easy to learn

– Powerful

Page 5: Implied Volatility using Python's Pandas Library - Nag

5 Numerical Excellence Commercial in Confidence

Python

β€’ Why use python?

– Cheap

– Easy to learn

– Powerful

β€’ Why use python over R?

– β€œI would rather do math in a programming language than programming in a math language.”

Page 6: Implied Volatility using Python's Pandas Library - Nag

6 Numerical Excellence Commercial in Confidence

Python

β€’ What python has:

– Many built-in powerful packages

– OO programming

β€’ Classes

β€’ Base + Derived Classes

– Plotting

β€’ What python does not have:

– Operator Overloading

– Multiple constructors

– Speed

– Pointers

– ???

Page 7: Implied Volatility using Python's Pandas Library - Nag

7 Numerical Excellence Commercial in Confidence

Numpy

β€’ Has made numerical computing much easier in recent years.

β€’ Numpy matrices / arrays

β€’ Numpy.linalg

β€’ Behind many of these functions are LAPACK + BLAS!

Page 8: Implied Volatility using Python's Pandas Library - Nag

8 Numerical Excellence Commercial in Confidence

Scipy

β€’ Special functions (scipy.special)

β€’ Integration (scipy.integrate)

β€’ Optimization (scipy.optimize)

β€’ Interpolation (scipy.interpolate)

β€’ Fourier Transforms (scipy.fftpack)

β€’ Signal Processing (scipy.signal)

β€’ Linear Algebra (scipy.linalg)

β€’ Sparse Eigenvalue Problems with ARPACK

β€’ Compressed Sparse Graph Routines scipy.sparse.csgraph

β€’ Spatial data structures and algorithms (scipy.spatial)

β€’ Statistics (scipy.stats)

β€’ Multidimensional image processing (scipy.ndimage)

Page 9: Implied Volatility using Python's Pandas Library - Nag

9 Numerical Excellence Commercial in Confidence

nag4py

β€’ Built on top of NAG C Library + Documentation

β€’ 1600 NAG functions easily accessible from python

β€’ 25 examples programs to help users call NAG functions

from nag4py.c05 import c05ayc

from nag4py.util import NagError,Nag_Comm

Page 10: Implied Volatility using Python's Pandas Library - Nag

10 Numerical Excellence Commercial in Confidence

Pandas

β€’ Data Analysis Package

β€’ Many nice built in functions

β€’ Common tools:

– Series / DataFrame

– Reading + Writing CSVs

– Indexing, missing data, reshaping

– Common time series functionality

(Examples)

Page 11: Implied Volatility using Python's Pandas Library - Nag

11 Numerical Excellence Commercial in Confidence

Implied Volatility

β€’ Black Scholes Formula for pricing a call/put option is a function of 6 variables:

– 𝐢 𝑆0, 𝐾, 𝑇, 𝜎, π‘Ÿ, 𝑑 = 𝑆0𝑁 𝑑1 βˆ’ πΎπ‘’βˆ’π‘Ÿπ‘‡π‘ 𝑑2

β€’ Where

– 𝑑1,2 =1

𝜎 𝑇𝑙𝑛

𝑆

𝐾+ 𝑇 π‘Ÿ Β±

𝜎2

2

– 𝑁 π‘₯ = Standard Normal CDF

Page 12: Implied Volatility using Python's Pandas Library - Nag

12 Numerical Excellence Commercial in Confidence

Implied Volatility

β€’ We can observe the following in the market:

β€’ 𝐢 𝑆0, 𝐾, 𝑇, 𝜎, π‘Ÿ, 𝑑 = 𝐢

β€’ But what is ?

β€’ πœŽπ‘–π‘šπ‘ β†’ 𝐢𝐡𝑆 𝑆0, 𝐾, 𝑇, πœŽπ‘–π‘šπ‘, π‘Ÿ, 𝑑 = π‘€π‘Žπ‘Ÿπ‘˜π‘’π‘‘ π‘ƒπ‘Ÿπ‘–π‘π‘’

Page 13: Implied Volatility using Python's Pandas Library - Nag

13 Numerical Excellence Commercial in Confidence

Implied Volatility

β€’ We can observe the following in the market:

β€’ 𝐢 𝑆0, 𝐾, 𝑇, 𝜎, π‘Ÿ, 𝑑 = 𝐢

β€’ But what is ?

β€’ πœŽπ‘–π‘šπ‘ β†’ 𝐢𝐡𝑆 𝑆0, 𝐾, 𝑇, πœŽπ‘–π‘šπ‘, π‘Ÿ, 𝑑 = π‘€π‘Žπ‘Ÿπ‘˜π‘’π‘‘ π‘ƒπ‘Ÿπ‘–π‘π‘’

β€’ Does πœŽπ‘–π‘šπ‘exist?

Page 14: Implied Volatility using Python's Pandas Library - Nag

14 Numerical Excellence Commercial in Confidence

Implied Volatility

β€’ We can observe the following in the market:

β€’ 𝐢 𝑆0, 𝐾, 𝑇, 𝜎, π‘Ÿ, 𝑑 = 𝐢

β€’ But what is ?

β€’ πœŽπ‘–π‘šπ‘ β†’ 𝐢𝐡𝑆 𝑆0, 𝐾, 𝑇, πœŽπ‘–π‘šπ‘, π‘Ÿ, 𝑑 = π‘€π‘Žπ‘Ÿπ‘˜π‘’π‘‘ π‘ƒπ‘Ÿπ‘–π‘π‘’

β€’ Does πœŽπ‘–π‘šπ‘exist?

– Yes

(Examples)

Page 15: Implied Volatility using Python's Pandas Library - Nag

15 Numerical Excellence Commercial in Confidence

Implied Volatility – Different Curves?

Page 16: Implied Volatility using Python's Pandas Library - Nag

16 Numerical Excellence Commercial in Confidence

Implied Volatility – Different Curves?

β€’ No hyphen or letter present = Composite A = AMEX American Stock Exchange B = BOX Boston Stock Exchange - Options E = CBOE Chicago Board Options Exchange I = BATS J = NASDAQ OMX BX O = NASDAQ OMX P = NYSE Arca X = PHLX Philadelphia Stock Exchange Y = C2 Exchange 4 = Miami Options Exchange 8 = ISE International Securities Exchange

Page 17: Implied Volatility using Python's Pandas Library - Nag

17 Numerical Excellence Commercial in Confidence

Implied Volatility

β€’ Reasons for skews/smiles?

– Risk Preferences

– Fat tailed distributions

Page 18: Implied Volatility using Python's Pandas Library - Nag

18 Numerical Excellence Commercial in Confidence

Implied Volatility Timings

Method Timing

fsolve + python BSM

fsolve + NAG BSM

nag4py

NAG C

Page 19: Implied Volatility using Python's Pandas Library - Nag

19 Numerical Excellence Commercial in Confidence

Implied Volatility Timings

Method Timing

fsolve + python BSM ~180 seconds

fsolve + NAG BSM

nag4py

NAG C

Page 20: Implied Volatility using Python's Pandas Library - Nag

20 Numerical Excellence Commercial in Confidence

Implied Volatility Timings

Method Timing

fsolve + python BSM ~180 seconds

fsolve + NAG BSM ~15 seconds

nag4py

NAG C

Page 21: Implied Volatility using Python's Pandas Library - Nag

21 Numerical Excellence Commercial in Confidence

Implied Volatility Timings

Method Timing

fsolve + python BSM ~180 seconds

fsolve + NAG BSM ~15 seconds

nag4py ~10 seconds

NAG C

Page 22: Implied Volatility using Python's Pandas Library - Nag

22 Numerical Excellence Commercial in Confidence

Implied Volatility Timings

Method Timing

fsolve + python BSM ~180 seconds

fsolve + NAG BSM ~15 seconds

nag4py ~10 seconds

NAG C ~.29 seconds

Page 23: Implied Volatility using Python's Pandas Library - Nag

23 Numerical Excellence Commercial in Confidence

Implied Volatility Timings

Method Timing

fsolve + python BSM ~180 seconds

fsolve + NAG BSM ~15 seconds

nag4py ~10 seconds

NAG C ~.29 seconds

β€’ Derivatives? β€’ We have the derivative, vega

‒𝝏π‘ͺ

𝝏𝝈= 𝑺 βˆ— 𝑻 βˆ— 𝑡′(π’…πŸ)

Page 24: Implied Volatility using Python's Pandas Library - Nag

24 Numerical Excellence Commercial in Confidence

Fitting Data Points

β€’ In our script we had k = l = 3…

– What if we try different values?

Page 25: Implied Volatility using Python's Pandas Library - Nag

25 Numerical Excellence Commercial in Confidence

Fitting Data Points

β€’ In our script we had k = l = 3…

– What if we try different values?

β€’ Poor results, can we do better?