The MVGC Multivariate Granger Causality Matlab初上手记录

介绍

环境要求

根据作者的HELP-system requirement介绍,该ToolBox是在Linux x86_64 (glnxa64) 系统里的 Matlab R2011a中使用的,不确定能毫无问题的在其他环境的matlab跑起来。
Matlab® Toolboxes依赖:
Statistics Toolbox™
Signal Processing Toolbox™ - 作者说少这个也不影响重要功能的实现
Control System Toolbox™

常见变量名称和数据结构

HELP-Common variable names and data structures
n,nvars 变量的个数
m,nobs number of observations (time steps)
N,ntrials number of trials (realisations) of a process
p,q,… 滞后阶数、 frequencies
x,y,z,… vector of sub-variables indices
fs sample rate (e.g. in Hz)
fres frequency resolution
X,Y,Z,… Time series data: an n x m matrix for single-trial, or an
n x m x N matrix for multi-trial data. The first index
references variables, the second observations and the third (if
present) trials.
A VAR coefficients: an n x n x p matrix containing coefficients
for an n-variable VAR§. The first index references target
(“to”) variables, the second source (“from”) variables and the
third lags.
SIG Residuals covariances: and n x n symmetric positive-definite
matrix containg residuals covariances for an n-variable VAR.
G Autocovariance sequence: an n x n x (q+1) matrix containing
autocovariances of a time series up to q lags. The first index
references target (“to”) variables, the second source (“from”)
variables and the third lags. The k-th block G(:,:,k)
corresponds to k-1 lags; the first block G(:,:,1) is thus a
covariance matrix.
S Cross-power spectral density (cpsd): an n x n x (q+1) matrix
containing cpsds of a time series at a resolution of q. The
first index references target (“to”) variables, the second
source (“from”) variables and the third frequency. Frequencies
generally run from zero up to (and including) the Nyqvist
frequency; i.e. block S(:,:,1) corresponds to frequency zero
and block S(:,:,q+1) corresponds to the Nyqvist frequency.
H VAR transfer function: an n x n x (q+1) matrix containing
transfer function components of a VAR at a resolution of q. The
first index references target (“to”) variables, the second
source (“from”) variables and the third frequency.
F Time-domain multivariate Granger causality: for the pairwise-
conditional case, an n x n matrix containing pairwise-
conditional causalities; the first index then references target
(“to”) variables and the second source (“from”) variables, with
NaNs on the diagonal.
f Frequency-domain (spectral) multivariate Granger causality: for
the pairwise-conditional case, an n x n x (q+1) matrix
containing pairwise-conditional spectral causalities; the first
index references target (“to”) variables, the second source
(“from”) variables and the third frequency, with NaNs on the
diagonal for each frequency block f(:,:,k).

Miscellaneous issues

HELP-Miscellaneous issues
作者记录了四个issue,稳定性、数据预处理、VAR建模、GCCA兼容模式
在数据预处理中,作者推荐使用其他软件进行数据预处理,当然,作者也对此进行了温馨提示,数据预处理有可能破坏数据之间的Granger Causality

C code

ToolBox的大部分功能都是由Matlab code编写的,处于效率上的问题,仅极少部分使用了C来编写。

Get start

我把从github上下载的zip包解压后拷贝的Matlab的ToolBox下(我拷贝到别的路径下也能run。。),然后运行startup.m,结果如下

>> startup
[mvgc startup] Initialising MVGC toolbox version 1.0
警告: 名称不存在或不是目录: D:\Program Files\MATLAB\R2017b\toolbox\MVGC1-1.0\testing 
> In path (line 109)
  In addpath (line 94)
  In startup (line 46) 
[mvgc startup] Added MVGC root directory D:\Program Files\MATLAB\R2017b\toolbox\MVGC1-1.0 and subdirectories to path
[mvgc startup] All MVGC 'mex' files for your platform exist
[mvgc startup] Statistics Toolbox(TM) seems to be present.
[mvgc startup] Signal Processing Toolbox(TM) seems to be present.
[mvgc startup] Control System Toolbox(TM) seems to be present.
[mvgc startup] Random number generator initialised
[mvgc startup] All warnings enabled
[mvgc startup] Initialisation complete (you may re-run 'startup' at any time)
[mvgc startup] Type 'helpon' to get started
>> 

看样子这个MVGC工具包依赖其它三个工具包都安装了(这个后来在MVGC的get start中才看到。。)、
还好安装的时候没有取消太多钩钩(硬盘太小不敢放肆)

demo记录

随后把自带的demo都跑了一遍,除了一个GCCA提示了NOTE,其它跑起来都挺正常的

