Storing results from a for-loop in a vector of zeros

I have the code Backtesting_10day_II (here attached).
From line 23-26 I created 4 vectors (of zeros) to pre-allocate the results from the for-loop, each of these results is the value of a portfolio calculated every 10 days (V_min_var, V_eff_var, V_min_ES, V_eff_ES).
Now in the for loop: i goes from 1001 until 3721, there are 273 iterations (n= 3740 and i=1001:10:n-19), so thinking logically there should be only 274 results per portfolio (274 values per portfolio), which means that each pre-allocating vector should be of size 274x1.
After I run the program I checked the pre-allocating vectors and I found that they were size 3731x1, the majority of the numbers were zeros (zeros from row 1 to 1000,then actual values at rows 1001,1011,1021,1031,etc until 3731, but everything in between is zero). I don't understand this because I predefined these vectors to be of size 274x1.
Why do I have so many zeros in between my results? and how can I fix this? I only need the results not the zeros in between.
Thanks!

回答 (1 件)

Iain
Iain 2014 年 8 月 20 日

0 投票

Indexing in matlab is done from a base of one.
So,
A(3479) = 1;
will change the 3479th element of A to 1.
You need to change how you are indexing your variables with the iteration number, rather than the loop variable. - How you manage that is up to you.

10 件のコメント

civs
civs 2014 年 8 月 20 日
Can you guide me a little bit? I need the iteration to be every 10 values. This should in theory work, right?
How can I make a vector with only the results from the pre-allocation vector? I just want to get rid of the zeros.
Adam
Adam 2014 年 8 月 20 日
Simplest way is probably just to introduce a variable separate to your loop control as e.g.
counter = 1
for i= 1001:10:n-19
...
V_min_var( counter ) = ...
...
counter = counter + 1;
end
civs
civs 2014 年 8 月 20 日
I don't understand what "counter" does... is this just to get the results and not the zeros out from the pre-allocation vectors?
Adam
Adam 2014 年 8 月 20 日
counter is just the index into your pre-allocated array so that you fill it up contiguously rather than it ending up resizing itself upto 3731 mostly zero values.
civs
civs 2014 年 8 月 21 日
So "counter" will fill the pre-allocated vectors with the actual results (value of the portfolios) ONLY and not the zeros?... That's exactly what I need! Ok.. so would this work for example (for the first portfolio)?
counter= 1
for i= 1001:10:n-19 %from 1001 until 3721
% Determine weights of global min-var portfolio
wmin_var{i-1000}= mean_var_portopt2(-10, Rets (i-1000:i-1,:));
PR_min_var(i+10)= Rets(i+10,:)* wmin_var{i-1000};
% Compute tomorrow's portfolio value as
V_min_var(1001)=1000; %first value is fixed
V_min_var(counter)= V_min_var(i)*(PR_min_var(i+10)+1);
counter= counter+1
end
I guess I can do the same with to the other portfolios, right?
Adam
Adam 2014 年 8 月 21 日
Should work for all of them. Pre-allocation fills the vectors with zeros, this method just ensures that only the pre-allocated elements of the vector are then assigned values because if you call e.g.
myVector(1000) = someValue;
then myVector will be extended to size 1000 and padded with zeros if its previous size was < 1000.
civs
civs 2014 年 8 月 21 日
I just tried it, and I got this error:
Attempted to access V_min_var(1011); index out of bounds because numel(V_min_var)=1001.
Error in Backtesting_10day_II (line 40)
V_min_var(counter)= V_min_var(i)*(PR_min_var(i+10)+1);
I don't understand... I was afraid it will not work out... what can I do to fix it?
civs
civs 2014 年 8 月 21 日
Surely there must be an easier way to do this... I just want the results not the zeros and I know that the results are at row 1001, 1011, 1021, 1031, etc.. what if I do this?
Portfolio_1= V_min_var(1001:10:end)
Iain
Iain 2014 年 8 月 21 日
You are confusing "value" for "index".
You must always address vectors with an index. An index can be related to a value by a simple algorithm (eg (i-991)/10 ), or by finding a value's position in a list (find(x == 45,1))
civs
civs 2014 年 8 月 21 日
Ok, but how would it work for my problem here...? find(x==1001:10:3731)
I need a concrete answer. It doesn't need to be a sophisticated solution, just a simple solution will do.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangePortfolio Optimization and Asset Allocation についてさらに検索

質問済み:

2014 年 8 月 20 日

コメント済み:

2014 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by