This is an example of a (bivariate) Granger causality test in a rolling window simulated data. Note that, by construction, there is no causality, and the process is stable across the entire range, so the apparent change in "causality" from the start to the end of the sample is spurious.

This sets up the model for

$$\begin{array}{l}{x_t} = .85{x_{t - 1}} + {\varepsilon _{xt}} \\ {y_t} = .80{y_{t - 1}} + .20{x_{t - 1}} + {\varepsilon _{yt}} \\\end{array}$$

which has Granger causality running from x to y, but not y to x. The tests below are for y to x causality.

It's simplest to do small models like this using FRML's.

dec series x y

frml xfrml x = .85*x{1}

frml yfrml y = .80*y{1}+.2*x{1}

compute [symm] sigma=||1.0|0.4,1.0||

group nocause xfrml>>x yfrml>>y

This generates 500 data points. The first (needed for the one lag in the model) is initialized to zero so the (Gaussian) simulation starts in period 2. We won't use the first 100 generated data points in any of the subsequent analysis.

set x 1 500 = 0.0

set y 1 500 = 0.0

simulate(model=nocause,from=2,to=500,cv=sigma)

This does the rolling tests with a estimation span of 100 entries. The loop index TIME chooses the end of the window. The test statistic generated by the window ending at entry TIME is saved into the series CAUSETESTS at that entry.

compute nspan=100

compute nlags=3

*

do time=200,500

linreg(noprint) x time-nspan+1 time

# constant x{1 to nlags} y{1 to nlags}

exclude(noprint)

# y{1 to nlags}

compute causetests(time)=%cdstat

end do time

This graphs the causality tests with a grid line at the .05 critical value for the F-statistic. (It also uses GRTEXT to add the description to the line—that requires enclosing the two graph operations in an SPGRAPH.)

compute cv05=%invftest(.05,nlags,%ndf)

spgraph(footer="Rolling Causality Tests with Simulated Data")

graph(vgrid=||cv05||)

# causetests

grtext(y=cv05,entry=200,align=left,valign=bottom) ".05 critical value"

spgraph(done)

This does the regression over the full sample, full sample causality test and stability test. In practice, this should probably have been done first before doing a "rolling" analysis on an apparently (from the test) stable regression.

linreg x 101 *

# constant x{1 to nlags} y{1 to nlags}

exclude(title="Full sample causality test")

# y{1 to nlags}

@regstabtest

Full Program

seed 530393
dec series x y
frml xfrml x = .85*x{1}
frml yfrml y = .80*y{1}+.2*x{1}
compute [symm] sigma=||1.0|0.4,1.0||
group nocause xfrml>>x yfrml>>y
*
* This generates 500 data points and doesn't use the first 100 in any of
* the subsequent analysis.
*
set x 1 500 = 0.0
set y 1 500 = 0.0
simulate(model=nocause,from=2,to=500,cv=sigma)
*
set causetests = %na
*
* Window span
*
compute nspan=100
compute nlags=3
*
do time=200,500
linreg(noprint) x time-nspan+1 time
# constant x{1 to nlags} y{1 to nlags}
exclude(noprint)
# y{1 to nlags}
compute causetests(time)=%cdstat
end do time
*
* For comparison, get the .05 critical value for the test
*
compute cv05=%invftest(.05,nlags,%ndf)
spgraph(footer="Rolling Causality Tests with Simulated Data")
graph(vgrid=||cv05||)
# causetests
grtext(y=cv05,entry=200,align=left,valign=bottom) ".05 critical value"
spgraph(done)
*
* Regression over the full sample, full sample causality test and
* stability test.
*
linreg x 101 *
# constant x{1 to nlags} y{1 to nlags}
exclude(title="Full sample causality test")
# y{1 to nlags}
@regstabtest


Graphs Output

Linear Regression - Estimation by Least Squares

Dependent Variable X

Usable Observations                       400

Degrees of Freedom                        393

Centered R^2                        0.7555190

R-Bar^2                             0.7517865

Uncentered R^2                      0.7744024

Mean of Dependent Variable       -0.609150354

Std Error of Dependent Variable   2.108121670

Standard Error of Estimate        1.050287998

Sum of Squared Residuals         433.52021748

Regression F(6,393)                  202.4145

Significance Level of F             0.0000000

Log Likelihood                      -583.6702

Durbin-Watson Statistic                1.9920

Variable                        Coeff      Std Error      T-Stat      Signif

************************************************************************************

1.  Constant                     -0.084909568  0.057561569     -1.47511  0.14098400

2.  X{1}                          0.814474236  0.054717064     14.88520  0.00000000

3.  X{2}                          0.077385887  0.068523074      1.12934  0.25944305

4.  X{3}                         -0.003803219  0.056427411     -0.06740  0.94629738

5.  Y{1}                          0.079625050  0.056878842      1.39991  0.16233027

6.  Y{2}                         -0.112254392  0.071012075     -1.58078  0.11473305

7.  Y{3}                          0.008531989  0.053921462      0.15823  0.87435689

Full sample causality test

Null Hypothesis : The Following Coefficients Are Zero

Y                Lag(s) 1 to 3

F(3,393)=      1.32852 with Significance Level 0.26466512

Hansen Stability Test

Test   Statistic  P-Value

Joint    2.09131776    0.05

Variance 0.06680167    0.76

Constant 0.48563061    0.04

X{1}     0.10685158    0.53

X{2}     0.09467980    0.59

X{3}     0.35975192    0.09

Y{1}     0.24542486    0.19

Y{2}     0.29445031    0.14

Y{3}     0.21454996    0.23