Hi All,
I want Matlab to provide two matrices of same length (1*25). So basically I want the loop to only run an iteration if both conditions are satisfied.
If any of T6 or T7 is shorter that 6 observations, it should skip that particular iteration for both "estimates_2010" and "estimates_2011".
I have succeeded in previous loops but different variables with this procedure, however in this loop I am ending up with a 1*25 matrix for "alpha_2010" (which I should), but ending up with af 1*23 matrix for "alpha_2011" (which is wrong). T6, T7, Carhart_t6 and Carhart_t7 is of same length and are cell variables, lags is a fixed number on 2 which is necessary in the input function "nwest". I was certain, that the "|" was an or condition, so both variable condition needed to be satisfied in order to proceed.
I am trying to do the following:
for i = 1:size(Afkast,2) % Afkast is a 12*25 matrix
if length(T6{i}) <6 | length(T7{i}) <6 % I want my if condition to evaluate both variables, if either is shorter than 6, it should be skipped.
continue
end
estimates_2010 = nwest(T6{i},Carhart_t6{i},lags); estimates_2011 = nwest(T7{i},Carhart_t7{i},lags);
alpha_2010(:,i) =estimates_2010.beta(1); alpha_2011(:,i) = estimates_2011.beta(1);
end
I have done it for multiple different variables with same conditions, however there seem to be something wrong, maybe I am misunderstanding the "|" operator.

 Réponse acceptée

Rik
Rik le 10 Avr 2020

0 votes

If you use the debugger to step through your code line by line, you will have notice that your condition works (although I would suggest ||, because that skips the evaluation of the second part if the first is true).
The actual issue is with code like this:
alpha_2010(:,i) =estimates_2010.beta(1);
If you skipped one iteration because the lengths are incorrect, a line like this will fill the skipped line with zeros, unless you pre-allocated it before your loop:
k= ___ ;
alpha_2010 = NaN(k,size(Afkast,2));
for i = 1:size(Afkast,2)
You could also use a separate column counter to fill you columns consecutively.

8 commentaires

