File: check_mode_file.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 (163 lines) | stat: -rw-r--r-- 8,325 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
function [xparam1, hh] = check_mode_file(xparam1, hh, options_, bayestopt_)
% function [xparam1, hh] = check_mode_file(xparam1, hh, options_, bayestopt_)
% -------------------------------------------------------------------------
% Check that the provided mode_file is compatible with the current estimation settings.
% -------------------------------------------------------------------------
% INPUTS
%  o xparam1:                [vector] current vector of parameter values at the mode
%  o hh:                     [matrix] current hessian matrix at the mode
%  o options_:               [structure] information about options
%  o bayestopt_:             [structure] information about priors
% -------------------------------------------------------------------------
% OUTPUTS
%  o xparam1:                [vector] updated vector of parameter values at the mode
%  o hh:                     [matrix] updated hessian matrix at the mode
% -------------------------------------------------------------------------
% This function is called by
%  o dynare_estimation_init.m
% -------------------------------------------------------------------------

% Copyright © 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/>.

number_of_estimated_parameters = length(xparam1);
mode_file = load(options_.mode_file);
if number_of_estimated_parameters>length(mode_file.xparam1)
    % More estimated parameters than parameters in the mode_file.
    skipline()
    disp(['The ''mode_file'' ' options_.mode_file ' has been generated using another specification of the model or another model!'])
    disp(['Your file contains estimates for ' int2str(length(mode_file.xparam1)) ' parameters, while you are attempting to estimate ' int2str(number_of_estimated_parameters) ' parameters:'])
    md = []; xd = [];
    for i=1:number_of_estimated_parameters
        id = strmatch(deblank(bayestopt_.name(i,:)),mode_file.parameter_names,'exact');
        if isempty(id)
            disp(['--> Estimated parameter ' bayestopt_.name{i} ' is not present in the loaded ''mode_file'' (prior mean or initialized values will be used, if possible).'])
        else
            xd = [xd; i];
            md = [md; id];
        end
    end
    for i=1:length(mode_file.xparam1)
        id = strmatch(mode_file.parameter_names{i},bayestopt_.name,'exact');
        if isempty(id)
            disp(['--> Parameter ' mode_file.parameter_names{i} ' is not estimated according to the current mod file.'])
        end
    end
    if ~options_.mode_compute
        % The mode is not estimated.
        error('Please change the ''mode_file'' option, the list of estimated parameters or set ''mode_compute''>0.')
    else
        % The mode is estimated, the Hessian evaluated at the mode is not needed so we set values for the parameters missing in the mode file using the prior mean or initialized values.
        if ~isempty(xd)
            xparam1(xd) = mode_file.xparam1(md);
        else
            error('Please remove the ''mode_file'' option.')
        end
    end
elseif number_of_estimated_parameters<length(mode_file.xparam1)
    % Less estimated parameters than parameters in the mode_file.
    skipline()
    disp(['The ''mode_file'' ' options_.mode_file ' has been generated using another specification of the model or another model!'])
    disp(['Your file contains estimates for ' int2str(length(mode_file.xparam1)) ' parameters, while you are attempting to estimate only ' int2str(number_of_estimated_parameters) ' parameters:'])
    md = []; xd = [];
    for i=1:number_of_estimated_parameters
        id = strmatch(deblank(bayestopt_.name(i,:)),mode_file.parameter_names,'exact');
        if isempty(id)
            disp(['--> Estimated parameter ' deblank(bayestopt_.name(i,:)) ' is not present in the loaded ''mode_file'' (prior mean or initialized values will be used, if possible).'])
        else
            xd = [xd; i];
            md = [md; id];
        end
    end
    for i=1:length(mode_file.xparam1)
        id = strmatch(mode_file.parameter_names{i},bayestopt_.name,'exact');
        if isempty(id)
            disp(['--> Parameter ' mode_file.parameter_names{i} ' is not estimated according to the current mod file.'])
        end
    end
    if ~options_.mode_compute
        % The posterior mode is not estimated. If possible, fix the mode_file.
        if isequal(length(xd),number_of_estimated_parameters)
            disp('==> Fix mode_file (remove unused parameters).')
            xparam1 = mode_file.xparam1(md);
            if isfield(mode_file,'hh')
                hh = mode_file.hh(md,md);
            end
        else
            error('Please change the ''mode_file'' option, the list of estimated parameters or set ''mode_compute''>0.')
        end
    else
        % The mode is estimated, the Hessian evaluated at the mode is not needed so we set values for the parameters missing in the mode_file using the prior mean or initialized values.
        if ~isempty(xd)
            xparam1(xd) = mode_file.xparam1(md);
        else
            % None of the estimated parameters are present in the mode_file.
            error('Please remove the ''mode_file'' option.')
        end
    end
else
    % The number of declared estimated parameters match the number of parameters in the mode file.
    % Check that the parameters in the mode file and according to the current mod file are identical.
    if ~isfield(mode_file,'parameter_names')
        disp(['The ''mode_file'' ' options_.mode_file ' has been generated using an older version of Dynare. It cannot be verified if it matches the present model. Proceed at your own risk.'])
        mode_file.parameter_names=deblank(bayestopt_.name); %set names
    end
    if isequal(mode_file.parameter_names, bayestopt_.name)
        xparam1 = mode_file.xparam1;
        if isfield(mode_file,'hh')
            hh = mode_file.hh;
        end
    else
        skipline()
        disp(['The ''mode_file'' ' options_.mode_file ' has been generated using another specification of the model or another model!'])
        % Check if this is only an ordering issue or if the missing parameters can be initialized with the prior mean.
        md = []; xd = [];
        for i=1:number_of_estimated_parameters
            id = strmatch(deblank(bayestopt_.name(i,:)), mode_file.parameter_names,'exact');
            if isempty(id)
                disp(['--> Estimated parameter ' bayestopt_.name{i} ' is not present in the loaded ''mode_file''.'])
            else
                xd = [xd; i];
                md = [md; id];
            end
        end
        if ~options_.mode_compute
            % The mode is not estimated
            if isequal(length(xd), number_of_estimated_parameters)
                % This is an ordering issue.
                xparam1 = mode_file.xparam1(md);
                if isfield(mode_file,'hh')
                    hh = mode_file.hh(md,md);
                end
            else
                error('Please change the ''mode_file'' option, the list of estimated parameters or set ''mode_compute''>0.')
            end
        else
            % The mode is estimated, the Hessian evaluated at the mode is not needed so we set values for the parameters missing in the mode_file using the prior mean or initialized values.
            if ~isempty(xd)
                xparam1(xd) = mode_file.xparam1(md);
                if isfield(mode_file,'hh')
                    hh(xd,xd) = mode_file.hh(md,md);
                end
            else
                % None of the estimated parameters are present in the mode_file.
                error('Please remove the ''mode_file'' option.')
            end
        end
    end
end
skipline()