-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbt_causality.m
80 lines (59 loc) · 1.71 KB
/
dbt_causality.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
function [DSS,SSR,DTF,dbxpast,dbxfut] = dbt_causality(dbx,varargin)
% This is an experimental application of causal modeling using a crude
% but efficient factorization.
make_plot = true;
if ~dbx.remodphase
error('dbx.remodphase must be TRUE');
end
B = dbx.blrep;
FB = ifft(B,[],2);
nfr = length(dbx.frequency);
w = ifftshift((0:nfr-1)-floor(.5*nfr))/nfr;
Bpast = FB;
Bpast(:,w>=0,:) = 0;
Bfut = FB;
Bfut(:,w<=0,:) = 0;
dbxpast = dbx;
dbxpast.blrep = fft(Bpast,[],2);
dbxfut = dbx;
dbxfut.blrep = fft(Bfut,[],2);
DTF = dbtcoh(dbxpast,dbxfut);
for k = 1:length(dbx.frequency);
Y = squeeze(dbxfut.blrep(:,k,:));
X = squeeze(dbxpast.blrep(:,k,:));
ssY = sum(abs(Y).^2);
ssX = sum(abs(X).^2);
npar = size(X,2);
for kk = 1:npar+1;
getw = (1:npar)~=kk-1;
[glm(k,kk).b,glm(k,kk).dev,glm(k,kk).pval,glm(k,kk).iXX,glm(k,kk).sigma,glm(k,kk).res,glm(k,kk).Yfit] = complexglm(Y,X(:,getw),'intercept',true);
ssR = diag(ssX)*abs(glm(k,kk).b(1:size(ssX,2),:)).^2*diag(ssY.^-1);
glm(k,kk).ssR =ssR;
glm(k,kk).ssRes = sum(abs(glm(k,kk).res).^2);
glm(k,kk).dSSR = (glm(k,kk).ssRes-glm(k,1).ssRes)./ssY;
end% if any(ssR(:)>1)
% keyboard
% end
k
end
for k =1:npar
DSS(:,:,k) = cat(1,glm(:,k+1).dSSR);
DSS(:,k,k) = 0;
end
SSR = cat(3,glm(:,1).ssR);
if make_plot
figure
pln = 1;
yl = minmax(DSS(:));
for k = 1:npar
for kk = 1:npar
if k~=kk
subplot(npar,npar,pln)
plot(dbx.frequency,squeeze(DSS(:,kk,k)))
title(sprintf('%i -> %i',k,kk))
ylim(yl)
end
pln = pln+1;
end
end
end