Message Boards Message Boards


How to run Johansen test in Mathematica

Posted 9 years ago
11 Replies
11 Total Likes

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?

11 Replies
Posted 9 years ago
Thanks Shenghui. I've tried this piece of code but it doesn't seem to work on Mathematica 9.
Could you please post some results here or error message? 
Posted 9 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 abort
Dot::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. >>

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. 
Posted 9 years ago
thanks. Found the problem and it seems to work now.
Posted 9 years ago
Could you post the code after the fix? 
Posted 9 years ago
If you got it to work, could you uplode the working code?
Posted 9 years ago
I'm the owner of the website Shenghui linked to. If there are any changes needed to be made to that code, please let me know at the email address on the web site. It is more than ten years since I posted that code and I have not had time to check which of those notebooks still work in version 9. I think I was using version 3 or 4 when I posted all that code. And as I said under the link to that notebook: "I translated the algorithm from Gauss. Untested. Use at your own risk! "
Posted 9 years ago
Thank you for posting this and related discussion on MSE, Luci, it is really appreciated.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract