-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXConditionalForecasts.m
More file actions
306 lines (235 loc) · 7.67 KB
/
Copy pathXConditionalForecasts.m
File metadata and controls
306 lines (235 loc) · 7.67 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
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
%% Conditional forecasts
%
% * Prepare a reduced-form model for experiments with zero restrictions
% * Prepare a table with conditions
% * Prepare a table with a "simulation plan"
% * Run and report an unconditional forecast
% * Run and report a conditional forecast using all shocks vs seletected shocks
% * Calculate and report the contributions of shocks, exogenous, initials
%
%% Housekeeping
clear
clear classes
close all
rehash path
addpath ../bear -end
addpath ../bearing -end
%% Define convenience functions
%
% The |extremesFunc| function compresses any number of samples (draws from the
% posterior) into two numbers - the minimum and the maximum.
percentiles = [10, 50, 90];
prctilesFunc = @(x) prctile(x, percentiles, 2);
medianFunc = @(x) median(x, 2);
extremesFunc = @(x) [min(x, [], 2), max(x, [], 2)];
defaultColors = get(0, "defaultAxesColorOrder");
%% Prepare meta information
estimStart = datex.q(1975,1);
estimEnd = datex.q(2014,4);
estimSpan = datex.span(estimStart, estimEnd);
meta = base.Meta( ...
endogenousNames=["DOM_GDP", "DOM_CPI", "STN"], ...
exogenousNames="Oil", ...
order=4, ...
intercept=true, ...
estimationSpan=estimSpan, ...
identificationHorizon=12, ...
shockNames=["DEM", "SUP", "POL"] ...
);
%% Prepare input data holder
inputTbl = tablex.fromCsv("exampleData.csv");
inputTbl = tablex.extend(inputTbl, -Inf, datex.q(2017,4));
inputTbl.Oil = fillmissing(inputTbl.Oil, "nearest");
dataH = base.DataHolder(meta, inputTbl);
%% Prepare reduced-form estimator
estimatorR = base.estimator.NormalWishart( ...
meta ...
);
%% Create reduced-form model
modelR = base.ReducedForm( ...
meta=meta ...
, dataHolder=dataH ...
, estimator=estimatorR ...
);
%% Initialize and presample the reduced-form model
modelR.initialize();
infoR = modelR.presample(100);
%% Identify 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 = base.identifier.Cholesky(order=["DOM_CPI", "DOM_GDP", "STN"]);
% Equivalent to
% identChol = identifier.Cholesky(order=["DOM_CPI"]);
modelS0 = base.Structural(reducedForm=modelR, identifier=identChol);
modelS0
modelS0.initialize();
info0 = modelS0.presample(100);
modelS0.Presampled{1}.D
modelS0.Presampled{2}.D
respTbl0 = modelS0.simulateResponses(includeInitial=false);
respTbl0 = tablex.apply(respTbl0, extremesFunc);
respTbl0 = tablex.flatten(respTbl0);
respTbl0
%% Create conditional forecast assumptions
fcastStart = datex.shift(estimEnd, 1);
fcastEnd = datex.shift(estimEnd, 12);
fcastSpan = datex.span(fcastStart, fcastEnd);
initStart = datex.shift(fcastStart, -modelS0.Meta.Order);
% Create empty table templates for conditioning data and a conditioning plan
% [condDataTbl0, planTbl0] = tablex.forConditional(modelS0, fcastSpan);
%
% condDataTbl0
% planTbl0
%
% % Populate the conditions table with some values
% condDataTbl0{datex("2015-Q4"), "DOM_GDP"} = -1.5;
% condDataTbl0{datex("2016-Q4"), "DOM_CPI"} = 5.5;
% condDataTbl0{datex("2016-Q3"), "STN"} = 5.1;
%
% condDataTbl0{:, "Oil"} = inputTbl{end, "Oil"};
% tablex.toFile(condDataTbl0, "condDataTbl.xlsx")
%
% planTbl0{datex("2015-Q4"), "DOM_GDP"} = "DEM POL";
% planTbl0{datex("2016-Q4"), "DOM_CPI"} = "DEM SUP";
% planTbl0{datex("2016-Q3"), "STN"} = "POL";
% tablex.toFile(planTbl0, "planTbl.xlsx");
%% Run across-the-board vs selective conditions forecasts
condDataTbl = tablex.fromFile("condDataTbl.xlsx");
planTbl = tablex.readConditioningPlan("planTbl.xlsx");
% Run an unconditional forecast and calculate contributions starting at the
% beginning of the forecast
[uncFcastTbl1, uncFcastContribTbl1] = modelS0.forecast( ...
fcastSpan, ...
contributions=true ...
);
% Run an unconditional forecast and calculate contributions starting at the
% beginning of the historical (estimation) span
% Calculate the contributions on the estimation span
histContribTbl = modelS0.calculateContributions();
[uncFcastTbl2, uncFcastContribTbl2] = modelS0.forecast( ...
fcastSpan, ...
contributions=true, ...
precontributions=histContribTbl ...
);
% Run a conditional forecast and calculate contributions starting at the
% beginning of the forecast span
[condFcastTbl1, condFcastContribTbl1] = modelS0.conditionalForecast( ...
fcastSpan, ...
conditions=condDataTbl, ...
plan=[] ...
);
[condFcastTbl2, condFcastContribTbl2] = modelS0.conditionalForecast( ...
fcastSpan, ...
conditions=condDataTbl, ...
plan=planTbl ...
);
condFcastPrctilesTbl1 = tablex.apply(condFcastTbl1, prctilesFunc);
condFcastPrctilesTbl2 = tablex.apply(condFcastTbl2, prctilesFunc);
% Run a conditional forecast and calculate contributions starting at the
% beginning of the forecast span
[condFcastTbl1, condFcastContribTbl1] = modelS0.conditionalForecast( ...
fcastSpan, ...
conditions=condDataTbl, ...
plan=[], ...
contributions=true ...
);
% Run a conditional forecast and calculate contributions starting at the
% beginning of the historical (estimation) span
[condTbl2, condContTbl2] = modelS0.conditionalForecast( ...
fcastSpan, ...
conditions=condDataTbl, ...
plan=[], ...
contributions=true, ...
precontributions=histContribTbl ...
);
% %% Visualize conditional forecasts
% %
%
% plotSettings = { ...
% {"color"}, {defaultColors(2,:); defaultColors(1,:); defaultColors(2,:)}, ...
% {"lineStyle"}, {":";"-";":"}, ...
% };
%
% ch = visual.Chartpack( ...
% span=datex.span(initStart, fcastEnd), ...
% namesToPlot=[modelS0.Meta.EndogenousNames, modelS0.Meta.ShockNames], ...
% plotSettings=plotSettings ...
% );
%
% ch.Captions = "Across-the-board conditional forecast";
% ch.plot(condPrctilesTbl1);
%
% ch.Captions = "Selective conditional forecast";
% % ch.plot(condPrctilesTbl2);
%
%
% %% Calculate median contributions, merge with history
%
% histContribMedianTbl = tablex.apply(histContTbl, medianFunc);
%
% condContribMedianTbl1 = tablex.apply(condContTbl1, medianFunc);
%
% condContribMedianTbl2 = tablex.apply(condContTbl2, medianFunc);
%
%
% % Merge history and conditional forecast contributions
%
% allContribMedianTbl = tablex.merge( ...
% histContribMedianTbl, ...
% condContribMedianTbl2 ...
% );
%
%
% %% Visualize contributions
%
% legendEntries = tablex.getHigherDims(allContribMedianTbl, 3);
%
% plotFunc = @(tbl, name, span) {
% tablex.plot(tbl, name, periods=span, plotFunc=@bar, dims={1:3})
% tablex.plot(tbl, name, periods=span, dims={4}, plotSettings={"lineWidth", 4})
% tablex.plot(tbl, name, periods=span, dims={5}, plotSettings={"lineWidth", 4})
% title(name, interpreter="none")
% };
%
%
% % Create three figures:
% % Fig 1: Forecast span, no precontributions
% % Fig 2: Forecast span, including precontributions
% % Fig 3: Entire span (history & forecast)
%
% fig1 = figure(name="Contributions with no precontributions");
% fig2 = figure(name="Contributions including precontributions");
% fig3 = figure(name="Contributions, all history and forecast");
%
%
% % Loop over and plot the endogenous names
%
% for i = 1 : 3
%
% name = modelS0.Meta.EndogenousNames(i);
%
% figure(fig1);
% subplot(2, 2, i);
% hold on;
% plotFunc(condContribMedianTbl1, name, fcastSpan);
% legend(legendEntries);
%
% figure(fig2);
% subplot(2, 2, i);
% hold on;
% plotFunc(allContribMedianTbl, name, fcastSpan);
% legend(legendEntries);
%
% figure(fig3);
% subplot(2, 2, i);
% hold on;
% plotFunc(allContribMedianTbl, name, Inf);
% xline(fcastSpan(1)-0.5, lineWidth=3);
% legend([legendEntries, "Start of forecast"]);
%
% end
%
%