2022.08.21 09:04 PM
hi, is there a function to compute rolling linear regression (multiple independent variables) for a q table?
I am looking for something like https://www.statsmodels.org/dev/generated/statsmodels.regression.rolling.RollingOLS.html in python. Thanks.
pseudo code:
e.g. rlreg[n;endog;exog]
select rlreg[20;col1;(col2;col3)] from table... -> the result is a list of betas
2022.08.21 11:26 PM - edited 2022.08.21 11:54 PM
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
q)
It returns rows of lists of betas with rolling window:5
2022.08.21 11:26 PM - edited 2022.08.21 11:54 PM
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
q)
It returns rows of lists of betas with rolling window:5
2022.08.22 01:25 AM - edited 2022.08.22 01:26 AM
Wrapped them into function:
tabData:([]y:10?100.0;x1:10?10.0;x2:10?20.0;x3:10?30.0;const:1.0);
main:{[n;y;xs;tab]
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;xs] each rolling[n;tab]
}
betas:main[5;`y;`x1`x2`x3;tabData]
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.