# How to run Johansen test in Mathematica

GROUPS:
 Hi,Does anyone have any idea on wether it's possible to run the Johansen Cointegration tests in Mathematica? If not, is the code available somewhere?Xave
5 years ago
11 Replies
 Shenghui Yang 1 Vote You may quickly take a look at this link: http://www.verbeia.com/mathematica/mathecon/mathecon_index.html
5 years ago
 Thanks Shenghui. I've tried this piece of code but it doesn't seem to work on Mathematica 9.
5 years ago
 Could you please post some results here or error message?
5 years ago
 I am trying to test the cointegration between two ETFs.----------------------------------------------------------- ewa = FinancialData["NYSE:EWA", {{2006, 4, 4}, {2012, 4, 9}}, "Value"]; ewc = FinancialData["NYSE:EWC", {{2006, 4, 4}, {2012, 4, 9}}, "Value"]; data = Transpose@{ewa, ewc};  johansenProcedure[levels_?MatrixQ, p_Integer] /; (Dimensions[levels][[ 1]] > (Dimensions[levels][[2]] + p)) := With[{N = Dimensions[levels][[1]], T = Dimensions[levels][[2]], diff = Rest[levels - RotateLeft[levels]], ypt = Drop[levels, -p]}, Module[{x, r0t, rpt, s00, sop, cc, \[Lambda], v\[Lambda], log\[Lambda]},x = Join @@ (Drop[RotateLeft[diff, #], p - 1] & /@ Range[p]);With[{q = Inverse[Transpose[x].x], y0t = Drop[diff, p - 1]},r0t = y0t - x.q.Transpose[x].y0t; rpt = ypt - x.q.Transpose[x].ypt; s00 = (Transpose[r0t].r0t)/T; s0p = (Transpose[r0t].rpt)/T;cc = Inverse[CholeskyDecomposition[(Transpose[rpt].rpt)/T]];{\[Lambda], v\[Lambda]} = Eigensystem[(cc.Transpose[s0p].Inverse[s00].s0p.Transpose[cc])];log\[Lambda] = Log[1 - \[Lambda]];{Reverse[-T* FoldList[Plus, First[log\[Lambda]], Rest[log\[Lambda]]]],Reverse[-T log\[Lambda]], Join[Reverse[Transpose[Transpose[cc].v\[Lambda]]], s00, s0p]} ]]];johansenProcedure[data, 10]and then it keeps running until I abortDot::dotsh: Tensors {<<1>>} and {{0.38,0.42},{-0.21,-0.27},{0.03,0.13},{0.,-0.03},<<44>>,{0.04,0.02},{-0.56,-0.63},<<1455>>} have incompatible shapes. >>
5 years ago
 Shenghui Yang 2 Votes @Xavier, Usually if you have error message from your function, the best way to debug is to break a block of codes into separated cells. In your case, I notice this line is broken: r0t = y0t - x.q.Transpose[x].y0t; rpt = ypt - x.q.Transpose[x].ypt;The dimensional mismatch is from here.
5 years ago
 Xavier Vallee 1 Vote thanks. Found the problem and it seems to work now.
5 years ago
 Could you post the code after the fix?
4 years ago
 If you got it to work, could you uplode the working code?