File: smoother2histval.m

package info (click to toggle)
dynare 4.6.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 74,896 kB
  • sloc: cpp: 98,057; ansic: 28,929; pascal: 13,844; sh: 5,947; objc: 4,236; yacc: 4,215; makefile: 2,583; lex: 1,534; fortran: 877; python: 647; ruby: 291; lisp: 152; xml: 22
file content (237 lines) | stat: -rw-r--r-- 8,411 bytes parent folder | download
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
function smoother2histval(opts)
% This function takes values from oo_.SmoothedVariables (and possibly
% oo_.SmoothedShocks) and copies them into M_.histval.
%
% Optional fields in 'opts' structure:
%    infile:      An optional *_results MAT file created by Dynare.
%                 If present, oo_.Smoothed{Variables,Shocks} are read from
%                 there. Otherwise, they are read from the global workspace.
%    invars:      An optional char or cell array listing variables to read in
%                 oo_.SmoothedVariables. If absent, all the endogenous
%                 variables present in oo_.SmoothedVariables are used.
%    period:      An optional period number to use as the starting point
%                 for subsequent simulations. It should be between 1 and
%                 the number of observations that were used to produce the
%                 smoothed values. If absent, the last observation is used.
%    outfile:     An optional MAT file in which to save the histval structure.
%                 If absent, the output will be written in M_.endo_histval
%    outvars:     An optional char or cell array listing variables to be written in
%                 outfile or M_.endo_histval. This cell must be of same
%                 length than invars, and there is a mapping between the input
%                 variable at the i-th position in invars, and the output
%                 variable at the i-th position in outvars. If absent, then
%                 taken as equal to invars.
%
% The function also uses the value of option_.parameter_set

% Copyright (C) 2014-2018 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 <http://www.gnu.org/licenses/>.

global M_ options_ oo_

if ~isfield(opts, 'infile')
    if ~isfield(oo_, 'SmoothedVariables')
        error('Could not find smoothed variables; did you set the "smoother" option?')
    end
    smoothedvars = oo_.SmoothedVariables;
    smoothedshocks = oo_.SmoothedShocks;
    steady_state = oo_.steady_state;
else
    S = load(opts.infile);
    if ~isfield(S, 'oo_') || ~isfield(S.oo_, 'SmoothedVariables')
        error('Could not find smoothed variables in file; is this a Dynare results file, and did you set the "smoother" option when producing it?')
    end
    smoothedvars = S.oo_.SmoothedVariables;
    smoothedshocks = S.oo_.SmoothedShocks;
    steady_state = S.oo_.steady_state;
end

% Hack to determine if oo_.SmoothedVariables was computed after a Metropolis
tmp = fieldnames(smoothedvars);
if isstruct(smoothedvars.(tmp{1}))
    post_metropolis = 1;
    if ~ isstruct(smoothedvars.(tmp{end}))
        % point and metropolis results are simultaneously present
        post_metropolis = 2;
    end

elseif isstruct(smoothedvars.(tmp{end}))
    % point and metropolis results are simultaneously present
    post_metropolis = 2;
else
    post_metropolis = 0;
end

if post_metropolis
    tmp = fieldnames(smoothedvars.Mean);
    tmpexo = fieldnames(smoothedshocks.Mean);
else
    tmp = fieldnames(smoothedvars);
    tmpexo = fieldnames(smoothedshocks);
end

% If post-Metropolis, select the parameter set
if isempty(options_.parameter_set)
    if post_metropolis
        smoothedvars = smoothedvars.Mean;
        smoothedshocks = smoothedshocks.Mean;
        steady_state = zeros(size(steady_state));
    end
