cancel
Showing results for 
Search instead for 
Did you mean: 

=?UTF-8?Q?How_to_calculate_the_rolling_co?=

huangyuanfei14
New Contributor
Hi All
 
I have a table t.
q)t
sym  minute       ri      rm
--------------------------------
IBM  09:30:00   0.10  0.05
IBM  09:31:00   0.05  0.03
IBM  09:32:00   0.11  0.05
IBM  09:33:00   0.13  0.02
IBM  09:34:00   0.11  0.04
 
How to calculate the rolling covariance(cov1) and mobile covariance(cov2)?
 
cov1:
                0.10 cov 0.05
        0.10 0.05 cov 0.05 0.03
0.10 0.05 0.11 cov 0.05 0.03 0.05
......
 
cov2:
          0.1 cov 0.05 
0.10 0.05 cov 0.05 0.03
0.05 0.11 cov 0.03 0.05
0.11 0.13 cov 0.05 0.02
......
 
Thanks,
 
Sky
4 REPLIES 4

sohagan
New Contributor
update cov1: raze (cov).' flip (ri;rm)@\:{x,y} scan til count t from t
update cov2:(cov) .' flip flip each {(til x)xprev\:y}[2;] each (ri;rm) from t

HTH,
Sean

Hi sean,Thank you very much!
在 2015年3月19日星期四 UTC+8下午8:50:30,Sean O'Hagan写道:
update cov1: raze (cov).' flip (ri;rm)@\:{x,y} scan til count t from t
update cov2:(cov) .' flip flip each {(til x)xprev\:y}[2;] each (ri;rm) from t

HTH,
Sean

On Thursday, March 19, 2015 at 3:20:17 AM UTC, huangyu...@gmail.com wrote:
Hi All
 
I have a table t.
q)t
sym  minute       ri      rm
--------------------------------
IBM  09:30:00   0.10  0.05
IBM  09:31:00   0.05  0.03
IBM  09:32:00   0.11  0.05
IBM  09:33:00   0.13  0.02
IBM  09:34:00   0.11  0.04
 
How to calculate the rolling covariance(cov1) and mobile covariance(cov2)?
 
cov1:
                0.10 cov 0.05
        0.10 0.05 cov 0.05 0.03
0.10 0.05 0.11 cov 0.05 0.03 0.05
......
 
cov2:
          0.1 cov 0.05 
0.10 0.05 cov 0.05 0.03
0.05 0.11 cov 0.03 0.05
0.11 0.13 cov 0.05 0.02
......
 
Thanks,
 
Sky

Kevin_Smyth
New Contributor
Hi Sky,

The formula for moving covariance is mcov:{mavg[x;y*z] - mavg[x;y] * mavg[x;z]}

So for your second example the 2-step moving covariance is

q)update m:mcov[2;ri;rm] from t
sym minute   ri   rm   m
-------------------------------
IBM 09:30:00 0.1  0.05 0
IBM 09:31:00 0.05 0.03 0.00025
IBM 09:32:00 0.11 0.05 0.0003
IBM 09:33:00 0.13 0.02 -0.00015
IBM 09:34:00 0.11 0.04 -0.0001


And for your first example you could use:

q)update m:mcov[count t;ri;rm] from t
sym minute   ri   rm   m
-----------------------------------
IBM 09:30:00 0.1  0.05 0
IBM 09:31:00 0.05 0.03 0.00025
IBM 09:32:00 0.11 0.05 0.0002444444
IBM 09:33:00 0.13 0.02 -6.25e-006
IBM 09:34:00 0.11 0.04 0

Which ties in with your examples above

Thanks,
Kevin

Hi Kevin, Thank you very much!
在 2015年3月19日星期四 UTC+8下午11:00:26,Kevin Smyth写道:
Hi Sky,

The formula for moving covariance is mcov:{mavg[x;y*z] - mavg[x;y] * mavg[x;z]}

So for your second example the 2-step moving covariance is

q)update m:mcov[2;ri;rm] from t
sym minute   ri   rm   m
-------------------------------
IBM 09:30:00 0.1  0.05 0
IBM 09:31:00 0.05 0.03 0.00025
IBM 09:32:00 0.11 0.05 0.0003
IBM 09:33:00 0.13 0.02 -0.00015
IBM 09:34:00 0.11 0.04 -0.0001


And for your first example you could use:

q)update m:mcov[count t;ri;rm] from t
sym minute   ri   rm   m
-----------------------------------
IBM 09:30:00 0.1  0.05 0
IBM 09:31:00 0.05 0.03 0.00025
IBM 09:32:00 0.11 0.05 0.0002444444
IBM 09:33:00 0.13 0.02 -6.25e-006
IBM 09:34:00 0.11 0.04 0

Which ties in with your examples above

Thanks,
Kevin

On 19 March 2015 at 03:20, <huangyu...@gmail.com> wrote:
Hi All
 
I have a table t.
q)t
sym  minute       ri      rm
--------------------------------
IBM  09:30:00   0.10  0.05
IBM  09:31:00   0.05  0.03
IBM  09:32:00   0.11  0.05
IBM  09:33:00   0.13  0.02
IBM  09:34:00   0.11  0.04
 
How to calculate the rolling covariance(cov1) and mobile covariance(cov2)?
 
cov1:
                0.10 cov 0.05
        0.10 0.05 cov 0.05 0.03
0.10 0.05 0.11 cov 0.05 0.03 0.05
......
 
cov2:
          0.1 cov 0.05 
0.10 0.05 cov 0.05 0.03
0.05 0.11 cov 0.03 0.05
0.11 0.13 cov 0.05 0.02
......
 
Thanks,
 
Sky

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.