cancel
Showing results for 
Search instead for 
Did you mean: 

Johansen cointegration test kdb+ implement

terryyao
New Contributor II

hi all:

    do anyone know kdb+ implement of Johansen cointegration test ?

regard

thanks

2 ACCEPTED SOLUTIONS

Laura
Community Manager Community Manager
Community Manager

Hi @terryyao ,

I haven't seen the implementation done in kdb+, however we do have over 121 interfaces and connectors including Python and R, which (after a brief google) I was able to see implementation examples of online. 

If you are working on this implementation in kdb+, we would love you to share with the Community, I know many of us would be very interested to see your approach!

Good luck, and thanks for your question,

Laura

 

View solution in original post

rocuinneagain
Valued Contributor
Valued Contributor

You could use the python statsmodels.tsa.vector_ar.vecm.JohansenTestResult by importing it through EmbedPy and passing the data as a dataframe using mltab2df.

 

Below is an example based of a similar python version: http://web.pdx.edu/~crkl/ceR/Python/example14_3.py

 

$pip install statsmodels

 

q)\l p.q
q)\l ml/ml.q
q).ml.loadfile`:init.q

q)data:flip `YEAR`Y`C!"IFF"$flip 1_-12_{{x where not ""~/:x}" " vs x} each "\r\n" vs .Q.hg "http://web.pdx.edu/~crkl/ceR/data/usyc87.txt"
q)coint_johansen:.p.import[`statsmodels.tsa.vector_ar.vecm;`:coint_johansen]

q)pd:.ml.tab2df[data][`:set_index;"YEAR"]
q)res:coint_johansen[pd;0;2]

q)res[`:lr1]`
31.78169 12.17119 -1.566747e-012
q)res[`:lr2]`
19.6105 12.17119 -1.566747e-012

q)res[`:cvm]`
18.8928 21.1314 25.865
12.2971 14.2639 18.52
2.7055  3.8415  6.6349

q)res[`:cvt]`
27.0669 29.7961 35.4628
13.4294 15.4943 19.9349
2.7055  3.8415  6.6349

q){flip y!(x@/:hsym y)@\:`}[res;`lr1`lr2`cvm`cvt]
lr1            lr2            cvm                    cvt
----------------------------------------------------------------------------
31.78169       19.6105        18.8928 21.1314 25.865 27.0669 29.7961 35.4628
12.17119       12.17119       12.2971 14.2639 18.52  13.4294 15.4943 19.9349
-1.566747e-012 -1.566747e-012 2.7055  3.8415  6.6349 2.7055  3.8415  6.6349

 

 

 

 

 

View solution in original post

2 REPLIES 2

Laura
Community Manager Community Manager
Community Manager

Hi @terryyao ,

I haven't seen the implementation done in kdb+, however we do have over 121 interfaces and connectors including Python and R, which (after a brief google) I was able to see implementation examples of online. 

If you are working on this implementation in kdb+, we would love you to share with the Community, I know many of us would be very interested to see your approach!

Good luck, and thanks for your question,

Laura

 

rocuinneagain
Valued Contributor
Valued Contributor

You could use the python statsmodels.tsa.vector_ar.vecm.JohansenTestResult by importing it through EmbedPy and passing the data as a dataframe using mltab2df.

 

Below is an example based of a similar python version: http://web.pdx.edu/~crkl/ceR/Python/example14_3.py

 

$pip install statsmodels

 

q)\l p.q
q)\l ml/ml.q
q).ml.loadfile`:init.q

q)data:flip `YEAR`Y`C!"IFF"$flip 1_-12_{{x where not ""~/:x}" " vs x} each "\r\n" vs .Q.hg "http://web.pdx.edu/~crkl/ceR/data/usyc87.txt"
q)coint_johansen:.p.import[`statsmodels.tsa.vector_ar.vecm;`:coint_johansen]

q)pd:.ml.tab2df[data][`:set_index;"YEAR"]
q)res:coint_johansen[pd;0;2]

q)res[`:lr1]`
31.78169 12.17119 -1.566747e-012
q)res[`:lr2]`
19.6105 12.17119 -1.566747e-012

q)res[`:cvm]`
18.8928 21.1314 25.865
12.2971 14.2639 18.52
2.7055  3.8415  6.6349

q)res[`:cvt]`
27.0669 29.7961 35.4628
13.4294 15.4943 19.9349
2.7055  3.8415  6.6349

q){flip y!(x@/:hsym y)@\:`}[res;`lr1`lr2`cvm`cvt]
lr1            lr2            cvm                    cvt
----------------------------------------------------------------------------
31.78169       19.6105        18.8928 21.1314 25.865 27.0669 29.7961 35.4628
12.17119       12.17119       12.2971 14.2639 18.52  13.4294 15.4943 19.9349
-1.566747e-012 -1.566747e-012 2.7055  3.8415  6.6349 2.7055  3.8415  6.6349