Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Part II
3. Numerical solution of PDEs
1
4. Numerical Solution of Partial Differential Equations
1. Diffusion Equations of One State Variable.
2
∂u 2 ∂ u
=c , (x, t) ∈ D , (1)
∂t ∂x2
where t is a time variable, x is a state variable, and u(x, t) is an unknown
function satisfying the equation.
To find a well-defined solution, we need to impose the initial condition
2
If D = (−∞, ∞) × (0, ∞), we need to impose the boundary conditions
2
lim u(x, t) e−a x = 0 for any a > 0. (4)
|x|→∞
3
2. Grid Points.
To find a numerical solution to equation (1) with finite difference methods,
we first need to define a set of grid points in the domain D as follows:
Choose a state step size Δx = b−a N (N is an integer) and a time step size Δt,
draw a set of horizontal and vertical lines across D, and get all intersection
points (xj , tn ), or simply (j, n),
where xj = a + j Δx, j = 0, . . . , N , and tn = n Δt, n = 0, 1, . . ..
T
If D = [a, b] × [0, T ] then choose Δt = M (M is an integer) and tn = n Δt,
n = 0, . . . , M .
4
tM = T
..
.
t3
t2
t1
t0 = 0
x0 = a x 1 x2 ... xN = b
5
3. Finite Differences.
The partial derivatives
∂u ∂ 2u
ux := and uxx := 2
∂x ∂x
are always approximated by central difference quotients, i.e.
unj+1 − unj−1 unj+1 − 2 unj + unj−1
ux ≈ and uxx ≈ (5)
2 Δx (Δx)2
at a grid point (j, n). Here unj = u(xj , tn ).
6
4. Explicit Scheme.
If ut is approximated by a forward difference quotient
un+1
j − unj
ut ≈
Δt
at (j, n),
then the corresponding difference equation to (1) at grid point (j, n) is
wjn+1 = λ wj+1
n
+ (1 − 2 λ) wjn + λ wj−1
n
, (6)
where
Δt
λ = c2 .
(Δx)2
The initial condition is wj0 = u0 (xj ), j = 0, . . . , N , and
the boundary conditions are w0n = ga (tn ) and wN
n
= gb (tn ), n = 0, 1, . . ..
The difference equations (6), j = 1, . . . , N − 1, can be solved explicitly.
7
5. Implicit Scheme.
If ut is approximated by a backward difference quotient
un+1
j − unj
ut ≈
Δt
at (j, n + 1),
then the corresponding difference equation to (1) at grid point ( j, n + 1) is
n+1
−λ wj+1 + (1 + 2 λ) wjn+1 − λ wj−1
n+1
= wjn . (7)
The difference equations (7), j = 1, . . . , N −1, together with the initial and
boundary conditions as before, can be solved using the Crout algorithm or
the SOR algorithm.
8
Explicit Method.
Implicit Method.
In matrix form
1 + 2 λ −λ
−λ 1 + 2 λ −λ
. . . . w = b.
. .
−λ 1 + 2 λ
The matrix is tridiagonal and diagonally dominant. ⇒ Crout / SOR.
9
6. Crank–Nicolson Scheme.
The Crank–Nicolson scheme is the average of the explicit scheme at (j, n)
and the implicit scheme at (j, n + 1).
The resulting difference equation is
λ n+1 λ n+1 λ n λ n
− wj−1 + (1 + λ) wjn+1 − wj+1 = wj−1 + (1 − λ) wjn + wj+1 . (8)
2 2 2 2
The difference equations (8), j = 1, . . . , N − 1, together with the initial
and boundary conditions as before, can be solved using Crout algorithm
or SOR algorithm.
10
Crank–Nicolson.
1
[(†) + (‡)] gives
2
wjn+1 − wjn n
1 2 wj−1 − 2 wjn + wj+1
n n+1 n+1 n+1
1 2 wj−1 − 2 wj + wj+1
= c + c
Δt 2 (Δx)2 2 (Δx)2
Δt
Letting μ := 12 c2 (Δx) 2 =
λ
2 gives
n+1
− μ wj+1 + (1 + 2 μ) wjn+1 − μ wj−1
n+1
bjn+1
=w
bjn+1 = μ wj+1
and w n
+ (1 − 2 μ) wjn + μ wj−1
n
.
bjn+1
w — predictor (explicit method)
wjn+1 — corrector (implicit method)
11
7. Local Truncation Errors.
These are measures of the error by which the exact solution of a differential
equation does not satisfy the difference equation at the grid points and are
obtained by substituting the exact solution of the continuous problem into
the numerical scheme.
A necessary condition for the convergence of the numerical solutions to the
continuous solution is that the local truncation error tends to zero as the
step size goes to zero. In this case the method is said to be consistent.
It can be shown that all three methods are consistent.
The explicit and implicit schemes have local truncation errors O(Δt, (Δx)2 ),
while that of the Crank–Nicolson scheme is O((Δt)2 , (Δx)2 ).
12
Local Truncation Error.
For the explicit scheme we get for the LTE at (j, n)
13
8. Numerical Stability.
Consistency is only a necessary but not a sufficient condition for conver-
gence.
Roundoff errors incurred during calculations may lead to a blow up of the
solution or erode the whole computation.
A scheme is stable if roundoff errors are not amplified in the calculations.
The Fourier method can be used to check if a scheme is stable.
Assume that a numerical scheme admits a solution of the form
14
Define
a(n+1) (ω)
G(ω) = (n) ,
a (ω)
where G(ω) is an amplification factor, which governs the growth of the
Fourier component a(ω).
The von Neumann stability condition is given by
|G(ω)| ≤ 1
for 0 ≤ ωΔx ≤ π.
It can be shown that the explicit scheme is stable if and only if λ ≤ 12 ,
called conditionally stable, and the implicit and Crank–Nicolson schemes
are stable for any values of λ, called unconditionally stable.
15
Stability Analysis.
For the explicit scheme we get on substituting (9) into (6) that
a(n+1) (ω) ei j ω Δx = λ a(n) (ω) ei (j+1) ω Δx + (1 − 2 λ) a(n) (ω) ei j ω Δx + λ a(n) (ω) ei (j−1) ω Δx
a(n+1) (ω)
⇒ G(ω) = (n)
= λ ei ω Δx + (1 − 2 λ) + λ e−i ω Δx .
a (ω)
The von Neumann stability condition then is
|G(ω)| ≤ 1 ⇐⇒ |λ ei ω Δx + (1 − 2 λ) + λ e−i ω Δx | ≤ 1
⇐⇒ |(1 − 2 λ) + 2 λ cos(ω Δx)| ≤ 1
2 ω Δx
⇐⇒ |1 − 4 λ sin |≤1 [cos 2 α = 1 − 2 sin2 α]
2
ω Δx
⇐⇒ 0 ≤ 4 λ sin2 ≤2
2
1
⇐⇒ 0≤λ≤
2 sin2 ω Δx
2
for all 0 ≤ ω Δx ≤ π.
This is equivalent to 0 ≤ λ ≤ 12 .
16
Remark.
The explicit method is stable, if and only if
(Δx)2
Δt ≤ . (†)
2 c2
1
(†) is a strong restriction on the time step size Δt. If Δx is reduced to 2 Δx,
then Δt must be reduced to 14 Δt.
So the total computational work increases by a factor 8.
Example.
17
Remark.
In vector notation, the explicit scheme can be written as
wn+1 = A wn + bn ,
−1 ) ∈ R
N −1
where wn = (w1n , . . . , wN
n T
and
λ w0n
1 − 2λ λ
0
λ 1 − 2λ λ .
A= ... ... ∈ R(N −1)×(N −1) , bn = .
. ∈R
N −1
.
0
λ 1 − 2λ n
λ wN
18
Remark.
Forward diffusion equation: ut − c2 uxx = 0 t ≥ 0.
Backward diffusion equation:
ut + c2 uxx = 0 t≤T
u(x, T ) = uT (x) ∀x
u(a, t) = ga (t), u(b, t) = gb (t) ∀t [as before]
The von Neumann stability condition for the backward problem then becomes
n
a (ω)
e
|G(ω)| = n+1 ≤ 1.
a (ω)
19
Stability of the Binomial Model.
The binomial model is an explicit method for a backward equation.
1 1
Vjn = (p Vj+1 n+1 n+1
+ (1 − p) Vj−1 ) = (p Vj+1 n+1
+ 0 Vjn+1 + (1 − p) Vj−1 n+1
)
R R
for j = −n, −n + 2, . . . , n − 2, n and n = N − 1, . . . , 1, 0.
N N N N
Here the initial values V−N , V−N +2 , . . . , VN −2 , VN are given.
Now let Vjn = a(n) (ω) ei j ω Δx , then
(n) i j ω Δx 1 (n+1) i (j+1) ω Δx (n+1) i (j−1) ω Δx
a (ω) e = pa (ω) e + (1 − p) a (ω) e
R
e
⇒ G(ω) = p ei ω Δx + (1 − p) e−i ω Δx e−r Δt
= cos(ω Δx) + (2 p − 1) i sin(ω Δx) e−r Δt
| {z }
= q
2
e
⇒ |G(ω)| = (cos2 (ω Δx) + q 2 sin2 (ω Δx)) e−2 r Δt
= (1 + (q 2 − 1) sin2 (ω Δx)) e−2 r Δt
≤ e−2 r Δt ≤ 1
if q 2 ≤ 1 ⇐⇒ −1 ≤ q ≤ 1 ⇐⇒ p ∈ [0, 1]. Hence the binomial model is
stable.
20
Stability of the CRR Model.
We know that the binomial model is stable if p ∈ (0, 1).
For the CRR model we have that
√ √ R−d
u = eσ Δt , d = e−σ Δt , p = ,
u−d
so p ∈ (0, 1) is equivalent to u > R > d.
Clearly, for Δt small, we can ensure that
√
Δt
e σ
> er Δt .
σ2
Hence the CRR model is stable, if Δt is sufficiently small, i.e. if Δt < r2 .
21
9. Simplification of the BSE.
Assume V (S, t) is the price of a European option at time t.
Then V satisfies the Black–Scholes equation (??) with appropriate initial
and boundary conditions.
Define
τ = T − t, x = ln S, w(τ, x) = eα x+β τ V (t, S) ,
where α and β are parameters.
Then the Black–Scholes equation can be transformed into a basic diffusion
equation:
∂w 1 2 ∂ 2w
= σ
∂τ 2 ∂x2
with a new set of initial and boundary conditions.
Finite difference methods can be used to solve the corresponding difference
equations and hence to derive option values at grid points.
22
Transformation of the BSE.
Consider a call option.
Let τ = T − t be the remaining time to maturity. Set u(x, τ ) = V (x, t). Then
∂u ∂V
∂τ = − ∂t and the BSE (??) is equivalent to
1 2 2
uτ = σ S uSS + r S uS − r u , (†)
2
u(S, 0) = V (S, T ) = (S − X)+ , (IC)
u(0, τ ) = V (0, t) = 0 , u(S, τ ) = V (S, t) ≈ S as S → ∞. (BC)
23
2
Note that the growth condition (4), lim |x|→∞ ũ(x, τ ) e−a x = 0 for any a > 0, is
satisfied. Hence (‡) is well defined.
Let w(x, τ ) = eα x+β τ ũ(x, τ ) ⇐⇒ ũ(x, τ ) = e−α x−β τ w(x, τ ) =: C w(x, τ ).
Then
ũτ = C (−β w + wτ )
ũx = C (−α w + wx )
ũxx = C (−α (−α w + wx ) + (−α wx + wxx )) = C (α2 w − 2 α wx + wxx ) .
So (‡) is equivalent to
1 2 1
C (−β w + wτ ) = σ C (α2 w − 2 α wx + wxx ) + (r − σ 2 ) C (−α w + wx ) − r C w .
2 2
In order to cancel the w and wx terms we need to have
( (
1 2 2 1 2 1 1 2
−β = 2 σ α − (r − 2 σ ) α − r , α= σ 2 (r − 2 σ ) ,
⇐⇒
0 = 12 σ 2 (−2 α) + r − 12 σ 2 . β= 1 1 2 2
2 σ 2 (r − 2 σ ) +r.
24
With this choice of α and β the equation (‡) is equivalent to
1 2
wτ = σ wxx , (])
2
w(x, 0) = eα x ũ(x, 0) = eα x (ex − X)+ , (IC)
w(x, τ ) = 0 as x → −∞ , w(x, τ ) ≈ eα x+β τ ex as x → ∞. (BC)
Note that the growth condition (4) is satisfied. Hence (]) is well defined.
Implementation.
25
3. Solve (]) with
w(x, 0) = eα x (ex − X)+ , (IC)
(
e(α+1) b+β τ or
w(a, τ ) = 0 , w(b, τ ) = . (BC)
eα b (eb − X) eβ τ (a better choice)
Note: If the explicit method is used, N and M need to be chosen such that
1 2 Δτ 1 σ 2 (T − t) 2
σ ≤ ⇐⇒ M≥ N .
2 (Δx)2 2 (b − a)2
If the implicit or Crank–Nicolson scheme is used, there are no restrictions
on N , M . Use Crout or SOR to solve.
4. Assume w(xj , τM ), j = 0, 1, . . . , N are the solutions from step 3, then the
call option price at time t is
V (Sj , t) = e−α xj −β (T −t) w(xj , T − }t)
| {z j = 0, 1, . . . , N ,
τM
where Sj = exj and T − t ≡ τM .
Note: The Sj are not equally spaced.
26
10. Direct Discretization of the BSE.
Exercise: Apply the Crank–Nicolson scheme directly to the BSE (??),
i.e. there is no transformation of variables, and write out the resulting
difference equations and do a stability analysis.
C++ Exercise: Write a program to solve the BSE (??) using the result of
the previous exercise and the Crout algorithm. The inputs are the interest
rate r, the volatility σ, the current time t, the expiry time T , the strike
price X, the maximum price Smax , the number of intervals N in [0, Smax ],
and the number of subintervals M in [t, T ]. The output are the asset prices
Sj , j = 0, 1, . . . , N , at time t, and their corresponding European call and
put prices (with the same strike price X).
27
11. Greeks.
Assume that the asset prices Sj and option values Vj , j = 0, 1, . . . , N , are
known at time t.
The sensitivities of V at Sj , j = 1, . . . , N − 1, are computed as follows:
∂V Vj+1 − Vj−1
δj = |S=Sj ≈ ,
∂S Sj+1 − Sj−1
Vj+1 − Vj−1
which is , if S is equally spaced.
2 ΔS
Vj+1 −Vj Vj −Vj−1
∂ 2V Sj+1 −Sj − Sj −Sj−1
γj = |S=Sj ≈ ,
∂S 2 Sj − Sj−1
Vj+1 − 2 Vj + Vj−1
which is , if S is equally spaced.
(ΔS)2
28
12. Diffusion Equations of Two State Variables.
2
∂u 2 ∂ u ∂ 2u
=α + , (x, y, t) ∈ [a, b] × [c, d] × [0, ∞). (10)
∂t ∂x2 ∂y 2
The initial conditions are
and
Here we assume that all the functions involved are consistent, in the sense
that they have the same value at common points, e.g. ga (c, t) = gc (a, t) for
all t ≥ 0.
29
13. Grid Points.
(xi , yj , tn ), where
b−a
xi = a + i Δx, , i = 0, . . . , I ,
Δx =
I
d−c
yj = c + j Δy, Δy = , j = 0, . . . , J ,
J
T
tn = n Δt, Δt = , n = 0, . . . , N
N
and I, J, N are integers.
30
Depending on how ut is approximated, we have three basic schemes: ex-
plicit, implicit, and Crank–Nicolson schemes.
31
14. Explicit Scheme.
If ut is approximated by a forward difference quotient
un+1 n
i,j − ui,j
ut ≈
Δt
at (i, j, n),
then the corresponding difference equation at grid point (i, j, n) is
n+1 n n n n n
wi,j = (1 − 2 λ − 2 μ) wi,j + λ wi+1,j + λ wi−1,j + μ wi,j+1 + μ wi,j−1 (11)
32
15. Implicit Scheme.
un+1 −uni,j
If ut is approximated by a backward difference quotient ut ≈ i,j Δt at
(i, j, n + 1), then the difference equation at grid point (i, j, n + 1) is
n+1 n+1 n+1 n+1 n+1 n
(1 + 2 λ + 2 μ) wi,j − λ wi+1,j − λ wi−1,j − μ wi,j+1 − μ wi,j−1 = wi,j (12)
for i = 1, . . . , I − 1 and j = 1, . . . , J − 1.
For fixed n, there are (I − 1) (J − 1) unknowns and equations. (12) can be
solved by relabeling the grid points and using the SOR algorithm.
(12) is unconditionally stable with local truncation error O(Δt, (Δx)2 , (Δy)2 ),
but is more difficult to solve, as it is no longer tridiagonal, so the Crout
algorithm cannot be applied.
33
Solving the Implicit Scheme.
n+1 n+1 n+1 n+1 n+1 n
(1 + 2 λ + 2 μ) wi,j − λ wi+1,j − λ wi−1,j − μ wi,j+1 − μ wi,j−1 = wi,j
2. For k = 0, 1, . . .
For i = 1, . . . I − 1, j = 1, . . . , J − 1
k+1 1 n n+1,k n+1,k+1 n+1,k n+1,k+1
wbi,j = 1+2 λ+2 μ wi,j + λ wi+1,j + λ wi−1,j + μ wi,j+1 + μ wi,j−1
n+1,k+1 n+1,k k+1
wi,j = (1 − ω) wi,j bi,j
+ωw
34
With Block Jacobi/Gauss–Seidel.
w1,j e1
w
w w
2,j e2
Denote wej = .. ∈ RI−1 , j = 1, . . . , J −1, w = .. ∈ R(I−1) (J−1) .
. .
wI−1,j eJ−1
w
On setting c = 1 + 2 λ + 2 μ, we have from (12) for j fixed
c −λ w1,j w1,j+1 w1,j−1
−λ c −λ w −μ w −μ
2,j . . 2,j+1 . . w2,j−1
... + . + .
... ..
. ..
.
−μ −μ
−λ c wI−1,j wI−1,j+1 wI−1,j−1
n n+1
w1,j + λ w0,j
n
w2,j
.
=
..
⇐⇒ Awejn+1 + B w n+1
ej+1 +Bw n+1
ej−1 = dnj
n
wI−2,j
n n+1
wI−1,j + λ wI,j
35
Rewriting
ejn+1 + B w
Aw n+1
ej+1 n+1
ej−1
+Bw = dnj j = 1, . . . , J − 1
as
A B e1n+1
w −B dn1 w e
e0n+1
d1n
n+1
B A B e
w dn2 dn2
2
... ... ... ... = ..
. =: ... ,
.. n n
B A B . dJ−2 dJ−2
B A n+1
eJ−1
w dnJ−1 − B weJn+1 denJ−1
where we0n+1 and weJn+1 represent boundary points, leads to the following Block
Jacobi iteration: For k = 0, 1, . . .
e2n+1,k + den1
e1n+1,k+1 = −B w
Aw
e2n+1,k+1 = −B w
Aw e1n+1,k − B w
e3n+1,k + dn2
..
.
n+1,k+1 n+1,k n+1,k
eJ−2
Aw = −B w
eJ−3 −Bw eJ−1 + dnJ−2
en+1,k+1 = −B w
Aw J−1 en+1,k + den
J−2 J−1
36
Similarly, the Block Gauss–Seidel iteration is given by:
For k = 0, 1, . . .
e2n+1,k + den1
e1n+1,k+1 = −B w
Aw
e2n+1,k+1 = −B w
Aw e1n+1,k+1 − B w
e3n+1,k + dn2
..
.
n+1,k+1 n+1,k+1 n+1,k
eJ−2
Aw = −B w
eJ−3 −BweJ−1 + dnJ−2
en+1,k+1 = −B w
Aw J−1 en+1,k + den
J−2 J−1
ejn+1,k+1 , j = 1, . . . , J − 1.
In each case, use the Crout algorithm to solve for w
37
17. Alternating Direction Implicit (ADI) Method.
An alternative finite difference method is the ADI scheme, which is un-
conditionally stable while the difference equations are still tridiagonal and
diagonally dominant.
The ADI algorithm can be used to efficiently solve the Black–Scholes two
asset pricing equation:
38
ADI scheme
Implicit method at (i, j, n + 1):
n+1
wi,j n
− wi,j 2
n
wi+1,j n
− 2 wi,j n
+ wi−1,j wn+1 − 2 wi,j
2 i,j+1
n+1 n+1
+ wi,j−1
= α +α
Δt (Δx)2 (Δy)2
| {z }
approx. uxx using (i, j, n) data
39
n+1
To solve (†), fix i = 1, . . . , I − 1 and solve a tridiagonal system to get wi,j for
j = 1, . . . , J − 1.
This can be done with e.g. the Crout algorithm.
n+2
To solve (‡), fix j = 1, . . . , J − 1 and solve a tridiagonal system to get wi,j for
i = 1, . . . , I − 1.
Currently the method works on the interval [tn , tn+2 ] and has features of an
explicit method. In order to obtain an (unconditionally stable) implicit method,
we need to adapt the method so that it works on the interval [tn , tn+1 ] and hence
n
gives values wi,j for all n = 1, . . . , N .
n+ 12
Introduce the intermediate time point n + 12 . Then (†) generates wi,j (not
n+1
used) and (‡) generates wi,j .
μ n+ 12 n+ 1 μ n+ 12 λ n n λ n
− wi,j+1 + (1 + μ) wi,j 2 − wi,j−1 = wi+1,j + (1 − λ) wi,j + wi−1,j (†)
2 2 2 2
λ n+1 n+1 λ n+1 μ n+ 21
n+ 1 μ n+ 12
− wi+1,j + (1 + λ) wi,j − wi−1,j = wi,j+1 + (1 − μ) wi,j 2 + wi,j−1 (‡)
2 2 2 2
40