-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv.m
231 lines (213 loc) · 8.23 KB
/
cv.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
function [lambda_opt, d_opt, h_opt, cv_err_1, cv_err_2, cv_err_3, rtime] = cv(nfold, ...
t, h, d, phi, lambda, p, l, time, m, n, w, kernel_type, options)
% V-fold cross validation
% INPUTS
% nfold: number of folds for cv
% t: a sequence of time points to be estimated
% h, d, lambda: candidate hyperparameters
%
% OUTPUTS
% lambda_opt, d_opt: vectors of the same length as t
% h_opt: a scalar
t1 = clock;
%% Tune lambda specifically. One lambda for each estimated time point.
fprintf('================================\n');
fprintf(' Tune lambda \n');
fprintf('--------------------------------\n');
cv_err = zeros(length(t), length(lambda), nfold);
for fold = 1:nfold
% construct training data and validation data
a = fold:nfold:length(time);
b = setdiff(1:length(time), a);
t_phi = phi(b);
v_phi = phi(a);
t_time = time(b);
v_time = time(a);
t_n = n(b);
v_n = n(a);
for i = 1:length(t)
fprintf('fold = %.0f, t = %.4f \n', fold, t(i))
% fit model to get graph topology using t_phi
adjacency = local_tvgm(t(i), median(h), median(d), t_phi, ...
lambda, p, l, t_time, m, t_n, w, kernel_type, options);
% refit model to eliminate over-shrinkage
for j = 1:length(lambda)
[solution, t_mu, t_sigma] = refit(t(i), adjacency{j}, ...
t_phi, l, t_time, m, p, median(h), t_n, ...
kernel_type, options);
% standardize v_phi and l
s_phi = v_phi;
s_l = l;
for r = 1:sum(m)
for k = 1:length(v_time)
s_phi{k}(:,r) = (v_phi{k}(:,r) - t_mu(r)) ./ t_sigma(r);
end
end
for r = 1:p
[r_lower, r_upper] = getindex(m, r);
s_l(r_lower:r_upper) = l(r_lower:r_upper) ./ (max(t_sigma(r_lower:r_upper))^2);
end
% construct v_Sigma using min(h)
D = diag(s_l);
weight = zeros(length(v_time), 1);
for k = 1:length(v_time)
weight(k) = v_n(k)*kernel(kernel_type, min(h), v_time(k), t(i));
end
weight = weight ./ sum(weight);
H = zeros(sum(m), sum(m));
for k = 1:length(v_time)
H = H + weight(k) .* (s_phi{k}'*s_phi{k}./v_n(k));
end
H = H + D;
mu0 = zeros(sum(m), 1);
for k = 1:length(v_time)
mu0 = mu0 + weight(k) .* (s_phi{k}'*ones(v_n(k),1)./v_n(k));
end
v_Sigma = [1, mu0'; mu0, H]; % sum(m)+1 by sum(m)+1 matrix
% calculate cv error
cv_err(i,j,fold) = trace(solution*v_Sigma) - log(det(solution));
end
end
end
cv_err_1 = mean(cv_err, 3);
[~, ind] = min(cv_err_1, [], 2);
lambda_opt = lambda(ind);
%% Tune d specifically for each estimated time point.
fprintf('================================\n');
fprintf(' Tune d \n');
fprintf('--------------------------------\n');
cv_err = zeros(length(t), length(d), nfold);
for fold = 1:nfold
% construct training data and validation data
a = fold:nfold:length(time);
b = setdiff(1:length(time), a);
t_phi = phi(b);
v_phi = phi(a);
t_time = time(b);
v_time = time(a);
t_n = n(b);
v_n = n(a);
for i = 1:length(t)
for j = 1:length(d)
fprintf('fold = %.0f, t = %.4f \n', fold, t(i))
if sum(abs(t_time-t(i)) <= d(j)) > 0
% fit model to get graph topology using t_phi
adjacency = local_tvgm(t(i), median(h), d(j), t_phi, ...
lambda_opt(i), p, l, t_time, m, t_n, w, kernel_type, options);
% refit model to eliminate over-shrinkage
[solution, t_mu, t_sigma] = refit(t(i), adjacency{1}, ...
t_phi, l, t_time, m, p, median(h), t_n, ...
kernel_type, options);
% standardize v_phi and l
s_phi = v_phi;
s_l = l;
for r = 1:sum(m)
for k = 1:length(v_time)
s_phi{k}(:,r) = (v_phi{k}(:,r) - t_mu(r)) ./ t_sigma(r);
end
end
for r = 1:p
[r_lower, r_upper] = getindex(m, r);
s_l(r_lower:r_upper) = l(r_lower:r_upper) ./ (max(t_sigma(r_lower:r_upper))^2);
end
% construct v_Sigma using min(h)
D = diag(s_l);
weight = zeros(length(v_time), 1);
for k = 1:length(v_time)
weight(k) = v_n(k)*kernel(kernel_type, min(h), v_time(k), t(i));
end
weight = weight ./ sum(weight);
H = zeros(sum(m), sum(m));
for k = 1:length(v_time)
H = H + weight(k) .* (s_phi{k}'*s_phi{k}./v_n(k));
end
H = H + D;
mu0 = zeros(sum(m), 1);
for k = 1:length(v_time)
mu0 = mu0 + weight(k) .* (s_phi{k}'*ones(v_n(k),1)./v_n(k));
end
v_Sigma = [1, mu0'; mu0, H]; % sum(m)+1 by sum(m)+1 matrix
% calculate cv error
cv_err(i,j,fold) = trace(solution*v_Sigma) - log(det(solution));
else
cv_err(i,j,fold) = inf;
end
end
end
end
cv_err_2 = mean(cv_err, 3);
[~, ind] = min(cv_err_2, [], 2);
d_opt = d(ind);
%% Tune a common h for all estimated time points
fprintf('================================\n');
fprintf(' Tune h \n');
fprintf('--------------------------------\n');
cv_err = zeros(length(t), length(h), nfold);
for fold = 1:nfold
% construct training data and validation data
a = fold:nfold:length(time);
b = setdiff(1:length(time), a);
t_phi = phi(b);
v_phi = phi(a);
t_time = time(b);
v_time = time(a);
t_n = n(b);
v_n = n(a);
for i = 1:length(t)
for j = 1:length(h)
fprintf('fold = %.0f, t = %.4f \n', fold, t(i))
% fit model to get graph topology using t_phi
adjacency = local_tvgm(t(i), h(j), d_opt(i), t_phi, ...
lambda_opt(i), p, l, t_time, m, t_n, w, kernel_type, options);
% refit model to eliminate over-shrinkage
[solution, t_mu, t_sigma] = refit(t(i), adjacency{1}, ...
t_phi, l, t_time, m, p, h(j), t_n, ...
kernel_type, options);
% standardize v_phi and l
s_phi = v_phi;
s_l = l;
for r = 1:sum(m)
for k = 1:length(v_time)
s_phi{k}(:,r) = (v_phi{k}(:,r) - t_mu(r)) ./ t_sigma(r);
end
end
for r = 1:p
[r_lower, r_upper] = getindex(m, r);
s_l(r_lower:r_upper) = l(r_lower:r_upper) ./ (max(t_sigma(r_lower:r_upper))^2);
end
% construct v_Sigma using min(h)
D = diag(s_l);
weight = zeros(length(v_time), 1);
for k = 1:length(v_time)
weight(k) = v_n(k)*kernel(kernel_type, min(h), v_time(k), t(i));
end
weight = weight ./ sum(weight);
H = zeros(sum(m), sum(m));
for k = 1:length(v_time)
H = H + weight(k) .* (s_phi{k}'*s_phi{k}./v_n(k));
end
H = H + D;
mu0 = zeros(sum(m), 1);
for k = 1:length(v_time)
mu0 = mu0 + weight(k) .* (s_phi{k}'*ones(v_n(k),1)./v_n(k));
end
v_Sigma = [1, mu0'; mu0, H]; % sum(m)+1 by sum(m)+1 matrix
% calculate cv error
cv_err(i,j,fold) = trace(solution*v_Sigma) - log(det(solution));
end
end
end
fprintf('--------------------------------\n');
t2 = clock;
rtime = etime(t2, t1);
cv_err = mean(cv_err, 3);
cv_err_3 = mean(cv_err, 1);
[~, ind] = min(cv_err_3);
h_opt = h(ind);
end
%% column indices of sufficient statistics of node m
function [lower, upper] = getindex(m, node)
% get the index corresponding to each variable
lower = sum(m(1:node)) - m(node) + 1;
upper = sum(m(1:node));
end