[英]Efficient way to do a rolling linear regression
I have two vectors x and y, and I want to compute a rolling regression for those, eg a on (x(1:4),y(1:4)), (x(2:5),y(2:5)), ...
我有两个向量x和y,我想为它们计算滚动回归,例如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).
我想到的最好的算法是O(n),但是在每个子数组上应用单独的线性回归将是O(n ^ 2)。 I'm working with Matlab and Python (numpy).
我正在使用Matlab和Python(numpy)。
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.
这样做将意味着多次调用诸如conv或filter之类的工具。 This is how a Savitsky-Golay tool would work, which DOES do most of what you want.
这就是Savitsky-Golay工具的工作方式,它可以满足您的大部分需求。 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. 使用最新的和最新的工具来使用/修改先前的回归估计将不会像调用conv那样有效,因为在使用conv进行工作时只需要分解线性系统ONCE即可。 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.
这就是Savitsky-Golay起作用的原因。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.