-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBase.m
More file actions
269 lines (189 loc) · 6.02 KB
/
Copy pathtestBase.m
File metadata and controls
269 lines (189 loc) · 6.02 KB
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
%% Introduction to common tasks
%%
% * Read data from different sources
% * Set up the components for a reduced-form VAR model
% * Estimate a reduced-form VAR model
% * Run common tasks with reduced-form VAR model
% * Set up the components for a structural identification of a VAR model
% * Identify a structural VAR model from its original reduced form
% * Run common tasks with structural VAR model
% * Use multiple VAR models (objects) at the same time
% * Use basic visualization tools
%% Housekeeping
clear
close all
rehash path
addpath ../sandbox
addpath ../bear
%% Convenience functions
% These functions will be used when manipulating some of the output tables
percentiles = [10, 50, 90];
prctileFunc = @(x) prctile(x, percentiles, 2);
firstFunc = @(x) x(:, 1, :, :, :);
medianFunc = @(x) median(x, 2);
flatFunc = @(x) x(:, :);
defaultColors = get(0, "defaultAxesColorOrder");
%% Reading data
%%
% * CSV
% * Excel
% * MAT file
inputTbx = tablex.fromCsv("exampleData.csv");
inputTbx
%% Setting up components for reduced-form VAR model
%%
% * Meta information
% * Data holder
% * Estimator
% * Dummy observations
% * Transformer
estimStart = datex.q(1975,1);
estimEnd = datex.q(2014,4);
estimSpan = datex.span(estimStart, estimEnd);
meta = base.Meta( ...
endogenous=["DOM_GDP", "DOM_CPI", "STN"], ...
units="", ...
exogenous="Oil", ...
order=4, ...
intercept=true, ...
estimationSpan=estimSpan ...
);
meta
dataH = base.DataHolder(meta, inputTbx);
estimatorR = estimator.NormalWishart( ...
meta ...
);
estimatorR
% estimatorR2 = estimator.GeneralTV(meta, Burnin=100);
% estimatorR2.Settings
minnesotaD = dummies.Minnesota(exogenousLambda=30);
modelR = base.ReducedForm( ...
meta=meta ...
, dataHolder=dataH ...
, estimator=estimatorR ...
, dummies={minnesotaD} ...
, stabilityThreshold=Inf ...
);
modelR
%% Initializing reduced-form VAR model
%
%
% The initialization step precalculates all the values needed for running a
% posterior simulator, creates the following function handles (accessible as properties
% of the reduced-form objec):
% |.Sampler|
% A function returning one sample from the posterior distribution
% |.IdentificationDrawer|
% A function taking one sample and returning a sequence (cell array) of VAR
% system matrices |A|, |C| and a fixed covariance matrix |Sigma| used for calculating
% shock responses. This sequence is generated only once for each sample and cached
% within the model object.
% |.HistoryDrawer|
% A function taking one sample and returning a sequence (cell array) of |A|,
% |C|, and |Sigma| matrices with their historical estimates (in time-varying models)
% or simply their estimates repeated the corresponding number of times.
% |.UnconditionalDrawer|
% A function taking one sample, a start date (index), and a forecast horizon
% lenght, and generates a sequence (cell array) of |A|, |C|, and |Sigma| matrices
% meant for calculating an unconditional forecast on the given forecast span.
% This sequence is not cached:
%%
% * for time-invariant models, the function simply returns a sequence of fixed
% system matrices;
% * for time-varying models, the function generates (randomly) using the estimated
% probabilistic assumptions about the time evolution of the model parameters.
% |.ConditionalDrawer|
% Same as |.UnconditionalDrawer| but for conditional forecasts.
%
%
modelR.initialize();
modelR
%% Presampling from posterior
%
%
% Generate a specified number of samples from the posterior distributions. These
% samples are cached, and use in all of the subsequent calculations.
%
%
modelR.presample(100);
modelR
%% Using reduced-form VAR model
%
%
% Using the estimated reduced-form model, run the following calculations:
%%
% * Estimate the historical reduals (one set of residuals for each sample)
% * Run an unconditional forecast (within historical range)
residTbx = modelR.estimateResiduals();
fcastStart = datex.shift(modelR.Meta.EstimationEnd, -10);
fcastEnd = datex.shift(modelR.Meta.EstimationEnd, 0);
fcastSpan = datex.span(fcastStart, fcastEnd);
fcastStart, fcastEnd
fcastTbx = modelR.forecast(fcastSpan);
fcastTbx
fcastPrctileTbx = tablex.apply(fcastTbx, prctileFunc);
fcastPrctileTbx
tablex.plot( ...
fcastPrctileTbx, "DOM_GDP", ...
plotSettings={"color", defaultColors(1, :), {"lineStyle"}, {":"; "-"; ":"}} ...
);
keyboard
%% Setting up components for structural VAR
%
id = identifier.Cholesky();
modelS = base.Structural( ...
reducedForm=modelR, ...
identifier=id ...
);
modelS
%% Identifying structural VAR
%
modelS.initialize();
modelS
info = modelS.presample(100);
info
%% Using structural VAR
fcastTbx = modelS.forecast(fcastSpan);
residTbx = modelS.estimateResiduals();
shkTbx = modelS.estimateShocks();
simTbx = modelS.simulateResponses();
simPctTbx = tablex.apply(simTbx, prctileFunc);
simPctTbx = tablex.flatten(simPctTbx);
ch = visual.Chartpack( ...
span=tablex.span(simPctTbx), ...
namesToPlot=tablex.names(simPctTbx), ...
captions="Shock Responses" ...
);
ch.plot(simPctTbx);
%% Structural identification with general restrictions
testStrings = [
"abs($SHKRESP(1, 'DOM_GDP', 'POL')) > 0"
"$SHKEST('2014-Q1', 'DEM') > 0.1"
"$SHKCONT('2010-Q3', 'DOM_CPI', 'SUP') > 0"
]
id2 = identifier.Verifiables(testStrings, maxCandidates=50);
modelS = base.Structural( ...
reducedForm=modelR, ...
identifier=id2 ...
);
modelS.initialize()
info = modelS.presample(100);
info
%% Using structural VAR
shkTbx = modelS.estimateShocks();
simTbx = modelS.simulateResponses();
contTbx = modelS.breakdown();
fcastTbx = modelS.forecast(fcastSpan);
fevdTbx = modelS.calculateFEVD();
tablex.getHigherDims(contTbx)
contMedTbx = tablex.apply(contTbx, medianFunc);
contMedTbx = tablex.apply(contMedTbx, flatFunc);
ch = visual.Chartpack( ...
span=datex.span("2010-Q1", "2014-Q4"), ...
namesToPlot=modelS.Meta.EndogenousNames, ...
captions="Breakdown of historical observations (Median)", ...
plotFunc=@bar ...
);
ch.plot(contMedTbx);
leg = tablex.getHigherDims(contMedTbx);
legend(leg{1});