Generate correlated random variables from standard normal distribution

Hi everyone,
I would appreciate any opinion about if the following codes are correct in order to create 2 paths for asset prices, using correlated random variables for standard normal distribution.
The Code1: Asset1Paths is refered to the the path for Asset 1 . The random variable for standard normal distribution which is used is called E. From the command window i take the result for E and i paste it in Code 2: Asset2Paths (is refered to the path for Asset2). Before i run Code1 i type randn('state',0) in the command window.
Code1
function [SPaths,Eprice]=Asset1Paths(~)
S01=40;
sigma1=0.2;
T=30/365;
NSteps=30;
NRepl=1;
irate=0.05;
mu1=exp(irate*(T/NSteps));
SPaths=zeros(NRepl,1+NSteps);
SPaths(:,1)=S01;
dt=T/NSteps;
nudt=(mu1-0.5*sigma1^2)*dt;
sidt=sigma1*sqrt(dt);
E=randn;
for i=1:NRepl
for j=1:NSteps
SPaths(i,j+1)=SPaths(i,j)*exp(nudt+sidt*E);
end
price=[SPaths'];
end
xlswrite('2STATES.xls',price,'S01','A1:A70');
Eprice=E
end
Code2
function [SPaths,H]=Asset2Paths(~)
S02=40;
sigma2=0.3;
T=30/365;
NSteps=30;
NRepl=1;
irate=0.05;
rho=0.5;
E=-0.4326;
mu2=exp(irate*(T/NSteps));
SPaths=zeros(NRepl,1+NSteps);
SPaths(:,1)=S02;
dt=T/NSteps;
nudt=(mu2-0.5*sigma2^2)*dt;
sidt=sigma2*sqrt(dt);
H=randn
Z=(rho*E)+(sqrt(1-rho^2))*H
for i=1:NRepl
for j=1:NSteps
SPaths(i,j+1)=SPaths(i,j)*exp(nudt+sidt*Z);
end
price=[SPaths'];
end
xlswrite('2STATES.xls',price,'S02','A1:A70');
end
Thanks for any answer,

Answers (0)

Asked:

on 20 Dec 2019

Edited:

on 20 Dec 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!