else
    switch options_.parameter_set
      case 'calibration'
        if post_metropolis == 1
            error('Option parameter_set=calibration is not consistent with computed smoothed values.')
        end
      case 'posterior_mode'
        if post_metropolis == 1
            error('Option parameter_set=posterior_mode is not consistent with computed smoothed values.')
        end
      case 'posterior_mean'
        if ~post_metropolis
            error('Option parameter_set=posterior_mean is not consistent with computed smoothed values.')
        end
        smoothedvars = smoothedvars.Mean;
        smoothedshocks = smoothedshocks.Mean;
        steady_state = zeros(size(steady_state));
      case 'posterior_median'
        if ~post_metropolis
            error('Option parameter_set=posterior_median is not consistent with computed smoothed values.')
        end
        smoothedvars = smoothedvars.Median;
        smoothedshocks = smoothedshocks.Median;
        steady_state = zeros(size(steady_state));
      otherwise
        error([ 'Option parameter_set=' options_.parameter_set ' unsupported.' ])
    end
end

% Determine number of periods
n = size(smoothedvars.(tmp{1}));

if n < M_.maximum_endo_lag
    error('Not enough observations to create initial conditions')
end

if isfield(opts, 'invars')
    invars = opts.invars;
    if ischar(invars)
        invars = cellstr(invars);
    end
else
    invars = [tmp; tmpexo];
end

if isfield(opts, 'period')
    period = opts.period;
    if period > n
        error('The period that you indicated is beyond the data sample')
    end
    if period < M_.maximum_endo_lag
        error('The period that you indicated is too small to construct initial conditions')
    end
else
    period = n;
end

if isfield(opts, 'outvars')
    outvars = opts.outvars;
    if ischar(outvars)
        outvars = cellstr(outvars);
    end
    if length(invars) ~= length(outvars)
        error('The number of input and output variables is not the same')
    end
else
    outvars = invars;
end

% Initialize outputs
if ~isfield(opts, 'outfile')
    % Output to M_.endo_histval
    M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_endo_lag);
else
    % Output to a file
    o = struct();
end

% Handle all endogenous variables to be copied
for i = 1:length(invars)
    if isempty(strmatch(invars{i}, M_.endo_names))
        % Skip exogenous
        continue
    end
    s = smoothedvars.(invars{i});
    j = strmatch(invars{i}, M_.endo_names, 'exact');
    v = s((period-M_.maximum_endo_lag+1):period);% + steady_state(j);
    if ~isfield(opts, 'outfile')
        j = strmatch(outvars{i}, M_.endo_names, 'exact');
        if isempty(j)
            error(['smoother2histval: output variable ' outvars{i} ' does not exist.'])
        else
            M_.endo_histval(j, :) = v;
        end
    else
        % When saving to a file, x(-1) is in the variable called "x_"
        o.([ outvars{i} '_' ]) = v;
    end
end

% Handle auxiliary variables for lags (both on endogenous and exogenous)
for i = 1:length(M_.aux_vars)
    if ~ ismember(M_.endo_names{M_.aux_vars(i).endo_index},invars)
        if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3
            continue
        end
        if M_.aux_vars(i).type == 1
            % Endogenous
            orig_var = M_.endo_names{M_.aux_vars(i).orig_index};
        else
            % Exogenous
            orig_var = M_.exo_names{M_.aux_vars(i).orig_index};
        end
        [m, k] = ismember(orig_var, outvars);
        if m
            if ~isempty(strmatch(invars{k}, M_.endo_names))
                s = smoothedvars.(invars{k});
            else
                s = smoothedshocks.(invars{k});
            end
            l = M_.aux_vars(i).orig_lead_lag;
            if period-M_.maximum_endo_lag+1+l < 1
                error('The period that you indicated is too small to construct initial conditions')
            end
            j = M_.aux_vars(i).endo_index;
            v = s((period-M_.maximum_endo_lag+1+l):(period+l)); %+steady_state(j);
            if ~isfield(opts, 'outfile')
                M_.endo_histval(j, :) = v;
            else
                % When saving to a file, x(-2) is in the variable called "x_l2"
                lead_lag = num2str(l);
                lead_lag = regexprep(lead_lag, '-', 'l');
                o.([ orig_var '_' lead_lag ]) = v;
            end
        end
    end
end

% Finalize output
if isfield(opts, 'outfile')
    save(opts.outfile, '-struct', 'o')
end

end