File: write_expectations.m

package info (click to toggle)
dynare 6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,648 kB
  • sloc: cpp: 79,109; ansic: 28,917; objc: 12,430; yacc: 4,528; pascal: 1,993; lex: 1,441; sh: 1,129; python: 634; makefile: 626; lisp: 163; xml: 18
file content (306 lines) | stat: -rw-r--r-- 15,127 bytes parent folder | download | duplicates (2)
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
function [expression, growthneutralitycorrection] = write_expectations(expectationmodelname, expectationmodelkind, iscrlf, aggregate)

% Prints the exansion of the VAR_EXPECTATION or PAC_EXPECTATION term in files.
%
% INPUTS
% - epxpectationmodelname       [string]    Name of the expectation model.
% - expectationmodelkind        [string]    Kind of the expectation model ('var' or 'pac').
% - iscrlf                      [string]    Adds carriage return after each additive term if true.
%
% OUTPUTS
% - expression                  [string]    Unrolled expectation expression.
% - growthneutralitycorrection  [string]

% Copyright © 2019-2023 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.

global M_

if ismember(expectationmodelkind, {'var', 'pac'})
    if isequal(expectationmodelkind, 'var')
        expectationmodelfield = 'var_expectation';
    else
        expectationmodelfield = 'pac';
    end
else
    error('Value of third input argument must be ''var'' or ''pac''.')
end

expectationmodel = M_.(expectationmodelfield).(expectationmodelname);

if isfield(expectationmodel, 'model_consistent_expectations') && expectationmodel.model_consistent_expectations
    expression = '';
    if nargout>1
        growthneutralitycorrection = 0;
    end
    return
end

if nargout>1 && isequal(expectationmodelkind, 'var')
    error('Cannot return more than one argument if the expectation model is a VAR.')
end

if nargin<3
    iscrlf = false;
    aggregate = true;
end

if nargin<4
    aggregate = true;
end

if isfield(expectationmodel, 'h_param_indices')
    % Disaggregation requires components...
    aggregate = true;
end

% Get the name of the associated VAR model and test its existence.
if ~isfield(M_.(expectationmodel.auxiliary_model_type), expectationmodel.auxiliary_model_name)
    switch expectationmodelkind
      case 'var-expectations'
        error('Unknown VAR/TREND_COMPONENT model (%s) in VAR_EXPECTATION_MODEL (%s)!', expectationmodel.auxiliary_model_name, expectationmodelname)
      case 'pac-expectations'
        error('Unknown VAR/TREND_COMPONENT model (%s) in PAC_EXPECTATION_MODEL (%s)!', expectationmodel.auxiliary_model_name, expectationmodelname)
      otherwise
    end
end

auxmodel = M_.(expectationmodel.auxiliary_model_type).(expectationmodel.auxiliary_model_name);

maxlag = max(auxmodel.max_lag);
if isequal(expectationmodel.auxiliary_model_type, 'trend_component')
    % Need to add a lag since the error correction equations are rewritten in levels.
    maxlag = maxlag+1;
end

id = 0;

if isequal(expectationmodelkind, 'var')
    timeindices = (0:(maxlag-1))+abs(expectationmodel.time_shift);
end

if isequal(expectationmodelkind, 'var') && isequal(expectationmodel.auxiliary_model_type, 'var')
    id = id+1;
    expression = sprintf('%s', M_.param_names{expectationmodel.param_indices(id)});
end

