Mathematicalmethods Lecture Slides Week8 NumericalMethods
Mathematicalmethods Lecture Slides Week8 NumericalMethods
8. Numerical Methods
1 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
2 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
3 / 48
Implied Volatility
The Black-Scholes formula for the price of a European call option
C = Se q(T t) (d1 ) Ke r (T t) (d2 )
where
d1 =
log
S
K
+ r q + 2
T t
(T t)
d2 = d1 T t
8. Numerical Methods
4 / 48
Implied Volatility
The implied volatility problem is to find so that the Black-Scholes
price and the market price are equal
If S, K , q, r , T , and t are known, consider
f () = Se q(T t) (d1 ) Ke r (T t) (d2 ) C
Finding the implied volatility boils down to solving the nonlinear
problem
f () = 0
Problem can be solved numerically
For example, plot f () and see where it is equal to zero
8. Numerical Methods
5 / 48
Implied Volatility
R function to compute Black-Scholes call price
bsc <- function(S, T, t, K, r, s, q) {
d1 <- (log(S/K)+(r-q+0.5*s2)*(T-t))/(s*sqrt(T-t))
d2 <- d1-s*sqrt(T-t)
S*exp(-q*(T-t))*pnorm(d1)-K*exp(-r*(T-t))*pnorm(d2)
}
Since R treats all variables as vectors
> bsc(50, 0.5, 0.0, 45, 0.06, 0.2, 0.02)
[1] 6.508365
> bsc(50, 0.5, 0.0, 45, 0.06, c(0.15, 0.2, 0.25), 0.02)
[1] 6.119266 6.508365 6.986157
Kjell Konis (Copyright 2013)
8. Numerical Methods
6 / 48
Implied Volatility
Suppose the option sold for $7, find
Plot f () over a range of values and see where it crosses the x axis
> sigmas <- seq(0.05, 0.5, by = 0.01)
> fsig <- bsc(50, 0.5, 0.0, 45, 0.06, sigmas, 0.02) - 7
f()
1
0.1
0.2
0.3
0.4
0.5
8. Numerical Methods
7 / 48
Implied Volatility
8. Numerical Methods
8 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
9 / 48
Bisection Method
Goto 1
8. Numerical Methods
10 / 48
f()
1
f(b)
f(a)
0.1
f(c)
0.2
0.3
0.4
0.5
Each step preserves sign f (a) = sign f (b) , cuts interval in half
8. Numerical Methods
11 / 48
8. Numerical Methods
12 / 48
8. Numerical Methods
13 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
14 / 48
Newtons Method
Commonly used method for solving nonlinear equations
Again, want to find x so that f (x ) = 0
Assumptions
f (x ) is differentiable
Starting point x0
Idea: approximate f (x ) with a first order Taylor polynomial around xk
f (x ) f (xk ) + (x xk )f 0 (xk )
Want to choose xk+1 so that f (xk+1 ) = 0
f (xk ) + (xk+1 xk )f 0 (xk ) = f (xk+1 ) 0
Leads to the recursion
xk+1 = xk
Kjell Konis (Copyright 2013)
f (xk )
f 0 (xk )
8. Numerical Methods
15 / 48
f(x0)
f(x1)
f(x2)
x*
x3
x2
x1
x0
8. Numerical Methods
16 / 48
(x xk )2 00
f (k )
2
k [x , xk ]
f (xk )
f 00 (k )
+
(x
x
)
=
(x xk )2
k
f 0 (xk )
2f 0 (xk )
x xk+1 =
f 00 (k )
(x xk )2
2f 0 (xk )
f 00 (k ) 2
2f 0 (xk ) k
are bounded on the interval where the solver is active
00
f (k )
2
|k+1 | M |k |
M = max 0
2f (xk )
k+1 =
If f 0 and f 00
8. Numerical Methods
17 / 48
Caveats
Quadratic convergence not guaranteed
Need good starting point + well-behaved function
It gets worse: convergence not guaranteed
In particular, algorithm may cycle
For example: sin(x ) = 0 between 2 and
There is an xk such that
xk+1 = xk
sin(xk )
= xk
cos(xk )
sin(xk+1 )
sin(xk )
sin(xk )
= xk
= xk +
cos(xk+1 )
cos(xk )
cos(xk )
= xk
Kjell Konis (Copyright 2013)
8. Numerical Methods
18 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
19 / 48
F1
x1 (x )
F2 (x )
x1
D F (x ) =
..
Fn
x1 (x )
F1
x2 (x )
F2
x2 (x )
..
.
..
Fn
x2 (x )
F1
xn (x )
F2
(x
)
xn
..
Fn
xn (x )
8. Numerical Methods
20 / 48
xk+1 = xk D F (xk )
1
F (xk )
8. Numerical Methods
21 / 48
Algorithm
Need starting point x0
If D F (xk ) is nonsingular, can compute xk+1 using the recursion
xk+1 = xk D F (xk )
1
F (xk )
u =
1
D F (xk ) u =
D F (xk )
F (xk )
1
D F (xk ) D F (xk )
F (xk ) = F (xk )
8. Numerical Methods
22 / 48
Example
Let g(x , y ) = 1 (x 1)4 (y 1)4
Local maximum at (1, 1) = critical point at (1, 1)
Gradient of g(x , y )
F (x , y ) = D g(x , y )
T
= 4(x 1)3
4(y 1)3
T
Gradient of F (x , y )
2
12(x 1)
D F (x , y ) =
0
12(y
1)2
8. Numerical Methods
23 / 48
Example: R Implementation
First, need functions for F (x , y ) and its gradient
F <- function(x, y)
c(4*(x - 1)3, 4*(y - 1)3)
DF <- function(x, y)
diag(c(12*(x - 1)2, 12*(y - 1)2))
Need starting point:
x <- c(0, 0)
Do 25 Newton iterations
for(i in 1:25)
x <- x - solve(DF(x[1], x[2]), F(x[1], x[2]))
Result
[1] 0.9999604 0.9999604
Kjell Konis (Copyright 2013)
8. Numerical Methods
24 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
25 / 48
Lagranges Method
Lagranges method for solving constrained optimization problems
Need to find the critical points of the Lagrangian
Easy in certain cases: minimum variance portfolio (solve linear
system)
Difficult when system is nonlinear: maximum expected return
portfolio
4x2 2x3
2x1 x2 x3 = 0
x12 + x22 13 = 0
8. Numerical Methods
26 / 48
Newtons Method
Lagrangian
G(x , ) = 4x2 2x3 + 1 (2x1 x2 x3 ) + 2 (x12 + x22 13)
T
Set F (x , ) = D G(x , )
4 + 22 x1 = 0
set
6 + 22 x2 = 0
set
2 1 = 0
set
2x1 x2 x3 = 0
set
x12 + x22 13 = 0
Nonlinear equation to solve
4 + 22 x1
6 + 22 x2
F (x , 2 ) =
2x1 x2 x3
x12 + x22 13
22
0
0 2x1
0 2
0 2x2
2
D F (x , 2 ) =
2 1 1
0
2x1 2x2
0
0
8. Numerical Methods
27 / 48
8. Numerical Methods
28 / 48
Starting point
x <- rep(1, 4)
15 Newton iterations
for(i in 1:15)
x <- x - solve(DF(x), F(x))
Code for Newton iterations very simple!
8. Numerical Methods
29 / 48
8. Numerical Methods
30 / 48
Convergence Rate
600
100
10
15
Iteration
10
300
400
log( xk x* )
200
xk x*
500
10
15
Iteration
8. Numerical Methods
31 / 48
Observations
From a given starting point, Newtons method converges (if it
converges) to a single value x
Starting at (1, 1, 1, 1), computed solution (2, 3, 7, 1)
For this particular problem, know there are 2 critical points
Try another starting point
x <- -rep(1, 4)
for(i in 1:15)
x <- x - solve(DF(x), F(x))
Solution: (2, 3, 7, 1)
In general, will not know the number of critical points
Need additional information about the problem
Multiple starting points
Kjell Konis (Copyright 2013)
8. Numerical Methods
32 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
33 / 48
Lagrangian
F (x , ) = 3x1 4x2 + x3 2x4
+ 1 (x22 + x32 + x42 1)
+ 2 (3x12 + x32 + 2x42 6)
Kjell Konis (Copyright 2013)
8. Numerical Methods
34 / 48
4
21 x2
1 + 2 x + 2 x
T
1 3
2 3
G(x , ) = D F (x , ) =
2 + 21 x4 + 42 x4
62
0
0
0
0
6x1
0
21
0
0
2x2 0
0
0
21 + 22
0
2x3 2x3
D G(x , ) =
0
0
0
21 + 42 2x4 2x4
0
2x2
2x3
2x4
0
0
6x1
0
2x3
4x4
0
0
8. Numerical Methods
35 / 48
R Implementation
Function to compute G(x , )
G <- function(x)
c(3 + 6*x[6]*x[1], -4 - 2*x[5]*x[2],
1 + 2*x[5]*x[3] + 2*x[6]*x[3],
-2 + 2*x[5]*x[4] + 4*x[6]*x[4],
-x[2]2 + x[3]2 + x[4]2 - 1,
3*x[1]2 + x[3]2 + 2*x[4]2 - 6)
8. Numerical Methods
36 / 48
Newtons Method
Starting point
x <- c(1, -1, 1, -1, 1, -1)
Newton iterations
for(i in 1:25)
x <- x - solve(DG(x), G(x))
Numeric solution
> x
[1]
[5]
0.4067205 -2.0091498
0.9954460 -1.2293456
2.1376693 -0.6834125
That is
0.4067205
2.0091498
xc =
2.1376693
0.6834125
"
0.9954460
c =
1.2293456
8. Numerical Methods
37 / 48
Analysis
Does the point (xc , c ) correspond to a minimum or a maximum?
Value of objective at (xc , c )
f <- function(x) 3*x[1] - 4*x[2] + x[3] - 2*x[4]
> f(x)
[1] 12.76125
Rewrite Lagrangian with fixed multipliers (for feasible x )
f (x ) = F (x , c ) = 3x1 4x2 + x3 2x4
+ 0.9954460 (x22 + x32 + x42 1)
1.2293456 (3x12 + x32 + 2x42 6)
Constrained min/max f (x ) min/max F (x , c )
Kjell Konis (Copyright 2013)
8. Numerical Methods
38 / 48
Analysis
Already know xc is a critical point of F (x , c )
xc minimum if D 2 F (xc , c ) positive definite
xc maximum if D 2 F (xc , c ) negative definite
Already have D 2 F (xc , c ), upper-left 4 4 block of D G(xc , c )
> round(DG(x), digits = 3)
[,1]
[,2]
[,3]
[,4]
[,5]
[,6]
[1,] -7.376 0.000 0.000 0.000 0.000 2.440
[2,] 0.000 -1.991 0.000 0.000 4.018 0.000
[3,] 0.000 0.000 -0.468 0.000 4.275 4.275
[4,] 0.000 0.000 0.000 -2.926 -1.367 -1.367
[5,] 0.000 4.018 4.275 -1.367 0.000 0.000
[6,] 2.440 0.000 4.275 -2.734 0.000 0.000
8. Numerical Methods
39 / 48
Outline
1
Implied Volatility
Bisection Method
Newtons Method
8. Numerical Methods
40 / 48
T w
eTw = 1
w T w = P2
8. Numerical Methods
41 / 48
w T w P2
eTw 1
eTw 1
G(w , ) = D F (w , ) =
=0
w T w P2
Gradient of G
22 e 2w
0
0
D G(w , ) = e T
2(w )T 0
0
8. Numerical Methods
42 / 48
Example
Vector of expected returns
= (0.08, 0.10, 0.13, 0.15, 0.20)
Asset returns covariance matrix
0.019600 0.007560
0.012880
0.008750 0.009800
0.007560
0.032400
0.004140
0.009000
0.009450
0.052900
0.020125
0.020125
= 0.012880 0.004140
0.008750 0.009000
0.020125
0.062500 0.013125
0.009800
0.009450
0.020125 0.013125
0.122500
8. Numerical Methods
43 / 48
R Implementation
Definition of G(w , )
+ 1 e + 22 w
eTw 1
G(w , ) =
w T w P2
8. Numerical Methods
44 / 48
R Implementation
Gradient of G
22 e 2w
0
0
D G(w , ) = e T
2(w )T 0
0
8. Numerical Methods
45 / 48
R Implementation
From starting point
> x <- c(rep(0.5, 5), 1, 1)
> x
[1] 0.5 0.5 0.5 0.5 0.5 1.0 1.0
Newton iterations
> for(i in 1:25)
x <- x - solve(DG(x, mu, Sigma, 0.252),
G(x, mu, Sigma, 0.252))
Numerical solution
> x
[1] -0.39550317 0.09606231 0.04583865
[5] 0.54372203 -0.09200813 -0.85714641
Kjell Konis (Copyright 2013)
8. Numerical Methods
0.70988017
46 / 48
Analysis
Recall: upper-left n n block of D G(w , c ) Hessian of F (w , c )
> DG(x, mu, Sigma, sigmaP2)[1:5, 1:5]
[,1]
[,2]
[,3]
[,4]
[,5]
[1,] -0.03360 0.01296 -0.02208 -0.01500 0.0168
[2,] 0.01296 -0.05554 0.00710 0.01543 -0.0162
[3,] -0.02208 0.00710 -0.09069 -0.03450 -0.0345
[4,] -0.01500 0.01543 -0.03450 -0.10714 0.0225
[5,] 0.01680 -0.01620 -0.03450 0.02250 -0.2100
Can check second order condition by computing eigenvalues
> eigen(DG(x, mu, Sigma, sigmaP2)[1:5, 1:5])$values
[1] -0.02024 -0.05059 -0.05806 -0.14445 -0.22364
Computed w is a constrained maximum
> t(x[1:5]) %*% mu
[1] 0.1991514
Kjell Konis (Copyright 2013)
8. Numerical Methods
47 / 48
http://computational-finance.uw.edu
8. Numerical Methods
48 / 48