-
Notifications
You must be signed in to change notification settings - Fork 33
/
ModelSolution.m
190 lines (157 loc) · 6.02 KB
/
ModelSolution.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
function [ Info, M, options, oo, dynareOBC ] = ModelSolution( SkipResol, M, options, oo, dynareOBC, SlowMode )
if nargin < 6
SlowMode = true;
end
if SlowMode
fprintf( '\n' );
disp( 'Solving the model for specific parameters.' );
fprintf( '\n' );
end
ns = dynareOBC.NumberOfMax;
% temporary work around for warning in dates object.
options.initial_date = [];
options.initial_period = [];
options.dataset = [];
if SkipResol
Info = 0;
else
[ dr, Info, M, options, oo ] = resol( 0, M, options, oo );
oo.dr = dr;
if Info ~= 0
return
end
end
if options.order > 1
if ( ~isfield( oo.dr, 'ghs2' ) ) || ( ~isfield( oo.dr, 'ghxx' ) ) || ( ~isfield( oo.dr, 'ghxu' ) ) || ( ~isfield( oo.dr, 'ghuu' ) )
options.order = 1;
dynareOBC.Order = 1;
dynareOBC.FirstOrderAroundRSS1OrMean2 = 0;
fprintf( '\n' );
disp( 'Falling back on a first order approximation as your model appears to be linear apart from any constraints.' );
fprintf( '\n' );
elseif options.order > 2
if ( ~isfield( oo.dr, 'ghxxx' ) ) || ( ~isfield( oo.dr, 'ghxxu' ) ) || ( ~isfield( oo.dr, 'ghxuu' ) ) || ( ~isfield( oo.dr, 'ghuuu' ) )
options.order = 2;
dynareOBC.Order = 2;
fprintf( '\n' );
disp( 'Falling back on a second order approximation as dynare did not generate a complete third order approximation for your model.' );
fprintf( '\n' );
end
end
end
if dynareOBC.FirstOrderAroundRSS1OrMean2 > 0
if dynareOBC.Sparse
if SlowMode
fprintf( '\n' );
disp( 'Converting to sparse matrices.' );
fprintf( '\n' );
end
DRFieldNames = fieldnames( oo.dr );
for i = 1 : length( DRFieldNames )
oo.dr.( DRFieldNames{i} ) = spsparse( oo.dr.( DRFieldNames{i} ) );
end
M.Sigma_e = spsparse( M.Sigma_e );
end
if SlowMode
fprintf( '\n' );
disp( 'Computing the first order approximation around the selected non-steady-state point.' );
fprintf( '\n' );
end
deflect_ = compute_deflected_linear_approximation( M, options, oo, dynareOBC.FirstOrderAroundRSS1OrMean2 );
else
deflect_ = [];
end
if ~isempty( deflect_ )
dynareOBC.Order = 1;
dynareOBC.Constant = deflect_.y;
if any( dynareOBC.Constant( ( end - dynareOBC.NumberOfMax + 1 ) : end ) < 0 )
Info = 19090714;
return
end
oo.dr.ys = deflect_.y;
oo.dr.ghx = deflect_.y_x;
oo.dr.ghu = deflect_.y_u;
end
oo.steady_state = oo.dr.ys;
if dynareOBC.Sparse
if SlowMode
fprintf( '\n' );
disp( 'Converting to sparse matrices.' );
fprintf( '\n' );
end
DRFieldNames = fieldnames( oo.dr );
for i = 1 : length( DRFieldNames )
oo.dr.( DRFieldNames{i} ) = spsparse( oo.dr.( DRFieldNames{i} ) );
end
M.Sigma_e = spsparse( M.Sigma_e );
end
if SlowMode
fprintf( '\n' );
disp( 'Saving NLMA parameters.' );
fprintf( '\n' );
end
[ EmptySimulation, oo.dr ] = LanMeyerGohdePrunedSimulation( M, oo.dr, [], 0, dynareOBC.Order, 0 );
dynareOBC.Constant = EmptySimulation.constant;
if any( dynareOBC.Constant( ( end - dynareOBC.NumberOfMax + 1 ) : end ) < 0 )
Info = 19090714;
return
end
dynareOBC.SelectState = ( M.nstatic + 1 ):( M.nstatic + M.nspred );
if ns > 0
if SlowMode
fprintf( '\n' );
disp( 'Retrieving IRFs to shadow shocks.' );
fprintf( '\n' );
end
dynareOBC = GetIRFsToShadowShocks( M, oo, dynareOBC );
if SlowMode
fprintf( '\n' );
disp( 'Preparing normalized sub-matrices.' );
fprintf( '\n' );
end
dynareOBC = PrepareNormalizedSubMatrices( dynareOBC, SlowMode );
end
if SlowMode
fprintf( '\n' );
disp( 'Pre-calculating the augmented state transition matrices and possibly conditional covariances.' );
fprintf( '\n' );
end
dynareOBC = CacheConditionalCovariancesAndAugmentedStateTransitionMatrices( M, options, oo, dynareOBC );
dynareOBC.FullNumVarExo = M.exo_nbr;
% if SlowMode
% fprintf( '\n' );
% disp( 'Reducing the size of decision matrices.' );
% fprintf( '\n' );
% end
%
% [ M, oo, dynareOBC ] = ReduceDecisionMatrices( M, oo, dynareOBC );
dynareOBC.ZeroVecS = sparse( dynareOBC.TimeToEscapeBounds * ns, 1 );
dynareOBC.ParametricSolutionFound = zeros( dynareOBC.TimeToEscapeBounds, 1 );
if SlowMode
if ~exist( [ 'dynareOBCTempCustomLanMeyerGohdePrunedSimulation.' mexext ], 'file' ) && ( dynareOBC.CompileSimulationCode || dynareOBC.Estimation || dynareOBC.Smoothing )
fprintf( '\n' );
disp( 'Attempting to build a custom version of the simulation code.' );
fprintf( '\n' );
try
BuildCustomLanMeyerGohdePrunedSimulation( M, oo, dynareOBC, dynareOBC.Estimation );
catch Error
warning( 'dynareOBC:FailedCompilingCustomLanMeyerGohdePrunedSimulation', [ 'Failed to compile a custom version of the simulation code, due to the error: ' Error.message ] );
dynareOBC.UseSimulationCode = false;
end
end
if ns > 0
fprintf( '\n' );
disp( 'Performing initial checks on the model.' );
fprintf( '\n' );
dynareOBC = InitialChecks( dynareOBC );
end
end
if ns > 0
if SlowMode
fprintf( '\n' );
disp( 'Forming optimizer.' );
fprintf( '\n' );
end
dynareOBC = FormOptimizer( dynareOBC );
end
end