-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhoi_exhaustive_loop_lagged_all.m
291 lines (248 loc) · 11.6 KB
/
hoi_exhaustive_loop_lagged_all.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
function [Otot_lagged, O_val_size_tot_lagged] = hoi_exhaustive_loop_lagged_all(ts, modelorder, maxsize, biascorrection, groups)
% ts: input (observations x variables), time series or static/behavioral data
% model order:
% maxsize: max number of drivers in the multiplet (i.e. size 2 means a multipet of 3 variables,
% the target and two drivers). This is different from the zero lag version
% biascorrection: apply or not bias correction for entropy calculation
% groups: if you want to constrain the search to multiplets of variables belonging to different groups,
% provide a vector of length equal to the number of variables, whose entries are the group assignment
% of each variable
X = copnorm(ts);
[N, nvartot] = size(X); % matrix size
%% Parameters
nboot = 1000; % number of bootstrap samples
alphaval = .05;
chunklength = round(N/5); % can play around with this
Otot_lagged(nvartot,maxsize) = struct('index_var_red', [], 'sorted_red', [], 'index_red', [], 'bootsig_red', [], 'bootsigCI_red', [], ...
'index_var_syn', [], 'sorted_syn', [], 'index_syn', [], 'bootsig_syn', [], 'bootsigCI_syn', []);
O_val_size_tot_lagged(nvartot,maxsize) = struct('multiplet_value',[]);
if nargin<5
groups = ones(nvartot,1);
end
pathTmp = pwd;
%% create data for CI estimation by Bootstrap
[outBoot, outJack] = hoi_createBootsData_lagged(X,chunklength,modelorder,nboot,pathTmp);
%% this section is for the expansion of redundancy, so maximizing the O
% there's no need to fix the target here
load(outBoot, 'covBootst'); % varible: covBootst
isLoadBoot = false;
for itarget = 1:nvartot
for isize = 2:maxsize
boolPos = false; boolNeg = false;
C = nchoosek(setdiff(1:nvartot,itarget),isize);
if max(groups)>1
C = select_combinations(C,groups);
end
ncomb = size(C,1);
O_val_size_lagged = zeros(ncomb,1);
XX = covBootst(:,:,1);
%----- for bias correction
if biascorrection
Ntrl = N-modelorder;
Nvarxyz = size(C,2)*modelorder+modelorder+1;
psiterms = psi((Ntrl - (1:Nvarxyz))/2) / 2;
ln2 = log(2);
dterm = (ln2 - log(Ntrl-1)) / 2;
else
psiterms = [];
dterm = [];
end
%------
parfor icomb = 1:ncomb
O_val_size_lagged(icomb) = hoi_o_information_lagged_boot(XX,itarget,C(icomb,:),modelorder, ...
biascorrection,psiterms,dterm);
end
O_val_size_tot_lagged(itarget,isize).multiplet_val=O_val_size_lagged; % here we save all the values
% select positive and negative values
ind_pos = find(O_val_size_lagged > 0);
ind_neg = find(O_val_size_lagged < 0);
O_pos = O_val_size_lagged(O_val_size_lagged>0);
O_neg = O_val_size_lagged(O_val_size_lagged<0);
[Osort_pos, ind_pos_sort] = sort(O_pos,'descend');
[Osort_neg, ind_neg_sort] = sort(O_neg);
if ~isempty(Osort_pos)
n_pos = length(Osort_pos);
end
if ~isempty(Osort_neg)
n_neg = length(Osort_neg);
end
if ~isempty(Osort_pos) && boolPos==false
boot_sig_pos = zeros(n_pos,1); p_pos = boot_sig_pos;
boolPos = true;
end
if ~isempty(Osort_neg) && boolNeg==false
boot_sig_neg = zeros(n_neg,1); p_neg = boot_sig_neg;
boolNeg = true;
end
% compute bootstrapped measure for CI estimation
if (boolPos || boolNeg) && isLoadBoot==false
covJack = [];
load(outJack, 'covJack'); % variable: covJack
isLoadBoot = true;
end
%----- for bias correction\
if biascorrection
Ntrl = N-1-modelorder;
Nvarxyz = size(C,2)*modelorder+modelorder+1;
psiterms = psi((Ntrl - (1:Nvarxyz))/2) / 2;
ln2 = log(2);
dterm = (ln2 - log(Ntrl-1)) / 2;
else
psiterms = [];
dterm = [];
end
%-----
if boolPos
ci = zeros(n_pos,2);
icount_pos=0;
for isel = 1:n_pos
bstats_pos = zeros(nboot,1);
indvar = C(ind_pos(ind_pos_sort(isel)),:);
for iboot=1:nboot
bstats_pos(iboot) = hoi_o_information_lagged_boot(covBootst(:,:,iboot+1),itarget, indvar,...
modelorder, biascorrection,psiterms,dterm);
end
p_pos(isel) = (1+sum(bstats_pos(:,1)<0)) / (nboot+1);
% jackniffe
z_0 = fz0(bstats_pos,Osort_pos(isel));
jstat = zeros(N,1);
parfor i = 1:N
jstat(i) = hoi_o_information_lagged_boot(covJack(:,:,i),itarget,indvar,modelorder,biascorrection,psiterms,dterm);
end
ci(isel,:) = bootci_jack(N,jstat,alphaval,z_0,Osort_pos(isel),bstats_pos);
boot_sig_pos(isel) = ~((ci(isel,1)<0) && (ci(isel,2)>0));
h = fdr_bh(p_pos(1:isel));
if h(end)==0
icount_pos=icount_pos+1;
else
icount_pos=0;
end
if icount_pos>10
break;
end
end
htot = zeros(n_pos,1);
htot(1:isel) = h;
Otot_lagged(itarget,isize).index_var_red = C(ind_pos(ind_pos_sort(1:n_pos)),:);
Otot_lagged(itarget,isize).sorted_red = Osort_pos(1:n_pos);
Otot_lagged(itarget,isize).index_red = ind_pos(ind_pos_sort(1:n_pos));
Otot_lagged(itarget,isize).bootsig_red = htot.*boot_sig_pos;
Otot_lagged(itarget,isize).bootsigCI_red = ci(1:n_pos,:);
end
if boolNeg
ci = zeros(n_neg,2);
icount_neg=0;
for isel = 1:n_neg
bstats_neg = zeros(nboot,1);
indvar = C(ind_neg(ind_neg_sort(isel)),:);
for iboot=1:nboot
bstats_neg(iboot) = hoi_o_information_lagged_boot(covBootst(:,:,iboot+1),itarget, indvar,...
modelorder, biascorrection,psiterms,dterm);
end
p_neg(isel) = (1+sum(bstats_neg>0)) / (nboot+1);
% jackniffe
z_0 = fz0(bstats_neg,Osort_neg(isel));
jstat = zeros(N,1);
parfor i = 1:N
jstat(i) = hoi_o_information_lagged_boot(covJack(:,:,i),itarget,indvar,modelorder,biascorrection,psiterms,dterm);
end
ci(isel,:) = bootci_jack(N,jstat,alphaval,z_0,Osort_neg(isel),bstats_neg);
boot_sig_neg(isel) = ~((ci(isel,1)<0) && (ci(isel,2)>0));
h = fdr_bh(p_neg(1:isel));
if h(end)==0
icount_neg=icount_neg+1;
else
icount_neg=0;
end
if icount_neg>10
break
end
end
htot = zeros(n_neg,1);
htot(1:isel) = h;
Otot_lagged(itarget,isize).index_var_syn = C(ind_neg(ind_neg_sort(1:n_neg)),:);
Otot_lagged(itarget, isize).sorted_syn = Osort_neg(1:n_neg);
Otot_lagged(itarget,isize).index_syn = ind_neg(ind_neg_sort(1:n_neg));
Otot_lagged(itarget,isize).bootsig_syn = htot.*boot_sig_neg(1:n_neg);
Otot_lagged(itarget,isize).bootsigCI_syn = ci(1:n_neg,:);
end
end
end
for itarget = 1:nvartot
for isize = 1:maxsize
if ~isempty(Otot_lagged(itarget,isize).bootsig_red)
Otot_lagged(itarget,isize).index_var_red(Otot_lagged(itarget,isize).bootsig_red==0,:)=[];
Otot_lagged(itarget,isize).sorted_red(Otot_lagged(itarget,isize).bootsig_red==0)=[];
Otot_lagged(itarget,isize).index_red(Otot_lagged(itarget,isize).bootsig_red==0)=[];
Otot_lagged(itarget,isize).bootsigCI_red(Otot_lagged(itarget,isize).bootsig_red==0,:)=[];
Otot_lagged(itarget,isize).bootsig_red(Otot_lagged(itarget,isize).bootsig_red==0)=[];
end
if ~isempty(Otot_lagged(itarget,isize).bootsig_syn)
Otot_lagged(itarget,isize).index_var_syn(Otot_lagged(itarget,isize).bootsig_syn==0,:)=[];
Otot_lagged(itarget,isize).sorted_syn(Otot_lagged(itarget,isize).bootsig_syn==0)=[];
Otot_lagged(itarget,isize).index_syn(Otot_lagged(itarget,isize).bootsig_syn==0)=[];
Otot_lagged(itarget,isize).bootsigCI_syn(Otot_lagged(itarget,isize).bootsig_syn==0,:)=[];
Otot_lagged(itarget,isize).bootsig_syn(Otot_lagged(itarget,isize).bootsig_syn==0)=[];
end
end
end
% and now flag the multiplets which don't have a significant increase of
% info with respect to their lower order composants
Otot_lagged = find_carryover_significance_lagged(Otot_lagged);
for itarget = 1:nvartot
for isize = 4:maxsize
if ~isempty(Otot_lagged(isize).inc_sig_red)
Otot_lagged(itarget,isize).index_var_red(Otot_lagged(isize).inc_sig_red==0,:)=[];
Otot_lagged(itarget,isize).sorted_red(Otot_lagged(isize).inc_sig_red==0)=[];
Otot_lagged(itarget,isize).index_red(Otot_lagged(isize).inc_sig_red==0)=[];
Otot_lagged(itarget,isize).bootsigCI_red(Otot_lagged(isize).inc_sig_red==0,:)=[];
Otot_lagged(itarget,isize).bootsig_red(Otot_lagged(isize).inc_sig_red==0)=[];
Otot_lagged(itarget,isize).inc_sig_red(Otot_lagged(isize).inc_sig_red==0)=[];
end
if ~isempty(Otot_lagged(isize).inc_sig_syn)
Otot_lagged(itarget,isize).index_var_syn(Otot_lagged(isize).inc_sig_syn==0,:)=[];
Otot_lagged(itarget,isize).sorted_syn(Otot_lagged(isize).inc_sig_syn==0)=[];
Otot_lagged(itarget,isize).index_syn(Otot_lagged(isize).inc_sig_syn==0)=[];
Otot_lagged(itarget,isize).bootsigCI_syn(Otot_lagged(isize).inc_sig_syn==0,:)=[];
Otot_lagged(itarget,isize).bootsig_syn(Otot_lagged(isize).inc_sig_syn==0)=[];
Otot_lagged(itarget,isize).inc_sig_syn(Otot_lagged(isize).inc_sig_syn==0)=[];
end
end
end
end
%% internal functions
function ci = bootci_jack(N,jstat,alpha,z_0,stat,bstat) % from matlab bootci function
weights = repmat(1/N,N,1);
% acceleration finding, see DiCiccio and Efron (1996)
mjstat = sum(bsxfun(@times,jstat,weights),1); % mean along 1st dim.
score = bsxfun(@minus,mjstat,jstat); % score function at stat; ignore (N-1) factor because it cancels out in the skew
iszer = all(score==0,1);
skew = sum(bsxfun(@times,score.^3,weights),1) ./ ...
(sum(bsxfun(@times,score.^2,weights),1).^1.5) /sqrt(N); % skewness of the score function
skew(iszer) = 0;
acc = skew/6; % acceleration
% transform back with bias corrected and acceleration
z_alpha1 = norminv(alpha/2);
z_alpha2 = -z_alpha1;
pct1 = 100*normcdf(z_0 +(z_0+z_alpha1)./(1-acc.*(z_0+z_alpha1)));
pct1(z_0==Inf) = 100;
pct1(z_0==-Inf) = 0;
pct2 = 100*normcdf(z_0 +(z_0+z_alpha2)./(1-acc.*(z_0+z_alpha2)));
pct2(z_0==Inf) = 100;
pct2(z_0==-Inf) = 0;
% inverse of ECDF
m = numel(stat);
lower = zeros(1,m);
upper = zeros(1,m);
for i = 1:m
lower(i) = prctile(bstat(:,i),pct2(i),1);
upper(i) = prctile(bstat(:,i),pct1(i),1);
end
% return
ci = sort([lower;upper],1);
end
% -------------------------
function z0 = fz0(bstat,stat) % from matlab bootci function
% Compute bias-correction constant z0
z0 = norminv(mean(bsxfun(@lt,bstat,stat),1) + mean(bsxfun(@eq,bstat,stat),1)/2);
end