Anton Sørensen
Anton Sørensen le 10 Avr 2020
Modifié(e) : Anton Sørensen le 10 Avr 2020
Hi Rik,
Thanks for your feedback. It is actually OK, that it fills with zeros in that way I can crosscheck that it has skipped the same observations for both variables --> And that works for the previous variables. It is just very strange, that the alpha_2011 are missing two iterations compared to alpha_2010.
I have crosschecked all variables relevant for this loop and they are of same length/size.
Whether I use "|" or "||" yields the same results..
Iteration 24 and 25 should yield zero for both variables, but for alpha_2010 it yields an estimate on 0 for iteration 24 which is correct, but for iteration 25 T6 has 12 observations, while T7 has 5, so this iteration should be skipped, but for some reason it gives an estimate for alpha_2010 in iteration 25 and for alpha_2011 it doesn't give a zero or an estimate for iteration 24 and 25.
When I insert an "i" it also only runs 23 iterations, where it should run 25. That is very strange... If I set the condition to less than 5 it runs the 25 iterations. The variabel T6(25) theres is 12 observations, for the variable T7(25) there is 5 observations. So due to the second part of the restriction this iteration should be skipped. I don't know, why the less than 6 restriction gives a problem here?
Do you have any suggestions? I can provide the complete code, if that helps.
Thanks.
Rik
Rik le 10 Avr 2020
Have you tried using break points? That should show you what is happening in your code. That is the strategy I would use if you would provide the code and variables to be able to run your code.
Anton Sørensen
Anton Sørensen le 10 Avr 2020
I have not used breakpoints, but I have tried to run the code step-by-step to identify the problem.
I have attached the code and data and the function nwest that you need in your path to be able to run it.
Thanks a lot.
Anton Sørensen
Anton Sørensen le 10 Avr 2020
I have no clue, why the loop include an estimate of the 25th iteration for T6 and why the loop ignore the 24th and 25th iteration instead of including a "0" since the condition is not met...
Anton Sørensen
Anton Sørensen le 10 Avr 2020
I located the issue - but not completely fixed. Since there already is a variable called "alpha_2010" the length of the new alpha_2010 and alpha_2011 differs. But this is due to an unsolved issue, that I still wish to fix. I don't understand, why the last two iterations are not "continued" and therefore replacing the last to entries in "alpha_2010" with a 0? The question boil down to this issue: It seems like if the last iterations (in this case 2) are not meeting the conditions, then the loop will just ignore everything and not replace it with a 0. While this is smart in some contexts, I wish that all my matrices are of same length. Are there any way, where I can ensure, that it run all iterations to completion with the function "continue" or is this just a standard build-in for Matlab?
Thanks.
Rik
Rik le 10 Avr 2020
Use the debugger. If you run the code you shared, you will see that the last two iterations are skipped. For alpha_2010 that means it will remain unchanged, but for the other variable it means it is not expanded.
You should also avoid putting data in your variable names. alpha_2010 is a good name if you have only 2 or 3 years you are describing. But you have copied the same code over and over again. That is the job of a computer. Your job is to do the thinking. In this case that would mean changing everything to an array. That way it is easy to expand your code to several more years, or run only a single year.
I'll edit some of your code and post it.
Rik
Rik le 10 Avr 2020
The code below is much cleaner and easier to adapt to what you need than your original code. Whenever you find yourself using numbered variables you should consider using an array instead. You should also avoid long lines of code (keeping it under 100 chars wide improves readability). Another good practice is to write your comments in English so people you share your code with don't need to use Google translate.
The code below is a complete reproduction of what your Persistance script is doing. No need for clc,clear all, close all, as you don't print to the command window, or open any figures. clear all should be replaced by clear, for reasons the documentation explains.
%% Persistence tests foretages med udgangspunkt i alle geografiske områder
% Bemærk persistence foretages kun for Carharts 4-factor
S=load('aDanmark.mat');
aDanmark=table2array(S.aDanmark(:,2:end))';
S=load('bDanmark.mat');
bDanmark=table2array(S.Danmark(:,2:end));
rf = bDanmark(:,5); % Er den samme for alle foreninger
Afkast = aDanmark - rf; %Variablen gøres universel, så de forskellige investeringsuniverser bare skal loades ind respektivt, så koden vil være den samme.
mkt_d=+bDanmark(:,1)-rf; %Risikojusterede markedsafkast
alpha=ones(size(mkt_d(:,1))); %Der inkluderes intercept
carhart=[ alpha mkt_d (bDanmark(:,2:4))]; %De fire faktorer i Carharts model medtages.
lags = floor(4*(12/100).^(2/9)); % Da der for hver regression kun anvendes 12 observationer, bruges Newey-West (1994) plug-in procedure
N_years=size(Afkast,1)/12;
columns=size(Afkast,2);
Carhart_t=cell(N_years,columns);
T=cell(N_years,columns);
alpha=zeros(N_years,columns);
%generate the Carhart_t and T arrays
for i = 1:columns
for year=1:N_years
months=(1:12)+12*(year-1);%1:12 for year 1, 13:24 for year 2 etc
months=months(isnan(Afkast(months,i))==0);
% Hver periode sættes op, så foreningerne og dertilhørende faktorer matcher hinanden for hver periode
Carhart_t{year,i}=carhart(months,:);
T{year,i}=Afkast(months,i);
end
end
%calculate the alpha array
%%%replaces alpha_2005, alpha_2006, etc
for year=1:(N_years-1)
for i=1:columns
if numel(T{year,i}) <6 || numel(T{year+1,i}) <6 % Minimumskrav til antal obs for hver periode, er der i en af perioderne færre end 6 obs, kan foreningen ikke medtages
%mark alpha with NaN
alpha(year ,i)=NaN;
alpha(year+1,i)=NaN;
else
estimates_year = nwest(T{year ,i},Carhart_t{year ,i},lags);
estimates_year_p_1 = nwest(T{year+1,i},Carhart_t{year+1,i},lags);
alpha(year ,i)=estimates_year.beta(1);
alpha(year+1,i)=estimates_year_p_1.beta(1);
end
end
end
Anton Sørensen
Anton Sørensen le 10 Avr 2020
Much cleaner!
Thanks Rik, I think this will help me alot, since I need to do the same for 9 more variables.
Have a great easter!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by