KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Johansen cointegration test kdb+ implement

  • Johansen cointegration test kdb+ implement

    Posted by terryyao on January 13, 2022 at 12:00 am

    hi all:

    do anyone know kdb+ implement of Johansen cointegration test ?

    regard

    thanks

    terryyao replied 9 months, 1 week ago 3 Members · 2 Replies
  • 2 Replies
  • Laura

    Administrator
    January 13, 2022 at 12:00 am

    Hi  ,

    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

    Member
    January 13, 2022 at 12:00 am

    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 "rn" 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

     

     

     

     

     

Log in to reply.