For loop over different dimensions

I have to loop over i portfolios with different dimensions, in order to obtain OLS estimates
My length of the portfolios can be anything between 60 to 180.
For now it is a fairly simple loop, if I just have a fixed length on say 180, however I can't find a solution, when I have to extend the loop and allow for different dimensions.
I am thinking I need to put a restriction on both my X matrix (Explanatory variables) and Y matrix (Portfolios), so the loop will switch to the different dimensions depending on which i'th portfolio it loops over.
Here is the loop now:
for i = 1:size(portfolios,2)
X=[alfa factor]; % My matrix containing explanatory variables
y=portfolios(:,i); % Between 60 and 180 observations
results=ols(y,X); % Functionen constructing a struct with prefered OLS estimates
estimates(:,i)=results.beta;
tstat(:,i)=results.tstat(1);
residuals=results.resid;
end
Thanks in advance.

2 Comments

I don't understand the question
X=[alfa factor]; % My matrix containing explanatory variables
alfa and factor are of different sizes? Are you trying to concantenate them?
Anton Sørensen
Anton Sørensen on 19 Feb 2020
Edited: Anton Sørensen on 19 Feb 2020
No.
I have i portfolios of different lenghts, between 60 and 180 observations.
Then I have my explanatory variables (factors), they are 180 observations long.
Say, I have a portfolio that is 60 observations and one that is 100 observations.
I then want the loop to be able to first run an OLS regression that use the 60 observations for the first portfolio and regress those on the first 60 observations in my "factors" matrix and after that, the loop should be able to do that for the portfolio that is 100 long for the first 100 observations, and so on.
I hope it makes sense :)

Sign in to comment.

 Accepted Answer

What about filling with NaN?
y = portfolios(:,i); % Between 60 and 180 observation
if length(portfolios(:,i)) < 180
y(end+1:180) = nan;
end
results = ols(y,X); % Functionen constructing a struct with prefered OLS estimates

20 Comments

Anton Sørensen
Anton Sørensen on 20 Feb 2020
Edited: Anton Sørensen on 20 Feb 2020
I thought of that solution, however both the function and the manual OLS regression will provide estimates called "NaN".
I don't know if it is possible, but if I could set a restriction/some kind of dependence in the loop, that will take the length of portfolio i and then compare it to the same length of the factors? I am almost certain that would fix the problem, since the first return observation of portfolio i obviously match the first observation of the factors.
Thanks in advance.
Anton Sørensen
Anton Sørensen on 21 Feb 2020
Edited: Anton Sørensen on 21 Feb 2020
Do you have a suggestion on how to resolve that?
So if a portfolio is shorter than the max length, it will take the length(amount of observations) of that portfolio and regress those observations on factors equal to the length of the portfolio?
Example: The first portfolio has 87 observations, then this portfolio will be regressed on the 87 first observations from my factor matrix and on the same time do it for the next portfolio that has 101 observations (regressing on the first 101 factor observations), making the loop dynamic.
In that way I can get estimates for all portfolios.
Thanks in advance.
But later you adding this data to larger matrix. Matrix can't change its size
results=ols(y,X); % Functionen constructing a struct with prefered OLS estimates
estimates(:,i)=results.beta;
Is it correct?
Anton Sørensen
Anton Sørensen on 21 Feb 2020
Edited: Anton Sørensen on 21 Feb 2020
Not entirely sure I understand the question.
The "ols" function simply makes a struct of desired estimates from an OLS regression.
Estimates (:,i) will simply store an estimate from the previous ols function.
Example: estimates(:,i)=results.beta; will simply store the estimated beta coefficients from my regression. In this particular regression I have 4 different beta coefficients and that will be constant for each portfolio(So for each portfolio I will have 4 different beta coefficients). So in essence I just need to be able to match different portfolio sizes with the same size in factors.
Let me know if you need more information.
Thanks in advance.
I want my "X matrix" to be dynamic and adjust to different lenghts of all portfolios.
Is that not possible?
What about this?
y=portfolios(:,i); % Between 60 and 180 observations
n = length(y,1);
results=ols(y,X(1:n,:)); % Functionen constructing a struct with prefered OLS estimates
Anton Sørensen
Anton Sørensen on 21 Feb 2020
Edited: Anton Sørensen on 21 Feb 2020
Yes, that might do the trick.
One question. Why use length(y,1) instead of length(y,i)? Maybe I am thinking about it all wrong?
Thanks in advance.
just n = length(y). Without "1"
Okay, then it will take the length of each portfolio and compare to similar length of the X matrix? Is that correct understood?
Thanks.
Anton Sørensen
Anton Sørensen on 21 Feb 2020
Edited: Anton Sørensen on 21 Feb 2020
Since I want to load in my portfolios (which are of different sizes) is there anyway to circumvent this in order to get a matrix that enables me to do the procedure you just helped me with? I am certain I will get the error message "Horzcat" if I try to load in the excel file.
Appreciate the help!
Maybe you can make some scheme or simple drawing? Because im lost
Can you attach your whole code you use now? And data
Hi again,
Yes, be aware the portfolios I am testing on in the Matlab code is in same matrix and this loop works. AND the data attached to that code is called "Factirs" and "Portfolios".
However, in my thesis I am using data, where data is as described in this question I have posted here.
My dataset is not finished yet, but I have attached an example on how it might look.
So in the code, I need to allow for the data I attached, called "example data". The portfolio data is in "ark1" and the factors are in "ark2" so you can see how it looks.
Hope it makes sense, otherwise I will collect all my data and form my dataset within the next week.
I'm sorry but can't handle it. See my previous post
y=portfolios(:,i); % Between 60 and 180 observations
n = length(y);
results=ols(y,X(1:n,:)); % Functionen constructing a struct with prefered OLS estimates
It's the only idea i have
Hm, okay.
I also think that might work, if you have some kind of solution on how to load data, that is of different matrices? I can order the data, so from row 1:174 are all the portfolios and from 175-178 is the factors. I just need to avoid the horzcat error and then the loop you suggested should work.
Thanks for your help so far, appreciate it.
Or instead of looping over the loaded matrix I just loop straight over the dataset, I don't know if that is the prefered method?
Try to specify rows you want to fill
estimates(1:n,i)=results.beta;
tstat(1:n,i)=results.tstat(1);
residuals=results.resid;
Thanks. I have accepted the answer, but I still have a problem.
I will try to post a new thread, when I have assembled my data and I can show you the specific problem in the code. I hope you will see the thread, darova. :)
Appreciate the help.
Happy to help!
:)
If you can think of a way to load/import data of different dimensions without adding NaN to non filled entries, or you can loop directly without importing first, that would be of great help! I am struggling to see a solution, since my matrix is in different lengths. But, as I told you previously I will later provide my whole dataset and code for you to see, in that way the problem should be obvious, I know it is not easy to see it just from words..

Sign in to comment.

More Answers (1)

Anton Sørensen
Anton Sørensen on 21 Feb 2020
Edited: Anton Sørensen on 21 Feb 2020
Hi again,
So as I have tried to explain. My Y(portfolios) have between 60-180 observations and I need somehow to loop over all of them in order to regress those observations on my X factors. And my X factors therefore need to be adjusted to the same length as the different portfolios. If I can do that, I can run my function 'ols' and then obtain my estimates.
Does that make sense?
Thanks.

Categories

Community Treasure Hunt

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

Start Hunting!