1. mvgc_demo.m

估计VAR模型并计算时域和频域成对条件Granger因果关系百分比(也称为“因果图”)。
计算Seth的因果密度度量。

>> mvgc_demo

*** var_to_tsdata... 0.130861 secs

*** tsdata_to_infocrit
model order = 1
model order = 2
model order = 3
model order = 4
model order = 5
model order = 6
model order = 7
model order = 8
model order = 9
model order = 10
model order = 11
model order = 12
model order = 13
model order = 14
model order = 15
model order = 16
model order = 17
model order = 18
model order = 19
model order = 20
*** tsdata_to_infocrit took 0.500980 secs

best model order (AIC) = 3
best model order (BIC) = 3
actual model order     = 3

using AIC best model order = 3

*** tsdata_to_var... 0.027967 secs
*** var_to_autocov... 0.046133 secs

VAR info:
    no errors
    no warnings
    spectral radius   : 0.951091
    ac relative error : 1.36065e-13
    minimum ac lags   : 368
    actual  ac lags   : 368

*** autocov_to_pwcgc... 0.777737 secs

causal density = 0.069997

*** autocov_to_spwcgc... 1.736699 secs

checking that frequency-domain GC integrates to time-domain GC... 
maximum absolute difference OK: = 1.57e-14 (< 1.00e-05)
>> 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. mvgc_demo_bootstrap.m

cpu占用99%,耗时最久的demo

在具有已知因果结构的5节点网络上,使用非参数引导程序对生成的VAR数据使用非参数引导来构造置信区间(请参见<var5_test.html | var5_test |>)。 估计成对条件的格兰杰因果关系,并使用理论分布和自举分布来构造置信区间。

>> mvgc_demo_bootstrap

*** var_to_tsdata... 0.010878 secs
*** tsdata_to_var... 0.044584 secs
*** var_to_autocov... 0.009415 secs

VAR check:
        error: 0
       errmsg: ''
     warnings: 0
      warnmsg: {
    
    0×1 cell}
          rho: 0.9451
        iters: NaN
     acrelerr: 1.8486e-13
    acminlags: 326
       aclags: 326

*** autocov_to_pwcgc... 0.742574 secs

*** bootstrap_tsdata_to_pwcgc
PWCGC: bootstrap sample 1 of 100
PWCGC: bootstrap sample 2 of 100
PWCGC: bootstrap sample 3 of 100
PWCGC: bootstrap sample 4 of 100
PWCGC: bootstrap sample 5 of 100
。。。。。。
PWCGC: bootstrap sample 98 of 100
PWCGC: bootstrap sample 99 of 100
PWCGC: bootstrap sample 100 of 100
*** bootstrap_tsdata_to_pwcgc took 41.85 secs

在这里插入图片描述

3. mvgc_demo_GCCA.m

NOTE: no frequency-domain pairwise-conditional causality calculation in GCCA compatibility mode!
关于这个Note,见参考二 - MVGC “GCCA compatibility mode” demo
演示在“ GCCA兼容模式”下使用MVGC工具箱的情况; 看到
帮助文档中的<mvgchelp.html#6其他问题>。 这部分是为了使格兰杰因果连通性分析(<http://www.sussex.ac.uk/Users/anils/aks_code.htm GCCA>)工具箱的以前的用户受益,部分是为了实现 Granger因果关系计算的一种更“传统”的方法。 主要区别在于,这里有两个独立的VAR回归-_full_和_reduced_回归(请参见[1])- 明确执行(请参见<GCCA_tsdata_to_pwcgc.html| GCCA_tsdata_to_pwcgc |>),而不是MVGC Toolbox首选方法(请参见<mvgc_demo.html | mvgc_demo |>),该方法仅需要完全回归,因此更加灵活且在数值上更准确。

>> mvgc_demo_GCCA

*** var_to_tsdata... 0.009682 secs

*** tsdata_to_infocrit
model order = 1
model order = 2
model order = 3
...
model order = 20
*** tsdata_to_infocrit took 0.398898 secs

best model order (AIC) = 3
best model order (BIC) = 3
actual model order     = 3

using AIC best model order = 3

*** GCCA_tsdata_to_pwcgc... 0.110287 secs

spectral radius = 0.953989

NOTE: no frequency-domain pairwise-conditional causality calculation in GCCA compatibility mode!

在这里插入图片描述
在这里插入图片描述

4. mvgc_demo_non-stationarity.m

通过对多试验数据进行“垂直”(窗口式)回归,针对具有线性趋势和正弦变化因果系数的最小2变量VAR,演示了针对非平稳时间序列的MVGC工具箱的用法。 数据是使用<var_to_tsdata_nonstat.html | var_to_tsdata_nonstat |>例程生成a的。

