Group Abstract Group Abstract

Message Boards Message Boards

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
POSTED BY: Xavier Vallee
Answer
1 year ago
You may quickly take a look at this link: 
http://www.verbeia.com/mathematica/mathecon/mathecon_index.html
POSTED BY: Shenghui Yang
Answer
1 year ago
Thanks Shenghui. I've tried this piece of code but it doesn't seem to work on Mathematica 9.
POSTED BY: Xavier Vallee
Answer
1 year ago
Could you please post some results here or error message? 
POSTED BY: Shenghui Yang
Answer
1 year 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. >>
POSTED BY: Xavier Vallee
Answer
1 year ago
@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. 
POSTED BY: Shenghui Yang
Answer
1 year ago
thanks. Found the problem and it seems to work now.
POSTED BY: Xavier Vallee
Answer
1 year ago
Could you post the code after the fix? 
POSTED BY: Aleks
Answer
10 months ago
If you got it to work, could you uplode the working code?
POSTED BY: Aleks
Answer
11 months 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 BY: Luci Ellis
Answer
11 months ago
POSTED BY: Luci Ellis
Answer
10 months ago
Thank you for posting this and related discussion on MSE, Luci, it is really appreciated.
POSTED BY: Vitaliy Kaurov
Answer
10 months ago