19 views (last 30 days)

Hello,

I have two matrices: One is an x-variable matrix (for example, 8 x variables as columns, with 1,000 rows, where each row represents a day). And one is a y-variable matrix (for example, 20 y variables as columns, with the same 1,000 rows).

I would like to calculate a matrix C that produces a rolling 100-day beta of each y variable to each x variable. Thus, C would have 20 * 8 = 160 columns. And moreover, since it's a rolling beta, the number of rows would be (1,000-100+1) = 901 rows (since the first 99 days wouldn't be eligible for a 100-day beta).

I have been playing around with various functions, e.g., corr, polyfit, and regress. However, none of these appear to address my query on rolling betas. In fact, I'm not sure I even see the ability to implement a rolling beta for just one variable in each matrix.

I would appreciate any guidance on this. Thank you!

the cyclist
on 5 Sep 2019

Edited: the cyclist
on 6 Sep 2019

I believe this does what you intend.

% Set seed for reproducibility

rng default

% Set a few convenience parameters

N = 1000;

WINSIZE = 100;

XN = 8;

YN = 10;

% Simulate some data

x = randn(N,XN);

y = randn(N,YN);

% Calculate the number of window

numberWindows = N - WINSIZE + 1;

% Preallocate the output

output = zeros(YN,XN,numberWindows);

% Loop over the windows

for nw = 1:numberWindows

% Find the data for this window

thisWindowIndex = nw:(nw+WINSIZE-1);

thisWindowXData = x(thisWindowIndex,:);

thisWindowYData = y(thisWindowIndex,:);

for ny = 1:YN

for nx = 1:XN

% Solve the regression (returns intercept and slope)

tmp = [ones(WINSIZE,1) thisWindowXData(:,nx)]\thisWindowYData(:,ny);

% Store the slope

output(ny,nx,nw) = tmp(2);

end

end

end

John D'Errico
on 4 Sep 2019

Edited: John D'Errico
on 4 Sep 2019

Is the x vector equally spaced? If so, then my movingslope code (found on the File Exchange) will do it trivially and efficiently.

If not, then nothing stops you from using a loop and polyfit. It still will be reasonably efficient. You could make it a little faster with carefully written code than polyfit, but why bother?

John D'Errico
on 4 Sep 2019

If the points are not evenly spaced, then the regression matrix changes for each location. You could write code that would work, not using a loop. It would look more elegant. It might take more memory though.

For example, you could write it using an update and downdate for a QR decomposition. Adding one point at the end, then dropping the first point. It would still be a loop. And the update/downdate would be slower then just throwing backslash at it, or even polyfit.

Or, given a simple regression for just a simple slope, you could do effectively the same thing. The formula for the slope is easy to write down. So, again, it would be easy to do, though still a loop.

Is this something you will be doing often? If so, then it would be worth the programmer time to do it better. But for a one shot deal, I'd not bother. CPU time is really cheap, and for a problem that is not a bottleneck in your task, a loop is easy.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 10 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742272

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742272

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742276

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742276

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742281

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742281

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742285

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742285

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742290

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742290

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742294

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742294

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742295

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742295

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742296

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742296

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742299

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742299

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742300

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/478913-rolling-beta-for-multiple-x-and-y-variables-simultaneously#comment_742300

Sign in to comment.