0% found this document useful (0 votes)
29 views7 pages

Lab Ass 2 (By Me)

Matlab problems

Uploaded by

ahnaf rafid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

Lab Ass 2 (By Me)

Matlab problems

Uploaded by

ahnaf rafid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

%% TASK 1

%Wiener process paths


%Bpath brownianpath simulation for the interval [0,1]:

clc
clear all
randn('state',100) %set the state of randn

T=1;
N=500;
dt=T/N;
dW=sqrt(dt)*randn(1,N); %array of BM increments
W=cumsum(dW); %cumulatively summing up the increments
plot(0:dt:T,[0,W],'r-') % plot W against t
xlabel('t','FontSize',16)
ylabel('B(t)','FontSize',16,'Rotation',45)

%randn('state',100) -This line of code initializes the random number generators of


the randn function with a specific state value

%By initializing the random number generator with the same state value,we ensure
that the same sequence of random numbers is generated each time we run our program

%% TASK 2
% Euler-Maruyama

clc
clear all
randn('state',100)
T=1;
N=200;

dt=T/N;
dW=sqrt(dt)*randn(1,N); %array of BM increments
W=cumsum(dW);

mu=2;
sigma=1;
Xzero=1; %given X(0)=1

%Exact value
Xtrue=Xzero*exp((mu-0.5*sigma^2)*([dt:dt:T])+sigma*W);
figure
plot([0:dt:T],[Xzero,Xtrue],'m-')
hold on

% Euler-Maruyama (EM METHOD)


R=5;
Dt=R*dt;
L=N/R;
Xem=zeros(1,L);
Xtemp=Xzero;

% f(x)=mu*X and g(x)=sigma*X

for j=1:L
Winc=sum(dW(R*(j-1)+1:R*j));
Xtemp=Xtemp+mu*Xtemp*Dt+sigma*Xtemp*Winc;
Xem(j)=Xtemp;
end

plot([0:Dt:T],[Xzero,Xem],'r--*')
hold off
title('Geometric Brownian Motion Path')
legend('Exact','Euler-Maruyama')

error=abs(Xem(end)-Xtrue(end));
error1=abs(Xem(end/2)-Xtrue(end/2))
fprintf('at t=%2.1f error is=%5.6f\n',T,error)

%% TASK 3
% Arithmetic BM SDE

clc
clear all
randn('state',100)
T=1;
N=300;
dt=T/N;
dW=sqrt(dt)*randn(1,N); %array of BM increments
W=cumsum(dW);

mu=2;
sigma=1;
Xzero=1; %given X(0)=1

%Exact value
Xtrue=Xzero+mu*[dt:dt:T]+sigma*W;
figure
plot([0:dt:T],[Xzero,Xtrue],'m-')
hold on

% Euler-Maryama
R=4;
Dt=R*dt;
L=N/R;
Xem=zeros(1,L);
Xtemp=Xzero;

for j=1:L
Winc=sum(dW(R*(j-1)+1:R*j));
Xtemp=Xtemp+mu*Dt+sigma*Winc;
Xem(j)=Xtemp;
end

plot([0:Dt:T],[Xzero,Xem],'r--*')
hold off
title('Arithmatic Brownian Motion Path')
legend('Exact','Euler-Maruyama')

error=abs(Xem(end)-Xtrue(end));
fprintf('at t=%2.1f error is=%5.6f\n',T,error)

%% TASK 4
% Ornstein-Uhlenbeck SDE

clc
clear all
randn('state',100)
T=1;
N=2^8;

dt=T/N;
dW=sqrt(dt)*randn(1,N); %array of BM increments
W=cumsum(dW);

lambda=2;
sigma=1;
Xzero=1; %given X(0)=1

%Exact value
Xtrue=Xzero-lambda*[dt:dt:T]+sigma*W;
figure
plot([0:dt:T],[Xzero,Xtrue],'m-')
hold on

% Euler-Maryama
R=4;
Dt=R*dt;
L=N/R;
Xem=zeros(1,L); %preallocating for efficieny
Xtemp=Xzero;

for j=1:L
Winc=sum(dW(R*(j-1)+1:R*j));
Xtemp=Xtemp-lambda*Xtemp*Dt+sigma*Winc;
Xem(j)=Xtemp;
end

plot([0:Dt:T],[Xzero,Xem],'r--*')
hold off
title('Ornstein -Uhlenbeck SDE Path')
legend('Exact','Euler-Maruyama')

%% TASK 5
%Mean Reversion SDE

% NOT SURE JE HOISE NAKI

clc
clear all
randn('state',100)
T=1;
N=2^8;

dt=T/N;
dW=sqrt(dt)*randn(1,N); %array of BM increments

lamda=2;
sigma=1;
Xzero=1; %given X(0)=1

% Euler-Maruyama
R=4;
Dt=R*dt;
L=N/R;
Xem=zeros(1,L);
Xtemp=Xzero;
Xbar=3;

for j=1:L
Winc=sum(dW(R*(j-1)+1:R*j));
Xtemp=Xtemp-lamda*(Xtemp-Xbar)*Dt+sigma*sqrt(Xtemp)*Winc;
Xem(j)=Xtemp;
end
figure
plot([0:Dt:T],[Xzero,Xem],'r-*')
title('Mean Reversion with square root SDE Path')
legend('Euler-Maruyama')

%% TASK 6
% Milstein's Higher Order

clc
clear all
randn('state',100)

T=1; N=2^8; dt=T/N;


dW=sqrt(dt)*randn(1,N);
W=cumsum(dW);
mu=2;
sigma=1;
Xzero=1;

%Exact value:
Xtrue=Xzero*exp((mu-0.5*sigma^2)*([dt:dt:T])+sigma*W);
figure
plot([0:dt:T],[Xzero,Xtrue],'m-')
hold on

%Euler-Maruyama
R=4;
Dt=R*dt;
L=N/R;
XMM=zeros(1,L);
Xtemp=Xzero;

% f(x)=mu*X and g(x)=sigma*X and g'(x)=sigma

for j=1:L
Winc=sum(dW(R*(j-1)+1:R*j));
Xtemp=Xtemp+mu*Xtemp*Dt+sigma*Xtemp*Winc+(1/2)*sigma^2*Xtemp*(Winc^2 -Dt);
XMM(j)=Xtemp;
end

plot([0:Dt:T],[Xzero,XMM],'r--*')
hold on

% Comparing with EM
Xem=zeros(1,L);
Xtemp=Xzero;
for j=1:L
Winc=sum(dW(R*(j-1)+1:R*j));
Xtemp=Xtemp+mu*Xtemp*Dt+sigma*Xtemp*Winc;
Xem(j)=Xtemp;
end

plot([0:Dt:T],[Xzero,Xem],'k--')
hold off

title('GBM path')
legend('Exact','Milstein higher order','Euler Maruyama')

er1=abs(XMM(end)-Xtrue(end));
er2=abs(Xem(end)-Xtrue(end));

if er1<er2
disp('Milstein method gives more accurate result than EM method')
else
disp('EM method gives more accurate result than Milstein method')
end

You might also like