Rolling Beta For Multiple x and y variables simultaneously
이전 댓글 표시
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!
댓글 수: 10
Dwight Schrute III
2019년 9월 4일
the cyclist
2019년 9월 4일
For each y (at one of the time points), do you want
- the 8 coefficients of a single regression against the 8 x's together, OR
- the 8 slopes of y vs. each x, individually?
Dwight Schrute III
2019년 9월 4일
the cyclist
2019년 9월 4일
OK.
But note that there would 20*8 combinations for either of the above bullets. The second bullet gives 8 slopes, from 8 regressions. The first bullet gives 8 coefficients, in one regression. (That's why it needed clarification.)
Dwight Schrute III
2019년 9월 4일
the cyclist
2019년 9월 4일
편집: the cyclist
2019년 9월 4일
Another clarification ...
For a single time point, and a given y and x, do you want
- the correlation coefficient, OR
- the regression coefficient
These will be the same if you standardize your variables first (i.e de-mean and divide by standard deviation). But you'll have to standardize each window separately.
Dwight Schrute III
2019년 9월 4일
the cyclist
2019년 9월 4일
Another clarification ...
As you have pointed out, the number of coefficients you'll be calculating is
(number y's) * (number x's) * (number windows) = 20 * 8 * 901.
How do you want those arranged in the output? In a 20 x 8 x 901 numeric array? Or something else?
Dwight Schrute III
2019년 9월 4일
채택된 답변
추가 답변 (1개)
John D'Errico
2019년 9월 4일
편집: John D'Errico
2019년 9월 4일
1 개 추천
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?
댓글 수: 3
Dwight Schrute III
2019년 9월 4일
John D'Errico
2019년 9월 4일
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.
Dwight Schrute III
2019년 9월 9일
카테고리
도움말 센터 및 File Exchange에서 Model Building and Assessment에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
