Create a loop to find Beta for each period, OLS

1 回表示 (過去 30 日間)
userch9
userch9 2018 年 11 月 29 日
コメント済み: the cyclist 2018 年 11 月 29 日
Hi,
I'm computing OLS for a work and I'd like to know how to create a vector of a Beta for every period in order to do a plot of the Beta over time.
I think that we have to create a loop to compute a beta for each period (i=1:2826) but it doesn't work...
Here is my draft if you want to understand what I mean :
Y1=Total_R;
XBeta=[Oil_R];
TitleBeta={'Oil return'};
for i=1:2826
OLSBeta=LinearModel.fit(XBeta,Y1,'Intercept',true);
display(OLSBeta.Coefficients(2,1))
end
But when I do this, I only receive the Beta for the entire period..
Thank you for your answer

回答 (1 件)

the cyclist
the cyclist 2018 年 11 月 29 日
The first thing I notice is that absolutely nothing inside of your for loop depends on i. So, the output of your loop will be identical for every iteration.
  2 件のコメント
userch9
userch9 2018 年 11 月 29 日
Thank you for your quick answer (I'm a bit lost in loops..)
Is that better?
Y1=Total_R;
XBeta=[Oil_R];
TitleBeta={'Oil return'};
for i=1:2826
while i<2826
OLSBeta=LinearModel.fit(XBeta(i),Y1(i),'Intercept',true);
display(OLSBeta.Coefficients(2,1))
end
end
Thank you!
the cyclist
the cyclist 2018 年 11 月 29 日
You shouldn't need that inner while loop at all. Just the single loop over your indexing variable i.
What size are Total_R and Oil_R?
Better yet, upload a MAT file with those variables.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by