if isequal(expectationmodelkind, 'pac') && isequal(expectationmodel.auxiliary_model_type, 'var')
    id = id+1;
    if isfield(expectationmodel, 'h_param_indices')
        expression = sprintf('%s', M_.param_names{expectationmodel.h_param_indices(id)});
    else
        if aggregate
            if isequal(expectationmodel.components(1).coeff_str, '1')
                expression = sprintf('%s', M_.param_names{expectationmodel.components(1).h_param_indices(id)});
            else
                expression = sprintf('%s*%s', expectationmodel.components(1).coeff_str, M_.param_names{expectationmodel.components(1).h_param_indices(id)});
            end
            for i=2:length(expectationmodel.components)
                if isequal(expectationmodel.components(i).coeff_str, '1')
                    expression = sprintf('%s+%s', expression, M_.param_names{expectationmodel.components(i).h_param_indices(id)});
                else
                    expression = sprintf('%s+%s*%s', expression, expectationmodel.components(i).coeff_str, M_.param_names{expectationmodel.components(i).h_param_indices(id)});
                end
            end
        else
            expression = cell(length(expectationmodel.components), 1);
            for i=1:length(expectationmodel.components)
                expression(i) = {M_.param_names{expectationmodel.components(i).h_param_indices(id)}};
            end
        end
    end
end

for i=1:maxlag
    for j=1:length(auxmodel.list_of_variables_in_companion_var)
        id = id+1;
        variable = auxmodel.list_of_variables_in_companion_var{j};
        transformations = {};
        ida = get_aux_variable_id(variable);
        op = 0;
        while ida
            op = op+1;
            if isequal(M_.aux_vars(ida).type, 8)
                transformations(op) = {'diff'};
                variable = M_.endo_names{M_.aux_vars(ida).orig_index};
                ida = get_aux_variable_id(variable);
            elseif isequal(M_.aux_vars(ida).type, 10)
                transformations(op) = {M_.aux_vars(ida).unary_op};
                variable = M_.endo_names{M_.aux_vars(ida).orig_index};
                ida = get_aux_variable_id(variable);
            else
                error('This case is not implemented.')
            end
        end
        switch expectationmodelkind
          case 'var'
            parameter = M_.param_names{expectationmodel.param_indices(id)};
          case 'pac'
            if isfield(expectationmodel, 'h_param_indices')
                parameter = M_.param_names{expectationmodel.h_param_indices(id)};
            else
                if aggregate
                    % TODO Check if we can have parameters entering with a minus sign in the linear combination defining the target.
                    if isequal(expectationmodel.components(1).coeff_str, '1')
                        parameter = M_.param_names{expectationmodel.components(1).h_param_indices(id)};
                    else
                        parameter = sprintf('%s*%s', expectationmodel.components(1).coeff_str, M_.param_names{expectationmodel.components(1).h_param_indices(id)});
                    end
                    for k=2:length(expectationmodel.components)
                        if isequal(expectationmodel.components(k).coeff_str, '1')
                            parameter = sprintf('%s+%s', parameter, M_.param_names{expectationmodel.components(k).h_param_indices(id)});
                        else
                            parameter = sprintf('%s+%s*%s', parameter, expectationmodel.components(k).coeff_str, M_.param_names{expectationmodel.components(k).h_param_indices(id)});
                        end
                    end
                    parameter = sprintf('(%s)', parameter);
                else
                    parameter = cell(length(expectationmodel.components), 1);
                    for k=1:length(expectationmodel.components)
                        parameter(k) = {M_.param_names{expectationmodel.components(k).h_param_indices(id)}};
                    end
                end
            end
          otherwise
        end
        switch expectationmodelkind
          case 'var'
            if timeindices(i)
                variable = sprintf('%s(-%d)', variable, timeindices(i));
            end
          case 'pac'
            variable = sprintf('%s(-%d)', variable, i);
          otherwise
        end
        if ~isempty(transformations)
            for k=length(transformations):-1:1
                variable = sprintf('%s(%s)', transformations{k}, variable);
            end
        end
        if isequal(id, 1)
            if aggregate
                if iscrlf
                    expression = sprintf('%s*%s\n', parameter, variable);
                else
                    expression = sprintf('%s*%s', parameter, variable);
                end
            else
                for k=1:length(expectationmodel.components)
                    if iscrlf
                        expression(k) = {sprintf('%s*%s\n', parameter{k}, variable)};
                    else
                        expression(k) = {sprintf('%s*%s', parameter{k}, variable)};
                    end
                end
            end
        else
            if aggregate
                if iscrlf
                    expression = sprintf('%s + %s*%s\n', expression, parameter, variable);
                else
                    expression = sprintf('%s + %s*%s', expression, parameter, variable);
                end
            else
                for k=1:length(expectationmodel.components)
                    if iscrlf
                        expression(k) = {sprintf('%s + %s*%s\n', expression{k}, parameter{k}, variable)};
                    else
                        expression(k) = {sprintf('%s + %s*%s', expression{k}, parameter{k}, variable)};
                    end
                end
            end
        end
    end
