I have a matrix of stock returns, 4280 (rows) by 7379 (columns).
I need to regress the 1st to 15th cell in column 3 (1,3) against the 1st to 15th cells in columns 1 and 2 (1,1), (1,2).
Then the 2nd to 16th cell in column 3 (2,3) against the 2nd to 16th cells in columns 1 and 2 (2,1) and (2,2) etc. So like a rolling window.
The columns remain constant, but each variable window (i:i+14) goes down by one row.
Once the whole column is done, it needs to go back up to the 4th column, but continue regressing against columns 1 and 2. Then the 5th column against columns 1 and 2, etc.
This is the code I have so far:
Nwin = 15; % set window for rolling regression
betasMkt = ret.*NaN; betasMkt(:,1) = ret (:,1); % initialize empty matrix for Market betas
betasDisp = ret.*NaN; betasDisp(:,1) = ret (:,1); % initialize empty matrix for CSAD betas
for j = 1:size(ret,2)-2 for i = 1:size(ret,1)-Nwin vars = [ret(i:i+Nwin-1,j+2) ret(i:i+Nwin-1,1) ret(i:i+Nwin-1,2)]; vars = vars(~isnan(vars(:,1)),:); vars = vars(~isnan(vars(:,2)),:); vars = vars(~isnan(vars(:,3)),:); if size(vars,1) > 15 y = vars(:,1); x = [ones(rows(y),1) vars(:,2:3)]; b = regress(y,x); betasMkt(i+Nwin,j+2) = b(2,1); % market beta
betasDisp(i+Nwin,j+2) = b(3,1); % dispersion beta
end endend
However, the betasMkt and betasDisp outputs are both just column 1 of 'ret'. It doesn't seem as if any regression has been performed.
Could anyone see where I am going wrong please?
I desperately need to figure this out soon.
Thank you for your time
Best Answer