이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Store results from for loop
조회 수: 2 (최근 30일)
이전 댓글 표시
답변 (2개)
Azzi Abdelmalek
2014년 8월 5일
댓글 수: 14
Hikaru
2014년 8월 6일
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
2014년 8월 6일
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
2014년 8월 6일
편집: Michael Haderlein
2014년 8월 6일
Whatever you do in the loop will be overwritten in your last line. What are you going to to in this last line?
Hikaru
2014년 8월 6일
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
2014년 8월 6일
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
2014년 8월 6일
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
2014년 8월 6일
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...
Michael Haderlein
2014년 8월 6일
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
2014년 8월 6일
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
2014년 8월 6일
Just to get you an idea, I attached the file with the prices and the code (so far).
Michael Haderlein
2014년 8월 6일
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
2014년 8월 7일
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
2014년 8월 7일
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!
Iain
2014년 8월 7일
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
civs
2014년 8월 7일
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
2014년 8월 7일
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
2014년 8월 7일
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
2014년 8월 7일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Financial Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)