end

if aggregate
    growthneutralitycorrection = '';
else
    growthneutralitycorrection = {};
end

if isfield(expectationmodel, 'growth_neutrality_param_index')
    if numel(expectationmodel.growth_linear_comb) == 1
        growthneutralitycorrection = sprintf('%s*%s', M_.param_names{expectationmodel.growth_neutrality_param_index}, expectationmodel.growth_str);
    else
        growthneutralitycorrection = sprintf('%s*(%s)', M_.param_names{expectationmodel.growth_neutrality_param_index}, expectationmodel.growth_str);
    end
else
    if isfield(expectationmodel, 'components')
        if aggregate
            growthneutralitycorrection = '';
            for i=1:length(expectationmodel.components)
                if ~isequal(expectationmodel.components(i).kind, 'll')
                    if isfield(expectationmodel.components(i), 'growth_neutrality_param_index')
                        if isempty(growthneutralitycorrection)
                            if ~isempty(expectationmodel.components(i).growth_str)
                                if isequal(expectationmodel.components(i).coeff_str, '1')
                                    if numel(expectationmodel.components(i).growth_linear_comb) == 1
                                        growthneutralitycorrection = sprintf('%s*%s', M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    else
                                        growthneutralitycorrection = sprintf('%s*(%s)', M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    end
                                else
                                    if numel(expectationmodel.components(i).growth_linear_comb) == 1
                                        growthneutralitycorrection = sprintf('%s*%s*%s', expectationmodel.components(i).coeff_str, M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    else
                                        growthneutralitycorrection = sprintf('%s*%s*(%s)', expectationmodel.components(i).coeff_str, M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    end
                                end
                            end
                        else
                            if ~isempty(expectationmodel.components(i).growth_str)
                                if isequal(expectationmodel.components(i).coeff_str, '1')
                                    if numel(expectationmodel.components(i).growth_linear_comb) == 1
                                        growthneutralitycorrection = sprintf('%s+%s*%s', growthneutralitycorrection, M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    else
                                        growthneutralitycorrection = sprintf('%s+%s*(%s)', growthneutralitycorrection, M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    end
                                else
                                    if numel(expectationmodel.components(i).growth_linear_comb) == 1
                                        growthneutralitycorrection = sprintf('%s+%s*%s*%s', growthneutralitycorrection, expectationmodel.components(i).coeff_str, M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    else
                                        growthneutralitycorrection = sprintf('%s+%s*%s*(%s)', growthneutralitycorrection, expectationmodel.components(i).coeff_str, M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str);
                                    end
                                end
                            end
                        end
                    end % if growth neutrality correction for this component
                end % if non stationary component
            end
        else
            growthneutralitycorrection = repmat({''}, length(expectationmodel.components), 1);
            for i=1:length(growthneutralitycorrection)
                if ~isequal(expectationmodel.components(i).kind, 'll')
                    if isfield(expectationmodel.components(i), 'growth_neutrality_param_index')
                        if ~isempty(expectationmodel.components(i).growth_str)
                            if numel(expectationmodel.components(i).growth_linear_comb) == 1
                                growthneutralitycorrection(i) = {sprintf('%s*%s', M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str)};
                            else
                                growthneutralitycorrection(i) = {sprintf('%s*(%s)', M_.param_names{expectationmodel.components(i).growth_neutrality_param_index}, expectationmodel.components(i).growth_str)};
                            end
                        end
                    end % if growth neutrality correction for this component
                end % if non stationary component
            end
        end % if aggregate
    end
end

if nargout==1 && ~isempty(growthneutralitycorrection)
    expression = sprintf('%s + %s', expression, growthneutralitycorrection);
end