"sushma sharma" <aim120c5@gmail.com> wrote in message <gsumni$okg$1@fred.mathworks.com>...
> am dealing with a large data set and am trying to try and write a vectorized way to output two variables from a linear ols regression (i only want the slope and rsquare).
> is it possible to output these varibles without having ot run the regress command?
There was a thread on CSSM, 16 May, 2003, that dealt with this question, entitled "vectorized rolling regression?" at:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/49181
It may answer your question. The code there devised by John D'Errico made use of matlab's 'filter' function.
Here is an alternative method which uses the 'cumsum' function. There is some sacrifice of accuracy here in favor of speed both because of the large size of accumulated sums and because of the nature of the formulas used to calculate the variances and covariance in terms of a "moving" sum.
Let X and Y be row vectors of length n of your independent and dependent data variables, respectively. Let m be the required length of each of the "rolling" regression intervals used. Then do this:
p = m+1:n+1; q = 1:nm+1;
sx = [0,cumsum(X)]; sx = sx(p)sx(q);
sy = [0,cumsum(Y)]; sy = sy(p)sy(q);
sx2 = [0,cumsum(X.^2)]; sx2 = sx2(p)sx2(q);
sxy = [0,cumsum(X.*Y)]; sxy = sxy(p)sxy(q);
sy2 = [0,cumsum(Y.^2)]; sy2 = sy2(p)sy2(q);
cx2 = sx2sx.^2/m;
cxy = sxysx.*sy/m;
cy2 = sy2sy.^2/m;
a = cxy./cx2;
R2 = cxy.^2./(cx2.*cy2);
The vectors a contain the slopes and R2 the rsquare terms (coefficients of determination) for the intervals 1:m, 2:m+1, 3:m+2, ..., nm+1:n. The quantities cx2, cy2, and cxy are proportional to the "rolling" variances and covariance.
Roger Stafford
