I have two vectors x and y, and I want to compute a rolling regression for those, e.g a on (x(1:4),y(1:4)), (x(2:5),y(2:5)), ...
Is there already a function for that? The best algorithm I have in mind for this is O(n), but applying separate linear regressions on every subarrays would be O(n^2).
I'm working with Matlab and Python (numpy).
2
votes
2 Answers
2
votes
No, there is NO function that will do a rolling regression, returning all the statistics you wish, doing it efficiently.
That does not mean you can't write such a function. To do so would mean multiple calls to a tool like conv or filter. This is how a Savitsky-Golay tool would work, which DOES do most of what you want. Make one call for each regression coefficient.
Use of up-dating and down-dating tools to use/modify the previous regression estimates will not be as efficient as the calls to conv, since you only need factorize a linear system ONCE when you then do the work with conv. Anyway, there is no need to do an update, as long as the points are uniformly spaced in the series. This is why Savitsky-Golay works.
1
votes
import numpy as np
# y=x*alpha+beta
# window_size - integer, x-numpy array, y-numpy array
x2=np.power(x,2)
xy=x*y
window = np.ones(int(window_size))
a1=np.convolve(xy, window, 'full')*window_size
a2=np.convolve(x, window, 'full')*np.convolve(y, window, 'full')
b1=np.convolve(x2, window, 'full')*window_size
b2=np.power(np.convolve(x, window, 'full'),2)
alphas=(a1-a2)/(b1-b2)
betas=(np.convolve(y, window, 'full')-alphas*np.convolve(x, window, 'full'))/float(window_size)
alphas=alphas[:-1*(window_size-1)] #numpy array of rolled alpha
betas=betas[:-1*(window_size-1)] #numpy array of rolled beta
O(n)? Amount of times you need to do a regression:O(n), work to be done in each regression:O(1)(assuming the windowsize is constant) - Dennis Jaheruddin