Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Rolling Regression Vectorized

Subject: Rolling Regression Vectorized

From: sushma sharma

Date: 25 Apr, 2009 09:56:02

Message: 1 of 2

hi,

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 r-square).
is it possible to output these varibles without having ot run the regress command?

any help would be appreciated!

thanks!

sushma

Subject: Rolling Regression Vectorized

From: Roger Stafford

Date: 26 Apr, 2009 01:10:03

Message: 2 of 2

"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 r-square).
> 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:n-m+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 = sx2-sx.^2/m;
 cxy = sxy-sx.*sy/m;
 cy2 = sy2-sy.^2/m;
 a = cxy./cx2;
 R2 = cxy.^2./(cx2.*cy2);

The vectors a contain the slopes and R2 the r-square terms (coefficients of determination) for the intervals 1:m, 2:m+1, 3:m+2, ..., n-m+1:n. The quantities cx2, cy2, and cxy are proportional to the "rolling" variances and covariance.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us