How to allocate an output of 2 rows in a loop?
1 view (last 30 days)
Show older comments
Antonio Amoretti
on 10 Jan 2016
Commented: Star Strider
on 11 Jan 2016
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
0 Comments
Accepted Answer
Star Strider
on 10 Jan 2016
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 Comments
Star Strider
on 11 Jan 2016
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!