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