## Numerical Solution of PDEs in finance

Analysis, methods and coding for numerical problems, libraries and code.

You can create new topics and reply to other posts Please keep it factual and polite.

### Numerical Solution of PDEs in finance

(introductory) articles on Finite Difference Method for Black Scholes PDE
Attachments DuffyExponentialFitting.pdf From Navier-Stokes to Black-Scholes Numerical Methods in Computational Finance.pdf Cuchulainn

Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

### Re: Numerical Solution of PDEs in finance

An overview of the ADE (Alternating Direction Explicit) method.

Intro Black Scholes PDE/FDM
Attachments ADEOverview.pdf From Navier-Stokes to Black-Scholes Numerical Methods in Computational Finance.pdf Cuchulainn

Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

### Re: Numerical Solution of PDEs in finance TestComplexStep.cpp
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).
. 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. Cuchulainn

Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

### Re: Numerical Solution of PDEs in finance

C++ for option greeks
Attachments TestGreeks101.cpp Cuchulainn

Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

### Re: Numerical Solution of PDEs in finance

Python for option greeks

# TestComplexStepMethod.py
#
# The complex step method for differentiation.
#
# This code was ported from C++
#
# https://pdfs.semanticscholar.org/3de7/e ... 7aaae9.pdf
#
# http://mdolab.engin.umich.edu/sites/def ... 003CSD.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) Cuchulainn

Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands 