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)}.$$
In the case of the basic implicit finite difference method we compute the values $U_{ik}$ as follows
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
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
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.
#use put and call functions defined before
Repeat the previous exercise in the case of boundary conditions derived from special solutions.
Solution
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.
Algorithm for CN method is as follows:
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}$.
#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!
Practical homework 5 is again an VPL exercise in Moodle.