A covariance matrix is at the heart of many data analysis and time series models - as it is a measure of how two data sets change together. In Q, the natural data structure is a vector, which can be nested to represent a matrix. A covariance function is already provided that takes two vectors and computes its covariance, but as a scalar and not as a matrix.
Here is a function I wrote that creates a covariance matrix given a set of vectors. It will create a nxn matrix:
q) covar:{{x[y] cov/: x}[x;] each til (x#:)}
Here is how to use it:
q) a:1+til 10
q) b:10-til 10
q) c:2*1+til 10
q) d:2*1-til 10
q) e:4*1+til 10
q) f:4*1-til 10
q) covar (a;b;c;d;e;f)
8.25 -8.25 16.5 -16.5 33 -33
-8.25 8.25 -16.5 16.5 -33 33
16.5 -16.5 33 -33 66 -66
-16.5 16.5 -33 33 -66 66
33 -33 66 -66 132 -132
-33 33 -66 66 -132 132
Next blog post will be how to compute its eigenvectors and eigenvalue.
This is excellent, thank you very much for posting this. I'm actually trying to do something very similar and this helped me a lot. Looking forward to the next post on eigenvalue and eigenvectors!
ReplyDeleteYou are welcome. I am glad that is of use to you. I hopefully get some time to write how to compute eigenvectors in Q. Out of interest, are you using kdb at work?
ReplyDelete