% Compares European and American Call options by using an explicit method

% Parameters of the problem:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r=0.2;				% Interest rate
sigma=0.25;			% Volatility of the underlying
D=0.3;				% Continuous dividend yield
Nt=1600;			% Number of time steps
Ns=160;				% Number of asset price steps
Smax=20;			% Maximum asset price considered
Smin=0;				% Minimum asset price considered
T=1.;				% Maturation (expiry) of the contract
E=10;				% Exercise price of the underlying
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


dt=(T/Nt);			% Time step
ds=(Smax-Smin)/Ns;	% Price step
 
% Initializing the matrices of the option values: V is the European and Vam is the American option
V(1:Ns+1,1:Nt+1) = 0.0;
Vam(1:Ns+1,1:Nt+1) = 0.0;

% Discretization of asset (S) and tau (tau=T-t) variables
S=Smin+(0:Ns)*ds;
tau=(0:Nt)*dt;

% Initial conditions prescribed by the Call payoff at expiry: V(S,tau=0)=max(S-E,0)
V(1:Ns+1,1)=max(S-E,0);
Vam(1:Ns+1,1)=max(S-E,0);


% Boundary conditions prescribed by call options with dividends:
V(1,1:Nt+1)=0;                                          % V(0,t)=0
Vam(1,1:Nt+1)=0;  
V(Ns+1,1:Nt+1)=Smax*exp(-D*tau)-E*exp(-r*tau);          % V(S,t)=Se^(-D*(T-t))-Ee^(-r(T-t)) as S -> infininty.
Vam(Ns+1,1:Nt+1)=Smax*exp(-D*tau)-E*exp(-r*tau);

% Implementing the explicit algorithm
for j=1:Nt              % Time  loop
    for n=2:Ns          % Asset loop
        V(n,j+1)=0.5*dt*(sigma*sigma*n*n-(r-D)*n)*V(n-1,j)+(1-dt*(sigma*sigma*n*n+r))*V(n,j)+0.5*dt*(sigma*sigma*n*n+(r-D)*n)*V(n+1,j);
        Vam(n,j+1)=0.5*dt*(sigma*sigma*n*n-(r-D)*n)*Vam(n-1,j)+(1-dt*(sigma*sigma*n*n+r))*Vam(n,j)+0.5*dt*(sigma*sigma*n*n+(r-D)*n)*Vam(n+1,j);
    end
    Vam(1:Ns+1,j+1)=max(Vam(1:Ns+1,j+1),Vam(1:Ns+1,1));  % Checks if early exercise is better for the American Option
end

% Compares the values of the European (dashed lines) and American (solid lines) Calls, V(S,tau), 
% as a function of S at three different times: tau=0 (t=T), tau=T/2 (t=T/2) and tau=T (t=0). 
figure(1)
plot(S,V(:,1),'r--',S,Vam(:,1),'r-',S,V(:,round(Nt/2)),'g--',S,Vam(:,round(Nt/2)),'g-',S,V(:,Nt+1),'b--',S,Vam(:,Nt+1),'b-');
xlabel('S');
ylabel('V(S,tau)');
title('European (dashed lines) and American (solid lines) Call Options');











