Creation of this worksheet was supported by IT Academy program of Information Technology Foundation for Education (HITSA)

Exercises of lab 11

The aim of the lab is to implement Basic Implicit and Crank-Nicolson methods for solving option pricing problems

we consider the problem

\begin{align*} \frac{\partial u}{\partial t}(x,t)&+\alpha(x,t)\frac{\partial^2 u}{\partial x^2}(x,t)+ \beta(x,t)\frac{\partial u}{\partial x}(x,t)-r\, u(x,t)=0,\ x\in (x_{min},x_{max}), 0\leq t<T, \\ u(x_{min},t)&=\phi_1(x_{min},t), 0\leq t<T, \\ u(x_{max},t)&=\phi_2(x_{max},t), 0\leq t<T, \\ u(x,T)&=u_0(x),\ x\in (x_{min},x_{max}). \end{align*}

We introduce the points $x_i=x_{min}+i\Delta x,\ i=0,\ldots,n$ and $t_k=k\Delta t,\ k=0,\ldots,m$ and denote by $U_{i,k}$ the =approximate values of $u(x_i,t_k)$. Here $\Delta x=\frac{x_{max}-x_{min}}{n}$ and $\Delta t=\frac{T}{m}$.

When we want to solve the transformed BS equation, we have to define $$\alpha(x,t)=\frac{\sigma^2(e^x,t)}{2},\ \beta(x,t)=r-D-\alpha(x,t),\ u_0(x)=p(e^x)$$ and correct boundary conditions, in the case of solving untransformed BS equation we have $$\alpha(x,t)=\frac{x^2\sigma^2(x,t)}{2},\ \beta(x,t)=(r-D)x,\ u_0(x)=p(x)$$ and corresponding boundary conditions. If we use $x_{min}=0$ when solving untransformed BS equation, we have an exact boundary condition $$\phi_1(x_{min},t)=p(0)e^{-r\,(T-t)}.$$

Basic Implicit method

In the case of the basic implicit finite difference method we compute the values $U_{ik}$ as follows

  1. Using the final condition we set \begin{align*} U_{im}&=u_0(x_i),\ i=0,\ldots,n \end{align*}
  2. For each value $k=m-1,m-2,\ldots,0$ we find $U_{ik},\ i=0,\ldots,n,\ k=m-1,\ldots,0$ by solving the system of equations \begin{align*} U_{0k}&=\phi_1(t_{k}),\\ a_{ik}U_{i-1,k}+b_{ik}U_{ik}+c_{ik}U_{i+1,k}&=U_{i,k+1},\ i=1,\ldots,n-1,\\ U_{nk}&= \phi_2(t_{k})\\ \end{align*} for the unknown values of $U_{ik},\ i=0,\ldots,n$. Here \begin{align*} a_{ik}&=-\frac{\alpha(x_i,t_k)\Delta t}{\Delta x^2}+\frac{\beta(x_i,t_k)\Delta t}{2\Delta x},\\ b_{ik}&=1+\frac{2\alpha(x_i,t_k)\Delta t}{\Delta x^2}+r\Delta t,\\ c_{ik}&=-\frac{\alpha(x_i,t_k)\Delta t}{\Delta x^2}-\frac{\beta(x_i,t_k)\Delta t}{2\Delta x}. \end{align*}

Exercise 1

Write a function that for given values of $m$, $n$, $x_{min},x_{max}$, $T$ and for given functions $p$,$\sigma$, $\phi_1$ and $\phi_2$ returns the values $U_{i0},\ i=0,\ldots,n$ of the approximate solution (option prices) obtained by solving the transformed BS equation by the implicit finite difference method. Use this method for computing approximate values of the option price in the case $r=0.01$, $\sigma(s,t)=0.5$, $D=0.05$, $T=0.5$, $E=100$, $S0=90$, $p(s)=2\cdot |E-s|$, $\rho=2$, $x_{min}=\ln \frac{S0}{\rho},\ x_{max}=\ln(\rho\, S0)$ for $n=80$, $m=320$ and for $n=160$, $m=1280$. Use $\phi_1(t)=p(e^{x_{min}}),\ \phi_2(t)=p(e^{x_{max}})$ and estimate the discretization error of the last result by Runge's method. Find also the actual error of the last result.

Solution

In [ ]:
import numpy as np
from scipy import linalg
#Recommendation: implement the solver so that it can be used for both transformed/untransformed equation
#This can be achieved by assuming that xmin, xmax, alpha, beta, u0, phi1 and phi2 are defined outside and given as input arguments to the solver
#Write the solver so that it works when parameters alpha and beta are functions of x and t
#return the values corresponding to t=0


def implicit_solver(m,n,xmin,xmax,r,T,alpha,beta,u0,phi1,phi2):
    """alpha and beta are assumed to be functions of x and t
    phi1,phi2 are functions of xmin,t and xmax,t
    """

    return U[:,0] #option prices for t=0
#define the functions p, sigma (as a function of 2 variables with constant return value) and parameters for the problem
#also alpha, beta, u0 for transformed problem

#do computations
#and estimate the discretization error
Check your results! answer1: 55.790099688336106 answer2: 55.814190260051035 Runge's error estimate: -0.008030190571642967

Since in the case of the test problem volatility is actually constant and the pay-off function is continuous and piecewise linear, it is possible to express the exact solution in term of pricing functions of the Put and Call options. For this we notice that the payoff function is just two times the sum of payoff functions with the Put and Call options with exercise price E, so the linearity of the problem implies that the solution is also two times the sum of Put and Call pricing functions for all time values and stock prices. Compute the exact price of the option and the actual error of the last answer.

In [ ]:
#use put and call functions defined before
Check your result! exact answer: 55.78697757199063 actual absolute error: 0.027212688060402

