how can i create a loop for this pattern

Sorry for asking a seemingly dumb question however it has been doing my head in! I am simulating asset paths in a Monte-Carlo simulation and I am trying to get the 1 year return for each year separately stored in a variable rt. I tried to create a loop for the rt1-rt4 variables so I could do this for all 10 years that I am simulating however I've spent way too much time fiddling around!
I was hoping that someone could point me in the right direction. I've had similar problems like this before and am avoiding a brute force method.
Thank you for your time!
timesteps = 252;
years = 10;
draws = timesteps*years;
epsilon = randn(1,252);
sigma = 0.2;
for j = 1:draws
tsr(j) = exp(( - 0.5 * sigma^2) * ...
(1/timesteps) + epsilon(j) * sigma * ...
sqrt(1/timesteps));
end
% first year
yr1cumret = cumprod(tsr(1,1:timesteps));
yr1ret = yr1cumret(1,end);
rt1 = log(yr1ret);
% second year
yr2cumret = cumprod(tsr(1,timesteps+1:timesteps*2));
yr2ret = yr2cumret(1,end);
rt2 = log(yr2ret);
% third year
yr3cumret = cumprod(tsr(1,timesteps*2+1:timesteps*3));
yr3ret = yr3cumret(1,end);
rt3 = log(yr3ret);
% fourth year
yr4cumret = cumprod(tsr(1,timesteps*3+1:timesteps*4));
yr4ret = yr4cumret(1,end);
rt4 = log(yr4ret);
rt = [rt1, rt2, rt3, rt4];

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 29 Nov. 2017
Bearbeitet: Andrei Bobrov am 29 Nov. 2017

0 Stimmen

I'm corrected my code.
Maybe so?
ts = 252;
years = 10;
d = ts*years;
epsilon = randn(1,d); % !!!!!!!!! here fixed
sigma = 0.2;
f = 1/ts;
jj = reshape(1:d,ts,[]);
tsr = exp(-.5*sigma^2*f + epsilon(jj)*sigma*sqrt(f));
rt = reshape(log([tsr(1,:);prod(tsr)]),1,[]);

2 Kommentare

Andrei Bobrov
Andrei Bobrov am 29 Nov. 2017
Corrected.
H Flem
H Flem am 30 Nov. 2017
Thank you for that. for the rt variable it is returning a 1x20 vector.
Sorry for my ignorance but I am having trouble understanding what exactly is going on here.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 29 Nov. 2017

Kommentiert:

am 30 Nov. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by