-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXModelsPanel.m
More file actions
203 lines (141 loc) · 4.64 KB
/
Copy pathXModelsPanel.m
File metadata and controls
203 lines (141 loc) · 4.64 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
%% TV models
%%
% * Prepare time-varying models
% * Identify a SVAR using Cholesky
% * Checking TV features of the model (beta/sigma) in estimation/IRF/forecast
% * test panel
clear
close all
rehash path
addpath ../sandbox
addpath ../bear
%% Convenience functions
%
percentiles = [10, 50, 90];
prctileFunc = @(x) prctile(x, percentiles, 2);
medianFunc = @(x) prctile(x, 50, 2);
extremesFunc = @(x) [min(x, [], 2), max(x, [], 2)];
numPresampled = 100;
%% Prepare data and a reduced-form model
inputTbx = tablex.fromCsv("panel_data.csv");
estimStart = datex.q(1972,1);
estimEnd = datex.q(2014,4);
estimSpan = datex.span(estimStart, estimEnd);
meta = model.Meta( ...
endogenous=["YER", "HICSA", "STN"], ...
units=["US", "EA", "UK"], ...
exogenous=["Oil"], ...
order=4, ...
intercept=true, ...
estimationSpan=estimSpan, ...
...
identificationHorizon=20, ...
shockConcepts=["DEM", "SUP", "POL"] ...
);
dataH = model.DataHolder(meta, inputTbx);
%% No CrossSection
% Panel model (NormalWishartPanel)
estimatorR1 = estimator.DynamicCrossPanel(meta);
modelR1 = model.ReducedForm( ...
meta=meta ...
, dataHolder=dataH ...
, estimator=estimatorR1 ...
, stabilityThreshold=Inf ...
);
modelR1.Estimator.Settings
%%
% and General time-varying (i.e parameters and covariance are both TV)
% estimatorR2 = estimator.GeneralTV(meta);
%
% modelR2 = model.ReducedForm( ...
% meta=meta ...
% , dataHolder=dataH ...
% , estimator=estimatorR2 ...
% , stabilityThreshold=Inf ...
% );
% modelR2.Estimator.Settings
%% Indentify a SVAR using Cholesky (without reordering)
% Panel model (NormalWishartPanel)
identChol = identifier.Cholesky(order=[]);
modelS1 = model.Structural(reducedForm=modelR1, identifier=identChol);
modelS1.initialize();
info1 = modelS1.presample(numPresampled);
modelS1.Presampled{1}
modelS1.Presampled{1}.IdentificationDraw
modelS1.Presampled{1}.IdentificationDraw.A{1,1}
modelS1.Presampled{1}.IdentificationDraw.A{2,1}
%%
% and General time-varying
% modelS2 = model.Structural(reducedForm=modelR2, identifier=identChol);
% modelS2.initialize();
% info2 = modelS2.presample(numPresampled);
% modelS2.Presampled{1}
% modelS2.Presampled{1}.IdentificationDraw
% modelS2.Presampled{1}.IdentificationDraw.A{1,1}
% modelS2.Presampled{1}.IdentificationDraw.A{2,1}
%% Impulse responses
% Panel model (NormalWishartPanel)
respTbx1 = modelS1.simulateResponses();
respTbx1 = tablex.apply(respTbx1, prctileFunc);
respTbx1 = tablex.flatten(respTbx1);
respTbx1
tablex.plot(respTbx1,"US_YER___US_DEM")
%%
% and General time-varying
% respTbx2 = modelS2.simulateResponses();
% respTbx2 = tablex.apply(respTbx2, prctileFunc);
% respTbx2 = tablex.flatten(respTbx2);
%
% respTbx2
%
% tablex.plot(respTbx2,"YER___DEM")
%% Unconditional forecast
fcastStart = datex.shift(modelS1.Meta.EstimationEnd, -10);
fcastEnd = datex.shift(modelS1.Meta.EstimationEnd, 0);
fcastSpan = datex.span(fcastStart, fcastEnd);
% Panel model (NormalWishartPanel)
histContTbx = modelS1.calculateContributions();
fcastTbx1 = modelS1.forecast(fcastSpan);
fcastPrctileTbx1 = tablex.apply(fcastTbx1, prctileFunc);
fcastPrctileTbx1 = tablex.flatten(fcastPrctileTbx1);
fcastTbx1
figure();
tablex.plot( ...
fcastPrctileTbx1, "US_YER", ...
plotSettings={{"lineStyle"}, {":"; "-"; ":"}} ...
);
% General time-varying
% fcastTbx2 = modelS2.forecast(fcastSpan);
%
% fcastPrctileTbx2 = tablex.apply(fcastTbx2, prctileFunc);
% fcastPrctileTbx2 = tablex.flatten(fcastPrctileTbx2);
%
% fcastTbx2
%
% tablex.plot( ...
% fcastPrctileTbx2, "YER", ...
% plotSettings={{"lineStyle"}, {":"; "-"; ":"}} ...
% );
%% Decomposition of the unconditional forecast
% Simulate the forecast including the decomposition into contributions
[fcastTbx1, fcastContTbx1] = modelS1.forecast( ...
fcastSpan, ...
contributions=true ...
);
% Calculate median contributions
medianContTbx1 = tablex.apply(fcastContTbx1, medianFunc);
% Extract labels (used later in the chart)
contLabels = tablex.getHigherDims(medianContTbx1);
contLabels = contLabels{1};
% Extract contributions of shocks
medianContShocks = tablex.apply(medianContTbx1, @(x) x(:, :, 1:end-2));
% Extract contributions of exogenous variables including intercept
medianContExog = tablex.apply(medianContTbx1, @(x) x(:, :, end-1));
% Extract contributions of initial condition
medianContInit = tablex.apply(medianContTbx1, @(x) x(:, :, end));
figure();
hold on
tablex.plot(medianContShocks, "US_YER", plotFunc="bar");
tablex.plot(medianContExog, "US_YER", plotSettings={"color", "black", "lineWidth", 2});
tablex.plot(medianContInit, "US_YER", plotSettings={"color", "red", "lineWidth", 2});
legend(contLabels);