August 2nd, 2012, 6:01 pm
Below is my Matlab code for pricing down-and-out call options by explicit finite difference. I transformed two variables, which are x = ln(S) and tau = T-t = time to expiry. The upper boundary condition is u(i+1,1)=exp(-r*tau)*u(i,1) and the lower boundary condition is u(i,Nx) = 0 for all i. Can anyone have a look at my code and let me know why the price produced by this code is way off the analytical value? Thanks!function price = test_SingleBarrier_EFD(H,K,Texp,So,r,sig,Nt,NX)% Pricing down-and-out call options by the explicit finite difference% method.% H : barrier% K : strike price% Texp : time to expiry% So : spot price% r : risk-free rate% q : dividend yield (not included yet)% sig : volatility% Nt : number of time partitions% NX : number of variable X partitions% time griddt = Texp / (Nt - 1);t = (0 : Nt - 1) * dt;% X grid Xmin = log(H);Xmax = log(3 * K); % a dummy upper barrierdX = (Xmax - Xmin) / (NX - 1);X = Xmax : -dX : Xmin;% check stability condition: dt/dX^2 < 1instable = (dt / dX ^ 2 > 1);if instable disp('need more time steps.');end% calculate coefficientsalpha = dt / dX ^ 2;a_ = -0.5 * alpha * dX * (r - sig ^ 2 / 2) + 0.5 * sig ^ 2 * alpha;b_ = 1 - sig ^ 2 * alpha - r * dt;c_ = 0.5 * alpha * dX * (r - sig ^ 2 / 2) + 0.5 * sig ^ 2 * alpha;a = repmat(a_, 1, NX - 2);b = repmat(b_, 1, NX - 2);c = repmat(c_, 1, NX - 2);A = diag([a, 0], -1) + diag([exp(-r * dt), b, 0]) + diag([0, c], 1);Vold = [max(exp(X(1 : NX - 1)) - K, 0)'; 0];Vnew = A * Vold;for i = 2 : Nt - 1 Vold = Vnew; Vnew = A * Vold;end% optVal = Vnew * exp(-r * Texp);optVal = Vnew;logS = log(So);j = (Xmax - logS) / dX;j1 = 1 + ceil(j); % index of the lower pricej2 = 1 + floor(j); % index of the higher priceprice = ((X(j2) - logS) / (X(j2) - X(j1))) * optVal(j1) + ((logS - X(j1)) / (X(j2) - X(j1))) * optVal(j2);% -------------- Results -----------------% % test_SingleBarrier_EFD(90,100,1,95,.1,.25,10000,100)% ans = 2.4881 Analytical value for this down-and-out call option = 5.9968