Store results from for loop

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 5 Août 2014

0 votes

14 commentaires

civs
civs le 6 Août 2014
This is not really helping
Hikaru
Hikaru le 6 Août 2014
It is. The articles listed in the link are all the basic concepts you'll need to do this.
Plus, we can't really help you specifically without more specifics from you.
civs
civs le 6 Août 2014
well this is what I have :
% i represents the day number in the loop
for i= 1001:n
mu_p= 0.01;
% Determine weights of global min-var portfolio
wmin_var{i-1000}= mean_var_portopt2(-10, Rets(i-1000:i-1,:));
end
% Store the weights at every iteration
wmin_var=cell(2740,5);
somehow it doesn't store the weights... wmin_var is all empty. How can I fix this?
Michael Haderlein
Michael Haderlein le 6 Août 2014
Modifié(e) : Michael Haderlein le 6 Août 2014
Whatever you do in the loop will be overwritten in your last line. What are you going to to in this last line?
Hikaru
Hikaru le 6 Août 2014
Like Michael said, the line
wmin_var=cell(2740,5);
at the end will overwrite whatever you have in the loop.
If mu_p is constant, you should put it before the for loop. Code will still work, just a good programming practice to put it outside.
I can't really help much without knowing the dimension of your other arrays, but this is an example of storing data from every iteration.
wmin_var=cell(2740,5);
a = size(wmin_var);
count = 0; % you don't really need this, it's here only for an example
for i=1:a(1)
for j=1:a(2)
wmin_var{i,j} = count + j; % whatever your formula is
% to calculate wmin_var
count = count + 1;
end
end
civs
civs le 6 Août 2014
Your are right the result of the loop is overwritten by the last line. What I want is store the result of the loop.. I will paste the actual code below:
[filename,pathname]=uigetfile('*.xlsx');
[data,textdata,raw] = xlsread(filename,'Portfolio');
Rets=data(1:1000,[2,3,4,5,6]); % Returns for the period from day1-day1000
Beta= 0.99;
mu_p= 0.01; % mu_p is the minimum return demanded by the investor
disp('********************************************************')
% Calculate the initial weights of every portfolio at day 1001
% Every set of weights is a vector 5x1
wmin_var= mean_var_portopt2(-10, Rets);
W_var= mean_var_portopt2(mu_p, Rets);
wmin_ES= mean_ES_portopt1(-10, Rets, Beta);
W_ES= mean_ES_portopt1(mu_p, Rets, Beta);
% Invest amount 1000 USD in initial portfolio according to the weights
V=1000;
V1 = V * wmin_var;
V2 = V * W_var;
V3 = V * wmin_ES;
V4 = V * W_ES;
% Tracking portfolio value using moving-window time frame starting at day 1001
[filename,pathname]=uigetfile('*.xlsx');
[data,textdata,raw] = xlsread(filename,'Portfolio');
Prices=data(:,[2,3,4,5,6]); % All prices for the period
n= length(Prices);
% i represents the day number in the loop
for i= 1001:n
% Tracking value of global min-var portfolio
port_glo_min_var(i)= Prices(i-1000:i-1).* wmin_var';
% Tracking value of min-variance efficient portfolio
port_eff_min_var(i)= Prices(i-1000:i-1).* W_var';
% Tracking value of global min-ES portfolio
port_glo_min_ES(i)= Prices(i-1000:i-1).* wmin_ES';
% Tracking value of min-ES efficient portfolio
port_eff_mean_ES(i)= Prices(i-1000:i-1,:).* W_ES';
end
The loop will calculate the market value of every portfolio in every iteration. So how do I store the results of the loop for the four different portfolio values in the code? What I want is to store the results of port_glo_min_var, port_eff_min_var, etc.. each in a different matrix.
Michael Haderlein
Michael Haderlein le 6 Août 2014
So, when I scroll through your code, some questions arise:
- In the equation, you use the Prices(i-1000:i-1). As you let i run from 1001 to length(Prices), I wonder if that's really what you want. It makes sense, if length(Prices)>1001. But then, you use only the Prices(1:length(Prices)-1000).
- Also, you write port_xx_yy_zz(i), thus, you will have 1000 zeros and only write numbers in the 1001 and later indices. Also looks as if that's not what you want.
- Third, Prices is a matrix (at least the line Prices=data(:,[2,3,4,5,6]); looks this way). When you do your calculations in the loop, you rearrange it as a vector. To see what I mean, please run "a=magic(3), a(1:5)". I guess you want it to be a vector, but most likely a vector of another kind.
- When you want to store a line vector in a matrix, you write port_xx_yy_zz(i,:)=...
Hope that these points will help you debugging your program.
Best regards,
Michael
civs
civs le 6 Août 2014
I modified it slightly to :
% Tracking value of global min-var portfolio
port_glo_min_var(i)= Prices(i-1000:i-1,:).* wmin_var';
My idea is to calculate the value of the portfolio as a moving window. That is, the window has the data for 1000 days. I start calculating on day 1001, based on the data for day1-day1000. Then on day 1002, the market value of the portfolio is based on the prices from day2-day1001, then on day 1003, the market value of the portfolio is based on prices from day3-day1002, etc...
Now we can only guess if there's further help required and if so, what it should be. My guess is that you get the error message
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Then please see the last point of my previous comment. If not, please provide further details.
civs
civs le 6 Août 2014
I get this error:
Error using .* Matrix dimensions must agree.
Error in Backtesting2 (line 51) port_glo_min_var(i)= Prices(i-1000:i-1,:).* wmin_var';
To be honest I am not sure about the indexing in the loop. I just want to create a moving window with the prices using 1000 days, and the prices in this window should be multiplied by the weight. This would be the market value of the portfolio.
civs
civs le 6 Août 2014
Just to get you an idea, I attached the file with the prices and the code (so far).
Michael Haderlein
Michael Haderlein le 6 Août 2014
The very unclear variable is wmin_var (and the similar ones). As you use it, it must be of size (5x1000) (you transpose it, so it shouldn't be a scalar and you use element-wise multiplication, so its transpose must be equal sized to Prices(i-1000:i-1,:)) Also, the result of this multiplication will be a matrix (1000x5). Is that what you want? I guess you want to multiply something else, but I don't know what.
civs
civs le 7 Août 2014
Oh ok, I didn't know both matrices (prices and weights) had to be of the same size for element wise multiplication.
I have modified the code a bit... I will attach it here.
civs
civs le 7 Août 2014
Ok guys, so here is the code (sorry! it took me long to post because I made more changes). It takes quite a long time to run though. Anyway my question is...
how can I store the values of V_min_var, V_eff_var, V_min_ES, V_eff_ES without overwriting the results of the loop?
Thanks!

Connectez-vous pour commenter.

Iain
Iain le 7 Août 2014

0 votes

what you want to do is to store your output like this:
for i = 1:X
...
result_V_min_var(i) = V_min_var;
V_eff_var_list(i) = V_eff_var_list;
... etc
end
Whatever you use as the loop number, needs to NOT change within the loop.

4 commentaires

civs
civs le 7 Août 2014
Would this work?
for i= 1001:n
% Determine weights of global min-var portfolio
wmin_var{i-1000}= mean_var_portopt2(-10, Rets (i-1000:i-1,:));
PR_min_var(i+1)= Rets(i+1,:)* wmin_var{i-1000};
% Compute tomorrow's portfolio value as
V_min_var(1001)=1000; % first value is fixed
V_min_var(i+1)= V_min_var(i)*(PR_min_var(i+1)+1);
end
% Store the portfolio value (the last result in the loop)
result_Vmin_var(i)= V_min_var;
Thanks!
Iain
Iain le 7 Août 2014
previous_value = 1000;
list_of_values(1000) = previous_value ;
for i = 1001:n
....
new_value = previous_value * (PR_min_var(i+1)+1); % At the end of the loop, "new_value" is the final value.
list_of_values(i) = new_value; % this is a list of ALL the values.
previous_value = new_value; % - so new value can change and we keep the last answer available
end
civs
civs le 7 Août 2014
I am not sure I fully understand this code. What I want is to store all the portfolio values from the loop so wherever I store it will be a vector of portfolio values. So I have 4 vectors in the end, one for each portfolio.
civs
civs le 7 Août 2014
is the below a vector of portfolio values?
list_of_values(i) = new_value; % this is a list of ALL the values.
I attached the code. Line 59-62 those are the vectors I am referring to, these are supposed to be column vectors. I am not sure if this will work though...
The idea is to store all the values at every iteration.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Portfolio Optimization and Asset Allocation dans Centre d'aide et File Exchange

Question posée :

le 5 Août 2014

Commenté :

le 7 Août 2014

Community Treasure Hunt

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

Start Hunting!

Translated by