how can i create a loop for this pattern
Show older comments
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];
Answers (1)
Andrei Bobrov
on 29 Nov 2017
Edited: Andrei Bobrov
on 29 Nov 2017
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,[]);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!