You are on page 1of 3

1; clear all; %---------r=0.1; %annualized risk free rate T=.

5; %no of years K=3; %strike S1=122; %current price for stock 1 S2=120; %current price for stock 2 v1=0.20; %annual volatility for stock 1 v2=0.25; %annual volatility for stock 2 n=50; %time steps rho=0; %correlation ExerciseType='a'; %Enter a for american and 'e' for European dividend1=.1; %dividened yield for 1 dividend2=.1; %dividened yield for 2 %---------nsteps=n; nsimulations=50000; CallPutFlag="c"; nsimulations=floor(nsimulations/2); nsimulations=nsimulations*2; if CallPutFlag=="c", z=1; else z=-1; end; smat=zeros(nsimulations,nsteps); CC=zeros(nsimulations,nsteps); %cash flow from continuation CE=zeros(nsimulations,nsteps); %cash flow from exercise EF=zeros(nsimulations,nsteps); %Excercise flag dt=T/(nsteps-1); %simulate stock 1 smat1=zeros(nsimulations,nsteps); smat1(:,1)=S1; drift1=((r-dividend1)-v1^2/2)*dt; vsqrdt1=v1*dt^0.5; randmat1=randn(nsimulations/2,nsteps-1); randmat2=-randmat1; randmats1=[randmat1;randmat2]; smat1(:,2:nsteps)=exp(drift1+vsqrdt1*randmats1); smat1=cumprod(smat1,2); %simulate stock 2 smat2=zeros(nsimulations,nsteps); smat2(:,1)=S2; drift2=((r-dividend2)-v2^2/2)*dt; vsqrdt2=v2*dt^0.5; randmat1=randn(nsimulations/2,nsteps-1); randmat2=-randmat1; randmats2=[randmat1;randmat2]; rhotmp=(1-rho^2)^0.5; smat2(:,2:nsteps)=exp(drift2+rho*vsqrdt2*randmats1 + rhotmp*vsqrdt2*randmats2); smat2=cumprod(smat2,2);

s1vec=smat1(:,nsteps); s2vec=smat2(:,nsteps); payoffvec=exp(-r*(nsteps-1)*dt)*max(z*(s1vec-s2vec-K),0); payoff_sum=sum(payoffvec); MCEurpeanPrice=payoff_sum/nsimulations

CC=smat*0; %cash flow from continuation CE=smat*0; EF=smat*0; %Excercise flag st=smat(:,nsteps); CE(:,nsteps)=max(z*(s1vec-s2vec-K),0); CC(:,nsteps)=CE(:,nsteps); EF(:,nsteps)=(CE(:,nsteps)>0); paramat=zeros(8,nsteps); %coeff of basis functions for k=nsteps-1:-1:2, st=smat(:,k); s1vec=smat1(:,k); s2vec=smat2(:,k); CE(:,k)=max(z*(s1vec-s2vec-K),0); %only the positive payoff points are input for regression idx=find(CE(:,k)>0); X1vec=smat1(idx,k); X2vec=smat2(idx,k); Yvec=CC(idx,k+1)*exp(-r*dt); %Regress discounted continuation value at next time step % to S and A variables at current time step regrmat=[ones(size(X1vec,1),1),X1vec,X1vec.^2,X2vec,X2vec.^2,X2vec.*X1ve c,X2vec.*X1vec.*X1vec,X2vec.*X2vec.*X1vec]; p=ols(Yvec,regrmat); CC(idx,k)=p(1)+p(2)*X1vec+p(3)*X1vec.^2; CC(idx,k)=CC(idx,k) + p(4)*X2vec+p(5)*X2vec.^2; CC(idx,k)=CC(idx,k) + p(6)*X2vec.*X1vec + p(7)*X2vec.*X1vec.*X1vec + p(8 )*X2vec.*X2vec.*X1vec; %If exercise value is more than continuation value %choose to exercise EF(idx,k)=CE(idx,k) > CC(idx,k); tmp=find(EF(:,k)); if tmp>0, EF(find(EF(:,k)),k+1:nsteps)=0; end paramat(:,k)=p; idx=find(EF(:,k) == 0); %don't store regressed value of CC for next use CC(idx,k)=CC(idx,k+1)*exp(-r*dt); idx=find(EF(:,k) == 1); CC(idx,k)=CE(idx,k); end

payoff_sum=0; for i=1:nsteps, idx=find(EF(:,i) == 1); st=smat(idx,i); s1vec=smat1(idx,i); s2vec=smat2(idx,i); payoffvec=exp(-r*(i-1)*dt)*max(z*(s1vec-s2vec-K),0); payoff_sum=payoff_sum+sum(payoffvec); end MCAmericanPrice=payoff_sum/nsimulations