Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Topic Author
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Computing sensitivities/greeks in computational finance, optimization and Machine Learning (ML): the Candidates

May 8th, 2019, 4:07 pm

How many Ways are there to compute Derivatives of a Function?
Differentiation, Sensitivities (greeks), gradients, Jacobians etc.
 
The computation of derivatives and gradients of scalar and vector-valued functions is a pervasive and ongoing activity in many areas of computational finance, optimisation, engineering and Machine Learning (ML), to name just a few. In general, we may need to compute derivatives as part of some algorithm or software program. Some specific applications are:

. Computing the Black Scholes greeks/sensitivities.
. Hedge ratios in fixed income and credit risk applications (for example, vega, duration and complexity).
.  Optimisation of design shape parameters.
. Computing derivatives in Neural Network (NN) algorithms, for example (Stochastic) Gradient Descent Method (SGD).
. Hedge ratios for stochastic differential equations (SDE), using Automatic Differentiation (AD), for example.
 . Global optimisation of (Langevin) SDEs and gradient ODE systems.
 . Applications (many) in which gradients, Jacobians and Hessians need to be calculated.
  . Robust calibration and volatility surfaces.
 
There are several established methods to address the problem of computing the derivatives of functions:
  
. Analytically (using calculus mainly).
. Using finite differences (or to be more precise, divided differences) to approximate derivatives (this is a well-known approach in PDE models).
. Automatic  (Algorithmic) Differentiation (AD).
. Continuous Sensitivity Equation (CSE). In this case we view the derivative of a function (with respect to a parameter) as a quantity that satisfies an ordinary or partial differential equation with auxiliary initial and boundary conditions.
. Approximating the function whose derivatives we wish to compute by an ‘easier’ function, for example a cubic spline whose derivatives are easily computed.
. The Complex Step Method (CSM). This little known but robust method to compute derivatives using function values alone (and no need to take divided differences). The method employs complex numbers and complex arithmetic.

There is no golden rule as to which of the above methods to use in general because the choice depends on the requirements and context. In general, we wish a given method to be suitable for the problem at hand and ideally it should be efficient, robust and the resulting code should be easy to use or to create.  Some questions to address are: exact or approximate values of the derivative and do we wish to have a discrete or continuous approximation.
The above methods are readily supported in C++, C#, Python and several (open-source) libraries.
In this note we give two examples of using The Complex Step Method in C++ and Python.  The first example is generic to show how the method works and the second example shows how to compute option ‘greeks’ in derivative pricing.
The CSM is discussed in the links attached to my source code. I give two C++ programs and a Python proof-of-concept code to make the method more accessible. In this case we used and modified the C++ code to get a working version in Python up and running.
The rationale for the code is to show how CSM works. Extensions to more complex cases are possible and will be discussed elsewhere.

https://www.linkedin.com/pulse/computin ... ine-duffy/

 
Attachments
TestGreeks101.cpp
(3.37 KiB) Downloaded 247 times
TestComplexStep.cpp
(1.66 KiB) Downloaded 254 times
Last edited by Cuchulainn on May 8th, 2019, 7:52 pm, edited 3 times in total.
 
User avatar
Cuchulainn
Topic Author
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Computing sensitivities/greeks in computational finance, optimization and Machine Learning (ML): the Candidates

May 8th, 2019, 4:14 pm

The forum doesn't accept .py format file, so I do an insert code
 
User avatar
Cuchulainn
Topic Author
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Computing sensitivities/greeks in computational finance, optimization and Machine Learning (ML): the Candidates

May 8th, 2019, 4:14 pm

# TestComplexStepMethod.py
#
# The complex step method for differentiation.
# 
# This code was ported from C++
#
# https://pdfs.semanticscholar.org/3de7/e8ae217a4214507b9abdac66503f057aaae9.pdf
#
# http://mdolab.engin.umich.edu/sites/default/files/Martins2003CSD.pdf
#
# (C) Datasim Education BV 2019
#

import numpy, math, cmath, random

j = cmath.sqrt(-1)

def Derivative(f, x, h):
# df/dx at x using tbe Complex Step Method

    z = x + h*j
    fz = f(z)
    return fz.imag/h;

def DerivativeZ(f, z):
# df/dx at z using tbe Complex Step Method

    #z = x + h*j
    fz = f(z)
    return fz.imag/h;

def DerivativeComposite(f, g, x, h):
# df(g)/dx at x  = df/dg X dg/dx using the Complex Step Method
# i.e. function composition

    z = x + h*j
    dgz = DerivativeZ(g,z)
    
    w = g(z)
    dfw = f(w)
   
    return (dfw.imag)/h + dgz.imag/h;

def SquireTrapp(t):
	n1 = cmath.exp(t);
	d1 = cmath.sin(t);
	d2 = cmath.cos(t);

	return n1 / (d1*d1*d1 + d2*d2*d2);

def func(t):
    return cmath.exp(t);

def func2(t):
    return cmath.cos(t);

def f(t):
    return cmath.exp(t);

def g(t):
    return cmath.exp(t);

x = 1.5
h = 0.001

ans = Derivative(SquireTrapp, x, h)
print(ans)

x = 5.0
ans = Derivative(func, x, h)
print(ans)

x = 1# math.sqrt(5)
ans2 = DerivativeComposite(g, g, x, h)
print(ans2)

b = math.exp(1)* math.exp(math.exp(1))
print(b)
 
User avatar
bearish
Posts: 5186
Joined: February 3rd, 2011, 2:19 pm

Re: Computing sensitivities/greeks in computational finance, optimization and Machine Learning (ML): the Candidates

May 9th, 2019, 12:39 am

That certainly works, although a much smaller value of h makes it work even better... In Matlab, which is more my native habitat, many functions naturally expect complex arguments, so one doesn't even need to call out to a special library.
 
User avatar
Cuchulainn
Topic Author
Posts: 20252
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: Computing sensitivities/greeks in computational finance, optimization and Machine Learning (ML): the Candidates

May 9th, 2019, 8:40 am

That certainly works, although a much smaller value of h makes it work even better... In Matlab, which is more my native habitat, many functions naturally expect complex arguments, so one doesn't even need to call out to a special library.
The nice thing about the Complex Step Method is that it works for all values of h without catastrophic subtraction cancellation errors as with traditional divided differences. You can even take [$]h = 1.0e{-303}[$] but accuracy has an "ergodic" value around [$]h = 1.0e{-6}[$] (roughly speaking).
If the sensitivity satisfies ODE/SDE/PDE then the whole process is even more robust. I reckon this is rather well-known on the floor but I have not seen anything written about it..
 In that case we can ensure no-arbitrage by using heavy machinery of maximum principle for PDEs and using monotonic finite difference schemes, for example PDEs for vega, delta and gamma. This is essentially the CSE method.