Repository navigation
Expand file tree
/
Copy pathexactZeroRestrictions.m
More file actions
274 lines (189 loc) · 6.33 KB
/
Copy pathexactZeroRestrictions.m
File metadata and controls
274 lines (189 loc) · 6.33 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
270
271
272
273
274
%% Exact zero restrictions
% * Prepare a reduced-form model for experiments with zero restrictions
% * Identify a SVAR using Cholesky with reordering
% * Identify a SVAR based on exact zero restrictions only
% * Combine exact zero restrictions with general restrictions
% * Use tables to specify sign restrictions
clear
close all
rehash path
addpath ../sandbox
addpath ../sandbox/gui
addpath ../bear
%% Convenience functions
% The `extremesFunc` function compresses any number of samples (draws from the
% posterior) into two numbers - the minimum and the maximum.
extremesFunc = @(x) [min(x, [], 2), max(x, [], 2)];
%% Prepare data and a reduced-form model
%%
% * Same as in `introCommonTasks`
inputTbx = tablex.fromCsv("exampleData.csv");
estimStart = datex.q(1975,1);
estimEnd = datex.q(2014,4);
estimSpan = datex.span(estimStart, estimEnd);
meta = model.Meta( ...
endogenous=["DOM_GDP", "DOM_CPI", "STN"], ...
units="", ...
exogenous="Oil", ...
order=4, ...
intercept=true, ...
estimationSpan=estimSpan, ...
...
identificationHorizon=12, ...
shockConcepts=["DEM", "SUP", "POL"] ...
);
dataH = model.DataHolder(meta, inputTbx);
estimatorR = estimator.NormalWishart(meta);
modelR = model.ReducedForm( ...
meta=meta ...
, dataHolder=dataH ...
, estimator=estimatorR ...
, stabilityThreshold=Inf ...
);
return
%% Indentify a SVAR using Cholesky with reordering
% * Use Cholesky as if the endogenous variables were ordered in a different
% way than in meta
% * If a certain trailing portion of the order follows the meta order, you can
% omit that part
identChol = identifier.Cholesky(order=["DOM_CPI", "DOM_GDP", "STN"]);
% Equivalent to
% identChol = identifier.Cholesky(order=["DOM_CPI"]);
modelS0 = model.Structural(reducedForm=modelR, identifier=identChol);
modelS0
modelS0.initialize();
info0 = modelS0.presample(100);
modelS0.Presampled{1}.D
modelS0.Presampled{2}.D
respTbx0 = modelS0.simulateResponses();
respTbx0 = tablex.apply(respTbx0, extremesFunc);
respTbx0 = tablex.flatten(respTbx0);
respTbx0
decompTbx0 = modelS0.breakdown();
%% Specify exact zero restrictions
% * Create an empty table for exact zero restrictions using `tablex.forInstantZeros`
% * The table has endogenous variables in rows, shocks in columns
% * Fill in zeros for the elements to be restricted
% * The algorithm can only handle *underdetermined* systems, meaning it must
% have at least one "degree of freedom"; this means the max number of zero restrictions
% is `n * (n-1) / 2 - 1`
% * The algorithm is able handle an edge case of no restrictions - it simply
% produces fully randomized unconstrained factors of the covariance matrix
instantZerosTbx = tablex.forInstantZeros(modelR);
instantZerosTbx{"DOM_CPI", "DEM"} = 0;
instantZerosTbx{"STN", "SUP"} = 0;
instantZerosTbx
%% Identify a SVAR using exact zero restrictions
%%
% * Create an "exact zero" identifier from the exact zeros table
% * Use this identifier to set up a SVAR object
% * Initialize and presample...
identInstantZeros = identifier.InstantZeros(instantZerosTbx);
identInstantZeros
modelS1 = model.Structural(reducedForm=modelR, identifier=identInstantZeros);
modelS1
modelS1.initialize();
info1 = modelS1.presample(100);
respTbx1 = modelS1.simulateResponses();
respTbx1 = tablex.apply(respTbx1, extremesFunc);
respTbx1 = tablex.flatten(respTbx1);
respTbx1
%% Identify a SVAR using sign restrictions
%%
% * Same as in `introCommonTasks`
%%
%
rng(0);
signStrings = [
"$SHKRESP(2, 'DOM_GDP', 'DEM') > 0"
"$SHKRESP(2, 'DOM_CPI', 'DEM') > 0"
"$SHKRESP(3, 'DOM_GDP', 'DEM') > 0"
"$SHKRESP(3, 'DOM_CPI', 'DEM') > 0"
"$SHKRESP(2, 'DOM_GDP', 'SUP') < 0"
"$SHKRESP(2, 'DOM_CPI', 'SUP') > 0"
"$SHKRESP(3, 'DOM_GDP', 'SUP') < 0"
"$SHKRESP(3, 'DOM_CPI', 'SUP') > 0"
]
identVerifiables = identifier.Verifiables( ...
signStrings, ...
maxCandidates=50, ...
shortCircuit=false ...
);
modelS2 = model.Structural( ...
reducedForm=modelR, ...
identifier=identVerifiables ...
);
modelS2
modelS2.initialize();
info2 = modelS2.presample(100);
info2
respTbx2 = modelS2.simulateResponses();
respTbx2 = tablex.apply(respTbx2, extremesFunc);
respTbx2 = tablex.flatten(respTbx2);
respTbx2
%% Report details of acceptance statistics
% * Extract the true-false information about success/fail for in
trackers = [];
for s = modelS2.Presampled
trackers = [trackers, s{:}.Tracker]; %#ok<AGROW>
end
statsTbl = table(mean(trackers, 2), variableNames="Success rate", rowNames=signStrings);
statsTbl
%% Use a table to specify sign restrictions as a convenience feature
% * Create an "empty" table designed specifically for sign restrictions using
% `tablex.forSignRestrictions`
% * Fill in `1`s and `-1`s for positive and negative signs
% * The table has variable names in rows, shock names in columns, and each table
% element is a vector of `NaN`s, `1`s and `-1`s corresponding to the periods of
% the identification horizon
rng(0);
signTbx = tablex.forSignRestrictions(modelR);
signTbx{"DOM_GDP", "DEM"}(2:3) = 1;
signTbx{"DOM_CPI", "DEM"}(2:3) = 1;
signTbx{"DOM_GDP", "SUP"}(2:3) = -1;
signTbx{"DOM_CPI", "SUP"}(2:3) = 1;
testStrings = tablex.toVerifiables(signTbx);
testStrings
identVerifiables = identifier.Verifiables( ...
signTbx, ...
maxCandidates=50 ...
);
modelS3 = model.Structural( ...
reducedForm=modelR, ...
identifier=identVerifiables ...
);
modelS3
modelS3.initialize();
info3 = modelS3.presample(100);
info3
respTbx3 = modelS3.simulateResponses();
respTbx3 = tablex.apply(respTbx3, extremesFunc);
respTbx3 = tablex.flatten(respTbx3);
respTbx3
%% Identify a SVAR combining exact zeros and signs
rng(0);
signTbx = tablex.forSignRestrictions(modelR);
signTbx{"DOM_GDP", "DEM"}(2:3) = 1;
signTbx{"DOM_CPI", "DEM"}(2:3) = 1;
signTbx{"DOM_GDP", "SUP"}(2:3) = -1;
signTbx{"DOM_CPI", "SUP"}(2:3) = 1;
testStrings = tablex.toVerifiables(signTbx);
testStrings
identVerifiables = identifier.Verifiables( ...
signTbx, ...
instantZeros=identInstantZeros, ...
maxCandidates=50 ...
);
modelS4 = model.Structural( ...
reducedForm=modelR, ...
identifier=identVerifiables ...
);
modelS4
modelS4.initialize();
info4 = modelS4.presample(100);
info4
respTbx4 = modelS4.simulateResponses();
respTbx4 = tablex.apply(respTbx4, extremesFunc);
respTbx4 = tablex.flatten(respTbx4);
decomp = modelS4.breakdown();
respTbx4