-
Notifications
You must be signed in to change notification settings - Fork 6
/
generateGIRF2linear.m
405 lines (295 loc) · 13 KB
/
generateGIRF2linear.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
%#ok<*NOSEL>
%#ok<*DISPLAYPROG>
%#ok<*UNRCH>
%#ok<*ASGLU>
%#ok<*DATNM>
%#ok<*DATST>
%% load em toolboxes
warning('off','MATLAB:handle_graphics:exceptions:SceneNode')
path(pathdef)
addpath matlabtoolbox/emtools/
addpath matlabtoolbox/emtexbox/
addpath matlabtoolbox/emgibbsbox/
addpath matlabtoolbox/emeconometrics/
addpath matlabtoolbox/emstatespace/
addpath matlabtoolbox/empbsbox/
rng(01012023)
%% Initial operations
clear; close all; clc;
%% set parameters for VAR and MCMC
datalabel = 'fredblockMD20EBP-2022-09';
jumpDate = datenum(2022,08,01);
irfDATES = [datenum(2006,12,1) datenum(2012,12,1)];
irfSCALES = 1;
irfNdraws = 1e3;
irfHorizon = 120;
resultsdir = pwd;
doPlots = false;
ELBbound = 0.25;
p = 12;
np = 12;
samStart = []; % truncate start of sample if desired (leave empty if otherwise)
% SED-PARAMETERS-HERE
%% load data
% load CSV file
dum=importdata(sprintf('%s.csv', datalabel),',');
ydates=dum.data(3:end,1);
% Variable names
ncode=dum.textdata(1,2:end);
% Transformation codes (data are already transformed)
tcode =dum.data(1,2:end);
cumcode=logical(dum.data(2,2:end));
cumcode(tcode == 5) = 1;
% Data
data=dum.data(3:end,2:end);
setShadowYields
ndxYIELDS = union(ndxSHADOWRATE, ndxOTHERYIELDS);
Nyields = length(ndxYIELDS);
Nshadowrates = length(ndxSHADOWRATE);
Tdata = length(ydates);
Ylabels = fredMDprettylabel(ncode);
%% process settings
N = size(data,2);
K = N * p + 1; % number of regressors per equation
% truncate start of sample (if desired)
if ~isempty(samStart)
ndx = ydates >= samStart;
data = data(ndx,:);
ydates = ydates(ndx);
Tdata = length(ydates);
end
%% some parameters
fontsize = 12;
TID = parid;
thisT = find(ydates == jumpDate);
T = thisT - p;
setQuantiles = [.5, 2.5, 5, normcdf(-1) * 100, 25 , 75, (1 - normcdf(-1)) * 100, 95, 97.5, 99.5];
Nquantiles = length(setQuantiles);
ndxCI68 = ismember(setQuantiles, [normcdf(-1) * 100, 100 - normcdf(-1) * 100]);
ndxCI90 = ismember(setQuantiles, [5 95]);
ndxCI = ndxCI68 | ndxCI90;
rndStream = getDefaultStream;
%% collect MCMC results
titlename=sprintf('%s-p%d-jumpoff%s', datalabel, p, datestr(jumpDate, 'yyyymmm'));
mcmc = matfile(fullfile(resultsdir, sprintf('mcmcLinear-%s', titlename)));
MCMCdraws = mcmc.MCMCdraws;
%% shock size
switch datalabel
case {'fredMD20VXO-2022-09', 'fredMD20VXOexYield-2022-09'}
shocksize = 1.27;
case {'fredblockMD20EBP-2022-09', ...
'fredMD20EBP-2022-09', 'fredMD20EBPexYield-2022-09', 'fredMD3EBP-2022-09'}
shocksize = 0.11;
otherwise
shocksize = 1;
end
%% GIRF
for IRF1scale = irfSCALES
display(IRF1scale)
for irfDate = irfDATES
close all
display(datestr(irfDate, 'yyyymmm'))
% prepare wrap
titlename=sprintf('%s-p%d-IRF1scale%d-jumpoff%s-irfDate%s', datalabel, p, IRF1scale, ...
datestr(jumpDate, 'yyyymmm'), datestr(irfDate, 'yyyymmm'));
if ~isempty(samStart)
titlename = strcat(titlename,'-', datestr(samStart, 'yyyymmm'));
end
wrap = [];
initwrap
%% load mcmc
PAIdraws = permute(mcmc.PAI_all, [3 2 1]);
invAdraws = permute(mcmc.invA_all, [2 3 1]);
PHIdraws = permute(mcmc.PHI_all, [2 1]);
[~, ndxvech] = ivech(PHIdraws(:,1));
ndxIRFT0 = find(ydates == irfDate);
SVjumpoffDraws = permute(mcmc.sqrtht_all(:,ndxIRFT0 - p,:), [3 1 2]);
%% allocate memory
[fcstYHATdraws, fcstYHATdraws1plus, fcstYHATdraws1minus] = deal(NaN(N, irfHorizon, MCMCdraws));
% prepare state space for forecast simulation
ndxfcstY = 1+(1:N);
fcstB = zeros(K,N);
fcstB(ndxfcstY,:) = eye(N);
fcstA0 = zeros(K,K);
fcstA0(1,1) = 1; % unit root for the constant
fcstA0(1+N+1:end,2:end) = [eye(N*(p-1)),zeros(N*(p-1),N)]; % fill in lower part of companion form
% construct forecast jumpoff (with placeholders for shadow rates)
jumpoffDate = ydates(thisT);
ndx = ydates <= jumpoffDate;
jumpoffData = data(ndx,:);
prc70 = normcdf([-1 1]) * 100;
%% generate draws RB for linear IRF
IRF1RBdraws = NaN(N, irfHorizon, MCMCdraws);
YhatRBdraws = NaN(N, irfHorizon, MCMCdraws);
parfor mm = 1 : MCMCdraws
% map into state space
fcstA = fcstA0;
fcstA(ndxfcstY, :) = PAIdraws(:,:,mm);
% baseline path
thisData = jumpoffData;
% construct jump off vector
Xjumpoff = zeros(K,1);
Xjumpoff(1) = 1;
for l=1:p
Xjumpoff(1+(l-1)*N+(1:N)) = thisData(ndxIRFT0-(l-1),1:N);
end
fcstX0 = Xjumpoff;
for hh = 1 : irfHorizon
fcstX0 = fcstA * fcstX0;
YhatRBdraws(:,hh,mm) = fcstX0(ndxfcstY);
end
% IRF
thisResponse = zeros(K,1);
thisResponse(ndxfcstY(1)) = IRF1scale * shocksize;
thisResponse(ndxfcstY) = invAdraws(:,:,mm) * thisResponse(ndxfcstY);
for hh = 1 : irfHorizon
IRF1RBdraws(:,hh,mm) = thisResponse(ndxfcstY);
thisResponse = fcstA * thisResponse;
end
end
% cumulate and collect moments
YhatRBdraws(cumcode, :,:) = cumsum(YhatRBdraws(cumcode,:,:), 2) / np;
YhatRB = median(YhatRBdraws, 3);
YhatRBtails = prctile(YhatRBdraws, prc70, 3);
IRF1RBdraws(cumcode, :,:) = cumsum(IRF1RBdraws(cumcode,:,:), 2) / np;
IRF1RB = median(IRF1RBdraws, 3);
IRF1RBtails = prctile(IRF1RBdraws, prc70, 3);
%% GIRF simulation across MCMC nodes
parfor mm = 1 : MCMCdraws
TID = parid;
% parfor preps (better to do inside parfor loop)
thisData = jumpoffData;
% construct jump off vector
Xjumpoff = zeros(K,1);
Xjumpoff(1) = 1;
for l=1:p
Xjumpoff(1+(l-1)*N+(1:N)) = thisData(ndxIRFT0-(l-1),1:N);
end
fcstX0 = Xjumpoff;
% draw shocks
zdraws = randn(rndStream, N, irfHorizon, irfNdraws);
fcstSVdraws = randn(rndStream, N, irfHorizon * irfNdraws);
% map into state space
fcstA = fcstA0;
fcstA(ndxfcstY, :) = PAIdraws(:,:,mm);
PHI = ivech(PHIdraws(:,mm), ndxvech);
SV0 = SVjumpoffDraws(:,mm);
invA = invAdraws(:,:,mm);
% generate SV paths
sqrtPHI = chol(PHI, 'lower');
fcstSVdraws = sqrtPHI * fcstSVdraws;
fcstSVdraws = reshape(fcstSVdraws, N, irfHorizon, irfNdraws);
fcstSVdraws = cumsum(fcstSVdraws,2);
fcstSVdraws = exp(fcstSVdraws * 0.5);
shock11 = IRF1scale * shocksize;
simfun = @(nushocks) simVAR(nushocks, N, fcstX0, ndxfcstY, cumcode, np, fcstA, fcstB, invA, irfHorizon, irfNdraws);
% baseline
fcstYHATdraws(:,:,mm) = antitheticSim(0, zdraws, fcstSVdraws, SV0, N, irfHorizon, irfNdraws, simfun);
% positive shock
fcstYHATdraws1plus(:,:,mm) = antitheticSim(shock11, zdraws, fcstSVdraws, SV0, N, irfHorizon, irfNdraws, simfun);
% negative shock
fcstYHATdraws1minus(:,:,mm) = antitheticSim(-1 * shock11, zdraws, fcstSVdraws, SV0, N, irfHorizon, irfNdraws, simfun);
end % parfor
%% clean up workspace
clear PAIdraws invAdraws PHIdraws SVjumpoffDraws
clear fcstA fcstB
%% IRF
IRFdraws1plus = fcstYHATdraws1plus - fcstYHATdraws;
IRFdraws1minus = fcstYHATdraws1minus - fcstYHATdraws;
% integrate over MCMC nodes
fcstYhat = median(fcstYHATdraws, 3);
fcstYhat1plus = median(fcstYHATdraws1plus, 3);
fcstYhat1minus = median(fcstYHATdraws1minus, 3);
IRF1plus = median(IRFdraws1plus, 3);
IRF1plusTails = prctile(IRFdraws1plus, prc70, 3);
IRF1minus = median(IRFdraws1minus, 3);
IRF1minusTails = prctile(IRFdraws1minus, prc70, 3);
clear IRFdraws1plus IRFdraws1minus
%% PLOT RESULTS
if doPlots
colorPlus = Colors4Plots(1);
colorMinus = Colors4Plots(2);
colorBase = Colors4Plots(8);
colorRB = Colors4Plots('green');
%% plot ELB IRF
for n = 1 : N
thisfig = figure;
hold on
set(gca, 'FontSize', fontsize)
hplus = plot(0:irfHorizon-1, IRF1plus(n,:), '-', 'color', colorPlus, 'linewidth', 3);
plot(0:irfHorizon-1, squeeze(IRF1plusTails(n,:,:)), '-', 'color', colorPlus, 'linewidth', 1);
% hminus = plot(0:irfHorizon-1, -1 * IRF1minus(n,:), '-.', 'color', colorMinus, 'linewidth', 3);
% plot(0:irfHorizon-1, -1 * squeeze(IRF1minusTails(n,:,:,:)), '-.', 'color', colorMinus, 'linewidth', 1);
hRB = plot(0:irfHorizon-1, IRF1RB(n,:), '-.', 'color', colorRB, 'linewidth', 3);
plot(0:irfHorizon-1, squeeze(IRF1RBtails(n,:,:)), '-.', 'color', colorRB, 'linewidth', 1);
xlim([0 irfHorizon-1])
yline(0, 'k:')
legend([hplus, hRB], 'simulated GIRF', 'RB IRF', 'location', 'southoutside')
sgtitle(sprintf('%s per %s', Ylabels{n}, datestr(irfDate, 'yyyymmm')), 'FontSize', 18', 'FontWeight', 'bold')
wrapthisfigure(thisfig, sprintf('IRF1rb-%s-IRF1scale%d-%s-jumpoff%s-irfDate%s', datalabel, IRF1scale, ncode{n}, ...
datestr(jumpDate, 'yyyymmm'), datestr(irfDate, 'yyyymmm')), wrap)
end
%% Response paths
for n = 1 : N
thisfig = figure;
hold on
set(gca, 'FontSize', fontsize)
hbase = plot(0:irfHorizon-1, fcstYhat(n,:), '-', 'color', colorBase, 'linewidth', 2);
hplus = plot(0:irfHorizon-1, fcstYhat1plus(n,:), '-', 'color', colorPlus, 'linewidth', 2);
hminus = plot(0:irfHorizon-1, fcstYhat1minus(n,:), '-', 'color', colorMinus, 'linewidth', 2);
hRB = plot(0:irfHorizon-1, YhatRB(n,:), '--', 'color', colorRB, 'linewidth', 2);
xlim([0 irfHorizon-1])
legend([hbase hplus hminus hRB], 'baseline', 'positive shock', 'negative shock', 'baseline (RB)', 'location', 'best')
title(sprintf('%s per %s', Ylabels{n}, datestr(irfDate, 'yyyymmm')), 'FontWeight', 'normal')
wrapthisfigure(thisfig, sprintf('pathResponses1plusminus-%s-IRF1scale%d-%s-jumpoff%s-irfDate%s', datalabel, IRF1scale, ncode{n}, ...
datestr(jumpDate, 'yyyymmm'), datestr(irfDate, 'yyyymmm')), wrap)
end
end
%% wrap up
allw = whos;
ndx = contains({allw.class}, 'Figure');
if any(ndx)
clear(allw(ndx).name)
end
save(sprintf('irf2Linear-%s.mat', titlename), '-v7.3')
close all
finishwrap
end % irfDate
end % irfscale
finishscript
%% define forecast simulation as function
function ydraws = simVAR(nushocks, N, fcstX0, ndxfcstY, cumcode, np, fcstA, fcstB, invA, irfHorizon, irfNdraws)
ydraws = NaN(N,irfHorizon,irfNdraws);
theseShocks = zeros(N, irfHorizon+1); % padded with zeros for use with ltitr
for nn = 1 : irfNdraws
theseShocks(:,1:irfHorizon) = invA * nushocks(:,:,nn);
xdraws = ltitr(fcstA, fcstB, theseShocks', fcstX0); % faster forecast simulation using ltitr
ydraws(:,:,nn) = xdraws(2:end,ndxfcstY)';
end
ydraws(cumcode, :,:) = cumsum(ydraws(cumcode,:,:), 2) / np;
end % function simVARshadowrate
%% wrapper function for antithetic simulation
function yhatdraws = antitheticSim(shock11, zdraws, SVdraws, SV0, N, irfHorizon, irfNdraws, simfun)
ydraws = NaN(N,irfHorizon,irfNdraws * 4);
mcndxpp = 1: irfNdraws;
mcndxpm = irfNdraws + (1: irfNdraws);
mcndxmp = 2 * irfNdraws + (1: irfNdraws);
mcndxmm = 3 * irfNdraws + (1: irfNdraws);
%% pp
nushocks = zdraws .* SVdraws .* SV0;
nushocks(1,1,:) = shock11 + nushocks(1,1,:);
ydraws(:,:,mcndxpp) = simfun(nushocks);
%% pm
nushocks = -1 .* zdraws .* SVdraws .* SV0;
nushocks(1,1,:) = shock11 + nushocks(1,1,:);
ydraws(:,:,mcndxpm) = simfun(nushocks);
%% mp
nushocks = zdraws ./ SVdraws .* SV0;
nushocks(1,1,:) = shock11 + nushocks(1,1,:);
ydraws(:,:,mcndxmp) = simfun(nushocks);
%% mm
nushocks = -1 .* zdraws ./ SVdraws .* SV0;
nushocks(1,1,:) = shock11 + nushocks(1,1,:);
ydraws(:,:,mcndxmm) = simfun(nushocks);
yhatdraws = mean(ydraws,3);
end % function antitheticSim