>> mvgc_demo_nonstationary
window 1 of 100 at time = 5
window 2 of 100 at time = 15
window 3 of 100 at time = 25
...
window 27 of 100 at time = 265
window 28 of 100 at time = 275
window 29 of 100 at time = 285
window 30 of 100 at time = 295
window 31 of 100 at time = 305
...
window 44 of 100 at time = 435
window 45 of 100 at time = 445
window 46 of 100 at time = 455
window 47 of 100 at time = 465
window 48 of 100 at time = 475
....
window 68 of 100 at time = 675
window 69 of 100 at time = 685
window 70 of 100 at time = 695
window 71 of 100 at time = 705
window 72 of 100 at time = 715
...
window 87 of 100 at time = 865
window 88 of 100 at time = 875
...
window 99 of 100 at time = 985
window 100 of 100 at time = 995

在这里插入图片描述

5. mvgc_demo_permutation.m

演示了使用MVGC对具有已知因果结构的5节点网络生成的VAR数据进行排列重要性测试(请参见<var5_test.html | var5_test |>)。 估计成对条件的格兰杰因果关系,并使用理论检验和置换检验零分布进行检验。 将所有对的理论和置换检验零分布一起绘制,并使用Kolmogorov-Smirnov检验进行比较(请参见<matlab:doc(‘kstest’)| kstest |>)。

>> mvgc_demo_permtest

*** var_to_tsdata... 0.018087 secs
*** tsdata_to_var... 0.011904 secs
*** var_to_autocov... 0.008961 secs

VAR check:
        error: 0
       errmsg: ''
     warnings: 0
      warnmsg: {
    
    0×1 cell}
          rho: 0.9522
        iters: NaN
     acrelerr: 2.5316e-13
    acminlags: 376
       aclags: 376

*** autocov_to_pwcgc... 0.867992 secs

*** tsdata_to_mvgc_pwc_permtest
WARNING: truncating sequence length by 1 observations
PWCGC from node 1: permutation test sample 1 of 100
PWCGC from node 1: permutation test sample 2 of 100
PWCGC from node 1: permutation test sample 3 of 100
PWCGC from node 1: permutation test sample 4 of 100
PWCGC from node 1: permutation test sample 5 of 100
。。。
PWCGC from node 5: permutation test sample 98 of 100
PWCGC from node 5: permutation test sample 99 of 100
PWCGC from node 5: permutation test sample 100 of 100
*** tsdata_to_mvgc_pwc_permtest took 40.97 secs

KS-test p-values:

       NaN    0.8377    0.3781    0.1605    0.5109
    0.5053       NaN    0.1420    0.8190    0.8267
    0.3470    0.0777       NaN    0.8369    0.9061
    0.1963    0.9024    0.0850       NaN    0.8437
    0.0986    0.7942    0.1041    0.1753       NaN

KS significance test; ones indicates that permutation and theoretical
null distributions are significantly different at p-value 0.05; 

   NaN     0     0     0     0
     0   NaN     0     0     0
     0     0   NaN     0     0
     0     0     0   NaN     0
     0     0     0     0   NaN

在这里插入图片描述
在这里插入图片描述

6. mvgc_demo_stats.m

展示MVGC工具箱时间序列统计和频谱分析工具

>> mvgc_demo_stats

*** var_to_tsdata... 0.011458 secs

*** tsdata_to_infocrit
model order = 1
model order = 2
model order = 3
。。。
model order = 20
*** tsdata_to_infocrit took 0.394318 secs

best model order (AIC) = 3
best model order (BIC) = 3
actual model order     = 3

using AIC best model order = 3

*** tsdata_to_var... 0.008802 secs
*** var_to_autocov... 0.005034 secs

VAR info:
    no errors
    no warnings
    spectral radius   : 0.951203
    ac relative error : 2.06665e-13
    minimum ac lags   : 369
    actual  ac lags   : 369

*** autocov_to_cpsd... 0.007286 secs
*** tsdata_to_cpsd... 2.215061 secs

Durbin-Watson statistics =
    1.9987    2.0005    1.9978    2.0001    1.9998

all residuals are white by Durbin-Watson test at significance 0.05

RSQ (adjusted) =
    0.9106    0.7345    0.6406    0.8496    0.5783

adjusted r-squares OK: > 30% of variance is accounted for by the model

model consistency = 92%
consistency OK: > 80%
>>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

References

猜你喜欢

转载自blog.csdn.net/weixin_43031092/article/details/109059833