MATLAB: How to allocate an output of 2 rows in a loop

allocatecapmloopregressiontime series

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 2×1 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 2×1 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

Best Answer

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!