Home › Forums › kdb+ › rolling linear regression in q › Re: rolling linear regression in q
-
tData:([]y:10?100.0;x1:10?10.0;x2:10?20.0;x3:10?30.0;const:1.0) tData y x1 x2 x3 const ------------------------------------------- 8.226874 2.037285 8.538354 16.01854 1 51.32018 7.757617 15.40955 28.16125 1 49.47829 0.6938325 0.3188056 9.083402 1 86.65565 4.101914 7.146077 13.34548 1 64.14975 2.337549 0.5094767 13.24347 1 90.82711 7.125845 13.76178 21.80396 1 97.96094 1.392257 12.75511 29.98824 1 30.77491 2.701876 0.7691273 22.29986 1 36.52273 6.357182 17.94471 7.113864 1 95.91177 7.771303 15.87103 17.01243 1 rolling:{[w;t] (w-1)_({ 1_x,y }[w#delete from t;t])} fn:{[t;Y;X] yx:enlist t[Y] mmu flip t[`const,X]; xx:x mmu flip[x:t[`const,X]];yx lsq xx} fn[;`y;`x1`x2`x3] each rolling[5;tData] 49.22355 4.14351 -3.200252 -0.6170487 30.90215 9.65294 -4.097335 0.03631143 41.24432 -5.843066 0.4397651 1.026252 14.49142 -2.988273 -0.3123183 2.138712 6.81604 -5.333931 1.68819 1.800037 -18.19828 -2.163089 3.582677 1.640662
It returns rows of lists of betas with rolling window:5