numerical challenge in finance solved by fem …neela/cimpa/notes/iitbcimpaop.pdf · implementation...

114
Numerical Challenge in Finance Solved by FEM (Financial Engineering) University of Paris VI, Laboratoire J.-L. Lions Olivier Pironneau 1 1 LJLL-University of Paris VI July 9, 2015 Aim: Be fast and accurate. Be adaptive. Prepare for the future Compare PDE with Monte-Carlo solutions. (Stay away from modeling) O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM July 9, 2015 1 / 110

Upload: leanh

Post on 12-Aug-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Numerical Challenge in Finance Solved by FEM(Financial Engineering)

University of Paris VI, Laboratoire J.-L. Lions

Olivier Pironneau1

1

LJLL-University of Paris VI

July 9, 2015

Aim:• Be fast and accurate. Be adaptive. Prepare for the future• Compare PDE with Monte-Carlo solutions. (Stay away frommodeling)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 1 / 110

Page 2: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 2 / 110

Page 3: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Options: The mechanism

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 3 / 110

Page 4: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Introduction to the Black & Scholes Model

A financial asset with tendency µ and volatility σ

dSt = St (µdt + σdWt ), S0 known

• µ = drift of St , = r , interest rate under the risk-neutral probability•Wt : a standard Brownian motion.

• If σ, r constant then St = S0e(µ−σ22 )t+σWt

A European option on S is a contract giving the right to buy ( call)or sell ( put) S at date T (maturity) at price K (strike).Its value at t is the expected profit at T discounted to t :

Ct = e−r(T−t)E((ST − K )+

), Pt = e−r(T−t)E

((K − ST )+

)

See also, C. Schwab, M. Griebel, C. Oosterlee, P. Glasserman.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 4 / 110

Page 5: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Pricing Options: 3 numerical methods

Due to the put call parity we can either compute P or C:

Ct − Pt = St − Ke−∫ T

t r(τ)dτ

1. Monte-Carlo:

Snk+1 − Sn

k = Snk (µδt + σ

√δtN (0,1)), Sn

0 = S0.

Ct = e−r(T−t) 1N

N∑1

((Sn

T − K )+)N (0,1) ≈ f (rand())

2. Tree methods

3. Ito Calculus :

∂tC +σ2x2

2∂2

xxC + rx∂xC − rC = 0

C(T , x) = (x − K )+ C(0, t) = 0C(x , t) ∼ x − Ke−r(T−t) when x →∞ 0

20

40

60

80

100

120

0 20 40 60 80 100 120 140 160 180 200

’result.dat’ using 1:2’result.dat’ using 1:3’result.dat’ using 1:4

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 5 / 110

Page 6: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Some Analytical Formulae

When r and σ are constant St = S0e(r−σ22 )t+σWt

. Easy to prove by using the Itô’s formula:

df (t ,wt ) =∂f∂t

dt +∂f∂w

dwt +12∂2f∂w2 (dwtdwt )

• Black-Scholes formula

Let d1 =log(St

K ) + (r + σ2

2 )(T − t)σ√

T − tand d2 = d1 − σ

√T − t

Introducing N the upper tail of the Gaussian function

Ct = StN(d1)− Ke−r(T−t)N(d2), Pt = −SN(−d1) + Ke−r(T−t)N(−d2)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 6 / 110

Page 7: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Implementation in C/C++With a text editor type in bsformula.cpp the following

#include <iostream>#include <cmath>using namespace std;

double BSput(double T,double S,double sig,double r, double K)const double sd= sig *sqrt(T), s2=sqrt(2.);const double d1 = (log(S/K)+r*T)/sd + sd/2, d2 = d1 - sd;return(-S*(1-erf(d1/s2))+K*exp(-r*T)*(1-erf(d2/s2)))/2;

int main()

cout<< "Put= "<< BSput(1., 100., 0.3, 0.03, 110) << endl;

Compile it withg++ bsformula.cppRun it with./a.out

On Windows you need to install mingw and type a.exe instead.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 7 / 110

Page 8: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Monte-Carlo Methods

References• Paul Glasserman. Monte Carlo methods in financial engineering, volume 53 ofApplications of Mathematics (New York). Springer-Verlag, New York, 2004. StochasticModelling and Applied Probability.• Yuh-Dauh Lyuu. Financial engineering and computation. Cambridge UniversityPress, Cambridge, 2002. Principles, mathematics, algorithms.

• In the C-library stdlib.h, there is a function rand() which returns an integervalue (of type long int) uniformly distributed in [0,RAND_MAX].• To obtain a Gaussian random variable, first make the change of scale

w → w :=w

RAND_MAXw ∈ [0,1]. x =

√−2 log(w1) cos(2πw2) ∈ N01

(1)• N01 = zero mean, unit variance i.e. probability density 1√

2πe−

x22 .

• Hence Wt+δt −Wt =√δtN01

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 8 / 110

Page 9: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

