-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgoVARSVt.m
661 lines (530 loc) · 20.2 KB
/
goVARSVt.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
%% Estimates quasi-real-time runs of VAR-SV-t model
% Carriero, Clark, Marcellino and Mertens (forthcoming, REStat)
%#ok<*NOSEL>
%#ok<*DISPLAYPROG>
%#ok<*UNRCH>
%% 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/
%% Initial operations
clear; close all; clc;
Nstreams = max(1,getparpoolsize);
rndStreams = initRandStreams(Nstreams, [], 0);
%% set parameters for VAR and MCMC
datalabel = 'fredMD16levels-2021-04';
doQuarterly = false;
MCMCdraws = 1e3; % Final number of MCMC draws after burn in
fcstNdraws = 100 * MCMCdraws; % draws sampled from predictive density
doCensorYields = true;
do2020 = false;
do1975 = false;
doRobustPrior = false;
tdofGrid = 3 : 40; % grid for uniform prior
% SED-PARAMETERS-HERE
doStoreXL = false; %#ok<*NASGU>
check_stationarity = 0; % Truncate nonstationary draws? (1=yes)
Compute_diagnostics = false; % compute Inefficiency Factors and Potential
doLoMem = ~do2020; % do not store memory intensive stuff, just do oos forecasts
doPlotData = false;
samStart = []; % datenum(1988,12,1); % truncate start of sample if desired (leave empty if otherwise)
if doCensorYields
ELBbound = .25;
else
ELBbound = [];
end
if doQuarterly
p = 4;
np = 4; % number of periods per year, used for calibrating priors
datalabel = strcat(datalabel, '-quarterly');
else
p = 12;
np = 12;
end
if doLoMem
doStoreXL = false;
end
%% 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));
% Data
data=dum.data(3:end,2:end);
% define index of yields that need to obey ELB (out of sample)
if doCensorYields
ndxYIELDS = find(ismember(ncode, {'FEDFUNDS', 'GS1', 'GS5', 'GS10'}));
else
ndxYIELDS = [];
end
Tdata = length(ydates);
Ylabels = fredMDprettylabel(ncode);
%% process settings
N = size(data,2);
doTightPrior = false; % N >= 10;
Kbvar = N * p + 1; % number of regressors per equation
K = Kbvar;
if isempty(ELBbound)
labelSampling = 'NOshadowrate';
else
labelSampling = 'censoredYields';
end
if doTightPrior
labelSampling = strcat(labelSampling, '-tightBVARshrinkage');
end
if doRobustPrior
labelSampling = strcat(labelSampling, '-covidrobustPrior');
end
labelSampling = strcat(labelSampling, '-SVt');
% truncate start of sample (if desired)
if ~isempty(samStart)
ndx = ydates >= samStart;
data = data(ndx,:);
ydates = ydates(ndx);
Tdata = length(ydates);
end
% define oos jump offs
if isempty(samStart)
Tjumpoffs = find(ydates >= datenum(1975,1,1));
else
Tjumpoffs = find(ydates >= datenum(2000,1,1));
end
if do2020
Tjumpoffs = find(ydates >= datenum(2020,1,1));
labelSampling = strcat(labelSampling, '-2020');
end
if do1975
Tjumpoffs = find(ydates >= datenum(1975,1,1) & ydates < datenum(1985,1,1));
labelSampling = strcat(labelSampling, '-1975');
end
Njumpoffs = length(Tjumpoffs);
% ELB settings
% other settings
setQuantiles = [.5, 2.5, 5, normcdf(-1) * 100, 25 , 75, (1 - normcdf(-1)) * 100, 95, 97.5, 99.5];
Nquantiles = length(setQuantiles);
ndxCI = ismember(setQuantiles, [5, normcdf(-1) * 100, 100 - normcdf(-1) * 100, 95]);
%% mean for Minnesota prior: zero (diff) or RW (level)
if contains(lower(datalabel), 'levels')
minnesotaPriorMean = ones(N,1);
else
minnesotaPriorMean = NaN(N,1);
for n = 1 : N
switch ncode{n}
case {'CUMFNS', 'UNRATE', ...
'WPSFD49207', 'PPICMM', 'PCEPI', ...
'FEDFUNDS', 'HOUST', 'GS5', 'GS10', 'BAAFFM', 'WUXIASHADOWRATE'}
minnesotaPriorMean(n) = 1;
otherwise
minnesotaPriorMean(n) = 0;
end
end
end
%% allocate memory for out-of-sample forecasts
fcstNhorizons = 24; % number of steps forecasted (1:fcstNhorizon)
% fcstYdraws = NaN(N,fcstNhorizons,fcstNdraws,Njumpoffs);
fcstYrealized = NaN(N,fcstNhorizons,Njumpoffs);
fcstYhat = NaN(N,fcstNhorizons,Njumpoffs); % predictive mean
fcstYhatRB = NaN(N,fcstNhorizons,Njumpoffs); % predictive mean (linear RB)
fcstYmedian = NaN(N,fcstNhorizons,Njumpoffs); % predictive median
fcstYhaterror = NaN(N,fcstNhorizons,Njumpoffs);
fcstYhatRBerror = NaN(N,fcstNhorizons,Njumpoffs);
fcstYmederror = NaN(N,fcstNhorizons,Njumpoffs);
fcstLogscore = NaN(N,fcstNhorizons,Njumpoffs);
fcstCRPS = NaN(N,fcstNhorizons,Njumpoffs);
fcstYquantiles = NaN(N,fcstNhorizons,Nquantiles, Njumpoffs);
fcstYmvlogscoreDraws = NaN(fcstNdraws,Njumpoffs); % one-step ahead only
fcstYmvlogscore = NaN(1,Njumpoffs); % one-step ahead only
fcstYmvlogscore2 = NaN(1,Njumpoffs); % one-step ahead only
[PAImedian, PAImean, PAIstdev] = deal(NaN(K, N, Njumpoffs));
PAIquantiles = NaN(K, N, Nquantiles, Njumpoffs);
drawsSVmid = NaN(N, Tdata, Njumpoffs);
drawsSVtails = NaN(N, Tdata, Nquantiles, Njumpoffs);
drawsLambdaSVmid = NaN(N, Tdata, Njumpoffs);
drawsLambdaSVtails = NaN(N, Tdata, Nquantiles, Njumpoffs);
%% allocate memory for MCMC output (ex forecast)
if ~doLoMem
drawsPAI = NaN(K, N, MCMCdraws, Njumpoffs);
drawsPHI = NaN(N*(N-1)/2+N, MCMCdraws, Njumpoffs);
drawsINVA = NaN(N, N, MCMCdraws, Njumpoffs);
drawsSQRTHT = NaN(N, Tdata, MCMCdraws, Njumpoffs);
drawsSVtscalelog2 = NaN(N, Tdata, MCMCdraws, Njumpoffs);
end
drawsSVtdof = NaN(N, MCMCdraws, Njumpoffs);
drawsMaxVARroot = NaN(MCMCdraws, Njumpoffs);
%% start latexwrapper to collect results
titlename=sprintf('%s-%s-p%d', datalabel, labelSampling, p);
if ~isempty(samStart)
titlename = strcat(titlename, '-', datestr(samStart, 'yyyymmm'));
end
initwrap
% wrap = [];
%% plot input data
if doPlotData
for n = 1 : N
this = figure;
plot(ydates, data(:,n))
xtickdates(ydates)
wrapthisfigure(this, sprintf('data%s', ncode{n}), wrap)
end
end
%% loop over QRT estimates
% progressbar(0)
parfor ndxT = 1 : Njumpoffs % parfor
TID = parid;
thisT = Tjumpoffs(ndxT);
T = thisT - p;
fprintf('loop %d, thisT %d, with TID %d\n', ndxT, thisT, TID)
thisdata = data; % parfor
%% prepare realized values
yrealized = NaN(N, fcstNhorizons);
for h = 1 : fcstNhorizons
if thisT + h <= Tdata
yrealized(:,h) = thisdata(thisT+h,:)';
end
end
yrealized(cumcode,:) = cumsum(yrealized(cumcode,:),2);
%% MCMC sampler
[PAI_all, PHI_all, invA_all, sqrtht_all, ...
SVtscalelog2_all, SVtdof_all, ...
ydraws, yhat, yhatRB, fcstSVdraws, logscoredraws] ...
= mcmcVARSVt(thisT, MCMCdraws, p, np, thisdata, ydates, ...
minnesotaPriorMean, doTightPrior, doRobustPrior, ...
tdofGrid, ...
ndxYIELDS, ELBbound, ...
check_stationarity, ...
yrealized,...
fcstNdraws, fcstNhorizons, rndStreams{TID}); %#ok<PFBNS>
%% Convergence diagnostics
if Compute_diagnostics
% display('computing convergence diagnostics..')
Diagnostics(sqrtht_all,invA_all,PAI_all,PHI_all,N,K,MCMCdraws);
end
%% compute out-of-sample forecasts
% a word on parfor strategy:
% to make matlab better see the intended use of sliced variabes, use
% local temp variables and then copy those into the slices at end of
% loop
% cumulate realizations and predictions if necessary
ydraws(cumcode,:,:) = cumsum(ydraws(cumcode,:,:),2);
yhat(cumcode,:) = cumsum(yhat(cumcode,:),2);
yhatRB(cumcode,:) = cumsum(yhatRB(cumcode,:),2);
% compute median
ymed = median(ydraws,3);
% mv logscore one step ahead (ignoring ELB)
thesedraws = squeeze(ydraws(:,1,:));
MU = mean(thesedraws, 2);
Sigma = cov(thesedraws', 1); % normalize variance by N
sqrtSigma = chol(Sigma)';
logdetSigma = 2 * sum(log(diag(sqrtSigma)));
dev = sqrtSigma \ (yrealized(:,1) - MU);
SSR = dev' * dev;
yMVlogscore = -.5 * (N * log(2 * pi) + logdetSigma + SSR);
% logscore
yLogscore = NaN(N,fcstNhorizons);
if isempty(ELBbound)
ndxBoundedSupport = false(N,1);
else
ndxBoundedSupport = ismember(1:N, ndxYIELDS)';
end
ndxUnboundedSupport = ~ndxBoundedSupport;
% a) compute logscore for variables with unbounded support via gaussian approximation
thesedraws = ydraws(ndxUnboundedSupport,:,:);
mu = mean(thesedraws, 3);
sigma2 = var(thesedraws, 1, 3); % normalize variance by N (rather than N-1)
yLogscore(ndxUnboundedSupport,:) = -.5 * (log(2 * pi) + log(sigma2) + ((yrealized(ndxUnboundedSupport,:) - mu).^2 ./ sigma2));
% b) compute logscore for variables with bounded support vis kernel density of truncated normal
if ~isempty(ndxBoundedSupport)
ndx = find(ndxBoundedSupport);
for h = 1 : fcstNhorizons
for n = 1 : length(ndx) % loop over elements of Y
if ~isnan(yrealized(ndx(n),h))
% note: ksdensity around NaN returns 0, log(ksdensity) is then also NaN
thesedraws = squeeze(ydraws(ndx(n),h,:));
adjust4ELB = ELBbound - eps; % eps to ensure *positive* draws
yLogscore(ndx(n),h) = log(ksdensity(thesedraws - adjust4ELB, yrealized(ndx(n),h) - adjust4ELB, 'support', 'positive'));
end
end
end
end
% CRPS
yCRPS = NaN(N,fcstNhorizons);
for h = 1 : fcstNhorizons
for n = 1 : N % loop over elements of Y
yCRPS(n,h) = crpsDraws(yrealized(n,h), ydraws(n,h,:));
end
end
%% compute maxVARroot
theseMaxVARroots = NaN(MCMCdraws, 1); % placed before doLoMem to avoif parfor warning
% setup companion form matrix
comp = zeros(N * p);
comp(N + 1 : end,1:N*(p-1)) = eye(N*(p-1));
for m = 1 : MCMCdraws
thisPAI = PAI_all(:,:,m);
comp(1:N,:) = thisPAI(2:Kbvar,:)';
% compute maxLambda
theseMaxVARroots(m) = max(abs(eig(comp)));
end
%% collect PAI moments
PAImedian(:,:,ndxT) = median(PAI_all,3);
PAImean(:,:,ndxT) = mean(PAI_all,3);
PAIstdev(:,:,ndxT) = std(PAI_all,1,3);
PAIquantiles(:,:,:,ndxT) = prctile(PAI_all,setQuantiles,3);
%% compute SV
SVdraws = NaN(N,Tdata,MCMCdraws);
stochvol = NaN(N, Tdata, MCMCdraws);
stochvol(:,p+1:thisT, :) = sqrtht_all;
for m=1:MCMCdraws
invA = invA_all(:,:,m);
for t=1:Tdata
SVdraws(:,t,m) = sqrt(diag(invA*diag(stochvol(:,t,m).^2)*invA'));
end
end
drawsSVmid(:,:,ndxT) = median(SVdraws, 3);
drawsSVtails(:,:,:,ndxT) = prctile(SVdraws, setQuantiles, 3);
% pure SV part
SVdraws = NaN(N,Tdata,MCMCdraws);
stochvol = NaN(N, Tdata, MCMCdraws);
stochvol(:,p+1:thisT, :) = sqrtht_all .* exp(-.5 * SVtscalelog2_all);
for m=1:MCMCdraws
invA = invA_all(:,:,m);
for t=1:Tdata
SVdraws(:,t,m) = sqrt(diag(invA*diag(stochvol(:,t,m).^2)*invA'));
end
end
drawsLambdaSVmid(:,:,ndxT) = median(SVdraws, 3);
drawsLambdaSVtails(:,:,:,ndxT) = prctile(SVdraws, setQuantiles, 3);
%% copy results into sliced variables
fcstYmvlogscoreDraws(:,ndxT) = logscoredraws;
maxlogscoredraw = max(logscoredraws);
fcstYmvlogscore(:,ndxT) = log(mean(exp(logscoredraws - maxlogscoredraw))) + maxlogscoredraw;
fcstYmvlogscore2(:,ndxT) = yMVlogscore;
% forecast
fcstYhat(:,:,ndxT) = yhat; % mean(ydraws,3) or analytically
fcstYhatRB(:,:,ndxT) = yhatRB;
fcstYmedian(:,:,ndxT) = ymed; % median(ydraws,3);
fcstYhaterror(:,:,ndxT) = yrealized - yhat;
fcstYmederror(:,:,ndxT) = yrealized - ymed;
% fcstYdraws(:,:,:,ndxT) = ydraws;
fcstYrealized(:,:,ndxT) = yrealized;
fcstCRPS(:,:,ndxT) = yCRPS;
fcstLogscore(:,:,ndxT) = yLogscore;
fcstYquantiles(:,:,:,ndxT) = prctile(ydraws, setQuantiles, 3);
% copy mcmc output
drawsMaxVARroot(:,ndxT) = theseMaxVARroots;
drawsSVtdof(:,:,ndxT) = SVtdof_all;
if ~doLoMem
drawsPAI(:,:,:,ndxT) = PAI_all;
drawsPHI(:,:,ndxT) = PHI_all;
drawsINVA(:,:,:,ndxT) = invA_all;
% prepare dummy to make parfor work
dummy = NaN(N, Tdata, MCMCdraws);
dummy(:,p+1:thisT, :) = sqrtht_all;
drawsSQRTHT(:, :, :, ndxT) = dummy;
% prepare dummy to make parfor work
dummy = NaN(N, Tdata, MCMCdraws);
dummy(:, p+1:thisT,:) = SVtscalelog2_all;
drawsSVtscalelog2(:,:,:,ndxT) = dummy;
end
end
%% plot evolution of predictive densities
theseHorizons = [1 8 16 24];
for n = 1 : N
thisfig = figure;
for ii = 1 : length(theseHorizons)
h = theseHorizons(ii);
subplot(2,2,ii)
fcstMid = squeeze(fcstYhat(n,h,:));
theseTails = squeeze(fcstYquantiles(n,h,ndxCI,:))';
hold on
plotCI(fcstMid, theseTails, ydates(Tjumpoffs));
if any(n == ndxYIELDS)
plot(ydates(Tjumpoffs),squeeze(fcstYmedian(n,h,:)), 'r-.', 'linewidth', 3)
end
plot(ydates(Tjumpoffs),squeeze(fcstYhatRB(n,h,:)), 'b--', 'linewidth', 3)
title(sprintf('h=%d', h))
sgtitle(sprintf('%s', Ylabels{n}))
xtickdates(ydates(Tjumpoffs))
end
wrapthisfigure(thisfig, sprintf('predictiveDensity-%s', ncode{n}), wrap)
end
%% SVtscale
close all
if ~doLoMem
% collect QRT SVtscales
thisSVtscaleQRT = NaN(N,length(ydates));
thisSVtscaleQRTtails = NaN(N,2,length(ydates));
drawsSVtscale = exp(0.5 * drawsSVtscalelog2);
theseSVtscaleVintages = squeeze(median(drawsSVtscale, 3)); % returns N x Tdata x Njumpoff
theseSVtscaleVintagesTails = prctile(drawsSVtscale, [normcdf(1) 90], 3); % returns N x Tdata x Njumpoff
for v = 1 : Njumpoffs
ndx = find(~isnan(theseSVtscaleVintages(1,:,v)),1, 'last'); % note: sufficient to check only for first element
if isnan(theseSVtscaleVintages(1,ndx,v))
error houston
end
thisSVtscaleQRT(:,ndx) = theseSVtscaleVintages(:,ndx,v);
thisSVtscaleQRTtails(:,:,ndx) = squeeze(theseSVtscaleVintagesTails(:,ndx,:,v));
end
thisSVtscaleT = theseSVtscaleVintages(:,:,end);
% plot SVt scale QRT vs final
bardates = ydates(Tjumpoffs);
bardates = cat(1, bardates(1) - 15, bardates, bardates(end) + 15);
for n = 1 : N
thisfig = figure;
hold on
h1 = bar(ydates(Tjumpoffs), thisSVtscaleT(n,Tjumpoffs));
h2 = plot(ydates(Tjumpoffs), thisSVtscaleQRT(n,Tjumpoffs), 'rd', 'linewidth', 5);
h3 = plot(ydates(Tjumpoffs), squeeze(thisSVtscaleQRTtails(n,1,Tjumpoffs)), 'r--', 'linewidth', 2);
h4 = plot(ydates(Tjumpoffs), squeeze(thisSVtscaleQRTtails(n,2,Tjumpoffs)), 'r:', 'linewidth', 2);
xtickdates(bardates)
legend([h1 h2 h3 h4], 'Final (median)', 'QRT (median)', 'QRT (upper 84)', 'QRT (upper 90)')
title(sprintf('SV-t scale for %s', Ylabels{n}))
wrapthisfigure(thisfig, sprintf('SVtscale%d', n),wrap)
end
end
clear drawsSVtscale
%% plot COVIDSV
% ndx = ydates >= datenum(2020,1,1);
% thesedates = ydates(ndx);
%
% % theseJumpoffs = [1 4 9 13];
% if sum(ndx) > 9
% theseJumpoffs = [1 4 9 sum(ndx)];
% else
% theseJumpoffs = 3 : sum(ndx);
% end
%
% for n = 1 : N
% thisfig = figure;
% subplot(1,2,1)
% ax = gca;
% set(ax, 'fontsize', 16)
% hold on
%
% lineTypes = {'-d', '--o', 'd-.', ':o'};
% iter = 0;
% for ndxT = theseJumpoffs
% iter = iter + 1;
% plot(thesedates, drawsSVmid(n,ndx,ndxT), lineTypes{iter}, 'linewidth', 2)
% if iter == length(lineTypes)
% iter = 0;
% end
% end
% ylim([0 max(ylim)])
% xtickdates(thesedates)
% title('including t scales')
%
% subplot(1,2,2)
% axo = gca;
% set(axo, 'fontsize', 16)
% hold on
% lineTypes = {'-d', '--o', 'd-.', ':o'};
% iter = 0;
% for ndxT = theseJumpoffs
% iter = iter + 1;
% plot(thesedates, drawsLambdaSVmid(n,ndx,ndxT), lineTypes{iter}, 'linewidth', 2)
% if iter == length(lineTypes)
% iter = 0;
% end
% end
% ylim([0 max([ylim(axo), ylim(ax)])])
% xtickdates(thesedates)
% legend(datestr(ydates(Tjumpoffs(theseJumpoffs)), 'yyyy:mm'), 'location', 'best', 'box', 'off')
% title('ex t scales')
%
% sgtitle(sprintf('\\bf %s FE-SV', Ylabels{n}))
%
% % legend(datestr(ydates(Tjumpoffs), 'yyyy:mm'), 'location', 'northwest', 'box', 'off')
%
%
% wrapthisfigure(thisfig, sprintf('covidSV%s', ncode{n}), wrap)
%
% end
%% plot tdof
% generate histograms
% tdofposterior = NaN(length(tdofGrid), Njumpoffs, N);
% for n = 1 : N
% for j = 1 : Njumpoffs
% % tposterior(n,:) = ksdensity(SVtdof_all(n,:), tdofGrid);
% tdofposterior(:,j,n) = histcounts(drawsSVtdof(n,:,j), [tdofGrid tdofGrid(end) + 1], 'Normalization', 'probability');
% end
% end
%
% % plot histograms (As surface)
% for n = 1 : N
% thisfig = figure;
% surf(ydates(Tjumpoffs), tdofGrid, tdofposterior(:,:,n));
% shading interp
% yticks([3, 5 : 5 : tdofGrid(end)])
% datetick('x', 'keeplimits', 'keepticks')
% title(Ylabels{n})
% wrapthisfigure(thisfig, sprintf('SVt-dofposterior-%s', ncode{n}),wrap)
% end
% medians
tdofmed = squeeze(median(drawsSVtdof, 2));
tdofCI = prctile(drawsSVtdof, [5 95], 2);
for n = 1 : N
thisfig = figure;
plotCI(tdofmed(n,:)', squeeze(tdofCI(n,:,:))', ydates(Tjumpoffs));
yticks([3, 5 : 5 : tdofGrid(end)])
ylim([2 40])
datetick('x', 'keeplimits', 'keepticks')
title(Ylabels{n})
wrapthisfigure(thisfig, sprintf('SVt-dofposterior-med-%s', ncode{n}),wrap)
end
%% plot companion maxLambda
meanMaxVARroot = mean(drawsMaxVARroot,1);
medMaxVARroot = median(drawsMaxVARroot,1);
tailsMaxVARroot = prctile(drawsMaxVARroot, [5 95], 1);
this = figure;
hold on
plot(ydates(Tjumpoffs), meanMaxVARroot, 'k-', 'linewidth', 2)
plot(ydates(Tjumpoffs), medMaxVARroot, 'r--', 'linewidth', 2)
plot(ydates(Tjumpoffs), tailsMaxVARroot', 'k-', 'linewidth', 1)
xtickdates(ydates(Tjumpoffs))
wrapthisfigure(this, 'maxVARroot', wrap)
%% plot logscores
this = figure;
hold on
h1 = plot(ydates(Tjumpoffs), fcstYmvlogscore, 'r-', 'linewidth', 2);
% jack = mean(fcstYmvlogscoreDraws,1);
% plot(ydates(Tjumpoffs), jack, 'm-', 'linewidth', 2)
h2 = plot(ydates(Tjumpoffs), fcstYmvlogscore2 , 'k--', 'linewidth', 2);
legend([h1 h2], 'mixture approx.', 'Gaussian approx.', 'location', 'best')
xtickdates(ydates(Tjumpoffs))
wrapthisfigure(this, 'MVlogscore', wrap)
%% store qrt summary
matfilename = sprintf('%s-%s-p%d', datalabel, labelSampling, p);
if ~isempty(samStart)
matfilename = strcat(matfilename, '-', datestr(samStart, 'yyyymmm'));
end
varlist = {'ydates', 'p', 'Tjumpoffs', 'N', ...
'data', 'np', ...
'ncode', 'tcode', 'cumcode', ...
'fcst*', 'fcstNhorizons', ...
'ndxYIELDS', 'ELBbound', ...
'PAI*', ...
'drawsSV*', 'drawsLambda*', ...
'meanMaxVARroot', 'medMaxVARroot', 'tailsMaxVARroot', ...
'datalabel', 'labelSampling', ...
'doQuarterly', ...
'setQuantiles', ...
'MCMCdraws'};
if doStoreXL
matfilename = sprintf('%s-%s-p%d-draws', datalabel, labelSampling, p);
save(matfilename, varlist{:}, 'draws*', '-v7.3');
end
clear drawsSVtdof drawsSVtscale* *_all
clear drawsLambdaSVtails drawsSVtails drawsMaxVARroot
clear fcstYmvlogscore*Draws
save(matfilename, varlist{:}, '-v7.3');
%% wrap up
dockAllFigures
finishwrap