-
Notifications
You must be signed in to change notification settings - Fork 6
/
DemoMVGC.m
257 lines (206 loc) · 9.18 KB
/
DemoMVGC.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
%%
% this function is a slight variation on the demo provided by the MVGC
% toolbox, written by Bethany Lusch for testing
%
% INPUTS:
%
% X
% [n x m x N] matrix of time series data (n nodes, m time points, N time series)
%
% OUTPUTS:
%
% mat
% [n x n] inferred adjacency matrix
%
% diagnostics
% vector of diagnostics about this run: [warning, morder, diffCheck], where
% warning: number of autovariance lags over 1000 (acmaxlags = 1000)
% morder: model order chosen
% diffCheck: non-zero if 'WARNING: high maximum absolute difference = %e.2 (> %.2e)\n'
% is received (specifically it's the amount past the threshold)
%
% below are comments from MVGC toolbox:
%% MVGC demo
%
% Demonstrates typical usage of the MVGC toolbox on generated VAR data for a
% 5-node network with known causal structure (see <var5_test.html |var5_test|>).
% Estimates a VAR model and calculates time- and frequency-domain
% pairwise-conditional Granger causalities (also known as the "causal graph").
% Also calculates Seth's causal density measure [2].
%
% This script is a good starting point for learning the MVGC approach to
% Granger-causal estimation and statistical inference. It may serve as a useful
% template for your own code. The computational approach demonstrated here will
% make a lot more sense alongside the reference document> [1], which we
% _strongly recommend_ you consult, particularly Section 3 on design principles
% of the toolbox. You might also like to refer to the <mvgc_schema.html schema>
% of MVGC computational pathways - <mvgc_schema.html#3 algorithms> |A<n>| in
% this demo refer to the algorithm labels listed there - and the
% <mvgchelp.html#4 Common variable names and data structures> section of the
% Help documentation.
%
% *_FAQ:_* _Why do the spectral causalities look so smooth?_
%
% This is because spectral quantities are calculated from the estimated VAR,
% rather than sampled directly. This is in accordance with the MVGC design
% principle that all causal estimates be based on the <mvgc_demo.html#6
% estimated VAR model> for your data, and guarantees that spectral causalities
% <mvgc_demo.html#10 integrate correctly> to time-domain causality as theory
% requires. See [1] for details.
%
% *_Note_*: Do _not_ pre-filter your data prior to GC estimation, _except_
% possibly to improve stationarity (e.g notch-filtering to eliminate line noise
% or high-pass filtering to suppress low-frequency transients). Pre-filtering
% (of stationary data) may seriously degrade Granger-causal inference! If you
% want (time-domain) GC over a limited frequency range, rather calculate
% "band-limited" GC; to do this, calculate frequency-domain GCs over the full
% frequency range, then integrate over the desired frequency band [3]; see
% <smvgc_to_mvgc.html |smvgc_to_mvgc|>.
%
%% References
%
% [1] L. Barnett and A. K. Seth,
% <http://www.sciencedirect.com/science/article/pii/S0165027013003701 The MVGC
% Multivariate Granger Causality Toolbox: A New Approach to Granger-causal
% Inference>, _J. Neurosci. Methods_ 223, 2014
% [ <matlab:open('mvgc_preprint.pdf') preprint> ].
%
% [2] A. B. Barrett, L. Barnett and A. K. Seth, "Multivariate Granger causality
% and generalized variance", _Phys. Rev. E_ 81(4), 2010.
%
% [3] L. Barnett and A. K. Seth, "Behaviour of Granger causality under
% filtering: Theoretical invariance and practical application", _J. Neurosci.
% Methods_ 201(2), 2011.
%
% (C) Lionel Barnett and Anil K. Seth, 2012. See file license.txt in
% installation directory for licensing terms.
%
%%
function [mat, diagnostics] = DemoMVGC(X)
%% Parameters
ntrials = size(X, 3); % number of trials
nobs = size(X, 2); % number of observations per trial
nvars = size(X, 1); % number of nodes
regmode = 'OLS'; % VAR model estimation regression mode ('OLS', 'LWR' or empty for default)
icregmode = 'LWR'; % information criteria regression mode ('OLS', 'LWR' or empty for default)
morder = 'AIC'; % model order to use ('actual', 'AIC', 'BIC' or supplied numerical value)
momax = 20; % maximum model order for model order estimation
acmaxlags = 1000; % maximum autocovariance lags (empty for automatic calculation)
tstat = ''; % statistical test for MVGC: 'F' for Granger's F-test (default) or 'chi2' for Geweke's chi2 test
alpha = 0.05; % significance level for significance test
mhtc = 'FDR'; % multiple hypothesis test correction (see routine 'significance')
fs = 200; % sample rate (Hz)
fres = []; % frequency resolution (empty for automatic calculation)
%% We don't generate data here because we pass it in.
%% Model order estimation (<mvgc_schema.html#3 |A2|>)
% Calculate information criteria up to specified maximum model order.
ptic('\n*** tsdata_to_infocrit\n');
[AIC,BIC,moAIC,moBIC] = tsdata_to_infocrit(X,momax,icregmode);
ptoc('*** tsdata_to_infocrit took ');
% Plot information criteria.
%figure(1); clf;
%plot_tsdata([AIC BIC]',{'AIC','BIC'},1/fs);
%title('Model order estimation');
%amo = size(AT,3); % actual model order
fprintf('\nbest model order (AIC) = %d\n',moAIC);
fprintf('best model order (BIC) = %d\n',moBIC);
%fprintf('actual model order = %d\n',amo);
% Select model order.
% if strcmpi(morder,'actual')
% morder = amo;
% fprintf('\nusing actual model order = %d\n',morder);
if strcmpi(morder,'AIC')
morder = moAIC;
fprintf('\nusing AIC best model order = %d\n',morder);
elseif strcmpi(morder,'BIC')
morder = moBIC;
fprintf('\nusing BIC best model order = %d\n',morder);
else
fprintf('\nusing specified model order = %d\n',morder);
end
%% VAR model estimation (<mvgc_schema.html#3 |A2|>)
% Estimate VAR model of selected order from data.
ptic('\n*** tsdata_to_var... ');
[A,SIG] = tsdata_to_var(X,morder,regmode);
ptoc;
% Check for failed regression
assert(~isbad(A),'VAR estimation failed');
% NOTE: at this point we have a model and are finished with the data! - all
% subsequent calculations work from the estimated VAR parameters A and SIG.
%% Autocovariance calculation (<mvgc_schema.html#3 |A5|>)
% The autocovariance sequence drives many Granger causality calculations (see
% next section). Now we calculate the autocovariance sequence G according to the
% VAR model, to as many lags as it takes to decay to below the numerical
% tolerance level, or to acmaxlags lags if specified (i.e. non-empty).
ptic('*** var_to_autocov... ');
[G,info] = var_to_autocov(A,SIG,acmaxlags);
ptoc;
decay = zeros(size(G,3),1);
for j = 1:size(G,3)
decay(j) = norm(G(:,:,j));
end
% The above routine does a LOT of error checking and issues useful diagnostics.
% If there are problems with your data (e.g. non-stationarity, colinearity,
% etc.) there's a good chance it'll show up at this point - and the diagnostics
% may supply useful information as to what went wrong. It is thus essential to
% report and check for errors here.
acminlags = info.acminlags;
var_info(info,true); % report results (and bail out on error)
%% Granger causality calculation: time domain (<mvgc_schema.html#3 |A13|>)
% Calculate time-domain pairwise-conditional causalities - this just requires
% the autocovariance sequence.
ptic('*** autocov_to_pwcgc... ');
F = autocov_to_pwcgc(G);
ptoc;
% Check for failed GC calculation
assert(~isbad(F,false),'GC calculation failed');
% Significance test using theoretical null distribution, adjusting for multiple
% hypotheses.
pval = mvgc_pval(F,morder,nobs,ntrials,1,1,nvars-2,tstat); % take careful note of arguments!
sig = significance(pval,alpha,mhtc);
% Plot time-domain causal graph, p-values and significance.
%figure(2); clf;
%subplot(1,3,1);
%plot_pw(F);
%title('Pairwise-conditional GC');
%subplot(1,3,2);
%plot_pw(pval);
%title('p-values');
%subplot(1,3,3);
%plot_pw(sig);
%title(['Significant at p = ' num2str(alpha)])
mat = sig;
mat(1:(nvars+1):end) = 0;
% For good measure we calculate Seth's causal density (cd) measure - the mean
% pairwise-conditional causality. We don't have a theoretical sampling
% distribution for this.
cd = mean(F(~isnan(F)));
fprintf('\ncausal density = %f\n',cd);
%% Granger causality calculation: frequency domain (<mvgc_schema.html#3 |A14|>)
% Calculate spectral pairwise-conditional causalities at given frequency
% resolution - again, this only requires the autocovariance sequence.
ptic('\n*** autocov_to_spwcgc... ');
f = autocov_to_spwcgc(G,fres);
ptoc;
% Check for failed spectral GC calculation
assert(~isbad(f,false),'spectral GC calculation failed');
% Plot spectral causal graph.
%figure(3); clf;
%plot_spw(f,fs);
%% Granger causality calculation: frequency domain -> time-domain (<mvgc_schema.html#3 |A15|>)
% Check that spectral causalities average (integrate) to time-domain
% causalities, as they should according to theory.
fprintf('\nchecking that frequency-domain GC integrates to time-domain GC... \n');
Fint = smvgc_to_mvgc(f); % integrate spectral MVGCs
mad = maxabs(F-Fint);
madthreshold = 1e-5;
if mad < madthreshold
fprintf('maximum absolute difference OK: = %.2e (< %.2e)\n',mad,madthreshold);
diffCheck = 0;
else
fprintf(2,'WARNING: high maximum absolute difference = %e.2 (> %.2e)\n',mad,madthreshold);
diffCheck = mad - madthreshold;
end
diagnostics = [acminlags-acmaxlags, morder, diffCheck];
%%
% <mvgc_demo.html back to top>