The program in C++ (in http://me.com/opironneau)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 9 / 110

Page 10: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

The program stores the result in a file call stoch.dat in a format thatgnuplot can use for graphics (see www.gnuplot.org).

0

20

40

60

80

100

120

0 5 10 15 20 25 30 35 40

C

S

"stoch.dat"

Displays C versus S at T=1 from data in file "stoch.dat", with gnuplot(and the command :plot "stoch.dat" w l).O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 10 / 110

Page 11: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Theorem

(Central limit)Let x be a random variable with probability density p, expectation E(x)and variance

var(x) = E((x − E(x))2).

The following approximation

E(x) :=

∫ +∞

−∞p(x)xdx ≈ EN(x) :=

1N

N∑1

xi (2)

verifies for all c1 < 0 < c2:

limN→∞

P(E(x)− EN(x) ∈

(c1

√var(x)

N, c2

√var(x)

N

))=

1√2π

∫ c2

c1

e−x22 dx

(3)where P(y ∈ Y ) stands for the probability that y belongs to Y .

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 11 / 110

Page 12: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Variance Reduction: Control Variate

To compute X := E(X ) compute E(X − X ′) instead for some X ′ withE(X ′) known and var(X − X ′) < var(X ).Consider Z = X − b(Y − Y ), for some Y for which Y is known.

var(Z ) = E((Z − Z )2) = var(X ) + b2var(Y )− 2bE((X − X )(Y − Y ))is minimized when

b =E((X − X )(Y − Y ))

var(Y )≈∑n

1(Xi − X )(Yi − Y )∑n1(Yi − Y )2

Thenvar(Z )

var(X )= 1− E

((X − X )(Y − Y )2)

E((X − X )2)E((Y − Y )2)

For a European vanilla call we take XT = e−rT (ST − K )+, YT = STand notice that E(YT ) = erT S0. Let Sin1 be n samples of ST , thenE(XT ) = E(ZT ) with ZT = XT − b(YT − E(YT )) which yields

E(ZT ) ≈ 1n

n∑1

(e−rT (Si − K )+ − b(Si − erT S0))

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 12 / 110

Page 13: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Barriers

A barrier option is an option which stops to exist at tb if

St < SM , ∀t < tb and Stb ≥ SM

A similar definition is made for lower barrier options with St < Sm.

The Monte-Carlo method for barrier options consists in selecting onlythe trajectories for which the constraint holds for all time up to T ..Therefore

Snk+1 − Sn

k = Snk (µδt + σ

√δtN (0,1)), Sn

0 = S0.

Ct = e−r(T−t) 1N

N∑1

((Sn

T − K )+1St<SM , ∀t<T)

Notice that the computing time will be more.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 13 / 110

Page 14: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Edging

The very reason of options is edging.For example take a portfolio which has z shares St . Can we add to it ycalls Ct on S such that its value is less random? its value is

Vt = zSt + yCt

and we need to choose y so that E(Vt ) = 0.Without loss of generality we can assume y = 1 and then find what isthe best value for z.We shall see that z = −∂SCt , which finance engineers call the "delta".Later we shall prove that

Ct − St∂Ct

∂Sis not random.

We can also try to verify it numerically? But ∂SCt is not so easy tocompute, yet we can use finite difference approximations or automaticdifferentiation (AD).O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 14 / 110

Page 15: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Self Financing Portfolio

We wish to obtain a riskless portfolio made of aSt + Ct , i.e.

aSt+δt + Ct+δt = erδt (aSt + Ct )

By Itô calculus E(aSt+δt + Ct+δt |St ) can be obtained by

aSt + adS + Ct +∂C∂tδt +

∂C∂S

dS +12∂2C∂S2 dS2

Randomness is eliminated when a = −∂C∂S (hedging). Then

erδt (aSt + Ct ) = aSt + Ct +∂C∂tδt +

12∂2C∂S2 S2σ2δt ⇒

rδt(aSt + Ct ) ≈∂C∂tδt +

12∂2C∂S2 S2σ2δt ⇒

−r∂C∂S

S + rC ≈ ∂C∂t

+12∂2C∂S2 S2σ2

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 15 / 110

Page 16: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Tutorial 1

1 Compute the exact value with the Black-Scholes formula at onesampling point S

2 Run 10 times a Monte-Carlo simulation with N=10000 trajectories3 Make a plot with Excel showing the errors and compute the

standard deviation.4 Study the regression error as a function of the number of

realizations N5 Study the influence of the time step.6 Modify the program to compute the same option with a barrier

St ≤ SM .7 Try to hedge the portfolio Ct −St∂SCt and check that this does not

oscillate by randomness.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 16 / 110

Page 17: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 17 / 110

Page 18: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

A simple Tree method

Suppose that from one day nδt to the next (n + 1)δt , Sn becomesSn+1 = uSn with probability p or Sn+1 = dSn with probability 1− p. andno other.After N days SN = umdN−mS0 with probability Cm

N pm(1− p)N−m whereCm

N = N!/m!(N −m)!.To have the compatibility with the Black-Scholes model we write

puSn + (1− p)dSn = erδtSn because left is a deterministic quantityE(S2

n+1)− E(Sn+1)2 = S2n [pu2 + (1− p)d2 − e2rδt ]

= S2n(e(2r+σ2)δt − e2rδt ) equating variance from SDO

The option is found from the opposite direction by

Cn = e−rδt (Cun+1p + Cd

n+1(1− p))

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 18 / 110

Page 19: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Implementation in C++

... const int M=100, nx=50;const double r=0.03, K=110, sigmap=0.3, dt=1.0/M;double S[100], C[100];

double binomial(const double S0)double disc=exp(-r*dt), u= (1+sqrt(exp(sigmap*sigmap*dt)-1))/disc;double d=(1-sqrt(exp(sigmap*sigmap*dt)-1))/disc, p=0.5;

S[0] = S0;for(int m=1; m<M; m++)for(int n=m; n>0; n--) S[n] = u*S[n-1];S[0] = d*S[0];

for(int n=0;n<M;n++) C[n] = S[n]>K?S[n]-K:0;for(int m=M-1;m>0;m--)for(int n=0; n<m;n++)C[n] = (p*C[n+1]+(1-p)*C[n])*disc;

return C[0];int main()for(int i=0;i<nx;i++) cout <<binomial(2.*i*K/nx)<<endl;return 0;

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 19 / 110

Page 20: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 20 / 110

Page 21: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Black-Scholes in Log-Scales

Theorem C0 = C(S0,0) where C(S, t) is the solution of

∂C∂t

+σ2

2∂2C∂S2 S2 + r

∂C∂S

S − rC = 0, C(S,T ) = (S − K )+

θ = T − t , x = log S, and ϕ(x , θ) = C(ex ,T − θ). (4)

S∂C∂S

=∂ϕ

∂xand S2∂

2C∂S2 = S

∂S(S∂C∂S

)− S∂C∂S

=∂2ϕ

∂x2 −∂ϕ

∂xSo, for a call (with ν := (r − σ2

2 ))

∂ϕ

∂θ− 1

2σ2∂

∂x2 − ν∂ϕ

∂x+ rϕ = 0 in R× (0,T [

ϕ(x ,0) = (ex − K )+

ϕ(x , θ) ∼ 0, as x → −∞ϕ(x , θ) ∼ ex − Ke−rθ as x → +∞. (5)

There is an analytical solution with erf functions as shown by a lastchange of variableO. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 21 / 110

Page 22: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Analytical Solution

With ϕ(x , θ) = ψ(x , θ)eaθ+bx , b = 12 − r

σ2 and a = −r − σ2

2 b2:

∂ψ

∂θ− σ2

2∂2ψ

∂x2 = 0, ψ(x ,0) = (e(σ2

2 +r) xσ2 − Ke(−σ2

2 +r) xσ2 )+, (6)

Recall that

∂tu − k∂xxu = 0, u(x ,0) = δ(x − x0) ⇒ u(x , t) =1√

2πkte−|x−x0|

2

4kt

Therefore ψ(x , t) =

∫R

σ√πt

e−(x−y)2

2t (e(σ2

2 +r)y − Ke(−σ22 +r)y )+dy

From then on the Black-Scholes formula can be derived.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 22 / 110

Page 23: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 23 / 110

Page 24: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Finite Difference: Explicit Schemes

∂u∂t

+σ2

2∂2u∂x2 + r

∂u∂x− ru = 0, u(x ,T ) = (ex − K )+

umj − um−1

j

δt+

σ2

2δx2 (umj+1 − 2um

j + umj−1) +

12δx

(r − σ2

2)(um

j+1 − umj−1)− rum−1

j = 0

Explicit but a stability condition is needed: δt < cδx2

Equivalence with binomial trees: Take δx = σ√δt then

(1 + rδt)um−1j =

12

(umj+1 + um

j−1) +

√δt

2σ(r − σ2

2)(um

j+1 − umj−1)

⇒ um−1j ≈ e−rδt [(

12

+

√δt

2σ(r − σ2

2))um

j+1 + (12−√δt

2σ(r − σ2

2))um

j−1]

which is a tree with p ≈ erδt−du−d ,u = eσ

√δt ,d = e−σ

√δt ⇒ error O(δt

32 ).

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 24 / 110

Page 25: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Finite Difference: Explicit Schemes

∂u∂t

+σ2

2∂2u∂x2 + r

∂u∂x− ru = 0, u(x ,T ) = (ex − K )+

umj − um−1

j

δt+

σ2

2δx2 (umj+1 − 2um

j + umj−1) +

12δx

(r − σ2

2)(um

j+1 − umj−1)− rum−1

j = 0

Explicit but a stability condition is needed: δt < cδx2

Equivalence with binomial trees: Take δx = σ√δt then

(1 + rδt)um−1j =

12

(umj+1 + um

j−1) +

√δt

2σ(r − σ2

2)(um

j+1 − umj−1)

⇒ um−1j ≈ e−rδt [(

12

+

√δt

2σ(r − σ2

2))um

j+1 + (12−√δt

2σ(r − σ2

2))um

j−1]

which is a tree with p ≈ erδt−du−d ,u = eσ

√δt ,d = e−σ

√δt ⇒ error O(δt

32 ).

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 24 / 110

Page 26: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Finite Difference: Implicite Schemes

∂u∂t

+σ2

2∂2u∂x2 + r

∂u∂x− ru = 0, u(x ,T ) = (ex − K )+

um+1j − um

j

δt+

σ2

2δx2 (umj+1 − 2um

j + umj−1) +

12δx

(r − σ2

2)(um

j+1 − umj−1)− rum

j = 0

Linear system solved by Gauss LU tri-diag fact. : as fast as trees.Marginal stability condition δt < cδx due to 1st order x-derivativeUpwinding gives unconditional stabilityprecision O(δt) + O(δx2)

Crank-Nicolson time scheme will give O(δt2) + O(δx2)

Non-constant deltaxj can put the points where needed but thenFEM does better.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 25 / 110

Page 27: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 26 / 110

Page 28: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Monte-Carlo: Goods and bads

Near to the modelisationGives upper and lower bounds on the errorConverges in 1/

√N ⇒ Quasi-Monte Carlo

Can compute several derivatives with O(√

N) operationsEasy to parallelize.Complexity grows in O(d) with the dimension d .Greeks by Malliavin calculus.Calibration is very hard.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 27 / 110

Page 29: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

PDEs: Goods and bads

Nearer to the analytical formulasUpper and lower error bounds with a posteriori estimatesConverges in O(N−p) with order of approximation pCompute several derivatives in O(N log N) operations (Dupire).Can be parallelized.Cursed by dimension d in O(Nd ) except with sparse grid.Greeks are very easyCalibration is reasonably easy

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 28 / 110

Page 30: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Tutorial 2

Results to be given in LateX , MSWord or Excel (sent by email [email protected] with subject: tuto2 + your name)

1 By comparing with the Black-Scholes analytic formula checksensitivity of the tree method treeBS.cpp with respect to δt (3different δt will do)

2 By comparing with the Black-Scholes analytic formula checksensitivity of the finite difference method explicitfdm.cpp withrespect to δx (3 different NX will do)

3 Compare the results, computing time and precision for the sameδt of edostoch.cpp, bsformula.cpp,bstree.cpp,explicitfdm.cpp on the same put option withS = 100,K = 80, r = 3%, σ = 20%,T = 1.

4 Compare the graph of an American option with the European onewith treeBS.cpp . Americans are obtained by using

Pn = max(K − Sn)+,e−rδt(pPun + 1 + (1− p)Pdn+1)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 29 / 110

Page 31: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 30 / 110

Page 32: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Existence, Uniqueness and Regularity

Use the put-call parity to work with P which decays at infinity with timeto maturity τ = T − t :

∂τP − σ2x2

2∂2

xxP − rx∂xP + rP = 0

P(0, x) = (K − x)+, limx→∞

P(x , τ) = 0

Theorem Assume σ(x , t) ∈ [σm, σM ], x∂xσ ∈ L∞ and r , σ, x∂xσLipschitz in t, then ∃!P continuous in time and x2∂xxP ∈ L2(R+).Furthermore x2∂xxσ ∈ L∞ ⇒ P is convex and ≥ 0.

Proof uses the variational form in a weighted Sobolev space V in which

a is Garding coercive: ∃α, β ∈ R+ s.t. a(u,u) ≥ α‖u‖2V − β‖u‖2L2(R+)

u ∈ V := v ∈ L2(R+) : x∂xv ∈ L2(R+)and (∂tu,w) + at (u,w) = 0 ∀w ∈ V

with at (u,w) =

∫ ∞0

[∂xu · ∂x (x2σ2

2w)− rxw∂xu + ruw ]

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 31 / 110

Page 33: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Existence, Uniqueness and Regularity

Use the put-call parity to work with P which decays at infinity with timeto maturity τ = T − t :

∂τP − σ2x2

2∂2

xxP − rx∂xP + rP = 0

P(0, x) = (K − x)+, limx→∞

P(x , τ) = 0

Theorem Assume σ(x , t) ∈ [σm, σM ], x∂xσ ∈ L∞ and r , σ, x∂xσLipschitz in t, then ∃!P continuous in time and x2∂xxP ∈ L2(R+).Furthermore x2∂xxσ ∈ L∞ ⇒ P is convex and ≥ 0.

Proof uses the variational form in a weighted Sobolev space V in which

a is Garding coercive: ∃α, β ∈ R+ s.t. a(u,u) ≥ α‖u‖2V − β‖u‖2L2(R+)

u ∈ V := v ∈ L2(R+) : x∂xv ∈ L2(R+)and (∂tu,w) + at (u,w) = 0 ∀w ∈ V

with at (u,w) =

∫ ∞0

[∂xu · ∂x (x2σ2

2w)− rxw∂xu + ruw ]

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 31 / 110

Page 34: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Existence, Uniqueness and Regularity

Use the put-call parity to work with P which decays at infinity with timeto maturity τ = T − t :

∂τP − σ2x2

2∂2

xxP − rx∂xP + rP = 0

P(0, x) = (K − x)+, limx→∞

P(x , τ) = 0

Theorem Assume σ(x , t) ∈ [σm, σM ], x∂xσ ∈ L∞ and r , σ, x∂xσLipschitz in t, then ∃!P continuous in time and x2∂xxP ∈ L2(R+).Furthermore x2∂xxσ ∈ L∞ ⇒ P is convex and ≥ 0.

Proof uses the variational form in a weighted Sobolev space V in which

a is Garding coercive: ∃α, β ∈ R+ s.t. a(u,u) ≥ α‖u‖2V − β‖u‖2L2(R+)

u ∈ V := v ∈ L2(R+) : x∂xv ∈ L2(R+)and (∂tu,w) + at (u,w) = 0 ∀w ∈ V

with at (u,w) =

∫ ∞0

[∂xu · ∂x (x2σ2

2w)− rxw∂xu + ruw ]

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 31 / 110

Page 35: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

A posteriori estimates (Y.Achdou)

Proposition

[[u − uh,δt ]](tn) ≤ c(u0)δt

σ2min

(n∑

m=1

ηm2 +

δtmσ2

ming(ρδt )

m−1∏i=1

(1− 2λδti )∑ω∈Tmh

ηm,ω2

) 12

where L, µ are the time-continuity constants of σ2, r , xσ∂xσ in L∞,c(u0) = (‖u0‖2 + δt‖∇u0‖2)1/2, g(ρδt ) = (1 + ρδt )

2 max(2,1 + ρδt )

η2m = δtme−2λtm−1

σ2min2|um

h − um−1h |2V ,

ηm,ω =hω

xmax(ω)‖um

h − um−1h

δtm− rx

∂umh

∂x+ rum

h ‖L2(ω)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 32 / 110

Page 36: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Best Numerical Method

• Implicit in time, centered in space (upwind usually not necessary):

Pn+1 − Pn

δt− x2σ2

2∂2

xxPn+ 12 + rPn+ 1

2 − xr∂xPn+ 12 = 0

• FEM-P1 + LU factorization• mesh adaptivity + a posteriori estimates• Banks need 0.1% precision in split seconds ...within Excel

0 50

100 150

200 250

300 0

0.1

0.2

0.3

0.4

0.5

-20

0

20

40

60

80

100

PDE solutionBlack-Scholes formula

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 33 / 110

Page 37: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Numerical Example

0 50

100 150

200 250

300 0 0.05

0.1 0.15

0.2 0.25

0.3 0.35

0.4 0.45

0.5

0 0.2 0.4 0.6 0.8

1 1.2 1.4 1.6 1.8

"u.txt"using 1:2:7

0 50

100 150

200 250

0 0.05

0.1 0.15

0.2 0.25

0.3 0.35

0.4 0.45

0.5

0 0.2 0.4 0.6 0.8

1 1.2 1.4

"u.txt"using 1:2:7

0 50

100 150

200 250

300 0 0.05

0.1 0.15

0.2 0.25

0.3 0.35

0.4 0.45

0.5

-0.05

-0.04

-0.03

-0.02

-0.01

0

0.01

0.02

"u.txt"using 1:2:5

0 50

100 150

200 250

300 0 0.05

0.1 0.15

0.2 0.25

0.3 0.35

0.4 0.45

0.5

0

0.001

0.002

0.003

0.004

0.005

0.006

0.007

"u.txt"using 1:2:6

TOP: Indicator 1 with a fixed number of nodes (left) and varying (right)BOTTOM: Actual error (left). Second indicator (right).

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 34 / 110

Page 38: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Source Code in C++

Page: 1/Users/pironneau/Desktop/07fall / tex/NatIxis/optionNixis.cppSunday 18 November 2007 / 16:28

#include <math.h>#include <iostream>#include <fstream>using namespace std;

typedef double ddouble;

class Option public: const int nT, nX; // nb of time steps and mesh points const double r,S,K,T;// rate spot price strike and maturity ddouble **sigma;// volatility(function of x and t) ddouble *u, *uold, *am, *bm, *cm; // working arrays double dx, dt; ddouble phi(const double strike, double s1); // payoff void factLU(); void solveLU(ddouble* z); void calc(); Option(const int nT1, const int nX1, const double r1, const double S1, const double K1, const double T1); ~Option() delete [] am; delete [] bm; delete []cm; delete [] uold; ;

Option::Option(const int nT1, const int nX1, const double r1, const double S1, const double K1, const double T1) : nT(nT1), nX(nX1), r(r1), S(S1), K(K1), T(T1) const double xmax=3*S; dx = xmax/(nX-1); dt = T/(nT-1); u = new ddouble[nX]; sigma = new ddouble*[nT]; for(int i = 0; i < nT; i++) sigma[i] = new ddouble[nX]; am = new ddouble[nX]; bm = new ddouble[nX]; cm = new ddouble[nX]; uold = new ddouble[nX];

ddouble Option::phi(const double strike, double x) return x<strike?strike-x:0;

void Option::factLU()

cm[1] /= bm[1];for(int i=2;i<nX-1;i++)

bm[i] -= am[i]*cm[i-1];

Page: 2/Users/pironneau/Desktop/07fall / tex/NatIxis/optionNixis.cppSunday 18 November 2007 / 16:28bm[i] -= am[i]*cm[i-1];

cm[i] /= bm[i];

void Option::solveLU(ddouble *z)z[1] /= bm[1];for(int i=2;i<nX-1;i++)

z[i] = (z[i] - am[i]*z[i-1])/bm[i];for(int i=nX-2;i>0;i--)

z[i] -= cm[i]*z[i+1];

void Option::calc( ) for(int i=0;i<nX;i++) u[i] = phi(K,i*dx); int j1=0; for(int j=0;j<nT;j++) // time loop

for(int i=1;i<nX-1;i++) // rhs of PDE uold[i] = u[i] + dt*r*i*(u[i+1]-u[i-1])/2;

u[nX-1]=0; u[0] = K*exp(-r*(j+0.5)*dt); // B.C. of PDE uold[1] += u[0]*sigma[j][1]*sigma[j][1]*dt/2;

for(int i=1;i<nX-1;i++) // build matrix

ddouble aux=i*sigma[j][i]*i*sigma[j][i]*dt/2; bm[i] = (1+ r*dt + 2*aux); am[i] = -aux; cm[i] = -aux;

factLU(); for(int i=1;i<nX-1;i++) u[i]=uold[i]; solveLU(u);

int main()Option p(100,150,0.03, 100,110,4);// nT, nX, r, S, K, T for(int j=0;j<p.nT;j++) // time loop

for(int i=0;i<p.nX;i++) p.sigma[j][i]=0.3;p.calc();for(int i=0;i<p.nX;i++) cout<<p.u[i]<<endl;return 0;

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 35 / 110

Page 39: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Greeks (Challenge Malliavin Calculus)

∂tu −x2σ2

2∂xxu − rx∂xu + ru = 0, u(0) = (K − x)+

The ∆ = ∂xu is given by the method. The Γ = ∂xxu can be computedby:

(Γh,wh) = −(∆, ∂xwh) ∀wh ∈ Vh or Γh(qi) = −(

∫Ω∂xw i∂x (u)dx)/(

∫Ω

w i)

Where the second formula is a consequence of mass lumping.The vega ν = ∂σu is solution of

∂tν −x2σ2

2∂xxν − rx∂xν + rν = x2σ∂xxu, ν(0) = 0

It is the same PDE with a r.h.s. In variational form1δt

(νn+1h − νn

h ,wh) + a(νn+1h ,wh) = −(∂x (whx2σ), ∂xun+1

h )

etc. Automatic Differentiation can also be used.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 36 / 110

Page 40: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Freefem++ Implementation (I)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 37 / 110

Page 41: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Freefem++ Implementation (II)

Put option price u The Delta : ∆ = ∂xu

fespace Vhdc(th,P1dc); Vhdc dxu=dx(u); plot(dxu,th);

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 38 / 110

Page 42: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Barrier Options

If u stops to exist when St > SM and when St < SM then just addu(SM , t) = 0 for all t . The theory is the same but with

V =

v ∈ L2(]Sm,SM [) : x

dvdx∈ L2(]Sm,SM [), v(Sm) = v(SM) = 0

uh(x , t) =

N∑j=2

uj(t)w j(x) (do not take the first and last hat function)

⇔ BdUdt

+ A(t)U = 0, where Bij = (w j ,w i), Aij(t) = at (w j ,w i).

-10

0

10

20

30

40

50

60

0 20 40 60 80 100 120 140 160

"u.txt" using 1:2"u.txt" using 1:3"u.txt"using 1:4

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 39 / 110

Page 43: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Freefem++ Implementation of Barriers

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 40 / 110

Page 44: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Asian Options

For the financial modeling of path dependent options with PDE , seeWillmott et al. In an Asian option the payoff is

u0(S, y) = (K − y)+ where y =1t

∫ t

0S(τ)dτ

The price of the option is found by solving for allx , y , t ∈ R+ × R+ × (0,T ]

∂tu − ∂x (σ2x2

2∂xu) + (σ2 − r)x∂xu +

y − xT − t

∂yu + ru = 0,

u(x , y ,0) = (K − y)+, ∂xu|+∞,y ,t ≈∣∣∣∣∣ t

T e−rt if y < KTT−t

1−re−rt

Tr otherwise.(7)

The difficulties of this problem is that the second order part of thedifferential operator is incomplete and the convective velocityv = ((σ2 − r)x , y−x

T−t )T tends to infinity as t → T ;

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 41 / 110

Page 45: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Speed-up with Galerkin - Characteristic Method (I)

Don’t upwind or if you do, use this:

∂tu + a · ∇u|x ,(m+1)δt =um+1(x)− um(x − am(x)δt)

δt+ O(δt)

=um+1 − umoX

δt+ O(δt)

with X (x) = Xam (mδt) anddXdτ

(τ) = am(X (τ)), X ((m + 1)δt) = x

x

x − am(x)δt ΓX (x)

Second order approximation

(∂tu+ a · ∇u)|x,(m+1)δt ≈3um+1(x)− 4um(x − am(x)δt) + um−1((x − 2am(x)δt))

2δt

=3um+1 − 4umoX∗δt + um−1oX∗2δt

2δt+ O(δt2)

with X∗kδt(x) = Xa∗ m+ 1

2(k mδt), k = 1, 2

and a∗ m+ 12 = 2am − am−1

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 42 / 110

Page 46: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Speed-up with Galerkin - Characteristic Method (I)

Don’t upwind or if you do, use this:

∂tu + a · ∇u|x ,(m+1)δt =um+1(x)− um(x − am(x)δt)

δt+ O(δt)

=um+1 − umoX

δt+ O(δt)

with X (x) = Xam (mδt) anddXdτ

(τ) = am(X (τ)), X ((m + 1)δt) = x

x

x − am(x)δt ΓX (x)

Second order approximation

(∂tu+ a · ∇u)|x,(m+1)δt ≈3um+1(x)− 4um(x − am(x)δt) + um−1((x − 2am(x)δt))

2δt

=3um+1 − 4umoX∗δt + um−1oX∗2δt

2δt+ O(δt2)

with X∗kδt(x) = Xa∗ m+ 1

2(k mδt), k = 1, 2

and a∗ m+ 12 = 2am − am−1

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 42 / 110

Page 47: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Upwinding by Characteristics (II)

One of the best upwinding is the characteristic finite element method :

∂tu + v · ∇u|x ,y ,tm+1 ≈1δt

(um+1(q)− um(Q)),

with Q = q − δtv(q) where q = (x , y)T .

IsoValue3.3866110.196917.007223.817530.627837.43844.248351.058657.868964.679271.489578.299785.1191.920398.7306105.541112.351119.161125.972132.782

IsoValue2.961878.8882614.814620.74126.667432.593838.520244.446650.37356.299462.225868.152274.078580.004985.931391.857797.7841103.71109.637115.563

IsoValue1.954695.86499.7751113.685317.595521.505725.41629.326233.236437.146641.056844.96748.877252.787456.697660.607964.518168.428372.338576.2487

K = 100, r = 3%, σ = 0.3. the square domain is triangulated by auniform 50× 50 mesh and we have taken 50 time steps over the 4years period (T=4). Results are shown at t = 0.96, 1.96, 3.96.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 43 / 110

Page 48: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Freefem++ Implementation of Asian Option

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 44 / 110

Page 49: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 45 / 110

Page 50: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

American Put Option by Monte-Carlo

At τ we compare Peτ := E[e−r(T−τ)(K − ST )+|Sτ ] with (K − Sτ )+:

P0 = supτ∈(0,T )

e−rτESτ maxPeτ , (K − Sτ )+

When r , σ are constant ST = Sτe(r−σ22 )(T−τ)+σWT−τ , so

Peτ = E[e−r(T−τ)(K − Sτe(r−σ2

2 )(T−τ)+σWT−τ )+]P0 = sup

τ∈(0,T )

e−rτESτ[maxPe

τ , (K − Sτ )+]

≈ supm∈(0,..,M)

e−rτm

N

∑n

[maxPenm, (K − Snm)+]

with τm = mδt , Snm = S0e(r−σ22 )τm+σ

√τmNn

01

Penm ≈

1I

e−r(T−τm)∑

i

(K − Snme(r−σ22 )(T−τm)+σ

√T−τmN i

01 )+

By comparison the same European option is valued as1N

∑n

[e−rτ (K − S0e(r−σ22 )T +σ

√TN n

01)+]

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 46 / 110

Page 51: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

C++ code

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 47 / 110

Page 52: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Longstaff-Schwartz: an exampleStock K=110, r=6%, h(S)=(K−S)+

Path t=0 t = 1 t = 2 t = 3 h(S3)1 100 109 108 134 02 100 116 126 154 03 100 122 107 103 74 100 93 97 92 185 100 111 156 152 06 100 76 77 90 207 100 92 84 101 98 100 88 122 134 0

E [YIX ] = −107 + 2.983X − 0.01813X 2

Y=h(S3)(1-r) X=S2 h(X) E(X|Y)0 108 2 3.690 126 0 0

7 ×.94 107 3 4.6118 ×.94 97 13 11.76

0 156 0 020 ×.94 77 33 15.29 ×.94 84 26 15.7

0 122 0 0E [YIX ] = 203.8− 3.335X + 0.01356X 2

Path t = 2 t = 3 Y=h(S2)(1-r) X=S1 h(X) E(Y|X)1 0 0 0 109 1 1.392 0 0 0 0 0 03 0 7 0 0 0 04 13 0 13 ×.94 93 17 10.925 0 0 0 0 0 06 33 0 33 ×.94 76 34 28.667 26 0 26 ×.94 92 18 11.758 0 0 0 88 22 15.33

Stopping rulePath Stop P

1 02 03 t3 74 t1 175 06 t1 347 t1 188 t1 22

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 48 / 110

Page 53: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Mathematical description

Let s → C(ω, s; t ,T ) be the path of cash generated by the option whenit has not been exercized at t < s. The value of continuation is

F (ω, tk ) = E[N∑

k+1

e−r(tj−tk )C(ω, tj ; tk ,T )|Sk ]

This function is represented on a basis, for example, 1,X ,X 2.. orLaguerre polynomials eX/2

m!dm

dX m (X me−X )M1 . The coefficients arecomputed by least-square.It is done backward in time and by linearity it is done on C(ω, tj ; tk ,T ).

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 49 / 110

Page 54: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Implementation in C++(I)Page 1 of 4longstaff.cpp

Printed: 02/03/2009 22:05:58 Printed For: Olivier Pironneau

// Pricing an American Put Option by Longstaff & Schwartz's Least-Squares Monte-Carlo!// written by Tobias Lipp (LJLL-UPMC), Feb 2009!!

#include <iostream>!#include <cmath>!#include <cstdlib>!using namespace std;!!

class lsmc public:! lsmc(double, double, double, double, double, int, int, int);! ~lsmc();! void Cal_Price();! void Display_Results();!private:! double payoff(double S) return K-S>0. ? K-S : 0.; ! void Generate_Trajs();! void Init_cf();! void Cal_ExVal(int&, double*, int);! void Build_Ab(double**, double*, int, double*);! void factQR(double**, double*, double*, int);! void solveQR(double**, double*, double*, double*);! void Update_cf(double*, double*, int);! ! double T, K, S0, r, sig;! int I, M, N;! double dt, sdt,!! **S, **cf;! double PA;!;!!

lsmc::lsmc(double nT, double nK, double nS0, double nr, double nsig, int nI, int nM, int nN) !: T(nT), K(nK), S0(nS0), r(nr), sig(nsig), I(nI), M(nM), N(nN) !! dt = T/M;!! sdt = sqrt(dt);!!! // matrix: stock prices!! S = new double*[I];!! for(int i=0; i<I; i++) S[i] = new double[M+1];!! // matrix: cash flows!! cf = new double*[I];!! for( int i=0; i<I; i++) cf[i] = new double[2]; !! PA=0.;!!!

lsmc::~lsmc() !! for(int i=0; i<I; i++) delete[] S[i]; delete[] cf[i]; !! delete[] S; delete[] cf;!!// -----------------------------------------------------!void lsmc::Cal_Price() !! Generate_Trajs();!! Init_cf();!! !

! for(int t=M-1; t>0; t--) !! ! // Immediate exercise values and nb of 'in the money pathes' at time t !! ! double* const ex = new double[I];!! ! int nmp=0;!! ! Cal_ExVal(nmp,ex,t);!! ! // Least Square Pb.: min|Ax-b|^2, b = cashflow * discountfactor !! ! double** const A = new double*[nmp];!! ! for( int i=0; i<nmp; i++)!! ! ! A[i] = new double[N];!

Page 2 of 4longstaff.cpp

Printed: 02/03/2009 22:05:58 Printed For: Olivier Pironneau

! ! double* const b = new double[nmp];!! ! Build_Ab(A,b,t,ex);!! ! // QR-Decomposition via Householder, Transformations: A = QR!! ! double* diagR = new double[N];!! ! factQR(A,b,diagR,nmp);!! ! // Solve R x = b1 (b=(b1,b2))!! ! double* const x = new double[N];!! ! solveQR(A,b,x,diagR);! ! !

! ! // Update cashflow mx!! ! Update_cf(ex,x,t);!! ! !

! ! delete[] ex;!! ! for(int i=0; i<nmp; i++) delete[] A[i];!! ! delete[] A; delete[] b; delete[] diagR; delete[] x; !! !! // PA: Price American (Put)!! for( int i=0; i<I; i++) PA += exp(-r*cf[i][0]*dt)*cf[i][1];!! PA /= I;!! !

!// -----------------------------------------------------!double gauss() !! double x=double(1.+rand())/double(1.+RAND_MAX);!! double y=double(1.+rand())/double(1.+RAND_MAX);!! return sqrt(-2*log(x))*cos(2*M_PI*y);!!// -----------------------------------------------------!void lsmc::Generate_Trajs() !! for(int i=0; i<I; i++) !! ! S[i][0] = S0;!! ! for(int j=1; j<=M; j++)!! ! ! S[i][j] = S[i][j-1]*( 1 + r*dt + sig*sdt*gauss() );!! !!!

void lsmc::Init_cf() !! for( int i=0; i<I; i++) !! ! cf[i][0] = M; // cash flows at time cf[*][0] !! ! cf[i][1] = payoff(S[i][M]); // cf[*][1] flowing amount!! !!// -----------------------------------------------------!void lsmc::Cal_ExVal(int& nmp, double* ex, int t) !! for(int i=0; i<I; i++) !! ! ex[i] = payoff(S[i][t]);!! ! if( ex[i] > 0. ) nmp++;!! !!!

void lsmc::Build_Ab(double** A, double* b, int t, double* ex) !! int ii=0;!! for (int i=0; i<I; i++) !! ! if( ex[i] == 0. )!! ! ! continue;!! ! for(int j=0; j<N; j++)!! ! ! A[ii][j] = pow(S[i][t],j);!! ! b[ii] = exp(-r*(cf[i][0]-t)*dt) * cf[i][1]; !! ! ii++;!! !!// -----------------------------------------------------!

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 50 / 110

Page 55: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Implementation in C++(II)Page 3 of 4longstaff.cpp

Printed: 02/03/2009 22:05:58 Printed For: Olivier Pironneau

inline double sqr(double x) return x*x;!inline double sgn(double a) return a>0. ? 1. : -1.;!double norm2(double** A, int I, int j) !! double norm2=0.;!! for(int i=j; i<I; i++) norm2 += sqr(A[i][j]);!! return norm2;!!// -----------------------------------------------------!void lsmc::factQR(double** A, double* b, double* diagR, int nmp) !! for(int j=0; j<N; j++) !! ! diagR[j] = -sgn(A[j][j])*sqrt(norm2(A,nmp,j));!! ! A[j][j] -= diagR[j];!! ! !

! ! double v2 = norm2(A,nmp,j);!! ! !

! ! for(int jj=j+1; jj<N; jj++) !! ! ! double va=0.;!! ! ! for(int i=j; i<nmp; i++)!! ! ! ! va += A[i][j]*A[i][jj];!! ! ! for(int i=j; i<nmp; i++)!! ! ! ! A[i][jj] -= 2*va/v2*A[i][j];!! ! !! ! !

! ! double vb=0.;!! ! for(int i=j; i<nmp; i++)!! ! ! vb += A[i][j]*b[i];!! ! for(int i=j; i<nmp; i++)!! ! ! b[i] -= 2*vb/v2*A[i][j];!! !!// -----------------------------------------------------!void lsmc::solveQR(double** A, double* b, double* x, double* diagR) !! for(int i=N-1; i>-1; i--) !! ! x[i] = b[i];!! ! for(int j=i+1; j<N; j++)!! ! ! x[i] -= A[i][j]*x[j];!! ! x[i] /= diagR[i];!! !!// -----------------------------------------------------!void lsmc::Update_cf(double* ex, double* x, int t) !! for(int i=0; i<I; i++) !! ! if( ex[i] <= 0. )!! ! ! continue;!! ! double con=0.;!! ! for(int k=0; k<N; k++)!! ! ! con += x[k]*pow(S[i][t],k);!! ! if( con <= ex[i] ) !! ! ! cf[i][0] = t;!! ! ! cf[i][1] = ex[i];!! ! !! !!// -----------------------------------------------------!void lsmc::Display_Results() !! std::cout << " Price American Put: P = " << PA << std::endl; !!!

int main() !!

srand(time(0));!

Page 4 of 4longstaff.cpp

Printed: 02/03/2009 22:05:58 Printed For: Olivier Pironneau

int ela = clock();! ! const double T=1., K=100., S0=100., r=0.05, sig=0.3;! const int I=100000, // nb. of trajectories (muss >= N sein)! M=20, // nb. of time steps!! N=4; // nb. of basis fcts.!!

lsmc p(T,K,S0,r,sig,I,M,N);! ! p.Cal_Price();! p.Display_Results();!!

ela = clock() - ela;! cout << " Elapsed time: " << ela/1e6 << " sec" << endl; ! return 0;!!

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 51 / 110

Page 56: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

American Options by PDE

In American options one of the two must be also an equality

∂u∂t− σ2x2

2∂2u∂x2 − rx

∂u∂x

+ ru ≥ 0

u ≥ u := (K − x)+

V =

v ∈ L2(R+), x

∂v∂x∈ L2(R+)

,

K = v ∈ L2(0,T; V),v ≥ u a.e. in (0,T)× R+.

Find u ∈ K ∩ C0([0,T ]; L2(R+)),∂u∂t∈ L2(0,T ; V ′),

s.t. (∂u∂t,v− u) + at(u,v− u) ≥ 0, ∀v ∈ K,

u(t = 0) = u (8)

This is similar to the ice-water problem in engineering.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 52 / 110

Page 57: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Semi-Smooth Newton Method (K.Kunisch)

After time discretization reformulate the problem as

a(u, v)− (λ, v) = (f , v) ∀v ∈ H1(R+), i .e.Au − λ = fλ−min0, λ+ c(u − φ) = 0,

The last eq. is equivalent to λ ≤ 0, λ ≤ λ+ c(u − φ) i.e. u ≥ φ, λ ≤ 0,with equality on one of them for each x . This problem is equivalent forany real constant c > 0 because λ is the Lagrange multiplier of theconstraint.

Newton’s algorithm gives• Choose c > 0, ,u0, λ0, set k = 0.• Determine Ak := x : λk (x) + c(uk (x)− φ(x)) < 0• Set uk+1 = arg minu∈H1(R+)a(u,u)− 2(f ,u) : u = φ on Ak• Set λk+1 = f − Auk+1Theorem For any c > 0 uk → u solution of the problem.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 53 / 110

Page 58: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Tutorial 3

1 Compute a Barrier Option by 2 methods: Monte-Carlo andvanilafem.cpp

2 Plot on the same graph S0 → P0 := P(S0,0) when P0 is the aEuropean or an American Put computed by P solution of the PDE(program amerkunisch.cpp or the variational inequality. Trykmax=1, kmax=5 and kmax=20.

3 From now on stick to kmax=5. Find the asset prize for which theAmerican put is exercised today (t=0).

4 By using the Black-Scholes formula for the European put,compute the L2 error when amerkunisch.cpp is used for aEuropean option versus N = 1,2,3 when nS,M are changed toN ∗ nS,N ∗M. Plot this error in log-log scale versus N.

5 For one S0 compare the computing time for the American optionwhen amereuropmc.cpp is used or longstaff.cpp is used.

Results to be sent by email in MSWord format to [email protected] undersubject TUTO2+ your nameO. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 54 / 110

Page 59: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 55 / 110

Page 60: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Multidimensional problems: Basket Options

Basket Option with correlated Brownians E(dWidWj) = σijdt

dSi = Si(rdt + dWi), i = 1..d , u = e−(T−t)rE(∑

Si − K )+

Ito calculus leads to a multidimensional Black-Scholes equation

∂τu −∑

ij

(σijxixj

2∂xi xj u − rxi∂xi u) + ru = 0 u(0) = (

∑i

xi − K )+

Variational Formulation As before : (uτ ,w) + aτ (u,w) = 0, ∀w ∈ V

aτ (u,w) =∑

ij

(σijxixj

2∂xi u, ∂xj w)− (rxi −

∑j

∂xj

σijxixj

2)∂xi u,w) + (ru,w)

Discretization is best by Euler implicit in time and FEM in space.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 56 / 110

Page 61: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Exemple: an Option on a Basket of 2 Assets

dS1 = S1(rdt + σ1dW1), dS2 = S2(rdt + σ2dW2),u = e−(T−t)rE(K − S1 − S2)+

with E(dW1dW2) = qdt ,. . To generate W1,W2, take 2 uncorrolatedBrownians W1,w2 and set

dW2 = qdW1 +√

1− q2dw2 ⇒E(dW1dW2) = qE(dW 2

1 ) = qdtE(dW 2

2 ) = q2E(dW 21 ) + (1− q2)E(dw2

2 ) = dt

The PDE for the put option is (with time to maturity)

∂tu −x2σ2

12

∂xxu − y2σ22

2∂yyu − qσ1σ2xy∂xyu − rx∂xu − ry∂yu + ru

u(x , y ,0) = (K − x − y)+ ∀x , y ∈ R+ × R+

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 57 / 110

Page 62: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Source Code in C++ for the 2D case (I)

Page: 1/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cppSunday 18 November 2007 / 18:17

#include <iostream>#include <fstream>#include <math.h>#include <cmath>#include <stdlib.h>#include <assert.h>#define NDEBUG // uncomment when debugging is overusing namespace std;

template <class T> class A // this repaces arrays and has bound checks public: T *cc; long size; void init(long ssize) assert( !cc && ssize ); // don't create twice the same object size=ssize; cc= new T[size]; assert(cc != 0); // make sure it exists T& operator [] (long i) const assert ( cc && (i >= 0) && (i < size) ); return cc[i];

A(long csize = 0) size = csize; cc = (size > 0) ? new T[size] : 0; ~A() delete [] cc; size = 0; cc = 0; void destroy() delete [] cc; size = 0; cc = 0;

int no( T* t) return t - cc; // return the place in the array ;

class Vertex public:double x, y; // cordinates

int where; // on which boundary ;

class Triangle public: Vertex* v[3]; // the 3 vertices of the triangle int where; // in whichregion double area; ;

class Grid public: int nt, nv; // nb of triangles,verties and edges A<Vertex> v; // all vertices A<Triangle> t; // all triangles

Grid(const char *path ); // reads a triangulation in freefem format int no(Triangle* tt) return t.no(tt); // the place in array t of triangle tt int no(Vertex* tt) return v.no(tt); // the place in array v of Vertex tt 32 int operator()(int k,int iloc) return no(t[k].v[iloc]); //same as no(vertex) ;

Grid::Grid(const char *path ):v(),t()

Page: 2/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cppSunday 18 November 2007 / 18:17Grid::Grid(const char *path ):v(),t() // reads a triangulation in freefemformat

int i0,i1,i2;ifstream file(path); if(!file) cout<<"can t find triangulation file"<<endl; file >> nv >> nt; v.init(nv); t.init(nt); for(int i=0; i<nv; i++ ) file >> v[i].x >> v[i].y >> v[i].where; for(int i=0; i<nt; i++ ) file >> i0 >> i1 >> i2 >> t[i].where; t[i].v[0] = &v[i0-1]; t[i].v[1] = &v[i1-1];

t[i].v[2] = &v[i2-1]; t[i].area = ((t[i].v[1]->x - t[i].v[0]->x) * (t[i].v[2]->y - t[i].v[0]->y) - (t[i].v[2]->x - t[i].v[0]->x) * (t[i].v[1]->y - t[i].v[0]->y))/2;

void bMul(A<double>& bu, Grid& g, A<double>& u, double alpha, double b1, double b2) double rs1,rs2; alpha; // avoids warning as alpha is unused here

for(int i=0; i<g.nv; i++) bu[i] = 0; for(int k=0; k<g.nt;k++) for(int iloc = 0; iloc < 3; iloc++)

int i = g(k,iloc); int ip = g(k,(iloc+1)%3); int ipp= g(k,(iloc+2)%3); rs1=1/12*b1*(2*g.v[i].x+g.v[ip].x+g.v[ipp].x); rs2=1/12*b2*(2*g.v[i].y+g.v[ip].y+g.v[ipp].y); double a = (u[g(k,0)] + u[g(k,1)] + u[g(k,2)])/3; if (!g.v[i].where) bu[i]+=a*(rs1*(g.v[ipp].y-g.v[ip].y)-rs2*(g.v[ipp].x-g.v[ip].x))/2. ;

void aMul(A<double>& au, Grid& g, A<double>& u, double alpha, double s11, double s12, double s22,double a)

double k11,k12,k22, a3 = 1/3.;

for(int i=0; i<g.nv;i++) au[i] = 0; for(int k=0; k<g.nt;k++) int i=g(k,0),ip=g(k,1),ipp=g(k,2);

k11 = a3 * s11 *( pow(g.v[i].x,2) +pow(g.v[ip].x,2) +pow(g.v[ipp].x,2)); k22 = a3 * s22 *( pow(g.v[i].y,2) +pow(g.v[ip].y,2) +pow(g.v[ipp].y,2)); k12 = a3 * s12 *( g.v[i].y*g.v[i].x +g.v[ip].y*g.v[ip].x +g.v[ipp].y*g.v[ipp].x);

for(int iloc = 0; iloc < 3; iloc++) int i = g(k,iloc);

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 58 / 110

Page 63: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Source Code in C++ for the 2D case (II)

Page: 3/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cppSunday 18 November 2007 / 18:17 int i = g(k,iloc); int ip = g(k,(iloc+1)%3); int ipp= g(k,(iloc+2)%3);

for(int jloc = 0; jloc<3; jloc++) int j = g(k,jloc); int jp = g(k,(jloc+1)%3); int jpp= g(k,(jloc+2)%3); double aijk = a*(k22*(g.v[jpp].x - g.v[jp].x)*(g.v[ipp].x - g.v[ip].x) +

k11*(g.v[jpp].y - g.v[jp].y)*(g.v[ipp].y - g.v[ip].y) - k12*((g.v[jpp].x - g.v[jp].x)*(g.v[ipp].y - g.v[ip].y)

+ (g.v[jpp].y - g.v[jp].y)*(g.v[ipp].x - g.v[ip].x))) /g.t[k].area/4.; if (!g.v[i].where) au[i] += aijk * u[j]; if (!g.v[i].where) au[i] += (u[i]*2.+u[ip]+u[ipp])* g.t[k].area * alpha/ 12.;

void solvecg(Grid& g, A<double>& f, A<double>& u, int nIter, double precise,double alpha, double s11,double s12,double s22)

int nv = g.nv; A<double> au(nv), ag(nv), grad(nv), hh(nv), diag(nv);

double normOldGrad = 1e60; for(int m=0; m<nIter ; m++) aMul(au, g, u, alpha,s11,s12,s22,1);

double normGrad = 0; for(int i=0;i<nv; i++) if(!g.v[i].where) grad[i] = (au[i] - f[i]) ; // / diag[i];

normGrad += pow(grad[i],2) ;// * diag[i]; double gh =0, gamma = normGrad / normOldGrad;normOldGrad = normGrad;

for(int i=0;i<nv; i++) if(!g.v[i].where) hh[i] = gamma * hh[i] - grad[i];

gh += grad[i] * hh[i];

aMul(ag,g,hh,alpha,s11,s12,s22,1);

double rho = 0; for(int i=0;i<nv; i++) if(!g.v[i].where) rho += hh[i] * ag[i]; rho = - gh / rho ; for(int i=0;i<nv; i++) if(!g.v[i].where) u[i] += rho * hh[i];

Page: 4/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cppSunday 18 November 2007 / 18:17 for(int i=0;i<nv; i++) if(!g.v[i].where) u[i] += rho * hh[i]; // cout << " nb iter=" <<m<<" normGrad = " <<normGrad<< endl; if(m==0) precise = normGrad * pow(precise,2); if(normGrad < precise) cout << " nb iter=" <<m<<" normGrad = " <<normGrad<< endl; return;

void myexit() cout<<"program ended at myexit()"<<endl;

int main() atexit(myexit); // for debugging Grid g("carre.msh"); // triangulated square for(int i=0;i<g.nv;i++)// a hack to put axis at Neumann cond if(g.v[i].where == 1 || g.v[i].where == 4) g.v[i].where = 0;

const int itermax=25; // financial data const double T =0.7, dt = T/itermax, r=0.05, K1=75, K2=100, s1=0.3, s2=0.2, s11=s1*s1, s12=-s1*s2*0.6, s22=s2*s2; double t=0; A<double> u0(g.nv), u1(g.nv); A<double> f(g.nv),x(g.nv),f1(g.nv),f2(g.nv);

for(int i=0; i<g.nv;i++) // set initial time value double a = (g.v[i].x > g.v[i].y )? K1-g.v[i].x : K2-g.v[i].y ;

u0[i] = a>0 ? a : 0; u1[i]=0;

for(int timeIter=0; timeIter < itermax; timeIter++) // time loopt+=dt;

aMul(f2,g,u0,1./dt,0.,0.,0.,0); bMul(f1,g,u0,0,r,r); for(int i=0;i<g.nv;i++) f[i]=f1[i]+f2[i];

cout<<"timeiter = "<<timeIter+1<<" temps = "<<t<<'\t';solvecg(g,f, u1,200, 0.1e-5, r+1./dt,s11,s12,s22);

for(int i=0; i<g.nv;i++) u0[i] =u1[i] ;

ofstream plot("plot2"); for(int it=0;it<g.nt;it++)

plot <<g.v[g(it,0)].x <<" "<<g.v[g(it,0)].y << " " << u0[g(it,0)] << endl <<g.v[g(it,1)].x <<" "<<g.v[g(it,1)].y << " " <<u0[g(it,1)] << endl <<g.v[g(it,2)].x <<" "<<g.v[g(it,2)].y << " " << u0[g(it,2)] << endl <<g.v[g(it,0)].x <<" "<<g.v[g(it,0)].y << " " << u0[g(it,0)] << endl <<endl<<endl; return 0;

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 59 / 110

Page 64: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

SuperLU, Hypre... (N. Lantos)

Mesh size LU [s] Relative error superLU[s] Relative error101× 101 10.094 3.532 % 2.39 3.076 %126× 126 14.547 1.338 % 4.016 1.797 %151× 151 22.313 0.751 % 6.203 0.489 %176× 176 31.985 1.131% 8.735 0.790 %201× 201 43.938 0.432 % 12.109 0.670%

Comparison of CPU time for the LU algorithm and superLU for aproduct put option on a uniform mesh and 200 step time.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 60 / 110

Page 65: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Numerical Results: adapted meshes

24.045322.779821.514220.248718.983117.717616.452115.186513.92112.655411.389910.12438.85887.593266.327725.062173.796632.531091.265541e-06

Basket Euro Put option nine months to maturity: adapted meshand level lines. we solve 2D-B&S, with P0 = (K −max(S1,S2))+. Wetake σ11 = σ22 = 0.2, 2ρ

1+ρ2 = −0.6, K = 25 and r = 0.05

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 61 / 110

Page 66: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Basket with 3 Assets

Use http://ww.freefem.org/freefem3D :

double N = 25; double L=200.0; double T=0.5;double dt = T / 15 ; double K=100; double r = 0.02;double s1 = 0.3; double s2 = 0.2; double s3 = 0.25;double q12 = -0.2*s1*s2; double q13 = -0.1*s1*s3;double q23 = -0.2*s2*s3; double s11 = s1*s1/2;double s22=s2*s2/2; double s33=s3*s3/2;

vector n = (N,N,N);vector a = (0,0,0);vector b = (L,L,L);mesh M = structured(n,a,b);femfunction uold(M) = max(K-x-y-z,0);femfunction u(M);

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 62 / 110

Page 67: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

3D Basket Option(II)

double t=0; dosolve(u) in M cg(maxiter=900,epsilon=1E-10)

pde(u)(1/dt+r)*u-dx(s11*x^2*dx(u))-dy(s22*y^2*dy(u))-dz(s33*z^2*dz(u))- dx(q12*x*y*dy(u)) - dx(q13*x*z*dz(u)) - dy(q23*y*z*dz(u))- r*x*dx(u) - r*y*dy(u) - r*z*dz(u) = uold/dt;

dnu(u)=0 on M;; t = t + dt;

while(t<T);save(medit,"u",u,M);save(medit,"u",M);

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 63 / 110

Page 68: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Sparse Grids (in more than 3 dimensions)

If f is analytic then ∫D

f ≈∑

i

f (qi)ωi

where most of the points are on ∂D. The argument is recursive.S.A. Smolyak: Quadrature and interpolation formulas for tensorproducts of certain classes of functions Dokl. Akad. Nauk SSSR 4pp 240-243, 1963.Michael Griebel: The combination technique for the sparse gridsolution of PDEs on multiprocessor machine. Parallel ProcessingLetters 2 1(61-70), 1992.In polynomial approximations of f most of the mixed terms x i

1x j2...

are not needed.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 64 / 110

Page 69: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Sparse Grids (when dimension d > 3)

0 0.5 1 1.5 2 2.5 3 3.5 4

0 0.5 1 1.5 2 2.5 3 3.5 4

-0.1 0

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

1

"Bsk_Put_SparseG"

Computed by D. Pommier

See also C. Schwab et al. who can solve up to dimension 20 and aPIDE in dimension 5. Nils Reich built a sparse tensor product waveletcompression scheme of complexity O( 1

h | log h|2(d−1)).

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 65 / 110

Page 70: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Multidimensional Problems: Stochastic Volatilities

dSt = µStdt + σtStdWt ,

Let (σt ) be function of a mean reverting Orstein-Uhlenbeck process:

σt = f (Yt ), dYt = α(m − Yt )dt + βdZt ,

where α, m, β are positive constants, and (Zt ) is Brownian.The law of Yt knowing Y0 is N

(m + (Y0 −m)e−αt , β

2

α (1− e−2αt ))

.

Therefore, m = limt→+∞ EYt and 1α is the time of mean reversion.

ν2 := β2

α = limt→∞ varYt . As Zt and Wt are corrolated we take aBrownian (Zt ) Zt = ρWt +

√1− ρ2Zt .

One can hedge a self-financing portfolio ct with at shares of St , one optionwith maturity T1, P(1)

t := P(1)(St ,Yt , t) and bt options with maturity T2 > T1,P(2)

t := P(2)(St ,Yt , t). By the no arbitrage principle

dct = atdSt + dP(1)t + btdP(2)

t = rtctdt = rt (atSt + P(1)t + btP

(2)t )dt

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 66 / 110

Page 71: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

The PDE when Yt is a OU process

Pt = P(St , y , t) with P(S, y ,T ) = P(S) and

∂P∂t

+12

f (y)2S2∂2P∂S2 + r(t)

(S∂P∂S− P

)︸ ︷︷ ︸

BSf (y)

+ ρβSf (y)∂2P∂S∂y︸ ︷︷ ︸

correlation

+12β2∂

2P∂y2 + α(m − y)

∂P∂y︸ ︷︷ ︸

Orstein Uhlenbeck

−βΛ(S, y , t)∂P∂y︸ ︷︷ ︸

premium

= 0,(9)

where Λ(S, y , t) is arbitrary if |ρ| 6= 1: prime on the volatility risk. Wecan write Λ(S, y , t) as

Λ(S, y , t) = ρµ− r(t)

f (y)+√

1− ρ2γ(S, y , t),

where the function γ(S, y , t) can be chosen arbitrarily.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 67 / 110

Page 72: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Frequently used Models

Authors ρ f (y) Yt processHull-White ρ = 0 f (y) =

√y lognormal

Scott ρ = 0 f (y) = ey mean reverting OUStein-Stein ρ = 0 f (y) = |y | mean reverting OU

Heston ρ 6= 0 f (y) =√

y CIR

lognormal process:

dYt = c1Ytdt + c2YtdZt ,

mean reverting Orstein-Uhlenbeck (OU) process:

dYt = α(m − Yt )dt + βdZt ,

mean reverting Cox-Ingersoll-Ross (CIR) process

dYt = κ(m − Yt )dt + λ√

YtdZt .

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 68 / 110

Page 73: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Stein-Stein Model with freefem++

real T=5, K=120, r=0.0, mu =0.4,...; int n=1,L=500, LL=250, ...;mesh th = square(50*n,50*n,[x*L,y*LL]);fespace Vh(th,P1);Vh u=f,v,w,rhs;problem eq1(u,v,init=j) = int2d(th)( u*v/dt+ dx(u)*dx(v) *x*x*y*y*a11 + dy(u)*dy(v) *y*y*a22+ dy(u)*dx(v) *y*y*x*a12 + dx(u)*dy(v) *y*y*x*a21+ dx(u)*v *x*(y*( a1 + y/sc2)-r) + dy(u)*v *y*(y*a2+mu2))+ rhs[] + on(1,u=max(K-x,0.)) ;varf vrhs(u,v) = int2d(th)( v*u/dt );matrix M = rhs(Vh,Vh);for (int n=0; n<Nmax ; n++)w=u; rhs[] = M*u[]; eq1 ; t= t+dt; j++;if(n==20*(nM = vrhs(Vh,Vh); rhs =0; u=max(K-x,0.); t=0; j=0; Execute

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 69 / 110

Page 74: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Some Results I: Stein-Stein Model

The contours of the price computed in Ω = (0,800)× (−3,3): note theboundary layers due to artificial boundary conditions on y = ±3.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 70 / 110

Page 75: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Some Results II: Heston Model

r = 0, ρ = −0.5, κ = 2.5, λ = 0.5, m = 0.06, K = 1.

0.5 1 1.5 2 2.5 3

0

0.5

1

1.5

2

2.5

3

0.55

0.5

0.45

0.4

0.35

0.3

0.25

0.2

0.15

0.1

0.05

The contours of the pricing function of a put option with Heston modelhalf-year to maturityO. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 71 / 110

Page 76: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

American Options with Stochastic Volatility: OUprocess

0 20 40 60 80 100

-1

-0.5

0

0.5

1

"exercise"

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 72 / 110

Page 77: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Option Pricing with Jump processes

Poisson ProcessA random variable τ with exponential law (e.l.r.v.) is:

P(τ > y) = e−λy (10)

Let τi be a set of independent e.l.r.v. with the same λ. Let

Tn =n∑1

τi , Nt =∑n≥1

1t≥Tn

By definition Nt is a Poisson process. It can be used to model thenumber of buses which arrive at one station.Theorem Nt − Ns and Nt−s have the same law.

P(Nt = n) = e−λt (λt)n

n!

If it is known that NT = n then T1, ...,Tn are uniformly distributed on(0,T ).O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 73 / 110

Page 78: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Poisson Process

definition A compound Poisson process is Zt =∑Nt

1 Yi where Yiare independent r.v. with law f . Consequently a numerical method togenerate Xt is as follows:Algorithm

1 Generate NT by (10)2 Generate UiNT

1 uniform and random on (0,T ).3 Generate NT random variables YiNT

1 of law f4 Set Zt =

∑NT1 Yi1Ui≤t

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 74 / 110

Page 79: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Jump-Diffusion Process

Let Wt be a Brownian and Zt be a compound Poisson process. Letγ ∈ R , then Xt is a jump-diffusion process

Xt = γt + σWt + Zt (11)

Theorem Let ν(x) = λf (x); the characteristic function of Xt is

E(eiuXt ) = exp(t(iγu − σ2

2+

∫R

(eiux − 1− iu1|x |<1)ν(dx)))

Conversely if ν ∈ L1(R) and∫

[−1,1] x2ν(dx) <∞, the (??) is thecharacteristic function of a general Poisson process.Formula (??) is refered as Levy-Khintchine’s.Theorem The infinitesimal generator of the semi-group Xt is

LX := limt→0

1t

(E(φ(x + Xt )− φ(x))

=σ2

2∂xxφ+ γ∂xφ+

∫R

(φ(x + y)− φ(x)− y1|y |<1∂xφ(x))ν(dy)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 75 / 110

Page 80: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

The Generalized Black-Scholes Model

Let Xt be a jump-diffusion process, given by (11) Consider thefollowing model for the underlying asset St :

dSt = S−t (rdt + dXt ), S0 given, i.e. St = S0ert+Xt

Theorem St is a martigale if

γ +σ2

2+

∫R

(ex − 1− x1|x |<1)ν(dx) = 0

In Merton’s model ν(x) = λ√2πδ

exp(− (x−µ)2

2δ2 )

The variance gamma (Y = 0) or the CGMY processes whereη± > 0, η+ > 2

ν(y) =exp(−|y |η±)

ω|y |1+Y

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 76 / 110

Page 81: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Example: Merton’s model

Theorem The call on S at t=0, C can be computed by

C = e−rT∞∑0

e−λT (λT )n

n!ernT CBS(S0enδ2/T ,K ,T , σn, rn)

where rn = r − λ(eµ+δ2/2 − 1) + µTn σn =

√σ2 + n

δ2

2

Theorem The put on S is solution of

∂tP +σ2S2

2∂SSP + rS∂SP − rP

+

∫R

(P(Sey )− P(S)− S(ey − 1)∂SP(S))ν(dy) = 0 (12)

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 77 / 110

Page 82: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Numerical Method

The finite element method applies equally well to this case, however toavoid solving a full linear system it is wise to use a semi-implicite timescheme whereby the integrodifferential term is discretized explicitly.Then at each time step the right hand side of the linear system isPm+1δt + vδt and

v =

∫R

(P(Sey )− P(S)− S(ey − 1)∂SP(S))ν(dy)

≈N∑

j=0

ν(log SjSi

)

j + 12

[Pj − Pi − (Pi+1 − Pi)Sj − Si

δx(13)

Theorem The scheme is stable iff δt ≤ C δxλ+δx

Hedging: See Tankov’s book or Tankov-Voltchkova’ paperCalibration It is possible to adjust the parameters λ, ν, so as toreproduce some real options.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 78 / 110

Page 83: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Some Results with CGMY

60 80 100 120 140 0 0.05

0.1 0.15

0.2

0

10

20

30

40

50

60

"european_alltimes"

0

10

20

30

40

50

60

70

80

90

100

0 100 200 300 400 500 600 700 800

"european_0.5""european_1.5""european_0.9""european_1.2"

G = 1.8, M = 2.5, C = 1.Left: Y = 1.5 near maturity. Right: Y = 0.5, 0.9, 1.2, 1.5, one year to

maturity.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 79 / 110

Page 84: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Tutorial 4

1 Run edostoch2.cpp with your own set of parameters2 Replace the payoff by (K1 − S1T )(K2 − S2T ) and check that when

q = 0 (no corrolation) the value of the put can also be computedas the product of the results of 2 calls of the Black-Scholesanalytical formula times erT ..

3 Download the Basket option pricer from the web sitehttp://paul.wilmott.com/software.cfm and check that it agrees withyour results.

4 Modify edostoch2.cpp to implement Heston’s model.5 Check your results by going to the site

http://kluge.in-chemnitz.de/tools/pricer/6 Add the integral operator to explicitfdm.cpp to compute a put

with a jump process instead of a Brownian motion.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 80 / 110

Page 85: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 81 / 110

Page 86: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Calibration: How to choose σ(S, t)?

... by trying to reproduce the prices observable on the market:

Every day, one can observe (in the news)• The underlying asset So• The values (ci)i∈I of calls (CKi ,Ti (S0,0))i∈I .

Inverse Problem : find σ(x , t) s.t. for all , Cii∈I given by

∂tCi +σ2x2

2∂2

xxCi + rx∂xCi − rCi = 0,

Ci(x ,Ti) = (x − Ki)+, for all t ∈ [0,Ti[, x ∈ R+,

such that Ci(x0,0) = ci ...it involves as many B&S as different Ki , i ∈ I.

min∑|Ci(x0,0)− ci |2 : subject to all PDEs, i=1..I

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 82 / 110

Page 87: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Dupire’s Equation

Theorem

CS,t (T ,K ) = CT ,K (S, t) where C solves:

∂tC −σ2(t , x)x2

2∂2

xxC + rx∂xC = 0, C(0, x) = ( S − x )+

Proof (r=0)

Let ∂tu + µ(x , t)∂xxu = 0; Let ∂tp − ∂xx (µp) = 0

Then, with appropriate boundary conditions

.

∫R+

u(0)p0 =

∫R+

uT p(T ) = u(x0,0) when p(x ,0) = δ(x − x0)

Let ∂xxv = p then ∂tv − µ∂xxv = ax + b, v(0) = c + d(x − x0)+

Finally uT = (K − x)+, ⇒ ∂xxuT = −δ(x − K ) ⇒ u(x0,0) = −v(K ,T )d

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 83 / 110

Page 88: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Calibration with Dupire’s equation

Inverse Problem : find σ(S, t) s.t.

minσ

∑|u(Ki ,Ti)− ci |2

subject to

∂tu −σ2x2

2∂2

xxu + rx∂xu = 0, .

u(x ,0) = (S0 − x)+

Only one PDERemarks

Only one PDELet u be a C2 extrapolation of Ki ,Ti , ciI , then σ2 = ∂t u

12∂

2xx u+rx∂x u

is

solutionExtension to Black-Scholes with jumps can be done by the samemethod. The result is a similar optimal control problem but with aPIDE.Works only when Dupire’s apply?

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 84 / 110

Page 89: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Duality at the Discrete Level

FEM + Euler implicit with time step δt ⇒

(B + A)Cn − BCn+1 = 0 CN = CT

where Cn is the vector of values of Ch(qi ,nδt) and A,B are

Bij =1δt

∫Ωh

W iW j , Aij = a(W i ,W j)

the bilinear form of the PDE. Let Pn+1 be

(A + B)T Pn+1 − BT Pn = 0 P0 = P0

Then 0 = PnT(A + B)Un − PnT BUn+1 = Pn−1T

BUn − PnT BUn+1

Summing up over all n gives P0TBU1 = PN−1T

BUN

Choosing P0j = δij gives (

I∑j=1

bij)U1i ≈ (BU1)i = PN−1T

BUN

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 85 / 110

Page 90: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 86 / 110

Page 91: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Results on a Basket Call

S1\S2 20 50 80 110 14020:c -0.097 -0.089 5.61 31.57 61.4920:d 1.1e-07 0.0065 5.88 32.11 61.6750:c 4.32 31.50 61.49 91.49 31.5750:d 4.39 31.52 61.81 91.44 32.1180:c 61.49 91.49 121.49 61.49 5.6180:d 61.70 91.99 121.62 61.81 5.88110:c 121.49 151.49 91.5 31.50 -0.089110:d 122.23 151.86 91.99 31.52 0.0065140:c 181.49 121.5 61.49 4.3 -0.097140:d 181.60 122.2 61.69 4.3 0

Comparison between direct calculation of C based on B&S –lines :c–and U computed by solving the Discrete Kolmogorov-Dupire equation–lines :d–. at T=1, σ1 = 0.2σ2 = 0.3, q = −0.6, rate = µ1 = µ2 = 5%,K= 100.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 87 / 110

Page 92: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Calibration: Implementation

Brute force works but is slow : e.g. FEM and an unknown σ(qi , tj) atevery vertex qi and time tj . The derivative of the cost function withrespect to σ(qi , tj) is computed by using an adjoint and the conjugategradient method is used.

"eta_goal"

0 1 2 3 4 5 0 500 1000 1500 2000 2500 3000

0.025

0.03

0.035

0.04

0.045

0.05

0.055

"eta"

0123450 500 1000 1500 2000 2500 3000

0.0250.030.0350.040.0450.050.055

Solution (right) of an inverse problem with vol on the right (American)Strategy

Define the Vol surface by a splineBuild all the constraints within the parametrizationUse Automatic Differentiationand Conjugate Gradient or quasi-Newton Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 88 / 110

Page 93: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Smile with a Cheap Spline

Ex: σ(x , t) C1, bilinear in an (x,t) parallelogram, grows large outside⇒8 parameters: Sij , σij but 0 < S1(t) < S2(t) is needed so set

S11 = z20 , S21 = z2

1 , S12 = S11 + z22 , S22 = S21 + z2

3 ,

σ1j =z2

3+j

1 + z23+j

σ2j =z2

5+j

1 + z25+j

, j = 1,2

so as to work with an unconstrained set of parameters zi70Let Si , σi , i = 1,2 be linear in t :

Si = Si1(1− tT

) + Si2tT, σi = σi1(1− t

T) + σi2

tT

The vol surface is (a = σ(0, t)):

σ(S, t) =

a + (2σ1−a

S1− σ2−σ1

S2−S1)S + ( σ2−σ1

S2−S1− σ1−a

S1)S2

S1if (S < S1)

σ2S−S1S2−S1

+ σ1S2−SS2−S1

if S1 ≤ S ≤ S2

σ2 + (S − S2) σ2−σ1S2−S1

+(

(S − S2) σ2−σ1S2−S1

)2if S > S2

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 89 / 110

Page 94: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Observation Data

Strike 1 Month 2 Months 6 Months 12 Months 24 Months 36 Months700 733800 650.6900 569.81000 467.8

... ... ... ... ... ... ...1215 253.41225 219 2451250 196.6 224.2 269.21275 174.5 203.9 2511300 152.9 184.1 233.21325 131.9 164.9 215.81350 111.7 146.3 198.91365 1001375 50.6 60 92.5 139 182.61380 46.1 55.81385 41.8 51.8

... ... ... ... ... ... ...1700 32.7 75,71800 15.51900 5.2

index SPX on 21.12.2006) at spot price 1418.3, r=3/100

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 90 / 110

Page 95: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Results I

0 500 1000 1500 2000 2500 3000 3500 4000 0 0.5

1 1.5

2 2.5

3

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

1

"usigma.txt"using 1:2:4

0 500 1000 1500 2000 2500 3000 3500 4000 0 0.5

1 1.5

2 2.5

3

-200 0

200 400 600 800

1000 1200 1400

"usigma.txt"using 1:2:3

10

20

30

40

50

60

70

0 5 10 15 20 25 30 35 40 45 50

"converge.h"using 1:2"converge.h"using 1:3

600 800

1000 1200

1400 1600

1800 0.5

1

1.5

2

2.5

3

0 100 200 300 400 500 600 700 800

"uduo.txt"using 1:2:3"uduo.txt"using 1:2:4

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 91 / 110

Page 96: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 92 / 110

Page 97: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Calibration of the Implied Volatility surface

Definition Let BS(S0, σ, r ,T ,K ) denote the Black-Scholes formula forthe input S0, σ, r ,T ,K . The implied volatility at Ct ,St , t is the solution inσ of the equation

BS(St , σ, r ,T − t ,K ) = Ct

for given St , t , r ,T ,K ,Ct .

Given a set of data Si ,Ti − ti ,Ki ,CiM1 we can compute the set ofimplied volatilies Σi Now we solve the Σ:

minΣ∈S

J(Σ) :=M∑1

|Σ(Si , ti)− Σi |2

Then the Black-Scholes PDE is automatically satisfied. The art of thetrade is to find the right constraints for S.

Σ >= Σm, Σ(+∞) = +infty , Σ(0) = +infty , |Σ”| <= D

Note that the true Vol is not needed: use BS to valuate other options.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 93 / 110

Page 98: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Tutorial V

1 Compute the same put using explicitfdm.cpp and using theput-call parity and explicitfdm.cpp set up for Dupire’s eq..

2 Compute ∂K P0 by using explicitfdm.cpp with payoff 1S>K and byfinite difference using explicitfdm.cpp + Put-Call-Parity set up forDupire. Compare also to the finite difference approximation whichuses 2 calls to the Black-Scholes formula.

3 The CEV model is the Black-Scholes PDE withσ(S, t) = σ0K 0.3 (KeS−r(T−t))−0.3

where σ0 is a parameterconstant. Compute the put with explicitfdm.cpp and compare theimplied volatility at one point with the CEV volatility.

4 Add the integro-differential term which models the jump-diffusionmodel to the right hand side of the PDE in explicitfdm.cpp andplot the results S → P(S).

On a rainy week-end you might think of a/ Write a C/C++ -function for a compoundPoisson process with λ = 5 and f so that the Yi are Gaussian. b/ Price a Europeanput with a Black-Scholes stochastic ODE where the Gaussian process is replaced bya jump-diffusion.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 94 / 110

Page 99: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Explicit Finite Difference with right hand side is

Pm+1i − Pm

i

δt+ rPm+1

i − σ2

2δx2 (Pmi+1 − 2Pm

i + Pmi−1)− 2r − σ2

2δx(Pm

i+1 − Pmi−1) = vi

With jump-diffusion model CGMY ν(y) = exp(−|y |η±)ω|y |1+Y and vi is

vi =

∫R

(P(Sey )− P(S)− S(ey − 1)∂SP(S))ν(y ,dy)|S=Sj

≈N∑

j=0

[ν(logSj + δS

Si)− ν(log

Sj

Si)][Pj − Sj

Pi+1 − Pi

δS] (14)

Take η+ = η− = 3,Y = 1, ω = 1. Recall that the formula is truebecause in terms of y , Sj = Siey

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 95 / 110

Page 100: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 96 / 110

Page 101: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Steepest Descent Method

Objective: Use inexact gradient in the context of mesh refinement

minz∈Z

J(z) approximated by minz∈Zh

Jh(z).

Algorithm(Steepest descent with Goldstein’s rule). while ‖ gradzJh(zm)‖ > ε do. . zm+1 = zm − ρ gradzJh(zm) where ρ is any number satisfying,

. − βρ‖w‖2 < Jh(zm − ρw)− Jh(zm) < −αρ‖w‖2

. with w = gradzJh(zm). Set m := m + 1;

.

Improvement: Use wm = −ρ gradzJh(zm) + λwm.−1 be defined withthe best rho, λ with respect to J.O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 97 / 110

Page 102: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 98 / 110

Page 103: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Principle of Automatic Differentiation

Let J(u) = |u − ud |2, then its differential is

δJ = 2(u − ud )(δu − δud )∂J∂u

= 2(u − ud )(1.0− 0.0)

and obviously the derivative of J with respect to u is obtained byputting δu = 1, δud = 0. Now suppose that J is programmed in C/C++by

double J(double u, double u_d)double z = u-u_d; z = z*(u-u_d);return z;

int main() double u=2,u_d = 0.1;

cout << J(u,u_d) << endl;

A program which computes J and its differential can be obtained bywriting above each differentiable line its differentiated form:O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 99 / 110

Page 104: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

A simple example (cont)

double JandDJ(double u, double u_d, double du,double du_d, double *pdz)

double dz = du - du_d, z = u-u_d;double dJ = dz*(u-u_d) + z*(du - du_d);z = z*(u-u_d); *pdz = dz;return z;

int main() double u=2,u_d = 0.1;

double dJ;cout << J(u,u_d,1,0,&dJ) << endl;

Except for the embarrassing problem of returning both z,dz instead of z, theprocedure can be automatized by introducing a structured type holding thevalue of the variable and of its derivative:

struct double val[2]; ddouble;

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 100 / 110

Page 105: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

A simple example (II)

ddouble JandDJ(ddouble u, ddouble u_d) ddouble z;z.val[1] = u.val[1]-u_d.val[1];z.val[0] = u.val[0]-u_d.val[0];z.val[1] = z.val[1]*(uval[0]-u_d.val[0])

+ z.val[0]*(uval[1]-u_d.val[1]);z.val[0] = z.val[0]*(uval[0]-u_d.val[0]);return z;

int main() ddouble u;u.val[0]=2;u_d.val[0]=0.1;u.val[1]=1;u_d.val[1]=0;ddouble dJ;cout <<J(u,u_d).val[0]<<J(u,u_d,1,0).val[1];

In C++ the program can be simplified further by redefining the operators =, -and *. Then a class has to be used instead of a struct!

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 101 / 110

Page 106: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

The class ddouble

class ddouble public: double val[2];ddouble(double a, double b=0) v[0] = a; v[1]=b;ddouble operator=(const ddouble& a) val[1] = a.val[1]; val[0]=a.val[0];

return *this; friend dfloat operator - (const dfloat& a, const dfloat& b)

dfloat c;c.v[1] = a.v[1] - b.v[1]; // (a-b)’=a’-b’c.v[0] = a.v[0] - b.v[0];return c;

friend dfloat operator * (const dfloat& a, const dfloat& b) dfloat c;

c.v[1] = a.v[1]*b.v[0] + a.v[0]* b.v[1];c.v[0] = a.v[0] * b.v[0];return c;

;

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 102 / 110

Page 107: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Limitations

program newtontestx=0.0;al=0.5 subroutine newton(x,n,al)call newton(x,10,al) do i=1,nwrite(*,*) x f = x-alpha*cos(x)end fp= 1+alpha*sin(x)

x=x-f/fpenddoreturnend

2n adjoint variables are needed! while the theory is

f (x , α) = 0 ⇒ x ′f ′x + f ′α = 0 ⇒ x ′ = − f ′αf ′x

So it is better to understand the output of AD-reverse and clean it.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 103 / 110

Page 108: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Outline1 Lesson I: Introduction to Scientific Computing in Finance

Stochastic ModelingAdditional Material: Trees

2 Lesson II: Studying the Black-Scholes PDETransformations and Analytical SolutionsFinite Difference MethodsComparison Between Finite Differences and Monte-Carlo

3 Part II: Advanced Numerical MethodsThe Finite Element MethodAmerican OptionsChallenging Numerical Problems

4 Part III : CalibrationDupire’s equationAn ExampleImplied Volatilities

5 Part IV ComplementsConjugate Gradient MethodAutomatic DifferentiationAcceleration Methods

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 104 / 110

Page 109: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Quasi-Newton Method (Levenberg-Marquardt)Let C be sol of Dupire’s with vol σ(a), a ∈ Rna the spline parameters

minσ

J(a) =12

nE∑j=1

|Ej |2 Ej = C(Kj , tj )− Cj ⇒∂J∂ai

=∑

j

∂Ej

∂aiEj

J” = E”E + E ′T E ′ ≈ E ′T E ′ + αIApproximate Newton step: (E ′T E ′ + αI)(am+1 − am) = −E ′E

0 200 400 600 800 1000 1200 1400 1600 1800

0

0.5

1

1.5

2

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

"s.txt"

0 200 400 600 800 1000 1200 1400 1600 1800

0

0.5

1

1.5

2

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

1

"s.txt"

Volatility surface computed with the quasi-Newton method (left) and with theCG method (right).O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 105 / 110

Page 110: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Reduced Basis

Replace the hat functions w i(x) in the formula

uh(x , t) =N∑1

ui(t)w i(x)

by a Galerkin method with M << N:

uRB(x , t) =M∑1

ai(t)ui(x , t) with ai given by (Q = R+ × (t0,T ))∫R+

uσ(T )ui(T )−∫

Q(uRB∂tui + ui σ

2x2

2∂xxuRB) =

∫R+

(K − x)+ui(t0)

Or:∫R

(ui

δt(uRB(t)− uRB(t − δt)) + ui σ

2x2

2∂xxuRB(x , t)) = 0

if ui is independent of t but then uRB(x ,0) = (K − x)+.

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 106 / 110

Page 111: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

POD or SVD to Choose the basis

Snap shots (used with the Navier-Stokes equations)Compute the solution of the parabolic PDE u(x , t) =

∑M1 ai(t)ui(x)

where ui is obtained by Singular Value Decomposition (SVD) of

Aij =

∫Ω

u(ti) · u(tj)dx , A =

[U

M ×M

]λ1

λ2λ3

M × N

[ V T

N × N

].

or the orthogonal decomposition if symmetry A = PΛQ

with λ1 ≥ λ2..., U orthogonal unitary.Here v i(x) is the vector associated to λiand its components on the u(x , tj) are in U:ui(x) =

∑uiju(x , tj)

E.W. Sachs and M. Schu did it for B& S andPIDE and report a real improvement.

366 E.W. Sachs and M. Schu

We use the parameters = 0.5, J = 0, J = 0.5. Furthermore Tmax = 0.5 and

discretization parameters are 500 steps in x-direction and 150 steps in T -direction.

We compare the Finite Element solution and the POD solution of the PDE or

PIDE. The deviation is measured as 1151

150i=0 !D(Ti, ·)"DPOD(Ti, ·)!2L2 with Ti the

grid points in T -direction. The results are shown in Table 1.

Table 1 Accuracy of POD approximations, PIDE model

POD Basis El. Deviation smallest sing. val.

3 8.36e-001 1.63e+0034 1.36e-001 1.76e+0025 2.17e-002 2.28e+0016 3.31e-003 3.11e+0007 4.86e-004 4.25e-0018 6.82e-005 5.71e-0029 9.17e-006 7.46e-00310 1.18e-006 9.46e-004

One can see that the results in Table 1 show a fast decay in the value of the

smallest singular values. Hence it is expected that deviation of the POD model from

the FEM model is decaying rapidly too.

In the following tables we use a matlab subroutine ’fminunc’ to solve the cali-

bration problem for in the Black-Scholes case and also for the PIDE.

Let L denote the number of POD basis elements used inDPOD, opt the calculated

solution, fpod( opt) the optimal value of the objective function in (13) and time is

the time needed to solve the optimization problem.

We first look at the special case of a constant volatility. The optimal value is

= .30, the starting value always = .25 and the POD basis is calculated only

once for the starting value. Table 2 shows the results of the calibrating problem

without jumps = 0, this means the Black-Scholes case in the first four columns.

Table 2 Optimal Parameters for POD Approximations

Black-Scholes model PIDE modelL opt fpod( opt) time opt fpod( opt) time

4 0.3007 0.2610 0.6410 0.2806 0.1525 22.785 0.2991 0.0701 0.6870 0.2827 0.0904 22.676 0.2981 0.0338 0.6560 0.2845 0.0691 22.737 0.2977 0.0254 0.6250 0.2861 0.0549 22.588 0.2974 0.0238 0.6250 0.2874 0.0426 22.559 0.2974 0.0237 0.7340 0.2884 0.0333 22.9110 0.2974 0.0239 0.9840 0.2892 0.0272 22.69

500 0.3000 0.0000 2.1090 0.3000 0.0000 340.58

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 107 / 110

Page 112: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Reduced Basis for the PDE sover

C. Nguyen and T. Patera compute a 2-basket option by a reduced basismethod with snapshots with possibly different constant volatilities .

They have a sophisticated search method for the best basis based ona posteriori estimates and proven error bounds. The software is opensource

5 10 15 20 25 30

10 2

10 1

100

N

!! N,m

ax

Figure 6: The error indicator !!N,max as a function of N .

0.3 0.4 0.5 0.6 0.7

0.3

0.4

0.5

0.6

0.7

µ1

µ2

Figure 7: The sample set S! over the parameter space.

21

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 108 / 110

Page 113: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Speeding up computations with different volatilities

When σ, r are contant computing the option price is very fast:

double BSPut(double S, double t, double r, double K, double sigma) static const double sqrt2=sqrt(2.), sig2 = sigma*sigma/2;if(t<1e-5) return K>S? K-S : 0;double sigst = sigma*sqrt(t) ;double d1 = (log(S/K)+(r+sig2)*t)/sigst;double d2 = (log(S/K)+(r-sig2)*t)/sigst;double Nmd1 = 1-erfc(-d1/sqrt2)/2;double Nmd2 = 1-erfc(-d2/sqrt2)/2;return K*exp(-r*t)*Nmd2 - S*Nmd1;

Perform an SVD decomposition ofAij =

∫R×(0,T ) uσi (x , t)uσj (x , t)

The eigen values⇒-10

-8

-6

-4

-2

0

2

4

6

8

10

12

0 5 10 15 20 25 30 35 40

"eigen.txt"

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 109 / 110

Page 114: Numerical Challenge in Finance Solved by FEM …neela/CIMPA/notes/IITBcimpaOP.pdf · Implementation in C/C++ ... Numerical Challenge in Finance Solved by FEM (Financial Engineering)

Some Basis Functions

"basis.txt" matrix

0 10

20 30

40 50

60

0 5

10 15

20 25

30 35

40

-20 0

20 40 60 80

100 120 140 160

"basis.txt" matrix

0 10

20 30

40 50

60

0 5

10 15

20 25

30 35

40

-5 0 5

10 15 20 25 30 35 40

"basis.txt" matrix

0 10

20 30

40 50

60

0 5

10 15

20 25

30 35

40

-16-14-12-10

-8-6-4-2 0 2

First, second and fourth basis function (Recall that these arenon-convex combinations of elementary Black-Scholes solutions).

O. P. (www//ann.jussieu.fr/pironneau) Numerical Challenge in Finance Solved by FEM (Financial Engineering)July 9, 2015 110 / 110