-
Notifications
You must be signed in to change notification settings - Fork 2
/
sigact.m
298 lines (242 loc) · 8.37 KB
/
sigact.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
292
293
294
295
296
297
% add gpml package
gpml_path = "/Users/yahoo/Documents/WashU/CSE515T/Code/Gaussian Process/gpml-matlab-v3.6-2015-07-07";
addpath("model");
addpath("data");
addpath("../CNNForecasting/gpml-matlab-v3.6-2015-07-07");
addpath(gpml_path);
startup;
% set random seed
rng('default');
% load
sigacts = readtable("./data/sigacts_data.csv");
CATEGORY = ["Direct Fire"];
sigacts = sigacts(ismember(sigacts.category, CATEGORY),:);
% provinces adjacent to Pakistan
BORDER_PROVINCE = ["Nimroz",...
"Hilmand",...
"Kandahar",...
"Zabul",...
"Paktika",...
"Khost",...
"Nangarhar",...
"Nuristan",...
"Badakhshan",...
"Kunar"];
% set min/max date
date_min = datetime(2007,01,01, 'Format','yyyy-MM-dd');
date_max = datetime(2008,12,31, 'Format','yyyy-MM-dd');
treatment_date = datetime(2008,8,12, 'Format','yyyy-MM-dd');
treatment_day = caldays(between(date_min,treatment_date,'days')) + 1;
num_days = caldays(between(date_min,date_max,'days')) + 1;
sigacts = sigacts(sigacts.date<=date_max & sigacts.date>=date_min, :);
sigacts.border = ismember(sigacts.province, BORDER_PROVINCE);
sigacts=groupcounts(sigacts, {'border','date','category'});
treat = zeros(num_days,numel(CATEGORY));
control = zeros(num_days,numel(CATEGORY));
for t=date_min:date_max
i = caldays(between(date_min,t,'days')) + 1;
for j=1:numel(CATEGORY)
tmp = sigacts.GroupCount(sigacts.date==t & ...
strcmp(sigacts.category, CATEGORY(j)) & ...
sigacts.border==1);
if numel(tmp), treat(i, j) = tmp; end
tmp = sigacts.GroupCount(sigacts.date==t & ...
strcmp(sigacts.category, CATEGORY(j)) & ...
sigacts.border==0);
if numel(tmp), control(i, j) = tmp; end
end
end
% data is:
% 1: day number
% 2: group id: 1 for control, 2 for treat
% 3: day number (set to zero for task 1, used for drift process)
x = [(1:num_days), (1:num_days);...
ones(1,num_days), ones(1,num_days)*2;...
(1:num_days),(1:num_days)]';
x(x(:, 2) == 1, end) = 0;
y = [control; treat];
% init hyperparameter and define model
group_length_scale = 100;
group_output_scale = 0.5;
mean_std = 0.5;
treat_length_scale = 30;
treat_output_scale = 0.5;
rho = 0.0;
meanfunction = {@meanMask, [false, true, false], {@meanDiscrete, 2}}; % constant mean
tmp=y(x(:,2)==1);
theta.mean = [mean(log(tmp(tmp~=0)))];
tmp=y(x(:,2)==2);
theta.mean = [theta.mean, mean(log(tmp(tmp~=0)))]; % mean of log
% time covariance for group trends
time_covariance = {@covMask, {1, {@covSEiso}}};
theta.cov = [log(group_length_scale); ... % 1
log(group_output_scale)]; % 2
% inter-group covariance for group trends
inter_group_covariance = {@covMask, {2, {@covDiscrete2}}};
theta.cov = [theta.cov; ...
norminv((rho + 1) / 2)]; % 3
% complete group trend covariance
group_trend_covariance = {@covProd, {time_covariance, inter_group_covariance}};
% marginalize group mean
mean_covariance = {@covMask, {2, {@covSEiso}}};
theta.cov = [theta.cov; ...
log(0.01); ... % 4
log(mean_std)]; % 5
% treatment effect
treatment_effect_covariance = ...
{@covMask, {3, {@scaled_covariance, {@scaling_function}, {@covSEiso}}}};
theta.cov = [theta.cov; ...
treatment_day; ... % 6
30; ... % 7
log(treat_length_scale); ...% 8
log(treat_output_scale)]; % 9
covfunction = {@covSum, {group_trend_covariance, mean_covariance, treatment_effect_covariance}};
likfunction = {@likPoisson,'exp'};
theta.lik = [];
prior.cov = {{@priorTransform,@exp,@exp,@log,{@priorGamma,10,8}}, ...
[], ...
{@priorGauss, 0.0, 1}, ...
@priorDelta, ...
@priorDelta, ...
@priorDelta, ...
{@priorGamma, 3, 10}, ...
{@priorTransform,@exp,@exp,@log,{@priorGamma,10,8}}, ...
{@priorSmoothBox2, -4, -1, 5}};
prior.lik = {};
prior.mean = {@priorDelta, @priorDelta};
inference_method = {@infPrior, @infLaplace, prior};
non_drift_idx = [2, 5];
clear sigacts;
clear data_indirect;
clear data_direct;
% find MAP
p.method = 'LBFGS';
p.length = 100;
theta = minimize_v2(theta, @gp, p, inference_method, meanfunction, ...
covfunction, likfunction, x, y);
% effect process prior
theta_drift = theta;
theta_drift.cov(non_drift_idx) = log(0);
m_drift = feval(meanfunction{:}, theta_drift.mean, x)*0;
K_drift = feval(covfunction{:}, theta_drift.cov, x);
% effect posterior
[post, ~, ~] = infLaplace(theta, meanfunction, covfunction, likfunction, x, y);
m_post = m_drift + K_drift*post.alpha;
tmp = K_drift.*post.sW;
K_post = K_drift - tmp'*solve_chol(post.L, tmp);
% remove control group
mu = m_post(x(:,end)~=0,:);
tmp = diag(K_post);
s2 = tmp(x(:,end)~=0,:);
days = x(x(:,end)~=0,1);
clear m_drift;
clear K_drift;
clear m_post;
clear K_post;
clear tmp;
% plot MAP
fig = figure(1);
clf;
f = [exp(mu+1.96*sqrt(s2)); exp(flip(mu-1.96*sqrt(s2),1))];
fill([days; flip(days,1)], f, [7 7 7]/8);
xlim([min(days),max(days)]);
hold on; plot(days, exp(mu));
close all;
% sampler parameters
num_chains = 1;
num_samples = 3000;
burn_in = 1000;
jitter = 1e-1;
% setup sampler
% select index of hyperparameters to sample
theta_ind = false(size(unwrap(theta)));
theta_ind([1:3, 7:9]) = true;
theta_0 = unwrap(theta);
theta_0 = theta_0(theta_ind);
f = @(unwrapped_theta) ...
l(unwrapped_theta, theta_ind, theta, inference_method, meanfunction, ...
covfunction, x, y, likfunction);
% create and tune sampler
hmc = hmcSampler(f, theta_0 + randn(size(theta_0)) * jitter);
tic;
[hmc, tune_info] = ...
tuneSampler(hmc, ...
'verbositylevel', 2, ...
'numprint', 10, ...
'numstepsizetuningiterations', 100, ...
'numstepslimit', 500);
toc;
% use default seed for hmc sampler
rng('default');
tic;
[chain, endpoint, acceptance_ratio] = ...
drawSamples(hmc, ...
'start', theta_0 + jitter * randn(size(theta_0)), ...
'burnin', burn_in, ...
'numsamples', num_samples, ...
'verbositylevel', 1, ...
'numprint', 10);
toc;
% iterate all posterior samples
clear mus;
clear s2s;
day_index = 2;
for i=1:size(chain,1)
theta_0 = unwrap(theta);
theta_0(theta_ind)=chain(i,:);
theta_0 = rewrap(theta, theta_0);
% effect process prior
theta_drift = theta_0;
theta_drift.cov(non_drift_idx) = log(0);
m_drift = feval(meanfunction{:}, theta_drift.mean, x)*0;
K_drift = feval(covfunction{:}, theta_drift.cov, x);
% effect posterior
[post, ~, ~] = infLaplace(theta_0, meanfunction, covfunction, likfunction, x, y);
m_post = m_drift + K_drift*post.alpha;
tmp = K_drift.*post.sW;
K_post = K_drift - tmp'*solve_chol(post.L, tmp);
% remove control group
mu = m_post(x(:,end)~=0,:);
tmp = diag(K_post);
s2 = tmp(x(:,end)~=0,:);
days = x(x(:,end)~=0,1);
clear m_drift;
clear K_drift;
clear m_post;
clear K_post;
clear tmp;
mus{i} = mu;
s2s{i} = s2;
end
gmm_mean = mean(cell2mat(mus),2);
gmm_s2 = mean(cell2mat(s2s),2);
gmm_var = gmm_s2 + mean(cell2mat(mus).^2,2) - gmm_mean.^2;
save("./data/sigact_fullbayes" + ".mat");
idx = (1:num_days);
fig = figure(1);
clf;
f = [exp(gmm_mean(idx)+1.96*sqrt(gmm_var(idx))); exp(flip(gmm_mean(idx)-1.96*sqrt(gmm_var(idx)),1))];
fill([days(idx); flip(days(idx),1)], f, [7 7 7]/8, 'edgecolor', 'none');
hold on; plot(days(idx), exp(gmm_mean(idx)));
BIN = 90;
XTICK = BIN*[0:1:abs(810/BIN)];
XTICKLABELS = ["Jan 2007", "Apr 2007", "Jul 2007", "Oct 2007",...
"Jan 2008", "Apr 2008", "Jul 2008", "Oct 2008", "Jan 2009"];
set(gca, 'xtick', XTICK, ...
'xticklabels', XTICKLABELS,...
'XTickLabelRotation',45,...
'box', 'off', ...
'tickdir', 'out', ...
'FontSize',12);
xlim([1, num_days]);
legend("Effect 95% CI",...
"Effect mean",...
'Location', 'northwest', 'NumColumns',2,'FontSize',FONTSIZE);
legend('boxoff');
ylabel("Effect (ratio of densities)",'FontSize',12);
filename = "./data/sigactbot" + ".pdf";
set(fig, 'PaperPosition', [-2 0 22 3]);
set(fig, 'PaperSize', [18 3]);
% set(fig, 'OuterPosition', [0 0 0.8 1]);
print(fig, filename, '-dpdf','-r300');
close;