Alex Isakov has a Granger Causality Test in his Economica Time Series package here:-

Mathematica Package Repository - Economica

I'm not very familiar with the details, but I ran some tests using data from here:-

Dave Giles' Blog - Testing for Granger Causality

I downloaded the example data from the Data page. Here it is stored as QR codes.

```
qrimage = BarcodeImage[Compress[datalist]];
```

*Dates*

*Arabica data*

*Robusta data*

```
{dates, arabica, robusta} = Map[
Uncompress@BarcodeRecognize@Import[#] &,
{"http://i.stack.imgur.com/KdrUd.png",
"http://i.stack.imgur.com/hCbZV.png",
"http://i.stack.imgur.com/J9ESY.png"}];
arabica = Last /@
Select[Transpose[{DateList /@ dates, arabica}], #[[1, 1]] > 1975 &];
{dates, robusta} = Transpose[
Select[Transpose[{DateList /@ dates, robusta}], #[[1, 1]] > 1975 &]];
DateListPlot[{Transpose[{dates, arabica}],
Transpose[{dates, robusta}]}, Joined -> True]
```

*Alex Isakov's Granger Causality function*

```
getLags[data_, i_] := Drop[data, -i]
GrangerCausalityTest[dat_, lag_: 3] := Module[
{xx, x, y, laggeddata, le, res1, res2, grangerstat},
le = Select[dat, (And @@ (NumericQ /@ #)) &];
xx = Flatten[Most /@ le]; y = Last /@ le;
laggeddata = Transpose[Map[Take[#, -Length[y] + lag] &,
Join[Table[getLags[y, i], {i, lag}],
Table[getLags[xx, i], {i, lag}], {y}]]];
res1 = Total[LinearModelFit[laggeddata,
Table[Subscript[x, i],
{i, Length[First[laggeddata]] - 1}],
Table[Subscript[x, i],
{i, Length[First[laggeddata]] - 1}]]["FitResiduals"]^2];
res2 = Total[LinearModelFit[laggeddata,
Table[Subscript[x, i],
{i, (Length[First[laggeddata]] - 1)/2}],
Table[Subscript[x, i],
{i, Length[First[laggeddata]] - 1}]]["FitResiduals"]^2];
grangerstat = ((res2 - res1)/lag)/(res1/(Length[laggeddata] - 2*lag - 1));
1 - CDF[FRatioDistribution[lag, Length[laggeddata] - 2*lag - 1],
grangerstat]];
```

Checking a range of lag intervals.

```
a = Array[GrangerCausalityTest[Transpose[{robusta, arabica}], #] &, 10];
b = Array[GrangerCausalityTest[Transpose[{arabica, robusta}], #] &, 10];
ListLinePlot[{a, b}]
```

```
GrangerCausalityTest@Transpose[{arabica, robusta}]
```

0.127273

indicating low significance of null hypothesis.

```
GrangerCausalityTest@Transpose[{robusta, arabica}]
```

0.8631

Quoting from Dave Giles' page:

"In summary, we have reasonable evidence of Granger causality from the
price of Arabica coffee to the price of Robusta coffee, but not *vice
versa*."

**Check**

Using Free Statistics Software with the coffee data, plotting the results for the first ten lags.

```
ListLinePlot[{
{0.4541, 0.8248, 0.0696, 0.0454, 0.061,
0.0445, 0.0201, 0.0052, 0.0039, 0.0307},
{0.2605, 0.3755, 0.7118, 0.4239, 0.3455,
0.2779, 0.1233, 0.0361, 0.0055, 0.0172},
ConstantArray[0.05, 10]}, AxesOrigin -> {0, 0},
Epilog -> Inset["P-value 0.05", {1.5, 0.08}]]
```

The results indicate a rejection of the hypothesis that the price of Arabica coffee does not affect the price of Robusta coffee.

The shape of the graph is similar to that produced by Alex's function, but clearly there are differences.

*Additional note*

The results from Alex's function match exactly the results from here:

MathGroup archive: implementation of Granger causality tests in Mathematica

dated Jan 2006, by Darren Glosemeyer, Wolfram Research.

(Runs in version 7 but not 10.4, requiring legacy Statistics package.)

*Further reading*

How about CorrelationFunction? Put the same sequence as both arguments and it is autocorrelation. – bill s – 2015-02-02T16:07:00.160