-
Notifications
You must be signed in to change notification settings - Fork 3
/
goPrecisonsamplerCommonTrendCycle.m
257 lines (185 loc) · 9.42 KB
/
goPrecisonsamplerCommonTrendCycle.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
%% Apply precision-based ABC sampler to Common-Trend-cum-VAR(p) models (and simulated data)
%% clean workspace
clear
clc
close all
%% load toolboxes
path(pathdef)
addpath matlabtoolbox/emtools/
addpath matlabtoolbox/emtexbox/
addpath matlabtoolbox/emgibbsbox/
addpath matlabtoolbox/emeconometrics/
addpath matlabtoolbox/emstatespace/
rng(061222); % fix random seed
%#ok<*UNRCH>
%#ok<*NOPTS>
for doSingleThread = [true false]
maxNumCompThreads('automatic');
if doSingleThread
% enforce single threaded computations
usedThreads = 1;
availableThreads = maxNumCompThreads(usedThreads);
fprintf('Using 1 of %d available threads.\n', availableThreads)
else
usedThreads = maxNumCompThreads('automatic');
availableThreads = usedThreads;
fprintf('Using all %d available threads.\n', usedThreads)
end
%% define parameter grid
gridNy = 5 : 5 : 25;
gridT = [200 800];
gridP = [4 8 12 24];
[PStimes, PS0times, PSnoisetimes, PSnoise0times, TCPStimes, TCPS0times, PSyxtimes, PS0yxtimes, DKtimes] = deal(NaN(length(gridP), length(gridNy), length(gridT)));
for iterT = 1 : length(gridT)
T = gridT(iterT)
for iterNy = 1 : length(gridNy)
Ny = gridNy(iterNy)
for iterP = 1 : length(gridP)
p = gridP(iterP)
%% prepare vectorized system
Nx = Ny + 1;
Nx0 = Nx * p;
NyT = Ny * T;
NxT = Nx * T;
NxTp = Nx * (T + p);
kappa1 = .2^2;
kappa2 = .5^2;
kappa3 = 2;
minnesotaPrior = ones(Ny,Ny);
for i = 1 : Ny
for j = 1 : Ny
if i ~= j
minnesotaPrior(i,j) = kappa2;
end
end
end
minnesotaPrior = kappa1 .* minnesotaPrior;
minnesotaPrior = minnesotaPrior ./ permute(1:p, [1 3 2]).^kappa3;
agap = minnesotaPrior .* randn(Ny,Ny,p);
a = zeros(Nx,Nx,p);
a(1,1,1) = 1;
a(2:end,2:end,:) = agap;
b = randn(Nx);
% b = chol(b * b')';
c = [ones(Ny,1) eye(Ny, Ny)];
% prior for x0; recall ordering is from x(-p+1) to x(0)
X0 = zeros(Nx0,1);
cholsigX0 = 1e2 * eye(Nx0);
invcholsigX0 = inv(cholsigX0);
% create 3D state space matrices
aaa = repmat(a, 1, 1, 1, T);
ccc = repmat(c, 1, 1, T);
bbb = repmat(b, [1 1 T]);
invbbb = repmat(inv(b), [1 1 T]);
%% construct matrix-form state space
XX0 = sparse(1:Nx0, 1, X0, NxTp, 1);
%% AA
% build sequentially: first unit diagonal
arows1 = 1 : NxTp;
acols1 = 1 : NxTp;
arows2 = repmat((1 : Nx)', 1, Nx * p);
arows2 = Nx0 + arows2 + permute(Nx * (0 : T - 1), [1 3 2]);
acols2 = repmat(1 : Nx * p, Nx,1) + permute(Nx * (0 : T - 1), [1 3 2]);
arows = [arows1(:); arows2(:)];
acols = [acols1(:); acols2(:)];
values1 = ones(NxTp,1);
values2 = -aaa(:,:,p:-1:1,:);
values = [values1(:); values2(:)];
AA = sparse(arows, acols, values);
%% BB
brows0 = repmat((1 : Nx0)', 1 , Nx0);
brows1 = Nx0 + repmat((1 : Nx)', 1 , Nx) + permute(Nx * (0 : T-1), [1 3 2]);
brows = [brows0(:); brows1(:)];
bcols0 = repmat((1 : Nx0), Nx0, 1);
bcols1 = Nx0 + repmat((1 : Nx), Nx, 1) + permute(Nx * (0 : T-1), [1 3 2]);
bcols = [bcols0(:); bcols1(:)];
bvalues = [cholsigX0(:); bbb(:)];
BB = sparse(brows, bcols, bvalues);
invbvalues = [invcholsigX0(:); invbbb(:)];
invBB = sparse(brows, bcols, invbvalues);
%% CC
crows = repmat((1 : Ny)', 1 , Nx, T) + permute(Ny * (0 : T-1), [1 3 2]);
ccols = Nx0 + repmat(1 : NxT, Ny, 1);
CC = sparse(crows(:), ccols(:), ccc(:), NyT, NxTp);
%% simulate data
% prepare
rndStream = getDefaultStream;
xshocks = randn(rndStream, NxTp, 1);
X = AA \ (XX0 + BB * xshocks(:));
Y = CC * X;
%% measure Precision samplers
YXprecsam0 = @() abcYXprecisionsampler(Y,XX0,AA,invBB,CC,rndStream);
[~, QQ, RR1] = YXprecsam0();
YXprecsam = @() abcYXprecisionsampler(Y,XX0,AA,invBB,CC,rndStream,QQ,RR1);
PS0yxtimes(iterP, iterNy, iterT) = timeit(YXprecsam0, 1);
PSyxtimes(iterP, iterNy, iterT) = timeit(YXprecsam, 1);
y = reshape(Y, Ny, T);
aaa = aaa(:,:,p:-1:1,:);
precsam0 = @() ALBCprecisionsampler(aaa,invbbb,ccc,y,X0,invcholsigX0,rndStream);
[~, ~, CC, QQ, RR1, arows, acols, a0ndx, asortndx, brows, bcols, b0ndx, bsortndx] = precsam0();
precsam = @() ALBCprecisionsampler(aaa,invbbb,ccc,y,X0,invcholsigX0,rndStream,CC,QQ,RR1,...
arows, acols,a0ndx,asortndx,brows,bcols,b0ndx,bsortndx);
PS0times(iterP, iterNy, iterT) = timeit(precsam0, 1);
PStimes(iterP, iterNy, iterT) = timeit(precsam, 1);
%% measure precision sampler with (minimal) noise
invnoisevol = repmat(1e5, Ny, T);
precsamnoise0 = @() ALBCnoiseprecisionsampler(aaa,invbbb,ccc,invnoisevol,y,X0,invcholsigX0,rndStream);
[~, ~, ~, arows, acols, asortndx, brows, bcols, crows, ccols] = precsamnoise0();
precsamnoise = @() ALBCnoiseprecisionsampler(aaa,invbbb,ccc,invnoisevol,y,X0,invcholsigX0,rndStream,arows, acols, asortndx, brows, bcols, crows, ccols);
PSnoise0times(iterP, iterNy, iterT) = timeit(precsamnoise0, 1);
PSnoisetimes(iterP, iterNy, iterT) = timeit(precsamnoise, 1);
%% trend-cycle sampler
invbbar = repmat(1 / b(1,1), [1 1 T]);
invbgap = repmat(eye(Ny) / b(1+(1:Ny),1+(1:Ny)), [1 1 T]);
ybar0 = 0; % note: initial mot necessarily consistent with other sampples, but Ok for performance measurement
tcsam0 = @() commontrendcyclePrecisisionsampler(y, agap, invbgap, invbbar, ybar0, rndStream);
[~, ~, Abar, Cbar, agaprows, agapcols, agapsortndx, brows, bcols] = tcsam0();
tcsam = @() commontrendcyclePrecisisionsampler(y, agap, invbgap, invbbar, ybar0, rndStream, Abar, Cbar, agaprows, agapcols, agapsortndx, brows, bcols);
TCPS0times(iterP, iterNy, iterT) = timeit(tcsam0, 2);
TCPStimes(iterP, iterNy, iterT) = timeit(tcsam, 2);
%% DK application
Nw = Ny + 1;
Nstates = Ny * p + 1;
x0companion = zeros(Nstates,1);
cholsigx0companion = 1e2 * eye(Nstates);
sigx0companion = cholsigx0companion * cholsigx0companion';
Acompanion = zeros(Nstates);
Acompanion(1,1) = 1;
Acompanion(1+(1:Ny),1+1:end) = reshape(agap, Ny, Ny * p);
Acompanion(Nw+1:Nstates,1+(1:Ny*(p-1))) = eye(Ny*(p-1));
Bcompanion = zeros(Nstates,Nw);
Bcompanion(1:Nx,:) = b;
Ccompanion = zeros(Ny, Nstates);
Ccompanion(:,1:Nx) = c;
Acompanion = repmat(Acompanion, [1 1 T]);
Bcompanion = repmat(Bcompanion, [1 1 T]);
Ccompanion = repmat(Ccompanion, [1 1 T]);
Ydata = reshape(Y, Ny, T);
dk = @() abcDisturbanceSmoothingSampler1drawSLIM(Acompanion, Bcompanion, Ccompanion, Ydata, x0companion, cholsigx0companion, [], rndStream);
DKtimes(iterP, iterNy, iterT) = timeit(dk, 1);
end % p
end % Ny
end % T
%% store results
thisArch = computer('arch');
thisVer = ver;
if ismac
[~, thisSys] = system('sysctl -a | grep machdep.cpu ', '-echo');
[~, thisBrand] = system('sysctl -a | grep machdep.cpu | grep brand_string ', '-echo');
if contains(thisBrand, 'M1 Pro')
thisBrand = 'AppleSilicon';
else
thisBrand = 'MacOSIntel';
end
elseif isunix
[~, thisSys] = system('cat /proc/cpuinfo ', '-echo');
thisBrand = 'IntelUbuntu';
else % ispc
thisSys = 'Intel(R) Xeon(R) Gold 6320 CPU @ 2.1 GHz';
thisBrand = 'WindowsXeon';
end
varlist = {'grid*', '*times', 'thisArch', 'thisVer', 'thisSys', 'thisBrand', ...
'doSingleThread', 'usedThreads', 'availableThreads'};
matname = sprintf('COMMONTRENDCYCLEPStimes%sThreads%dof%d', thisBrand, usedThreads, availableThreads);
save(matname, varlist{:});
end % doSingleThread