Problem with a mean in a for loop

6 views (last 30 days)
Hi,
I'm a student, here's my problem:
I have to create a matrix "P" with N = 100 rows and T = 25 columns which describes the value of a portfolio for 25 years. Every year the spending of the portfolio holder should be the arithmetic mean of the last three years portfolio's value. This is inside a for loop. Below i show you my codes.
mu = 0.07;
sigma = .15;
N = 100; % Number of simulation
T = 25; % years
% Simulation of expected returns
randn('state',10);
r = [ones(N,1)*NaN, mu + randn(N,T)*sigma];
% Matrix containing portfolio values and spending values
P = NaN*ones(N,T+1); % Portfolio Matrix
P(1:N,1) = 2000; % Initial Value of Portfolio
E = NaN*ones(N,T+1); % Expenditure Matrix
spendingrate = 0.05;
The problem is here:
for t = 1:T
if t < 3
P(1:N,t+1) = max(0,P(1:N,t).*exp(r(1:N,t+1)) - spendingrate*P(1:N,t));
E(1:N,t+1) = min(P(1:N,t).*exp(r(1:N,t+1)), spendingrate*P(1:N,t+1));
else t > 3
P(1:N,t+1) = max(0,P(1:N,t).*exp(r(1:N,t+1)) - spendingrate*(mean(P(1:N,t:t-2),2)));
E(1:N,t+1) = min(P(1:N,t).*exp(r(1:N,t+1)), spendingrate*(mean(P(1:N,t:t-2),2)));
end
end
I'm not able to make it run, calculating for each year, the mean of Portfolio values of the past three years. This not allow me to calculate the Value of the portfolio. Maybe is because the values of which I need to make the mean are generated in the same loop. Can you help me to fix this?

Accepted Answer

Matt Tearle
Matt Tearle on 26 Oct 2012
Edited: Matt Tearle on 26 Oct 2012
A few minor things in your code:
  • Use rng instead of randn('state') [ not important ]
  • Use NaN(m,n) instead of NaN*ones(m,n) [ not important ]
  • For readability (arguable!), you can use A(:,k) instead of A(1:N,k)
  • For efficiency, two for-loops is probably better than one with an if statement [ not a big deal unless T is large ]
  • else should not be followed by a logical condition. Use elseif if necessary, but in this case, you actually want the second condition to execute when t >= 3, so just use else [ important ]
  • Indexing with t:t-2 will not work because t > t-2, so t:t-2 results in an empty array (try it at the Command prompt.) Use t-2:t instead. [ critically important ]
Here's your code with all that implemented:
mu = 0.07;
sigma = .15;
N = 100; % Number of simulation
T = 25; % years
% Simulation of expected returns
rng(10);
r = [NaN(N,1), mu + randn(N,T)*sigma];
% Matrix containing portfolio values and spending values
P = NaN(N,T+1); % Portfolio Matrix
P(:,1) = 2000; % Initial Value of Portfolio
E = NaN(N,T+1); % Expenditure Matrix
spendingrate = 0.05;
for t = 1:2
P(:,t+1) = max(0,P(:,t).*exp(r(:,t+1)) - spendingrate*P(:,t));
E(:,t+1) = min(P(:,t).*exp(r(:,t+1)), spendingrate*P(:,t+1));
end
for t = 3:T
P(:,t+1) = max(0,P(:,t).*exp(r(:,t+1)) - spendingrate*mean(P(:,t-2:t),2));
E(:,t+1) = min(P(:,t).*exp(r(:,t+1)), spendingrate*mean(P(:,t-2:t),2));
end
(I have no idea if the results are correct. But it runs.)
You could also lose the first column of r (and, of course, change your indexing later from t+1 to t), but maybe you'd prefer to have the initial column of NaNs so all the matrices match up with same number of columns.

More Answers (1)

Fabrizio Marinelli
Fabrizio Marinelli on 26 Oct 2012
Thank you!

Categories

Find more on Portfolio Optimization and Asset Allocation 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!