Effacer les filtres
Effacer les filtres

For loop over different dimensions

10 vues (au cours des 30 derniers jours)
Anton Sørensen
Anton Sørensen le 19 Fév 2020
Commenté : Anton Sørensen le 22 Fév 2020
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 commentaires
darova
darova le 19 Fév 2020
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 le 19 Fév 2020
Modifié(e) : Anton Sørensen le 19 Fév 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 :)

Connectez-vous pour commenter.

Réponse acceptée

darova
darova le 19 Fév 2020
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 commentaires
darova
darova le 22 Fév 2020
Happy to help!
Anton Sørensen
Anton Sørensen le 22 Fév 2020
:)
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..

Connectez-vous pour commenter.

Plus de réponses (1)

Anton Sørensen
Anton Sørensen le 21 Fév 2020
Modifié(e) : Anton Sørensen le 21 Fév 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.

Catégories

En savoir plus sur Portfolio Optimization and Asset Allocation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by