Exercise 2

Repeat the previous exercise in the case of boundary conditions derived from special solutions.

Solution

In [ ]:
 
Check your results! answer1: 55.7503787079978 answer2: 55.774964472558025 Runge's error estimate: -0.008195254853409514 Actual error: 0.012013099432607532

You should see that the Runge's error estimate is for both boundary conditions smaller than the actual error. This can be expained by the presense of the truncation error (since the actual error is the sum of truncation error and discretization error). It is clear that the special boundary conditions gave us an answer with smaller truncation error and therefore also with smaller actual error.

Crank-Nicolson method

Algorithm for CN method is as follows:

  1. Using the final condition we set \begin{align*} U_{im}&=u_0(x_i),\ i=0,\ldots,n \end{align*}
  2. For each value $k=m-1,m-2,\ldots,0$ we find $U_{ik},\ i=0,\ldots,n,\ k=m-1,\ldots,0$ by solving the system of equations \begin{align*} U_{0k}&=\phi_1(t_{k}),\\ a_{ik}U_{i-1,k}+b_{ik}U_{ik}+c_{ik}U_{i+1,k}&=d_{ik}U_{i-1,k+1}+e_{ik}U_{i,k+1}+f_{ik}U_{i+1,k+1},\ i=1,\ldots,n-1,\\ U_{nk}&= \phi_2(t_{k})\\ \end{align*} for the unknown values of $U_{ik},\ i=0,\ldots,n$. Here \begin{align*} a_{ik}&=-\frac{\alpha(x_i,t_k)\Delta t}{2\Delta x^2}+\frac{\beta(x_i,t_k)\Delta t}{4\Delta x},\\ b_{ik}&=1+\frac{\alpha(x_i,t_k)\Delta t}{\Delta x^2}+\frac{r}{2}\Delta t,\\ c_{ik}&=-\frac{\alpha(x_i,t_k)\Delta t}{2\Delta x^2}-\frac{\beta(x_i,t_k)\Delta t}{4\Delta x},\\ d_{ik}&=\frac{\alpha(x_i,t_{k+1})\Delta t}{2\Delta x^2}-\frac{\beta(x_i,t_{k+1})\Delta t}{4\Delta x},\\ e_{ik}&=1-\frac{\alpha(x_i,t_{k+1})\Delta t}{\Delta x^2}-\frac{r}{2}\Delta t,\\ f_{ik}&=\frac{\alpha(x_i,t_{k+1})\Delta t}{2\Delta x^2}+\frac{\beta(x_i,t_{k+1})\Delta t}{4\Delta x},\\ \end{align*}

Exercise 3

Implement a solver for CN method.Find approximately the value of the price of the option described in exercise 1 by using CN method for solving untransformed BS equation in the case $x_{min}=0$, $\rho=3$, $x_{max}=\rho S_0$, $m=50$, $n=120$, the exact boundary condition at $x=x_{min}$ and constant boundary condition at $x=x_{max}$.

In [ ]:
#Implement the solver and make sure that it works correctly. Ask for help if you get stuck!
#The code for this solver is not going to be shown in sample solutions!
Check your answer! The answer should be 55.787196014564955
If your answer was not correct ... 1. Think about the index you have to use to get the value corresponding to stock price S0. It is not n/2! 2. Print out the results you got for t=0 (that is, U[:,0]). The values should be as follows: array([199.00249584, 194.61360124, 190.22470663, 185.83581203, 181.44691743, 177.05802284, 172.66912842, 168.28023517, 163.89134841, 159.50248912, 155.11372382, 150.72522792, 146.33739819, 141.95102397, 137.56751527, 133.18917173, 128.81946506, 124.46330101, 120.12722772, 115.81956351, 111.55042733, 107.33166669, 103.17668823, 99.10020528, 95.11792178, 91.24617512, 87.50156056, 83.90055795, 80.45917856, 77.19264575, 74.11511938, 71.23946985, 68.57710419, 66.13784396, 63.92985218, 61.9596052 , 60.23190404, 58.74991958, 57.51526521, 56.52809135, 55.78719601, 55.29014645, 55.03340736, 55.01247177, 55.22199139, 55.65590377, 56.30755428, 57.16981117, 58.23517292, 59.49586681, 60.94393862, 62.57133309, 64.36996549, 66.33178432, 68.44882569, 70.71325983, 73.11743019, 75.65388588, 78.31540788, 81.09502968, 83.986053 , 86.98205899, 90.0769157 , 93.26478197, 96.54010858, 99.89763682, 103.3323949 , 106.83969276, 110.41511529, 114.05451447, 117.75400057, 121.50993268, 125.31890871, 129.17775505, 133.08351607, 137.03344346, 141.0249857 , 145.05577753, 149.12362967, 153.22651874, 157.36257744, 161.5300851 , 165.72745852, 169.95324314, 174.2061047 , 178.48482113, 182.78827493, 187.11544585, 191.46540403, 195.83730346, 200.23037574, 204.64392436, 209.07731913, 213.52999104, 218.00142746, 222.49116754, 226.99879796, 231.52394898, 236.06629067, 240.62552946, 245.20140487, 249.7936865 , 254.4021712 , 259.02668043, 263.66705787, 268.32316709, 272.99488947, 277.68212227, 282.38477676, 287.10277659, 291.83605627, 296.58455971, 301.34823887, 306.12705231, 310.92096409, 315.72994403, 320.55396791, 325.39300797, 330.24702016, 335.11605876, 340. ]) 3. If the firs/last value is not correct, then the boundary conditions were not defined correctly. The value at x=xmax should be p(xmax) for untransformed equation.

Practical homework 5 is again an VPL exercise in Moodle.