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 from
modeling)
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
1 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
2 / 110
Options: The mechanism
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
3 / 110
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.
σ2
• If σ, r constant then St = S0 e(µ− 2 )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 )+
Challenge
in Finance Solved
by FEM (Financial Engineering)
July 9, 2015
See also, C. Schwab, M. Numerical
Griebel,
C. Oosterlee,
P. Glasserman.
O. P. (www//ann.jussieu.fr/pironneau)
4 / 110
Pricing Options: 3 numerical methods
Due to the put call parity we can either compute P or C:
Ct − Pt = St − Ke−
RT
t
r (τ )dτ
1. Monte-Carlo:
√
Skn+1 − Skn = Skn (µδt + σ δtN (0, 1)), S0n = S0 .
N
1X
Ct = e−r (T −t)
(STn − K )+ N (0, 1) ≈ f (rand())
N
1
2. Tree methods
120
’result.dat’ using 1:2
’result.dat’ using 1:3
’result.dat’ using 1:4
100
3. Ito Calculus :
80
σ2x 2 2
∂t C +
∂ C + rx∂x C − rC = 0
2 xx
C(T , x) = (x − K )+ C(0, t) = 0
C(x, t) ∼ x − Ke−r (T −t) when x → ∞
O. P. (www//ann.jussieu.fr/pironneau)
60
40
20
0
0
20
40
60
80
100
120
140
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
160
180
200
5 / 110
Some Analytical Formulae
When r and σ are constant St = S0 e(r −
σ2
)t+σWt
2
. Easy to prove by using the Itô’s formula:
df (t, wt ) =
∂f
∂f
1 ∂2f
dt +
dwt +
(dwt dwt )
∂t
∂w
2 ∂w 2
• Black-Scholes formula
2
log( SKt ) + (r + σ2 )(T − t)
√
Let d1 =
σ T −t
and
√
d2 = d1 − σ T − t
Introducing N the upper tail of the Gaussian function
Ct = St N(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
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 with
g++ bsformula.cpp
Run 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
Monte-Carlo Methods
References
• Paul Glasserman. Monte Carlo methods in financial engineering, volume 53 of
Applications of Mathematics (New York). Springer-Verlag, New York, 2004. Stochastic
Modelling and Applied Probability.
• Yuh-Dauh Lyuu. Financial engineering and computation. Cambridge University
Press, Cambridge, 2002. Principles, mathematics, algorithms.
• In the C-library stdlib.h, there is a function rand() which returns an integer
value (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_MAX
w̃ ∈ [0, 1]. x =
p
−2 log(w1 ) cos(2πw2 ) ∈ N01
• N01 = zero mean, unit variance i.e. probability density
√
• Hence Wt+δt − Wt = δtN01
O. P. (www//ann.jussieu.fr/pironneau)
(1)
2
x
√1 e − 2
2π
.
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
8 / 110
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
The program stores the result in a file call stoch.dat in a format that
gnuplot can use for graphics (see www.gnuplot.org).
120
"stoch.dat"
100
C
80
60
40
20
0
0
5
10
15
20
S
25
30
35
40
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
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
Z
E(x) :=
+∞
−∞
N
1X
xi
p(x)xdx ≈ EN (x) :=
N
(2)
1
verifies for all c1 < 0 < c2 :
r
lim P
N→∞
E(x) − EN (x) ∈
c1
var(x)
, c2
N
r
var(x)
N
!!
1
=√
2π
Z
c2
x2
e− 2 dx
c1
(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
Variance Reduction: Control Variate
To compute X̄ := E(X ) compute E(X − X 0 ) instead for some X 0 with
E(X 0 ) known and var(X − X 0 ) < var(X ).
Consider Z = X − b(Y − Ȳ ), for some Y for which Ȳ is known.
var(Z ) = E((Z − Z̄ )2 ) = var(X ) + b2 var(Y ) − 2bE((X − X̄ )(Y − Ȳ ))
is minimized when
Pn
(Xi − X̄ )(Yi − Ȳ )
E((X − X̄ )(Y − Ȳ ))
≈ 1 Pn
b=
2
var(Y )
1 (Yi − Ȳ )
2
E (X − X̄ )(Y − Ȳ )
var(Z )
=1−
Then
var(X )
E((X − X̄ )2 )E((Y − Ȳ )2 )
For a European vanilla call we take XT = e−rT (ST − K )+ , YT = ST
and notice that E(YT ) = erT S0 . Let {Si }n1 be n samples of ST , then
E(XT ) = E(ZT ) with ZT = XT − b(YT − E(YT )) which yields
n
E(ZT ) ≈
1 X −rT
(e (Si − K )+ − b(Si − erT S0 ))
n
O. P. (www//ann.jussieu.fr/pironneau)
1
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
12 / 110
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 only
the trajectories for which the constraint holds for all time up to T ..
Therefore
√
Skn+1 − Skn = Skn (µδt + σ δtN (0, 1)), S0n = S0 .
N
X
−r (T −t) 1
Ct = e
(STn − K )+ 1St <SM , ∀t<T
N
1
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
Edging
The very reason of options is edging.
For example take a portfolio which has z shares St . Can we add to it y
calls 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 is
the best value for z.
We shall see that z = −∂S Ct , which finance engineers call the "delta".
Later we shall prove that
Ct − St
∂Ct
∂S
is not random.
We can also try to verify it numerically? But ∂S Ct is not so easy to
compute, yet we can use finite difference approximations or automatic
differentiation (AD).
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
14 / 110
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
1 ∂2C 2
∂C
δt +
dS +
dS
∂t
∂S
2 ∂S 2
Randomness is eliminated when a = − ∂C
∂S (hedging). Then
∂C
1 ∂2C 2 2
δt +
S σ δt ⇒
∂t
2 ∂S 2
1 ∂2C 2 2
∂C
δt +
r δt(aSt + Ct ) ≈
S σ δt ⇒
∂t
2 ∂S 2
2
∂C
∂C
1∂ C 2 2
−r
S + rC ≈
+
S σ
∂S
∂t
2 ∂S 2
er δt (aSt + Ct ) = aSt + Ct +
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
15 / 110
Tutorial 1
1
Compute the exact value with the Black-Scholes formula at one
sampling point S
2
Run 10 times a Monte-Carlo simulation with N=10000 trajectories
3
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 N
5
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 ∂S Ct 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
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
17 / 110
A simple Tree method
Suppose that from one day nδt to the next (n + 1)δt, Sn becomes
Sn+1 = uSn with probability p or Sn+1 = dSn with probability 1 − p. and
no other.
After N days SN = u m d N−m S0 with probability CNm pm (1 − p)N−m where
CNm = N!/m!(N − m)!.
To have the compatibility with the Black-Scholes model we write
puSn + (1 − p)dSn = er δt Sn because left is a deterministic quantity
2
) − E(Sn+1 )2 = Sn2 [pu 2 + (1 − p)d 2 − e2r δt ]
E(Sn+1
2
= Sn2 (e(2r +σ )δt − e2r δt ) equating variance from SDO
The option is found from the opposite direction by
u
d
Cn = e−r δt (Cn+1
p + Cn+1
(1 − p))
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
18 / 110
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))/dis
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
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
20 / 110
Black-Scholes in Log-Scales
Theorem C0 = C(S0 , 0) where C(S, t) is the solution of
∂C
σ2 ∂ 2C 2
∂C
+
S − rC = 0, C(S, T ) = (S − K )+
S +r
∂t
2 ∂S 2
∂S
θ = T − t,
S
∂ϕ
∂C
=
∂S
∂x
x = log S,
and
S2
∂2C
∂S 2
So, for a call (with ν := (r −
and
=S
ϕ(x, θ) = C(ex , T − θ).
(4)
∂2ϕ
∂C
∂C
∂
∂ϕ
(S
)−S
=
−
2
∂S ∂S
∂S
∂x
∂x
σ2
2 ))
∂ϕ 1 2 ∂ 2 ϕ
∂ϕ
− σ
−ν
+ r ϕ = 0 in R × (0, T [
2
∂θ
2 ∂x
∂x
x
+
ϕ(x, 0) = (e − K )
ϕ(x, θ) ∼ 0, as x → −∞
ϕ(x, θ) ∼ ex − Ke−r θ as x → +∞.
(5)
There is an analytical solution with erf functions as shown by a last
change of variable
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
21 / 110
Analytical Solution
With ϕ(x, θ) = ψ(x, θ)eaθ+bx , b =
1
2
−
r
σ2
and a = −r −
σ2 2
2 b :
2
2
∂ψ σ 2 ∂ 2 ψ
( σ2 +r ) x2
(− σ2 +r ) x2 +
σ − Ke
σ ) , (6)
=
0,
ψ(x,
0)
=
(e
−
∂θ
2 ∂x 2
Recall that
|x−x0 |2
1
∂t u − k ∂xx u = 0, u(x, 0) = δ(x − x0 ) ⇒ u(x, t) = √
e− 4kt
2πkt
Z
σ2
σ − (x−y )2 ( σ2 +r )y
(−
√ e 2t (e 2
Therefore ψ(x, t) =
− Ke 2 +r )y )+ dy
πt
R
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
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
23 / 110
Finite Difference: Explicit Schemes
∂u σ 2 ∂ 2 u
∂u
+
+r
− ru = 0, u(x, T ) = (ex − K )+
2
∂t
2 ∂x
∂x
ujm − ujm−1
δt
+
σ2
1
σ2
m
m
m
m
(uj+1
− 2ujm + uj−1
)+
(r −
)(uj+1
− uj−1
) − rujm−1 = 0
2
2δx
2δx
2
Explicit but a stability condition is needed: δt √
< cδx 2
Equivalence with binomial trees: Take δx = σ δt then
√
1 m
δt
σ2
m−1
m
m
m
(1 + r δt)uj
= (uj+1 + uj−1 ) +
(r −
)(uj+1
− uj−1
)
2
2σ
2
√
√
1
δt
σ2
1
δt
σ2
m
m
+( −
]
⇒ ujm−1 ≈ e−r δt [( +
(r −
))uj+1
(r −
))uj−1
2
2σ
2
2
2σ
2
which is a tree with p ≈
O. P. (www//ann.jussieu.fr/pironneau)
er δt −d
u−d ,u
= eσ
√
δt ,d
= e−σ
√
δt
3
⇒ error O(δt 2 ).
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
24 / 110
Finite Difference: Explicit Schemes
∂u σ 2 ∂ 2 u
∂u
+
+r
− ru = 0, u(x, T ) = (ex − K )+
2
∂t
2 ∂x
∂x
ujm − ujm−1
δt
+
σ2
1
σ2
m
m
m
m
(uj+1
− 2ujm + uj−1
)+
(r −
)(uj+1
− uj−1
) − rujm−1 = 0
2
2δx
2δx
2
Explicit but a stability condition is needed: δt √
< cδx 2
Equivalence with binomial trees: Take δx = σ δt then
√
1 m
δt
σ2
m−1
m
m
m
(1 + r δt)uj
= (uj+1 + uj−1 ) +
(r −
)(uj+1
− uj−1
)
2
2σ
2
√
√
1
δt
σ2
1
δt
σ2
m
m
+( −
]
⇒ ujm−1 ≈ e−r δt [( +
(r −
))uj+1
(r −
))uj−1
2
2σ
2
2
2σ
2
which is a tree with p ≈
O. P. (www//ann.jussieu.fr/pironneau)
er δt −d
u−d ,u
= eσ
√
δt ,d
= e−σ
√
δt
3
⇒ error O(δt 2 ).
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
24 / 110
Finite Difference: Implicite Schemes
∂u σ 2 ∂ 2 u
∂u
+r
+
− ru = 0, u(x, T ) = (ex − K )+
2
∂t
2 ∂x
∂x
ujm+1 − ujm
δt
+
σ2
σ2
1
m
m
m
m
m
(r
−
)(uj+1
− uj−1
) − rujm = 0
(u
−
2u
+
u
)
+
j+1
j
j−1
2δx 2
2δx
2
Linear system solved by Gauss LU tri-diag fact. : as fast as trees.
Marginal stability condition δt < cδx due to 1st order x-derivative
Upwinding gives unconditional stability
precision O(δt) + O(δx 2 )
Crank-Nicolson time scheme will give O(δt 2 ) + O(δx 2 )
Non-constant deltaxj can put the points where needed but then
FEM does better.
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
25 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
26 / 110
Monte-Carlo: Goods and bads
Near to the modelisation
Gives upper and lower bounds on the error
√
Converges in 1/ N ⇒ Quasi-Monte Carlo
√
Can compute several derivatives with O( N) operations
Easy 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
PDEs: Goods and bads
Nearer to the analytical formulas
Upper and lower error bounds with a posteriori estimates
Converges in O(N −p) with order of approximation p
Compute several derivatives in O(N log N) operations (Dupire).
Can be parallelized.
Cursed by dimension d in O(N d ) except with sparse grid.
Greeks are very easy
Calibration is reasonably easy
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
28 / 110
Tutorial 2
Results to be given in LateX , MSWord or Excel (sent by email at
[email protected] with subject: tuto2 + your name)
1
By comparing with the Black-Scholes analytic formula check
sensitivity of the tree method treeBS.cpp with respect to δt (3
different δt will do)
2
By comparing with the Black-Scholes analytic formula check
sensitivity of the finite difference method explicitfdm.cpp with
respect 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 with
S = 100, K = 80, r = 3%, σ = 20%, T = 1.
4
Compare the graph of an American option with the European one
with treeBS.cpp . Americans are obtained by using
d
Pn = max{(K − Sn )+ , e− r δt(pP u n + 1 + (1 − p)Pn+1
)}
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
29 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
30 / 110
Existence, Uniqueness and Regularity
Use the put-call parity to work with P which decays at infinity with time
to maturity τ = T − t:
σ2x 2 2
∂ P − rx∂x P + rP = 0
2 xx
P(0, x) = (K − x)+ , lim P(x, τ ) = 0
∂τ P −
x→∞
Theorem Assume σ(x, t) ∈ [σm , σM ], x∂x σ ∈ L∞ and r , σ, x∂x σ
Lipschitz in t, then ∃!P continuous in time and x 2 ∂xx P ∈ L2 (R+ ).
Furthermore x 2 ∂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) ≥ αkuk2V − βkuk2L2 (R+ )
u ∈ V := {v ∈ L2 (R+ ) : x∂x v ∈ L2 (R+ )}
and (∂t u, w) + at (u, w) = 0 ∀w ∈ V
Z ∞
x 2σ2
w) − rxw∂x u + ruw]
with at (u, w) =
[∂x u · ∂x (
2
0
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
31 / 110
Existence, Uniqueness and Regularity
Use the put-call parity to work with P which decays at infinity with time
to maturity τ = T − t:
σ2x 2 2
∂ P − rx∂x P + rP = 0
2 xx
P(0, x) = (K − x)+ , lim P(x, τ ) = 0
∂τ P −
x→∞
Theorem Assume σ(x, t) ∈ [σm , σM ], x∂x σ ∈ L∞ and r , σ, x∂x σ
Lipschitz in t, then ∃!P continuous in time and x 2 ∂xx P ∈ L2 (R+ ).
Furthermore x 2 ∂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) ≥ αkuk2V − βkuk2L2 (R+ )
u ∈ V := {v ∈ L2 (R+ ) : x∂x v ∈ L2 (R+ )}
and (∂t u, w) + at (u, w) = 0 ∀w ∈ V
Z ∞
x 2σ2
w) − rxw∂x u + ruw]
with at (u, w) =
[∂x u · ∂x (
2
0
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
31 / 110
Existence, Uniqueness and Regularity
Use the put-call parity to work with P which decays at infinity with time
to maturity τ = T − t:
σ2x 2 2
∂ P − rx∂x P + rP = 0
2 xx
P(0, x) = (K − x)+ , lim P(x, τ ) = 0
∂τ P −
x→∞
Theorem Assume σ(x, t) ∈ [σm , σM ], x∂x σ ∈ L∞ and r , σ, x∂x σ
Lipschitz in t, then ∃!P continuous in time and x 2 ∂xx P ∈ L2 (R+ ).
Furthermore x 2 ∂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) ≥ αkuk2V − βkuk2L2 (R+ )
u ∈ V := {v ∈ L2 (R+ ) : x∂x v ∈ L2 (R+ )}
and (∂t u, w) + at (u, w) = 0 ∀w ∈ V
Z ∞
x 2σ2
w) − rxw∂x u + ruw]
with at (u, w) =
[∂x u · ∂x (
2
0
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
31 / 110
A posteriori estimates (Y.Achdou)
Proposition
[[u
− uh,δt ]](tn ) ≤ c(u0 )δt
+
µ
n
X
2
σmin
m=1
ηm 2
m−1
Y
X
δtm
+ 2 g(ρδt )
(1 − 2λδti )
ηm,ω 2
σmin
ω∈T
i=1
! 12
mh
where L, µ are the time-continuity constants of σ 2 , r , xσ∂x σ in L∞ ,
c(u0 ) = (ku0 k2 + δtk∇u0 k2 )1/2 , g(ρδt ) = (1 + ρδt )2 max(2, 1 + ρδt )
2
σmin
|uhm − uhm−1 |2V ,
2
u m − uhm−1
∂u m
hω
=
k h
− rx h + ruhm kL2 (ω)
xmax (ω)
δtm
∂x
2
ηm
= δtm e−2λtm−1
ηm,ω
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
32 / 110
Best Numerical Method
• Implicit in time, centered in space (upwind usually not necessary):
1
1
P n+1 − P n x 2 σ 2 2 n+ 1
−
∂xx P 2 + rP n+ 2 − xr ∂x P n+ 2 = 0
δt
2
1
• FEM-P + LU factorization
• mesh adaptivity + a posteriori estimates
• Banks need 0.1% precision in split seconds ...within Excel
PDE solution
Black-Scholes formula
100
80
60
40
20
0
-20
0
0.5
50
0.4
100
0.3
150
0.2
200
0.1
250
300
O. P. (www//ann.jussieu.fr/pironneau)
0
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
33 / 110
Numerical Example
"u.txt"using 1:2:7
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0
"u.txt"using 1:2:7
1.4
1.2
1
0.8
0.6
0.4
0.2
0
0
50
100
150
200
250
3000
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
50
100
150
200
250
0
"u.txt"using 1:2:5
0.02
0.007
0.01
0.006
0
0.005
-0.01
0.004
-0.02
-0.03
0.003
0.002
-0.04
"u.txt"using 1:2:6
0.001
-0.05
0
0
50
100
150
200
250
3000
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
50
100
150
200
250
3000
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
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
Source Code in C++
/Users/pironneau/Desktop/07fall/tex/NatIxis/optionNixis.cpp
Sunday 18 November 2007 / 16:28
Page: 1
/Users/pironneau/Desktop/07fall/tex/NatIxis/optionNixis.cpp
Sunday 18 November
bm[i]2007
-=/ 16:28
am[i]*cm[i-1];
cm[i] /= bm[i];
#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();
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;
Option(const int nT1, const int nX1,
u[nX-1]=0; u[0] = K*exp(-r*(j+0.5)*dt); // B.C. of PDE
const double r1, const double S1, const double K1, const double T1);
uold[1] += u[0]*sigma[j][1]*sigma[j][1]*dt/2;
~Option() { delete [] am; delete [] bm; delete []cm; delete [] uold; }
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;
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) cm[i] = -aux;
}
{
factLU();
const double xmax=3*S;
for(int i=1;i<nX-1;i++) u[i]=uold[i];
dx = xmax/(nX-1);
solveLU(u);
dt = T/(nT-1);
}
u = new ddouble[nX];
}
sigma = new ddouble*[nT];
for(int i = 0; i < nT; i++) sigma[i] = new ddouble[nX];
int main(){
am
= new ddouble[nX];
Option p(100,150,0.03, 100,110,4);// nT, nX, r, S, K, T
bm
= new ddouble[nX];
for(int j=0;j<p.nT;j++) // time loop
cm
= new ddouble[nX];
for(int i=0;i<p.nX;i++) p.sigma[j][i]=0.3;
uold
= new ddouble[nX];
p.calc();
}
for(int i=0;i<p.nX;i++) cout<<p.u[i]<<endl;
return 0;
}
ddouble Option::phi(const double strike, double x) {
return x<strike?strike-x:0;
}
void Option::factLU()
{
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
35 / 110
Greeks (Challenge Malliavin Calculus)
x 2σ2
∂xx u − rx∂x u + ru = 0, u(0) = (K − x)+
2
The ∆ = ∂x u is given by the method. The Γ = ∂xx u can be computed
by:
Z
Z
i
i
(Γh , wh ) = −(∆, ∂x wh ) ∀wh ∈ Vh or Γh (q ) = −( ∂x w ∂x (u)dx)/( w i )
∂t u −
Ω
Ω
Where the second formula is a consequence of mass lumping.
The vega ν = ∂σ u is solution of
x 2σ2
∂xx ν − rx∂x ν + r ν = x 2 σ∂xx u, ν(0) = 0
2
It is the same PDE with a r.h.s. In variational form
1 n+1
(ν
− νhn , wh ) + a(νhn+1 , wh ) = −(∂x (wh x 2 σ), ∂x uhn+1 )
δt h
etc. Automatic Differentiation can also be used.
∂t ν −
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
36 / 110
Freefem++ Implementation (I)
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
37 / 110
Freefem++ Implementation (II)
Put option price u
The Delta : ∆ = ∂x u
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
Barrier Options
If u stops to exist when St > SM and when St < SM then just add
u(SM , t) = 0 for all t. The theory is the same but with
dv
∈ L2 (]Sm , SM [), v (Sm ) = v (SM ) = 0
V = v ∈ L2 (]Sm , SM [) : x
dx
N
X
uj (t)w j (x) (do not take the first and last hat function)
uh (x, t) =
j=2
dU
⇔ B
+ A(t)U = 0, where Bij = (w j , w i ), Aij (t) = at (w j , w i ).
dt
60
"u.txt" using 1:2
"u.txt" using 1:3
"u.txt"using 1:4
50
40
30
20
10
0
-10
0
O. P. (www//ann.jussieu.fr/pironneau)
20
40
60
80
100
120
140
160
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
39 / 110
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
Asian Options
For the financial modeling of path dependent options with PDE , see
Willmott et al. In an Asian option the payoff is
u 0 (S, y ) = (K − y )+ where y =
1
t
Z
t
S(τ )dτ
0
The price of the option is found by solving for all
{x, y , t} ∈ R+ × R+ × (0, T ]
σ2x 2
y −x
∂x u) + (σ 2 − r )x∂x u +
∂y u + ru = 0,
2
T −t
t e−rt if y < KT
T
T −t
u(x, y , 0) = (K − y )+ , ∂x u|+∞,y ,t ≈ 1−re
(7)
−rt
Tr otherwise.
∂t u − ∂x (
The difficulties of this problem is that the second order part of the
differential operator is incomplete and the convective velocity
T
v = ((σ 2 − r )x, yT−x
−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
Speed-up with Galerkin - Characteristic Method (I)
Don’t upwind or if you do, use this:
u m+1 (x) − u m (x − am (x)δt)
+ O(δt)
δt
u m+1 − u m oX
=
+ O(δt)
δt
with X (x) = Xam (mδt) and
dX
(τ ) = am (X (τ )), X ((m + 1)δt) = x
dτ
∂t u + a · ∇u|x,(m+1)δt
=
x
X (x)
x−
am (x)δt
(∂t u+
Γ
Second order approximation
3u m+1 (x) − 4u m (x − am (x)δt) + u m−1 ((x − 2am (x)δt))
2δt
∗
3u m+1 − 4u m oXδt∗ + u m−1 oX2δt
2
=
+ O(δt )
2δt
∗
with Xk δt (x) = X ∗ m+ 1 (k mδt), k = 1, 2
a · ∇u)|x,(m+1)δt ≈
1
a
2
and a∗ m+ 2 = 2am − am−1
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
42 / 110
Speed-up with Galerkin - Characteristic Method (I)
Don’t upwind or if you do, use this:
u m+1 (x) − u m (x − am (x)δt)
+ O(δt)
δt
u m+1 − u m oX
=
+ O(δt)
δt
with X (x) = Xam (mδt) and
dX
(τ ) = am (X (τ )), X ((m + 1)δt) = x
dτ
∂t u + a · ∇u|x,(m+1)δt
=
x
X (x)
x−
am (x)δt
(∂t u+
Γ
Second order approximation
3u m+1 (x) − 4u m (x − am (x)δt) + u m−1 ((x − 2am (x)δt))
2δt
∗
3u m+1 − 4u m oXδt∗ + u m−1 oX2δt
2
=
+ O(δt )
2δt
∗
with Xk δt (x) = X ∗ m+ 1 (k mδt), k = 1, 2
a · ∇u)|x,(m+1)δt ≈
1
a
2
and a∗ m+ 2 = 2am − am−1
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
42 / 110
Upwinding by Characteristics (II)
One of the best upwinding is the characteristic finite element method :
1 m+1
(u
(q) − u m (Q)),
δt
with Q = q − δtv (q) where q = (x, y )T .
∂t u + v · ∇u|x,y ,tm+1 ≈
IsoValue
3.38661
10.1969
17.0072
23.8175
30.6278
37.438
44.2483
51.0586
57.8689
64.6792
71.4895
78.2997
85.11
91.9203
98.7306
105.541
112.351
119.161
125.972
132.782
IsoValue
2.96187
8.88826
14.8146
20.741
26.6674
32.5938
38.5202
44.4466
50.373
56.2994
62.2258
68.1522
74.0785
80.0049
85.9313
91.8577
97.7841
103.71
109.637
115.563
IsoValue
1.95469
5.8649
9.77511
13.6853
17.5955
21.5057
25.416
29.3262
33.2364
37.1466
41.0568
44.967
48.8772
52.7874
56.6976
60.6079
64.5181
68.4283
72.3385
76.2487
K = 100, r = 3%, σ = 0.3. the square domain is triangulated by a
uniform 50 × 50 mesh and we have taken 50 time steps over the 4
years 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
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
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
45 / 110
American Put Option by Monte-Carlo
At τ we compare Pτe := E[e−r (T −τ ) (K − ST )+ |Sτ ] with (K − Sτ )+ :
P0 = sup e−r τ ESτ max{Pτe , (K − Sτ )+ }
τ ∈(0,T )
When r , σ are constant ST = Sτ e(r −
σ2
)(T −τ )+σWT −τ
2
, so
2
(r − σ2
)(T −τ )+σWT −τ +
Pτe = E[e−r (T −τ ) (K − Sτ e
) ]
−r τ
e
P0 = sup e ESτ [max{Pτ , (K − Sτ )+ ]
τ ∈(0,T )
≈
e−r τm X
e
[max{Pnm
, (K − Snm )+ ]}
N
m∈(0,..,M)
n
sup
σ2
√
n
with τm = mδt, Snm = S0 e(r − 2 )τm +σ τm N01
√
X
i
σ2
1
e
Pnm
≈ e−r (T −τm )
(K − Snm e(r − 2 )(T −τm )+σ T −τm N01 )+
I
i
By comparison the same European option is valued as
√
σ2
1 X −r τ
n
[e (K − S0 e(r − 2 )T +σ T N01 )+ ]
N n
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
46 / 110
C++ code
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
47 / 110
Longstaff-Schwartz: an example
Stock K=110, r=6%, h(S)=(K −S)+
Path
t=0
t=1 t=2 t=3
h(S3 )
E[YIX ] = −107 + 2.983X − 0.01813X 2
Y=h(S3 )(1-r)
X=S2
h(X) E(X|Y)
1
2
3
4
5
6
7
8
100
109
108
134
0
0
100
116
126
154
0
0
100
122
107
103
7
7 ×.94
100
93
97
92
18
18 ×.94
100
111
156
152
0
0
100
76
77
90
20
20 ×.94
100
92
84
101
9
9 ×.94
100
88
122
134
0
0
E[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
2
3
4
5
6
7
8
0
0
0
13
0
33
26
0
0
0
7
0
0
0
0
0
O. P. (www//ann.jussieu.fr/pironneau)
0
0
0
13 ×.94
0
33 ×.94
26 ×.94
0
109
0
0
93
0
76
92
88
1
0
0
17
0
34
18
22
1.39
0
0
10.92
0
28.66
11.75
15.33
108
2
3.69
126
0
0
107
3
4.61
97
13
11.76
156
0
0
77
33
15.2
84
26
15.7
122
0
0
Stopping rule
Path Stop
P
1
2
3
4
5
6
7
8
t3
t1
t1
t1
t1
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
0
0
7
17
0
34
18
22
48 / 110
Mathematical description
Let s → C(ω, s; t, T ) be the path of cash generated by the option when
it has not been exercized at t < s. The value of continuation is
F (ω, tk ) = E[
N
X
e−r (tj −tk ) C(ω, tj ; tk , T )|Sk ]
k +1
This function is represented on a basis, for example, 1, X , X 2 .. or
m
X /2
Laguerre polynomials { em! d m (X m e−X )}M
1 . The coefficients are
dX
computed 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
Implementation in C++(I)
longstaff.cpp
Printed: 02/03/2009 22:05:58
Page 1 of 4
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];!
O. P. (www//ann.jussieu.fr/pironneau)
longstaff.cpp
Printed: 02/03/2009 22:05:58
Page 2 of 4
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++;!
!
}!
}!
// -----------------------------------------------------!
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
50 / 110
Implementation in C++(II)
longstaff.cpp
Printed: 02/03/2009 22:05:58
Page 3 of 4
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));!
O. P. (www//ann.jussieu.fr/pironneau)
longstaff.cpp
Printed: 02/03/2009 22:05:58
Printed For: Oli
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;!
}!
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
51 / 110
American Options by PDE
In American options one of the two must be also an equality
∂u
∂u σ 2 x 2 ∂ 2 u
− rx
−
+ ru ≥ 0
2
∂t
2 ∂x
∂x
u ≥ u◦ := (K − x)+
∂v
2
+
2
+
V = v ∈ L (R ), x
∈ L (R ) ,
∂x
K = {v ∈ L2 (0, T; V), v ≥ u◦ a.e. in (0, T) × R+ }.
Find
s.t.
∂u
∈ L2 (0, T ; V 0 ),
∂t
u ∈ K ∩ C 0 ([0, T ]; L2 (R+ )),
(
∂u
, v − u) + at (u, v − u)
∂t
u(t = 0)
≥
=
0,
u◦
∀v ∈ K,
(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
Semi-Smooth Newton Method (K.Kunisch)
After time discretization reformulate the problem as
a(u, v ) − (λ, v ) = (f , v ) ∀v ∈ H 1 (R+ ), i.e.Au − λ = f
λ − min{0, λ + 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 for
any real constant c > 0 because λ is the Lagrange multiplier of the
constraint.
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∈H 1 (R+ ) {a(u, u) − 2(f , u) : u = φ on Ak }
• Set λk +1 = f − Auk +1
Theorem For any c > 0 u k → 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
Tutorial 3
1
2
3
4
5
Compute a Barrier Option by 2 methods: Monte-Carlo and
vanilafem.cpp
Plot on the same graph S0 → P0 := P(S0 , 0) when P0 is the a
European or an American Put computed by P solution of the PDE
(program amerkunisch.cpp or the variational inequality. Try
kmax=1, kmax=5 and kmax=20.
From now on stick to kmax=5. Find the asset prize for which the
American put is exercised today (t=0).
By using the Black-Scholes formula for the European put,
compute the L2 error when amerkunisch.cpp is used for a
European option versus N = 1, 2, 3 when nS, M are changed to
N ∗ nS, N ∗ M. Plot this error in log-log scale versus N.
For one S0 compare the computing time for the American option
when amereuropmc.cpp is used or longstaff.cpp is used.
Results to be sent by email in MSWord format to [email protected] under
subject TUTO2+ your name
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
54 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
55 / 110
Multidimensional problems: Basket Options
Basket Option with correlated Brownians E(dWi dWj ) = σij dt
dSi = Si (r dt + dWi ), i = 1..d,
X
u = e−(T −t)r E(
Si − K )+
Ito calculus leads to a multidimensional Black-Scholes equation
∂τ u −
X σij xi xj
∂xi xj u − rxi ∂xi u) + ru = 0
(
2
ij
X
u(0) = (
xi − K )+
i
Variational Formulation As before : (uτ , w) + aτ (u, w) = 0, ∀w ∈ V
aτ (u, w) =
X σij xi xj
X σij xi xj
∂xi u, ∂xj w) − (rxi −
∂xj
)∂xi u, w) + (ru, w)
(
2
2
ij
j
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
Exemple: an Option on a Basket of 2 Assets
dS1 = S1 (r dt + σ1 dW1 ), dS2 = S2 (r dt + σ2 dW2 ),
u = e−(T −t)r E(K − S1 − S2 )+
with E(dW1 dW2 ) = qdt,. . To generate W1 , W2 , take 2 uncorrolated
Brownians W1 , w2 and set
q
dW2 = qdW1 + 1 − q 2 dw2 ⇒
E(dW1 dW2 ) = qE(dW12 ) = qdt
E(dW22 ) = q 2 E(dW12 ) + (1 − q 2 )E(dw22 ) = dt
The PDE for the put option is (with time to maturity)
x 2 σ12
y 2 σ22
∂xx u −
∂yy u − qσ1 σ2 xy ∂xy u − rx∂x u − ry ∂y u + ru
2
2
u(x, y , 0) = (K − x − y )+ ∀x, y ∈ R+ × R+
∂t u −
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
57 / 110
Source Code in C++ for the 2D case (I)
/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cpp
Sunday 18 November 2007 / 18:17
Page: 1
#include <iostream>
#include <fstream>
#include <math.h>
#include <cmath>
#include <stdlib.h>
#include <assert.h>
#define NDEBUG // uncomment when debugging is over
using 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;
};
/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cpp
Sunday
18 November 2007 / 18:17
Grid::Grid(const
char *path ):v(),t()
{
Page: 2
// 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;
class Grid{ public:
for(int k=0; k<g.nt;k++)
int nt, nv; // nb of triangles,verties and edges
{
A<Vertex> v; // all vertices
int i=g(k,0),ip=g(k,1),ipp=g(k,2);
A<Triangle> t; // all triangles
k11 = a3 * s11 *( pow(g.v[i].x,2) +pow(g.v[ip].x,2) +pow(g.v[ipp].x,2));
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 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[
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)
for(int iloc = 0; iloc < 3; iloc++)
};
{
int i = g(k,iloc);
Grid::Grid(const char *path ):v(),t()
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
58 / 110
Source Code in C++ for the 2D case (II)
/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cpp
Sunday 18 November int
2007 /i18:17
= g(k,iloc);
Page: 3
int ip = g(k,(iloc+1)%3);
int ipp= g(k,(iloc+2)%3);
/Users/pironneau/Documents/B/psPDF/Achdou/book1_3/programs/BS2DFEM/BS2DfemCG.cpp
Sunday 18
November i=0;i<nv;
2007 / 18:17
for(int
i++) if(!g.v[i].where) u[i] += rho
//
Page: 4
* 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;
}
}
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)
void myexit() { cout<<"program ended at myexit()"<<endl;}
- 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)))int
/g.t[k].area/4.;
main(){
if (!g.v[i].where)
atexit(myexit);
// for debugging
au[i] += aijk * u[j];
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)
if(g.v[i].where == 1 || g.v[i].where == 4) g.v[i].where = 0;
au[i] += (u[i]*2.+u[ip]+u[ipp])* g.t[k].area * alpha/ 12.;
}
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;
void solvecg(Grid& g, A<double>& f, A<double>& u, int nIter, double precise, A<double> u0(g.nv), u1(g.nv);
double alpha, double s11,double s12,double s22){
A<double> f(g.nv),x(g.nv),f1(g.nv),f2(g.nv);
int nv = g.nv;
A<double> au(nv), ag(nv), grad(nv), hh(nv), diag(nv);
for(int i=0; i<g.nv;i++) { // set initial time value
double normOldGrad = 1e60;
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;
for(int m=0; m<nIter ; m++){
u1[i]=0;
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++)
for(int timeIter=0; timeIter < itermax; timeIter++){ // time loop
t+=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
<<g.v[g(it,1)].x <<" "<<g.v[g(it,1)].y
<<g.v[g(it,2)].x <<" "<<g.v[g(it,2)].y
<<g.v[g(it,0)].x <<" "<<g.v[g(it,0)].y
<<endl<<endl;
return 0;}
<<
<<
<<
<<
"
"
"
"
"
"
"
"
<< u0[g(it,0)]
<<u0[g(it,1)]
<< u0[g(it,2)]
<< u0[g(it,0)]
<<
<<
<<
<<
endl
endl
endl
endl
if(!g.v[i].where) u[i] += rho * hh[i];
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
59 / 110
SuperLU, Hypre... (N. Lantos)
Mesh size
101 × 101
126 × 126
151 × 151
176 × 176
201 × 201
LU [s]
10.094
14.547
22.313
31.985
43.938
Relative error
3.532 %
1.338 %
0.751 %
1.131%
0.432 %
superLU[s]
2.39
4.016
6.203
8.735
12.109
Relative error
3.076 %
1.797 %
0.489 %
0.790 %
0.670%
Comparison of CPU time for the LU algorithm and superLU for a
product 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
Numerical Results: adapted meshes
1e-06
1.26554
2.53109
3.79663
5.06217
6.32772
7.59326
8.8588
10.1243
11.3899
12.6554
13.921
15.1865
16.4521
17.7176
18.9831
20.2487
21.5142
22.7798
24.0453
Basket Euro Put option nine months to maturity: adapted mesh
and level lines. we solve 2D-B&S, with P0 = (K − max(S1 , S2 ))+ . We
2ρ
take σ11 = σ22 = 0.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
Basket with 3 Assets
Use http://ww.freefem.org/freefem3D :
double
double
double
double
double
double
N = 25;
double L=200.0;
double T=0.5;
dt = T / 15 ; double K=100;
double r = 0.02
s1 = 0.3;
double s2 = 0.2;
double s3 = 0.
q12 = -0.2*s1*s2;
double q13 = -0.1*s1*s3;
q23 = -0.2*s2*s3;
double s11 = s1*s1/2;
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
3D Basket Option(II)
double t=0; do{
solve(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^
- dx(q12*x*y*dy(u)) - dx(q13*x*z*dz(u)) - dy(q23*y*z*dz(
- 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
Sparse Grids (in more than 3 dimensions)
If f is analytic then
Z
D
f ≈
X
f (q i )ωi
i
where most of the points are on ∂D. The argument is recursive.
S.A. Smolyak: Quadrature and interpolation formulas for tensor
products of certain classes of functions Dokl. Akad. Nauk SSSR 4
pp 240-243, 1963.
Michael Griebel: The combination technique for the sparse grid
solution of PDEs on multiprocessor machine. Parallel Processing
Letters 2 1(61-70), 1992.
In polynomial approximations of f most of the mixed terms x1i x2j ...
are not needed.
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
64 / 110
Sparse Grids (when dimension d > 3)
"Bsk_Put_SparseG"
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-0.1
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
Computed by D. Pommier
See also C. Schwab et al. who can solve up to dimension 20 and a
PIDE in dimension 5. Nils Reich built a sparse tensor product wavelet
compression scheme of complexity O( h1 | 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
Multidimensional Problems: Stochastic Volatilities
dSt = µSt dt + σt St dWt ,
Let (σt ) be function of a mean reverting Orstein-Uhlenbeck process:
σt = f (Yt ),
dYt = α(m − Yt )dt + βd Ẑt ,
where α, m, β are positive constants,
and (Ẑt ) is Brownian.
2
The law of Yt knowing Y0 is N m + (Y0 − m)e−αt , βα (1 − e−2αt ) .
Therefore, m = limt→+∞ EYt and
ν2
β2
1
α
is the time of mean reversion.
:= α = limt→∞ varYt . As Ẑt and Wt are corrolated we take a
p
Brownian (Zt ) Ẑt = ρWt + 1 − ρ2 Zt .
One can hedge a self-financing portfolio ct with at shares of St , one option
(1)
with maturity T1 , Pt := P (1) (St , Yt , t) and bt options with maturity T2 > T1 ,
(2)
Pt := P (2) (St , Yt , t). By the no arbitrage principle
(1)
dct = at dSt + dPt
O. P. (www//ann.jussieu.fr/pironneau)
(2)
+ bt dPt
(1)
= r̃t ct dt = r̃t (at St + Pt
(2)
+ bt Pt )dt
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
66 / 110
The PDE when Yt is a OU process
Pt = P(St , y , t) with P(S, y , T ) = P◦ (S) and
2
1
∂P
∂2P
∂P
2 2∂ P
+
ρβSf
(y
)
+ f (y ) S
−
P
+
r
(t)
S
∂t
2
∂S
∂S∂y
∂S 2
{z
}
|
{z
} |
correlation
BSf (y )
(9)
∂2P
∂P
∂P
1
− βΛ(S, y , t)
= 0,
+ β 2 2 + α(m − y )
2 ∂y
∂y
∂y
{z
}
{z
} |
|
Orstein Uhlenbeck
premium
where Λ(S, y , t) is arbitrary if |ρ| =
6 1: prime on the volatility risk. We
can write Λ(S, y , t) as
Λ(S, y , t) = ρ
µ − r (t) p
+ 1 − ρ2 γ(S, y , t),
f (y )
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
Frequently used Models
Authors
Hull-White
Scott
Stein-Stein
Heston
ρ
ρ=0
ρ=0
ρ=0
ρ 6= 0
f (y )
√
f (y ) = y
f (y ) = ey
f (y ) = |y |
√
f (y ) = y
Yt process
lognormal
mean reverting OU
mean reverting OU
CIR
lognormal process:
dYt = c1 Yt dt + c2 Yt d Ẑt ,
mean reverting Orstein-Uhlenbeck (OU) process:
dYt = α(m − Yt )dt + βd Ẑt ,
mean reverting Cox-Ingersoll-Ross (CIR) process
p
dYt = κ(m − Yt )dt + λ Yt d Ẑt .
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
68 / 110
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
Some Results I: Stein-Stein Model
The contours of the price computed in Ω = (0, 800) × (−3, 3): note the
boundary 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
Some Results II: Heston Model
r = 0,
ρ = −0.5,
κ = 2.5,
λ = 0.5,
m = 0.06,
3
2.5
K = 1.
0.55
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
2
1.5
1
0.5
0
0.5
1
1.5
2
2.5
3
The contours of the pricing function of a put option with Heston model
half-year to maturity
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
71 / 110
American Options with Stochastic Volatility: OU
process
"exercise"
1
0.5
0
-0.5
0
20
O. P. (www//ann.jussieu.fr/pironneau)
40
60
80
-1
100
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
72 / 110
Option Pricing with Jump processes
Poisson Process
A 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
X
1
τi ,
Nt =
X
1t≥Tn
n≥1
By definition Nt is a Poisson process. It can be used to model the
number 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
Poisson Process
P t
definition A compound Poisson process is Zt = N
1 Yi where {Yi }
are independent r.v. with law f . Consequently a numerical method to
generate Xt is as follows:
Algorithm
1
Generate NT by (10)
T
2
Generate {Ui }N
1 uniform and random on (0, T ).
T
3
Generate NT random variables {Yi }N
1 of law f
PNT
4
Set Zt = 1 Yi 1Ui ≤t
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
74 / 110
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
Z
σ2
+ (eiux − 1 − iu1|x|<1 )ν(dx)))
E(eiuXt ) = exp(t(iγu −
2
R
R
1
Conversely if ν ∈ L (R) and [−1,1] x 2 ν(dx) < ∞, the (??) is the
characteristic function of a general Poisson process.
Formula (??) is refered as Levy-Khintchine’s.
Theorem The infinitesimal generator of the semi-group Xt is
1
(E(φ(x + Xt ) − φ(x))
t
Z
2
σ
=
∂xx φ + γ∂x φ + (φ(x + y ) − φ(x) − y 1|y |<1 ∂x φ(x))ν(dy )
2
R
LX := lim
t→0
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
75 / 110
The Generalized Black-Scholes Model
Let Xt be a jump-diffusion process, given by (11) Consider the
following model for the underlying asset St :
dSt = St− (rdt + dXt ),
S0 given, i.e. St = S0 ert+Xt
Theorem St is a martigale if
γ+
σ2
+
2
Z
R
(ex − 1 − x1|x|<1 )ν(dx) = 0
2
In Merton’s model ν(x) = √ λ exp(− (x−µ)
)
2δ 2
2πδ
The variance gamma (Y = 0) or the CGMY processes where
η ± > 0, η + > 2
exp(−|y |η ± )
ν(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
Example: Merton’s model
Theorem The call on S at t=0, C can be computed by
C = e−rT
∞
X
e−λT (λT )n
0
n!
where rn = r − λ(e
ern T CBS (S0 enδ
µ+δ 2 /2
2 /T
, K , T , σn , rn )
r
− 1) + µTn σn =
σ2 + n
δ2
2
Theorem The put on S is solution of
σ2S2
∂t P +
∂ P + rS∂S P − rP
2 SS
Z
+ (P(Sey ) − P(S) − S(ey − 1)∂S P(S))ν(dy ) = 0
(12)
R
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
77 / 110
Numerical Method
The finite element method applies equally well to this case, however to
avoid solving a full linear system it is wise to use a semi-implicite time
scheme whereby the integrodifferential term is discretized explicitly.
Then at each time step the right hand side of the linear system is
P m+1 δt + v δt and
Z
v = (P(Sey ) − P(S) − S(ey − 1)∂S P(S))ν(dy )
R
≈
S
N
X
ν(log Sj )
i
j=0
j+
1
2
[Pj − Pi − (Pi+1 − Pi )
Sj − Si
δx
(13)
δx
Theorem The scheme is stable iff δt ≤ C λ+δx
Hedging: See Tankov’s book or Tankov-Voltchkova’ paper
Calibration It is possible to adjust the parameters λ, ν, so as to
reproduce some real options.
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
78 / 110
Some Results with CGMY
"european_alltimes"
100
60
50
40
30
20
10
0
"european_0.5"
"european_1.5"
"european_0.9"
"european_1.2"
90
80
70
60
50
40
60
80
100
120
140
0
0.05
0.1
0.15
0.2
30
20
10
0
0
100
200
300
400
500
600
700
800
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
Tutorial 4
1
Run edostoch2.cpp with your own set of parameters
2
Replace the payoff by (K1 − S1T )(K2 − S2T ) and check that when
q = 0 (no corrolation) the value of the put can also be computed
as the product of the results of 2 calls of the Black-Scholes
analytical formula times erT ..
3
Download the Basket option pricer from the web site
http://paul.wilmott.com/software.cfm and check that it agrees with
your 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
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
81 / 110
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 , {Ci }i∈I given by
σ2x 2 2
∂ Ci + rx∂x Ci − rCi = 0,
2 xx
Ci (x, Ti ) = (x − Ki )+ , for all t ∈ [0, Ti [, x ∈ R+ ,
∂t Ci +
such that Ci (x0 , 0) = ci ...it involves as many B&S as different Ki , i ∈ I.
X
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
Dupire’s Equation
Theorem
CS,t (T , K ) = CT ,K (S, t) where C solves:
∂t C −
σ 2 (t, x)x 2 2
∂xx C + rx∂x C = 0,
2
C(0, x) = ( S − x )+
∂t u + µ(x, t)∂xx u = 0;
∂t p − ∂xx (µp) = 0
Proof (r=0)
Let
Let
Then, with appropriate boundary conditions
Z
Z
.
u(0)p0 =
uT p(T ) = u(x0 , 0) when p(x, 0) = δ(x − x0 )
R+
R+
Let ∂xx v = p then ∂t v − µ∂xx v = ax + b,
v (0) = c + d(x − x0 )+
Finally uT = (K − x)+ , ⇒ ∂xx uT = −δ(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
Calibration with Dupire’s equation
Inverse Problem : find σ(S, t) s.t.
X
min
|u(Ki , Ti ) − ci |2
σ
subject to
σ2x 2 2
∂ u + rx∂x u = 0,
2 xx
u(x, 0) = (S0 − x)+
∂t u −
.
Only one PDE
Remarks
Only one PDE
Let ũ be a C 2 extrapolation of {Ki , Ti , ci }I , then σ 2 =
∂t ũ
1 2
∂
ũ+rx∂
x ũ
xx
2
is
solution
Extension to Black-Scholes with jumps can be done by the same
method. The result is a similar optimal control problem but with a
PIDE.
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
Duality at the Discrete Level
FEM + Euler implicit with time step δt ⇒
(B + A)C n − BC n+1 = 0 C N = CT
where C n is the vector of values of Ch (q i , nδt) and A,B are
Z
1
Bij =
W i W j , Aij = a(W i , W j )
δt Ωh
the bilinear form of the PDE. Let P n+1 be
(A + B)T P n+1 − B T P n = 0 P 0 = P0
T
T
T
T
Then 0 = P n (A + B)U n − P n BU n+1 = P n−1 BU n − P n BU n+1
0T
N−1 T
Summing up over all n gives P BU 1 = P
BU N
I
X
T
Choosing Pj0 = δij gives (
bij )Ui1 ≈ (BU 1 )i = P N−1 BU N
j=1
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
85 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
86 / 110
Results on a Basket Call
S1 \S2
20:c
20:d
50:c
50:d
80:c
80:d
110:c
110:d
140:c
140:d
20
-0.097
1.1e-07
4.32
4.39
61.49
61.70
121.49
122.23
181.49
181.60
50
-0.089
0.0065
31.50
31.52
91.49
91.99
151.49
151.86
121.5
122.2
80
5.61
5.88
61.49
61.81
121.49
121.62
91.5
91.99
61.49
61.69
110
31.57
32.11
91.49
91.44
61.49
61.81
31.50
31.52
4.3
4.3
140
61.49
61.67
31.57
32.11
5.61
5.88
-0.089
0.0065
-0.097
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
Calibration: Implementation
Brute force works but is slow : e.g. FEM and an unknown σ(q i , tj ) at
every vertex q i and time tj . The derivative of the cost function with
respect to σ(q i , tj ) is computed by using an adjoint and the conjugate
gradient method is used.
"eta"
"eta_goal"
0.055
0.05
0.055
0.045
0.05
0.04
0.045
0.035
0.03
0.04
0.025
0.035
0.03
0
0.025
1
2
3
0
1
2
4
3
4
5
0
500
1000
1500
2000
2500
3000
5
0
500
1000
1500
2000
2500
3000
Solution (right) of an inverse problem with vol on the right (American)
Strategy
Define the Vol surface by a spline
Build all the constraints within the parametrization
Use Automatic Differentiation
and 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
Smile with a Cheap Spline
Ex: σ(x, t) C 1 , bilinear in an (x,t) parallelogram, grows large outside ⇒
8 parameters: Sij , σij but 0 < S1 (t) < S2 (t) is needed so set
S11 = z02 , S21 = z12 , S12 = S11 + z22 , S22 = S21 + z32 ,
2
2
z5+j
z3+j
σ
=
, j = 1, 2
σ1j =
2j
2
2
1 + z3+j
1 + z5+j
so as to work with an unconstrained set of parameters {zi }70
Let Si , σi , i = 1, 2 be linear in t:
t
t
t
t
σi = σi1 (1 − ) + σi2
Si = Si1 (1 − ) + Si2 ,
T
T
T
T
The vol surface is (a = σ(0, t)):
σ2 −σ1
σ1 −a S 2
1
a + (2 σ1S−a
− Sσ22 −σ
if (S < S1 )
−S1 )S + ( S2 −S1 − S1 ) S1
1
S−S1
S2 −S
σ2 S2 −S1 + σ1 S2 −S1
if S1 ≤ S ≤ S2
σ(S, t) =
2
σ + (S − S ) σ2 −σ1 + (S − S ) σ2 −σ1
if S > S
2
O. P. (www//ann.jussieu.fr/pironneau)
2 S2 −S1
2 S2 −S1
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
2
89 / 110
Observation Data
Strike
700
800
900
1000
...
1215
1225
1250
1275
1300
1325
1350
1365
1375
1380
1385
...
1700
1800
1900
1 Month
2 Months
6 Months
12 Months
...
...
...
...
253.4
245
224.2
203.9
184.1
164.9
146.3
50.6
46.1
41.8
...
60
55.8
51.8
...
219
196.6
174.5
152.9
131.9
111.7
100
92.5
...
467.8
...
36 Months
733
650.6
569.8
...
269.2
251
233.2
215.8
198.9
182.6
...
32.7
15.5
5.2
index SPX on 21.12.2006) at spot price 1418.3, r=3/100
O. P. (www//ann.jussieu.fr/pironneau)
...
139
24 Months
...
75,7
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
90 / 110
Results I
"usigma.txt"using 1:2:4
"usigma.txt"using 1:2:3
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
1400
1200
1000
800
600
400
200
0
-200
3
3
2.5
2.5
2
0
500
1000
2
0
1.5
1500
2000
500
1
2500
3000
1000
1.5
1500
0.5
3500
2000
40000
1
2500
3000
0.5
3500
40000
"uduo.txt"using 1:2:3
"uduo.txt"using 1:2:4
70
"converge.h"using 1:2
"converge.h"using 1:3
800
700
600
500
400
300
200
100
0
60
50
3
40
2.5
600
800
30
2
1000
1.5
1200
1
1400
20
1600
1800
0.5
10
0
5
10
15
20
O. P. (www//ann.jussieu.fr/pironneau)
25
30
35
40
45
50
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
91 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
92 / 110
Calibration of the Implied Volatility surface
Definition Let BS(S0 , σ, r , T , K ) denote the Black-Scholes formula for
the 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 , Ci }M
1 we can compute the set of
implied volatilies Σi Now we solve the Σ:
min J(Σ) :=
Σ∈S
M
X
1
|Σ(Si , ti ) − Σi |2
Then the Black-Scholes PDE is automatically satisfied. The art of the
trade 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
Tutorial V
1
2
3
4
Compute the same put using explicitfdm.cpp and using the
put-call parity and explicitfdm.cpp set up for Dupire’s eq..
Compute ∂K P0 by using explicitfdm.cpp with payoff 1S>K and by
finite difference using explicitfdm.cpp + Put-Call-Parity set up for
Dupire. Compare also to the finite difference approximation which
uses 2 calls to the Black-Scholes formula.
The CEV model is the Black-Scholes
PDE with
−0.3
σ(S, t) = σ0 K 0.3 KeS−r (T −t)
where σ0 is a parameter
constant. Compute the put with explicitfdm.cpp and compare the
implied volatility at one point with the CEV volatility.
Add the integro-differential term which models the jump-diffusion
model to the right hand side of the PDE in explicitfdm.cpp and
plot the results S → P(S).
On a rainy week-end you might think of a/ Write a C/C++ -function for a compound
Poisson process with λ = 5 and f so that the Yi are Gaussian. b/ Price a European
put with a Black-Scholes stochastic ODE where the Gaussian process is replaced by
a jump-diffusion.
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
94 / 110
Explicit Finite Difference with right hand side is
Pim+1 − Pim
σ2
2r − σ 2 m
m
m
m
m
+ rPim+1 −
(Pi+1 − Pi−1
) = vi
(P
−
2P
+
P
)
−
i+1
i
i−1
δt
2δx 2
2δx
With jump-diffusion model CGMY ν(y ) =
Z
vi =
R
≈
N
X
exp(−|y |η ± )
ω|y |1+Y
and vi is
(P(Sey ) − P(S) − S(ey − 1)∂S P(S))ν(y , dy )|S=Sj
[ν(log
j=0
Sj
Sj + δS
Pi+1 − Pi
) − ν(log )][Pj − Sj
]
Si
Si
δS
(14)
Take η + = η − = 3, Y = 1, ω = 1. Recall that the formula is true
because in terms of y , Sj = Si ey
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
95 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
96 / 110
Steepest Descent Method
Objective: Use inexact gradient in the context of mesh refinement
min J(z)
z∈Z
approximated by min Jh (z).
z∈Zh
Algorithm
(Steepest descent with Goldstein’s rule)
. while k gradz Jh (z m )k > do
. {
.
z m+1 = z m − ρ gradz Jh (z m ) where ρ is any number satisfying,
.
− βρkwk2 < Jh (z m − ρw) − Jh (z m ) < −αρkwk2
.
with w = gradz Jh (z m ).
. }
Set m := m + 1;
Improvement: Use w m = −ρ gradz Jh (z m ) + λw m.−1 be defined with
the 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
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial Engineering)
July 9, 2015
98 / 110
Principle of Automatic Differentiation
Let J(u) = |u − ud |2 , then its differential is
δJ = 2(u − ud )(δu − δud )
∂J
= 2(u − ud )(1.0 − 0.0)
∂u
and obviously the derivative of J with respect to u is obtained by
putting δ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 by
writing 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
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, the
procedure can be automatized by introducing a structured type holding the
value 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 July
Engineering)
9, 2015
100 / 110
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 July
Engineering)
9, 2015
101 / 110
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 July
Engineering)
9, 2015
102 / 110
Limitations
program newtontest
x=0.0;
al=0.5
call newton(x,10,al)
write(*,*) x
end
subroutine newton(x,n,al)
do i=1,n
f = x-alpha*cos(x)
fp= 1+alpha*sin(x)
x=x-f/fp
enddo
return
end
2n adjoint variables are needed! while the theory is
f (x, α) = 0 ⇒ x 0 fx0 + fα0 = 0 ⇒ x 0 = −
fα0
fx0
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 July
Engineering)
9, 2015
103 / 110
Outline
1
2
3
4
5
O. P.
Lesson I: Introduction to Scientific Computing in Finance
Stochastic Modeling
Additional Material: Trees
Lesson II: Studying the Black-Scholes PDE
Transformations and Analytical Solutions
Finite Difference Methods
Comparison Between Finite Differences and Monte-Carlo
Part II: Advanced Numerical Methods
The Finite Element Method
American Options
Challenging Numerical Problems
Part III : Calibration
Dupire’s equation
An Example
Implied Volatilities
Part IV Complements
Conjugate Gradient Method
Automatic Differentiation
(www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial July
Engineering)
9, 2015
104 / 110
Quasi-Newton Method (Levenberg-Marquardt)
Let C be sol of Dupire’s with vol σ(a), a ∈ R na the spline parameters
n
min J(a) =
σ
E
1X
|Ej |2
2
j=1
0T
Ej = C(Kj , tj ) − Cj ⇒
0T
0
X ∂Ej
∂J
=
Ej
∂ai
∂ai
j
0
J” = E”E + E E ≈ E E + αI
T
Approximate Newton step: (E 0 E 0 + αI)(am+1 − am ) = −E 0 E
"s.txt"
"s.txt"
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
2
1.5
2
1
1.5
0.5
0
0
200
400
600
800
1000
1200
1400
1600
1800
1
0.5
0 0
200
400
600
1600 1800
1200 1400
800 1000
Volatility surface computed with the quasi-Newton method (left) and with the
CG method (right).
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial July
Engineering)
9, 2015
105 / 110
Reduced Basis
Replace the hat functions w i (x) in the formula
uh (x, t) =
N
X
ui (t)w i (x)
1
by a Galerkin method with M << N:
M
X
uRB (x, t) =
1
Z
ai (t)u i (x, t) with ai given by (Q = R+ × (t0 , T ))
i
Z
iσ
2x 2
Z
∂xx uRB ) =
(K − x)+ u i (t0 )
2
R+
σ2x 2
ui
∂xx uRB (x, t)) = 0
Or: ( (uRB (t) − uRB (t − δt)) + u i
2
R δt
uσ (T )u (T ) −
R+Z
i
(uRB ∂t u + u
Q
if u i 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 July
Engineering)
9, 2015
106 / 110
POD or SVD to Choose the basis
Snap shots (used with the Navier-Stokes equations) P
i
Compute the solution of the parabolic PDE u(x, t) = M
1 ai (t)u (x)
where {u i } is obtained by Singular Value Decomposition (SVD) of
366
E.W. S
= 0.5,
We use the parameters
= 0, J = 0.5. Furthermore
λ1are 500 stepsJ in x-direction
Z
discretization parameters
T 150 steps i
Vand
λ
U
2
We
compare
the
Finite
Element
solution
and
the
POD
solution
.
Aij =
u(ti ) · u(tj )dx, A =
1
150 N × N
M×
M is measured asλ151
PIDE. The
deviation
3 i=0 !D(T
i , ·) − DPOD (Ti , ·)
Ω
grid points in T -direction. The
Mresults×are shown
N in Table 1.
or the orthogonal decomposition if symmetry A = PΛQ
Table 1 Accuracy of POD approximations, PIDE model
POD Basis El.
with λ1 ≥ λ2 ..., U orthogonal unitary.
i
3
Here v (x) is the vector associated to λi
4
and its components
on the u(x, tj ) are in U: 5
P
6
u i (x) = uij u(x, tj )
7
E.W. Sachs and M. Schu did it for B& S and 89
10
PIDE and report a real improvement.
O. P. (www//ann.jussieu.fr/pironneau)
Deviation
8.36e-001
1.36e-001
2.17e-002
3.31e-003
4.86e-004
6.82e-005
9.17e-006
1.18e-006
smallest sing. val.
1.63e+003
1.76e+002
2.28e+001
3.11e+000
4.25e-001
5.71e-002
7.46e-003
9.46e-004
Numerical Challenge in Finance Solved by FEM (Financial July
Engineering)
9, 2015
107 / 110
Reduced Basis for the PDE sover
C. Nguyen and T. Patera compute a 2-basket option by a reduced basis
method with snapshots with possibly different constant volatilities .
They have a sophisticated search method for the best basis based on
a posteriori estimates and proven error bounds. The software is open
source
0
!∗N,max
10
10
1
10
2
5
10
15
N
20
25
30
Figure 6: The error indicator !∗N,max as a function of N .
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial July
Engineering)
9, 2015
108 / 110
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;
}
12
"eigen.txt"
10
8
6
4
Perform
R an SVD decomposition of
Aij = R×(0,T ) uσi (x, t)uσj (x, t)
The eigen values ⇒
2
0
-2
-4
-6
-8
-10
0
O. P. (www//ann.jussieu.fr/pironneau)
5
10
15
20
25
30
Numerical Challenge in Finance Solved by FEM (Financial July
Engineering)
9, 2015
35
40
109 / 110
Some Basis Functions
"basis.txt" matrix
160
140
120
100
80
60
40
20
0
-20
"basis.txt" matrix
40
35
30
25
20
15
10
5
0
-5
0
0
5
0
5
10
5
10
15
10
15
20
25
30
35
4060
50
40
30
20
10
0
"basis.txt" matrix
2
0
-2
-4
-6
-8
-10
-12
-14
-16
15
20
25
30
35
4060
50
40
30
20
10
0
20
25
30
35
4060
50
40
30
20
10
First, second and fourth basis function (Recall that these are
non-convex combinations of elementary Black-Scholes solutions).
O. P. (www//ann.jussieu.fr/pironneau)
Numerical Challenge in Finance Solved by FEM (Financial July
Engineering)
9, 2015
110 / 110
0
© Copyright 2026 Paperzz