How to allocate an output of 2 rows in a loop?

2 ビュー (過去 30 日間)
Antonio  Amoretti
Antonio Amoretti 2016 年 1 月 10 日
コメント済み: Star Strider 2016 年 1 月 11 日
Hi everybody,
I am trying to create a loop using the function mvregress. I need to estimate coefficients, as well as the other outputs, with a rolling window of 60 months. I faced a problem with the allocation in the empty matrix, since the output "b" is a 2x1 vector. If I have, for instance, 203 months of observations for 43 stocks and I want to start with a time window of 60 months. I would do the following:
w=60;
CAPM_data=CAPM_Factors_data; %Import CAPM data
STOXX_M_EXRet= STOXX_M_Ret- repmat(CAPM_data(:,2),1,size(STOXX_M_Ret,2)); %Create Asset Excess returns
[T,n] = size(STOXX_M_EXRet);
XCAPM=[ones(T,1) , CAPM_data(:,1)]; %Create design matrix adding ones vectors to include constant
CAPMParameters=zeros(T-w,n);
for j=w:T
for i=1:n
[CAPMParameters(j,i)] = mvregress(STOXX_M_EXRet(j+1-w:j,:),XCAPM(j+1-w:j,:));
end;
end;
When I run this loop, I get "Subscripted assignment dimension mismatch". This is given by the fact that it's trying to allocate a 2x1 vector (constant and beta) in a single cell.
Could someone please help me? I would like to obtain a time series of all the values of the output (for example, a matrix with 286 rows (each2 rows the parameters at time t)).
Thank you very much

採用された回答

Star Strider
Star Strider 2016 年 1 月 10 日
You’re using the word ‘cell’ (apparently to mean matrix element), while you would likely best use actual cell addressing.
See if changing your mvregress call line to:
CAPMParameters{j,i} = mvregress(STOXX_M_EXRet(j+1-w:j,:),XCAPM(j+1-w:j,:));
does what you want. Note that I changed the parentheses ‘()’ to curly braces ‘{}’ denoting a cell.
To clarify, the values in a vector or matrix are referred to as ‘elements’. A cell is a particular MATLAB data type (see the documentation on Cell Arrays for details) and using ‘cell’ to mean ‘element’ can lead to confusion here on MATLAB Answers. Please understand that’s a clarification, not a criticism!
  2 件のコメント
Antonio  Amoretti
Antonio Amoretti 2016 年 1 月 11 日
Thank you, I first allocate with CAPMParameters=cell(T-w,n), then I ran the loop with the different brackets. It finally worked. Can I ask you the difference between the function mvregress and mvnrmle? Which one could be better to analyse a factor model like CAPM/Fama-French?
Thank you
Star Strider
Star Strider 2016 年 1 月 11 日
My pleasure.
I have the Statistics Toolbox. I don’t have the Financial Toolbox, so have no experience with mvnrmle. Looking at the online documentation however, they seem to be essentially the same with respect to their estimation algorithms, since according to the documentaiton:
  • mvregress treats NaN values in X as missing values, and ignores rows in X with missing values.
I will defer to someone with experience with the Financial Toolbox, but to me the functions seem to be equivalent. I am not familiar with the factor model you are referring to.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by