a b/Supporting Functions/granger_cause.m
1
function [F,c_v] = granger_cause(x,y,alpha,max_lag)
2
% [F,c_v] = granger_cause(x,y,alpha,max_lag)
3
% Granger Causality test
4
% Does Y Granger Cause X?
5
%
6
% User-Specified Inputs:
7
%   x -- A column vector of data
8
%   y -- A column vector of data
9
%   alpha -- the significance level specified by the user
10
%   max_lag -- the maximum number of lags to be considered
11
% User-requested Output:
12
%   F -- The value of the F-statistic
13
%   c_v -- The critical value from the F-distribution
14
%
15
% The lag length selection is chosen using the Bayesian information
16
% Criterion 
17
% Note that if F > c_v we reject the null hypothesis that y does not
18
% Granger Cause x
19
20
% Chandler Lutz, UCR 2009
21
% Questions/Comments: chandler.lutz@email.ucr.edu
22
% $Revision: 1.0.0 $  $Date: 09/30/2009 $
23
% $Revision: 1.0.1 $  $Date: 10/20/2009 $
24
% $Revision: 1.0.2 $  $Date: 03/18/2009 $
25
26
% References:
27
% [1] Granger, C.W.J., 1969. "Investigating causal relations by econometric
28
%     models and cross-spectral methods". Econometrica 37 (3), 424–438.
29
30
% Acknowledgements:
31
%   I would like to thank Mads Dyrholm for his helpful comments and
32
%   suggestions
33
34
%Make sure x & y are the same length
35
if (length(x) ~= length(y))
36
    error('x and y must be the same length');
37
end
38
39
%Make sure x is a column vector
40
[a,b] = size(x);
41
if (b>a)
42
    %x is a row vector -- fix this
43
    x = x';
44
end
45
46
%Make sure y is a column vector
47
[a,b] = size(y);
48
if (b>a)
49
    %y is a row vector -- fix this
50
    y = y';
51
end
52
53
54
55
%Make sure max_lag is >= 1
56
if max_lag < 1
57
    error('max_lag must be greater than or equal to one');
58
end
59
60
%First find the proper model specification using the Bayesian Information
61
%Criterion for the number of lags of x
62
63
T = length(x);
64
65
BIC = zeros(max_lag,1);
66
67
%Specify a matrix for the restricted RSS
68
RSS_R = zeros(max_lag,1);
69
70
i = 1;
71
while i <= max_lag
72
    ystar = x(i+1:T,:);
73
    xstar = [ones(T-i,1) zeros(T-i,i)];
74
    %Populate the xstar matrix with the corresponding vectors of lags
75
    j = 1;
76
    while j <= i
77
        xstar(:,j+1) = x(i+1-j:T-j);
78
        j = j+1;
79
    end
80
    %Apply the regress function. b = betahat, bint corresponds to the 95%
81
    %confidence intervals for the regression coefficients and r = residuals
82
    [b,bint,r] = regress(ystar,xstar);
83
    
84
    %Find the bayesian information criterion
85
    BIC(i,:) = T*log(r'*r/T) + (i+1)*log(T);
86
    
87
    %Put the restricted residual sum of squares in the RSS_R vector
88
    RSS_R(i,:) = r'*r;
89
    
90
    i = i+1;
91
    
92
end
93
94
[dummy,x_lag] = min(BIC);
95
96
%First find the proper model specification using the Bayesian Information
97
%Criterion for the number of lags of y
98
99
BIC = zeros(max_lag,1);
100
101
%Specify a matrix for the unrestricted RSS
102
RSS_U = zeros(max_lag,1);
103
104
i = 1;
105
while i <= max_lag
106
    
107
    ystar = x(i+x_lag+1:T,:);
108
    xstar = [ones(T-(i+x_lag),1) zeros(T-(i+x_lag),x_lag+i)];
109
    %Populate the xstar matrix with the corresponding vectors of lags of x
110
    j = 1;
111
    while j <= x_lag
112
        xstar(:,j+1) = x(i+x_lag+1-j:T-j,:);
113
        j = j+1;
114
    end
115
    %Populate the xstar matrix with the corresponding vectors of lags of y
116
    j = 1;
117
    while j <= i
118
        xstar(:,x_lag+j+1) = y(i+x_lag+1-j:T-j,:);
119
        j = j+1;
120
    end
121
    %Apply the regress function. b = betahat, bint corresponds to the 95%
122
    %confidence intervals for the regression coefficients and r = residuals
123
    [b,bint,r] = regress(ystar,xstar);
124
    
125
    %Find the bayesian information criterion
126
    BIC(i,:) = T*log(r'*r/T) + (i+1)*log(T);
127
    
128
    RSS_U(i,:) = r'*r;
129
    
130
    i = i+1;
131
    
132
end
133
134
[dummy,y_lag] =min(BIC);
135
136
%The numerator of the F-statistic
137
F_num = ((RSS_R(x_lag,:) - RSS_U(y_lag,:))/y_lag);
138
139
%The denominator of the F-statistic
140
F_den = RSS_U(y_lag,:)/(T-(x_lag+y_lag+1));
141
142
%The F-Statistic
143
F = F_num/F_den;
144
145
c_v = finv(1-alpha,y_lag,(T-(x_lag+y_lag+1)));
146
147
148
    
149
    
150
    
151
    
152
153