Generate correlated random variables from standard normal distribution
조회 수: 10 (최근 30일)
이전 댓글 표시
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,
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!